68
1 Moaid Hathot Software Consultant http://moaid.codes How to become a debugging Jedi

How to become a .net debugging jedi

Embed Size (px)

Citation preview

Page 1: How to become a .net debugging jedi

1

Moaid HathotSoftware Consultanthttp://moaid.codes

How to become a debugging Jedi

Page 2: How to become a .net debugging jedi

222

• Software engineer, consultant and code Jedi• Developing Software professionally since 2013• Software Craftsmanship advocate• Clean Coder

@[email protected]://www.moaid.codes

About Me

Page 3: How to become a .net debugging jedi

333

About CodeValue

A leading software company• ~180 employees: more than 120 technology experts

• Provides high quality software development solutions• Turn-Key projects• Software development and consultation• Tailor-made courses and training

• Fields of expertise include:• Desktop & LOB applications• Cloud Computing• Advanced Mobile & Web Technologies• User Experience (UX) & User Interface (UI)• Application Lifecycle Management (ALM) and DevOps• Embedded & IoT

Page 4: How to become a .net debugging jedi

Agenda

IntroductionBreakpointsDebugging attributesDebugging multithreaded codeToolsPost-mortem debugging

4

Page 5: How to become a .net debugging jedi

555

The Original Bug Report

• “Debugging is the process of finding and resolving bugs or defects that prevent correct operation of computer software or a system”

• The terms "bug" and "debugging" are popularly attributed to Admiral Grace Hopper in the 1940s

• However the term is much older• Thomas Edison wrote the following words in a letter to an associate in 1878:

• “It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, then difficulties arise — this thing gives out and [it is] then that "Bugs" — as such little faults and difficulties are called — show themselves and months of intense watching, study and labor are requisite before commercial success or failure is certainly reached”

Page 6: How to become a .net debugging jedi

666

The Original Bug Report

Page 7: How to become a .net debugging jedi

777

Squandered time

Every minute spent in the debugger is a minute squandered.

Period.

Page 8: How to become a .net debugging jedi

888

Debugging is a time-travel

Page 9: How to become a .net debugging jedi

999

The Aha Moment• The two endpoints of debugging

1. What the heck.

2. Aha!

Page 10: How to become a .net debugging jedi

101010

Know your tools

Know your tools

Page 11: How to become a .net debugging jedi

111111

Know your tools

Know your tools

Page 12: How to become a .net debugging jedi

121212

Controlling The Debugger• There are 10 types of (human) debuggers:

• The “I am too tired to think, let the debugger lead and control” type

• The “everything is under control” type

Page 13: How to become a .net debugging jedi

131313

Rubber ducking

rubber duck debugging or rubber ducking is a method of debugging code

The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck

Page 14: How to become a .net debugging jedi

141414

Rubber ducking

rubber duck debugging or rubber ducking is a method of debugging code

The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck

Page 15: How to become a .net debugging jedi

15

Breakpoints

15

Page 16: How to become a .net debugging jedi

Conditional Breakpoints

16

Page 17: How to become a .net debugging jedi

171717

Filters

Page 18: How to become a .net debugging jedi

Tracepoints (VS)

18

Page 19: How to become a .net debugging jedi

Tracepoints (VS)

19

Page 20: How to become a .net debugging jedi

Method Breakpoints

Ctrl + BIf you type a method name (without class name)

VS select all that matches

20

Page 21: How to become a .net debugging jedi

212121

Conditional Breakpoint’s drawbacks

What are the drawbacks of Conditional Breakpoints?

Page 22: How to become a .net debugging jedi

222222

Debugger Anatomy

Wait for debug event

Handle debug event

Continue debug event

Start the debugger or attach to it

Enter main debugger loop

Page 23: How to become a .net debugging jedi

232323

Debugger Main Loop

1. …2. for(;;) 3. { 4. WaitForDebugEvent(DebugEv, INFINITE); 5. switch (DebugEv->dwDebugEventCode) 6. { 7. case EXCEPTION_DEBUG_EVENT: 8. switch(DebugEv->u.Exception.ExceptionRecord.9. ExceptionCode)10. { 11. case EXCEPTION_ACCESS_VIOLATION: 12. case …13. }

Source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681675(v=vs.85).aspx

Page 24: How to become a .net debugging jedi

242424

Debugger Main Loop

Source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681675(v=vs.85).aspx

1. break;2. case CREATE_THREAD_DEBUG_EVENT: 3. dwContinueStatus = 4. OnCreateThreadDebugEvent(DebugEv);5. break;6. …7. } 8. // Resume executing the thread that reported the9. debugging event. 10. ContinueDebugEvent(DebugEv->dwProcessId, 11. DebugEv->dwThreadId, 12. dwContinueStatus);13. }}

Page 25: How to become a .net debugging jedi

252525

Debugger break

Page 26: How to become a .net debugging jedi

262626

Debugger break

Page 27: How to become a .net debugging jedi

272727

Conditional Attribute

Page 28: How to become a .net debugging jedi

282828

Object ID’s

Page 29: How to become a .net debugging jedi

292929

Lambda Evaluation in Immediate & Watch Windows

Page 30: How to become a .net debugging jedi

30

Debugging Attributes

30

Page 31: How to become a .net debugging jedi

313131

Debugger Display Attribute

Page 32: How to become a .net debugging jedi

323232

Debugger Display Attribute

Page 33: How to become a .net debugging jedi

333333

Debugger Display Attribute

Page 34: How to become a .net debugging jedi

343434

Debugger Type Proxy Attribute

Page 35: How to become a .net debugging jedi

353535

Debugger Browsable Attribute

Page 36: How to become a .net debugging jedi

363636

Debugger Visualizer

Page 37: How to become a .net debugging jedi

373737

Debugger Visualizer

Page 38: How to become a .net debugging jedi

38

Multithreading

38

Page 39: How to become a .net debugging jedi

Staying on the same thread (freezing other threads)

39

Page 40: How to become a .net debugging jedi

Finding Tasks Deadlocks

Debug –> windows –> Tasks

40

Page 41: How to become a .net debugging jedi

414141

Parallel Task & Parallel Stacks

View the logical call, the logical call stack and the task/thread state

Page 42: How to become a .net debugging jedi

424242

Parallel Watch - View local variable in each thread’s context

Page 43: How to become a .net debugging jedi

434343

Concurrent Visualizer

https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ConcurrencyVisualizerforVisualStudio2015

Page 44: How to become a .net debugging jedi

444444

Concurrent Visualizer

https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ConcurrencyVisualizerforVisualStudio2015

Page 45: How to become a .net debugging jedi

45

OzCode

45

Page 46: How to become a .net debugging jedi

LINQ Debugging

46

Page 47: How to become a .net debugging jedi

Predicting the future

Predict

47

Page 48: How to become a .net debugging jedi

Custom Expressions

48

Page 49: How to become a .net debugging jedi

Show All Instances

49

Page 50: How to become a .net debugging jedi

Tracepoints (OzCode)

50

Page 51: How to become a .net debugging jedi

Conditional Breakpoints (OzCode)

51

invoice.Id 915486 intinvoice.Id 915486 int

Page 52: How to become a .net debugging jedi

OzCode Comapre

52

Page 53: How to become a .net debugging jedi

Export

53

Page 54: How to become a .net debugging jedi

Give it a try

https://github.com/oz-code/OzCodeDemo

Page 55: How to become a .net debugging jedi

55

System Internals

55

Page 56: How to become a .net debugging jedi

Where to download

Browser: http://Live.sysinternals.comShared folder: \\live.sysinternals.com\tools

56

Page 57: How to become a .net debugging jedi

Process Explorer

57

Page 58: How to become a .net debugging jedi

Process Monitor

58

Page 59: How to become a .net debugging jedi

59

Post-Mortem Debugging

59

Page 60: How to become a .net debugging jedi

606060

Dump Files

• A minidump is a snapshot of a process– May be created at any time, not just when a process crashes

• Minidump types– Kernel minidumps (not relevant for this course)– Basic (usually enough for native processes)– Full (required to get useful info for managed processes)

Page 61: How to become a .net debugging jedi

616161

Dump Creation

• Minidump creation– WinDbg– On Vista, 2008 and up can use Task Manager– ADPlus– ProcDump

• Opening Dump files– WinDbg– Visual Studio 2012/2013

Page 62: How to become a .net debugging jedi

62

Automatic Dump Creation

To enable it, set the reg key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\<appName>

DumpFolder – Location for the output dumpsDumpCount – Maximum amount of dumps in the folderDumpType

1 – Mini Dump2 – Full Dump0 – Custom Dump

CustomDumpFlags – if DumpType is 0, set the dump options

Page 63: How to become a .net debugging jedi

Debugging using the Dump (1)

63

Page 64: How to become a .net debugging jedi

Debugging using the Dump (2)

64

Page 65: How to become a .net debugging jedi

666666

Generating Symbol Tables

1. Open DotPeek2. Add the required assembly 3. Press start Symbol server4. In VS add new symbol source

Page 66: How to become a .net debugging jedi

676767

Debugging is Hard

Debugging is HARD.Even the Pros get befuddled

sometimesUse the right tools to decrease your Time-to-Aha factor

Page 67: How to become a .net debugging jedi

686868

Kahoot it

Use your browser to visit:https://kahoot.it

Page 68: How to become a .net debugging jedi

69 69

Presenter contact detailst: @MoaidHathote: [email protected]: http://www.moaid.codesw: www.codevalue.net

Free 3 months of OzCodehttp://tinyurl.com/jpn4y96