Guido Roth
Guido Roth

Reputation: 219

Fody/Mono.Cecil: Hide Lines in debugger

I am developing a fody addin (using mono.cecil) and inject some code at the beginning of a method. I want the debugger to step over the injected code.

I found some information here: http://blogs.msdn.com/b/abhinaba/archive/2005/10/10/479016.aspx

So I tried to update the sequence point of the injected instructions to line number 0xfeefee.

I am doing this using the following code:

    public static void Inject(MethodDefinition method, List<Instruction> instructions)
    {
        var methodInstructions = method.Body.Instructions;

        int index = 0;
        var sequencePoint = method.Body.Instructions
            .Where(instruction => instruction.SequencePoint != null)
            .Select(instruction => instruction.SequencePoint)
            .FirstOrDefault();

        if (method.HasBody && sequencePoint != null && sequencePoint.Document != null)
        {
            var instruction = instructions[0];
            instruction.SequencePoint = new SequencePoint(sequencePoint.Document);
            instruction.SequencePoint.StartLine = 0xfeefee;
            instruction.SequencePoint.EndLine = 0xfeefee;
        }

        foreach (var instruction in instructions)
        {
            methodInstructions.Insert(index++, instruction);
        }

        method.Body.OptimizeMacros();
    }

This should be basically the same code as the NullGuard.Fody project uses, but it does not work. I am still getting a source not available info from visual studio when trying to debug into a method where the code was injected.

Do I need to do anything else, so that the pdb file is updated?

Upvotes: 1

Views: 352

Answers (1)

Cameron MacFarland
Cameron MacFarland

Reputation: 71906

Try remove the Where from the query to select the first sequence point.

You should only need to add your hidden sequence point if the first instruction of the method has a sequence point.

Upvotes: 1

Related Questions