Sunday, August 28, 2011

DEV401 Advanced .NET Debugging

Orion Edwards
Apologies that this post is fairly nonsensical. I've put my raw NZ TechEd 2011 notes up here for my reference. I'd like to think that I'll refine them over time, but that probably won't be the case.

WinDBG with the SOS Extension from the .NET framework team can make debugging specific classes of bugs much easier. Such as:

  • Memory Leaks
  • Deadlocks
  • Race conditions
  • Dumps form production applications (where it isn't otherwise possible to attach a debugger)
Install from the Windows SDK - Select the Re-distributable Packages "Debugging Tools"
WinDBG part of Debugging Tools for Windows Install from the Windows SDK - Select the Re-distributable Packages "Debugging Tools"
It's a native "Bare metal" debugger for windows. After one person has installed it others can get it: C:\Program Files\Microsoft SDKs\Windows\v7.1\Redist\Debugging Tools for Windows WinDBG + SOS SOS Extension from the .NET framework team. Attach To Process .loadby sos clr !help !dumpheap -stat : show summary of all objects !dumpheap -type Person : show objects matching type !dumpobj 025.. () : show an individual object !do XXXX .cls : clear screen !dumparray : see array elements !da !threads : .NET threads ~ : all threads ~0s : switch to thread !clrstack : See .NET view of the call stack !eestack -EE : call stack for all threads !pe or !printexception : print exception Object Object Header (4 bytes) MethodTable Pointer Field Data MethodTable EEClass Pointer Method Definitions EEClass Parent Clas Pointer Field Definitions MethodDesc MSIL / x86

Tracking memory leak

Tracking down managed memory leaks (how to find a GC leak)
!heapstat			: where is the memory all going
!dumpheap -stat			: find large/numerous objects
!dumpheap -mt XYZ		: print objects of that type
!do mt
!gcroot  			: view garbage collector path to X

Deadlock

!syncblk			: who is holding what locks
! ip2md and !dumpil 		: decompile
MethodHeld 1 means it exists but isn't locked

Memory Dump

Write the memory of a process to a file (Need a full dump - all memory in a process to capture heap).

procdump.exe - ma deadlock.exe	: sysInternals
ADPlus
Task Manager (need !wow64exts.sw iii created by 64 bit task manager)
.dump /ma
file > open crash dump
MiniDumpWrireDump p/invoke functional for dbghelp.dll

Race conditions

AppDomain.CurrentDomain.UnhandledException

Tips

Dumps are useful for crashes
Use MiniDumpWriteDump to capture dumps
MS Recommend you create dumps from an external process. A small utility is well worth it
Control with a registry or config entry - dumps are large! (250 - 300 mb)
Can capture dumps from testers
CPU thrashing - take several dumps in quick succession
!dumpheap -stat is surprisingly nice for helping to reduce memory consumption
lm -v		: for troubleshooting "wrong dll" bugs

"SOS commands msdn"
"msdn bugslayer SOS"
Tess Ferrandez