debugging effectively - symfonylive san francisco 2015

61
Photo by Joseph B // cc by- nc - nd 2.0 // https://flic.kr/p/7GAMBe Debugging Effectively Colin O’Dell

Upload: colin-odell

Post on 09-Feb-2017

756 views

Category:

Software


0 download

TRANSCRIPT

Page 2: Debugging Effectively - SymfonyLive San Francisco 2015

Colin O’Dell• Lead Web Developer at Unleashed Technologies• PHP developer since 2002• Certifications: Symfony and Magento• PHP League Member• league/commonmark• league/html-to-markdown

• What’s New in PHP 7 e-book• @colinodell / www.colinodell.com

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

Page 3: Debugging Effectively - SymfonyLive San Francisco 2015

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

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

Page 7: Debugging Effectively - SymfonyLive San Francisco 2015

PlanningCoding

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

Page 8: Debugging Effectively - SymfonyLive San Francisco 2015

Planning

Testing & Debugging

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

Page 13: Debugging Effectively - SymfonyLive San Francisco 2015

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

• Google the error• Try every solution

• Ask somebody else• Co-worker• StackOverflow post

• Give up

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

Page 15: Debugging Effectively - SymfonyLive San Francisco 2015

XY Problem

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

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

Page 16: Debugging Effectively - SymfonyLive San Francisco 2015

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

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

Page 18: Debugging Effectively - SymfonyLive San Francisco 2015

Bugs arelogical

Photo by Photofest

Page 19: Debugging Effectively - SymfonyLive San Francisco 2015

“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

Page 22: Debugging Effectively - SymfonyLive San Francisco 2015

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

Photo from youmustdesireit.wordpress.com

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

1. Gather Information

Page 23: Debugging Effectively - SymfonyLive San Francisco 2015

2. Replicate the Issue

Be able to replicate with 100% certainty

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

Page 24: Debugging Effectively - SymfonyLive San Francisco 2015

• Be methodical• Make no assumptions• Understand the bug

Photo by U.S. Navy // cc 2.0 // https://flic.kr/p/n6Wgks

3. Identify the Culprit

Page 25: Debugging Effectively - SymfonyLive San Francisco 2015

• 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

4. Fix & Re-test

Page 26: Debugging Effectively - SymfonyLive San Francisco 2015

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

Page 28: Debugging Effectively - SymfonyLive San Francisco 2015

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

Page 30: Debugging Effectively - SymfonyLive San Francisco 2015

Two essential tools

• Integrated development environment (IDE)

• Interactive debugger

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

Page 31: Debugging Effectively - SymfonyLive San Francisco 2015

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

Page 32: Debugging Effectively - SymfonyLive San Francisco 2015

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

Page 35: Debugging Effectively - SymfonyLive San Francisco 2015

1. Trace backwards•Use a debugger• Identify source of error•Establish context•Work backwards

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

Page 38: Debugging Effectively - SymfonyLive San Francisco 2015

2. Divide & Conquer

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

Page 39: Debugging Effectively - SymfonyLive San Francisco 2015
Page 40: Debugging Effectively - SymfonyLive San Francisco 2015
Page 41: Debugging Effectively - SymfonyLive San Francisco 2015
Page 42: Debugging Effectively - SymfonyLive San Francisco 2015

X

Page 43: Debugging Effectively - SymfonyLive San Francisco 2015
Page 44: Debugging Effectively - SymfonyLive San Francisco 2015

3. Use tools• VarDumper• Debug toolbars• Console utility• Profilers• git bisect

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

•netcat•curl• strace•etc.

Page 48: Debugging Effectively - SymfonyLive San Francisco 2015

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)

Page 49: Debugging Effectively - SymfonyLive San Francisco 2015

git bisect

v1.7 ? ? ? ? ? HEAD

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

Page 50: Debugging Effectively - SymfonyLive San Francisco 2015

git bisect

v1.7 ? ? BAD ? ? HEAD

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

Page 51: Debugging Effectively - SymfonyLive San Francisco 2015

git bisect

v1.7 ? ? BAD BAD BAD HEAD

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

Page 52: Debugging Effectively - SymfonyLive San Francisco 2015

git bisect

v1.7 GOOD ? BAD BAD BAD HEAD

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

Page 53: Debugging Effectively - SymfonyLive San Francisco 2015

git bisect

v1.7 GOOD X BAD BAD BAD HEAD

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

abcd123 is the first bad commit

Page 55: Debugging Effectively - SymfonyLive San Francisco 2015

4. 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

Page 57: Debugging Effectively - SymfonyLive San Francisco 2015

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

Page 59: Debugging Effectively - SymfonyLive San Francisco 2015

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-code/• https://www.jetbrains.com/phpstorm/help/debugging.html• http://

symfony.com/doc/current/components/var_dumper/introduction.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

Page 61: Debugging Effectively - SymfonyLive San Francisco 2015