Visual Studio is an extremely popular
integrated developer environment among developers. The power of this
IDE coupled with its ease of use makes it the environment of choice for
.NET developers today. As part of the Visual Studio integrated
experience comes a highly sophisticated debugger that can be used to
perform troubleshooting on a myriad of different problems, such as
using source-level debugging, script debugging, and SQL debugging. The
one area where Visual Studio debugging has lacked functionality is in
production-level debugging where we either need to work with post
mortem dump files or have access to the internals of the CLR (such as
the SOS commands). Fortunately, the Visual Studio team recognized and
addressed the latter part of the problem by making Visual Studio
compatible with the SOS debugger extension.
1. SOS Integration
To
illustrate how the SOS debugger extension can be integrated into Visual
Studio, start by creating a simple C# project (command-line application
will suffice). After the project has been created, set a breakpoint on
the first line of code and start debugging by pressing F5. Figure 1 shows the debugger after the breakpoint has been hit.
The
next step in the SOS integration process is to bring up the
intermediate window. The intermediate window is used to execute various
commands (such as variable assignments, expression evaluations, etc.)
during a debugging session. Figure 2 shows how to enable the Immediate Window.
After
the Immediate Window has been enabled, you can begin entering debugging
commands. One of the commands available at our disposal is the load command, which can be used to load the SOS debugger extension. Figure 3 illustrates the result of executing the load command.
The net result of using the load command is that Visual Studio fails by stating that unmanaged debugging support must be enabled for the project. Figure 4 illustrates the process of enabling unmanaged debugging support.
Start
by right-clicking the project in Solution Explorer followed by
selecting the Properties context menu item, which brings up the
properties page on the right side. In the properties page, select the
Debug tab and check the Enable Unmanaged Code Debugging checkbox.
After unmanaged code debugging has been enabled, you can restart the debugging session, issue the load SOS command, and start using the SOS debugger extension commands in the Immediate Window (see Figure 5).
In Figure 5, we used two SOS commands (EEVersion and DumpHeap) to illustrate what the output looks like in the Immediate Window.
Combining
the power and ease of Visual Studio with the in-depth CLR knowledge of
SOS creates a single, integrated experience for complex debugging
scenarios.