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

More Related Content

What's hot

Captain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit MitigationsCaptain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
enSilo
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
Dimitry Snezhkov
 
Manish Chasta - Securing Android Applications
Manish Chasta - Securing Android ApplicationsManish Chasta - Securing Android Applications
Manish Chasta - Securing Android Applications
Positive Hack Days
 
JAVA First Day
JAVA First DayJAVA First Day
JAVA First Day
Sher Singh Bardhan
 
線上遊戲與雲端運算
線上遊戲與雲端運算線上遊戲與雲端運算
線上遊戲與雲端運算
Sheng-Wei (Kuan-Ta) Chen
 
Bypassing anti virus scanners
Bypassing anti virus scannersBypassing anti virus scanners
Bypassing anti virus scanners
martacax
 
Deep Dive into WinRT
Deep Dive into WinRTDeep Dive into WinRT
Deep Dive into WinRT
Sasha Goldshtein
 
Runtime 8 and Windows Phone 8
Runtime 8 and Windows Phone 8Runtime 8 and Windows Phone 8
Runtime 8 and Windows Phone 8
Damir Dobric
 
JavaScript Module Loaders
JavaScript Module LoadersJavaScript Module Loaders
JavaScript Module Loaders
zeroproductionincidents
 
C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.
Richard Taylor
 
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable codenullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
n|u - The Open Security Community
 
Android Booting Sequence
Android Booting SequenceAndroid Booting Sequence
Android Booting Sequence
Jayanta Ghoshal
 
iOS 7.1 accessibility for developers
iOS 7.1 accessibility for developersiOS 7.1 accessibility for developers
iOS 7.1 accessibility for developers
Ted Drake
 
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage FuzzerThe Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
Joxean Koret
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android apps
Pranay Airan
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
Mário Almeida
 
2012 04-19 theory-of_operation
2012 04-19 theory-of_operation2012 04-19 theory-of_operation
2012 04-19 theory-of_operation
bobwolff68
 
Programming With WinRT And Windows8
Programming With WinRT And Windows8Programming With WinRT And Windows8
Programming With WinRT And Windows8
Rainer Stropek
 
Practice of Android Reverse Engineering
Practice of Android Reverse EngineeringPractice of Android Reverse Engineering
Practice of Android Reverse Engineering
National Cheng Kung University
 
Entwicker camp2007 calling-the-c-api-from-lotusscript
Entwicker camp2007 calling-the-c-api-from-lotusscriptEntwicker camp2007 calling-the-c-api-from-lotusscript
Entwicker camp2007 calling-the-c-api-from-lotusscript
Bill Buchan
 

What's hot (20)

Captain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit MitigationsCaptain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
 
Manish Chasta - Securing Android Applications
Manish Chasta - Securing Android ApplicationsManish Chasta - Securing Android Applications
Manish Chasta - Securing Android Applications
 
JAVA First Day
JAVA First DayJAVA First Day
JAVA First Day
 
線上遊戲與雲端運算
線上遊戲與雲端運算線上遊戲與雲端運算
線上遊戲與雲端運算
 
Bypassing anti virus scanners
Bypassing anti virus scannersBypassing anti virus scanners
Bypassing anti virus scanners
 
Deep Dive into WinRT
Deep Dive into WinRTDeep Dive into WinRT
Deep Dive into WinRT
 
Runtime 8 and Windows Phone 8
Runtime 8 and Windows Phone 8Runtime 8 and Windows Phone 8
Runtime 8 and Windows Phone 8
 
JavaScript Module Loaders
JavaScript Module LoadersJavaScript Module Loaders
JavaScript Module Loaders
 
C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.
 
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable codenullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
nullcon 2011 - Reversing MicroSoft patches to reveal vulnerable code
 
Android Booting Sequence
Android Booting SequenceAndroid Booting Sequence
Android Booting Sequence
 
iOS 7.1 accessibility for developers
iOS 7.1 accessibility for developersiOS 7.1 accessibility for developers
iOS 7.1 accessibility for developers
 
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage FuzzerThe Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
The Nightmare Fuzzing Suite and Blind Code Coverage Fuzzer
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android apps
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
2012 04-19 theory-of_operation
2012 04-19 theory-of_operation2012 04-19 theory-of_operation
2012 04-19 theory-of_operation
 
Programming With WinRT And Windows8
Programming With WinRT And Windows8Programming With WinRT And Windows8
Programming With WinRT And Windows8
 
Practice of Android Reverse Engineering
Practice of Android Reverse EngineeringPractice of Android Reverse Engineering
Practice of Android Reverse Engineering
 
Entwicker camp2007 calling-the-c-api-from-lotusscript
Entwicker camp2007 calling-the-c-api-from-lotusscriptEntwicker camp2007 calling-the-c-api-from-lotusscript
Entwicker camp2007 calling-the-c-api-from-lotusscript
 

Similar to .NET Debugging Tips and Techniques

C# Production Debugging Made Easy
 C# Production Debugging Made Easy C# Production Debugging Made Easy
C# Production Debugging Made Easy
Alon Fliess
 
Debugging in .Net
Debugging in .NetDebugging in .Net
Debugging in .Net
Muhammad Amir
 
Introductiontoasp netwindbgdebugging-100506045407-phpapp01
Introductiontoasp netwindbgdebugging-100506045407-phpapp01Introductiontoasp netwindbgdebugging-100506045407-phpapp01
Introductiontoasp netwindbgdebugging-100506045407-phpapp01
Camilo Alvarez Rivera
 
Spug pt session2 - debuggingl
Spug pt session2 - debugginglSpug pt session2 - debuggingl
Spug pt session2 - debuggingl
Comunidade Portuguesa de SharePoiint
 
Android Logging System
Android Logging SystemAndroid Logging System
Android Logging System
William Lee
 
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
DevOpsDays Tel Aviv
 
Crash dump analysis - experience sharing
Crash dump analysis - experience sharingCrash dump analysis - experience sharing
Crash dump analysis - experience sharing
James Hsieh
 
IDAPRO
IDAPROIDAPRO
IDAPRO
Matt Vieyra
 
Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007
Stephan Chenette
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
sarangowtham_gunnam
 
Advanced driver debugging (13005399) copy
Advanced driver debugging (13005399)   copyAdvanced driver debugging (13005399)   copy
Advanced driver debugging (13005399) copy
Burlacu Sergiu
 
MobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android AppsMobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android Apps
Ron Munitz
 
Porting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidPorting your favourite cmdline tool to Android
Porting your favourite cmdline tool to Android
Vlatko Kosturjak
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
mpaproductions
 
Debugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications revDebugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications rev
Tess Ferrandez
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search
llangit
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
WE-IT TUTORIALS
 
A Life of breakpoint
A Life of breakpointA Life of breakpoint
A Life of breakpoint
Hajime Morrita
 
Continuous Integration using Cruise Control
Continuous Integration using Cruise ControlContinuous Integration using Cruise Control
Continuous Integration using Cruise Control
elliando dias
 

Similar to .NET Debugging Tips and Techniques (20)

C# Production Debugging Made Easy
 C# Production Debugging Made Easy C# Production Debugging Made Easy
C# Production Debugging Made Easy
 
Debugging in .Net
Debugging in .NetDebugging in .Net
Debugging in .Net
 
Introductiontoasp netwindbgdebugging-100506045407-phpapp01
Introductiontoasp netwindbgdebugging-100506045407-phpapp01Introductiontoasp netwindbgdebugging-100506045407-phpapp01
Introductiontoasp netwindbgdebugging-100506045407-phpapp01
 
Spug pt session2 - debuggingl
Spug pt session2 - debugginglSpug pt session2 - debuggingl
Spug pt session2 - debuggingl
 
Android Logging System
Android Logging SystemAndroid Logging System
Android Logging System
 
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
How We Analyzed 1000 Dumps in One Day - Dina Goldshtein, Brightsource - DevOp...
 
Crash dump analysis - experience sharing
Crash dump analysis - experience sharingCrash dump analysis - experience sharing
Crash dump analysis - experience sharing
 
IDAPRO
IDAPROIDAPRO
IDAPRO
 
Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
 
Advanced driver debugging (13005399) copy
Advanced driver debugging (13005399)   copyAdvanced driver debugging (13005399)   copy
Advanced driver debugging (13005399) copy
 
MobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android AppsMobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android Apps
 
Porting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidPorting your favourite cmdline tool to Android
Porting your favourite cmdline tool to Android
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
 
Debugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications revDebugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications rev
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
 
A Life of breakpoint
A Life of breakpointA Life of breakpoint
A Life of breakpoint
 
Continuous Integration using Cruise Control
Continuous Integration using Cruise ControlContinuous Integration using Cruise Control
Continuous Integration using Cruise Control
 

More from Bala Subra

Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
Serverless architectures: APIs, Serverless Functions, Microservices - How to ...Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
Bala Subra
 
Windows Azure Sdk
Windows Azure SdkWindows Azure Sdk
Windows Azure Sdk
Bala Subra
 
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
Bala Subra
 
Dean Keynote Ladis2009
Dean Keynote Ladis2009Dean Keynote Ladis2009
Dean Keynote Ladis2009
Bala Subra
 
Scalable Resilient Web Services In .Net
Scalable Resilient Web Services In .NetScalable Resilient Web Services In .Net
Scalable Resilient Web Services In .Net
Bala Subra
 
Driver Debugging Basics
Driver Debugging BasicsDriver Debugging Basics
Driver Debugging Basics
Bala Subra
 
What is new in WCF 4.0?
What is new in WCF 4.0?What is new in WCF 4.0?
What is new in WCF 4.0?
Bala Subra
 
Advanced Windows Debugging
Advanced Windows DebuggingAdvanced Windows Debugging
Advanced Windows Debugging
Bala Subra
 
System Center: Virtual Machine Manager 2008 Version 2.0
System Center: Virtual Machine Manager 2008 Version 2.0System Center: Virtual Machine Manager 2008 Version 2.0
System Center: Virtual Machine Manager 2008 Version 2.0
Bala Subra
 
Parallel Computing For Managed Developers
Parallel Computing For Managed DevelopersParallel Computing For Managed Developers
Parallel Computing For Managed Developers
Bala Subra
 
IT Management Firestarter
IT Management FirestarterIT Management Firestarter
IT Management Firestarter
Bala Subra
 
Demystifying Windows Communication Foundation
Demystifying Windows Communication FoundationDemystifying Windows Communication Foundation
Demystifying Windows Communication Foundation
Bala Subra
 
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
Bala Subra
 
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for ITDenny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Bala Subra
 
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
Bala Subra
 
Biz talk BI
Biz talk BIBiz talk BI
Biz talk BI
Bala Subra
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check Tuneup
Bala Subra
 
.Net Debugging Techniques
.Net Debugging Techniques.Net Debugging Techniques
.Net Debugging Techniques
Bala Subra
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
Bala Subra
 
Ssrs 2005 Reporting Services
Ssrs 2005 Reporting ServicesSsrs 2005 Reporting Services
Ssrs 2005 Reporting Services
Bala Subra
 

More from Bala Subra (20)

Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
Serverless architectures: APIs, Serverless Functions, Microservices - How to ...Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
Serverless architectures: APIs, Serverless Functions, Microservices - How to ...
 
Windows Azure Sdk
Windows Azure SdkWindows Azure Sdk
Windows Azure Sdk
 
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
Mobile Development Architecture Ppt with Slides, Book Notes on using Web Silv...
 
Dean Keynote Ladis2009
Dean Keynote Ladis2009Dean Keynote Ladis2009
Dean Keynote Ladis2009
 
Scalable Resilient Web Services In .Net
Scalable Resilient Web Services In .NetScalable Resilient Web Services In .Net
Scalable Resilient Web Services In .Net
 
Driver Debugging Basics
Driver Debugging BasicsDriver Debugging Basics
Driver Debugging Basics
 
What is new in WCF 4.0?
What is new in WCF 4.0?What is new in WCF 4.0?
What is new in WCF 4.0?
 
Advanced Windows Debugging
Advanced Windows DebuggingAdvanced Windows Debugging
Advanced Windows Debugging
 
System Center: Virtual Machine Manager 2008 Version 2.0
System Center: Virtual Machine Manager 2008 Version 2.0System Center: Virtual Machine Manager 2008 Version 2.0
System Center: Virtual Machine Manager 2008 Version 2.0
 
Parallel Computing For Managed Developers
Parallel Computing For Managed DevelopersParallel Computing For Managed Developers
Parallel Computing For Managed Developers
 
IT Management Firestarter
IT Management FirestarterIT Management Firestarter
IT Management Firestarter
 
Demystifying Windows Communication Foundation
Demystifying Windows Communication FoundationDemystifying Windows Communication Foundation
Demystifying Windows Communication Foundation
 
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
Basics &amp; Intro to SQL Server Reporting Services: Sql Server Ssrs 2008 R2
 
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for ITDenny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
 
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
BizTalk 2010 with Appfabric Hosting in the Cloud: WCF Services vs BT2010
 
Biz talk BI
Biz talk BIBiz talk BI
Biz talk BI
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check Tuneup
 
.Net Debugging Techniques
.Net Debugging Techniques.Net Debugging Techniques
.Net Debugging Techniques
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
 
Ssrs 2005 Reporting Services
Ssrs 2005 Reporting ServicesSsrs 2005 Reporting Services
Ssrs 2005 Reporting Services
 

.NET Debugging Tips and Techniques

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

Editor's Notes

  1. Code reviews and Tracing are the old techniques to find out issues with the code. Unit Testing is the current mantra. But, the good old native debuggers are the way to resolve multi-process, multi-machine server applications. We will chat about using SOS, MDA, Windows Performance Monitors, CLR Profilers and NTSD with zero footprint &amp; no config changes to production environment. The Visual Studio Debugger provides a slew of features that make the task of debugging both easier and more efficient. Learn about time-saving tips and tricks for all versions of the Visual Studio Debugger, including the new debugger features in Visual Studio 2010. Hear about the new mixed-mode debugging feature, the breakpoints window enhancements, the new WPF visualizes, and a number of other features. Also learn about thread debugging enhancements, new features for making stepping into properties easier, and more. Join us as we crack open the toolbox and walk through some of the debugger&apos;s best practices. Focus on powerful native debugging tools including WinDBG, NTSD, and CDB. introduce key concepts needed to successfully use .NET’s native debuggers. Next, turn to more sophisticated debugging techniques, using real-world examples that demonstrate many common C# programming errors. For developers who want a deeper understanding of how .NET works, to gain the necessary tools and use them to debug and solve real world problems that cannot be fixed with regular debuggers.
  2. A defect that costs $1 to fix on the programmer’s desktop costs $100 to fix once it is incorporated into a complete program and many thousands of dollars if it is identified only after the software has been deployed in the field. (Building a Better Bug Trap – The Economist June 2003) Static Analysis can reduce defects by up to a factor of six! (Capers Jones, Software Productivity Group)
  3. Compile-time analysis of the source program like code inspection, but performed by a tool Looks for violations of well-defined constraints procedure contracts Examples of bugs to be found by Static Analysis IoCompleteRequest(p) requires p to be non-NULL: p = NULL ; … ; IoCompleteRequest(p); Completing the same task twice when NOT needed: IoCompleteRequest (Irp); … IoCompleteRequest (Irp);
  4. Static Analysis does 2 Things: Finds a defect - without testing Reveals the path - without debugging Path Coverage One test case covers only one path in the driver The path remains unrevealed if no defect found 100 test cases cover &lt; 100 paths? More test cases -&gt; more duplication How many paths remain untested? How long would it take to test all of them? When are you done? Longer than you can afford! Time to market? 100% Coverage In minutes or hours, not weeks or months Employing Computer, not a Test Engineer Targeting a large set of potential violations
  5. 1: Introduction to .NET Debugging. Explains the differences between Unmanaged (native) and managed (.NET) debugging, and explore the tools required for successful debugging. 2: CLR Fundamentals. Explores the physical layout of a managed assembly, the native CLR structural hierarchy, managed objects, and the role that JIT compilation plays in managed execution. 3: .NET Memory Management. Examines .NET memory management including the Garbage Collector (GC) and Finalizers. 4: Troubleshooting Hangs. Defines, parses, and troubleshoots “hang” scenarios in WinForms and ASP.NET applications. 5: Troubleshooting Exceptions. Dives into the different types of exceptions, including those that go unhandled in WinForms and ASP.NET applications. 6: Additional Debugging Tools. Reviews additional tools and debugging scenarios.
  6. Dbgrsv.exe, kdsrv.exe, dbengprx.exe Debugger protocol remoting tools Discussed later as part of debugger remoting Security Audit Debugging
  7. Nothing magic; Understand the code and its underlying illusions
  8. Inspire of code auditors, reviews, FxCop... Impossible Business side of things
  9. For example: Compiler will not catch an un-initialized pointer but you WILL get a run-time error if you try to use it! Some simple programming techniques * To improve diagnosability of your code * To help support folks get more out of the crash dumps * To enable them determine root cause of an issue from a single crash dump * So they don’t have to ask the customer to reproduce the problem again to get them yet another crash dump Debugging should be data centric not code centric * Especially TRUE for a crash dump * No execution and no execution control * All you have is snapshot of data structures to examine
  10. Kdbgctrl.exe Tool to configure the kernel’s debugging options Run it on the target machine Change behavior of DbgPrint, user mode int 3, DbgPrint buffer size, etc.
  11. http://blogs.msdn.com/jacdavis/archive/2008/02/20/new-debug-engine-sample.aspx he Visual Studio Debugger is split up architecturally into a few pieces: 1)      The debugger UI: the windows and commands the user actually interacts with.  A good example of this is the watch window or the little red circle that appears in the text editor as a breakpoint. 2)      The SDM (session debug manager): “ A debug engine multiplexer” Admittedly, that’s a confusing explanation. Essentially, the SDM’s job to combine all of the events and commands for the various debug engines into one unified stream for the UI.  The debugger UI only displays one ”view” of what is being debugged at a time.  Even if the user is debugging multiple processes or threads, they are only looking at one of them. 3)      Debug Engines – the components that perform the actual debugging of a debuggee.  For instance, a native debug engine would be responsible for debugging native win32 applications. A script debug engine would be responsible for debugging jscript or vbscript. A CLR debug engine would be responsible for debugging .Net applications running on the CLR.  A hypothetical Perl engine could be responsible for debugging Perl… and on and on Visual Studio Debug Engines implement and interact with a set of interfaces called AD7 which stands for Active Debugging 7. AD7 is publically documented here: http://msdn2.microsoft.com/en-us/library/bb147088.aspx
  12. symproxy: a symbol server request proxy Similar to a web server proxy Allows local caching to reduce load on primary server Putting a proxy on a bridge server allows symsrv requests to cross network boundaries agestore: a new tool to manage local caches Simple aging and cleanup of locally-stored files
  13. Use Right click on Module:  “Symbol Load Information” to find out where we looked
  14. 16 Megs in today&apos;s world of .NET framework Production machines can&apos;t hv Visual Studio; not updated with latest patches Operations team is diff from dev primarily used for managed code debugging Diff between them WinDBG is not console based; GUI; step through etc NTSD &amp; CDB are console based Security team; Windows HEAT team etc hv written plugins; etc capabilities Ability to write debugging extensions for our programs/projects/products
  15. 64 or which architecture? SDK is also available for developing our own extensions Debugger Extensions Dump or analyze complex data structures !process, !devnode, !poolval Leverages the type information from the PDB file Simplify routine tasks !analyze Automate repetitive steps Regularly check state of certain objects Fully control the state of the target Can write a mini-debugger using the extension APIs When Should You Write One? Any task that is repetitive can be sped up by developing an extension Allows other people (testers) to help with basic debugging Can help identify common problems quickly To dump internal data structures in a custom readable format Avoid writing extensions when: Code is still fluctuating a lot Extension must match the code being debugged
  16. Analysis Step 1 Use bugcheck or exception parameters to extract basic information Each condition is processed by a separate routine that understands the meaning of each parameter If specific follow-up or faulting code is found, report results Save trap frame, context recording, faulting thread, etc. Analysis Step TWO Use information in step one to get faulting stack Scan the stack for special functions such as Trap0E or UnhandledExceptionFilter to find alternate stack Analyze frames on the final stack to determine most likely culprit Different weights are assigned to routines Internal kernel routines have lowest weight Device drivers have highest weight Fine grain control provided by triage.ini Highest weight frame found on the stack is treated as the culprit Analysis Step Three If stack does not yield an interesting frame, analyze raw stack data Iterate on all stack values using the same weight algorithm The ‘dps’ command will show that output This finds code that corrupts the stack Analysis Step Four Check for presence of memory or pool corrupting drivers Check for corrupted code streams Bad RAM Check for other possible problems, such as invalid call sequences Possible CPU problem Analysis Step Five Generate final bucket ID and follow-up based on all gathered information Determine which fields need to be embedded in the bucket ID !analyze assigns ownership of failure
  17. Use Task manager; Get Process ID (TList) Start the app; instead of attaching to already existing Example of Windows NT Heat dependent process; additional information is filled in the blocks. Not Release debugging; So, Debug version is ran
  18. show diff between -p (more dlls loaded) and starting manually
  19. http://blogs.msdn.com/jacdavis/archive/2006/08/25/using-sos-in-visual-studio.aspx http://http/www.microsoft.com/whdc/devtools/debugging/default.mspx http://msdn.microsoft.com/msdnmag/issues/03/06/Bugslayer/default.aspx How do you go about using SOS in VS? Well first, you have to have the native debugging engine enabled. This is because SOS examines the CLR data structures from the debugger process so it must have access to the native address space. To do this from a console application project or winforms project, right mouse click on the project in solution explorer, choose properties, switch to the Debug tab, and make sure &amp;quot;Enable Unmanaged Code Debugging&amp;quot; is selected. With that done, close the property page add a breakpoint somewhere in your project, and hit F5. Once you&apos;ve hit your breakpoint, the real fun begins. 1) Open the Immediate window (Debug-&gt;Windows-&gt;Immediate) 2) Type &amp;quot;.load C:\\windows\\Microsoft.NET\\Framework\\v2.0.50727\\sos.dll&amp;quot; and hit enter (you may need to change the path to point to a different location on your machine). The immediate window should respond by saying &amp;quot;extension C:\\windows\\Microsoft.NET\\Framework\\v2.0.50727\\sos.dll loaded&amp;quot; 3) Type &amp;quot;!help&amp;quot; sos should dump its help contents to the immediate window. It is now ready to use.
  20. Symbols are extremely important when debugging native code While managed code are not so much important Source code lines are not present Proprietary information Run tool to strip all Private symbol Publish public symbol files For own symbol files instead of Microsoft refresh the symbol files
  21. 03Simple.exe demo for Symbols start with ntsd instead of attaching ntsd 03simple.exe .sysmfix .sympath+ c:\\ .reload
  22. Collect all binary files into a Single Location Essential for library vendors Use them from a share (Private symbols) Publish the symbols on a HTTP server (public) Publish the symbols on a Secure HTTP server (Private) Works with other Tools (Process Monitor, profiler) SourceServer is an alternate stream into Private Symbol PDBs Contains information to retrieve the source file from the Source revision control system
  23. CLR manages thread differently than Windows Native debuggers don&apos;t hv much idea abt Managed Code SO CLR understanding debuggers Threads getting deadlocked kind of scenarios
  24. Demo ntsd 03simple.exe g ctrl + C .sysmfix .reload .loadby sos mscorwks !help !help Threads
  25. Multi-threading is easy but tricky Hash code of the objects Lock info how many bytes long? 4 or 8 Deadlock issues 200 Threads waiting Instead of going one by one on each of the 200 Execute and get everything for each thread running
  26. Run the app - start from cmd 06deadblock.exe TList ntsd -p processID .symfix .reload .loadby sos mscorwks ~*e!ClrStack ~0S (ZEROTH thread) !Dumpstack ~3s !DumpStack Use Syncblock to find out more info !SyncBlk Who is holding locks? Actual object which is holding? Address of the object !do !do on the object open the source code SOSex has the DumpObject command
  27. No memory leaks Just new No need to delete in destructor Statistical summary - how many instances Type etc Specify address of the object Size, type etc 2 things Does not hv any references for it ?? Is it eligible for GC If not, who has references for it? (Native resources not getting freed up) Instead of GCRoot - Visual graph Whole lot easier Link to video
  28. 03simple.exe .sysmfix - set symbols .reload g ctrl + C load sos dump heap Address of the object - size of the object --- 327 objects Last part - stats 135 instances system.string - how much does it occupy dumpheap -type system.string (filtered on type) Pick an address of size 80 !do on its address !GCRoot on its address No references So it will be cleaned up on next GC Useful - why the reference is held? Who holds it etc
  29. Live process is stopped; resumed; break into debugging We controlled the execution Services running in the cloud (credit card transactions) 1000s of requests coming each second Service will be down costs millions of $$ lost Always try to get Full memory dump Otherwise becomes difficult to execute all cmds
  30. Automatic way vs .dump command Monitor with conditions for symptoms, problems - ADPLUS Win Error Reporting - Service based solutions version1 is getting used Diff flavors of windows Take the binaries of the product When the crash happens, anywhere Something bad happens at customer site Do you like to report this problem? Windows error reporting will create new entry Forward it to you/Download all the crash data Produce the fix Close the loop with customer Send it up to MS Register the response? - sorry for the inconvenience Take action on your issues Free of cost 3000+ companies signed up Client issues resolved as conditional maintenance
  31. SOS debugger extensions loads small piece of native code Asks CLR to give info to debug When live debugging versions are same CLR is present on the system Diff machines when postmortem Hot fixes, service packs, dll hell etc - pain Public symbol server published in Microsoft Contact customer to give the correct version Load it in ur system debugger will launch with -z
  32. 08simple.exe Null exception Enable ADPLUS adplus.vbs -crash -pn 08simpleexec.exe (start) pn for process name Crash mode Find crash dumps in location click ok start app Adplus - performance degradation - slow debuggers folder crash mode date Text file Dump files generated Shut down - we are not interested second chance exception ntsd -z path to the file .symfix .reload loadby sos mscorwks !Threads Which generated !pe
  33. http://msdn.microsoft.com/en-us/library/bb145915.aspx