.Net Debugging Techniques

8,056 views

Published on

Advanced .Net Debugging of native (unmanaged) and Managed CLR code using NTSD, SOS, SosEx, KD.

  • Be the first to comment

.Net Debugging Techniques

  1. 1. .NET Debugging Techniques Bala Subra
  2. 3. Software Bugs are Expensive <ul><li>Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian Kernighan </li></ul>
  3. 4. Why Debugging? <ul><li>We need Reliable Software </li></ul><ul><li>Users Choose Reliability over Price </li></ul><ul><li>“ Software bugs or errors are so prevalent and so detrimental that they cost the US economy an estimated $59 billion annually, or about 0.6 percent of the GDP.” </li></ul><ul><ul><li>www.nist.gov/public_affairs/releases/n02-10.htm </li></ul></ul>
  4. 5. Most Commonly Found Defects <ul><li>http://scan.coverity.com/report/Coverity_White_Paper-Scan_Open_Source_Report_2009.pdf </li></ul>
  5. 6. Technical Requirements <ul><li>Understand unmanaged (native) vs. Managed debugging. </li></ul><ul><li>Explore production-environment debugging. </li></ul><ul><li>Deep-dive into CLR fundamentals. </li></ul><ul><li>Understand .NET memory management. </li></ul><ul><li>Debug hangs, crashes, and memory leaks. </li></ul>
  6. 7. Agenda <ul><li>Importance of debugging </li></ul><ul><li>Available Tools </li></ul><ul><li>Basic Tasks and Walkthroughs </li></ul><ul><li>Postmortem Debugging </li></ul>
  7. 8. Common Traits for good debuggers <ul><li>Willingness to venture outside of your &quot;own&quot; code </li></ul><ul><li>Curiosity </li></ul><ul><li>Patience </li></ul><ul><li>Treat dependent code as just that - code </li></ul><ul><li>Ability to see patterns </li></ul>
  8. 9. Importance of debugging <ul><li>Perfect code is an Illusion </li></ul><ul><li>Legacy Code </li></ul><ul><li>Deeper Understanding </li></ul><ul><li>Helps you learn & write better code in the future </li></ul>
  9. 10. Debugging Basics <ul><li>What are you trying to find and fix? </li></ul><ul><ul><li>Two main types of code errors </li></ul></ul><ul><ul><ul><li>Syntax: Compiler catches most if not all of these for you. </li></ul></ul></ul><ul><ul><ul><li>Semantic or logical: Syntactically correct yet program may “crash and burn” at run-time! </li></ul></ul></ul><ul><li>Autos </li></ul><ul><li>Locals </li></ul><ul><li>Watch </li></ul><ul><li>Call Stack </li></ul><ul><li>Command Window </li></ul><ul><li>QuickWatch Dialog </li></ul><ul><li>Breakpoints window </li></ul><ul><li>Threads </li></ul><ul><li>Modules </li></ul><ul><li>Processes </li></ul><ul><li>Memory </li></ul><ul><li>Disassembly </li></ul><ul><li>Registers </li></ul>
  10. 11. Execution Control: Breakpoints <ul><li>Stepping through your code </li></ul><ul><ul><li>Starting / Stopping </li></ul></ul><ul><ul><li>Breaking </li></ul></ul><ul><ul><li>Stepping through your application </li></ul></ul><ul><ul><ul><li>(F10, F11 or Toolbar buttons) </li></ul></ul></ul><ul><ul><li>Run to a specific location </li></ul></ul><ul><ul><ul><li>Run To Cursor (right-click menu) </li></ul></ul></ul><ul><li>Situations under which breakpoints are not feasible </li></ul><ul><ul><li>Timing sensitive issues </li></ul></ul><ul><ul><li>Breakpoint triggers too often </li></ul></ul><ul><ul><li>Live Debug not possible </li></ul></ul><ul><ul><li>Debugging production systems </li></ul></ul>
  11. 12. Tools available for Debugging .NET <ul><li>Visual Studio 2008 (& soon 2010) </li></ul><ul><ul><li>2010: </li></ul></ul><ul><ul><ul><li>Historical Debugging </li></ul></ul></ul><ul><ul><ul><li>Visualizations for locks, threads </li></ul></ul></ul><ul><li>CorDBG / MDbg </li></ul><ul><li>Debugging Tools for Windows </li></ul><ul><ul><li>Focus for Today </li></ul></ul><ul><ul><li>Used by everybody for everybody </li></ul></ul>
  12. 13. Visual Studio 2008: Debugger Tips and Tricks <ul><li>Execution Control </li></ul><ul><li>Breakpoints </li></ul><ul><li>Symbols </li></ul><ul><li>Multi-Threaded debugging </li></ul><ul><li>Post Mortem Debugging </li></ul><ul><li>Behind the Debugger Magic </li></ul>
  13. 14. VS Debugger Overview <ul><li>Debugs many different code Environments </li></ul><ul><ul><li>Native Windows </li></ul></ul><ul><ul><ul><li>X86 </li></ul></ul></ul><ul><ul><ul><li>X64 </li></ul></ul></ul><ul><ul><ul><li>IA64 </li></ul></ul></ul><ul><ul><li>Managed Code </li></ul></ul><ul><ul><ul><li>Windows (32 & 64 bit) </li></ul></ul></ul><ul><ul><ul><li>SQLCLR </li></ul></ul></ul><ul><ul><li>Script </li></ul></ul><ul><ul><li>T-SQL </li></ul></ul><ul><ul><li>Native Device Programs </li></ul></ul>
  14. 15. VS Debugger Architecture VSDebug Package SDM CPDE (Managed) NatDbgDE (Native) Your Engine Here Your EE Here http://msdn.microsoft.com/en-us/library/bb161718.aspx
  15. 16. Execution Control <ul><li>Step Filtering for Managed Code </li></ul><ul><ul><li>Adds support for automatically stepping over simple properties </li></ul></ul><ul><ul><li>Right click to “Step into Specific”: pick Step into Target </li></ul></ul><ul><ul><li>Switch off: How? </li></ul></ul>
  16. 17. Breakpoints <ul><li>Tracepoints </li></ul><ul><ul><li>Print a variety of program state types without stopping </li></ul></ul><ul><ul><ul><li>Program Location, including Stack </li></ul></ul></ul><ul><ul><ul><li>Expressions (including @clk in native for quick timing) </li></ul></ul></ul><ul><ul><ul><li>Thread Info </li></ul></ul></ul><ul><ul><ul><li><Your Message Here> </li></ul></ul></ul><ul><ul><li>Run a Macro </li></ul></ul><ul><ul><ul><li>Useful on stop on certain condition that is difficult to express as a ‘Watch window’ expression. For eg. “Stop if this method is in Call Stack” </li></ul></ul></ul>
  17. 18. Visual Studio 2010 <ul><li>Tagging </li></ul><ul><li>Filtering </li></ul><ul><li>Import from others & Export Breakpoints </li></ul>
  18. 19. Symbols <ul><li>Ensure Symbols are switched on in Final/Retail/Optimized Builds </li></ul><ul><li>Archive Symbols using Symbol Server </li></ul><ul><li>VS 2010 </li></ul><ul><ul><li>Team Build support for Symbol Server </li></ul></ul><ul><ul><li>Add Symbol & Source indexing into the Build’s Workflow </li></ul></ul>
  19. 20. Symbols: Reference Source Support
  20. 21. Symbols: Visual Studio Options
  21. 22. Symbol Loading Internals <ul><li>VS Never loads Mismatched symbols </li></ul><ul><li>Path Plan: </li></ul><ul><ul><li>Where the EXE think it is </li></ul></ul><ul><ul><li>On the path we create from the “Symbols” Dialog </li></ul></ul><ul><ul><li>On the path at HKLM/HKCU </li></ul></ul><ul><ul><ul><li>SoftwareMicrosoftVisualStudioMSPDBSymbolSearchPath </li></ul></ul></ul><ul><ul><li>On the path @ Any of these: </li></ul></ul><ul><ul><ul><li>_NT_ALT_Symbol_Path </li></ul></ul></ul><ul><ul><ul><li>_NT_Symbol_Path </li></ul></ul></ul><ul><ul><ul><li>SystemRoot </li></ul></ul></ul>
  22. 23. Threads <ul><li>Thread Categories </li></ul><ul><li>Flagging Threads in the List (for tracking) </li></ul><ul><li>Using Breakpoint Filters </li></ul><ul><li>Freezing & Thawing </li></ul><ul><li>Stack Tips </li></ul><ul><li>Naming Threads </li></ul><ul><ul><li>Managed: Thread.Name </li></ul></ul><ul><ul><li>Native: Use the SetThreadName execution wrapper </li></ul></ul><ul><ul><li>http://blogs.msdn.com/stevejs/archive/2005/12/19/505815.aspx </li></ul></ul>
  23. 24. Post Mortem Analysis <ul><li>Windows Error Reporting </li></ul><ul><li>http://winqual.microsoft.com </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/aa939342.aspx </li></ul><ul><li>Debugging </li></ul><ul><ul><li>Open the Disassembly Window </li></ul></ul><ul><ul><li>Open the Autos window to see pertinent Registers </li></ul></ul><ul><ul><li>http://blogs.msdn.com/greggm/archive/2004/12/15/315673.aspx </li></ul></ul><ul><ul><li>Trust Statics/Globals </li></ul></ul><ul><ul><li>Trust Stacks when you have Symbols </li></ul></ul>
  24. 25. VS2010: Managed/Interop Support <ul><li>Support for reading & writing Minidumps from Processes with Managed Code </li></ul><ul><li>Support for Mixed Mode debugging on x64 </li></ul>
  25. 26. How does the Debugger do Minidumps <ul><li>Use dbgHelp.dll Method: MiniDumpReadDumpStream to read streams from MiniDump File </li></ul><ul><li>Read the following Streams </li></ul><ul><ul><li>SystemInfoStream </li></ul></ul><ul><ul><li>ThreadListStream </li></ul></ul><ul><ul><li>ModuleListStream </li></ul></ul><ul><ul><li>MemoryListStream </li></ul></ul><ul><li>Create Container Objects in the debugger that wrap the instances from the MiniDump </li></ul><ul><li>Wrap memory as needed by StackWalking or Data Inspection </li></ul>
  26. 27. Benefits of Debugging Tools for Windows <ul><li>Small Footprint </li></ul><ul><ul><li>XCopy Enabled </li></ul></ul><ul><li>Ideal for debugging problems on machines that are locked down </li></ul><ul><li>Frequent releases </li></ul><ul><ul><li>Updated for new versions of Windows </li></ul></ul><ul><li>Which debuggers does it include? </li></ul><ul><ul><li>User Mode Debuggers: windbg/ntsd/cdb </li></ul></ul><ul><ul><li>Kernel Mode Debugger: kd </li></ul></ul><ul><li>Powerful Extensions & Instrumentation </li></ul><ul><li>Extensible by us </li></ul>
  27. 28. Debugging: Package Content <ul><li>Symbol Indexing Tools </li></ul><ul><li>Source Indexing Tools </li></ul><ul><li>Stand-alone Tools </li></ul><ul><ul><li>AgeStore </li></ul></ul><ul><ul><li>AdPlus </li></ul></ul><ul><ul><li>BreakIN </li></ul></ul><ul><ul><li>DbgSrv </li></ul></ul><ul><ul><li>GFlags </li></ul></ul><ul><ul><li>TList </li></ul></ul><ul><ul><li>Remote </li></ul></ul>
  28. 29. Installing Debugging tools for Windows <ul><li>Download Point: </li></ul><ul><ul><li>www.microsoft.com/whds/devtools/debugging/default.msp </li></ul></ul><ul><li>Default options sufficient </li></ul><ul><li>By Default installs into </li></ul><ul><ul><li>C:Program Filesdebugging Tools for Windows </li></ul></ul><ul><li>Directory Listing </li></ul>
  29. 30. Debugger Interaction: 1 st Steps <ul><li>Command Mode or GUI? </li></ul><ul><li>User mode prompt can get us a Head Start </li></ul><ul><li>Get the Exception Code </li></ul><ul><li>Understand the Environment </li></ul><ul><li>Set the Correct Symbols </li></ul><ul><li>Start from the Current Execution Context </li></ul><ul><li>Check the Loaded Module </li></ul>
  30. 31. Basic Tasks : Running the debugger <ul><li>Attaching to Process </li></ul><ul><ul><li>By Process ID: -p <process id> </li></ul></ul><ul><ul><li>By Process Name: -pn <process name> </li></ul></ul><ul><ul><li>TList Command </li></ul></ul><ul><li>Running Under the Debugger </li></ul><ul><ul><li>NTSD.EXE <Command Line> </li></ul></ul><ul><ul><li>NTSD.EXE C:Windows otepad.exe </li></ul></ul><ul><ul><li>Caveats: Various Components may go into Debug Mode </li></ul></ul>
  31. 32. Demos
  32. 33. Working with the Target <ul><li>Last Event </li></ul><ul><li>Registers </li></ul><ul><li>Memory </li></ul><ul><li>Variables </li></ul><ul><li>Stack </li></ul><ul><li>Unassemble </li></ul><ul><li>Process Information </li></ul><ul><li>Thread Information </li></ul><ul><li>Address Information </li></ul>
  33. 34. Basic Tasks : Symbols <ul><li>Additional Metadata about the Code </li></ul><ul><li>Managed Types far more self-descriptive </li></ul><ul><li>Private vs Public Symbols </li></ul><ul><li>Microsoft Public Symbol Server </li></ul><ul><li>How to tell the debugger the Location </li></ul><ul><ul><li>Pointing to MS Public Server: .symfix </li></ul></ul><ul><ul><li>Pointing to additional Paths: .sympath+ </li></ul></ul><ul><ul><li>Reloading Symbols: .reload </li></ul></ul><ul><li>Custom Symbol Servers </li></ul>
  34. 35. Walkthrough for Symbols
  35. 36. Symbol Server <ul><li>Large Store of Symbol & Binary Files </li></ul><ul><li>Files are organized based on properties: </li></ul><ul><ul><li>Name </li></ul></ul><ul><ul><li>Type </li></ul></ul><ul><ul><li>Time stamp </li></ul></ul><ul><ul><li>Size of the Image </li></ul></ul><ul><ul><li>RSDS Signature </li></ul></ul><ul><li>Binary files can be stored in different location </li></ul><ul><li>Files can be compressed </li></ul>
  36. 37. Building a Symbol Server <ul><li>Tools: </li></ul><ul><ul><li>PdbCopy.exe </li></ul></ul><ul><ul><li>BinPlace.Exe (WDK) </li></ul></ul><ul><ul><li>SymStore.exe </li></ul></ul><ul><ul><li>Extending the build process (Batch Files) </li></ul></ul>
  37. 38. Basic Tasks : SOS <ul><li>Powerful managed code debugger extension </li></ul><ul><ul><li>Introspect on the internal state of the CLR </li></ul></ul><ul><ul><li>Son of Strike </li></ul></ul><ul><li>Loading SOS </li></ul><ul><ul><li>.NET 2.0 : .loadby sos mscorwks </li></ul></ul><ul><ul><li>.NET 4.0 : .loadby sos clr </li></ul></ul><ul><li>Help Command </li></ul><ul><ul><li>!help displays all commands </li></ul></ul><ul><ul><li>!help <command> displays help for specific command </li></ul></ul><ul><li>SOSEX is another useful debugger extension </li></ul><ul><ul><li>http://www.stevestechspot.com </li></ul></ul>
  38. 39. Debugger Extension walkthroughs
  39. 40. Basic Tasks : Thread <ul><li>Basic Tasks : Thread </li></ul><ul><li>Basic Unit of code Execution </li></ul><ul><li>Before you launch a new thread, think twice </li></ul><ul><li>Sync-blocks </li></ul><ul><ul><li>Plethora of information about objects </li></ul></ul><ul><li>SOS Thread commands </li></ul><ul><ul><li>!Threads: List all managed Thread </li></ul></ul><ul><ul><li>!ClrStack: Displays Managed Callstack for currently active Thread </li></ul></ul><ul><ul><li>~<ThreadNum>s: Switches currently active Thread </li></ul></ul><ul><ul><li>~*e!ClrStack: Shows Callstack for all managed threads </li></ul></ul><ul><ul><li>!syncBlk </li></ul></ul>
  40. 41. Deadlock problem walkthrough
  41. 42. Basic Tasks: Managed Heap & Garbage collection <ul><li>Automatic Memory Management </li></ul><ul><ul><li>Sits on top of the Windows Memory Manager </li></ul></ul><ul><ul><li>Currently consists of 3 generations (0, 1, 2) </li></ul></ul><ul><ul><li>Caveat: Native resources must be explicitly cleaned up </li></ul></ul><ul><li>SOS Commands </li></ul><ul><ul><li>DumpHeap </li></ul></ul><ul><ul><li>DumpObj (do) </li></ul></ul><ul><ul><li>GCRoot </li></ul></ul><ul><li>Visualizing Runtime Object Graphs </li></ul><ul><ul><li>http://www.lovettsoftware.com/blogengine.net/post/2010/01/15/Visualizing-Runtime-Object-Graphs.aspx </li></ul></ul>
  42. 43. Walkthrough for managed heap commands
  43. 44. Resource Leaks <ul><li>What is a Resource? </li></ul><ul><ul><li>Handles </li></ul></ul><ul><ul><ul><li>File Object </li></ul></ul></ul><ul><ul><ul><li>Process Object </li></ul></ul></ul><ul><ul><ul><li>Thread Object </li></ul></ul></ul><ul><ul><ul><li>Isolation layer between User Mode code & Kernel </li></ul></ul></ul><ul><ul><li>Synchronization Primitives </li></ul></ul><ul><ul><li>Heap Memory Allocator </li></ul></ul><ul><ul><li>Virtual Memory Allocator </li></ul></ul><ul><ul><li>COM Allocator </li></ul></ul>
  44. 45. Tools for Heap Memory Tracking <ul><li>UMDH </li></ul><ul><ul><li>Tracks Heap based Memory </li></ul></ul><ul><ul><li>Requires OS Instrumentation to be Enabled (gflags) </li></ul></ul><ul><li>LeakDiag </li></ul><ul><ul><li>Uses Microsoft Detours Library </li></ul></ul><ul><ul><li>Tracks different types of Memory Allocators </li></ul></ul><ul><ul><ul><li>Heap Allocator </li></ul></ul></ul><ul><ul><ul><li>Virtual Memory Allocator </li></ul></ul></ul><ul><ul><ul><li>COM Allocator </li></ul></ul></ul><ul><ul><ul><li>C Runtime Allocator </li></ul></ul></ul><ul><li>Debugger Command: !heap </li></ul><ul><li>Static Source Code Analysis Tools: Prefast (WDK) </li></ul>
  45. 46. Memory Leaks: C Run-Time Functions <ul><li>_CrtDumpMemoryLeaks() </li></ul><ul><ul><li>Performs leak checking where called. You want to place this call at all possible exits of your app. </li></ul></ul><ul><li>_CrtSetDbgFlag () </li></ul><ul><ul><li>Sets debugging flags for the C run-time library. </li></ul></ul><ul><ul><li>_CRTDBG_REPORT_FLAG Gets current flag(s) </li></ul></ul><ul><ul><li>_CRTDBG_LEAK_CHECK_DF Perform automatic leak checking at program exit through a call to _CrtDumpMemoryLeaks </li></ul></ul>
  46. 47. Memory Leaks: Visual Studio <ul><li>_CRTDBG_MAP_ALLOC_ </li></ul><ul><ul><li>Memory allocation number (inside curly braces) </li></ul></ul><ul><ul><li>Block type (normal, client or CRT) </li></ul></ul><ul><ul><li>Memory location in hex </li></ul></ul><ul><ul><li>Size of block in bytes </li></ul></ul><ul><ul><li>Contents of the first 16 bytes in hex </li></ul></ul><ul><ul><li>File name </li></ul></ul><ul><ul><li>Line number </li></ul></ul>
  47. 48. Heap Corruptions <ul><li>Violate the Integrity of Memory allocated on the Heap </li></ul><ul><ul><li>Stray Pointers </li></ul></ul><ul><ul><li>Overruns </li></ul></ul><ul><ul><li>Underruns </li></ul></ul><ul><ul><li>Over-Deletion </li></ul></ul><ul><ul><li>Reuse after Deletion </li></ul></ul><ul><li>One of the toughest problem to Debug </li></ul>
  48. 49. Windows Memory Architecture Application Virtual Memory Manager Heap Manager Default Process Heap C Runtime Heap Other Heaps
  49. 50. Heap Block Structure Current Size Previous Size Seg Index Flags Unused Tag Index Pre-allocation Metadata Post-allocation Metadata User accessible part Pre-allocation Metadata Suffix Bytes Fill Area (debug) Heap Extra Post-allocation Metadata User accessible part User accessible part
  50. 51. Tools for Debugging Heap Corruptions <ul><li>Goal is to Break when the corruption occurs AND not after </li></ul><ul><li>PageHeap helps with that goal </li></ul><ul><ul><li>Annotates heap blocks to trigger fault at the time of write </li></ul></ul><ul><li>Light PageHeap uses Fill Patterns </li></ul><ul><li>Full PageHeap uses Fill Patterns and Guard Pages </li></ul><ul><ul><li>Very Memory Intensive </li></ul></ul>
  51. 52. Postmortem Debugging <ul><li>Scenarios </li></ul><ul><ul><li>Live debugging not feasible </li></ul></ul><ul><ul><li>Reproducing the problem is difficult </li></ul></ul><ul><li>Static Snapshot of a Live Process </li></ul><ul><li>Use the same debugger to debug offline </li></ul><ul><li>Limitations </li></ul><ul><ul><li>It is a snapshot; so you can't control execution </li></ul></ul><ul><ul><li>Depending on type of dumpfile, some SOS commands may not work. </li></ul></ul>
  52. 53. Postmortem Debugging: How to generate dumpfiles <ul><li>Using the debuggers </li></ul><ul><ul><li>.dump /mf c:CoreDump.dmp </li></ul></ul><ul><li>Automatic </li></ul><ul><ul><li>ADPlus </li></ul></ul><ul><ul><li>Windows Error Reporting </li></ul></ul><ul><ul><ul><li>https://winqual.microsoft.com </li></ul></ul></ul><ul><ul><ul><li>Available to everyone </li></ul></ul></ul>
  53. 54. Windows Error Reporting (WER) Architecture Error Sent Dr. Watson Process Crash Crash data over HTTPS Fault response over HTTPS Windows Error Reporting Service ISV Query Fault Data
  54. 55. Postmortem Debugging: How to debug Dumpfiles? <ul><li>Slightly different than Native code debugging </li></ul><ul><ul><li>The data Access Layer (DAC) </li></ul></ul><ul><ul><li>Implemented in mscordacwks.dll </li></ul></ul><ul><ul><li>Different for each version of the CLR </li></ul></ul><ul><li>Debugging Dump files </li></ul><ul><ul><li>use the -z switch with path to the dump file </li></ul></ul>
  55. 56. Walkthroughs with ADPlus & Postmortem debugging
  56. 57. When Not to use Native Debugging <ul><li>During Code Development </li></ul><ul><li>Tracing the Code </li></ul><ul><li>100% managed code </li></ul><ul><li>Need frequent variable inspection </li></ul><ul><li>Need frequent references to the source files </li></ul><ul><li>Debugging Partial Dumps </li></ul><ul><li>Kernel Mode Debugging </li></ul><ul><ul><li>Some pages are paged out </li></ul></ul>
  57. 58. Summary <ul><li>Importance of debugging </li></ul><ul><li>Be aware of Magic </li></ul><ul><li>Tools available for Debugging .NET </li></ul><ul><li>Basic debugging Tasks </li></ul><ul><ul><li>Running the debuggers </li></ul></ul>
  58. 59. Questions? <ul><li>Books </li></ul><ul><ul><li>Advanced .NET Debugging: Mario Hewardt </li></ul></ul><ul><ul><li>Windows Internals: Mark E. Russinovich, David A. Solomon with Alex Ionescu </li></ul></ul><ul><ul><li>Windows via C/C++: Jeffrey M. Richter, Christophe Nasarre </li></ul></ul><ul><li>Blogs </li></ul><ul><ul><li>http://blogs.msdn.com/ms_joc/ </li></ul></ul><ul><ul><li>http://www.wintellect.com/cs/blogs/jrobbins/default.aspx </li></ul></ul>

×