PHP Debugging Using XDebug

When it comes to php debugging, we seem to have only two options, one is zend debugger, the other is Xdebug, for life of me I never understood how to use zend debugger or even where to start, it looks like they called it a zend server which confuses me more(we do not need one more server, do we?), if there is any one here who knows how to debug php using zend debugger, please leave me a message.

Good news is Xdebug is a viable option for php debugging(even though sometime it is slow in Eclipse), and it is free, simple, here I am going to show you how to use it.

Xdebug is an extension of php, you just need to get the right version of Xdebug for your php, and config it properly. That is why I like it, the concept is so simple, it is just one file, you start your http request with URL variable XDEBUG_SESSION_START, then Xdebug will take over the php execution, you can pause, continue a thread, check the variables etc, do anything a debugger should do.

A few points here regarding the use of Xdebug

1. Which version of Xdebug? it depends on the version of php, there is an easy way to decide which xdebug to use, run your phpinfo() in a browser, and copy the content of phpinfo() output, go to Xdebug Tailored Installation Instructions then paste it into the text box, press ‘Analyse my phpinfo() output’ button. download the suggested version of xdebug dll, and make changes to your php.ini

2. Add
xdebug.remote_enable = 1
after line zend_extension = …
This is simply to enable Xdebug, as the default config is off for Xdebug, you normally only need these two lines for Xdebug to work. Stranglely enough this line is not in the Tailored Installation Instructions.

Restart your Apache service or recycle your IIS application pool.

3.Verify Xdebug extension through phpinfo() output

xdebug in phpinfo

xdebug in phpinfo

4.For FASTCGI IIS php users, you might find that Xdebug still does not function even you have done absolutely everything that is recommended, Xdebug needs write permission, so just ensure that the IIS thread worker (normally NETWORK SERVICE) has not only read/execution right on php engine folder, but also write permission.

5. Xdebug.remote_host setting in php.ini
This should be the ip address of caller, not the server, this is the address xdebug session call back to, if you are debugging the php application on the same computer, then localhost (the default value) will do.

6. You need a debug client to do the real debugging job, it can be Eclipse PDT, netbeans, or other php IDEs, there is also XdebugClient, a standalone windows client, provided here
What a debug client does is to start remote debug session with XDEBUG_SESSION_START and KEY URL variables, and mapping the local files with the ones on remote server, and providing a user interface for debugging.

7.Profiling php applications using Xdebug:

xdebug.profiler_enable = 1
xdebug.profiler_output_dir = "c:\wamp\tmp\"

You can enable php profiling by adding above entries in php.ini,all xdebug entries must be added after the zend_extension line. the dir can be anywhere on your pc where php user can write.

and in windows I use WinCacheGrind.exe to view and analyse the profiling result.

wincachegrind

wincachegrind

Happy php debugging.

This entry was posted on Tuesday, December 14th, 2010 at 11:54 pm and is filed under PHP, Software Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*