Debugging the .NET framework source code


I am sure lots of us have been in the situation where, after hours of poring through compiled source code in Reflector whilst simultaneously trying to hold the values of hundreds of conspiratorial variables in our heads, we are left thinking that it would all be so much easier if we could just set a breakpoint in the compiled code and step into it. In this way all of the power of Visual Studio debugging tools would be available to us, making the diagnostic process a more familiar and user-friendly experience.

All is not lost. The Shared Source Initiative, and specifically the Reference Source Code Center, gives developers the ability to step into .NET framework source code. Actually this ability has been around for a while but seems to receive relatively little press. It therefore seems worth writing a post so as to get a little more momentum going around the subject.

By the way, this is described in the Visual Studio literature and yet I always seem to have trouble finding it. That probably says more about my inability to use IE bookmarks but hey, none of us is perfect!

Ok, create a Visual Studio 2010 console application and overwrite Program.cs with the following:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

class Program{
static void Main(string[] args)
{
XmlReader r = XmlReader.Create(”http://helloworld”);
}
}

Go to Project-Properties-Debug and do the following:

  1. Uncheck the ‘Enable the Visual Studio hosting process’ checkbox
  2. Save the project

image_4_5DE2FE80

Now go to Tools-Options-Debugging-General and do the following:

  1. Uncheck ‘Enable Just My Code (Managed only)’
  2. Check ‘Enable .NET Framework source stepping’
  3. Check ‘Require source files to exactly match the original version’

image_7_5DE2FE80

Now go to Tools-Options-Debugging-Symbols and do the following:

  1. Check ‘Microsoft Symbol Servers’
  2. Choose a suitable directory to store the symbols
  3. Choose either to load all symbols or specified modules
    • The former is the safest approach but also the bigger performance hit
  4. Click ‘OK’

image_14_205ABBD5

Whilst the symbols are downloading from the reference server you will see something like the following:

image_18_4E480E8D

Finally, set a breakpoint on the line of code in the console app and press F5. Once the breakpoint has been hit, press F11 to step into the framework code, in this case System.Xml.dll (NB – you may have to agree to a EULA the first time you try this).

You will see something like the following:

image_22_4E480E8D

You are now able to debug framework code in the same way as your own custom code.

NB – You may experience problems with the odd variable here and here and this is most likely due to the issue described in the FAQ section of the reference source website. Additionally, there is a great MSDN forum where relevant topics are discussed. Also, not all of the source code symbols are available. You can see what is available here.

Finally I would recommend switching off this functionality when it is not needed as it is a significant performance overhead in Visual Studio.

Written by Bradley Cotier

Comments (4)

  1. Benjy says:

    Excellent article Brad.

  2. Richard F. Johnson says:

    I am having problems with your example.

    First, you did not indicate that I needed to download the .NET Source Code first, nor did it specify which componenets to download and install.  I chose ".Net" and "4".

    I'm not sure why I thought that I already had the source code, but I didn't realize that I didn't have it until I tried to step into the method.  At that point, VS asked me to supply the location of the source.  However, once I downloaded and installed the source, it no longer asked me where the source was, but rather tried to Step Over the call to XmlReader.Create().  It didn't like "http://helloworld" for a parameter.

    In Tools->Options->Debugging->General, you say to check "Require source files to exactly match the original," but the "ReadMe" file downloaded with the Reference Source says to uncheck it.

    Your instructions did not include adding the symbol file path of the newly downloaded symbol files, but after many failed attempts to step into the .NET source, I read the ReadMe file that came with the source files, and that's what it said to do.  I also set that path in the "Cache symbols in this directory:".

    I still cannot step into the source code.  I added this line (among others) as the first line inside my main function in my test code:

    if (0 < args.Length)

    I set my breakpoint on that line, but when I tried to Step Into (F11) args.Length, it just stepped over the Property.  It also stepped over "Console.WriteLine()", and "XmlReader.Create("http://helloworld&quot;);", but I still got the "WebException was unhandled" dialog, with the message "The remote name could not be resolved: 'helloworld'".

    I know I'm doing something wrong, I just don't know what.  I've been over both your instructions and those in the ReadMe file delivered with the Source Files, but I don't see what I am missing.

    Visual Studio 2010 Ultimate, Version 10.0.31118.1; Windows 7 Enterprise, 64-bit.

  3. Lukas says:

    It is not working for me. It is always displaying only "No source available".

  4. Sudesh Sawant says:

    Worked for me with Visual Studio 2010 SP1