Installation and Use of xdebug, a PHP debugging tool

Source: Internet
Author: User
Tags set cookie

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. Visit www.xdebug.org to download php_xdebug.dll. Download php_xdebug.dll based on the version and your operating system and PHP version.
  2. Put the downloaded php_xdebug.dll to the PHP installation Directory PHP \ Ext.
  3. Edit PHP. ini. Some collection environments already have xdebug configurations. 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"

    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.
  4. Restart Apache.
  5. Write a test. php file with the content <? PHP phpinfo () ;?>, 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 () Obtain the output file name, used with xdebug. auto_trace.
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.

1 xdebug_start_trace();
2 //your code required to trace
3 xdebug_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 displays the time, memory usage, function name, and function call depth fields. 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 is specified
= 4, you can view the type and value of the input parameter. To monitor the value returned by each function, set xdebug. collect_return = 1.

From: http://www.nowamagic.net/librarys/veda/detail/2333

Related Article

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.