You are hereZend Studio Debugging and Drupal on PHP 5.x

Zend Studio Debugging and Drupal on PHP 5.x


By dgtlmoon - Posted on 31 May 2007

So it's been a good few years since I've felt the soft caress of a good breakpoint conditional debugger environment, last time I had really got my hands dirty was either in VisualStudio 6 or Watcom 10. I'm not claiming PHP is a great language, infact it's got a few shortfalls but one thing it is good for is getting the job done, up until now ive always just used a generous supply of print_r()'s and watchdog() entries, I really know better than this but without a good IDE, often it really is easier just to pepper a few of these babies around and see what happens, generally theres not really a lot going on that you cant figure out in a few well placed debugging entries.

Anyway, heres how I got Zend Studio & Zend Debugger working with Drupal, In this setup i chose to keep my existing PHP 5.2 setup from current Debian (Lenny). There are 3 main parts, install and configure the zend debugger to php, Enable Drupal to be debug-environment aware, and connect the Zend Debugger to Drupal, sounds tricky but its pretty straight forwards.
First, you need to get the Zend Studio and Zend Debugger installation files.
I just created a dir off my ~ called "zend" and ran the two binaries there, they should be self-extractors.
You should have the following files available

$HOME/zend/ZendDebugger-5.2.6-linux-glibc21-i386/5_2_x_comp/ZendDebugger.so

Now you need to tell your php config to utilise these debugger libraries, so we add the following to our /etc/php5/apache2/php.ini assuming you're running debian.. careful you past all the code as the PRE's in my HTML may hide behind the right hand panel slightly, however this file is attached to the bottom of this article


zend_extension=/home/dgtlmoon/zend/ZendDebugger-5.2.6-linux-glibc21-i386/./5_2_x_comp/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1
zend_debugger.expose_remotely=always

now fully restart apache so it makes the php binaries reload, if no errors maybe just drop a phpinfo(); into a script so you can see if it has loaded the extention correctly, you should have something that looks like

Zend Debugger
Expose Zend Debugger    always
Passive Mode Timeout    20 seconds
Connector PID   15557

Directive       Local Value     Master Value
zend_debugger.allow_hosts       127.0.0.1       127.0.0.1
zend_debugger.allow_tunnel      no value        no value
zend_debugger.connect_password  no value        no value
zend_debugger.connector_port    10013   10013
zend_debugger.deny_hosts        no value        no value
zend_debugger.httpd_uid -1      -1
zend_debugger.max_msg_size      2097152 2097152
zend_debugger.tunnel_max_port   65535   65535
zend_debugger.tunnel_min_port   1024    1024

So this means PHP is running and ready to take debugger connections on port 10013 on localhost (127.0.0.1)

Ok next step is to make any request to drupal bootstrap a special piece of code thats required so you can run with breakpoints in drupal - when you installed that ZendDebugger there would have been a script called "dummy.php" this script has some commands that activate the zendebugger.so, i found the best place to drop this in - although MAYBE it would work as a drupal module on its own, is to put the following into the root of drupal installation called "dummy.php", this is exactle the same as the stock dummy.php except we've deleted out the 'exit()' so it always carries with drupal.
dummy.php

<?php
@ini_set('zend_monitor.enable', 0);
if(@function_exists('output_cache_disable')) {
        @output_cache_disable();
}
if(isset($_GET['debugger_connect']) && $_GET['debugger_connect'] == 1) {
        if(function_exists('debugger_connect'))  {
                debugger_connect();
        } else {
                echo "No connector is installed.";
        }
}

?>

Now after the first line of the index.php in drupal (inside the <?php , place the following

include_once 'dummy.php';

Now access your site, nothing should change, just make sure things still work.

Ok step three!
By default zend studio will be looking for this "dummy.php" but it doesnt exist! we've rolled it into our drupal installation. So what we do is set the "dummy file" field to "index.php" and just set the "Debug Server URL" to the path of my devel site, this case http://dgtlmoon.localhost/, no other settings are changed, it should default to connect in "Debug Mode" as "Server" so it does the server<->client business

zendconfig

Now to test it, let's place a breakpoint when the blog.module generates the form via it's hook_form implementation, what we expect is the debugger to kick in when this happens, so goto blog.module in the ZendStudio and click on the line number after where there is

function blog_form(&$node) {

the background should turn RED which indicates a breakpoint is set.
Now - very important you need to request a URL with the variables inthe URL set that trigger the zend engine to communicate back with the debugger, i think there may be a better way todo this, but so far this is what ive found.
You need to append something like ?start_debug=1&debug_port=10013&debug_fastfile=1&debug_host=10.1.1.21%2C127.0.0.1 to your requests so for example

http://dgtlmoon.localhost/node/add/blog?start_debug=1&debug_port=10013&debug_fastfile=1&debug_host=10.1.1.21%2C127.0.0.1

Additionally theres a firefox plugin which seemed to be automatically installed that can trigger this to work with zend.
browserplugin

Now at this point your browser should hang and there should be some action in your ZendDebugger - hit F10 to step over and you can see what happens, browse variables or F5 to continue on! from here you should read the tutorials on setting watches, conditional breakpoints, browsing/changing variables etc.

have fun :)

zendwatch

So where to from here?

- well, I really like the kdevelop environment and apparently Zend have release their debugging protocol which means other applications could use it, but unfortunately a lot of the great IDE's under KDE are tied to use gdb exclusively, I really would prefer to use a KDE environment over a JAVA environment for getting my work done!So I guess in the future maybe we will see better integration with OS specific IDE's instead of cross platform java based IDE's that have no real integration with the desktop etc etc

Your rating: None Average: 3.4 (19 votes)
AttachmentSize
php.ini41.49 KB
dummy.php.txt324 bytes
index.php.txt901 bytes
zend-browser.jpg4.2 KB

I believe your instruction is very useful, unfortunately, I didn't have the linux box as the drupal development box, would you mind to add one more piece of information of how to make it work on windows platform. I have drupal 6, work with xampp 1.6.2, and zend studio 5.1, will consider to use the PDT if that is the recommended choice. Thanks.

Don't forget if you move to a new platform (say AMD64) you'll need to download the 64 bit version of the zend debugger library


[Zend]
zend_extension=/usr/lib/php5/20060613/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1,165.228.129.12,58.96.74.28
zend_debugger.expose_remotely=always

We can verify the architecture with


nine:/home/dgtlmoon# file /usr/lib/php5/20060613/ZendDebugger.so
/usr/lib/php5/20060613/ZendDebugger.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), stripped

after setting the breakpoint and appending the url ... the debugger terminates after running index.php .. now that user cant proceed without logging in to the site, but the debugger has already stopped. Is it required to pass login parameters in url .. especially for accessing an authenticated page ... login is necessary or are you enabling the access permissions of blog module for anonymous user to debug the same. How to accomplish this ?
Hi, Very useful article. Have you tried using Zend with Eclipse PDT? I was able to use the debugger to a limited extent. I can stop at the first line of the PHP file and step through one step at a time for everr, but I cannot make it automatically break at a preset breakpoint. Any insights? Thanks, Chandra
I found i had to add an argument to my page so the PHP engine knows to send this thru to the debugger output basically you just add ?start_debug=1&debug_port=10013&debug_fastfile=1&debug_host=192.168.182.2%2C127.0.0.1 to your requests.

so for example http://mylocaldevel.localhost/index.php?start_debug=1&debug_port=10013&debug_fastfile=1&debug_host=192.168.182.2%2C127.0.0.1

make sure your forms also submit with this in the action FORM ACTION="index.php?start_debug=1&debug_port=10013&debug_fastfile=1&debug_host=192.168.182.2%2C127.0.0.1"