kestas.kuliukas.com

Zend Debugger

PHP is a good language for web development, but having to enter in print statements whenever you need to know what's going on inside your script can be very tiresome. It's much better to have a good IDE that supports debugging, but there's a lot of choices, a lot of places you can slip up, and the easiest option is too expensive for those of us who aren't students (I am, and $99 for Zend Studio is still too much).

Before arriving at the working configuration below I tried to use phpEclipse with DBG; big mistake. All through compilation, installation, configuration on the server, configuration on the client, there are gotchas and easy mistakes to be made.

I didn't try Xdebug, I'd be interested to know how well that works out.

In the end I finally (after well over 10 hours of stumbling around and googling obscure, out of date documents) got Eclipse PDT working with Zend's official debugger.


Versions

Make sure you've got the right-ish versions

I have no doubt that the slightest deviation from this working configuration will break; now that I've got it working I almost want to close Eclipse and never open it again because I think a single misplaced mouse-click will result in hours more hacking at the configuration.
Try and replicate my system below as accurately as possible, then try and alter parts one at a time until it does what you want.

The holy grail of PHP debugging, Zend's best kept secret; ZendDebugger.so

Zend Debugger for FreeBSD was troublesome to find; you have to get the Zend Platform 3.0 beta for FreeBSD 6.2, and look for the correct ZendDebugger.so (it's at ZendPlatform-3.0.3-freebsd6.0-i386/data/5_2_x_comp/ZendDebugger.so)

(They host these files on a server running TinyHTTPD, which may decide to cut off at any point during the download, so the files are hosted here too.)

Note that Zend Platform (non-beta) you have to pay for, however the ZendDebugger shared object is freeware (If you can find it of course).
There's a PDT site on Zend with links to ZendDebugger.so , but none of them are for FreeBSD so you have to get the object from the Zend Platform 3.0 beta instead. (Why they don't provide the FreeBSD shared object with the linux, windows, cygwin, mac, etc ones I don't know.)


PDT, phpEclipse. eeny meeny miny moe

You also need to get Eclipse with the PDT plugin; PHP Development Tools (not to be confused with phpEclipse, which is also a PHP Development Tool, and is also PHP development plugin for Eclipse, and shares the same goals, but which uses DBG instead of ZendDebugger).

PDT All-in-one is actually fairly easy to install; just unzip and it's ready. This is useful, because it will get corrupted and complain about invalid workspaces regularly. Create a bat file that runs eclipse --clean so that you can try to clean the directory up if this happens, and if that doesn't work you can copy and paste another eclipse install over the corrupted one.


Copy&pray

On the server copy ZendDebugger.so (the FreeBSD 6.2 5.2.x one) to your PHP plugins directory. For me it's /usr/local/lib/php/20060613/ . ZendAccelerator, ZendOptimzer, ZendGuard, Zend* may or may not work with ZendDebugger; I would remove all other Zend extensions, and remove Xdebug, DBG if you have them, and perhaps any non-standard extensions you have installed. After you've got things working you can copy the non-standard extensions back and hope they work with Zend Debugger.

Once Zend Debugger is in place edit your php.ini. For me php.ini is in /usr/local/etc/. At the end add:

[Zend]
zend_extension=/usr/local/lib/php/20060613/ZendDebugger.so
zend_debugger.allow_hosts=10.10.2.3,127.0.0.1
zend_debugger.expose_remotely=always
zend_debugger.connector_port=10001

(The path with zend_extension does have to be absolute. And on Windows it's zend_extension_ts for some reason. Replace 10.10.2.3 with the IP/subnet you want to allow.)

Now run php -m and look for Zend Debugger. If it shows up restart Apache and look in phpinfo() for ZendDebugger.


Configure PDT - Heuristic analysis

I'm still not sure exactly what I did right, or what I was doing wrong before, but somehow I stumbled on the working configuration. I'll show how it's done by showing the configurations for a test project, and an existing project.

A test project, includes a file and prints "Hello World"

Go to Window>Preferences. These are the global preferences which apply to all projects.

Modify the PHP Server to reflect your settings. The document root is /usr/local/www/wwwroot in my case. The publish information tells Eclipse where the publish directory that corresponds to the document root is. This is very important, as it is what makes sure that ZendDebugger and PDT both know which file is being debugged.

Now that global settings have been configured the settings for debugging this file are set.

Give it a project-specific name, and set the server to the one you configured a moment ago. When you set the directory to publish to it will work out how to access the file in a URL. If the URL isn't being auto-generated correctly the web server or PDT will be confused about which file is being debugged.

Now for the moment of truth; set a breakpoint in the PHP file you want to debug and select the debug profile you just created.

At last, the debugger! You can see variables, set breakpoints and watches, edit variables, step through the code and see what happens, etc.

Doing the same thing for another project of mine, to show what you have to change when you're working on a different project.

Stopped at a breakpoint in the code. It tells you where in the script you are, and the values of variables.

In the same debug session you can step into other scripts too, see the contents of variables by hovering the mouse over them, and see the call stack so you know where you have come from in the script.

This is very useful, and really decreases the time you spend tracking down bugs by putting print statements everywhere. Now if a variable has a strange value in it you can simply set a watch on it, and find out the exact point at which the variable goes wrong, without any tedious print statements or guesswork.

However the debugger isn't bulletproof; the PDT client isn't going to be as polished and seamless as the commercial Zend Studio Pro, but it definitely does the job well, especially if you can't afford the $299 cost for debugging via Zend Studio Pro (Or the $99 cost if you're a student). It also lets you use the same IDE for many languages, which Zend Studio doesn't. The worst thing about this set up is that there's practically no information whatsoever on how to get it going, and hopefully now that's less of an issue.

Hits: 6604