25

How can one debug the .NET framework source code using Visual Studio 2017?

There are some questions here on stackoverflow about this topic, but even after reading all of them, I still wasn't able to make it work. I thought it would be useful to present an up-to-date, working solution about how to debug .NET framework source code.

I would like to solve it without using any external tools (e.g. dotPeek as source server).

3 Answers 3

37

First of all, I tested it using Microsoft Visual Studio Enterprise 2017, Version 15.9.7 and via .NET Framework 4.7.2. Though, I think it should work on Community edition the same way.

Steps to take:

  1. Go to Tools / Options / Debugging / General, and perform these settings:

    • check Enable .NET Framework source stepping (this will automatically disable "Enable Just My Code"; if not, do it manually)
    • uncheck Require source files to exactly match the original version
    • check Enable source server support
  2. Go to Tools / Options / Debugging / Symbols, and:

    • in the upper listbox check Microsoft Symbol Servers
    • click Empty Symbol Cache button (to make sure you will get the correct symbols)
    • select Load all modules, unless excluded radio button at the bottom
  3. Download the source of the .NET framework version your project is targeting, from the https://referencesource.microsoft.com/download.html site.

  4. Unpack the downloaded archive (zip) file to a convenient path on your PC.

  5. Debug your application; set a breakpoint to the line of .NET code you wish to debug, and step to the desired code line with the debugger.

Note: your application may start slower since it will download PDBs from the internet.

  1. Press Step Into (F11 by default). If your settings are correct, this will cause some delay (if your VS crashes (like mine did), Empty Symbol Cache again). Eventually it will ask for the sources of the given file, e.g. dictionary.cs. Two things can happen here:

    • A) It asks for the source file (.cs) in a file dialog. Go to step 7.
    • B) It says whatever.cs not found, and there is a link that says "Browse and find whatever.cs...". Click that link.
  2. Select the corresponding .cs file on your disk (you can search for the file on the OS).

Note: I had to restart VS several times until it "did not crash" while looking for sources, this is most likely a bug in VS.

  1. If you did everything correctly, you will find yourself debugging the .NET source code.

Note: Since VS saves the path you entered for the source files, you can stop debugging or restart VS; it will work next time, too. Besides, you do not have to manually select any more source files within the framework, because the VS will use the source folder you entered and will search in source files there.

3
  • 3
    One weird thing is this works for me only when debugging code from mscorlib.dll, but not from e.g. System.Core.dll. Does anyone know a solution for it? Commented Feb 20, 2019 at 18:05
  • 3
    It worths mentioning that with Jetbrains Rider IDE, debugging .NET framework code works perfectly and without any setup required. Commented Nov 9, 2020 at 8:39
  • Using Visual Studio 2022 it seems I also had to "set COMPLUS_ZapDisable=1" and enable "Prevent using precompiled images on module load", then clear symbol cache. Then I was able to step into System.Text.Json, at least. Commented Nov 16, 2023 at 14:09
4

Many people wondering why they can't step into source although they does set the checkboxes as described above. I'm, too.

Because you can extract dotnet sources to any location, Visual Studio isn't able to know about them and the reason can't be the source files itself (why Visual Studio doesn't find the files).

But some dll's are browseable, some not (through double clicking in Visual Studios stack view or context menu > goto source). This brought me to the assumption, that the .pdb itself must be the reason. If you look into a file which works (e.g. notepad), you see at beginning a list of strings with file pathes (source files). In files, which doesn't work, the files starting immediatelly with binary data.

For some reason microsoft doesn't create her .pdb's with full debug information in every build process. But why not - good question! g

In short: you have to search a dll version of your file (which you like to debug) which contains FULL DEBUG INFORMATION. This is also the reason why context menu disables "goto source". I'm replacing this file temporary in global assembly cache for time of debug. This works for me.

Here an example of PresentationFramework.dll - 4.0.30319.298 => pdb size: 1219 KB - 4.0.30319.18408 => pdb size: 15.562 KB

Perhabs somebody can create a public database (wiki), which everyone can add files and versions for which full debug information are available?

2

(If you are like me and after following all steps you still can't step into code...your PDBs downloaded from Microsoft are wrong, try this)

Using JetBrains dotPeek as the symbol server worked for me. (4.6.2 framework) (I did everything mentioned in this thread and many more threads, and nothing worked)

https://hmemcpy.com/2014/07/how-to-debug-anything-with-visual-studio-and-jetbrains-dotpeek-v1-2/

JetBrains dotPeek decompiles your actual .NET DLLs, then hosts a symbol server that you download symbols from in Visual Studio. After a pretty slow download, then a restart of VS, I was able to breakpoint and step into the code.

You can find the path to your .NET DLLs in the "Modules" window when debugging in VS. Enter this into dotPeek. Then Host Symbol server in dotPeek. Then add http://localhost:33417 as your symbol server in VS symbol settings. then load those symbols. it takes a minute and a VS restart, but works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.