当Xdebug被激活时,它将显示一个堆栈使用轨迹,不管PHP是否开启了错误提示。堆栈轨迹显示的信息以及信息的呈现形式,都可以进行自定义配置以满足开发者的需要。
在错误发生时(如果php.ini的display_errors 设置为on),Xdebug对其所显示的大量的堆栈轨迹是非常谨慎的。这是因为堆栈轨迹的显示不仅会降低脚本的执行效率,而且还会有损堆栈信息在浏览器中的呈现。但是,可以通过不同的设置来显示详细的堆栈轨迹。
堆栈轨迹中的变量
默认情况下,Xdebug会显示由堆栈轨迹产生的变量的信息。显示变量信息会消耗一定的系统资源,包括搜集变量和显示变量。但是,在许多情况下,显示变量信息是十分有用的,而且这也是为什么Xdebug提供设置参数xdebug.collect_params的原因。下面的脚本,根据不同的xdebug.collect_params值,输出的内容也不同 。
脚本
<?php
function foo( $a ) {
for ($i = 1; $i < $a['foo']; $i++) {
if ($i == 500000) xdebug_break();
}
}
set_time_limit(1);
$c = new stdClass;
$c->bar = 100;
$a = array(
42 => false, 'foo' => 912124,
$c, new stdClass, fopen( '/etc/passwd', 'r' )
);
foo( $a );
?>
输出结果
不同的xdebug.collect_params参数的值,对应的输出内容也是不同的。可以通过下面的示例查看:
ini_set('xdebug.collect_params', '1');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58132 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62380 | foo( array(5) ) | ../stack.php:47 |
ini_set('xdebug.collect_params', '2');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58564 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62812 | foo( array(5) ) | ../stack.php:47 |
ini_set('xdebug.collect_params', '3');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58564 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62812 | foo( array (42 => FALSE, 'foo' => 912124, 43 => class stdClass { public $bar = 100 }, 44 => class stdClass { }, 45 => resource(2) of type (stream)) ) | ../stack.php:47 |
ini_set('xdebug.collect_params', '4');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58132 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62380 | foo( $a = array (42 => FALSE, 'foo' => 912124, 43 => class stdClass { public $bar = 100 }, 44 => class stdClass { }, 45 => resource(2) of type (stream)) ) | ../stack.php:47 |
其它信息
在上面的例子中,对于传递给每个函数的变量值,通过使用xdebug.dump_globals和xdebug.dump.*参数,Xdebug还可以任意地显示相关的超全局变量的信息。 参数xdebug.dump_once 和xdebug.dump_undefined,可以对超全局变量显示的内容和以及在什么时候显示稍作控制。使用xdebug.show_local_vars参数,你可以让Xdebug为一个用户自定义函数显示处于堆栈最顶端的所有变量。【译者注:从下面的例子来看,是显示函数内的局部变量】下面的示例将向你展示参数的效果(该示例对应的脚本和上面的示例一样)。
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 34 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58564 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62764 | foo( ) | ../stack.php:47 |
ini_set('xdebug.collect_vars', 'on'); ini_set('xdebug.collect_params', '4'); ini_set('xdebug.dump_globals', 'on'); ini_set('xdebug.dump.SERVER', 'REQUEST_URI');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 33 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0001 | 58132 | {main}( ) | ../stack.php:0 |
2 | 0.0004 | 62436 | foo( ) | ../stack.php:47 |
Dump $_SERVER | ||||
$_SERVER['REQUEST_URI'] = | string '/test/xdebug/docs/stack.php?level=5' (length=35)
|
ini_set('xdebug.collect_vars', 'on'); ini_set('xdebug.collect_params', '4'); ini_set('xdebug.dump_globals', 'on'); ini_set('xdebug.dump.SERVER', 'REQUEST_URI'); ini_set('xdebug.show_local_vars', 'on');
( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 | |||||
---|---|---|---|---|---|
Call Stack | |||||
# | Time | Memory | Function | Location | |
1 | 0.0001 | 58132 | {main}( ) | ../stack.php:0 | |
2 | 0.0005 | 62588 | foo( ) | ../stack.php:47 | |
Dump $_SERVER | |||||
$_SERVER['REQUEST_URI'] = | string '/test/xdebug/docs/stack.php?level=6' (length=35)
| ||||
Variables in local scope (#2) | |||||
$a = | array 42 => boolean(布尔型) false 'foo' => int 912124 43 => object(stdClass)[1] public 'bar' => int 100 44 => object(stdClass)[2] 45 => resource(2, stream) | ||||
$i = | int 275447
|
相关设置
默认值为0。当一个函数调用在函数轨迹或堆栈轨迹中被记录时,控制Xdebug是否搜集传递到函数的参数信息。
默认值为0,是因为对于非常大的脚本,它可能会耗费大量的内存以至于脚本无法运行。开启此设置是安全的,但是如果在脚本中调用了大量函数或者使用了较大的数据结构作为参数时,你可能会遇到一些问题。Xdebug 2改进了内存使用,因此不用担心此种情况,因为它不会在内存中存储这些信息,而是把它们写入磁盘。这意味着你需要注意磁盘空间的使用情况。
这个参数可以有4个不同的值,每一个值都将对应显示不同的信息。从下面的示例中,你可以看出每个值所提供的信息是什么。也可以查看Stack Traces特征的介绍。
参数值 | 显示信息 |
---|---|
0 | 无任何信息 |
1 | 变量类型和数量(如string(6), array(8)). |
2 | 变量类型和数量,附带有用于显示完整信息的小工具1。 |
3 | 完整变量内容(变量内容的显示由xdebug.var_display_max_children,xdebug.var_display_max_data和xdebug.var_display_max_depth参数来决定。 |
4 | 完整变量内容和变量名 |
1 在带有CLI的PHP版本中,将不会拥有工具附件,在输出文件中同样也没有。
* = COOKIE, FILES, GET, POST, REQUEST, SERVER, SESSION。这7个参数控制当错误情况发生时,显示哪些超全局变量。在php.ini设置中,要显示的超全局变量之间用逗号隔开,但是要确保没有添加空格。如果要在错误发生时显示REMOTE_ADDR和REQUEST_METHOD,添加如下设置:
xdebug.dump.SERVER = REMOTE_ADDR,REQUEST_METHOD
此设置控制在堆栈轨迹显示中的超链接格式,堆栈轨迹中使用了文件名称。它将允许IDE来建立一个链接协议,通过点击Xdebug显示在堆栈轨迹中的文件名能够直接跳转到指定的行和文件。 链接格式可能是这种形式:
myide://%f@%l
可用的格式操作符有:
操作符 | 意义 |
---|---|
%f | 文件名 |
%l | 行号 |
如果要在linux下使用火狐浏览器,请遵循以下步骤:
- 打开about:config
- 添加一个boolean(布尔型)设置"network.protocol-handler.expose.xdebug"
- 将下面的代码写入一个shell脚本"~/bin/ff-xdebug.sh":
#! /bin/sh f=`echo $1 | cut -d @ -f 1 | sed 's/xdebug:\/\///'` l=`echo $1 | cut -d @ -f 2`
再添加以下的其中一行(取决与你用komodo还是gvim):komodo $f -l $l
gvim --remote-tab +$l $f
- 使用
chmod +x ~/bin/ff-xdebug.sh
命令让脚本可执行 - 将xdebug.file_link_format设置为
xdebug://%f@%l
相关函数
返回的数组中包含了当前作用域中定义的每个变量的名称。 xdebug.collect_vars参数需要被启用。
<?php
class strings {
static function fix_strings($a, $b) {
foreach ($b as $item) {
}
var_dump(xdebug_get_declared_vars());
}
}
strings::fix_strings(array(1,2,3), array(4,5,6));
?>
array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'item' (length=4)
在PHP5.1之前, 变量名"a"并没有包含在返回的数组中,因为在调用xdebug_get_declared_vars()函数的作用域中,它没有被使用。
在这里,返回的数组内容类似于堆栈轨迹。示例代码:
<?php
class strings {
function fix_string($a)
{
var_dump(xdebug_get_function_stack());
}
function fix_strings($b) {
foreach ($b as $item) {
$this->fix_string($item);
}
}
}
$s = new strings();
$ret = $s->fix_strings(array('Derick'));
?>
array 0 => array 'function' => string '{main}' (length=6) 'file' => string '/var/www/xdebug_get_function_stack.php' (length=63) 'line' => int 0 'params' => array empty 1 => array 'function' => string 'fix_strings' (length=11) 'class' => string 'strings' (length=7) 'file' => string '/var/www/xdebug_get_function_stack.php' (length=63) 'line' => int 18 'params' => array 'b' => string 'array (0 => 'Derick')' (length=21) 2 => array 'function' => string 'fix_string' (length=10) 'class' => string 'strings' (length=7) 'file' => string '/var/www/xdebug_get_function_stack.php' (length=63) 'line' => int 12 'params' => array 'a' => string ''Derick'' (length=8)
返回堆栈的深度。脚本的主体部分深度为0,而每一个include或函数调用都将导致堆栈深度加1.
显示当前的函数堆栈信息,类似于Xdebug在错误情况时显示的信息。
"message"参数在Xdebug 2.1版中引进。
<?php
function foo( $far, $out )
{
xdebug_print_function_stack( 'Your own message' );
}
foo( 42, 3141592654 );
?>
( ! ) Xdebug: Your own message in /home/httpd/html/test/xdebug/print_function_stack.php on line 5 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0006 | 653896 | {main}( ) | ../print_function_stack.php:0 |
2 | 0.0007 | 654616 | foo( 42, 3141592654 ) | ../print_function_stack.php:7 |
3 | 0.0007 | 654736 | xdebug_print_function_stack ( 'Your own message' ) | ../print_function_stack.php:5 |