31

Is there any IL level debugger in form of a VS plugin or standalone application?

Visual studio’s debugger is great, but it allows you to debug on either HLL code level or assembly language, you can’t debug IL. It seems that in some situations it would be useful to have an opportunity to debug at IL level.

In particular it might be helpful when debugging a problem in the code that you don't have the source of.

It is arguable if it is actually useful to debug IL when you don't have the source, but anyway.

Stu
  • 15,675
  • 4
  • 43
  • 74
axk
  • 5,316
  • 12
  • 58
  • 96

5 Answers5

25

The best way to do this is to use ILDASM to disassemble the managed binary, which will generate the IL instructions. Then recompile that IL source code in debug mode using ILASM, when you fire up the Visual Studio debugger you will be able to step through the raw IL.

  1. ildasm foo.exe /OUT=foo.exe.il /SOURCE
  2. ilasm foo.exe.il /DEBUG

I've written a blog post about this topic at: How to debug Compiler Generated code.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Chris Smith
  • 18,244
  • 13
  • 59
  • 81
3

Here's the .BAT file that I use to debug IL assembler in Visual Studio. The created .IL.IL file contains the original source code lines and your generated IL assembler lines but does not show jitted machine code. I named the batch file ILDEB.BAT and is invoked as "ILDEB mypgm". I use the IL assembler directive "break" to force Visual Studio debugger to breakpoint when hit.

for /f "tokens=1 delims=." %%1 in ("%1") do set NAME_ONLY=%%1
@erase/q %NAME_ONLY%.il.il
@if not exist %NAME_ONLY%.dll goto quit
ildasm /out:%NAME_ONLY%.il.il /source /nobar %NAME_ONLY%.dll
@if not exist %NAME_ONLY%.il.il goto quit
ilasm /dll /debug /out=%NAME_ONLY%.dll %NAME_ONLY%.il.il
@if not exist %NAME_ONLY%.dll goto quit
peverify %NAME_ONLY%.dll
:quit
BSalita
  • 8,420
  • 10
  • 51
  • 68
  • Do you run this in a post build step in your visual studio environment? – Shahzad Qureshi Apr 24 '15 at 22:07
  • 1
    Currently I invoke from the command line. However, I don't see any technical reason why it wouldn't work. IIRC, I already tested in a post build step and it works as expected. – BSalita Apr 25 '15 at 04:09
  • 2
    You can replace `%NAME_ONLY%` with `%~dpn1` and get rid of the first line – Adassko Apr 29 '16 at 20:47
1

Debug Companion VS plugin seem to be exactly what I was looking for, except that it won't see library project in my solution. Only when I added a console win application to the solution did something appear in that list of projects.

The problem with the decompile/compile approach for me was that the code I was debugging wasn't my code. I could have decompiled it anyway but I think there's no way to sign that recompiled assembly so that it gets loaded instead of the original one.

With the particular problem I had it turned out that it was enough to just debug it on the assembly language level and get the call stack of the method which was throwing the exception and the parameters with which the method was called.

axk
  • 5,316
  • 12
  • 58
  • 96
0

ISTR there's a debugger plug-in for Reflector.

Not used it myself, though I have used TestDriven.net to debug a 3rd-party assembly with the aid of Reflector:

weblogs.asp.net/nunitaddin

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
Will Dean
  • 39,055
  • 11
  • 90
  • 118
0

Here is an article about IL debugging. It says you can't do it and then talks about ways to do it. There is also some info in the comments about doing it also.

bruceatk
  • 5,118
  • 2
  • 26
  • 36