Debugging .NET Applications

41
Debugging .NET Applications Huma Qureshi Debugging .NET Apps © Huma Qureshi

description

Handouts for tips on how to debug .NET applications in visual studio.

Transcript of Debugging .NET Applications

Page 1: Debugging .NET Applications

Debugging .NET Applications

Huma Qureshi

Debugging .NET Apps © Huma Qureshi

Page 2: Debugging .NET Applications

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

Page 3: Debugging .NET Applications

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

Page 4: Debugging .NET Applications

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

Page 5: Debugging .NET Applications

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

Page 6: Debugging .NET Applications

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

Page 7: Debugging .NET Applications

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

Page 8: Debugging .NET Applications

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

Page 9: Debugging .NET Applications

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

Page 10: Debugging .NET Applications

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

Page 11: Debugging .NET Applications

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

Page 12: Debugging .NET Applications

VISUAL STUDIO DEBUGGING TOOLS

Debugging .NET Apps © Huma Qureshi

Debugee modules and symbols

Debugging .NET Apps © Huma Qureshi

Page 13: Debugging .NET Applications

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

Page 14: Debugging .NET Applications

Start debugging: Process running outside Visual Studio

Debugging .NET Apps © Huma Qureshi

Show the modules window

Debugging .NET Apps © Huma Qureshi

Page 15: Debugging .NET Applications

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

Page 16: Debugging .NET Applications

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

Page 17: Debugging .NET Applications

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

Page 18: Debugging .NET Applications

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

Page 19: Debugging .NET Applications

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

Page 20: Debugging .NET Applications

Data tips

Debugging .NET Apps © Huma Qureshi

“Refresh to enumerate” –function evaluation

Debugging .NET Apps © Huma Qureshi

Page 21: Debugging .NET Applications

Call stack: Show external code

Debugging .NET Apps © Huma Qureshi

Call stack: show parameter values

Debugging .NET Apps © Huma Qureshi

Page 22: Debugging .NET Applications

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

Page 23: Debugging .NET Applications

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

Page 24: Debugging .NET Applications

Step into specific

Debugging .NET Apps © Huma Qureshi

Immediate window

• Debugging unintegrated code

Debugging .NET Apps © Huma Qureshi

Page 25: Debugging .NET Applications

Tracepoints

Debugging .NET Apps © Huma Qureshi

Tracepoints

Debugging .NET Apps © Huma Qureshi

Page 26: Debugging .NET Applications

Conditional breakpoints

Debugging .NET Apps © Huma Qureshi

Conditional breakpoints

Debugging .NET Apps © Huma Qureshi

Page 27: Debugging .NET Applications

Viewing out of scope objects (Object ID)

Debugging .NET Apps © Huma Qureshi

Viewing out of scope objects (Object ID)

Debugging .NET Apps © Huma Qureshi

Page 28: Debugging .NET Applications

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

Page 29: Debugging .NET Applications

Q & A

Debugging .NET Apps © Huma Qureshi

Page 30: Debugging .NET Applications

BUILDING FOR DEBUGGABILITY

Debugging .NET Apps © Huma Qureshi

Build configurations

Debugging .NET Apps © Huma Qureshi

Page 31: Debugging .NET Applications

Building debug vs release

Debugging .NET Apps © Huma Qureshi

Building debug vs release:Default settings in proj file

Debugging .NET Apps © Huma Qureshi

Page 32: Debugging .NET Applications

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

Page 33: Debugging .NET Applications

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

Page 34: Debugging .NET Applications

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

Page 35: Debugging .NET Applications

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

Page 36: Debugging .NET Applications

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

Page 37: Debugging .NET Applications

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

Page 38: Debugging .NET Applications

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

Page 39: Debugging .NET Applications

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

Page 40: Debugging .NET Applications

Microsoft public symbols

Debugging .NET Apps © Huma Qureshi

.NET source stepping

Debugging .NET Apps © Huma Qureshi

Page 41: Debugging .NET Applications

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