PHP Debugging tool Xdebug Installation and use _php Tutorial

Source: Internet
Author: User
Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf (), and so on, although this is sufficient for programmers with more extensive development experience, they can often be in the process of program execution, By outputting the value of a particular variable, you can tell whether the program is executing correctly, or even how efficient it may be (some time functions, of course). So why do we need a dedicated debug program to monitor our program's operation?

In our usual PHP development, a large project after a long period of accumulation you will find that performance is getting slower, and the performance of what is consumed in what place, often is a headache, function a () calls the number of times, Function B () and how much time spent, How do we find out which moth is slowing down our program? Here to introduce a tool xdebug, I believe many people have heard, I hope that with this tool we can play a simple analysis of PHP program performance bottleneck problem.

What is Xdebug?
Xdebug is an open source PHP program debugger (a debug tool) that can be used to track, debug, and analyze the health of PHP programs.

Installing Xdebug
Download Php_xdebug.dll, according to the version number with your own operating system, PHP version download appropriate.
Place the downloaded Php_xdebug.dll in the PHP installation directory php\ext.
Edit PHP.ini, some of the collection environments have their own xdebug configuration, and if not, manually add the following lines yourself:
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 Loading Xdebug Extensions
Xdebug.auto_trace Automatic Open function call monitoring
Xdebug.auto_profile Auto-open performance monitoring
Xdebug.trace_output_dir sets the path of the output file for the function invocation monitoring information.
Xdebug.profiler_output_dir set the path of the performance monitoring information output file.
Xdebug.collect_params opens the ability to collect "function parameters". The parameter values of the function call are included in the monitoring information for the Function procedure call.
Xdebug.collect_return opens the ability to collect "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, content , if you see Xdebug in the output content, indicating that the installation configuration was successful. Or go to the/home/ad/xdebug_log to see if the logs are 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 of the file name, please refer to the official website
Xdebug.trace_options records are added to a file by: 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 displayed.
1 = parameter type, value (for example: Array (9)).
2 = Ibid. 1, only 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 = Displays the function return value. Default 0 does not display
Xdebug.collect_vars 1 = shows which variables are used in the current scope, displays the variable name, does not record the value of the variable, and, if necessary, uses Xdebug.collect_params
Xdebug.collect_assignments 1 = Add a row to display the variable assignment (if 1, the shape is $ A = 1; this type of assignment expression is displayed 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), rank, function name, function parameter (need to set, Xdebug.collect_params=1, as long as non-0), the current code line is located in the file name, Line number.
1 = machine readable [1]. Need to use third-party apps, such as: Xdebug trace file parser or Xdebug trace viewer
2 = HTML format is table, open with browser, show table

Xdebug.show_mem_delta 1 = Show memory consumption per function call (Memory difference)
Behavior Xdebug.auto_trace 1 = Turn on auto-tracking. (There are 2 ways of tracking, one is auto-tracking, all PHP scripts run, will produce trace files, the other is the trigger mode tracking, such as the following)
XDEBUG.TRACE_ENABLE_TRIGGER[2] 1 = trigger tracing using Xdebug_trace get/post, or by setting a cookie xdebug_trace. To avoid each request, the corresponding trace trace file is generated and you need to set the Auto_trace to 0

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

[Xdebug-general] Re:is Trace_enable_trigger defunct?

Restricts the display depth of xdebug.var_display_max_depth arrays and object elements: mainly used in arrays nesting, when object attributes are nested, showing levels of element content. Default 3.
How long the Xdebug.var_display_max_data variable is displayed when the value is 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, when an error occurs, displays the function stack information (provided that php.ini html_errors is 1), uses Xdebug.overload_var_dump to set whether to overwrite
void Xdebug_start_trace (
String Trace_file_path
[, integer options]) Manual control of code snippets that need to be traced
Trace_file_path: File path (relative or absolute, if empty). If empty, or do not pass the parameter, use the Xdebug.trace_output_dir set directory
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 trace stops at that line
String Xdebug_get_tracefile_name () obtains the output file name, which is used in conjunction with Xdebug.auto_trace.
void Xdebug_var_dump ([mixed var[,...]]) output variable details, equivalent to the var_dump in PHP, see here
Xdebug.show_local_vars defaults to 0, does not display, nonzero, displays all local variables in the scope of the error code when PHP performs an error (note: This produces a lot of information, so the default is closed), showing differences such as [3]
Array xdebug_get_declared_vars () displays declared variables in the current scope
Array xdebug_get_code_coverage () shows which lines the code executes within a piece of code [4]

About 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).

Do you feel very troublesome, then install a plugin, let it to help you. Chrome XDEBUG Helper, using it, you can switch 3 states, disabled, debugging Enabled,profiling enabled (in detail below), and then switch to debugging enabled. Run the script, (get rid of the URL?) 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 tracking before all PHP scripts are executed. In addition, you can set xdebug.auto_trace = 0 by code and enable and disable tracing using the Xdebug_start_trace () and Xdebug_stop_trace () functions, respectively. However, if Xdebug.auto_trace is 1, you can start the trace before including the configured Auto_prepend_file.

The options Xdebug.trace_ouput_dir and Xdebug.trace_output_name are used to control where the trace output is saved. Here, all the files are saved to/tmp/traces, and each trace file begins with Trace, followed by the name of the PHP script (%s) and the process ID (%p). All Xdebug trace files end with an. XT suffix.

By default, XDebug displays time, memory usage, function name, and call depth fields. If Xdebug.trace_format is set to 0, the output will conform to human reading habits (set the parameter to 1 in machine readable format). In addition, if you specify Xdebug.show_mem_delta = 1, you can see whether the 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.

http://www.bkjia.com/PHPjc/477546.html www.bkjia.com true http://www.bkjia.com/PHPjc/477546.html techarticle Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf (), and so on, although for programmers with more extensive development experience these are enough, they can often in the program ...

  • 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.