Home > Backend Development > PHP Tutorial > Installation and use of PHP debugging tool XDebug_PHP tutorial

Installation and use of PHP debugging tool XDebug_PHP tutorial

WBOY
Release: 2016-07-14 10:10:09
Original
964 people have browsed it

Many PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging. Although these are enough for programmers with rich development experience, they can often perform debugging during program execution. In , by outputting the value of a specific variable, you can determine whether the program is executed correctly, and even the efficiency can be seen (of course, you may also need to use some time functions). So why do we need a special debugger to monitor the operation of our program?

In our usual PHP development, after a long period of accumulation of a large project, you will find that the performance is getting slower and slower, and where the performance is consumed is often a headache. Function a () has been called many times, and how much time has been consumed by function b(). How do we find out which bug is slowing down the running speed of our program? Here I would like to introduce to you a tool called xdebug. I believe many people have heard of it. I hope that with this tool we can simply analyze the performance bottleneck of PHP programs.

What is XDebug
XDebug is an open source PHP program debugger (i.e. a Debug tool) that can be used to track, debug and analyze the running status of PHP programs.

Install XDebug
Download php_xdebug.dll and download the one that is suitable for your operating system and PHP version according to the version number.
Place the downloaded php_xdebug.dll in the PHP installation directory phpext.
Edit php.ini. Some collection environments have their own xdebug configuration. If not, manually add the following lines:
1 [xdebug]

2 zend_extension = "/home/ad/php/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"

3 xdebug.auto_trace = on

4 xdebug.auto_profile = on

5 xdebug.collect_params = on

6 xdebug.collect_return = on

7 xdebug.profiler_enable = on

8 xdebug.trace_output_dir = "/home/ad/xdebug_log"

9 xdebug.profiler_output_dir = "/home/ad/xdebug_log"

Introduction to XDebug parameters:

zend_extension loads xdebug extension
xdebug.auto_trace automatically turns on function call monitoring
xdebug.auto_profile automatically turns on performance monitoring
xdebug.trace_output_dir sets the path to the output file of function call monitoring information.
xdebug.profiler_output_dir sets the path to the performance monitoring information output file.
xdebug.collect_params turns on the function of collecting "function parameters". Include the parameter values ​​of the function call in the monitoring information of the function procedure call.
xdebug.collect_return turns on the function of collecting "function return values". Include the return value of the function in the monitoring information of the function procedure call.
Restart Apache.
Write a test.php with the content of . If xdebug is seen in the output content, the installation and configuration are successful. Or go to /home/ad/xdebug_log to see if the log has come out.
Setting options
Category Setting Description

Log
xdebug.trace_output_dir
Log tracking output directory
xdebug.trace_output_name Log file name. xdebug provides a series of identifiers to generate file names in corresponding formats. For details, please refer to the official website
xdebug.trace_options How to add records to the file: 1 = Append (if the file exists). 0 (default) = Overwrite (if the file exists)
Display data xdebug.collect_params non-zero value = control function’s parameter display options

0 = Do not display.
1 = parameter type, value (for example: array(9)).
2 = Same as 1, but slightly different in CLI mode
3 = All variable contents
4 = All variable contents and variable names (for example: array(0 => 9)).

xdebug.collect_return 1 = Display function return value. Default 0 Do not display
xdebug.collect_vars 1 = Show which variables are used in the current scope and display the variable name. This option will not record the value of the variable. If necessary, use xdebug.collect_params
xdebug.collect_assignments 1 = Add a line to display variable assignments (if it is 1, it will be in the form $a = 1; this type of Assignment Expression will be displayed in the trace file)
Format xdebug.trace_format 0 = human readable. From left to right, each column represents: time point, memory, memory difference (needs to set xdebug.show_mem_delta=1), level, function name, function parameters (needs to set, xdebug.collect_params =1, as long as it is non-zero), the file name of the current code line, and the line number.
1 = machine readable[1]. Need to use third-party app, such as: xdebug trace file parser or xdebug trace viewer
2 = html format, that is, table, open with browser, display table

xdebug.show_mem_delta 1 = Show memory consumption (memory difference) of each function call
Behavior xdebug.auto_trace 1 = Turn on automatic tracing. (There are two tracing methods, one is automatic tracing, all php scripts will generate trace files when they run; the other is triggered tracing, as follows)
xdebug.trace_enable_trigger[2] 1 = Use XDEBUG_TRACE GET/POST to trigger tracing, or set the cookie XDEBUG_TRACE. In order to avoid generating the corresponding trace file for each request, you need to set auto_trace to 0

Note: This feature can only be set in version 2.2+

[xdebug-general] Re: Is trace_enable_trigger defunct?

Limit xdebug.var_display_max_depth array and object element display depth: mainly used in array nesting and object attribute nesting to display several levels of element content. Default 3.
xdebug.var_display_max_data How long to display when the variable value is a string. Default 512.
xdebug.var_display_max_children The number of array and object elements displayed. Default 128


Some custom functions
Function Description
void xdebug_enable() manually open, equivalent to xdebug.default_enable=on
void var_dump() overwrites the var_dump provided by PHP. When an error occurs, the function stack information is displayed (prerequisite: html_errors in php.ini is 1). Use xdebug.overload_var_dump to set whether to overwrite
void xdebug_start_trace(
string trace_file_path
[, integer options] ) Manually control the code segments that need to be traced
trace_file_path: file path (relative or absolute, if empty). If it is empty, or no parameters are passed, use the directory set by xdebug.trace_output_dir
options:

XDEBUG_TRACE_APPEND: 1 = append to the end of the file content, 0 = overwrite the file
XDEBUG_TRACE_COMPUTERIZED:
2 =Same as xdebug.trace_format=1 .
XDEBUG_TRACE_HTML: 4 = Output HTML table, the browser opens it as a table

void xdebug_stop_trace() stops tracing, code tracing stops at this line
string xdebug_get_tracefile_name() gets the output file name, used with xdebug.auto_trace.
void xdebug_var_dump([mixed var[,...]]) Output variable details, equivalent to var_dump in php, please see here for specific display
xdebug.show_local_vars defaults to 0 and is not displayed; when it is non-zero, when an error occurs in PHP execution, all local variables in the scope of the error code are displayed (Note: This will generate a lot of information, so the default is closed). The specific display difference is as follows [ 3]
array xdebug_get_declared_vars() displays the declared variables in the current scope
array xdebug_get_code_coverage() displays which lines of code are executed in a certain section of code [4]

Regarding xdebug.trace_format=1, if you use trigger mode to enable code tracing: (xdebug.auto_trace = 0;xdebug.trace_enable_trigger = 1), then you can add XDEBUG_TRACE to the URL, for example: localhost/test.php ?XDEBUG_TRACE, or localhost//test.php?XDEBUG_TRACE=1 (any value).

If you find it troublesome, then install a plug-in and let it help you. Chrome XDEBUG Helper, using it, you can switch to 3 states, disabled, debugging enabled, profiling enabled (detailed introduction in the next article), and then switch to debugging enabled. Run the script (remove ?XDEBUG_TRACE in the URL), and you can track the code.

Use xdebug_start_trace() and xdebug_stop_trace() to manually trace the execution of your code.

1 xdebug_start_trace();

2 //your code required to trace

3 xdebug_stop_trace();

Setting xdebug.auto_trace = 1 will enable automatic tracing before executing all PHP scripts. Alternatively, you can set xdebug.auto_trace = 0 through code and enable and disable tracing using the xdebug_start_trace() and xdebug_stop_trace() functions respectively. However, if xdebug.auto_trace is 1, tracing can be started before including the configured auto_prepend_file.

Options xdebug.trace_ouput_dir and xdebug.trace_output_name control where trace output is saved. Here, all files are saved to /tmp/traces, and each trace file starts with trace, followed by the name of the PHP script (%s) and the process ID (%p). All Xdebug trace files end with the .xt suffix.

By default, XDebug will display the time, memory usage, function name, and function call depth fields. If xdebug.trace_format is set to 0, the output will be human-readable (setting the parameter to 1 will be in a machine-readable format). Additionally, if you specify xdebug.show_mem_delta = 1, you can see whether memory usage is increasing or decreasing, and if you specify xdebug.collect_params = 4, you can see the types and values ​​of the parameters passed in. To monitor the value returned by each function, set xdebug.collect_return = 1.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477546.htmlTechArticleMany PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging. This is enough for programmers with rich development experience. They can often program...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template