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屬性->調試->啟用Unmanaged 程式碼調試,如所示:
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
安裝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上,可以從這裡(http://files.cnblogs.com/happyhippy/sos.rar)下載。