Discover, Master, InfluenceSlide 1 Debugging the World Starting with the CLR Corneliu I. Tusnea Senior Consultant, Readify It works on my machine!
Agenda (Improving) Visual Studio Debugging Production Debugging Tips & Tools Resources Discover, Master, InfluenceSlide 2
Visual Studio Debugging Attributes – Debug support DebuggerStepThroughAttribute DebuggerNonUserCodeAttribute – Debug enhancement DebuggerDisplayAttribute DebuggerTypeProxyAttribute DebuggerBrowsableAttribute DebuggerVisualizerAttribute Discover, Master, InfluenceSlide 3
Visual Studio Debugging Demo Attributes Discover, Master, InfluenceSlide 4
Sample Source Code [DebuggerDisplayAttribute( "Id={id} Name={firstName, nq} {lastName, nq}")] public class Customer { [DebuggerBrowsable(DebuggerBrowsableState.Never)] private int id; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private string firstName; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private string lastName; Discover, Master, InfluenceSlide 5
Improving Visual Studio Debugging Object Ids Custom breakpoints – Trace points – Breakpoint Conditions Discover, Master, InfluenceSlide 6
Visual Studio Debugging Demo Object Ids, Breakpoints and Trace points Discover, Master, InfluenceSlide 7
Production Debugging Memory dumps – Full memory dump – Mini memory dump Process crashes Start-up crashes Recovering code – Full memory dump – Mini memory dump Discover, Master, InfluenceSlide 8
Production Debugging Demo Taking memory dumps of processes Discover, Master, InfluenceSlide 9
Demo Walkthrough Vista/W2k8: - Task Manager - Right-click process - “Create Dump File” Memory dump on crash/exception: - adplus –crash –quiet –pn w3wp.exe Memory dump now: - adplus –hang –quiet –pn w3wp.exe Discover, Master, InfluenceSlide 10
Production Debugging Process crashes – Crash – Start-up crashes – Hang (deadlock) – Sudden death (unexpected exit) Exceptions First chance exceptions Second chance exceptions Discover, Master, InfluenceSlide 11
Production Debugging Demo Finding the cause of a crashing process Discover, Master, InfluenceSlide 12
Demo walkthrough Windbg.loadby sos mscorwks – load sos extentions !threads – check running threads ~xs – switch to thread id (x = thread id) !pe – print thread exception !dso – dump stack objects !do – dump object with address Discover, Master, InfluenceSlide 13
Production Debugging Demo Recovering the code from a memory dump Discover, Master, InfluenceSlide 14
Recovering Assemblies Discover, Master, InfluenceSlide 15 start endModule: ACorns_Debugging_StartupCrashTest Method Table -> lmv m* * c0public class MyClass { Method Desc -> !dumpmt private void MyFunction(string someParameter) { fif (String.IsNullOrEmpty( someParameter )) IP -> !ip2md c3throw new ApplicationException(); d1 } private void MyOtherFunction() {...} } Use RetAddress from Call Stack (!clrstack) Use IP from Exception (!pe)
Demo walkthrough !ip2md - map instruction to method !dumpmt - show the method table lmv m* * - show details of module !savemodule - save the module Discover, Master, InfluenceSlide 16
Production Debugging Demo Finding crashes on start-up Discover, Master, InfluenceSlide 17
Start-up Crashes Start-up Crashes in Windows Services – Gflags.exe – Service has to be able to interact with desktop – Set Debugger for your ImageFile to windbg.exe Image file is the executable name This will modify the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT \ CurrentVersion\Image File Execution Options\ \Debugger – For processes set this value to allow long wait from SCM on start-up HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ ServicesPipeTimeout ( ) Discover, Master, InfluenceSlide 18
GFlags Discover, Master, InfluenceSlide 19 gflags.exe
WinDbg & SOS Reference Run control – Ctrl + Break – F5 or g –.lastevent –.detach – F6 attach –.help.hh !help.NET Debug Extension: SOS –.load c:\...sos.dll –.loadby sos mscorwks Memory Dumps –.dump c:\.... Mini dump –.dump /ma c:\... Full dump Exceptions – sxe clr stop on CLR exceptions – sxi clr ignore exception – sxn clr notify but continue – !printexception (or.ecxr for native) – sx show exception types – !analyze –v analyse exception – !dae Discover, Master, InfluenceSlide 20
WinDbg & SOS Reference Control – bpmd module method set breakpoint – KB callstack details Threads – ~ unmanaged threads – ~##s switch thread – !threads managed Call stack – !clrstack (–a stack + parameters) – !dumpstack managed + unmanaged stack – ~*e!clrstack show CLR stack for all threads Discover, Master, InfluenceSlide 21
WinDbg & SOS Reference Heap – !dumpstackbobjects – !dumpheap heap info – !dumpheap -mt – !dumpheap –type type heap for a specific type – !dumpheap –type type -stat heap statistics for a type – !do address – !dumparray address (-details can help) – !gcroot address – !syncblk managed locks – !gcref – !objsize address Large Heap – !eeheap –gc – !dumpheap start end Discover, Master, InfluenceSlide 22
WinDbg Good to know tips: – sxe av break on access violation – sxe clr break on all clr exceptions – SRV*c:\..* – bp native breakpoint bp ntdll!zwterminateprocess breakpoint when process is terminated – sxe -c "!pe;!clrstack;gc" clr stop, display exception, dump stack, go – !dumpheap -type CompilerError asp.net compilation errors !dumpobject 0xxxxxxx !dumpobject errorMessageText – !dumpheap -stat -min Large Object Heap Objects – !dumpdomain -stat AdPlus (Memory dumps) – Adplus.vbs –crash –pn processname | –p processid – Adplus.vbs –hang –p|-pn Discover, Master, InfluenceSlide 23
Tools and Resources ACorns.Debugging Tools – FindDeadlock – FindStartupException SOS Assist SOS Extensions – DbgHelp (trigger memory dumps from.net code) – Hawkeye – – Good references – – – instructions.aspx instructions.aspx – Discover, Master, InfluenceSlide 24
Discover, Master, InfluenceSlide 25 A Readify Developer Network session By Corneliu I. Tusnea, Senior Consultant, Readify Address: RDN Website: Blog: