EFFECTIVE DEBUGGING
SPEND TIME FIXING PROBLEMS, NOT FINDING THEM
/AndyDawson @AD7six
THE WEB IS JUST PLUMBING WITH BYTES
Image:Wikipedia
OF COURSE IT'S NOT REALLY THAT SIMPLE
Image:Flickr.com
↖ The hardestproblems to fix existhere.
In the developer's head.
FIND IT WITH A HAMMER
Image:Codinghorror.com
//pickone
thrownewException('Madeithere!');
print_r(debug_backtrace());
die(__FILE__.':'.__LINE__);
MOST APPS AREN'T THAT SIMPLE
Ahammer willwork, butitmighttake awhile.
Image:energysystemsgroup.com
HELLO WORLD EXAMPLE
"Where's mywebpage"
CHECK THE HTTP RESPONSE CODE
Is itactually an error?
READ THE WEBSERVER ERROR LOG
Location depends on configuration
Typically/var/log/*/
PHPParseerror: [...]in/var/www/example.dev/public/index.phponline4"
IDENTIFY THE PROBLEM
$cat/var/www/example.dev/public/index.php
<?php
echo"helloworld';
Parseerrorsareoftenthelinebefore(orearlier)inafile
NOT AN ERROR EXAMPLE
Notan error response code so no (direct) logmessages:"
GREP FOR IT
$grep-rl"NotFound"*
...
src/Really/Not/Obvious/File.php
$grep-r"NotFound"*
...
src/Really/Not/Obvious/File.php 404=>'NotFound',
$catsrc/Really/Not/Obvious/File.php
...
functionerror(){
die($this->_statusCodes['404']);
}
CAKEPHP HELLO WORLD EXAMPLE
"Whythe Four Oh Four?"
CHECK THE ERROR LOG
app/tmp/error.log
IDENTIFY THE PROBLEM
$catapp/View/Pages/home.ctp
<?php
...
if(!Configure::read('debug')):
thrownewNotFoundException();
endif;
DEBUG BASICS
Configand functions everydeveloper should know about
PHP CONFIG
Inifile settings are the defaults if notchanged (duh)
display_errors(On/Off)
log_errors(On)
display_startup_errors(Off)
error_reporting(On/Off)
Runtime settings override the inifile -no effectif the file theyare
in has aparse error (duh)
ini_set('display_errors', 0/1)
PHP FUNCTIONS
print_r();
debug_backtrace();
get_included_files();
phpinfo();
PHP VARIABLES/CONSTANTS
__FILE__
__LINE__
$_SERVER
et.al.
A WAY TO REPRODUCE
Have awayto consistentlyreproduce the error
$curl-Ihttp://example.dev/
HTTP/1.1500InternalServerError
Server:nginx
Date:Sat,23Aug201410:33:46GMT
Content-Type:text/html
gitbisect-find regression errors quickly
XDEBUG
peclinstallxdebug
WEBGRIND
Turn on xdebugprofiling, and look atwhatarequestis doing
CLI DEBUGGING
Pause execution with read:
print_r($somethingInteresting);
`readfoo`;
Usefulwhen debuggingaloop.
NOT CAKEPHP CODE?
Aghetto debugfunction:
functiondebug($var,$showHtml=null){
if(!defined('DEBUG')||!DEBUG){return;}
if($showHtml===null){
$showHtml=php_sapi_name()==='cli'?false:true;
}
$var=var_export($var,true);
if(!$showHtml){
echo$var;
return;
}
echo'<pre>'.htmlspecialchars($var).'</pre>';
}
createatracefunction(Debugger::trace)tooifneeded
LOGIC AIDES
Justvoicingaproblem can find the solution/error
Image:Wikipedia
ERROR MESSAGES DON'T LIE
PHP Parse error: syntax error, unexpected '$bar'(T_VARIABLE)
in parse.php on line 3
$catfoo.php
<?php
...
if($foo||bar){
There's an accidentalnone-breakingspace on thatline
NETWORKING PROBLEMS
If the problem is noton the webserver -where is it?
NAMESERVER PROBLEMS
No response from nameservers is the same as adomain not
existsing
$digcakefest.org
;<<>>DiG9.8.3-P1<<>>cakefest.org
;;globaloptions:+cmd
;;Gotanswer:
;;->>HEADER<<-opcode:QUERY,status:SERVFAIL,id:15266
;;flags:qrrdra;QUERY:1,ANSWER:0,AUTHORITY:0,ADDITIONAL:0
;;QUESTIONSECTION:
;cakefest.org. IN A
;;Querytime:4142msec
;;SERVER:8.8.8.8#53(8.8.8.8)
;;WHEN:TueAug1916:26:592014
;;MSGSIZE rcvd:30
...
;;ANSWERSECTION:
cakefest.org. 1568 IN A 50.56.232.22
...
DNS PROBLEMS
"No route to host"means the ip requested isn'taccessible
$traceroutecakefest.org
traceroutetocakefest.org(50.56.232.22),64hopsmax,52bytepackets
1 172.26.81.1(172.26.81.1) 1.032ms 0.912ms 0.912ms
2 192.168.0.1(192.168.0.1) 2.777ms 1.267ms 2.338ms
...
12 rackspace-ic-302090-dls-bb1.c.telia.net(62.115.33.78) 152.340ms 15
9.367ms 146.339ms
13 ***
14 ***
15 ***
If there are stars -there be problems
BACKEND SERVER IS
DOWN
502 Bad Gateway
E.g. php-fpm or hhvm is notrunning
Willbe in the webserver error log
There willnotbe anyapplication logentries
PITFALLS TO AVOID
Whatnotto do when debuggingcode
FIX PROBLEMS, NOT SYMPTOMS
Don'tignore errors/warnings/notices
Fix them in order, some errors willbe the concequence of earlier
problems.
COMMON MISTAKES
Notlookingfor error logs
Readingor focussingon the wrongerror message
Notreadingthe error message
Notreadingthe error message aloud
Misinterprettingthe error message
Stoppingdebuggingtoo early.
If "the problem"is aclass/function with source code -debug the
source code of thatfunction
New user: Ifound the problem, it's
app/webroot/index.php!
IMPLICIT ASSUMPTIONS
Be waryof implictassumptions
You're debuggingthe same file the browser is loading
You're debuggingthe same application the browser is loading
You're debuggingthe same server the browser is loading
You're debuggingthe same requestthe browser is loading
Conclusions so far are accurate
WTF?Backup, and re-verifyeverything.
COMMON PROBLEMS
And how to debug/identifythem
NO MODREWRITE
Don'tlook atphp files -the error is atthe webserver.
Enable mod rewrite
Restartthe webserver
CAKEPHP AUTOMODELS
$model=ClassRegistry::init('MyModel');
$model->methodName();
SQLError:1064:YouhaveanerrorinyourSQLsyntax;[...]
check[...]fortherightsyntaxtousenear'methodName'
MyModeldoes nothave the function methodName
MyModelhas no behaviour bound implementing
methodName
$modelis an instance of AppModel
CAKEPHP AUTOMODELS - IDENTIFICATION
$model=ClassRegistry::init('MyModel');
debug(get_class($model));
##########DEBUG##########
'MyModel'
###########################
$model=ClassRegistry::init('MyModel');
debug(get_included_files());
##########DEBUG##########
array(
...
...app/Model/MyModel.php
...
)
###########################
CAN'T FIX IT
(╯°□°)╯︵ ┻━┻
Can'tfind the problem/solution?-gethelp. Butfirst:
Collectthe information you've got
Write astandalone, reproducible example if possible
Reduce the question to it's simplest, form
Don'tover simplifyor make acontrived example
Ask colleagues/the internet
Brace for impact
Stack overflow, the google group and irc as greatplaces to get
help
SUMMARY
Identifythe rightpartof arequestto debug
Fix errors in order
Check your assumptions/conclusions ateveryWTF
Formulate aquestion, and Ask for help
Profit!

Effective debugging