VS2008中查看.NET源码的设置方法
Configuring Visual Studio to Debug .NET Framework Source CodeItsfinally here - the launch of the .NET Reference Source project. Thispost (hopefully!) contains everything you need to know. Ove
Configuring Visual Studio to Debug .NET Framework Source Code
It's finally here - the launch of the .NET Reference Source project. This post (hopefully!) contains everything you need to know. Over the past few weeks, we ran a pilot of this feature and collected lots of great data that helped us work through some issues and understand where people were likely to have problems.
First, though, if you have any problems, please make sure you've followed all of the steps exactly as described. If you're still having problems, please check the FAQ/Troubleshooting section at the bottom. If that doesn't work, post a comment below and I'll look into it.
BASIC SETUP
Note this functionality is not available on the Express versions of the Visual Studio 2008 products.
1) Install the Visual Studio 2008 QFE . This Hotfix just updates a DLL that's part of the Visual Studio debugger that fetches the source files, more details on the download page.
UPDATE: If you get an error installing the Hotfix , try inserting your VS 2008 DVD and then running the Hotfix EXE again. We're looking into the root cause - it's related to having a prior version of VS 2008 (e.g. Beta 2) installed on the machine. But this workaround should allow the Hotfix to install properly.
UPDATE (1/18): There were some problems with the QFE link above that have been addressed, sorry for the inconvenience, it's fixed now.
2) Start Visual Studio 2008 and bring up Tools > Options > Debugging > General. If you are running under the Visual Basic Profile , you will need to check the box on the lower left of the Options Dialog marked "Show All Settings " before continuing (other profiles won't have this option).
Set the following two settings:
- Turn OFF the "Enable Just My Code " setting
- Turn ON the "Enable Source Server Support " setting
Your settings should be as below:
3) Next, bring up the "Symbols" Page and set the symbols download URL and a cache location. Specifically, set the three settings below:
-
- Set the symbol file location to be: http://referencesource.microsoft.com/symbols
- Set a cache location. Make sure this is a location that your account has read/write access to. A good option for this is to place this path somewhere under your user hive (e.g. c:/users/sburke/symbols)
- Enable the "Search the above locations only when symbols are loaded manually" option.
When you're finished, the settings should look like the image below:
Setup is done! That's it, really!
DEBUGGING INTO FRAMEWORK SOURCE
For this simple example, we'll start with a blank C# Windows Application project, but it will work the same with a VB, Web, or WPF project. To walk through this, go ahead and create that project.
Set a breakpoint in Form_Load :
Now run your project to hit that breakpoint and go to your Call Stack window (CTRL+ALT+C ). In the Call Stack, right click a frame that starts with System.Windows.Forms.dll , and choose "Load Symbols ". This will load the symbols for the System.Windows.Forms assembly, which are about 10 megabytes, so the speed of the download will vary according to your connection speed. Note that Visual Studio may be unresponsive during this time. However, this download is a one-time cost for each assembly. The symbols (PDB) file will be cached on your machine, in the directory specified in the steps above.
This will load the symbols for the DLL from the server, and you'll see some information in the status bar to reflect this. Note that when this completes, the call frames will turn black and line numbers will be available. Note you'll need to do the Right Click -> Load Symbols step each time you launch a debugging session (but, again, the symbols will now be cached locally so they won't have to download). For more information on this, see the ADVANCED USERS section below.
You have now loaded the symbols for the Windows Forms DLL and can begin viewing the code. You can view code in any way that you normally would in a debugging session. In this case you can either Step In to the line of code above, or you can double-click one of the frames in the Call Stack Window. For this case, we'll step in (F11 ).
The first time you step into code, we'll be presented with the EULA for accessing the source code. Please take the time to read this EULA. If you agree to the terms of the EULA, hit ACCEPT, and the source will then be downloaded.
That's it! You're now debugging .NET Framework Source!
Now, for each assembly that you'd like to debug into just repeat the steps above (note you'll only see the EULA once , not for each file).
There are times when the Assembly you'd like to debug into isn't on the call stack, for example in the code below:
Before you step in to Graphics.DrawRectangle , you need to get the symbols for System.Drawing.Dll loaded. To do this, we use the Modules Window (CTRL+ALT+U ). This lists all of the modules (DLLs) loaded by the debuggee. Just find System.Drawing.DLL in this list, right click , and choose Load Symbols .
Note that once a symbol file is loaded, the path to the symbol file shows up in the "Symbol File" column.
You can now step into the Graphics.DrawRectangle code above using F11! In this case, note, you'll have to step through the PaintEventArgs.Graphics property code first.
ADVANCED USERS
Normally, each time you launch a debugging session, Visual Studio attempt to download symbols for each DLL that loads into the debuggee process. As part of this process, it asks each path specified in the Debugging Symbols dialog for the corresponding PDB. Some projects load a LOT of DLLs which won't have symbols available, so this process can significantly impact debugger startup time as this probing occurs. It is mainly for this reason we've recommended manual symbol loading in the steps above; we don't want using this feature to degrade the debugging experience across-the-board.
There is, however, a way to allow automatic symbol loading (which avoids the "Load Symbols" step) in a way that minimizes performance impact. This is for more advanced users because it requires regular trips back to this Debugging Symbols Dialog. Note you can quickly get to this dialog by choosing the "Symbol Settings... " item on the right click menus pictured in steps above form the Call Stack or Modules windows.
The key is to get all of the symbols for a given project type downloaded and cached locally, then turn off the automatic symbol downloads. This will prevent the pings at debugger startup as well.
To do this, configure your setup as above with the following difference: Uncheck the "Search from the above locations..." item on the dialog.
Now, launch your project in the debugger. This will cause all of the symbols available for the DLLs in your process to be downloaded at on-demand as the DLLs are loaded into the process. Depending on your connection speed, this could take a while (it's generally about 50MB of symbols), so it's a good idea to hit F5 then go do something else for a while. Again, these symbols are cached so this is a one-time cost. Visual Studio will likely be unresponsive during this download process.
Once that process has completed, stop the debugger, and UNCHECK the the Reference Source Server symbol location, and hit OK:
Now when you launch the debugger, symbols will load automatically and you'll be be able to step in and through call stacks normally. Note if you switch to a different project type (that has different assemblies), or have assemblies that are loaded later by your project, you can just repeat these steps to get any assemblies you don't have cached locally).
FAQ/TROUBLESHOOTING
1) Which assemblies are currently available for symbol/source loading:
- Mscorlib.DLL
- System.DLL
- System.Data.DLL
- System.Drawing.DLL
- System.Web.DLL
- System.Web.Extensions.DLL
- System.Windows.Forms.DLL
- System.XML.DLL
- WPF (UIAutomation*.dll, System.Windows.DLL, System.Printing.DLL, System.Speech.DLL, WindowsBase.DLL, WindowsFormsIntegration.DLL, Presentation*.dll, some others)
- Microsoft.VisualBasic.DLL
2) When I choose "Load Symbols" I get an Open File dialog asking for the PDB.
This can be caused by one of four situations:
- You didn't configure the symbol location URL properly from basic setup above. Ensure it's http://referencesource.microsoft.com/symbols .
- You chose a cache symbols directory that your user account doesn't have write permissions for.
- You attempted to Load Symbols for a DLL that's not available in the list above
- You have a different version of the .NET Framework on your machine - this can happen, for example, if you're running a Windows Server 2008 Beta. To check this, go to the Modules Window in Visual Studio (CTRL+ALT+U) and ensure that the version number of your mscorlib.dll is 2.0.50727.1433, as below. If not, make sure you install the RTM Version of the .NET Framework 3.5 .
- Check your "C:/Program Files/Microsoft Visual Studio 9.0/Common7/IDE" (or wherever you installed VS) for a file called symsrv.no . If that file exists, rename it to symsrv.yes , and restart Visual Studio.
3) When I try to open or step into a source code file, I get a dialog that says "Source is not available for this location" or I get an Open File dialog for the file.
First, see FAQ item (2) above to ensure the symbols for the DLL were loaded successfully. You can verify this by looking in the Modules Window under the "Symbols Status" column.
If the Symbols Status is "Symbols loaded.", check the following.
- If you have configured Microsoft Symbol Server in the past, you may have already downloaded symbols for this DLL that don't contain source information. Try specifying a different cache path or deleting your existing cache path, and then repeating the Load Symbols command. See FAQ #4 for more information on this.
- Ensure you have checked the "Enable Source Server " item on the Tools -> Options -> Debugging -> General page
- Ensure that your account has read/write access to your cache path
- If you have _NT_SYMBOL_PATH set, it will override these settings. Details here .
4) I also use Microsoft Symbol Server to download symbols. What's the difference? Can these two programs co-exist?
Microsoft Symbol Server provides symbols without any source information in them. That information has been removed (sometimes referred to as "stripped") before publishing. The symbols provided on the Reference Source Server are full symbols with debugging information.
The key to using both is to have your Reference Source path above the Symbol Server Path so that those symbols are searched/found first. As described in the ADVANCED USERS section above, you'll likely want to launch your debugger once with this configuration to get all the symbols downloaded, then uncheck both of these paths to avoid debugging launch slowdowns in the future. Also note that this may conflict in the future as more DLLs are added to the Reference Source Project. Meaning, if you've already downloaded the Symbol Server symbol, you'll need to delete that or change your cache path to get the Reference Source one (Visual Studio has no way of knowing which is which).
One final note here, if you have the Microsoft Symbol Server configured via _NT_SYMBOL_PATH, you'll need to add the Reference Source path above to that path as well - _NT_SYMBOL_PATH overrides the above settings.
5) Does this work with 64-bit?
Yes, we've also provided 64-bit versions of the PDBs. Note some DLLs work on multiple architectures, so not all of them need a separate 64-bit PDB.
6) How do I set breakpoints in Framework code?
Visual Studio requires the code to exactly match what is expected by the PDB. The source publishing process, however, makes some small updates to the code, such as pushing a standard copyright banner to the end of the source file. This changes the signature (CRC) of the code file. However, it's still easy to set a breakpoint.
Just set your breakpoint as normal (you'll see it fails with a little warning icon), then right click the breakpoint and choose "Location..."
Then check the "Allow Source to be Different" box, hit OK.
Now the breakpoint will set successfully.
If you find yourself doing this often, you can also disable source matching in general by disabling Tools->Options->Debugging: “Require source files to exactly match the original version.”
7) Why don't some features like "Go To Definition" work?
Browse database information is separate from symbol (PDB) information within the debugger, so that information is maintained by the project system when a project is compiled and is not present within the symbol files. So, unfortunately, that ability is not available here.
8) Why are some member or local variables unavailable? Why can't I step into certain functions or lines of code?
The .NET Framework bits that you have installed on your machine are “retail” bits, which means they are optimized for size and performance. Part of this optimization process removes certain information from the process when it is no longer needed. Debugging retail assemblies reflects this. However, most debugging information is still present in the session, and setting breakpoints earlier in a function often allows it to be visible. Another aspect of retail builds is that some small methods may be “inlined” which means you will not be able to step into them or set breakpoints in them. But for the most part you can step and debug as normal.
9) Why does it take so long to download some source files?
Some source files are very large - nearly 1MB - and unfortunately many of these are the common ones to download. However, most are significantly smaller and download quickly. Source files must be downloaded each time you restart Visual Studio. They are not persistently cached like symbols are.
10) Can I just download all of the code at once?
Not currently, but we are currently working on enabling this functionality in the future.
11) When I debug into VB code, for example in Microsoft.VisualBasic.dll, there is a C# style comment at the bottom, is this a bug?
Not really - we do run a post processing step when publishing the code and it adds a standard copyright banner at the bottom. The processor, at this time, isn't able to support multiple comment formats. Having it in a non-VB format doesn't affect the debugging functionality in any way.
12) I got to a source file and all that downloaded was a blank file?
This is something we've seen intermittently but have not been able to diagnose. If you see this, usually the workaround is to just restart VS, which will force the file to reload. If you observe this behavior, please use the "Email" link on the left to email me the name of the file that failed and about what time the failure occurred.
13) What happens if I download a Hotfix or a Service Pack? Will I be able to get source for that?
We've built a system that allows us to publish any number of versions of source and symbols for a given product. We haven't made any firm decisions on how often we'll publish source and are open to customer feedback on those issues. For example, it's clear that publishing source for each Service Pack makes sense, but it's unclear if we'll be able to do so for each Hotfix. Again, we're looking forward to feedback here.
In the meantime, also note that symbol files may no longer match a module if a framework DLL has been updated via a Hotfix. In those cases, the modules window will indicate that the symbols could not be downloaded for that module. Assuming a new symbol file was published, it can be downloaded and cached using “Load Symbols”.
14) Can I point a web browser at the symbols URL and download the symbols directly?
No, you'll get an HTTP 400 (Bad Request) response.
SUPPORT
If you have any other questions, please visit our new MSDN Forum on the topic: Reference Source Server Discussion .
![Logo](https://devpress.csdnimg.cn/79de2bf0b7994defa4242ef90d5513fa.jpg)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)