Debugging the World Starting with the CLR Corneliu I. Tusnea Senior Consultant, Readify
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. Visual Studio Debugging. Attributes Debug support DebuggerStepThroughAttribute
Debugging the World Starting with the CLR Corneliu I. Tusnea Senior Consultant, Readify
E N D
Presentation Transcript
Debugging the World Starting with the CLR Corneliu I. Tusnea Senior Consultant, Readify It works on my machine! Discover, Master, Influence
Agenda • (Improving) Visual Studio Debugging • Production Debugging • Tips & Tools • Resources Discover, Master, Influence
Visual Studio Debugging • Attributes • Debug support • DebuggerStepThroughAttribute • DebuggerNonUserCodeAttribute • Debug enhancement • DebuggerDisplayAttribute • DebuggerTypeProxyAttribute • DebuggerBrowsableAttribute • DebuggerVisualizerAttribute Discover, Master, Influence
Visual Studio DebuggingDemo Attributes Discover, Master, Influence
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, Influence
Improving Visual Studio Debugging • Object Ids • Custom breakpoints • Trace points • Breakpoint Conditions Discover, Master, Influence
Visual Studio DebuggingDemo Object Ids, Breakpoints and Trace points Discover, Master, Influence
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, Influence
Production DebuggingDemo Taking memory dumps of processes Discover, Master, Influence
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, Influence
Production Debugging • Process crashes • Crash • Start-up crashes • Hang (deadlock) • Sudden death (unexpected exit) • Exceptions • First chance exceptions • Second chance exceptions Discover, Master, Influence
Production DebuggingDemo Finding the cause of a crashing process Discover, Master, Influence
Demo walkthrough Windbg <load memory dump or attach to process> .loadbysosmscorwks – load sosextentions !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, Influence
Production DebuggingDemo Recovering the code from a memory dump Discover, Master, Influence
Recovering Assemblies start end Module: 01250000 01256000ACorns_Debugging_StartupCrashTest Method Table -> lmv m*<name>* 800135c0 public class MyClass { Method Desc -> !dumpmt 80013570 private void MyFunction(stringsomeParameter) { ... 8015026f if (String.IsNullOrEmpty( someParameter )) IP -> !ip2md 801502c3 throw new ApplicationException(); ... 801504d1 } 80014290 private void MyOtherFunction() {...} } Use RetAddress from Call Stack (!clrstack) Use IP from Exception (!pe) Discover, Master, Influence
Demo walkthrough !ip2md <IP> - map instruction to method !dumpmt <method table> - show the method table lmv m*<module name>* - show details of module !savemodule <addr> <dll> - save the module Discover, Master, Influence
Production DebuggingDemo Finding crashes on start-up Discover, Master, Influence
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\<name of the exe>\Debugger • For processes set this value to allow long wait from SCM on start-up • HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ ServicesPipeTimeout (30000000) Discover, Master, Influence
GFlags • gflags.exe Discover, Master, Influence
WinDbg • Good to know tips: • sxeav break on access violation • sxeclr break on all clr exceptions • SRV*c:\..* http://msdl.microsoft.com/download/symbols • bpnative breakpoint • bpntdll!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 • !dumpobject0xxxxxxx • !dumpobjecterrorMessageText • !dumpheap -stat -min 85000 Large Object Heap Objects • !dumpdomain -stat • AdPlus (Memory dumps) • Adplus.vbs –crash –pnprocessname| –p processid • Adplus.vbs –hang –p|-pn Discover, Master, Influence
Tools and Resources • ACorns.Debugging Tools • FindDeadlock • FindStartupException • SOS Assist • SOS Extensions • http://www.stevestechspot.com/SOSEXANewDebuggingExtensionForManagedCode.aspx • DbgHelp (trigger memory dumps from .net code) • http://msdn.microsoft.com/en-us/library/ms679294(VS.85).aspx • Hawkeye • www.acorns.com.au/projects/hawkeye/ • www.codeplex.com/hawkeye/ • Good references • http://www.microsoft.com/whdc/ddk/debugging/ • http://dotnetdebug.net/category/windbg/ • http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx • http://www.codeproject.com/KB/debug/windbg_part1.aspx Discover, Master, Influence
A Readify Developer Network session By Corneliu I. Tusnea, Senior Consultant, Readify Email Address: corneliu.tusnea@readify.net RDN Website: www.readify.net/rdn Blog: http://www.acorns.com.au Discover, Master, Influence