Xdebug - Your first, last, and best option for troubleshooting PHP code


Published on

In depth presentation on setting up, configuring, and trigger PHP Xdebug for step through debugging and code profiling. This presentation will not tell you how to configure your Xdebug client. It will help you understand how to configure and tune Xdebug for web and CLI based debugging and profiling in PHP.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Xdebug - Your first, last, and best option for troubleshooting PHP code

  1. 1. Build better code
  2. 2. Who Am I? • Selling Source Direct Lead Developer • Coupla CTO • Founder/Organizer of Las Vegas PHP Users Group • Co-Organizer of Las Vegas Developers Users Group • PHP Machinist Maintainer • #VegasTech Enthusiast Adam Englander adamenglander@yahoo.com @adam_englander http://adamknowsstuff.com https://github.com/derptest
  3. 3. What is Xdebug? Xdebug is a PHP extension providing:  Enhanced variable display  Enhanced stack traces  Function traces  Code coverage analysis  Code profiling  Remote debugging
  4. 4. Who made Xdebug Xdebug is written by Derick Rethans. Derick is also the author of the PHP MongoDB extension. If you’d like to thank Derick, and you probably should, you can use his wish list.
  5. 5. What we’re not talking about Enhanced variable display  Enhanced stack traces  Function traces  Code coverage analysis  All of these are nice and all but they aren’t what were talking about.
  6. 6. What we are talking about Remote debugging: Step through debugging with a remote client via the DBGP protocol Code profiling: Finding bottlenecks in your code and visualize those with an external tool such as KCacheGrind or WinCacheGrind.
  7. 7. Xdebug Remote Debugging Line by line stepping through code.  Break points to stop at particular areas of code.  Variable inspection to be able to see variable values at a line of code. Death to var_dump.  Variable setting to allow replacement of variable values at runtime.  The single best way to troubleshoot bugs. 
  8. 8. Xdebug Profiling Find bottlenecks  Timings for optimization  Similar to Cachegrind but no memory data  Output format is standard so it can be read by standard Cachegrind tools: Kcachegrind, QCacheGrind, WinCacheGrind, IDE plugins, etc. 
  9. 9. Xdebug Requirements  Installing the Xdebug extension  PEAR/PECL install available  Available via most Linux package managers DBGP client for remote debugging – Debugclient comes with Xdebug source and a slew of IDEs have built in support.  Cachegrind analyzer for profiling – KCachegrind, QCachegrind, WinCacheGrind, or IDE support. 
  10. 10. Remote Debug Configuration extended_info – Enabled by default. Allows breakpoints.  idekey – Identifier for the debug session. Most clients will restrict to requests from a single idekey value.  remote_autostart – Disabled by default. Will start remote debugging with every request. 
  11. 11. Remote Debug Configuration remote_connect_back – Disabled by default. Helpful for debugging on shared hosts and virtual machines. Xdebug session will connect to host making the request.  remote_cookie_expire_time – Defaults to 3600 seconds (60 minutes). The time the debug session cookie will live. 
  12. 12. Remote Debug Configuration remote_enable – Defaults to 0 (off). Enable remote debugging  remote_handler – Defaults to DBGP. You should never have to use anything else so just leave it.  remote_host – Remote host with which Xdebug will connect at the start of an Xdebug session. 
  13. 13. Remote Debug Configuration remote_log – Default value “”. Log all remote debugger communication. You will probably never need this unless you are writing a client or have connectivity issues.  remote_mode – Default value is “req”. Determines how the session is initiated. “req” will start the session with every request. “jit” will start the session when an error condition occurs. 
  14. 14. Remote Debug Configuration  remote_port – Defaults to 9000. TCP/IP port of the remote client which which Xdebug will connect. Most clients default to 9000 so unless you are forced to debug multiple remote machines, just leave it.
  15. 15. Profiling Configuration profiler_append – Default value 0 (off). Append the profiler output to the same file if the profile_output_name will allow.  profiler_enable – Default value 0 (off). Enable profiling for all requests and script executions. If you are doing web request profiling, use profiler_enable_trigger instead. This should normally be used only for CLI profiling and configuring via environment variable. 
  16. 16. Profiling Configuration profiler_enable_trigger – Default value 0 (off). Allow triggering of profiling via request query parameter or cookie with the name XDEBUG_PROFILE. The value is optional and ignored.  profiler_output_dir – Defaults to “/tmp”. The directory in which the profiler output file will be written. 
  17. 17. Profiling Configuration  profiler_output_name – Default value is “cachegrind.out.%p” where %p is the process ID of the process running the profile. There are many variable options like %s for the file name. See the Xdebug documentation for details: http://xdebug.org/docs/all_settings#trace _output_name
  18. 18. Configuring via php.ini Always prepend option with “xdebug.” Example: zend_extension=/usr/local/php/modules/xd ebug.so xdebug.remote_enable=1 xdebug.remote_connect_back=1 xdebug.profiler_enable_trigger=1
  19. 19. Configuring via .htaccess If you have no other options, you can use a .htaccess file if allowed. Example: php_flag xdebug.remote_enable true php_value xdebug.remote_host “” php_flag xdebug.profiler_enable_trigger true
  20. 20. Configuring via environment  When running CLI scripts, you can configure Xdebug via the XDEBUG_CONFIG variable Profiling Example: export XDEBUG_CONFIG=“profiler_enable=1” Remote Debugging Example: export XDEBUG_CONFIG=“idekey=key”
  21. 21. Start/Stop Xdebug Sessions You can start and stop Xdebug remote debugging and profiling sessions in many ways:  IDE integration  Browser Plugin (Chrome/Firefox/Safari)  URL parameter  Cookie  CLI environment variable  Xdebug config
  22. 22. Start/Stop via IDE Most IDEs will have a way to start a remote debug or profiling session from the command line or web page.  See the documentation for your IDE to learn how to use that. 
  23. 23. Start/Stop via Browser Plugin Browser plugins are a nice simple way to start and stop an Xebug session  Available for most browsers.  Will automatically (un)set the XDEBUG_SESSION or XDEBUG_PROFILE cookie. 
  24. 24. Start/Stop via URL Parameter XDEBUG_SESSION_START=idekey  XDEBUG_SESSION_STOP  XDEBUG_PROFILE  Example: http://site.com?XDEBUG_SESSION_START=go http://site.com?XDEBUG_SESSION_STOP http://site.com?XDEBUG_PROFILE
  25. 25. Start/Stop via Cookie Use a cookie named XDEBUG_SESSION with the value being the idekey value. Remote Debug Example: curl --cookie=“XDEBUG_SESSION=eclipse” http://host.com Profiling Example: curl --cookie=“XDEBUG_PROFILE=1” http://host.com
  26. 26. Start/Stop via CLI Environment   XDEBUG_CONFIG environment variable will start stop CLI Xdebug for profiling and debugging Can be executed for a single script or multiple scripts Multi-Command Example: export XDEBUG_CONFIG=“remote_host=localhost” php script.php export XDEBUG_CONFIG= Single Command Example: XDEBUG_CONFIG=“profiler_enable=1” php script.php
  27. 27. Start/Stop via Xdebug Config Setting xebug.remote_autostart will start remote debugging with every execution.  Setting xdebug.profiler_enable will start profiling with every execution.  Requires config change to turn off.  Try and avoid this. There are so many better ways. 
  28. 28. Demo Here we go with some code and examples.