Appendix B: Runtime Debugger (CorDbg.exe)

The .NET Framework SDK includes the command-line runtime debugger named CorDbg.exe, which some developers might prefer over its Windows-based counterparts, DbgClr and the debugger that is built into Visual Studio. As with DbgClr, CorDbg does not compile applications. To use these compilers, it is first necessary to build the application so that it includes the appropriate /debug switch.

The run-time debugger allows you to perform the following tasks.

  • Start, attach to, continue, detach from, and stop a running process.
  • Display application domains, assemblies, loaded modules, classes, and global functions.
  • Step into and over both source and native instructions.
  • Show source-code lines.
  • Set the value of a variable.
  • Set the next statement to a new line.
  • Set or display breakpoints.

The full list of CorDbg options can be displayed by typing CorDbg /? from a command prompt.

For a brief walkthrough on using the run-time debugger, take a look again at the Calculator sample. From a command prompt, you can start the debugger with this program by typing CorDbg calc.exe from the directory that contains the executable file. This action starts CorDbg and displays the following lines.

(cordbg) run calc.exe
Process 1596/0x63c created.
[thread 0x624] Thread created.
234:   using (Form f = new VersioningDemo()){ //automatically calls Dispose()

**Note   **If you are not running a debug version of the runtime, you will receive several warnings about the debugger not being able to load symbols for core .NET assemblies. These warnings can be ignored.

Now execute the "pro(cessenum)" instruction:

(cordbg) pro

PID=0x49c (1596) Name=C:\...\Tutorials\Debugging\Calc\CS\calc.exe

   ID=1 AppDomainName=calc.exe*

PID=0x544 (1348) Name=C:\...\aspnet_wp.exe

   ID=3 AppDomainName=/LM/w3svc/1/Root/NetSDK/Debug-2-...

   ID=2 AppDomainName=/LM/w3svc/1/Root/NetSDK-1-...

   ID=1 AppDomainName=DefaultDomain

This shows that Calc.exe has a ProcessID of 1596. This computer is also executing an ASP.NET application with a ProcessID of 1348. If you are not running debug versions of the .NET runtime, you will likely not have the symbols installed for the runtime libraries, and you will also see a series of warnings saying those symbols cannot be loaded. Now, execute the "sh(ow)" instruction to show the source code:

(cordbg) sh
229:            }
230:     }
231:
232:     [System.STAThreadAttribute()]
233:     public static void Main(string[] args) {
234*           Form f = new VersioningDemo();
235:            f.ShowDialog();
236:            //throw(new System.Exception());  //a runtime error
237:     }
238: }
239: } 

This tells us that execution is currently halted in line 234, which is the first line in the program. At this point, you might want to try executing the following commands:

  • ap — Displays information about the application domain.
  • l — Lists the currently loaded modules, classes, and global functions.
  • p — Prints the current variables.
  • sh 50 — Shows the 50 preceding source-code lines.

Next, set a "b(reakpoint)" at line 195, which is the first line in the btnNumbersClicked routine:

(cordbg) b 195
Breakpoint #1 has bound to C:\...\Tutorials\Debugging\Calc\CS\calc.exe.
#1      C:\ ...\Tutorials\Debugging\Calc
\CS\Calc.cs:192 btnNumbersClicked+0x0(il) [active]

Finally, enter "con(t)" to continue executing the program, which displays the calculator form. Clicking one of the calculator's numeric buttons will break back into the debugger on line 195. Finally, enter "ex(it)" to exit the debugger.

For additional information on CorDbg, see the Runtime Debugger (Cordbg.exe).