PHP Debugging
Finding and reducing defects,
increasing code quality and becoming incredibly wealthy
What is debugging?
A methodical process of finding and reducing the number of bugs or defects.
- Understand code execution flow
- Understand program state
- Reproduce and correct bugs
Types of debugging
- Print debugging (echo/printf debugging)
- Post-mortem debugging (core dumps, stack traces)
- Remote debugging (attaching debugging software to a server)
Print logging
Beware being unable to find die()
and var_dump()
statements!
Consider simple logging class that can be enabled/disabled (Zend_Log
is recommended, but is
heavyweight)
Pretty goddamnn rudimentary, should be coalesced and output in an "authorised-only footer", overlay or email
to avoid mangling program output
Print logging
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
var_dump($array); die(__FILE__ . ' :: ' . __LINE__);
Print logging
array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" }
C:\Users\Andy\Documents\My Dropbox\_dev\vhosts\tmp.localh0.st\talk-PhpDebugging\test.php :: 14
Print logging with XDebug
array
'foo' => string 'bar' (length=3)
'bar' => string 'foo' (length=3)
C:\Users\Andy\Documents\My Dropbox\_dev\vhosts\tmp.localh0.st\talk-PhpDebugging\test.php :: 13
FirePHP
Firebug extension for firefox, also available for chrome
firephp-chrome
Nicer print debugging, XDebug-esque output formatting
Can be enabled in production (IP whitelist, application auth, server config) without risk of changing program
output (HTTP header-based)
FirePHP
FirePHP
Log Files/rSyslog
Passive logging - traditionally restricted to a single message per line
Can be useful for unknown errors in production, although emails/metrics gathering probably superior
Retricted verbosity means this is not usually a useful debugging technique, although it can help to indicate
problems
Remote (runtime) Debugging
The most valuable form of debugging (remote to process, not system)
Halt program execution, examine state, evaluate expressions, alter code
Very powerful - when exposed to the complexity of the flow of some software, the brain may melt
Remote (runtime) Debugging
-
Zend Debugger
not a fan, no experience, sorry
-
DBG
commercial, no experience
-
APD
Remote (runtime) Debugging
Xdebug
-
Very well maintained, excellent and responsive author (now works for MongoDB as PHP advocate/dev)
-
Automatic stack trace upon error
-
Function call logging
-
Display features such as enhanced
var_dump()
output and code coverage information
-
Code coverage integrates well with PHPUnit
-
Xdebug 2.2.0 is out (8/5/12)
Remote (runtime) Debugging
Xdebug
-
Configuration is the trickiest part! Ensure
idekey
and port match debugger settings and remote_enable =1
and
-
Trigger from browser with Cookies or Request parameters
-
Javascript Bookmarklets:
javascript:(/** @version 0.5.2 */function() {
document.cookie='XDEBUG_SESSION='+'PHPSTORM-XDEBUG'+';path=/;';}
)()
javascript:(/** @version 0.5.2 */function() {
document.cookie='XDEBUG_SESSION='+''+';expires=Mon, 05 Jul 2000 00:00:00 GMT;path=/;';}
)()
-
Trigger from CLI with environment variable
"XDEBUG_CONFIG=idekey=PHPSTORM-XDEBUG remote_enable=1 remote_host=127.0.0.1 remote_port=9000 remote_handler=dbgp remote_mode=req"
Go forth and Remotely Debug your PHP!