Finding bugs in any web application can be frustrating, especially if it takes hours, or even days, to find. I have found the PHP extension, xdebug, very helpful in those situations. Xdebug provides debugging and profiling capabilities for PHP.
The installation of xdebug is fairly straightforward. Let’s take a look at how to install it in Ubuntu.
Install the xdebug extension.
sudo apt-get install php5-xdebug
Settings in php.ini (/etc/php5/apache2/php.ini).
; Enable the xdebug extension. xdebug.remote_enable=1 ; Define the debugging protocol. xdebug.remote_handler=dbgp ; Mode ; - req: Debug ; - profile: Profiling xdebug.remote_mode=req ; Host name for debugging requests. xdebug.remote_host=127.0.0.1 ; Port to listen for debugging requests. xdebug.remote_port=9000
Next, install the “xdebug helper” extension for chrome or firefox. This extension allows for both debugging and profiling requests.
Make sure your IDE (Integrated Development Environment) is setup to receive requests via the port number setup in php.ini. In the IDE preferences, there should be a section to configure these options. The screenshots below detail how I have debugging setup in Komodo IDE 7.
Let’s walk through a debugging request in Drupal 7. I created a plain install with only a couple of modules and generated some content. The first thing to do when debugging a request is to set a breakpoint.
In the browser, there is an icon next to where you would bookmark a page and it looks like a bug. If you click on that icon, a menu will appear. When debug is selected, the code execution will pause at the breakpoint selected above. The output is below.
In the above image, variable details are shown on the left and the call stack is shown on the right.
The variable details allow you to drill down into the values of a particular variable at the breakpoint. Having a snapshot of a variable on a given line of code can help determine if there is some data causing an error at that point.
The call stack is one of my favorite parts of debugging. You are able to see each function that was called to get to the line of code where a breakpoint was set. This is useful to determine where a problem might have occurred.
The buttons above the call stack area are used to step through code line by line. the variables and call stack will update as you traverse through the code. This can be very useful to gain a good understanding of how a particular process functions.