Debug .NET Source Code

Michael Schwarz on Wednesday, December 19, 2007

Scott Guthrie announced [1] some weeks ago that Microsoft will offer the source code of the .NET framework. The source code will be released under the Microsoft Reference License [2] (MS-RL) and includes all comments which is very useful.

The last days I had a more deeper look inside. First I tried to create a small ConsoleApplication1 and debug the Console class:

image

Well, pressing F5 will stop at the breakpoint, but F11 doesn't open the Console.cs which contains the source code of the Console class. To get the .NET source code for System.Console you need to load the symbols for the mscorlib.dll by right-clicking on the Call Stack and select Load Symbols.

The Call Stack will then change from gray line for mscorlib.dll to black:

call stack before loading symbols

call stack after loading symbols

After loading the symbols for mscorlib.dll you are able to press F11 do step into the Console class. First Visual Studio will display some license dialogs that you have to accept... and the you will see the Console.cs and are able to debug each line.

image

Upps, I modified the Console.cs (I see the yellow indicator on the left side), but you are not able to compile a new .NET version, of course.

While extending the very simple debug test I tried to debug the XmlDocument class. I added some lines to load an XML fragment:

image

I started the debugger by pressing F5 and stopped at the breakpoint. Having a look at the call stack I couldn't find the System.Xml.dll, of course. But how can I debug the XmlDocument class now?

What you have to do is loading the symbols for System.Xml.dll. This can be done by opening the Modules window (Debug -> Windows -> Modules or by pressing Ctrl+D, M). In the Modules window you will find the System.Xml.dll, right-click on it and select Load Symbols.

image

Now you are able to debug the XmlDocument by pressing F11 while in debug mode:

image