xdebug php 调试

当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 == 500000xdebug_break();
    }
}

set_time_limit(1);
$c = new stdClass;
$c->bar 100;
$a = array(
    
42 => false'foo' => 912124,
    
$c, new stdClassfopen'/etc/passwd''r' )
);
foo$a );
?>

输出结果

不同的xdebug.collect_params参数的值,对应的输出内容也是不同的。可以通过下面的示例查看:


( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 34
Call Stack
#TimeMemoryFunctionLocation
1 0.0001 58564{main}( ) ../stack.php:0
2 0.0004 62764foo( ) ../stack.php:47
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
#TimeMemoryFunctionLocation
1 0.0001 58132{main}( ) ../stack.php:0
2 0.0004 62380foo( 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
#TimeMemoryFunctionLocation
1 0.0001 58564{main}( ) ../stack.php:0
2 0.0004 62812foo( 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
#TimeMemoryFunctionLocation
1 0.0001 58564{main}( ) ../stack.php:0
2 0.0004 62812foo( 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
#TimeMemoryFunctionLocation
1 0.0001 58132{main}( ) ../stack.php:0
2 0.0004 62380foo( $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_globalsxdebug.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
#TimeMemoryFunctionLocation
1 0.0001 58564{main}( ) ../stack.php:0
2 0.0004 62764foo( ) ../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
#TimeMemoryFunctionLocation
1 0.0001 58132{main}( ) ../stack.php:0
2 0.0004 62436foo( ) ../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
#TimeMemoryFunctionLocation
1 0.0001 58132{main}( ) ../stack.php:0
2 0.0005 62588foo( ) ../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

相关设置


xdebug.cli_color
类型:  boolean(布尔型), 默认值:  0, 在 Xdebug 2.2 版中引进
如果此项设置为1,当使用CLI模式时,Xdebug将为所有的变量信息和堆栈轨迹添加颜色。

xdebug.collect_includes
类型:  boolean(布尔型), 默认值:  1
默认值为1。控制Xdebug是否应该将用于include(), include_once(), require()或require_once()的文件名写入轨迹文件。

xdebug.collect_params
类型:  integer(整型), 默认值:  0

默认值为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_dataxdebug.var_display_max_depth参数来决定。
4 完整变量内容和变量名

1 在带有CLI的PHP版本中,将不会拥有工具附件,在输出文件中同样也没有。


xdebug.collect_vars
类型:  boolean(布尔型), 默认值:  0
这个设置告诉Xdebug来搜集在一定范围内使用的变量的信息。这一过程很慢,因为Xdebug必须对PHP的opcode数组进行反向工程。这个设置不会记录不同变量的变量值,如果需要记录变量值,可以使用 xdebug.collect_params。只有当你希望使用 xdebug_get_declared_vars()参数时, xdebug.collect_params才需要被启用。

xdebug.dump.*
类型:  string(字符串), 默认值:  Empty

* = COOKIE, FILES, GET, POST, REQUEST, SERVER, SESSION。这7个参数控制当错误情况发生时,显示哪些超全局变量。在php.ini设置中,要显示的超全局变量之间用逗号隔开,但是要确保没有添加空格。如果要在错误发生时显示REMOTE_ADDR和REQUEST_METHOD,添加如下设置:

xdebug.dump.SERVER = REMOTE_ADDR,REQUEST_METHOD

xdebug.dump_globals
类型:  boolean(布尔型), 默认值:  1
控制是否显示由 xdebug.dump.* 参数指定的超全局变量的值。

xdebug.dump_once
类型:  boolean(布尔型), 默认值:  1
控制是对所有的错误情况(设置为0)都显示超全局变量的值还是只对第一个发生的错误情况(设置为1)显示超全局变量的值。

xdebug.dump_undefined
类型:  boolean(布尔型), 默认值:  0
如果你想显示超全局变量中未定义的值,你应该将此项设置为1,否则使用默认值。

xdebug.file_link_format
类型:  string(字符串), 默认值:  , 在 Xdebug 2.1 版中引进

此设置控制在堆栈轨迹显示中的超链接格式,堆栈轨迹中使用了文件名称。它将允许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.manual_url
类型:  string(字符串), 默认值:  http://www.php.net
xdebug信息中有关函数和错误信息的超链接将直接链接到PHP帮助手册中的函数页面。最好将此项设置为最近的镜像。

xdebug.show_exception_trace
类型:  integer(整型), 默认值:  0
当此项设置为1时, 不关什么时候发生异常,Xdebug都将显示一个堆栈轨迹-即使已经捕捉到了异常。

xdebug.show_local_vars
类型:  integer(整型), 默认值:  0
当此项设置为不等于0的值时,Xdebug在错误发生时生成的堆栈信息仍然将在最顶层显示所有变量的信息。注意,使用这个选项可能会生成很多信息,所以此参数默认是关闭的。

xdebug.show_mem_delta
类型:  integer(整型), 默认值:  0
当此项设置为不等于0的值时,Xdebug生成的人类可读的轨迹文件将会显示函数调用间不同的内存使用情况。如果Xdebug被设置为生成机器可读的轨迹文件,那么将始终显示此信息。

xdebug.var_display_max_children
类型:  integer(整型), 默认值:  128
此参数用于当使用 xdebug_var_dump()xdebug.show_local_vars函数轨迹来显示变量时,控制数组子节点和对象属性的显示数量。 此参数对通过 原程调试功能发送给客户端的子节点的数量没有任何影响。

xdebug.var_display_max_data
类型:  integer(整型), 默认值:  512
此参数用于当使用 xdebug_var_dump()xdebug.show_local_vars函数轨迹来显示变量时,控制被显示的字符串的最大长度。此参数对通过 原程调试功能发送给客户端的数据的数量没有任何影响。

xdebug.var_display_max_depth
类型:  integer(整型), 默认值:  3
此参数用于当使用 xdebug_var_dump()xdebug.show_local_vars函数轨迹来显示变量时,控制数组元素和对象属性的最大嵌套层数。 此参数对通过 原程调试功能发送给客户端的子节点的深度(译者注:深度是数据结构上的概念)没有任何影响。

相关函数


array xdebug_get_declared_vars()
返回已声明的变量

返回的数组中包含了当前作用域中定义的每个变量的名称。 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()函数的作用域中,它没有被使用。


array xdebug_get_ function_stack()
返回堆栈的相关信息

在这里,返回的数组内容类似于堆栈轨迹。示例代码:

示例代码:

<?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)


integer(整型) xdebug_get_stack_depth()
返回当前堆栈的深度

返回堆栈的深度。脚本的主体部分深度为0,而每一个include或函数调用都将导致堆栈深度加1.


none xdebug_print_ function_stack(  [ string message ] )
显示当前的函数堆栈

显示当前的函数堆栈信息,类似于Xdebug在错误情况时显示的信息。

"message"参数在Xdebug 2.1版中引进。

示例代码:

<?php
function foo$far$out )
{
    
xdebug_print_function_stack'Your own message' );
}
foo423141592654 );
?>

返回值:


    
    
( ! ) Xdebug: Your own message in /home/httpd/html/test/xdebug/print_function_stack.php on line 5
Call Stack
#TimeMemoryFunctionLocation
10.0006653896{main}( )../print_function_stack.php:0
20.0007654616foo( 42, 3141592654 )../print_function_stack.php:7
30.0007654736xdebug_print_function_stack ( 'Your own message' )../print_function_stack.php:5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值