📜 ⬆️ ⬇️

Debugging .Net Framework Source Code in MS VS 2008 and More

Translation of several articles, incl. Shawn Burke ( thanks! ), ScottGu ( thanks! ), Paul Krill ( thanks! ) And John Robbins ( First great thanks! Second great thanks! )

The end result to which we are striving here is the possibility of a debugger for any IDE to go "inside" of the .Net Framework methods. I hope this post contains everything you need. The last few weeks we have done a lot of experiments and collected a lot of data on this issue. And we want to share our experience with the community.


')

Method # 1: Configure Visual Studio to debug .NET Framework source code (Visual Studio 2008 only. Theoretically 2010)


If you have any problems, please make sure you have completed all the steps first. In ACCURACY as written by us. If, however, nothing helps, pay attention to the FAQ section at the end of the document. If this does not work, write your comments, we will solve them all together.

Basic setting

To begin with, I note that if you have MS VS 2008 Express Edition installed, then it will not work for you.

1) Install Visual Studio QFE . This fix simply updates the DLLs that are part of the Visual Studio debugger, which selects the source files. All the details of the corrections read on the download page.

If you received an update error message, try inserting your DVD VS 2008 and run the EXE again. This may help to install the fix correctly.

2) Start Visual Studio and select Tools> Options> Debugging> General. If you are working on a Visual Basic Profile, you need to check the box at the bottom of the Options Dialog, “Show All Settings” before continuing.

Set the following settings:

Everything should turn out like this:

image

3) Next, select Debugging> Symbols from the properties tree. Set the source of symbols for download and the space on your hard disk where Visual Studio will cache them:
Add to the Symbol file (.pdb) location address: http://referencesource.microsoft.com/symbols
Set the cache location. Make sure that this place has read and write permissions. A great option is the place of your documents. For example, C: \ Users \ Stanislav \ VsSymbols.
Check the box: Search the above locations only whan symbols are loaded manually.

Everything, installation is made!

Debugging inside the Source Code Framework

To do this, take a simple example. Create an empty C # Windows Application project. Set the entry point to Form_Load .

image

We start solution debugging. When the program stops at the breakpoint we set, go to the Call Stack window. In the call stack, right-click on the line starting with System.Windows.Forms.dll and select Load Symbols. We hope that you have a good Internet channel. Otherwise, stock up in advance buns and tea. Files to be downloaded weigh about 10 megabytes. Please note that Visual Studio will not respond all this time (minus it in karma. A good tone is the loading indicator. What if it hangs? Then kill the process. And if it doesn’t hang? Kill - time is a pity). However, such a download is performed once and will be stored on your computer in the directory with the cache you specified above.

image

These actions will allow to load symbols from the DLL from sevrera, and you will see that all the lines denoting calls inside System.Windows.Forms.dll will turn black, i.e. affordable. Line numbers will also be available. Remember that every time you need to look at the symbols, you will need to right-click and select Load Symbols.

image

So, from this moment on, all the symbols for System.Windows.Forms.dll are loaded and now you can watch its code. You can view the code in exactly the same way as during normal debugging of your code. To do this, as usual, double-click the CallStack line or go inside the build methods for (F11) . When you try to review the code for the first time, you will be offered a license, on the basis of which you are allowed to read it, and if you agree with it, click Accept, after which the source code will be downloaded.

image

Now for all the builds in which you want to debug, just repeat all the steps described above. If you need to debug those assemblies that are not in the Call Stack, open the Modules window and download the symbols from there.

image

image

Now you can in the example above go to the code DrawRectangle on F11.

For advanced users

As a rule, during each debugging session, Visual Studio tries to load symbols for each DLL that is loaded into the process being debugged. To find information on characters, it looks at all the paths specified in Options> Debugging> Symbols. But there are projects that use a lot of DLLs for which there is no information on symbols. In these cases, the debugging process will be very long. This is the main reason why we advise you to use character loading on demand.

There is, however, a way to automatically load symbols (which essentially eliminates the “Load Symbols” step), which increases overall performance. It makes sense to install this flag only to advanced users of the system, since then you will have to return to this dialogue quite often. By the way, in order to quickly enter it, select "Symbol Settings ..." in the context menu on the image above.

The basis of this is to get all the characters, download them and store them locally. To do this, disable the "Search the above locations ..." checkbox.

After that, run your project in the debugger. Oh, by the way, sorry, I completely forgot to say. Be patient, tea and cakes. Depending on the connection, the studio will either download long or very long. Total need to download about 50 megabytes. And since All this will be cached, then this will be at the same time.

image

Once this process is complete, you can stop the debugger and remove the source with the server address and click OK:

image

After these steps, all characters will be loaded automatically, and you will be able to intervene in the call stack without early restrictions. Please note that if you are working on another project that does not contain information on loaded symbols, then repeat all these steps;)

FAQ

1) When I select “Load Symbols”, I get an “Open File” dialog box in which I specify the position of the .pdb file
This can happen in four situations:
  1. You have not configured or configured, but the server address is not correct: referencesource.microsoft.com/symbols
  2. You have selected a directory for the cache in such a way that your account does not allow Visual Studio to write or read files along this path.
  3. You are trying to load symbols for DLLs for which they do not exist.
  4. You may have a different version of the .NET Framework installed. This can happen, for example, if you have Microsoft Server 2008 Beta installed. To check this, look at the version number for the mscorlib.dll assembly. It should be 2.0.50727.1433, if not, install the RTM version of .NET Framework 3.5


image

Also check your "C: \ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE" (or the place where you installed Visual Studio) a file called symsrv.no . If this file exists, rename it to symsrv.yes .

2) When I try to debug inside the source code, I get the dialog “Source is not available for this location”
First, see item (2) in the FAQ. to make sure all the characters were loaded successfully. This can be seen by looking at the Modules window, in the character status column. If characters are loaded, check the following:
If you have configured Microsoft Symbol Server in the past, and you have loaded symbols for this DLL, which do not contain source codes. Try to specify a different cache location or delete the existing cache, then roll up the “Load Symbols” command. See also FAQ (3) for more information.
Make sure once again that you have enabled the “Enable Source Server” checkbox in the Tools> Options> Debugging> General window.
Make sure the folder that is configured to collect the character cache has read and write permissions.
If you have _NT_SYMBOL_PATH installed, it will override all these settings. For details here

3) I also use Microsoft Symbol Server to load symbols. What is the difference?

Microsoft Symbol Server will provide you with symbols without providing any information in them. This information will be deleted before publication. To use both the Reference Source path and the Micrososft Symbol Server this is the location of the Reference Source path first on the list.

image

However, if you have a Microsoft Symbol Server configured via _NT_SYMBOL_PATH, remember that _NT_SYMBOL_PATH overrides these settings.

4) Is there support for 64-bit OS?

Yes, subject to availability of 64-bit PDB versions. It is worth noting here that there are DLLs that work on various architectures. Therefore, they will need only one PDB.

5) How can I set breakpoints in Framework code?

Initially, Visual Studio requires the code to exactly match the PDB file. Although quite often PDB files are not significantly changed. For example, the author added copyright strings. But the code can still be easily debugged. Just set a breakpoint (an incomplete shaded point will appear) and specify the location of the PDB file (context menu from breakpoint, Location position ...)

image

Then check the box below:

image

After these actions, you can safely continue working.

6) Why don't features like “Go To Defenition” work?

Because this information is created on the period of working with the code, and not during the execution.

7) Why are some members or local variables not available? Why can't I go inside some methods or walk along some lines of code?

This means that the .NET Framework optimizes code for size and performance. Some of these optimizations remove some information when it is no longer needed. Debugging reflects this. However, most debugging information is still present in the session, and setting breakpoints at the beginning of the methods makes it visible. The second reason may be that some of the methods are built-in, which means that you cannot go inside these methods. But for the most part of the source code you can debug freely.

8) Why are PDB files loaded for so long?

Because there are many of them, and among them there are files under 1 megabyte.

9) Can I open and view them through the browser?

No, you get an HTTP 400 error.

Method # 2: Download from the official Microsoft website in one archive (any IDE)


To download all symbols and source codes in one archive, follow the link: http://referencesource.microsoft.com/netframework.aspx .
However, of the entire set of links provided for download, only one worked for me. Not solid.

Method # 3: .NET MASS DOWNLOADER (any IDE)


Designed for downloading all symbols and source codes. It works from the command line and is available in source code.

To download what we need, click on the link ( .NET MASS DOWNLOADER ), download it and run as follows:

And of course, this method is good when you do not have MSVS 2008, but installed, for example, MS VS 2005 or CodeGear's RAD Studio or #develop.
Setting up Visual Studio for ways # 2 and # 3

Configure Visual Studio as shown below:

image

image

image

Progg it

Source: https://habr.com/ru/post/69644/


All Articles