Debugging .NET 4.0 applications using SOS extension

Posted by: Suprotim Agarwal , on 2/7/2011, in Category .NET Framework
Views: 92119
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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