More on debugging with SOS.DLL - enter Visual Studio

来源:互联网 发布:天下霸唱作品 知乎 编辑:程序博客网 时间:2024/06/06 07:50
I can’t get off this debugging topic at the moment.
 
To follow up briefly on the postings that I put up about WinDbg and the SOS.DLL  I wanted to add some information about Visual Studio because I think I goofed really with the last couple of postings on that topic.
 
You learn something new every day and I just learnt some stuff about Visual Studio. It turns out that Visual Studio can load up the SOS.DLL extension with consummate ease. I'd thought that this was a 2005 feature but it turns out that it’s in Visual Studio 2003 as well so that's a major bonus.
 
So, if you're keen to play with SOS.DLL you can work happily with it inside of Visual Studio and you don't have to worry so much about going and getting WinDbg unless you want some of its more advanced features.
  
How do you make this work?
 
Firstly, you need to make sure that the debugging project that you’re working in has support for unmanaged debugging. You can do this in your project properties (Configuration Properties->Debugging->Enable Unmanaged Debugging) and you can also explicitly set what type of debugging you want to do if you attach the debugger to a process through the Tools->Debug Processes menu option.
 
Note that mixing managed/unmanaged debugging does seem to slow the debugger’s operation down quite a bit.
 
Once you’ve got unmanaged debugging switched on you can work with the debugger exactly as you usually do and set breakpoints and so on.
 
At the point where you want some SOS.DLL functionality, switch to your Immediate window (Debug->Windows->Immediate).
 
Put the Immediate window into immediate mode  rather than command mode. You do this by typing;
 
immed
 
at the command prompt (you revert back to command mode by typing “>cmd”). You can spot command mode because it gives you a “>” prompt.
 
When in immediate mode type in;
 
.load sos
 
And there you go. Type in !EEVersion and a quick !ProcInfo so that you feel at home and you’re off on your extended debugging session.
 
Note that within Visual Studio, commands such as !ClrStack work on the thread that you have selected within the Threads window – i.e. exactly as you’d expect.
 
The beta version of Visual Studio 2005 also loads up SOS.DLL in the same way.
 
In V2.0 it looks like some new commands have been added and a whole bunch of addition help features are there. Doing a !Help on V2.0 gives a command list;
 
  • DumpObj (do)                      
  • DumpStackObjects (dso)            
  • DumpHeap                          
  • DumpVC                            
  • GCRoot                            
  • ObjSize                           
  • FinalizeQueue                     
  • PrintException (pe)               
  • TraverseHeap                      
  • Threads
  • CLRStack
  • IP2MD
  • U
  • DumpStack
  • EEStack
  • GCInfo
  • COMState
  • DumpDomain                        
  • EEHeap                             
  • Name2EE                           
  • SyncBlk                           
  • DumpMT                            
  • DumpClass                         
  • DumpMD                            
  • Token2EE                          
  • EEVersion                         
  • DumpModule
  • ThreadPool                        
  • DumpAssembly                      
  • DumpMethodSig                     
  • DumpRuntimeTypes
  • DumpSig                      
  • VerifyHeap
  • DumpLog
  • FindAppDomain
  • SaveModule
  • GCHandles
  • GCHandleLeaks
  • VMMap
  • VMStat
  • ProcInfo
  • FAQ    
 
You can get a (much improved) set of help information about these commands by executing;
 
!Help <command>
 
and you even get examples of usage and output.
 
Some of these commands are carried over from SOS V1.1 but a lot are new. It appears that some of the commands do not work inside of Visual Studio – the 2 that I found are as below;
 
            VMStat, VMMap
 
Enjoy.