Linux tip, Fedora tip / howto: Installing and using the dbg php debugger

 
Note that these tips are mostly outdated


back to notes and tips index

Try my online puzzle page with Calcudoku, Killer Sudoku and online Sudoku.

Installing and using the dbg php debugger

This is one of those installs where you have to dig up the appropriate information from all over the place. I've tried to condense all useful info I found into this single page. My original goal was to single-step through a Zend framework application, which worked in the end :-)

Download dbg and dbg-cli from this page. The version I got was 2.15.5 (dbg-2.15.5.tar.gz and dbg-cli-2.15.5-src.tar.gz).

Check if you have php-devel installed:
rpm -q php-devel
if not, then install:
yum install php-devel

Compile and install dbg:

  • tar zxvf dbg-2.15.5.tar.gz
  • cd dbg-2.15.5
  • ./configure --enable-dbg=shared --with-dbg-profiler --with-php-config=/usr/bin/php-config --prefix=/usr/lib/php
    Note: I had (probably from an older compiled version of PHP) two copies of php-config on my system (check this with where php-config). The old one was in /usr/local/bin, which I removed
  • make; make install

Add the following lines to your /etc/php.ini:

[debugger] extension = dbg.so debugger.enabled = true debugger.profiler_enabled = true debugger.JIT_host = clienthost debugger.JIT_port = 9000 debugger.host_allow = 127.0.0.1 debugger.host_deny = ALL The last two lines are to make sure you can only run the debugger from your local machine. Note I'm using port 9000 instead of the default 7869. Next, restart Apache:
/etc/init.d/httpd restart

To verify that the extension is loaded properly, create a file /var/www/html/info.php:

<?php phpinfo(); ?> (assuming your Apache DocumentRoot is /var/www/html) and load it at http://localhost/info.php. After the first main block it should say somewhere "with DBG v2.15.5, (C) 2000,2007, by Dmitri Dmitrienko". Lower on the page there should be a debugger info block, stating "DBG php debugger, version 2.15.5, Copyright 2001, 2007, Dmitri Dmitrienko, www.nusphere.com", and a list of variable settings.

Compile and install dbg-cli:

  • tar zxvf dbg-cli-2.15.5-src.tar.gz
  • cd dbg-cli-2.15.5-src
  • ./configure; make; make install
  • if you get an error about a missing readline/readline.h, you need to:
    yum install readline-devel

Create a file ~/.dbgcli_init:

set mode on set mapurlroot http://localhost set cgiexec /usr/bin/php set maplocalroot /var/www/html set mapremoteroot /var/www/html set port 9000 maplocalroot and mapremoteroot should correspond to your Apache DocumentRoot directory. Below I've written some more about the changes to make when you're debugging a Zend framework application. Probably I don't need the set port command, as that's already in my /etc/php.ini

You could now create a test file, say /var/www/html/test.php, for example:

<?php print "debugger test file\n"; for($i=0; $i < 10; $i++) { print "$i<br>"; } print "test done\n"; ?>

Now you're ready to fire up ddd (yum install ddd if not installed):
ddd --debugger /usr/local/bin/dbg-cli
In the debugger, type:
listen

Next, start your web browser, and type the URL to the file you want to debug, with a special string appended:
http://localhost/test.php?DBGSESSID=1@clienthost:9000

Switch back to the debugger. Now you can type stuff like:
step
or:
break test.php:3
(to set a breakpoint)
I have not yet been able to get the debugger to stop at a breakpoint. Instead I add the function call:
debugbreak();
to the lines where I want the debugger to break. Then I type cont in the debugger, after which it'll run until the next debugbreak() call.

If you use Firefox, you can install this handy debugging toolbar.

After you're done with one page "run", and want to debug the next page load, you'd type listen again. Unfortunately I kept getting this error:
Failed to start listener.Error (98)
Apparently this is because the port was closed server-side, after which it stays in use for another 2 × MSL (Maximum Segment Lifetime) seconds, which turned out to be 60 seconds in my case. Running
netstat -noap | grep 9000
confirmed this:
tcp 0 0 127.0.0.1:9000 127.0.0.1:49051 TIME_WAIT - timewait (53.97/0/0)

The solution is not in reducing the duration of this timeout (which apparently is not good for system stability), but to set a "fast recycle" parameter:
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle

Debugging a Zend framework application

Let's assume I've put my Zend site at /var/www/html/mysite (and /var/www/html/mysite/html has the index.php and .htaccess files) and made this a local virtual host by adding these lines to /etc/httpd/conf/httpd.conf: NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /var/www/html/mysite/html ServerName local.mysite.com </VirtualHost> Add this entry to /etc/hosts:
127.0.0.1 local.mysite.com

The ~/.dbgcli_init file mentioned above now becomes:

set mode on set mapurlroot http://local.mysite.com set cgiexec /usr/bin/php set maplocalroot /var/www/html/mysite set mapremoteroot /var/www/html/mysite set port 9000 Note that here the paths are not /var/www/html/mysite/html: the debugger has to find all your application and Library files obviously.

Do send me questions/comments if you have them using the form below. Don't forget to enter your e-mail address if you'd like a reply.

Windows PHP debugger

Alright, for all you Windoze people, there's the free version of the CodeLobster PHP debugger.


← back to notes and tips index
Please do not copy the text of this tip (© Patrick Min) to your web site.