作者:zhanhailiang 日期:2013-03-12
1.简述
引用官方描述:
The Xdebug extension helps you debugging your script by providing a lot of valuable debug information.
基本应用场景如下:
» Basic Features Xdebug's basic functions include the display of stack traces on error conditions, maximum nesting level protection and time tracking. » Variable Display Features Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well. » Stack Traces When Xdebug is activated it will show a stack trace whenever PHP decides to show a notice, warning, error etc. The information that stack traces display, and the way how they are presented, can be configured to suit your needs. » Function Traces Xdebug allows you to log all function calls, including parameters and return values to a file in different formats. » Code Coverage Analysis Code coverage tells you which lines of script (or set of scripts) have been executed during a request. With this information you can for example find out how good your unit tests are. » Profiling PHP Scripts Xdebug's built-in profiler allows you to find bottlenecks in your script and visualize those with an external tool such as KCacheGrind or WinCacheGrind. » Remote Debugging Xdebug provides an interface for debugger clients that interact with running PHP scripts. This section explains how to set-up PHP and Xdebug to allow this, and introduces a few clients.
2.安装配置教程
# 下载xdebug安装包 linux-06bq:/data/software/ # wget http://xdebug.org/files/xdebug-2.2.1.tgz linux-06bq:/data/software/ # tar zxvf xdebug-2.2.1.tgz linux-06bq:/data/software/ # cd xdebug-2.2.1/ # 编译安装xdebug linux-06bq:/data/software/xdebug-2.2.1/ # phpize linux-06bq:/data/software/xdebug-2.2.1/ # ./configure --with-php-config=/usr/local/services/php/bin/php-config --enable-xdebug linux-06bq:/data/software/xdebug-2.2.1/ # make linux-06bq:/data/software/xdebug-2.2.1/ # sudo make install Installing shared extensions: /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/ +----------------------------------------------------------------------+ | | | INSTALLATION INSTRUCTIONS | | ========================= | | | | See http://xdebug.org/install.php#configure-php for instructions | | on how to enable Xdebug for PHP. | | | | Documentation is available online as well: | | - A list of all settings: http://xdebug.org/docs-settings.php | | - A list of all functions: http://xdebug.org/docs-functions.php | | - Profiling instructions: http://xdebug.org/docs-profiling2.php | | - Remote debugging: http://xdebug.org/docs-debugger.php | | | | | | NOTE: Please disregard the message | | You should add "extension=xdebug.so" to php.ini | | that is emitted by the PECL installer. This does not work for | | Xdebug. | | | +----------------------------------------------------------------------+ linux-06bq:/data/software/xdebug-2.2.1/ # cp /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so /usr/local/services/php/extensions/ # 修改php配置文件,添加xdebug配置 ################################################################ add the following line to php.ini: zend_extension="/wherever/you/put/it/xdebug.so" (for non-threaded use of PHP, for example the CLI, CGI or Apache 1.3 module) or: zend_extension_ts="/wherever/you/put/it/xdebug.so" (for threaded usage of PHP, for example the Apache 2 work MPM or the the ISAPI module). Note: In case you compiled PHP yourself and used --enable-debug you would have to use zend_extension_debug=. From PHP 5.3 onwards, you always need to use the zend_extension PHP.ini setting name, and not zend_extension_ts, nor zend_extension_debug. However, your compile options (ZTS/normal build; debug/non-debug) still need to match with what PHP is using. ################################################################ linux-06bq:/data/software/xdebug-2.2.1/ # vim /usr/local/services/php/etc/php.ini zend_extension=/usr/local/services/php/extensions/xdebug.so [xdebug] ; 输出样式 xdebug.cli_color = 2 ; 输出参数控制 ; xdebug.collect_params=3 ; xdebug.collect_vars=1 ; 全局变量控制 xdebug.dump.GET=* xdebug.dump.POST=* ; xdebug.dump.SERVER=* ; xdebug.dump.COOKIE=* ; xdebug.dump.ENV=* ; 跟踪调用栈日志 xdebug.show_exception_trace=1 xdebug.auto_trace=1 xdebug.trace_options=1 xdebug.trace_format=1 ; 跟踪性能测试日志 xdebug.profiler_enable=1 xdebug.profiler_append=1 # 检测xdebug是否加载成功(两种方法,php -m或输出phpinfo()结果) linux-06bq:/data/software/xdebug-2.2.1/ # php -m|grep xdebug xdebug linux-06bq:/data/software/xdebug-2.2.1/ # php -r "phpinfo();" |grep xdebug xdebug xdebug support => enabled xdebug.auto_trace => Off => Off xdebug.cli_color => 0 => 0 xdebug.collect_assignments => Off => Off xdebug.collect_includes => On => On xdebug.collect_params => 0 => 0 xdebug.collect_return => Off => Off xdebug.collect_vars => Off => Off xdebug.coverage_enable => On => On xdebug.default_enable => On => On xdebug.dump.COOKIE => no value => no value xdebug.dump.ENV => no value => no value xdebug.dump.FILES => no value => no value xdebug.dump.GET => no value => no value xdebug.dump.POST => no value => no value xdebug.dump.REQUEST => no value => no value xdebug.dump.SERVER => no value => no value xdebug.dump.SESSION => no value => no value xdebug.dump_globals => On => On xdebug.dump_once => On => On xdebug.dump_undefined => Off => Off xdebug.extended_info => On => On xdebug.file_link_format => no value => no value xdebug.idekey => no value => no value xdebug.max_nesting_level => 100 => 100 xdebug.overload_var_dump => On => On xdebug.profiler_aggregate => Off => Off xdebug.profiler_append => Off => Off xdebug.profiler_enable => Off => Off xdebug.profiler_enable_trigger => Off => Off xdebug.profiler_output_dir => /tmp => /tmp xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p xdebug.remote_autostart => Off => Off xdebug.remote_connect_back => Off => Off xdebug.remote_cookie_expire_time => 3600 => 3600 xdebug.remote_enable => Off => Off xdebug.remote_handler => dbgp => dbgp xdebug.remote_host => localhost => localhost xdebug.remote_log => no value => no value xdebug.remote_mode => req => req xdebug.remote_port => 9000 => 9000 xdebug.scream => Off => Off xdebug.show_exception_trace => Off => Off xdebug.show_local_vars => Off => Off xdebug.show_mem_delta => Off => Off xdebug.trace_enable_trigger => Off => Off xdebug.trace_format => 0 => 0 xdebug.trace_options => 0 => 0 xdebug.trace_output_dir => /tmp => /tmp xdebug.trace_output_name => trace.%c => trace.%c xdebug.var_display_max_children => 128 => 128 xdebug.var_display_max_data => 512 => 512 xdebug.var_display_max_depth => 3 => 3 OLDPWD => /data/software/xdebug-2.2.1 _SERVER["OLDPWD"] => /data/software/xdebug-2.2.1 # 最后重启服务器或php-fpm即可(根据当前服务器加载PHP模式而定)
3.测试用例
简单的测试用例如下:
<?php
header( 'X-Test: Testing' );
setcookie( "TestCookie", "test-value" );
var_dump( xdebug_get_headers() );
输出如下:
array(2) {
[0] =>
string(15) "X-Test: Testing"
[1] =>
string(33) "Set-Cookie: TestCookie=test-value"
}
高级测试用例——查看变量的zval值
$a = array(1, 2, 3);
$b =& $a;
$c =& $a[2];
xdebug_debug_zval('a');
输出如下:
a: (refcount=2, is_ref=1)=array(3) {
[0] =>
(refcount=1, is_ref=0)= int(1)
[1] =>
(refcount=1, is_ref=0)= int(2)
[2] =>
(refcount=2, is_ref=1)= int(3)
}
4.扩展阅读
开始Xdebug之旅吧,少年!
5.Xdebug缺点及替代品
一般情况下来说,大家都是使用Xdebug,但是Xdebug太麻烦,需要各种配置,还有复杂的查看生成的数据文件,并且 Xdebug 无法再线上使用,因为特别占用CPU资源,所以就诞生了 Xhprof。Xhprof 是Facebook开源出来的一个性能测试工具,它比较轻量级,它运行更轻便快速,输出的数据更容易查看。
Xhprof 的优点:
-
它是个轻量级的性能监测工具,安装部署简单,占用系统资源更少
-
Xhprof 不是通过配置来启用监测,是否启用你直接在代码里启用,你需要监测哪个文件就在该文件启用监测,这样不会像Xdebug一样每个执行的文件都会记录下性能情况
-
Xhprof 自带Web断的结果查看页面,不需要像Xdebug另外安装
Xhprof是PHP C扩展,详细的安装配置与使用,请转至由黑夜路人与雪候鸟合作的【《PHP调试技术手册》P47】。