Xdebug trace != profile

So it just took me all night to figure out that in Xdebug ‘tracing’ is different to ‘profiling’.

It didn’t help that the xdebug.trace_output_name included an example format of ‘cachegrind.out.%s’. Why would you name a trace file cachegrind.out if only profile files are in cachegrind format!?

In my efforts to get profiling to work I found myself loading trace files into KCachegrind and getting the rather unhelpful message: “Could not open the file “/run/shm/xdebug/trace…xt”. Check it exists and you have enough permissions to read it.”

So the error message is about file permissions. I checked and checked and checked again but the file permissions were OK. The problem was I was loading a trace file, not a profile file.

So I gave up on KCachegrind and tried to get webgrind to work. Turns out loading a trace file (not a profile file) into webgrind doesn’t work either. I got the same problem reported by Sven about ‘parsers is undefined’. I fucked around trying to update the jquery.tablesorter plugin in webgrind, but the problem was due to the fact I was loading a tracing file, not a profiling file.

In the end I got it all figured out. Here’s my /etc/php5/mods-enabled/xdebug.ini file:

zend_extension=/usr/lib/php5/20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
;xdebug.remote_host=127.0.0.1
xdebug.remote_host=10.1.1.203
;xdebug.remote_port=9000
xdebug.remote_port=9999

; http://www.xdebug.org/docs/all

xdebug.profiler_enable=1
xdebug.profiler_output_dir=/run/shm/xdebug
xdebug.profiler_output_name=cachegrind.out.%t.%R

xdebug.auto_trace=1
xdebug.collect_assignments=1
xdebug.collect_includes=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.collect_vars=0
xdebug.show_mem_delta=1
;2 for HTML:
xdebug.trace_format=0
xdebug.trace_options=1
xdebug.trace_output_dir=/run/shm/xdebug
xdebug.trace_output_name=trace.%t.%R
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1

The above config supports generation of tracing files and profiling files. I can now load my profiling files into KCachegrind and webgrind.

Everything is easy when you know how!

Eclipse and XDebug NIGHTMARE

Welcome to my nightmare.

To see listeners:

 # netstat -anp | grep LISTEN | less
 # nmap localhost

To use IPv4 instead of IPv6 in Eclipse, edit:

/home/jj5/.eclipse/org.eclipse.platform_3.8_155965261/configuration/config.ini

And add:

 # https://lists.debian.org/debian-java/2010/03/msg00073.html
 java.net.preferIPv4Stack=true

More to come…

Debugging PHPUnit tests in Eclipse PDT with XDebug on Debian GNU/Linux

Wow, this was complicated!

Make sure PHP, Xdebug, etc. are installed:

 # apt-get install php php5-xdebug php5-curl php5-mysql php5-gd

Install Eclipse:

 # apt-get install eclipse

To install PHP Developer Tools in Eclipse: Open Eclipse, click Help -> Install New Software…

Work with: http://download.eclipse.org/tools/pdt/updates/release

Select PHP Development Tools / PHP Development Tools (PDT) and install.

Create a new PHP project in Eclipse.

Download PHPUnit into your project folder, e.g.:

 $ cd ~/workspace/new-project
 $ wget https://phar.phpunit.de/phpunit.phar
 $ chmod +x phpunit.phar
 $ cp phpunit.phar /usr/local/bin/phpunit

In your Eclipse PHP project right-click on your project and select “Include Path” -> “Configure Include Path”.

Click “Libraries” -> “Add External PHARs” then add “phpunit.phar” (in your project’s workspace).

In Eclipse click “Run” -> “Debug Configurations”. Click “PHP CLI Application” and then “New”. Enter the PHP Script name as “PHPUnit” with Project default PHP: PHP CLI (Xdebug 5.4.4 CLI). Set the PHP file as “/project-name/phpunit.phar”.

Edit your php.ini file, e.g.:

 # vim /etc/php5/cli/php.ini

And make sure to specify an xdebug configuration (append to end of file is OK):

[xdebug]
zend_extension=/usr/lib/php5/20100525/xdebug.so
xdebug.remote_enable=On
;xdebug.remote_host="localhost"
xdebug.remote_host=localhost
;xdebug.remote_port=9999
;xdebug.remote_port=9000
xdebug.remote_port=9999
xdebug.remote_handler="dbgp"
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/var/tmp"

xdebug.default_enable = on

xdebug.remote_autostart=on
xdebug.remote_mode = "req"
xdebug.remote_connect_back = on
xdebug.remove_log = /tmp/xdebug.log

Make sure you have a phpunit.xml file next to phpunit.phar in your workspace, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
  backupStaticAttributes="false"
  syntaxCheck="false">
  <testsuites>
    <testsuite name="Tests">
       <directory suffix="Test.php">test</directory>
    </testsuite>
  </testsuites>
</phpunit>

Make sure there is one test in your test directory, e.g. /project-name/test/MyTest.php

<?php
class MyTest extends PHPUnit_Framework_TestCase {

  public function setUp() {
    //require_once( __DIR__ . '/../src/example.php' );
  }

  public function testExample() {
    $this->assertSame( '1', '1' );
  }
}

Double-click in the sidebar next to $this->assertSame to put a breakpoint there.

Then in Eclipse on the toolbar at the top is a little ‘bug’ icon, click ‘down’ next to that and debug ‘PHPUnit’.

Your unit tests should run in PHPUnit and break into the PHP debugger in Eclipse.

Did that work for you? Let me know!