Debugging PHP on Windows with XDebug and VIM

Habari is a sufficiently complex PHP application that sometimes the only way to figure out what is going on is to fire up a debugger. Following a suggestion on #habari and some google searching, I found an article on the Box.net blog on using XDebug with vim. The instructions were for a linux box, and I initially struggled to get things working on Windows (XAMPP). But, I finally got it through digging through the comments on the Box.net article and Vim documentation. I thought it would be useful to gather the required steps in one place, so here are my instructions:

  • Read the Box.net article.
  • Download XDebug and put it in xampplite\php\ext
  • Edit xampplite\apache\bin\php.ini. Add these lines:
    zend_extension_ts="c:/xampplite/php/ext/php_xdebug-2.0.3-5.2.5.dll"
    xdebug.remote_enable = 1
    xdebug.remote_port = 9000
    xdebug.remote_host = localhost
  • Restart Apache. Check that xdebug appears on your phpinfo() page.
  • Download the xdebug plugin for Vim and put it in: C:\Program Files\Vim\vim71\plugin
  • Download and install Python 2.4 (not 2.5). You can see which version of Python your copy of gVim is looking for by looking for the 'python' string in the gVim binary (dirty, but it works!)
  • Now you need to modify debugger.py to work with windows paths, so open debugger.py in gVim
    • Change the session file to something reasonable. XAMPP has a directory that already serves this purpose, so you might change self.sessfile to
      self.sessfile = "C:\\xampplite\\tmp\\debugger_vim_saved_session." + str(os.getpid())
    • Now change every occurance of getAttribute['filename'][7:] to getAttribute['filename'][8:]. Do the same with getAttribute['fileuri'][7:]
  • Open up a browser and append ?XDEBUG_SESSION_START=1 to the end of an address on your local webserver.
  • Open gVim. Hit F5, then refresh your browser page. You should now be debugging!

18 Responses to Debugging PHP on Windows with XDebug and VIM

  1. 777 David Damstra July 15, 2008 3:45:pm

    Hey Blake, thanks for the tips. I am so close to getting this working. Hope you can help...

    Developing on WinXP with GVIM connected via sftpdrive to the remote Ubuntu box with the website on it.

    Set up php.ini for remote debugging on my local WinXP IP.

    I edit the file in vim, hit F5, refresh the page in firefox. It connects, but I get the following error in gVim:

    E185: Invalid buffer name: var/www/{siteName}}/htdocs/filename.php

    Seems to me that its trying to map the same directory structure on my locally mapped drive (via sftp) to the one on the webserver and they don't match.

    Any ideas? Any help appreciated. Thanks.

  2. 803 Blake Johnson July 16, 2008 9:25:am

    Hi David,

    It sounds like you need to modify the paths that debugger.py is trying to open. I actually don't do any python programming myself, so I can't give you specifics, but I would imagine that you want to write a function which takes in a linux path and maps it to your sftpdrive path.

    In PHP your function would look like

    function remap( $path ) { return 'F:\' + preg_replace( '%/%', '\', $path ); }

    where "F:" is the drive letter of your sftpdrive. Then you want to wrap every reference to getAttribute['filename'][7:]. You just need to convert my function to Python.

  3. 804 Blake Johnson July 16, 2008 9:34:am

    If I am not mistaken, in Python my function would be:

    def remap (path): return 'F:\' + path.replace('/', '\')

  4. 814 David Damstra July 17, 2008 9:43:am

    Thanks Blake,

    It's more complex than that, since the full paths are not even close.

    I gave up on it.

    Thanks for your efforts though.

  5. 2522 cyberhobo February 19, 2009 7:28:pm

    Thanks, I wouldn't have gotten very far without this!

    Apache was dying on me until I made this edit on line 675 in my php.ini:

    extension=php_xdebug-2.0.4-5.2.8.dll

    Then I kept getting an error with the sessfile edit in debugger.py. In the end I added "import tempfile" at the top and changed it to:

    self.sessfile = tempfile.NamedTemporaryFile().name;

    Now I can start the debugger, but I always seem to get a socket timeout error before I can do anything in it. Probably an apache configuration issue...

  6. 2551 Kim Hermansson March 10, 2009 9:42:am

    I had a small problem where debugger.py tried to open files with spaces in their pathnames. Xdebug sent the filenames with space as '%20'.

    To fix it i changed all occurences of getAttribute['filename'][7:] to getAttribute['filename'][7:].replace('%20', ' ') and getAttribute['fileuri'][7:] to getAttribute['fileuri'][7:].replace('%20', ' ')

    Im sure there are some elite python command to strip html entities but that atleast fixed it for me.

  7. 2564 Blake Johnson March 13, 2009 12:07:am

    Thanks for the tip, Kim.

  8. 2716 John July 27, 2009 8:11:pm

    Blake, Thank you very much for this great tutorial. This is an extremely helpful tool to have attached to vim!

  9. 2718 John July 28, 2009 1:46:pm

    Hi Blake, I just ran across an error. After setting a breakpoint, and then hitting F5 to run the code I would get a command not recognized error back. After some tinkering, I found the problem was that I was using a newer version of XDebug. So maybe it would be good to specify that this works for XDebug php_xdebug-2.0.3-5.2.5.dll This is the version that you used, but it is not explicitly stated that this is the version that the scripts work with.

    Thanks again!

  10. 2719 John July 28, 2009 2:58:pm

    Apologies for all the comments, but What I thought was a solution, is not actually one at all. It was working for a while, and now is not any longer. Feel free to delete both of these comments. I am still getting a

    error code=5 : Command not available (Is used for async commands. For instance if the engine is in state "run" than only "break" and "status" are available). message command is not available

    error when i try to run the program.

    Looking more closely, it seems the error is actually on the stack_get call that is automatically run after the run command when F5 is hit. Again, sorry for all the emails. If you have any idea what is causing this, I would appreciate the insight. If not, I will keep plugging away at it.

  11. 2756 Aaron Boushley August 24, 2009 7:35:pm

    Has anyone come up with a good solution on how to do this from an actual remote machine? I want to be able to debug code from my workstation when its on our in house server. There shouldn't be any firewall problems... but the server box is running linux, while my machine is running windows. So I need to be able to map changes in the path...

  12. 2826 siti di Bingo September 22, 2009 1:21:am

    I was using a newer version of XDebug. So maybe it would be good to specify that this works for XDebug php_xdebug.So thanks for providing the information.

  13. 3109 moechofe November 5, 2009 6:50:am

    I'm using vim 7.2 on windows with PHP 5.3.0 (cgi), Xdebug v2.1.0-dev and vim plugin: DBGp client 1.1.1

    Put the plugin in the c:\program files\vim\vimfiles\plugin

    Insert: elseif filereadable($VIMRUNTIME."/../vimfiles/plugin/debugger.py") pyfile $VIMRUNTIME/../vimfiles/plugin/debugger.py to the debugger.vim file at line 124

    Replace: node.getAttribute('filename')[7:] by: node.getAttribute('filename')[7:].replace('%20',' ').replace('/','\').lstrip('\') to the debugger.py file (3 times, lines: 295, 791, 823)

  14. 3763 The-One March 10, 2010 1:40:pm

    You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

  15. 3800 Php Programmer April 3, 2010 6:48:am

    Good useful post. Knowledge of Debugging information is very useful and necessary to save time and increase performance. And this article has helped me alot.Thanks.

  16. 3875 alehro September 12, 2010 11:25:am

    In my situation it was needed "zendextension" instead of "zendextension_ts". Thanks.

  17. 7469 Mert Nuhoglu March 20, 2012 6:07:am

    In my installation, there occurred a problem in Vim:

    cannot find debugger.py

    I put debugger.py into $home.vim\plugin\ then it worked.

    Thank you for the clear explanation Blake.

  18. 32164 Stas February 6, 2014 10:15:am

    I like to bebug my PHP code in Codelobster PHP Edition - http://www.codelobster.com It works very well

Leave a Reply



User