Xdebug - Your first, last, and best option for troubleshooting PHP code
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 3,086 views

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 ...

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.

Statistics

Views

Total Views
3,086
Views on SlideShare
3,083
Embed Views
3

Actions

Likes
0
Downloads
5
Comments
0

3 Embeds 3

http://www.slideee.com 1
http://translate.googleusercontent.com 1
https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • Build better code
  • 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
  • What is Xdebug? Xdebug is a PHP extension providing:  Enhanced variable display  Enhanced stack traces  Function traces  Code coverage analysis  Code profiling  Remote debugging
  • 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.
  • 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.
  • 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.
  • 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. 
  • 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. 
  • 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. 
  • 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. 
  • 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. 
  • 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. 
  • 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. 
  • 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.
  • 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. 
  • 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. 
  • 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
  • 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
  • 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 “10.0.0.2” php_flag xdebug.profiler_enable_trigger true
  • 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”
  • 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
  • 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. 
  • 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. 
  • 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
  • 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
  • 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
  • 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. 
  • Demo Here we go with some code and examples.