Debugging with Xdebug and Eclipse on Mac

In my previous posts I explained how to install MacPorts and setup typical Apache/PHP setup with it. Now we’re going to start with something more serious, something that sooner or later every PHP developer will have to deal with. Debugging! And yes when I say debugging I didn’t mean echoing or using var_dump ;)

First off some background info. As far as I know, the very first PHP IDE which actually had debugger built in was Zend Studio. I’ve been using ZDE 5.2.x for very long time, but since Zend stopped supporting it, it was very difficult to continue using it. On Snow Leopard it required some nasty Java hacks to make it working, and any Java update would break it. It was obvious that time of ZDE 5.x was over so I had to pick something new. My choice was Eclipse (I didn’t see any point in paying Zend extra fee for IDE based on Eclipse) so I downloaded Eclipse Hellios release from Zend PDT pages. Unfortunately it was really slow and buggy so after a while I’ve decided to download “classic” Eclipse and get PHP Dev SDK installed manually. Somehow it worked much better for me and since there is new Eclipse Indigo 3.7.1 release right now, I’ve decided to wrote detailed explanation how to install it. In case you already have Eclipse installed and you’re happy with it, just skim to Xdebug setup part.

Setting up Eclipse
————————-
So, let’s download Eclipse 3.7.1 classic and unpack it to some dir. I prefer to use my home dir (which is /Users/dinke/eclipse) but you can pick any, just copy files there and run Eclipse application from that folder. On first run it will ask you about workspace (which is place where Eclipse keeps some internal files), I used /Users/dinke/workspace but you can use any location that suit you. Eclipse will start with Welcome window that we are going to close for now (you can always get it reopened by choosing Help->Welcome)

Eclipse Startup

Now it’s time to download and install PHP Development Tools SDK. So open Help -> Install New Software. Pick Indigo from “work with” drop-down and when list bellow gets updated, go to Programming Languages and check box next to PHP Development Tools SDK as in image bellow.

PDT Startup

Now click next, accept terms and Finish, and PHP tools will be installed. After installation is done (it may take some time), you’ll be offered to restart Eclipse, so do it immediately. After Eclipse has been restarted we are going to switch to PHP perspective. So click to “Open Perspective” icon in upper right corner of Eclipse and pick “Other”. There you’ll be offered options similar as in image bellow:

PHP Perspective

Pick PHP click OK and we’re done with Eclipse setup. Congratulations! Now you can tweak Eclipse to your own preferences (Eclipse->Preferences) or import/start new project and start playing with code.

Xdebug
————
Now we’re going to deal with Xdebug part. First we have to install it, and we’re going to use MacPorts. If you don’t have MacPorts installed just look at my previous post about it.

So, start terminal and type this:

sudo port install php5-xdebug

Restart Apache (sudo /opt/local/apache2/bin/apachectl restart) and make sure that you have proper Xdebug section on phpinfo page (see bellow):

phpinfo

Now we need to add some configuration options to php.ini file, so let’s add this to the bottom of php.ini file (in my case located at /opt/local/etc/php5/php.ini):

[xdebug]
zend_extension="/opt/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.profiler_output_dir = "/tmp/xdebug/"
xdebug.profiler_enable = On
xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
xdebug.idekey=ECLIPSE_DBGP 

and restart Apache again.

Now let’s configure Eclipse debugging options. Open Eclipse Preferences and then PHP->Debug and instead of Zend pick XDebug from PHP Debugger drop down.

eclipse_debug

Click Configure link next to dropdown, select Xdebug and click configure button again and make sure that in Accept remote session (JIT) is set to any (see image bellow).

eclipse_xdebug_settings

And this is it! Hooray!

Now let’s see how it’s actually working, we’re going to use so called “remote” debugging which allow us to actually debug directly from browser (there’s even firefox ext for that), however I prefer to insert my URL’s directly to configuration options. So select Run->Debug As from the menu and pick PHP Web Page. You will be asked to insert URL and then Debugging perspective will be opened along with editor. There you have ability to step into code, setup break points and watch variables on the right.

debugger

Happy Debugging :)

Useful Links:
Eclipse/Xdebug Remote debugging on Windows
Xdebug

Comments

  1. January 13th, 2012 | 6:23 pm

    Great article! Thanks!

Leave a reply