views:

113

answers:

4

If I add let's say 1 line at the beggining of a method, if I set a breakpoint through Visual Studio on the first line, will it point to the first line or the second? If it will flag the wrong line, is there anything we could do when editing .exe files to ensure a regular debugging session later?

Isn't there something like setting line x to be Y? I remember seeing something like that somewhere, not sure if .NET related or not.

+3  A: 

You'll need to update the debugging symbols in the PDB file if you want the debugging experience to remain unchanged.

The best option for this I've seen is to use Mono.Cecil, as it supports (limited) modification of the debugging symbols as well as the IL.

Reed Copsey
Thanks for the answer. I am using Cecil, by chance can you point me out how to do that modification of debugging symbols through it?
devoured elysium
Check out the FAQ: http://www.mono-project.com/Cecil:FAQ From what I remember, it's possible for the assembly worker to also load the PDB file, in which case I think it does the possible modifications automatically.
Reed Copsey
+2  A: 

If you are modifying IL then the PDB files will contain outdated information. Note that there probably won't be a 1:1 between changes in IL lines to the C# line #s (eg inserting 3 IL statements won't offset the IDE breakpoint by 3 lines of C#).

You may want to separate the IL-modified portions of your code into separate methods to minimize the impact. Also, assuming you are the one doing the IL modification, you may find it convenient to switch between the C# & IL views while debugging.

You might need to muck a bit with the generated code to facilitate that. For example, if the injected IL can be in a wrapper method then you could tell the debugger to ignore it by usage of attrbiutes such as DebuggerStepThroughAttribute, DebuggerNonUserCodeAttribute, or DebuggerHiddenAttribute. (Look up the documentation for the nuances in behavior)

Addys
I know there is not a direct correspondence. What I want to do is just to "ignore" the code I am putting on the compiled exe. This is, if I insert 10 IL lines at the beggining of the method, I'd like the debugger to consider line 11th like the first one.
devoured elysium
You might need to muck a bit with the generated code to facilitate that. For example, if the injected IL can be in a wrapper method then you could tell the debugger to ignore it by usage of attrbiutes such as DebuggerStepThroughAttribute, DebuggerNonUserCodeAttribute, or DebuggerHiddenAttribute. (Look up the documentation for the nuances in behavior).
Addys
I added the suggestion to the answer itself too
Addys
+1  A: 

I expect that you get flagged as .pdb file might not match.

Joshua
A: 

How are you adding the IL? If you are doing this via profiler instrumentation (SetILFunctionBody) then you need to supply a new IL code map as well (SetILInstrumentationCodeMap) so that the debugger becomes aware of the IL modification.

I am doing it through cecil
devoured elysium