当xdebug激活时,php一旦要显示通知、警告或错误时,xdebug 显示堆栈跟踪信息。这个堆栈信息能跟据你的需要来配置显示。
xdebug显示的堆栈跟踪都是以保守数量状态显示信息。因为大量的信息处理和呈现会拖慢脚本执行。通过不同的设置对于显示更多详尽的信息提供了可能。
堆栈跟踪的变量
xdebug一般会在堆栈跟踪时显示变量信息。在收集和显示情况下变量信息都会携带大量资源。尽管如此,很多情况下这些变量信息的显示是很有帮助的,这是为什么会有xdebug.clollect_params设置的原因。以下脚本,会在设置不同值情况下输出不同的信息:
the scriptphpfunction 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 );?>
默认值:
( ! ) 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
1值:
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
2值:
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
3值:
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
4值:
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.cli_color
类型: integer, 默认值: 0, 始于 2.2版以上
设置为1时,xdebug会在cli模式下且在tty终端输出时,会以有色显示var_dump跟踪输出的文字。window下, ansicon工具需要安装。
设置为2时,则不管是否连接到tty终端或ansicon是否被安装,xdebug会一直用颜色显示var_dump和调试跟踪信息。这种情形下,你可能在结束处看到转义码。
xdebug.collect_includes
类型: boolean, 默认值: 1
默认情况下xdebug将使用include(), include_once(), require() 或 require_once()方法引用的文件名写入到跟踪文件里。
xdebug.collect_params
类型: integer, 默认值: 0
默认为0时,该设置控制xdebug不管是函数追踪还是堆栈跟踪都会收集调用函数的参数。
默认0值是考虑到大规模脚本会占用大量内存,所以不会为了大脚本来运行它。你可以安全地打开此设置,但你会预料到会一些脚本上的问题像大量函数调用兼庞大的数据结构作为参数传递。xdebug2不会有增加内存使用的问题,因为它不会存储到内存,而是只存入磁盘中。这只需要你有足够的磁盘使用量即可。
该设置有4种设置值。每种都会呈现不同的信息。以下表格展示各种设置值信息:
value
argument information shown
0
无.
1
展示变量元素的值类型和值。
2
展示变量元素的值类型和值,并附带滑鼠提示显示完整信息。(cli模式下不存在滑鼠提示)
3
完整变量内容(内容受限于以下设置: xdebug.var_display_max_children,xdebug.var_display_max_data and xdebug.var_display_max_depth.)
4
完整变量内容和名称。
5
php 序列化变量内容,不含名称。(2.3版本新特性)
xdebug.collect_vars
类型: boolean, 默认值: 0
该设置会让xdebug在一定范围内去收集变量信息。而这种分析工作相当慢因为xdebug会逆向解析php代码。该设置不会记录不同变量的值。如果你要利用xdebug_get_declared_vars()函数,那么就需要开启该设置了。
xdebug.dump.*
类型: string, 默认值: empty
* 号可以用 cookie, files, get, post, request, server, session任意一个来代替. 这七个设置值控制在错误发生时的超全局变量的数据。
在php.ini中每个设置值都由逗号分隔形成变量列表,或者*号代表全部。要确定你在设置里没有空格。
为了在错误发生时收到remote_addr 和 request_method 信息和所有get参数,可以设置:
xdebug.dump.server = remote_addr,request_method
xdebug.dump.get = *
xdebug.dump_globals
类型: boolean, 默认: 1
控制超全局变量值是否显示,无论在xdebug.dump.*设置了什么。
xdebug.dump_once
类型: boolean, 默认值: 1
控制是否在所有错误情况下显示超全局变量值(设为0值)或只在第一次出现(设为1值)。
xdebug.dump_undefined
类型: boolean, 默认值: 0
如果需要显示超全局变量中未定义值则该项设为1,否则保留0默认项。
xdebug.manual_url
类型: string, 默认值: http://www.php.net, 始于 xdebug 2.2.1以下版本
指定函数追踪和错误信息的链接说明来源。建议设定使用最近的镜像链接。
xdebug.show_exception_trace
类型: integer, 默认值: 0
当设置为1时,xdebug会在异常出现时甚至是该异常被捕捉也会显示其堆栈跟踪信息。
xdebug.show_local_vars
类型: integer, 默认值: 0
当设置为非0值时,xdebug在错误情况下产生的堆栈跟踪会显示所有变量信息在最顶端范围。这有可能会产生大量信息,所以默认情况下关闭。
xdebug.show_mem_delta
type: integer, default value: 0
当该设置不为0时,xdebug的人类可读性追踪记录文件会显示函数调用时内存使用量。如果xdebug配值为产生机器可读性追踪文件,那么它们经常显示这些信息。、
xdebug.var_display_max_children
类型: integer, 默认值: 128
在使用 xdebug_var_dump(),xdebug.show_local_vars 或 追踪函数时,该设置控制数组元素和对象属性的数量显示。
若不受限制,可以设为-1值。
该设置不受remot_debuggin远程调试的任何影响。
xdebug.var_display_max_data
类型: integer, 默认值: 512
在使用 xdebug_var_dump(),xdebug.show_local_vars 或 追踪函数时,该设置控制字符串长度显示最大值。
若不受限制,可以设为-1值。
该设置不受remot_debugging远程调试的任何影响。
xdebug.var_display_max_depth
类型: integer, 默认值: 3
在使用 xdebug_var_dump(),xdebug.show_local_vars 或 追踪函数时,该设置控制数组元素和对象属性的显示层级。
最大值为1023,你可以设为-1表示其最大值。
该设置不受remot_debugging远程调试的任何影响。
相关函数:
array xdebug_get_declared_vars()
返回一个数组,数组元素都是当前范围内已定义的变量名。要使函数生效则xdebug.collect_vars必须开启。
example:
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));?>/**returns: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()
返回一数组,内含在函数这个点显示出来的类似于堆栈跟踪的信息。
example:
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'));?>/**returns: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级而各种引用或调用函数则添加一个堆栈深度层级。
none xdebug_print_function_stack( [ string message [, int options ] ] )
用类似在错误情况下显示当前函数追踪信息。
message 参数允许你可以自定义显示信息。 (始于xdebug 2.1版本).
example:
phpfunction foo( $far, $out ){ xdebug_print_function_stack( 'your own message' );}foo( 42, 3141592654 );?>
returns:
( ! ) 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
掩码参数options 允许你配置一些额外的参数选项。支持的选项有:
xdebug_stack_no_desc
如果设置此项,则显示的追踪信息不包含头部。这对于你想从自定义的错误处理器中显示自己的错误追踪信息就很有用。除此之外,你可以在需要显示的位置调用xdebug_print_function_statck()函数。(始于xdebug2.3)
void xdebug_start_function_monitor( array $list_of_functions_to_monitor )
开始函数监控。
始于版本2.4
该函数在将一系列的函数名作为参数传递时就开始监视这些函数。函数监视器会找出这些你提供的函数所在的代码。这样可以用于追踪那些旧函数或废弃的函数。
example:
phpxdebug_start_function_monitor( [ 'strrev', 'array_push' ] );?>
你也可以添加类方法或静态方法到数组中进行监视。例如,为了捕获静态调用drammodel::cansee 和 动态调用 whisky->drink,你可以开始以下监视:
example:
phpxdebug_start_function_monitor( [ 'drammodel::cansee', 'whisky->drink'] );?>
被定义的函数要区分大小写,若动态调用到静态方法将不会被捕获。
void xdebug_stop_function_monitor()
停止函数监视
始于版本2.4
该函数停止对函数监视。需要获取被监视函数列表,可以使用xdebug_get_monitored_functions()函数。