Using SOS in Visual Studio
First, you need to right mouse click on the project in solution explorer, choose properties, switch to the Debug tab, and make sure “Enable Unmanaged Code Debugging” is selected.
Second, open the Immediate window (Debug->Windows->Immediate). You can simply press shortcut keys Ctrl + Alt + I. Then type following command in the Immediate window.
.load C:\windows\Microsoft.NET\Framework\v2.0.50727\sos.dll
Third, Type “!help” for help.
!help ------------------------------------------------------------------------------- SOS is a debugger extension DLL designed to aid in the debugging of managed programs. Functions are listed by category, then roughly in order of importance. Shortcut names for popular functions are listed in parenthesis. Type "!help <functionname>" for detailed info on that function. Object Inspection Examining code and stacks ----------------------------- ----------------------------- DumpObj (do) Threads DumpArray (da) CLRStack DumpStackObjects (dso) IP2MD DumpHeap U DumpVC DumpStack GCRoot EEStack ObjSize GCInfo FinalizeQueue EHInfo PrintException (pe) COMState TraverseHeap BPMD Examining CLR data structures Diagnostic Utilities ----------------------------- ----------------------------- DumpDomain VerifyHeap EEHeap DumpLog Name2EE FindAppDomain SyncBlk SaveModule DumpMT GCHandles DumpClass GCHandleLeaks DumpMD VMMap Token2EE VMStat EEVersion ProcInfo DumpModule StopOnException (soe) ThreadPool MinidumpMode DumpAssembly DumpMethodSig Other DumpRuntimeTypes ----------------------------- DumpSig FAQ RCWCleanupList DumpIL
Some common commands are listed below:
!CLRStack !ip2md [EIP Address] !DumpIL [MethodDesc Address] !help [Command]
Command: !CLRStack
!CLRStack [-a] [-l] [-p]
CLRStack attempts to provide a true stack trace for managed code only. It is handy for clean, simple traces when debugging straightforward managed programs. The -p parameter will show arguments to the managed function. The -l parameter can be used to show information on local variables in a frame. SOS can’t retrieve local names at this time, so the output for locals is in the format = . The -a (all) parameter is a short-cut for -l and -p combined.
When you see methods with the name “[Frame:...", that indicates a transition between managed and umanaged code. You could run !IP2MD on the return addresses in the call stack to get more information on each managed method.
On IA64 and x64 platforms, Transition Frames are not displayed at this time. Especially on IA64, there is heavy optimization of parameters and locals. You can request the JIT compiler to not optimize functions in the managed app by creating a file myapp.ini (if your program is myapp.exe) in the same directory.
Put the following lines in myapp.ini and re-run:
[.NET Framework Debugging Control] GenerateTrackingInfo=1 AllowOptimize=0