Installation and Use of XDebug, a powerful PHP debugging tool -- a brief introduction to XDebug

Source: Internet
Author: User
Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. Although this is sufficient for programmers with rich development experience, during program execution, they can output the values of specific variables to determine whether the program is correctly executed, even the efficiency can be seen (of course, some time functions may need to be used ). So why do we need a special adjustment?

Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. Although this is sufficient for programmers with rich development experience, during program execution, they can output the values of specific variables to determine whether the program is correctly executed, even the efficiency can be seen (of course, some time functions may need to be used ). So why do we need a special debugging program to monitor our program running?

In our daily php development, after a large project has been accumulated for a long time, you will find that the performance is getting slower and slower, and what is the performance consumption, this is often a headache. how many times does function a () call, and how much time does function B () consume, how can we find out which worm slows down the running speed? Here we will introduce you to xdebug, a tool that many people have heard of. we hope to use this tool to analyze the performance bottleneck of php programs.

What is XDebug?

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

Install XDebug
    1. Access? Www.xdebug.org ?, Download php_xdebug.dll. download php_xdebug.dll based on the version number and your operating system and PHP version.
    2. Put the downloaded php_xdebug.dll to the PHP installation directory php \ ext (Click here for installation in linux ).
    3. Edit php. ini. some collection environments already have xdebug configurations. if not, manually add the following lines:
[xdebug]zend_extension = "/home/ad/php/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"xdebug.auto_trace = onxdebug.auto_profile = onxdebug.collect_params = onxdebug.collect_return = onxdebug.profiler_enable = onxdebug.trace_output_dir = "/home/ad/xdebug_log"xdebug.profiler_output_dir = "/home/ad/xdebug_log"

XDebug parameters:

    • Zend_extension load xdebug extension
    • Xdebug. auto_trace automatically opens function call monitoring
    • Xdebug. auto_profile automatically enables performance monitoring
    • Xdebug. trace_output_dir: specifies the path of the output file of the function call monitoring information.
    • Xdebug. profiler_output_dir: set the path of the output file of performance monitoring information.
    • Xdebug. collect_params enables the function of collecting "function parameters. Include the parameter values of function calls in the monitoring information of function process calls.
    • Xdebug. collect_return enables the function to collect "function return values. Include the return value of a function in the monitoring information of function call.
  1. Restart Apache.
  2. Write a test. php file with the content: If xdebug is displayed in the output, the installation and configuration are successful. Or go to/home/ad/xdebug_log to check if the log has been generated.
Set Options
Category Setting Description
 

Logs

Xdebug. trace_output_dir Log tracing output Directory
Xdebug. trace_output_name Log file name. xdebug provides a series of identifiers to generate file names in the corresponding format. for details, refer to the official website.
Xdebug. trace_options Method of adding a record to a file: 1 = append (if the file exists). 0 (default) = overwrite (if the file exists)
Show data Xdebug. collect_params Non-zero value = parameter display option for function control

  • 0 = not displayed.
  • 1 = parameter type, value? (For example, array (9 )).
  • 2 = same as 1, but slightly different in CLI mode
  • 3 = all variable content
  • 4 = all variable content and variable names (for example, array (0 => 9 )).
Xdebug. collect_return 1 = show function return value. Default 0 not show
Xdebug. collect_vars 1 = display the variables used in the current scope and the variable name. This option does not record the value of the variable. if necessary, use xdebug. collect_params
Xdebug. collect_assignments 1 = add a value for the display variable (for example, $ a = 1; this type of Assignment Expression will be displayed in the trace file)
Format Xdebug. trace_format
  • 0 = readable. each column from left to right indicates the time point, memory, and memory difference (xdebug needs to be set. show_mem_delta = 1), level, function name, function parameter (required, xdebug. collect_params = 1, as long as it is non-zero), the file name of the current code line, row number.
  • 1 = Machine readable [1]. third-party apps are required, such as xdebug trace file parser? Or? Xdebug trace viewer
  • 2 = table in html format. open it in browser and display the table
Xdebug. show_mem_delta 1 = display memory consumption for each function call (memory difference)
Action Xdebug. auto_trace 1 = enable automatic tracing. (there are two tracing methods, one is automatic tracing, all php scripts will generate trace files when running, and the other is trigger tracing, as shown below)
Xdebug. trace_enable_trigger [2] 1 = Use XDEBUG_TRACE GET/POST to trigger tracing, or set cookie XDEBUG_TRACE. to avoid generating corresponding trace Tracing files every time a request is made, you need to set auto_trace to 0

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

? [Xdebug-general] Re: Is trace_enable_trigger defunct?

Restrictions Xdebug. var_display_max_depth Array and object element display depth: mainly used in array nesting. When object attributes are nested, several levels of element content are displayed. Default 3.
Xdebug. var_display_max_data How long the variable value is displayed when it is a string. Default 512.
Xdebug. var_display_max_children Array and object element display count. Default 128
Some custom functions
Function Description
Void xdebug_enable () Manually open, equivalent to xdebug. default_enable = on
Void var_dump () Overwrite 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 data.
Void xdebug_start_trace (
String trace_file_path
[, Integer options])
Manually control the code segment to be tracked
Trace_file_path: the file path (relative or absolute, if empty). if it is empty or no parameter is passed, use the directory set by xdebug. trace_output_dir
Options:

  • XDEBUG_TRACE_APPEND: 1 = End of the append object content, 0 = overwrite the object
  • XDEBUG_TRACE_COMPUTERIZED:
    • 2 = same as xdebug. trace_format = 1.
  • XDEBUG_TRACE_HTML: 4 = output HTML table. open it as a table in the browser.
Void xdebug_stop_trace () Stop Tracing. code tracing stops at this row.
String xdebug_get_tracefile_name () Get the output file name, and? Xdebug. auto_trace used in combination.
Void xdebug_var_dump ([mixed var [,...]) ? The output variable details are equivalent to var_dump in php. for details, see here.
Xdebug. show_local_vars ? The default value is 0, not displayed. When an error occurs during php execution, all local variables in the scope of the error code are displayed (note: This will generate a large amount of information, so the default value is closed ), specific display differences are shown in [3]
Array xdebug_get_declared_vars () Display the declared variables in the current scope
Array xdebug_get_code_coverage () Display the lines in a piece of code [4]

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

Is it very troublesome to install a plug-in to help you. Chrome XDEBUG Helper, you can switch to three states: disabled, debugging enabled, and profiling enabled (details in the next article), and switch to debugging enabled. Run the script (remove? XDEBUG_TRACE) to trace the code.

You can use xdebug_start_trace () and xdebug_stop_trace () to manually track your code execution.

xdebug_start_trace(); //your code required to tracexdebug_stop_trace();

Set xdebug. auto_trace = 1 to enable automatic tracing before executing all PHP scripts. In addition, you can set xdebug. auto_trace to 0 in the code, and use the xdebug_start_trace () and xdebug_stop_trace () functions to enable and disable tracing respectively. However, if xdebug. auto_trace is 1, you can start the trail before including the configured auto_prepend_file.

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

By default, XDebug? The time, memory usage, function name, and function call depth fields are displayed. If xdebug. trace_format is set to 0, the output conforms to human reading habits (set the parameter to 1 to machine readable format ). In addition, if xdebug. show_mem_delta = 1 is specified, you can check whether the memory usage is increasing or decreasing. if xdebug. collect_params = 4 is specified, you can view the type and value of the input parameter. To monitor the value returned by each function, set xdebug. collect_return = 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.