PHP Debugging tool Xdebug installation and use ____php

Source: Internet
Author: User
Tags error code memory usage php debugger php script readable

Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf (), and so on, although these are sufficient for programmers with richer development experience, they can often be used during program execution By outputting the value of a particular variable, you can tell whether the program is performing correctly, or even how efficient it can be (some time functions may be required, of course). So why do we need a special debugging program to monitor our program's operation?

In our usual PHP development, after a large project has been accumulating for a long time, you will find that performance is getting slower and worse, and where performance is consumed, often a vexing question, how many times function a () has been invoked, and how much time function B () consumes. How do we find out which worm is slowing down the speed of our program? Here to introduce a tool xdebug, I believe many people have heard, hope that with this tool we can play a simple analysis of PHP program performance bottlenecks. What's xdebug?

Xdebug is an open source PHP debugger (a debug tool) that you can use to track, debug, and analyze the state of your PHP program. Install xdebug access www.xdebug.org, download Php_xdebug.dll, download the appropriate version number with your operating system, PHP version. Place the downloaded Php_xdebug.dll under the PHP installation directory php\ext. Editing php.ini, some collection environments have their own xdebug configuration, and if not, manually join 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 parameter introduction: zend_extension load xdebug extension xdebug.auto_trace automatically open Open function call monitoring xdebug.auto_profile automatically turn on performance monitoring xdebug.trace_output _dir the path of the output file that sets the monitoring information for the function call. Xdebug.profiler_output_dir set the path of the output file of the performance monitoring information. Xdebug.collect_params opens the ability to collect "function parameters". The parameter values of the function call are included in the monitoring information of the Function procedure call. Xdebug.collect_return opens the function of collecting function return values. The return value of the function is included in the monitoring information of the Function procedure call. Restart Apache. Write a test.php, the content is <?php phpinfo (), if the output of the contents of the see Xdebug, the installation configuration is successful. Or go to the/home/ad/xdebug_log to see if the log has come out. Setting Options

Category Setting Description

Log

Xdebug.trace_output_dir

Log Trace Output Directory
Xdebug.trace_output_name Log file name, Xdebug provides a series of identifiers, generate the appropriate format file name, please refer to the official website
Xdebug.trace_options Record added to file mode: 1 = append (if the file exists). 0 (default) = Overwrite (if the file exists)
Display data Xdebug.collect_params Non-0 value = parameter display options for control function
0 = not shown. 1 = parameter type, value (for example: Array (9)). 2 = ditto 1, only slightly different in CLI mode 3 = ALL variables content 4 = All variable contents and variable names (for example: Array (0 => 9)).
Xdebug.collect_return 1 = Display function return value. Default 0 does not show
Xdebug.collect_vars 1 = displays which variables are used by the current scope, displays the variable name, which does not record the value of the variable and, if necessary, uses the Xdebug.collect_params
Xdebug.collect_assignments 1 = Add a row to display variable assignment (if 1, shape like $a = 1; This type of assignment expression will be shown in the trace file)
Format Xdebug.trace_format 0 = Human readable. From left to right each column represents: Point in time, memory, memory difference (need to set xdebug.show_mem_delta=1), level, function name, function parameters (need to set, Xdebug.collect_params=1, as long as is not 0), the current line of code is the file name, Line number. 1 = machine readable [1]. Requires Third-party apps, such as Xdebug trace file parser or Xdebug trace viewer 2 = HTML format table, open with browser, display table
Xdebug.show_mem_delta 1 = Show memory consumption per function call (poor memory)
Behavior Xdebug.auto_trace 1 = turn on automatic tracing. (There are 2 ways of tracking, one is automatic tracking, all PHP scripts run, will produce trace file; the other is trigger tracking, like the following)
XDEBUG.TRACE_ENABLE_TRIGGER[2]

1 = use xdebug_trace get/post to trigger tracing, or by setting the cookie xdebug_trace. To avoid each request, a trace trace file is generated and you need to set the Auto_trace to 0

Note: This feature is only available in the 2.2+ version to set

[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, when object properties are nested, display a few levels of element content. Default 3.
Xdebug.var_display_max_data How long the variable value is displayed as a string. Default 512.
Xdebug.var_display_max_children The number of arrays and object elements displayed. Default 128

Some custom functions
Function Description
void Xdebug_enable () Open manually, equivalent to Xdebug.default_enable=on
void Var_dump () Overwrite PHP provided var_dump, error, display function stack information, (if: php.ini html_errors is 1), use xdebug.overload_var_dump settings whether overwrite
void Xdebug_start_trace (
String Trace_file_path
[, integer options])
Manual control of code snippets that need to be tracked
Trace_file_path: File path (relative or absolute, if null). If NULL, or no arguments, use the directory set by Xdebug.trace_output_dir
Options:
xdebug_trace_append:1 = Append file content end, 0 = overwrite the file Xdebug_trace_computerized:2 = same as xdebug.trace_format=1. Xdebug_trace_html:4 = output HTML table, browser open as a table
void Xdebug_stop_trace () Stop tracing, code tracking stops at the line
String Xdebug_get_tracefile_name () Gets the output file name, which is used in conjunction with Xdebug.auto_trace.
void Xdebug_var_dump ([mixed var[,...]]) Output variable details, equivalent to PHP in the Var_dump, show the details please see here
Xdebug.show_local_vars The default is 0, not displayed; When PHP executes an error, it displays all the local variables in the scope of the error code (note: This produces a lot of information, so the default is closed), the difference is shown in the following figure [3]
Array Xdebug_get_declared_vars () Displays the declared variables in the current scope
Array Xdebug_get_code_coverage () Show which lines the code executes in a section of code [4]

About Xdebug.trace_format=1, if you use trigger mode to enable Code tracking: (xdebug.auto_trace = 0;xdebug.trace_enable_trigger = 1), then, You can add xdebug_trace to the URL, such as: localhost/test.php? Xdebug_trace, or localhost//test.php? Xdebug_trace=1 (any value).

Do not feel very troublesome, then install a plugin, let it to help you. Chrome XDEBUG Helper, use it, you can switch 3 states, disabled, debugging Enabled,profiling enabled (detailed in the next chapter), and then switch to debugging enabled. Run the script, (remove the URL from the Xdebug_trace), you can trace the code.

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 ();

Setting Xdebug.auto_trace = 1 Enables automatic tracing before executing all PHP scripts. In addition, you can set xdebug.auto_trace = 0 by 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 tracing before including the configured Auto_prepend_file.

Options Xdebug.trace_ouput_dir and Xdebug.trace_output_name are used to control where the 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 a. XT suffix.

By default, XDebug displays the time, memory usage, function name, and functional call depth fields. If you set the Xdebug.trace_format to 0, the output will conform to the human reading habit (set the argument to 1 for machine readable format). In addition, 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 view the type and value of the incoming 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.