Debugging .NET 4.0 applications using SOS extension
Posted by: Suprotim Agarwal
in Category .NET Framework
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:
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.
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.
Run the application. When the breakpoint is hit, use the Immediate Window (Ctrl+Alt+I) to type the following:
If the sos.dll gets loaded successfully, you will see the following message
Let’s say we want to view all the managed threads in the process. Type the following command
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!
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.
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 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 eBook 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 .NET Standard and the upcoming C# 8.0 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!