10 minutes to interactive php debugger in vim

Let's assume you have a huge (or legacy, sic!) application written in php which is result of 5+ years of development (read: spagetty code, php3-5 code style and so on).

But, something doesn't work, and you can't figure it out by just looking into code. If you abstract problem a bit, ability to single step through php code might be generally useful, especially if it integrates well into my console-vim-firefox environment.

So, does it? It sees that it does...

First, install Xdebug extension:

$ apt-get install php5-xdebug
Then enable DBGP debugger in /etc/php5/conf.d/xdebug.ini
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_host = localhost
Now hop to Firefox Add-on site and install Xdebug Helper. This is small extension to add two icons (debug and profile) to Firefox status line.

Now you need interface, so download DBGp client for vim (take care of Mac hidden files inside zip) into your ~/.vim. There is also very nice tutorial now to install everything, but if you are using Debian (lenny or unstable) you can get all requirements from packages (thanks DDs!), just install vim-nox package.

How enable debugging in Firefox by clicking on Xdebug Helper icon (or add ?XDEBUG_SESSION_START=1 to URL of your application) and reload page once (this will install debug Cookie in Firefox session).

Finally you are ready to start debug session: start empty vim press F5 and reload your page again in next 5 seconds.
If you want to increase timeout of 5 seconds (I might be just snow when reloading page ;-), open ~/.vim/plugin/debugger.py and change line serv.listen(5) to some bigger timeout. You might also want to increase socket.setdefaulttimeout(5)

You will end up in window with loaded source code which you can single step, put breakpoints or examine variables. All in under 10 minutes (if you didn't dooze off while reading this post...)


In next installment of introduction to php development for 21th century we'll take a look at profiling and fun stuff with DBGp protocol using perl client :-)

Update: If you want to debug using vim on your local machine, and xdebug on server somewhere (over ssh) you will need to setup two things:

  1. Setup ssh tunnel for port 9000
  2. Host dev.example.com
        RemoteForward 9000 localhost:9000
  3. Mount source code locally using sshfs
  4. $ sshfs dev.example.com:/srv /srv/