Zend Studio Debugging and Drupal on PHP 5.x

Try these great books..

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

[inline:zend1.jpg=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.
[inline:zend-browser.jpg=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 :)

[inline:zend2.jpg=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 notes
  • xdebug plugin and zend seem to clash, causes the zend IDE to not talk to apache correctly
  • you dont really need the dummy.php

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