SOS(Son of Strike)扩展调试器可以用来显示CLR数据结构的内容。SOS是作为.net framework的一部分安装的(SOS.dll),位于%windir%\Microsoft.NET\Framework\v*.*.****(版本号)\目录下。在使用SOS(加载SOS.dll)进行调试的步骤:
Step1 在Visual Studio中设置项目属性,以启动托管调试:Visual Studio打开要调试的项目,菜单->项目(Project)->Project属性->调试->启用非托管代码调试,如下图所示:
Step2 在代码中设置断点->F5启动调试,运行到断点处;
Step3 设置Symbol目录:
如果不设置Symbol目录,运行下面的命令时会提示“PDB symbol for mscorwks.dll not loaded”,这里我们只需要再设置下Symbol目录就行了:菜单->工具->选项->调试->符号,设置如下:
Step4 菜单->调试(Debug)->窗口(Windows)->即时(Immediate),在打开的“即时窗口”中输入“!load sos.dll”来加载SOS.dll,如果加载成功,则会提示“已加载扩展%windir%\Microsoft.NET\Framework\v*.*.****(版本号)\sos.dll”;
Step5 在“即时窗口”中输入“!help”可以获得调试命令列表(如下),输入"!help <command>" 可以获得该命令的详细信息.
!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
DumpRuntimeTypes Other
DumpSig -----------------------------
RCWCleanupList FAQ
DumpIL
-------------------------------------------------------------------------------
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
DumpRuntimeTypes Other
DumpSig -----------------------------
RCWCleanupList FAQ
DumpIL
安装VS2003时,也会安装SOS的帮助文档,位于目录Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers Guide\Samples\sos下;但VS2005中貌似没有安装该帮助文档,这里有更详细的文档http://msdn2.microsoft.com/en-us/library/ms954594.aspx...。我将这两份文档放在我的cnblogs上,可以从这里(https://files.cnblogs.com/happyhippy/sos.rar)下载。