debugging for beginners · debugging for beginners rob allen may 2013. debugging debugging is a...

48
Debugging for beginners Rob Allen May 2013

Upload: others

Post on 29-Sep-2020

34 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Debugging for beginnersRob AllenMay 2013

Page 2: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

DebuggingDebugging is a methodical process of finding andreducing the number of bugs, or defects, in acomputer program thus making it behave asexpected.

Wikipedia

Page 3: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

BugsThere are two types of bugs:

Trivial and very very difficult.

Page 4: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

The 6 stages of debugging

1. That can’t happen.2. That doesn’t happen on my machine.3. That shouldn’t happen.4. Why is that happening?5. Oh, I see.6. How did that ever work?

— John Chang, 2003

Page 5: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

The debugging process

• Reproduce• Diagnose• Fix• Reflect

Page 6: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

ReproduceCan you make the error happen on demand?

Page 7: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Where to start?

• Don’t trust the bug report!• Find out what the correct operation is expected to

be!• Only ever work on one problem at a time!• Check simple things first.• Ask colleagues about problem area.

Page 8: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Reproduce

• Does it fail on the latest version?• Does it fail on reported version?• Match environment as closely as possible.• Assume user didn’t do as expected.• Last resort: add some logging and wait for new bug

report!

Page 9: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

RefineReduce the bug to the smallest possible number ofsteps

If it appears to be non-deterministic, it almost certainlycan be made deterministic

Automate the bug - create a unit test!

Page 10: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

DiagnoseInvestigate the error and work out what has to be

done!

Page 11: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Types of errorsThe ones PHP tells you about

Read any error messages and logs

The rest!Think & experiment!

Page 12: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Set up PHP to help you!

• Configure php.ini• Install Xdebug

Page 13: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Useful php.ini settingserror_reporting = E_ALL | E_STRICT

display_errors = On

display_startup_errors = On

html_errors = On

log_errors = 1

error_log = /path/to/php_error.log

Page 14: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Xdebug

• var_dump() override• set breakpoints• inspect variables

Get it from http://xdebug.org (or your distro!)

Page 15: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Xdebug settingsxdebug.var_display_max_children = 99999

xdebug.var_display_max_data = 99999

xdebug.var_display_max_depth = 10000

Page 16: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

xdebug.scream = 1

(This will save you hours)

Page 17: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Xdebug settingsEnsure Xdebug’s output is always readable regardlessof your designer!

Set a custom CSS file in your browser and add this:

table.xdebug-error th,

table.xdebug-error td {

color: black;

}

Page 18: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Types of error messages

• Fatal errors• Syntax errors• Recoverable errors• Warnings• Notices• Deprecation notices

Don’t ignore any!

Page 19: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Reading error messages

• Actually read the error!

• it’s usually right• backtraces from Xdebug!

• Only worry about the first error

Page 20: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

A Fatal errorFatal error: Call to undefined function datee() in//www/localhost/test.php on line 12

Page 21: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Xdebug display

Page 22: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Exceptions

Page 23: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

ExceptionsLook for a previous exception!

$previousException = $e->getPrevious();

Page 24: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

The other types of error

• Logical errors• It doesn’t do what the user expects

These are solved by experimentation and investigation

Page 25: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Var Dump DebuggingQuick and easy:

var_dump($comment);

exit;

Page 26: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Var Dump Debugging

(If you don’t have xdebug, then wrap in <pre> tags)

Page 27: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a
Page 28: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Divide and conquer

• Find halfway in process and inspect at that point• Find halfway in correct half and inspect there• etc.

Page 29: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Divide and conquer via git

• find a known working commit hash• git bisect until you find the commit that caused

the problem• Read the diff carefully.

Page 30: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Choose logical check pointse.g.

• Test values sent into script• Test storage• Test retrieval• Test display

Page 31: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Step by step with Xdebug

• Add xdebug_break() when you want stop.• Run in browser• debugger will kick in when break point reached.

Page 32: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

MacGDBp

Page 33: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Logging

• Long term error reporting & tracing.• Different levels for different types of message.• I use Zend\Log. Also consider monolog.

Page 34: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Zend\Log setup// setup

use Zend\Log\Logger;

use Zend\Log\Writer\Stream as LoggerStream;

$log = new Logger;

$writer = new LoggerStream($filename);

$log->addWriter($writer);

Page 35: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Zend\Log setup (2)// Log PHP errors & exceptions too

Logger::registerErrorHandler($log);

Logger::registerExceptionHandler($log);

Page 36: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Zend\Log in use$logger->log(Logger::INFO, 'My message');

// levels:

// * EMERG * WARN

// * ALERT * NOTICE

// * CRIT * INFO

// * ERR * DEBUG

Page 37: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

FixA quality update is worth the effort!

Page 38: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Know the root causeNever change the source unless you know why whatyou’re doing fixes the problem

Page 39: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Clean up firstStart from a clean source tree - save you what youneed first

git reset is good for this.

Page 40: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Create your test(s)

1. Add your new test(s)2. Run them to prove that they fail3. Fix the bug4. Run the tests to prove that they pass5. Run the full suite to ensure no (known) regressions

Page 41: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

RefactorThe golden rule of refactoring is to not changefunctionality.

Therefore refactor before or after fixing the bug.

Page 42: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

CommitIf it’s not in source control, then it hasn’t happened.

Page 43: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

ReflectMake sure it doesn’t happen again!

Page 44: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

What went wrong?

• Requirements / spec• Architecture / design• Construction• Testing

Page 45: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Change your dev practices?

• Coding standards• Pair programming / code reviews• Developer documentation• Staff training• Unit testing!• Refactor

Page 46: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Historical records

• Log every bug in your bug tracker!

Page 47: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Questions?

Page 48: Debugging for beginners · Debugging for beginners Rob Allen May 2013. Debugging Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a

Thank youhttps://joind.in/8184

Rob Allen : http://akrabat.com : @akrabat