Simultaneously debug through intermediate language (IL) and C# in Visual Studio
Asked Answered
A

3

11

I'm looking for an extension for Visual Studio where in debug mode it's possible to single step through the intermediate language beside C#.

I'm not looking for a solution to debug managed and unmanaged code.

Arlindaarline answered 13/3, 2011 at 21:37 Comment(2)
You'll have a hard time finding that. The mapping of machine code back to IL is tenuous and highly dependent on the jitter. There's more than one.Furlana
+1 for coolness of such an option if available.Actinoid
P
6

What is your purpose? Is your IL generated by C# compiler or dynamically produced at run time? If the former one, you can use a trick of re-compiling your binary through ilasm.

  1. Compile C# code as you normally would. It does not matter if it is optimized or not, but you have to specify compilation option to produce full PDB symbols.
  2. Use ildasm to convert your binary to .il file. It is option Dump in the menu.
  3. re-compile the .il file to get a new binary (and a new symbols)

    ilasm .il [/exe|/dll] /debug

  4. Now when debugging that specific assembly you will see IL code rather than C# code. You will also see a matching lines from original C# file if you select appropriate option in step 2.

For the case of dynamically generated IL, I would simply use WinDbg with SOS extension. It can dump IL and step through it, but it takes a bit to get used to.

Phosphate answered 13/3, 2011 at 22:16 Comment(2)
Can you please be more specific on option 2. what do you mean " select appropriate option"?Batts
Another way to seamlessly step from C# into IL (and back) in the vs2017 debugger is to fuse two netmodules, C# and IL, into a single assembly using the link.exe /LTCG option. I got this working great with the latest Roslyn and ILAsm (no ILDasm roundtrip required). What would be nice now would be a debugger plug-in that displays (a simulation of) the CIL execution stack as you step through the IL. Anyone know if there is such a thing?Percussive
P
2

Although not strictly a Visual Studio extension as the OP requested, there's now a maybe even better way to do this using dnSpy, a comprehensive, standalone, open-source .NET debugging tool. The tool actually does much more than just debugging; for example it allows direct editing of .NET and native (PEFile) assemblies, de-obfuscating them, browsing and modifying the raw managed and native headers, content, resources, BAML, and metadata, and more that I probably haven't discovered.

enter image description here

For the purposes of the discussion on this page, be sure to check out the IL interpreter section of the dnSpy project. Exactly as requested by the OP, this is the library that implements an IL interpreter for simulating the (pretend) execution of the IL code in parallel with the debugger's (actual) native instruction single-stepping, for the purposes of displaying the state of the (logical) IL execution stack. I believe there's some excellent x86/x64 disassembly rendering built-in to the debugger as well, if needed. Kudos to the developer of this tour-de-force app.

Percussive answered 13/1, 2019 at 21:14 Comment(0)
D
0

I don't think that an external disassembler is necessary here. When you're debugging in VS 2010 (though not Express) you can right-click on the code window and select "Go To Disassembly" to step through the IL code. Could that be what you're looking for? Read more here

Diversify answered 10/8, 2011 at 9:23 Comment(1)
This is the CPU assembly not IL.Guimar

© 2022 - 2024 — McMap. All rights reserved.