View
4.281
Download
2
Category
Preview:
DESCRIPTION
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.
Citation preview
XDEBUG: YOUR FIRST, LAST, AND BEST
OPTION FOR TROUBLESHOOTING
YOUR CODE
Build better code
Who Am I?
Adam Englanderadamenglander@yahoo.com@adam_englanderhttp://adamknowsstuff.comhttps://github.com/derptest
• 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
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 aboutRemote 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 extensionPEAR/PECL install availableAvailable 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/xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.profiler_enable_trigger=1
Configuring via .htaccessIf 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.
Recommended