Debugging .NET 4.0 applications using SOS extension

Posted by: Suprotim Agarwal , on 2/7/2011, in Category .NET Framework
Views: 60174
Abstract: The SOS (Son of Strike) debugger extension is an invaluable resource for debugging .NET 4.0 managed applications using native debuggers.The SOS extension lets you view information about code that is running inside the CLR. In this article, we will explore how to use this extension.

The SOS (Son of Strike) debugger extension  is an invaluable resource for debugging .NET 4.0 managed applications using native debuggers.The SOS extension lets you view information about code that is running inside the CLR. When you install .NET on your machine, the SOS.dll debugger extension gets installed too. Moreover every version of CLR ships with a new  SOS.dll so that the DLL can take advantage of the new capabilities of the CLR.

The location of the SOS.dll on my machine with .NET framework 4.0 installed is:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\SOS.dll

Here are a couple of exciting things you can do using the with latest SOS Debugger extension

  • detect object corruption
  • detect memory leaks
  • detect cross-generation references
  • find apartment model for each thread
  • find out which generation your object currently belongs to
  • breakdown of each generation with a view of managed heap size and free space
  • set managed code breakpoints
  • find the application domain of your object

and much much more as described over here

Note: If you have been using SOS in previous versions of .NET, remember that SOS in .NET 4.0 supports DML (Debugger Markup Language) now! Also note that with CLR 4.0, mscrowks.dll has been replaced with the clr.dll module.

Installing Debugging Tools for Windows

Before we use the SOS extension, make sure you have downloaded the latest version of Debugging Tools for Windows which is a free download package (available in 32-bit and 62-bit) and consist of a set of powerful debuggers and tools to troubleshoot your application. I have chosen the link which says Debugging Tools for Windows 32-bit Version. After downloading winsdk_web.exe, run it and choose ‘Debugging Tools for Windows’ under ‘Common Utilities’ in the wizard, as shown below.

Win 7 SDK Installation

After a couple of minutes, the Debugging Tools for Windows will get installed in the following location: C:\Program Files\Debugging Tools for Windows (x86)

Using SOS Debugger Extension in Visual Studio 2010

Before using SOS, you have to load it into the debugger by using the .load command in the Immediate window in VS 2010. Let us see how to use the SOS debugger extension in Visual Studio 2010 (works only in Pro, Premium, and Ultimate edition. Express Edition not supported)

Open a project in Visual Studio 2010 and create a breakpoint. In the Solution Explorer, right click the project > properties and in the Debug tab, make sure the ‘Enable unmanaged code debugging’ is checked.

Enabled Debuggers

Run the application. When the breakpoint is hit, use the Immediate Window (Ctrl+Alt+I) to type the following:

.load sos

If the sos.dll gets loaded successfully, you will see the following message

Load SOS

Let’s say we want to view all the managed threads in the process. Type the following command

!threads

Sos Threads

There you go. It's as simple as that to view managed threads. There are many more things you can do with this extension and we will explore them in forthcoming articles.

Note: You must be seeing a ‘PDB symbol for clr.dll not loaded’ message in the screenshot shown above. To resolve this error, go to Tools > Option > Debugging > Symbols and check the ‘Microsoft Symbol Servers’ checkbox in the Symbol file(.pdb) locations area. The error will not appear again!

Microsoft Symbol Servers

In this post, I introduced you to the SOS Debugger extension. In the future posts, we will see how to use its various commands to debug .NET apps.

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Piotr Trizy on Tuesday, February 8, 2011 6:28 AM
Good introduction to sos extension. debugging in .net 4 has certainly improved

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel