Thursday, July 2, 2009

How to debug your PHP5 scripts remotely using Xdebug, Geben and Emacs on Ubuntu Intrepid

Install needed packages (xdebug and some elisp libraries)
# sudo apt-get install php5-xdebug eieio cogre cedet cedet-contrib

# sudo emacs /etc/php5/cgi/conf.d/xdebug.ini
Add this:
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost ;; the hostname where emacs with geben is running.
xdebug.remote_port=9000

# Download geben (http://geben-on-emacs.googlecode.com/files/geben-0.24.tar.gz) and unpack it to your ~/elisp dir.

# Add to your ~/.emacs:
(set 'cedet-path "cedet-common/")
(load (concat cedet-path "cedet.el"))
(add-to-list 'load-path "~/elisp/geben-0.24")
(add-to-list 'load-path "~/elisp/geben-0.24/gud")
(load "geben.el");

- Restart your webserver (I use lighty ;)

- Restart Emacs

- In Emacs, press M-x (or esc + x)

- Enter "geben", and press enter. Geben will now wait for a debug client connection.

- Start your php script from your browser with ?XDEBUG_SESSION_START=1 in the URL.

- As soon as the script is executed, the debug client is connected to geben/emacs and geben loads up the source in your Emacs window!

Command summary for geben:

spc step into/step over
i step into
o step over
r step out
b set a breakpoint at a line
u unset a breakpoint at a line
g run
e eval expression (to inspect variables: best is probably print_r($this, true))

q stop debugger