Did you know that you can use SOS from within Visual Studio 2005, not just from WinDbg?
It has been previously discussed in a couple of places but most people don’t know it is possible to do that.

“Why should I use the SOS extension from within Visual Studio?”, you are probably asking.
The answer is quite simple.

Visual Studio is a rich debugger that shows us most of the information we want to know but it doesn’t show us a few important things like the actual size of an object and all of its references and who is referencing our object. While these things are not straight forward in Visual Studio, they are very easy to accomplish in SOS. For example, you can run the following command in SOS:

  • !objsize - to find out the actual size of the object and all of its referenced objects
  • !gcroot - to find who is referencing our object
  • !dumpheap - to see what’s all the stuff that we have on the heap. By using the -gen [generation number] parameter with !dumpheap, we can see all the objects that are currently in that generation
  • !SyncBlk - find out which managed locks are locked at the moment (I’ve talked about it before)

So, how do we actually do that?

Well, the first thing you need to do is enable “unmanaged debugging” by going to the “Project Properties” -> “Debugging” -> “Enable Unmanaged Debugging”.

After doing that, you can stop somewhere in your program, open the “Immediate” window (Debug -> Windows -> Immediate) and type:

.load sos

This will load the SOS extension. From there on you have most of the commands of the SOS extension at your disposal.
You might need to set the symbols path by calling .sympath, just like you would do in WinDbg.

An added bonus to this whole thing is that it also works on Visual Studio 2003, so you get to use it even with .NET Framework 1.1.

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • co.mments
  • del.icio.us
  • digg
  • Ma.gnolia
  • NewsVine
  • Reddit
  • TailRank
  • YahooMyWeb

4 Responses to “Loading and using SOS from within Visual Studio”

  1. Anonymous says:

    Can this be done with VS 2003 too?

  2. Eran Sandler says:

    Yes. You can use SOS with Visual Studio 2003.

    Just make sure that when you load the SOS DLL you are either using the one supplied with the .NET Framework (v1.1 of course) or the one that comes in any of the versions of WinDbg (which usually contains bug fixes and additional functionality).

  3. A small tutorial on how to figure out the .NET Framework code » Advanced .NET Debugging says:

    [...] He also shows how to load our favorite WinDbg Extension - SOS - into Visual Studio and use it while debugging as well as use Reflector to disassemble the framework’s code into readable C# or VB.NET code. I also briefly talked about loading SOS into Visual Studio a while back and mentioned the use of Reflector all over the place (because its such a great and useful utility!). [...]

  4. SOS.dll for .NET Framework 3.0 / 3.5 » Advanced .NET Debugging says:

    [...] located at Microsoft.NETFrameworkv2.0.50727 with either Visual Studio (as I’ve explained on this post) or with [...]

Leave a Reply

Login Method

OpenID

Anonymous