SOS - Debug Extensions for Managed Code

One extremely useful feature of the Shared Source CLI (SSCLI) execution engine is that it exposes runtime entry points for use from debuggers. This debugging facility is exposed on Microsoft® Windows® as a DLL that can be used as a debugger extension from within Microsoft Platformm SDK debugging tools (NTSD or WinDBG). It can also be used from gdb on FreeBSD.

SOS on Windows

Using either NTSD or WinDBG (which you can get from the Microsoft Debugging Tools page at microsoft.com), SOS can be loaded as a debugger extension. While the debugging facilities themselves are an integral part of the execution engine, the code that glues it into the debuggers is implemented by a wrapper DLL named sos.dll. You shouldn't need to load the extension manually, since the script that sets up the SSCLI build environment also sets the _NT_DEBUGGER_EXTENSION_PATH environment variable for you. Because the extension uses the SSCLI execution engine, much of which is demand loaded, you cannot access its commands or data display capabilities until after the managed executable with which you plan to work is running (or has crashed--SOS is also useful for postmortems). This means that you will typically begin a debugging session by setting a breakpoint that will be hit after the execution engine has started.

For the managed "hello world" program that can be found in the sscli/samples/hello directory, for example, you might decide to place a breakpoint at the call to the WriteLine API within the PAL. This API will be called from System.Console.WriteLine, and once you've gotten to this point in the execution of the program, most of the CLI should be ready and waiting for debugging.

The following are commands that should work from either NTSD or WinDBG. The debugger prompt is represented as 0:000>

From a command shell in which env.bat has been run, launch hello.exe using the clix loader:

cd %TARGETCOMPLUS%
ntsd clix dump/hello.exe

Set a breakpoint and start executing the program:

0:000>bp rotor_pal!pal_writefile
0:000>g
Once you've hit the breakpoint, try typing:

0:000>!sos.help

This command should load the extension DLL and display a summary of the extension's available commands. In this list, there are several useful commands that can act as starting points into the managed code. DumpStack and DumpStackObjects both walk the stack. In the case of DumpStack, the command produces a voluminous raw dump of the stack, from which methods descriptors [MethodDesc] can be harvested. In the case of DumpStackObjects, the stack is scoured for managed object instances, which are listed by address and type.

0:000>!sos.dumpstack
0:000>!sos.dumpstackobjects

Given a MethodDesc, you can get much more information. For example, the DumpMD command will print out information about the method such as its name, its class, the address of its method table, the metadata token, and other information. Using this data, other commands can be used to further examine the state of the execution engine, such as DumpMT (which will dump the object's method table) and DumpClass (which will dump the class associated with the object).

0:000>!sos.dumpmd 
0:000>!sos.dumpclass 

Perhaps the most interesting and useful thing you can do with a dump of the MethodDesc is to see the code produced by the JIT compiler. The MethodVA that is found in the dump is actually the address of this code, and by using the "u" command against this address (or against a MethodDesc directly), you can view the instruction stream. Also interesting in this context is the sos ip2md command, which will turn a method address into a MethodDesc. Note that this command doesn't work on methods that are actually calls to stubs - in this case you will likely see a jmp instruction, the argument of which may very well be JIT compiled code that can be passed to the ip2md command.

Given the address of an object, you can call DumpObj, which will give you the object's method tables, as well as its fields and their names, types, and offsets. Also contained in an object dump are addresses to AppDomains, which can be examined with the DumpDomain command.

0:000>!sos.dumpobj 
0:000>!sos.dumpdomain 

A couple of additional commands are worth pointing out: the stack and heaps can be examined by using the EEStack and EEHeap commands, and the state of managed threads and the threadpool is available through the Threads and the ThreadPool commands. Enjoy!

Note: the information above assumes you are using the SOS extension on Windows XP.  There is a known issue attempting to use SOS on Windows 2000 that will cause the following error:

The procedure entry point SymGetTypeInfo could not be located in the dynamic 
link library DBGHELP.dll.

The fix for this is to go to www.microsoft.com/ddk/debugging/default.asp and download the latest version of the Microsoft debugging tools for Windows.

SOS on FreeBSD

The gdb debugger that ships with FreeBSD 4.5 appears to have some issues when loading extension libraries.  It is recommended that you use later versions of gdb if you want to load the SOS extension library.

SOS can be used from gdb on FreeBSD, by inserting the following into your .gdbinit file:

define sos
call (void)SOS("$arg0")
echo \n
end
Type "sos Help" to see the extension's documentation.  Also note that the command set is case-sensitive when used on FreeBSD.

Copyright (c) 2002 Microsoft Corporation