28
XDEBUG: YOUR FIRST, LAST, AND BEST OPTION FOR TROUBLESHOOTING YOUR CODE Build better code

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

Embed Size (px)

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

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

XDEBUG: YOUR FIRST, LAST, AND BEST

OPTION FOR TROUBLESHOOTING

YOUR CODE

Build better code

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

Who Am I?

Adam [email protected]@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

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

What is Xdebug?

Xdebug is a PHP extension providing: Enhanced variable display Enhanced stack traces Function traces Code coverage analysis Code profiling Remote debugging

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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

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

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

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

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

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

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”

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

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

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

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.

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

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.

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

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

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

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

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

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

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

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.

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

Demo

Here we go with some code and examples.