Debugging .NET Applications
-
Upload
shoaibshakeel381 -
Category
Documents
-
view
19 -
download
0
description
Transcript of Debugging .NET Applications
Debugging .NET Applications
Huma Qureshi
Debugging .NET Apps © Huma Qureshi
About the trainer
• Huma Qureshi• Independent training
consultant• Self-employed
entrepreneur
• Past• BS-CS from Fast-
Lahore• MS-CS from University
of Washington, Seattle• Techlogix, Conrad Labs,
Microsoft
• Contact• huma.qureshi@netsolte
ch.com• [email protected]
m
Debugging .NET Apps © Huma Qureshi
Introductions
• Name & background
• Career aspirations
• Experience with .NET
• Any other thing…
Debugging .NET Apps © Huma Qureshi
Why this course
• Discussion
Debugging .NET Apps © Huma Qureshi
Course outline
1. The debugging process
2. Visual Studio debugging tricks
3. Building for debuggability1. Symbols, .NET source stepping
4. Minidumps and remote debugging
5. Multi-threaded debugging in VS
6. Profiling and IntelliTrace
7. Introduction to WinDbg
Debugging .NET Apps © Huma Qureshi
THE DEBUGGING PROCESS
Debugging .NET Apps © Huma Qureshi
Bugs
• Are a reality!• But good engineering teams see lesser of them late
in the cycle
• Are awesome!• If you find them before the customer does
• You learn most when fixing bugs
• Must be found “as early as possible”
Debugging .NET Apps © Huma Qureshi
Bugs
• Are a HUGE cost
• Most development teams spend more than 50% of their time debugging
• The better you debug the faster you ship and the higher the quality
Debugging .NET Apps © Huma Qureshi
Cost of buggy products
• More time maintaining -> less time building new features
• Huge maintenance cost
• Results in• You company going out of business
• You loosing your job
Debugging .NET Apps © Huma Qureshi
Reasons for buggy products
Debugging .NET Apps © Huma Qureshi
1. Dev ignorance or insufficient training• Reasons:
• Too many technologies to keep up with
• Dev ego comes in the way of learning
• Solutions:• Admit the deficiency
• Invest time in learning – online trainings, conferences, on-site trainings
• Mentoring
Debugging .NET Apps © Huma Qureshi
2. Lacking “commitment to quality”• Organizations claim, but not often are truly
committed to quality.
• Focus on short term achievements vs long term success.
• Every individual must advocate the “commitment to quality” in his capacity.
Debugging .NET Apps © Huma Qureshi
Debugging pre-reqs
Debugging .NET Apps © Huma Qureshi
1. Excellence = “Attention to detail”• Good debugging skills requires:
• “Phenomenal attention to detail” as a developer.• Avoid bugs
• Avoid resorting to the debugger
• Being good with available toolset.
• All good developers are good debuggers• They pay “attention to detail”
Debugging .NET Apps © Huma Qureshi
2. Avoid the debugger
• Take time to develop understanding of the problem you are trying to solve
• Take time to understand the underlying APIs you are consuming
• Program defensively
Debugging .NET Apps © Huma Qureshi
3. But before jumping into the debugger..• Know what you are trying to chase
• Write down the hypothesis you are going to test
Debugging .NET Apps © Huma Qureshi
4. Know the toolset
• Much faster and efficient if you know how to fully utilize the power of toolset available.!
Debugging .NET Apps © Huma Qureshi
5. Take a break - “Bug Talk”
• Getting stuck for too long kills creative thinking.
• When stuck at a problem for long• You are likely making some un-real assumptions
• Discuss with a peer
• Explain your issue – and likely you would understand while explaining.
Debugging .NET Apps © Huma Qureshi
The debugging process
Debugging .NET Apps © Huma Qureshi
Debugging .NET Apps © Huma Qureshi
Fix the bug
Formulate new
hypothesis
Duplicate
Own the bug
Formulate hypothesis
Divide and conquer
Think creatively
Leverage tools
Bug talk
Verify fix
Document learning
Describe
Q & A
Debugging .NET Apps © Huma Qureshi
VISUAL STUDIO DEBUGGING TOOLS
Debugging .NET Apps © Huma Qureshi
Debugee modules and symbols
Debugging .NET Apps © Huma Qureshi
Debugging tools are same across .NET• Technology doesn’t matter.
• There is one debugger that helps debug “.NET” code.
Debugging .NET Apps © Huma Qureshi
Start debugging: existing solution in Visual Studio
Debugging .NET Apps © Huma Qureshi
Start debugging: Process running outside Visual Studio
Debugging .NET Apps © Huma Qureshi
Show the modules window
Debugging .NET Apps © Huma Qureshi
Modules Window (loaded dlls)
Debugging .NET Apps © Huma Qureshi
Module Symbols (pdb files)
• An executing process is machine code.
• We need pdb files to translate the machine code to C# code and allow debugging.
• Must always save the pdb files for builds that are released.
Debugging .NET Apps © Huma Qureshi
Contents of pdb file
• Mapping of each IL offset to a source file and line number
• Names of the local variables in each function block, and their memory offset
Debugging .NET Apps © Huma Qureshi
Visual Studio debugger tips
Debugging .NET Apps © Huma Qureshi
Basic commands
• Start Debugging
• Insert/remove breakpoint
• Step over
• Step in
• Step out
Debugging .NET Apps © Huma Qureshi
Step into (F11)
• Start debugging with F11 to break at first line of your code executed.
• Good for debugging app startup issues
Debugging .NET Apps © Huma Qureshi
Run to cursor (Ctrl + F10)
• Runs the program and breaks execution at cursor position
• No need to set temporary breakpoint and then later delete it.
Debugging .NET Apps © Huma Qureshi
Data Visualizers
Debugging .NET Apps © Huma Qureshi
Data Visualizers: WPF Tree visualizer
Debugging .NET Apps © Huma Qureshi
Custom visualizers
• You can write your own visualizer for your specific ‘type’
• https://msdn.microsoft.com/en-us/library/ms164759.aspx
Debugging .NET Apps © Huma Qureshi
Data tips
Debugging .NET Apps © Huma Qureshi
“Refresh to enumerate” –function evaluation
Debugging .NET Apps © Huma Qureshi
Call stack: Show external code
Debugging .NET Apps © Huma Qureshi
Call stack: show parameter values
Debugging .NET Apps © Huma Qureshi
The locals, autos and watch window• Locals window
• All variables in current scope
• Autos window• Variables from current and previous statement
• Subset of locals window
• Watch window• User defined variables
Debugging .NET Apps © Huma Qureshi
Set next statement (altering execution flow)
Debugging .NET Apps © Huma Qureshi
Edit and continue
• Allows you to change your source code while your program is in break mode
• No need to restart debugging session to test a small code change.
• Handles most code changes in method bodies (except lambda expressions)
Debugging .NET Apps © Huma Qureshi
Step into specific
• Step into a specific method. For statements with nested method calls.
Debugging .NET Apps © Huma Qureshi
Step into specific
Debugging .NET Apps © Huma Qureshi
Immediate window
• Debugging unintegrated code
Debugging .NET Apps © Huma Qureshi
Tracepoints
Debugging .NET Apps © Huma Qureshi
Tracepoints
Debugging .NET Apps © Huma Qureshi
Conditional breakpoints
Debugging .NET Apps © Huma Qureshi
Conditional breakpoints
Debugging .NET Apps © Huma Qureshi
Viewing out of scope objects (Object ID)
Debugging .NET Apps © Huma Qureshi
Viewing out of scope objects (Object ID)
Debugging .NET Apps © Huma Qureshi
Useful keyboard shortcuts
Debug.BreakatFunction Ctrl+B
Debug.BreakAll Ctrl+Alt+Break
Debug.DeleteAllBreakpoints Ctrl+Shift+F9
Debug.Exceptions Ctrl+Alt+E
Debug.QuickWatch Ctrl+Alt+Q or Shift+F9
Debug.Restart Ctrl+Shift+F5
Debug.RunToCursor Ctrl+F10
Debug.SetNextStatement Ctrl+Shift+F10
Debug.Start F5
Debug.StartWithoutDebugging Ctrl+F5
Debug.StepInto F11
Debug.StepOut Shift+F11
Debug.StepOver F10
Debug.StopDebugging Shift+F5
Debug.ToggleBreakpoint F9
References
• Writing your own visualizer:• https://msdn.microsoft.com/en-us/library/ms164759.aspx
• Supported changes in ‘edit and continue’• https://msdn.microsoft.com/en-us/library/ms164927.aspx
• Full list of shortcuts:• http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx
• Visual Studio debugging MSDN resources:• http://msdn.microsoft.com/en-us/library/k0k771bt.aspx
Debugging .NET Apps © Huma Qureshi
Q & A
Debugging .NET Apps © Huma Qureshi
BUILDING FOR DEBUGGABILITY
Debugging .NET Apps © Huma Qureshi
Build configurations
Debugging .NET Apps © Huma Qureshi
Building debug vs release
Debugging .NET Apps © Huma Qureshi
Building debug vs release:Default settings in proj file
Debugging .NET Apps © Huma Qureshi
Building debug vs release:build configurations
Debugging .NET Apps © Huma Qureshi
#if Preprocessor directive
• When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol/constant is defined.
Defined in project msbuild file
Debugging .NET Apps © Huma Qureshi
Compiler optimizations
• The optimize property tells the compiler to optimize the compiled code.
• Optimized builds:• more performant and hence used for retail
configuration.
• hard to debug, so we turn of optimizations in debug builds.
Debugging .NET Apps © Huma Qureshi
Debug vs Release build
• Debug -> Not optimized
• Retail -> optimized
Debugging .NET Apps © Huma Qureshi
Debug vs Release build
• ‘Debug’ build is configured for debugging• So customize it to help debugging
• ‘Release’ build is configured for production
Debugging .NET Apps © Huma Qureshi
AssertionsExpecting the unexpected
Debugging .NET Apps © Huma Qureshi
Debug.Assert()
• Effects only debug build
• Tests a condition and if the condition evaluates to false, the assertion fails -- your program enters break mode.
Debugging .NET Apps © Huma Qureshi
Failed assertion
Debugging .NET Apps © Huma Qureshi
Failed assertion
• Ignore
• Retry – break in
• Abort – abort the process
Debugging .NET Apps © Huma Qureshi
Assertions uses
• Assert liberally. You can never have too many assertions.
• Assertions don't replace exceptions. • Exceptions cover the things your code demands;
assertions cover the things it assumes.
• A well-written assertion can tell you not just what happened and where (like an exception), but why.
• The assertion failed dialog lets you attach a debugger to the process, so you can poke around the stack as if you had put a breakpoint there.
Debugging .NET Apps © Huma Qureshi
Assertions: Never change program state
Debugging .NET Apps © Huma Qureshi
The Debug class
• Collection of methods to assert or print to Debug output.
• Only if DEBUG constant is defined
Debugging .NET Apps © Huma Qureshi
The Trace class
• Collection of methods to assert or print to Trace output.
• Only if TRACE constant is defined
Debugging .NET Apps © Huma Qureshi
Adding your custom debug and trace listeners
Debugging .NET Apps © Huma Qureshi
Build Symbols
Debugging .NET Apps © Huma Qureshi
Symbols and symbol servers
• Always keep symbols around for your builds• Allows to debug issues on actual build running in
production/staging/test environment
• Create an internal symbol server for caching symbols of internal and released builds
• Can be easily automated using team build
Debugging .NET Apps © Huma Qureshi
Microsoft public symbols
Debugging .NET Apps © Huma Qureshi
.NET source stepping
Debugging .NET Apps © Huma Qureshi
References
• .NET source stepping:• http://referencesource.microsoft.com/setup.html
• List of dlls you can step:• http://referencesource.microsoft.com/indexedpdbs.txt
• Creating your team symbol server:• http://www.lionhack.com/2014/01/14/advanced-
dotnet-debugging-pdbs-and-symbols/
Debugging .NET Apps © Huma Qureshi
Q & A
Debugging .NET Apps © Huma Qureshi