Using xDebug on Vagrant

Configuration

/etc/php.d/xdebug.ini

zend_extension=xdebug.so

;;;;;;;;;;;;;;;;;;;;;;;;;;
; Added to enable Xdebug ;
;;;;;;;;;;;;;;;;;;;;;;;;;;

xdebug.default_enable = 0
xdebug.idekey = "phpstorm"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
xdebug.remote_handler=dbgp
xdebug.remote_log="/var/log/xdebug.log"
; xdebug.remote_host=10.0.2.2 ; IDE-Environments IP, from vagrant box.
xdebug.remote_connect_back = 1

;PROFILLING
xdebug.profiler_enable = 0              ; Profiling not activated by default
xdebug.profiler_enable_trigger = 1      ; Profiling activated when requested by the GET parameter
xdebug.profiler_output_dir = "/var/log/xdebug"
xdebug.profiler_output_name = "cachegrind.out.%p"

xdebug.trace_format = 1
xdebug.auto_trace = 1
xdebug.trace_output_dir="/var/log/xdebug"

All settings

You can also enable xdebug from php scripts:

ini_set('xdebug.auto_trace', 1);

Profiling

Add ?XDEBUG_PROFILE=1 to the end of your URL and you will see:

# ls -l /var/log/xdebug/cachegrind.out*
-rw-r--r--. 1 apache apache 11035646 Feb 29 10:26 /var/log/xdebug/cachegrind.out.14443

Debugging

Add ?XDEBUG_SESSION=PHPSTORM to the end of your URL

Tracing

Add ?XDEBUG_TRACE=1 to the end of your URL

You can find some bookmarklets here

Tools

Gotchas

 Don't map xdebug port in Vagrantfile

e.g

 config.vm.network :forwarded_port, guest: 9000, host: 9000, auto_correct: true

could result in a port busy error

Url parameters are ignored by mod_rewrite?

Make sure you have [QSA] set:

RewriteRule ^page/(.*)/last$       page.php?pageid=$1 [L,QSA]

or define ?XDBEUG-XXX manually:

RewriteCond %{QUERY_STRING} ^XDEBUG_PROFILE
RewriteRule ^page/(.*)/last$       page.php?pageid=$1&XDEBUG_PROFILE=1 [L]

# Other requests go through as normal
RewriteRule ^page/(.*)/last$       page.php?pageid=$1 [L]

 Check your port is open

nmap -p 9000 server.name

Check the log file is writeable

ls -l /var/log/xdebug.log

If it doesn't exist create an empty file:

touch /var/log/xdebug.log
chmod [apache_owner] /var/log/xdebug

Useful in-code commands

  • xdebug_break() ; trigger a breakpoint
  • return xdebug_print_function_stack("stop here!"); print stack trace