Home > Article > Backend Development > Xdebug document (4) function tracking, xdebug document function tracking_PHP tutorial
Xdebug allows you to record all function calls, including parameters and return values, into files in different formats.
These so-called "function tracing" features can help you face a new application, or you want to figure out what the program is doing when it is running. Function tracing can optionally display the values of variables passed by a function or method, as well as the return value. Tracking of these two elements is not enabled by default.
There are three output formats. One has human-readable tracking information, another is more suitable for computer programs to parse, and the last uses HTML to format the tracking information. You can switch between these two ill-conceived formats using the xdebug_trace_format setting. There are also settings that control what information is written to the trace file. For example, set up variables (xdebug.collect_params) and return values (xdebug.collect_return). The following examples show the effect of different settings of human-readable function trace information:
<?<span>php </span><span>$str</span> = "Xdebug"<span>; </span><span>function</span> ret_ord( <span>$c</span><span> ) { </span><span>return</span> <span>ord</span>( <span>$c</span><span> ); } </span><span>foreach</span> ( <span>str_split</span>( <span>$str</span> ) <span>as</span> <span>$char</span><span> ) { </span><span>echo</span> <span>$char</span>, ": ", ret_ord( <span>$char</span> ), "\n"<span>; } </span>?>
The following are the results when xdebug.collect_params is set to different values. When not in a web environment, the 2 value does not include mouse prompts.
Default value:
TRACE START [2007-05-06 14:37:06]
0.0003 114112 -> {main}() ../trace.php:0
0.0004 114272 -> str_split() ../trace.php:8
0.0153 117424 -> ret_ord() ../trace.php:10
0.0165 117584 -> ord() ../trace.php:5
0.0166 117584 -> ret_ord() ../trace.php:10
0.0167 117584 -> ord() ../trace.php:5
0.0168 117584 -> ret_ord() ../trace.php:10
0.0168 117584 -> ord() ../trace.php:5
0.0170 117584 -> ret_ord() ../trace.php:10
0.0170 117584 -> ord() ../trace.php:5
0.0172 117584 -> ret_ord() ../trace.php:10
0.0172 117584 -> ord() ../trace.php:5
0.0173 117584 -> ret_ord() ../trace.php:10
0.0174 117584 -> ord() ../trace.php:5
0.0177 41152
TRACE END [2007-05-06 14:37:07]
Collect_params=1:
TRACE START [2007-05-06 14:37:11]
0.0003 114112 -> {main}() ../trace.php:0
0.0004 114272 -> str_split(string(6)) ../trace.php:8
0.0007 117424 -> ret_ord(string(1)) ../trace.php:10
0.0007 117584 -> ord(string(1)) ../trace.php:5
0.0009 117584 -> ret_ord(string(1)) ../trace.php:10
0.0009 117584 -> ord(string(1)) ../trace.php:5
0.0010 117584 -> ret_ord(string(1)) ../trace.php:10
0.0011 117584 -> ord(string(1)) ../trace.php:5
0.0012 117584 -> ret_ord(string(1)) ../trace.php:10
0.0013 117584 -> ord(string(1)) ../trace.php:5
0.0014 117584 -> ret_ord(string(1)) ../trace.php:10
0.0014 117584 -> ord(string(1)) ../trace.php:5
0.0016 117584 -> ret_ord(string(1)) ../trace.php:10
0.0016 117584 -> ord(string(1)) ../trace.php:5
0.0019 41152
TRACE END [2007-05-06 14:37:11]
Collect_params=3:
TRACE START [2007-05-06 14:37:13]
0.0003 114112 -> {main}() ../trace.php:0
0.0004 114272 -> str_split('Xdebug') ../trace.php:8
0.0007 117424 -> ret_ord('X') ../trace.php:10
0.0007 117584 -> ord('X') ../trace.php:5
0.0009 117584 -> ret_ord('d') ../trace.php:10
0.0009 117584 -> ord('d') ../trace.php:5
0.0010 117584 -> ret_ord('e') ../trace.php:10
0.0011 117584 -> ord('e') ../trace.php:5
0.0012 117584 -> ret_ord('b') ../trace.php:10
0.0013 117584 -> ord('b') ../trace.php:5
0.0014 117584 -> ret_ord('u') ../trace.php:10
0.0014 117584 -> ord('u') ../trace.php:5
0.0016 117584 -> ret_ord('g') ../trace.php:10
0.0016 117584 -> ord('g') ../trace.php:5
0.0019 41152
TRACE END [2007-05-06 14:37:13]
Collect_params=4:
TRACE START [2007-05-06 14:37:16]
0.0003 114112 -> {main}() ../trace.php:0
0.0004 114272 -> str_split('Xdebug') ../trace.php:8
0.0007 117424 -> ret_ord($c = 'X') ../trace.php:10
0.0007 117584 -> ord('X') ../trace.php:5
0.0009 117584 -> ret_ord($c = 'd') ../trace.php:10
0.0009 117584 -> ord('d') ../trace.php:5
0.0010 117584 -> ret_ord($c = 'e') ../trace.php:10
0.0011 117584 -> ord('e') ../trace.php:5
0.0012 117584 -> ret_ord($c = 'b') ../trace.php:10
0.0013 117584 -> ord('b') ../trace.php:5
0.0014 117584 -> ret_ord($c = 'u') ../trace.php:10
0.0014 117584 -> ord('u') ../trace.php:5
0.0016 117584 -> ret_ord($c = 'g') ../trace.php:10
0.0016 117584 -> ord('g') ../trace.php:5
0.0019 41152
TRACE END [2007-05-06 14:37:16]
In addition to the xdebug.collet_params settings, there are other settings that affect the output of the trace file. "show_mem_delta=1" can display memory usage in two different columns.
TRACE START [2007-05-06 14:37:26]
0.0003 114112 +114112 -> {main}() ../trace.php:0
0.0004 114272 +160 -> str_split('Xdebug') ../trace.php:8
0.0007 117424 +3152 -> ret_ord($c = 'X') ../trace.php:10
0.0007 117584 +160 -> ord('X') ../trace.php:5
0.0009 117584 +0 -> ret_ord($c = 'd') ../trace.php:10
0.0009 117584 +0 -> ord('d') ../trace.php:5
0.0011 117584 +0 -> ret_ord($c = 'e') ../trace.php:10
0.0011 117584 +0 -> ord('e') ../trace.php:5
0.0013 117584 +0 -> ret_ord($c = 'b') ../trace.php:10
0.0013 117584 +0 -> ord('b') ../trace.php:5
0.0014 117584 +0 -> ret_ord($c = 'u') ../trace.php:10
0.0015 117584 +0 -> ord('u') ../trace.php:5
0.0016 117584 +0 -> ret_ord($c = 'g') ../trace.php:10
0.0017 117584 +0 -> ord('g') ../trace.php:5
0.0019 41152
TRACE END [2007-05-06 14:37:26]
"collect_return=1" displays the return value of the called function:
TRACE START [2007-05-06 14:37:35]
0.0003 114112 -> {main}() ../trace.php:0
0.0004 114272 -> str_split('Xdebug') ../trace.php:8
>=> array (0 => 'X', 1 => 'd', 2 => 'e', 3 => 'b', 4 => 'u', 5 => 'g')
0.0007 117424 -> ret_ord($c = 'X') ../trace.php:10
0.0007 117584 -> ord('X') ../trace.php:5
>=> 88
>=> 88
0.0009 117584 -> ret_ord($c = 'd') ../trace.php:10
0.0009 117584 -> ord('d') ../trace.php:5
>=> 100
>=> 100
0.0011 117584 -> ret_ord($c = 'e') ../trace.php:10
0.0011 117584 -> ord('e') ../trace.php:5
>=> 101
>=> 101
0.0013 117584 -> ret_ord($c = 'b') ../trace.php:10
0.0013 117584 -> ord('b') ../trace.php:5
>=> 98
>=> 98
0.0015 117584 -> ret_ord($c = 'u') ../trace.php:10
0.0016 117584 -> ord('u') ../trace.php:5
>=> 117
>=> 117
0.0017 117584 -> ret_ord($c = 'g') ../trace.php:10
0.0018 117584 -> ord('g') ../trace.php:5
>=> 103
>=> 103
>=> 1
0.0021 41152
TRACE END [2007-05-06 14:37:35]
"collect_assignments=1" displays variable assignment, see xdebug.collect_assignments setting for details.
The "xdebug.trace_format" setting changes the output format to make it easier to parse but harder to understand. This is most useful for parsing trace files with third-party tools.
Version: 2.0.0RC4-dev
TRACE START [2007-05-06 18:29:01]
1 0 0 0.010870 114112 {main} 1 ../trace.php 0
2 1 0 0.032009 114272 str_split 0 ../trace.php 8
2 1 1 0.032073 116632
2 2 0 0.033505 117424 ret_ord 1 ../trace.php 10
3 3 0 0.033531 117584 ord 0 ../trace.php 5
3 3 1 0.033551 117584
2 2 1 0.033567 117584
2 4 0 0.033718 117584 ret_ord 1 ../trace.php 10
3 5 0 0.033740 117584 ord 0 ../trace.php 5
3 5 1 0.033758 117584
2 4 1 0.033770 117584
2 6 0 0.033914 117584 ret_ord 1 ../trace.php 10
3 7 0 0.033936 117584 ord 0 ../trace.php 5
3 7 1 0.033953 117584
2 6 1 0.033965 117584
2 8 0 0.034108 117584 ret_ord 1 ../trace.php 10
3 9 0 0.034130 117584 ord 0 ../trace.php 5
3 9 1 0.034147 117584
2 8 1 0.034160 117584
2 10 0 0.034302 117584 ret_ord 1 ../trace.php 10
3 11 0 0.034325 117584 ord 0 ../trace.php 5
3 11 1 0.034342 117584
2 10 1 0.034354 117584
2 12 0 0.034497 117584 ret_ord 1 ../trace.php 10
3 13 0 0.034519 117584 ord 0 ../trace.php 5
3 13 1 0.034536 117584
2 12 1 0.034549 117584
1 0 1 0.034636 117584
TRACE END [2007-05-06 18:29:01]
Xdebug carries a VIM syntax file to highlight the syntax of the tracking file. The syntax file name is: xt.vim. In order for VIM to recognize the new format, you need to do this:
augroup filetypedetect
au BufNewFile,BufRead *.xt setf xt
augroup END
After doing this, the opened tracking file will look like this:
<strong>TRACE START</strong> <strong>[2007-05-15 20:06:02]</strong>
0.0003 115208 <strong>-></strong> <strong>{main}()</strong> ../trace.php<strong>:0</strong>
0.0004 115368 <strong>-></strong> <strong>str_split() </strong>../trace.php<strong>:8</strong>
0.0006 118520 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0007 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0008 118680 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0009 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0010 118680 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0010 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0012 118680 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0012 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0014 118680 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0014 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0016 118680 <strong>-></strong> <strong>ret_ord() </strong>../trace.php<strong>:10</strong>
0.0016 118680 <strong>-></strong> <strong>ord() </strong>../trace.php<strong>:5</strong>
0.0019 54880
<strong>TRACE END</strong> <strong>[2007-05-15 20:06:02]<br /></strong>
相关设置:
xdebug.auto_trace
类型: boolean, 默认值: 0
打开此设置时,脚本在运行前函数调用追踪将开启。这将可能追踪auto_prepend_file设置的代码文件。
xdebug.collect_assignments
类型: boolean, 默认值: 0, 始于 Xdebug > 2.1
用于控制xdebug是否添加变量赋予到函数追踪当中。
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_return
Type: boolean, Default value: 0
Control whether the return value of the function call is written to the trace file.
To achieve computerized trace files (xdebug.trace_format=1), Xdebug 2.3 or above must be used.
xdebug.show_mem_delta
Type: integer, Default value: 0
When this setting is not 0, the trace file that can be read manually will show the difference in the amount of memory occupied by each function call. If xdebug is set up to generate computer-readable trace files, they will always display information like this.
xdebug.trace_enable_trigger
Type: boolean, Default value: 0, since Xdebug > 2.2
When set to 1, you can use the XDEBUG_TRACE GET/POST parameter or set a cookie value named XDEBUG_TRACE to trigger the generation of a trace file. Tracking data files will be written to predefined folders. To prevent xdebug from generating a trace file on every request, you need to set xdebug.auto_trace to 0. The trigger itself can be accessed by setting xdebug.trace_enable_trigger_value.
xdebug.trace_enable_trigger_value
Type: string, Default value: "", since Xdebug > 2.3
This setting is used to limit who can take advantage of the XDEBUG_TRACE function described in xdebug.trace_enable_trigger. When changing from the default null value, the cookie, GET or POST value needs to match the share hidden setting in the generated settings for the trace file.
xdebug.trace_format
Type: integer, Default value: 0
The format of the tracking file.
|
Description |
||||||||
0 |
Show human-readable formatted file: time point, amount of memory, memory delta (if xdebug.show_mem_delta is turned on), level, function name, function parameters (if xdebug.collect_params is turned on), file name and line number. | ||||||||
1 |
Create a computer-readable format file using two different records. The difference between the records is that one inserts the stack border and the other removes the stack border. The following table lists the differences between each field in the record. | ||||||||
2 |
Use HTML to write tracking files. |
计算机化格式的栏位:
Record type
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 - ... |
||||||||||||||||||||||||||||||||||||||||||||||||||||
Entry |
level | function # | always '0' | time index | memory usage | function name | user-defined (1) or internal function (0) | name of the include/require file | filename | line number | no. of parameters | parameters (as many as specified in field 11) - tab separated | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Exit |
level | function # | always '1' | time index | memory usage |
empty |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return |
level | function # | always 'R' |
empty |
return value |
empty |
xdebug.trace_options
Type: integer, Default value: 0
When set to 1, the tracking file will add content later instead of overwriting it directly in subsequent requests.
xdebug.trace_output_dir
Type: string, Default value: /tmp
Trace the file writing path and ensure that the user has write permissions to the directory when running PHP.
xdebug.trace_output_name
Type: string, Default value: trace.%c
This setting determines the file name to which tracking information is written. This setting uses a format identifier, similar to sprintf() and strftime(). Several format identifiers can be used to format file names. The suffix '.xt' is automatically added to the file name.
Format identifier list:
|
Meaning |
Format example |
File Example |
||||||||||||||||||||||||||||||||||||||||||||||||
%c |
Crc32 validation value of the current working path | trace.%c | trace.1258863198.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%p |
Process Identifier | trace.%p | trace.5174.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%r |
Random number | trace.%r | trace.072db0.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%s |
Script name 2 | cachegrind.out.%s | cachegrind.out._home_httpd_html_test_xdebug_test_php | ||||||||||||||||||||||||||||||||||||||||||||||||
%t |
Time cutoff (seconds) | trace.%t | trace.1179434742.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%u |
Time cutoff (microseconds) | trace.%u | trace.1179434749_642382.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%H |
$_SERVER['HTTP_HOST'] | trace.%H | trace.kossu.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%R |
$_SERVER['REQUEST_URI'] | trace.%R | trace._test_xdebug_test_php_var=1_var2=2.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%U |
$_SERVER['UNIQUE_ID'] 3 | trace.%U | trace.TRX4n38AAAEAAB9gBFkAAAAB.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%S |
session_id (source $_COOKIE, if set) | trace.%S | trace.c70c1ec2375af58f74b390bbdd2a679d.xt | ||||||||||||||||||||||||||||||||||||||||||||||||
%% |
literal % | trace.%% | trace.%%.xt |
2 This is not available for tracking filenames.
3 New features in version 2.2. This feature is set by the Apache mod_unique_id module.
xdebug.var_display_max_children
Type: integer, default value: 128
This setting controls the display of the number of array elements and object properties when using xdebug_var_dump(), xdebug.show_local_vars or the trace function.
If it is not restricted, it can be set to -1 value.
This setting is not affected by Remot_Debuggin in any way.
xdebug.var_display_max_data
Type: integer, default value: 512
This setting controls the maximum string length displayed when using xdebug_var_dump(), xdebug.show_local_vars or the trace function.
If it is not restricted, it can be set to -1 value.
This setting is not affected by Remot_Debugging in any way.
xdebug.var_display_max_depth
Type: integer, default value: 3
This setting controls the display level of array elements and object properties when using xdebug_var_dump(), xdebug.show_local_vars or the trace function.
The maximum value is 1023, you can set it to -1 to indicate the maximum value.
This setting is not affected by Remot_Debugging in any way.
Related functions:
string xdebug_get_tracefile_name()
Returns the script file name of the current trace output. This function is available when xdebug.auto_trace is turned on.
void xdebug_start_trace( string trace_file [, integer options] )
Start a new function trace
At a certain point starts the function trace merged into the file specified by the parameter trace_file. If no file name is specified, the trace file is stored in the directory specified by the xdebug.trace_output_dir setting. Once a filename is specified in the first argument, the name is relative to the current working directory. However, the current working directory may be different from what you expect, so it is best to specify an absolute directory when specifying the file name. The PHP function getcwd() can point out the current working directory.
The trace file name is generally "{trace_file}.xt". If xdebug.auto_trace is turned on, then the "{filename}" part of the file name "{filename}.xt" is determined by setting xdebug.trace_output_name. The parameter options is a bit value, and there are three options:
XDEBUG_TRACE_APPEND (1)
Make trace files open in append mode instead of overwrite mode.
XDEBUG_TRACE_COMPUTERIZED (2)
Creates a trace file whose format is described by "xdebug.trace_format".
XDEBUG_TRACE_HTML (4)
Create tracking files in html table.
XDEBUG_TRACE_NAKED_FILENAME (8)
Generally speaking, Xdebug will add ".xt" to the end of the file name you specify as the first parameter. Using the XDEBUG_TRACE_NAKED_FILENAME option, ".xt" will not be added (new feature of Xdebug 2.3).
Unlike Xdebug 1, Xdebug 2 does not occupy memory during function calls. , but it will write to disk and relieve the pressure on memory usage. Settings such as xdebug.collect_includes, xdebug.collect_params and xdebug.collect_return will affect what kind of information is recorded in the trace file, while xdebug.trace_format affects the format of the file information.
void xdebug_stop_trace()
Stop tracing function calls and close tracing files.