Debugging PHP with Xdebug
Configure Debugging
You have to install the Xdebug package, via apt
or pecl
.
apt-get install libedit-dev php5-dev php5-xdebug
pecl install xdebug
Edit the Xdebug PHP Configuration, it will be one of the following files, depending on your distribution.
cat /etc/php5/conf.d/xdebug.ini /etc/php/apache2-php5.5/ext-active/xdebug.ini /etc/php/cli-php5.5/ext-active/xdebug.ini zend_extension=/usr/lib/php5/20090626/xdebug.so xdebug.remote_autostart=1 xdebug.remote_enable=1 xdebug.remote_host="localhost" xdebug.remote_port="9000"
Debugger CLI from Source
Make sure you download the source version that matches the Xdebug that your system has.
cd /usr/src wget http://xdebug.org/files/xdebug-2.2.3.tgz tar -zxf xdebug-2.2.3.tgz cd xdebug-2.2.3 phpize ./configure make make install cd ./debugclient ./configure --prefix=/opt/xdebug --with-libedit make make install
Debugger CLI from Git
git clone https://github.com/derickr/xdebug /usr/src/xdebug cd /usr/src/xdebug/debugclient autoconf ./configure --prefix=/opt/xdebug --with-libedit
Breakpoints in Code
Similar to the breakpoints for JS.
xdebug_break();
How to use Xdebug to debug and profile your PHP applications
Profiling
With Xdebug profiling enabled it's a simple process to collect detailed information on application performance.
This configuration example below sets the profiler to only work with the proper trigger is set. This way we can run the profiler on a production system, just not for every request. This keeps the load on the system a bit lighter.
zend_extension=xdebug.so xdebug.profiler_enable = 0 xdebug.profiler_enable_trigger = 1 xdebug.profiler_append = 0 xdebug.profiler_output_dir = /tmp/phprof xdebug.profiler_output_name = xdebug.%R.out
Enable Profiling
To enable profiling the input parameter XDEBUG_PROFILE must be set via GET, POST or COOKIE; any value is acceptable (we generally use "1"). To start profiling, try appending the query string like.
curl -v http://test.domain.tld/?XDEBUG_PROFILE=1
It's possible for your PHP scripts to set this parameter, not for the calling script but for the next request. The example below sets a session cooke COOKIE when the GET parameter is seen.
if (!empty($_GET['XDEBUG_PROFILE'])) setcookie('XDEBUG_PROFILE','1');
Or it's possible to debug only some requests, perhaps some AJAX portions, assuming this jQuery.
$('#item-edit').load('/block/page.php?XDEBUG_PROFILE=1'); $.post('/api/user.php',{XDEBUG_PROFILE:1,a:'save',id:2,name:'Edoceo'); $.get('/api/user.php',{XDEBUG_PROFILE:1,id:2,name:'Edoceo');
Debugging
Xdebug also comes with tools to improve the error logging and reporting of PHP. Here are some annotated settings for the debugger and trace files to assist with debugging PHP applications. The collected traces will contain lots of detail on the causes of the errors.
- collect_params
- Show the parameters to functions in the trace
- dump_globals
- We disable cause it makes huge spew
- extended_info
- Disabled cause its very slow
xdebug.auto_trace = 0 xdebug.collect_params = 4 xdebug.collect_return = 1 xdebug.dump.COOKIE = * xdebug.dump.GET = * xdebug.dump.POST = * xdebug.dump_globals = 0 xdebug.extended_info = 0 xdebug.overload_var_dump = 0 xdebug.show_exception_trace = 1 xdebug.trace_enable_trigger = 1 xdebug.trace_output_dir = /tmp/phprof/xtrace xdebug.trace_output_name = %R.out
git clone https://github.com/edoceo/phprof.