Debugging Common Intermediate Language

One frequent query from new .NET developers is how to view and debug Common Intermediate Language (CIL). The Source window only allows you to step through source code, and the Disassembly window only shows you the source code together with the processor-native code. Although your .exe or .dll contains the CIL, it seems to be impossible to get at it in any useful way.

There is a neat trick that allows you to both view and step through CIL. It's based on using the CIL disassembler utility to dissect your executable into CIL source and then rebuilding the executable again so that the CIL code is bound to the VB .NET source. This may sound a little complicated, but the command-line steps are simple:

  1. VBC MyProgram.vb /debug:full /optimize- /out: MyProgram.exe

  2. ILDASM MyProgram.exe /source /out:

  3. ILASM /debug /out: MyTest.exe

The first step builds your executable as normal, in full debug mode, and without code optimization. The second step disassembles the resulting binary into a CIL source file ”the /source flag adds the original VB .NET source code as CIL comments. The final step builds a new executable from the CIL source, once again in debug mode.

Having completed these command-line compilation steps, load the original project into Visual Studio. In my case this was a console application called DoubleTrouble.sln containing a single project called DoubleTrouble that, in turn , contained a source code file called Module1.vb. Choose the Add Existing Item option from the File menu, change the files filter to be "All files," and select the source code file that was produced by the Ildasm step mentioned previously (the one with a .il suffix). Double-click this CIL file in Solution Explorer to see it displayed in Visual Studio's Source window. If you scroll down past the initial code preamble, you should see your VB .NET source code shown as comments (prefixed with //) and the CIL statements shown as executable code. Figure 4-13 shows how this looks with my selected project.

click to expand
Figure 4-13: Single-stepping and debugging CIL code with VB .NET source

Debugging the new executable that you've just created is simple. First, you place a breakpoint on one of the CIL statements shown in the Source window. Then you need to tell Visual Studio to launch your new executable. To do this, you should right-click your project in Solution Explorer and use the option under Properties Configuration Properties Debugging Start external program to specify the executable that you created in the final command-line step mentioned previously. Now when you press F5, the breakpoint that you set should be hit. If you then jump to the Disassembly window, you can see the CIL code together with its corresponding native code. This is an instructive way of learning about CIL and how it functions.

Comprehensive VB .NET Debugging
Comprehensive VB .NET Debugging
ISBN: 1590590503
EAN: 2147483647
Year: 2003
Pages: 160
Authors: Mark Pearce © 2008-2017.
If you may any questions please contact us: