Using PDT + xDebug (for debugging Drupal projects)
This blog post is meant to be a short guide and tips repository for setting up a PHP development environment using Eclipse PDT and xDebug. I use such an environment for my Drupal based development and it is an invaluable tool to me. This post doesn't explain everything because there's lots to it. From my experience (as a former Zend employee (Zend produces its own commercial PHP IDE)) and from my acquaintance with the PHP development community, I have a good reasons to believe this article could serve a few PHP developers out there.
If you have comments about this article please post them! The good thing about CMS is that they let you edit your posts easily even after you've published them
.
Background
- One must understand what it means to debug in this article's context and debug PHP applications as it still seems that the most prevalent PHP debugging
tool is var_dump/var_export/print_r() etc... . Debugging using PDT +xDebug is a whole different ball game. Don't worry - its really not that complicated, but it is different from using textual printing of variables. In fact, many developers are already familiar with debugging tools and concepts from other languages such as breakpoints, stack trace, flow control. This is the concept of debugging we'll see here as well. - The advantages of using the debugging concept that is demontrated in this article is that you will be able to excute a PHP application/scripts in a fully controlled manner and have excellent insight on what's going on in the system as the scirpt run: we'll be able to postpone the run in predetermined lines of code ("breakpoints"), carefully see each line of code being executed, the variables that lives in that context and their data (and alter them as well in real time!), and more... . Cool, aint?
- The debugging method we'll explore involves client and server side programs/utilities. The client side is the Eclipse PDT application. It is the controller of the whole "debug session". Most of your day to day debugging work will be conduct via PDT. It contacts and communicates with the server side, as needed. The server side entity is a php extension ('add on') that enables this debugging magic to take place. This is the xDebug extension, which needs to be incorporated into the LAMP/WAMP stack as a PHP extension during the set-up process we'll cover on our next section.
xDebug installation
Lets start with server side installation and configuration. xDebug has very good documentation on its web site. Therefore, I'll just give here a bullet list of points that you'll need to cover, without going into details. Those I'll provide with links to the relevant locations in the documentation mentioned.
Lets start:
- I highly recommend installing your personal development LAMP/WAMP machine. xDebug has a limitation (as far as I know) that it can initiate debug sessions only to one machine and that it needs to be configured in php.ini. This means that your client machine's IP should be present in php.ini (we'll cover that shortly) and it needs to be edited (+web server's restart) every time some other developer wishes to debug on that web server. This is of course not comfortable or even hardly usable in multi-developers-per-WAMP/LAMP environment (this in contrast to commercial debugging tools that enable multiple developers to debug simultaneously against one web server).
These days its quite easy to setup a LAMP/WAMP on your personal desktop/laptop so the limitation just mentioned is not a blocker. Linux folks would probably install apache/php/mysql with their package management tool and Windows ppl can use XAMPP or the like to install a WAMP (with great ease, I should note; XAMPP is great).
So, from this point on I will assume that you have your own LAMP/WAMP machine. Lets call it... <drums>the web server!.</drums> - Download and install xDebug extension on your web server. The steps to do that are described here: http://xdebug.org/docs/install . After this is performed successfully, you should be able to see the xDebug extension present and kicking when running a phpinfo() on the web server.
Please note that xDebug can collide with some other extensions such as other debugging extensions. Consult xDebug documents for more info on this... . - Now we shall configure the xDebug installation to work properly for our purposes, establishing our own debugging environment. There are a few ini directives that needs to be set up in php.ini. Here too, I'll give a short list of what I know to be working ok and references.
In your php.ini, put the following after the xDebug extension loading lines (that was configured in previous step):- xdebug.remote_mode="req" (this is the default value. not needed. I do it for verbosity)
- xdebug.remote_enable=On (you want to debug no?
...) - xdebug.remote_host="192.168.1.3" (this must be configured to your client's IP address)
- xdebug.remote_port=9000 (this is the default value. You can change it if you want. see important note below).
- xdebug.remote_handler="dbgp"
- Important - make sure that if you have a firewall on your client side machine (where PDT will run) it doesn't block incoming TCP connections to the port number configured in debug.remote_port above.
- For the complete documentation on xDebug php.ini directives have a look here: http://www.xdebug.org/docs/all_settings
- Don't forget to restart the web server after changes to the values in php.ini... .
Eclipse PDT
Ecliipse PDT "all-in-one" package should be almost-ready-to-debug with xDebug out of the box, but some configuration is still needed:
- First, obviously, download and install Eclipse PDT, if not already done. I'm not a great expert on the various packages it is provided in, but I think the "all-in-one" is a good choice. Link: http://www.eclipse.org/pdt/
- Open PDT preferences window -> PHP -> PHP servers. Add your web server's details there. Configure a name and document root for the server:
You might want to add "path mapping" in the tab that bears this title. This will enable the xDebug extension and the PDT to sing the same song while debugging. What do I mean? Imagine that you have several files named "db_funcs.php" in your project, each contain DB functions for different DB (mysql | postgres...) and each located in a different subdir of the project. The needed file is "included" at run time based on the configuration of the DB you are working against (in some config file). Lets say that you're debugging a specific line in db_funcs.php that lurks in includes/db/mysql/ directory. This means that the xDebug extension and PDT needs to be directory-aware when they refer to files because xDebug is running the file and you should be able to see the exact lines that run flowing in PDT. That's why on certain, not so rare occasions, you'll need to configure this tab. Simple configuration example:

- PDT -> Window -> preferences -> PHP -> Debug (here. no sub-item) -> PHP debugger should be selected as xDebug:
- Make sure to configure a browser for your PDT (Preferences -> General -> web browser). I work with Firefox:

- Your PDT should be configured at this point. Lets try to debug something shall we?:
Initiating a debug session
I should point out that the debug session instantiation method described below is what I use currently. Like in the rest of the article, TIMTOWDY and "it works for me"... 
- Open PDT, make sure that your project is opened. I usually close other projects. Not sure what affects will happen if they are kept open (on debugging and on other Eclipse functionality that I use such as search facility).
- Now, make sure that your workspace files are synced and that they are exactly as those on the project files which lurk on the web server. This is because of the reason already mentioned above (configuring "path mapping" tab in Eclipse PDT configuration above): xDebug will run over the web server's files while PDT will show and use the local workspace files. If the same file has one content on the web server's file system and different content on the PDT workspace - that wont be nice... .
To achieve this synchronization I use the ESftp plugin for eclipse (information and download available here). Its a very cool plugin which enables sFTP facility in a very convenient way for this purpose - I simply configure for the Eclipse project a "site root" on a remote SFTP connection and then I can sync them with very little effort. After you install ESftp go to the Eclipse project root in the "PHP Explorer", right click -> ESftp tab:
I also configured the general Eclipse key bindings to have Ctrl+U perform a "put file to server and save". This is achieving synchronization perfectly and very fast (on a LAN). Make sure to inspect permissions or other problems on the PDT console view (ESftp console). - Plan ahead your debugging session. What section of the code you wish to test? Put breakpoint(s) in the relevant line(s) of code by opening the file in the editor -> double click on line number on a line number where PHP code lies in (I think that trying to put a breakpoint on comment will fail. Sometimes it will, at least). See the blue dot that resulted from this double click? That's a breakpoint. I think that reading some on the Eclipse online help in the PDT section of debugging can't hurt at all (In eclipse PDT itself: Help -> Help contents).
- Now configure a debug session: open the debug dialog: Run -> Open debug dialog (or the toolbar equivalent) -> PHP web page -> "New luanch configuration" (top left button):
- Choose server debugger xDebug and the PHP server defined earlier
- "File" should be set to? I'm not sure here. I'm developing on Drupal. Drupal uses clean URLs and everything goes via index.php so I just put there /Project_root/index.php (use "browser" button to select).
- Break at first line? I don't need it so I deselect it.
- URL: I check off "Auto generate" and put here the "clean URL" that I wish to debug. For example: /my_view_url/ .
- Screenshot:

- Ready?... (don't close the window just you just configured) See next...
- Click "debug". Your debug session is starting right now! xDebug is getting the debug request and takes control over the execution of the PHP (that is part of the URL you requested) on the server, negotiating with PDT where, if any, there are any breakpoints it should stop at. The result should open in a browser window, should you defined it like that (in advanced tab of the "debug web page" settings configured above).
- While the debug session is active, once xDebug hits a breakpoint (as told about them by PDT), it'll stop its execution on those files, in the line number specified. PDT will then automatically open up the "PHP Debug" perspective, with the file where it hit a breakpoint (which it takes from it local workspace files - this is why the "path mapping is important, as mentioned above) opened automatically in the editor, and await further commands:

- You can see in the editor view, on the line number where it stopped the blue dot and a blue arrow indicating where the current execution flow is waiting for you. You can examine at that point the variables and PHP trace in the various views available in the PHP Debug perspective. If this perspective didn't open up for you, open it manually.
- What exactly happens during a debug session? How does this client-server communication works in regard to the debug session? Take a look in the sketch below.
- Controlling the flow of your debug session is done with the buttons in the toolbar or via the menu bar "Run" menu.

Again, please refer to PDT's online help facility for more info on what each flow control button does exactly and lots more on debugging (Help -> Help contents). - While PHP on the server (controlled there by xDebug extension) is awaiting command when its execution flow is halted by PDT, you can click on function names on the "debug" view (top left window, defaultly) in the PHP debug perspective. That can provide you with useful information on which function called other functions up to now and also a complete stack of variables that exists in each function call. The best way to understand this is to try it.
- Last tip: I usually stay with "debug all pages" in the advanced tab of the "debug web page" settings window explained above. Its comfortable to me: when the current debugged page has finished executing (PHP finished its run, passing over all breakpoints and continuing when we told it to, via PDT's flow control buttons) the debug session remains active and any activity via the browser's window will trigger another debug session. Want to finish the debug session completely? (for now) - click the red button in PDT's "Debug" view (PHP Debug perspective).
That's it!
Boaz.

Great post, I ran into a
Great post,
I ran into a problem when using it. I am working on a Zend application (i realize the post was mostly about drupal, but i was hoping you could help).
When i try and debug the application it throws a Session_Start error telling me that it the session has to be started before anything is sent to the screen - and some HTTP headers have already been sent. I am assuming these are being sent by xDebug, which means that i can't possibly get Zend to start the session earlier.
Any suggestions?
Thanks,
-E
Hi Elliot, Sorry for the
Hi Elliot,
Sorry for the (very) late response... .
I'm not aware of headers that are sent by xDebug. Not as far as I know. Maybe try xDebug forums to get insight on this. Also, you can easily tell if its not occurring without xdebug. Try to run the very same request in a debug session and not from a debug session (leave the server configuration as is in both cases). I suspect the problem is not xDebug related, but perhaps something else that relates to your need and preparations for doing "remote debugging".
Good luck with it!
Boaz.
Follow your instruction, I
Follow your instruction, I can successfully create the project for debugging, but when I try to set up break point, and debug it from index.php, the "access forbidden" was in my debugging window.
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
I am using XAMPP on Windows XP Pro, the PDT was set up right when I try to debug single PHP script, but for some of the open source project, it simply can not debug the particular page, I have to start from the index.php.
I changed the permission for web content didn't work.
Would you mind to share some detailed information about how to set up and start to debugging.
Thanks.
Hey thanks for this, I was
Hey thanks for this, I was just thinking it is about time I installed a PHP debugger for use on my PC.
For noobs like me it would appear that XAMPP is the same as LAMPP and is available from e.g. sourceforge.net and apachefriends.org.
Fingers crossed it all installs smoothly, and thanks again
just curious alot of the
just curious alot of the javascript files in drupal project will be 'red'/errors. like document.createElement('script'); will be function is undefined. anyway to get eclipse/pdt to recognize it (even better would be jquery recognition too, but thats next step, think theres a plugin called jquerywtp will test once get js working)?
i have all the latest things needed for pdt 2.0, like all the integration files, gef, dtlk, webtools, etc added to eclipse classic. didnt use the all-in-one pdt.
also where is preferable to have your workspace, in home directory like /home/site1 with apache virtual hosts/hosts file pointing http://site1 to there, or just have site setup in vhosts pointing to htdocs/site1 and use that as Projects. trying to think of whats best way to test when u have many sites working on locally.
Here’s a detailed
Here’s a detailed step-by-step article for setting up a PHP debugger using PDT+XDebug and XAMPP:
http://robsnotebook.com/php_debugger_pdt_xdebug
Working in an ideal
Working in an ideal organization and doing cowork with 5 others, we use Svn to synchronize sources on the web and a plugin started by svn updates the the test-server. When code is debugged and checked by beta-testers, coordinator publish the code from the test server to the main server.
Locally I use localhost and a local copy of the site, using same syntacs as on server, that is, a folder structure /local/www/...... and /local/cgi-bin/......
For development, I have the working copy in a separate place, /localtest/www etc. and some of the subfolders of cgi-bin is organized as /localtest/cgisubfolder1, /localtest/cgisubfolder2 etc of which each one is a separate project in svn. To administer in Windows I use TortoiseSVN.
Now, my question related to this is thus. Do I need to make a new folder to check out the sources to be used in Eclipse PDT/Zend Studio, or is there a way to couple Eclipse and my already existing folders run by TortoiseSVN and thus doubling disk usage?
I would like to see a way on how to set up svn support for debugging and development. Am I asking too much?
I think such a description would enhance the value of your eminent description on how to get started using xDebug and Eclipse PDT.
Alf Christophersen
DIS-Norge (http://www.disnorge.no)
Hi Alf, Well, the topic of
Hi Alf,
Well, the topic of integrating Subversion and methodologies for successful and effective team development would increase the size of this blog post to a chapter in a book (if not a a whole book...), let alone the time it would have taken me to write it...
If I got your question right then I don't think you need two trees on your development machine and you certainly can have SVN integration into your PDT.
Regarding subversion plug-ins for Eclipse - you are in good condition: there are two, AFAIK (from my checks in the past) - subclipse, which comes from the same home as the subversion itself (which should theoretically result in better coordination with subversion server and other benefits) and subversive, which I think, is supposed to be stronger in capabilities. From my checks subclipse was very good for me so I "went to the source" (I mean its very close to the subversion maintaining team).
I'm using the following scheme when working on my personal environment:
Hope this answers,
Boaz.
Well, in part, this was
Well, in part, this was known to me. But, I do not find how to hook up directly to the existing project that is already checked out, but in TortoiseSVN.
Since Eclipse is very slow on my computer, I'm using Eclipse only for debugging purpose (on the php code that is) and for future database model building, but not for php development where I would still use EditPlus and TortoiseSVN.
Seems like I need two source trees, anyway, right ??
Maybe I also should ask on TortoiseSVN list :-)
I now understand
I now understand better.
Well, if your PDT project' files are to be being edited outside of the PDT then I'm not sure what the consequences will be when you launch PDT or worse, edit opened files under its feet. I wouldn't recommend doing so, so in this case I suggest you do hold two separate trees.
I must note that PDT is very capable and comfortable (to me) PHP IDE. Perhaps you can make it run faster on your machine. What hardware do you have that its so slow on? Perhaps with some tweaking of the JRE you can make it run faster. Indeed, all in all, PDT as all GUI Java applications (well, most) are damn slow and memory consuming applications.
Boaz.
(I thought I posted this
(I thought I posted this yesterday but still don't see it, so I apologize if this is a repost)
For some reason, this thing won't work for me. When I start the debug session, it just sits there. Then, after I quit the session, it finally loads the url:
http://localhost/pageIwasTryingToDebug.php?XDEBUG_SESSION_STOP_NO_EXEC=E...
with content DEBUG SESSION ENDED.
Then, when I close that window, the page loads in a different window.
What am I doing wrong?
Hi (Chris, I
Hi (Chris, I assume...),
This is indeed a repost. See my reply from a few hours ago...
Boaz.
For some reason, this thing
For some reason, this thing won't work for me. When I start the debug session, it just sits there. Then, after I quit the session, it finally loads the url:
http://localhost/pageIwasTryingToDebug.php?XDEBUG_SESSION_STOP_NO_EXEC=E...
with content DEBUG SESSION ENDED.
Then, when I close that window, the page loads in a different window.
What am I doing wrong?
Hi Chris, Hmm... cant tell
Hi Chris,
Hmm... cant tell exactly. Try to:
- Double check your xdebug settings in php.ini on the server. Dont forget to restart web server afterwards and make sure that the new settings are being used and xdebug is being used (phpinfo() output).
- Double check your PDT configuration.
- Make sure not firewall is blocking communication between the php and PDT. Remember that in order to have a debug session a regular TCP connection should be made between the web server (more like the PHP on the web server) and PDT application (by default to port 9000 on the PDT machine).
Boaz.
I've checked, rechecked, and
I've checked, rechecked, and double rechecked the php.ini and PDT settings.
They're both the same machine. Thus, I'm using localhost. Would that be affected by a firewall?
Chris - well, at least I
Chris - well, at least I know it isn't just me! :) I see the exact same thing you do. Been trying to get this to work all week. It is not firewall related, I don't have a firewall active on my system. I'm thinking it might be a mismatch between xdebug versions in Eclipse vs. Web Server?? I don't know. But, you have a brother-in-arms out here fighting the same fight, just so you know :)
About an hour ago, I had
About an hour ago, I had posted a reply stating I had a similar problem. I made one change to the PDT configuration and now everything works like a charm. Its a localhost based setup (PDT and server running on same machine). I was able to debug through all my interactions with the browser.. and when I finally quit PDT, I got the tab in the browser stating "DEBUG SESSION ENDED".
If someone has specific questions about my configuration, please email me at tej_arora@yahoo.com
The change I made to PDT config was:
Window->Preferences->PHP->Debug->Installed Debuggers->Xdebug->Configure
CHECK "Use Multisession"
That's it.
Good luck.
Hi, Good to know some
Hi,
Good to know some commenters are able to get it working after all.
I'm not sure how to help the other commenters aside from my comments made previously to Chris.
I do recommend double checking your configuration, at least with what I've put above.
Then, yes, possibly xdebug version mismatch could result in problems. I'm using xdebug v2.0.2, as shown in an early screenshot above (there are newer versions in existence by now and I haven't upgraded to them yet).
I'm not familiar with the "Multisession" option just suggested by TA. xdebug has good documentation I suggest reading some to get wiser and consider for your environment to resolve issues.
One last comment - I'm working fully on Linux and it might be related. Although I have the same configuration on my work Windows 2K3, working fine, this article, including screenshots, was prepared on a 100% Linux environment (mixed 64/32bit: server is 32bit, client is 64bit).
Boaz.
I'm having the same problem.
I'm having the same problem. The debug session starts well, and I'm able to step through... but when I resume to let it run to completion, the browser window shows nothing. When I click on the red square button, 3 tabs appear in the browser window - 2 of them say "Debug session ended" and one of them shows the page i was trying to debug. How do I have a debug session through multiple pages?.
This post atleast got me as far as being able to step through a single page. Thanks a lot for the time & effort you've spent in putting this together.
First, indeed your firewall
First, indeed your firewall could be related. Not highly likely, but possible.
Next, did you check your phpinfo output to see that it has xdebug and the directives included?
Can you use some network diagnostics tool to check what happens in the during the communication? For example, during the session and after it, can you see an established TCP connection from localhost:SOME-PORT to localhost:9000 ? That would indicate a connection between the xdebug extension and the PDT.
Thanks for this post, quite
Thanks for this post, quite useful.
I did use PDT+xDebug but this is not my favorite way of debugging because of the memory requirements and general bloat of eclipse. I use it only for the most complicated bugs, one that require a broad view of what is going on in the system (watching variables, conditional breakpoints, jumping through the stack etc).
For the simple things, I just use Xdebug+vi, which gives me good control of 90% of the bugs, YMMV.
http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug....
Per this guide, I think it might be useful to add some screenshots. When you run this step by step, it is easy to follow the screenshots, and see where you left off, or where you might want to opt out.
Indeed, GUI Java
Thanks,
Boaz.
The screenshots abosolutely
The screenshots abosolutely make a difference!
Most issues I've had with setting up a session relate to defining the project
settings correctly in Eclipse. Exactly what you explain (5th and 6th
screenshots here). Probably because I'm not familiar with Eclipse..
I will definitely bookmark this page and go back when I need to run Eclipse
again..
Post new comment