Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

c# - How to debug into .NET framework source code

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...