debugging effectively - php uk 2017

Post on 21-Feb-2017

107 Views

Category:

Software

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Debugging Effectively

Colin O’Dell

@colinodell

Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe

Colin O’Dell

• Lead Web Developer at Unleashed Technologies• PHP League Member• league/commonmark• league/html-to-markdown

• PHP 7 Upgrade Guide e-book

@colinodell

OverviewI. Importance of debuggingII. Debugging processIII. Tools & TechniquesIV. Q&A

Photo by Roman Boed // cc by 2.0 // https://flic.kr/p/ngwcf1

@colinodell

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

PlanningCoding

@colinodell

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

Planning

Testing & Debugging

Coding

@colinodell

Junior Developers• Try the “usual” steps• bin/console cache:clear• composer install• chmod –R 777 *

• Ask somebody else• Co-worker• Google• StackOverflow post

Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL

@colinodell

XY ProblemPhoto by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL

•I want to solve problem X•How do I solve X?•Solution Y might work•How can I do Y?

@colinodell

Photo by quattrostagioni // cc by 2.0 // https://flic.kr/p/aGjVq8

1.Don’t parse HTML with regex2.Solve problems the right way

@colinodell

Bugs are logical

Photo by Photofest

@colinodell

“The bug is not moving around in your code, trying to trick or evade you. It is just siting in one place, doing the wrong thing in the same

way every time.” – Nick Parlante, Debugging Zen

Photo by GrahamC57 // cc by-nc-nd 2.0 // https://flic.kr/p/cwJi9Q

@colinodell

1. Gather information

• Expected behavior vs.actual behavior• Error messages• Stack traces

Photo from youmustdesireit.wordpress.com

• Screenshots•Browser & OS•Date & time• Log entries

@colinodell

2. Replicate the Issue

Be able to replicate with 100% certainty

Photo by Nick Royer // cc by-sa // https://flic.kr/p/d41ASC

@colinodell

3. Identify the CulpritPhoto by U.S. Navy // cc 2.0 // https://flic.kr/p/n6Wgks

• Be methodical• Make no assumptions• Understand the bug

@colinodell

4. Fix it & Re-test• Attempt to replicate again• Avoid XY problem• No temporary workarounds!• Add technical debt• May introduce other issues• Never get replaced with true solutions

Photo by Jeff Eaton // cc by-sa 2.0 // https://flic.kr/p/b33rSx

@colinodell

5. Mitigate Future Occurrences•Add an automated test• Share your new knowledge• Project documentation• Blog post• StackOverflow

• Submit patch upstream

Photo by marcokalmann // cc by-nc-nd 2.0 // https://flic.kr/p/4CqLMQ

@colinodell

Long-Term Results

• Gain experience• Learn how the system works• Build heuristics• Boost confidence

Photo by Glenn Beltz // cc by 2.0 // https://flic.kr/p/i7Csdx

@colinodell

Two essential tools

• Integrated development environment (IDE)• Interactive debugger

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

@colinodell

Integrated Development Environment•Minimum features:•Syntax highlighting•Auto-completion•Fast code navigation•Debugger

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

@colinodell

Interactive Debugger

•Pause code execution•Breakpoints•Conditional breakpoints

•Step through execution•Examine variables•Explore call stack

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

@colinodell

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

@colinodell

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

1. Trace backwards•Error is thrown from known location•Use a debugger•Establish context•Work backwards

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

2. Trace forwards•Opposite direction•Problematic lineisn’t known•Use debugger or logging

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

2. Trace forwards•Opposite direction•Problematic lineisn’t known•Use debugger or logging

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

2. Trace forwards•Opposite direction•Problematic lineisn’t known•Use debugger or logging

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

2. Trace forwards•Opposite direction•Problematic lineisn’t known•Use debugger or logging

a()

b()

c()

d()

Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw

3. Divide & Conquer• Identify different code sections• Set breakpoints at the boundaries• Isolate issue to one particular area• Focus efforts on that area

@colinodell

@colinodell

@colinodell

@colinodell

X@colinodell

@colinodell

4. Use tools•Variable dumps•Debug toolbars•Console utility•Profilers•git bisect

•netcat•curl• strace•etc.

Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC

@colinodell

Performance Profiling

Identify slowness:• Bottlenecks• Resource hogs• Inefficient code

Photo by Alan Stark // cc by-sa 2.0 // https://flic.kr/p/dn3qjx

Tools:• Blackfire (freemium)• New Relic (freemium)• xhprof (open-source)

@colinodell

git bisect

v1.7 ? ? ? ? ? ? ? HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 ? ? ? ? ? ? ? HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 ? ? ? BAD ? ? ? HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 ? ? ? BAD BAD BAD BAD HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 ? GOOD ? BAD BAD BAD BAD HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 GOOD GOOD ? BAD BAD BAD BAD HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

@colinodell

git bisect

v1.7 GOOD GOOD X BAD BAD BAD BAD HEAD

Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm

abcd123 is the first bad commit @colinodell

5. Get help•RTFM / RTFD•Project forums or issue queue• StackOverflow, IRC, etc.•Ask a colleague• Expert in that area• Senior developer

•Rubber ducking

Photo by Hiromitsu Morimoto // cc by-sa 2.0 // https://flic.kr/p/6Vzzaa

@colinodell

6. Take a break

Photo by Kristina Alexadnerson // cc by-nc-nd 2.0 // https://flic.kr/p/cFEcDC

• Clear your mind; start fresh• Forget invalid assumptions• Recharge your batteries• Let your subconscious work on it

@colinodell

Four things to walk away with

1. Computers aren’t random,and neither are bugs

2. Persistence will always pay off3. Don’t be afraid to dive deep4. Don’t make assumptions or

take things for granted

Photo by Kristina Alexadnerson // cc by-nc-nd 2.0 // https://flic.kr/p/dvEpfY

@colinodell

Learn More• https://web.duke.edu/cps001/notes/Debugging.pdf• http://www.fiveminutegeekshow.com/20• http://blog.codeunion.io/2014/09/03/teaching-novices-how-to-debug-co

de/• https://www.jetbrains.com/phpstorm/help/debugging.html• http://www.sitepoint.com/debugging-git-blame-bisect/• http://unix.stackexchange.com/a/50099/80744• http://codeception.com/docs/01-Introduction• http://chadfowler.com/blog/2014/01/26/the-magic-of-strace/ • http://c2.com/cgi/wiki?RubberDucking

Photo by Samantha Marx // cc by 2.0 // https://flic.kr/p/8KrU1R

@colinodell

top related