PHP debugging tracking: XDebug usage summary, php debugging tracking xdebug
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
PHP debugging tracking:
Xdebug is an open-source PHP program debugging tool that can be used to debug, track, and analyze program running status. Of course, Xdebug should be combined with PHP editing tools to break points, track, debug and analyze. The Xdebug debugging environment of PHP is commonly used: Vim + Xdebug.
· Installation Configuration
· Debugging environment
· Tracking and analysis
· Precautions
· Problems Encountered
I. installation and configuration
1. Installation
The installation of Xdebug exists as an extension of PHP, so you can refer to the PHP extension article:
Http://blog.csdn.net/why_2012_gogo/article/details/51120645
2. Configuration
Php. ini:
[Xdebug]
; Basic Debugging Configuration
Xdebug. auto_trace = on
Xdebug. collect_params = on
Xdebug. collect_return = on
Xdebug. profiler_enable = on
Xdebug. profiler_output_dir = "/php/ext/xdebug_profilers"
Xdebug. trace_output_dir = "/tmp/ext/xdebug_traces"
; Remote debugging settings
Xdebug. remote_enable = on
Xdebug. remote_host = localhost
Xdebug. remote_port = 9010
Xdebug. remote_autostart = on
Zend_extension =/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug. so
NOTE:
The most common configuration options are listed above. For other configuration options and their meanings, see:
Https://xdebug.org/docs/all_settings#auto_trace
Ii. debugging environment
Vim + Xdebug:
1. Download
Http://www.vim.org/scripts/script.php? Script_id = 1, 1929
2. Configuration
$ Cd ~
$ Sudo mkdir ~ /. Vim
Copy the xdebug plug-in file downloaded above to. vim:
$ Sudo cp-r/php/ext/plugin.
Create a. vimrc file in the user's home directory:
$ Sudo touch/usr/share/vim/vimrc ~ /. Vimrc
$ Sudo vim ~ /. Vimrc
Add the following content to. vimrc:
Let g: debuggerPort = 9010 (the port must be the same as xdebug. remote_port)
Let g: debuggerMaxDepth = 5 (representing the in-depth configuration of array debugging)
NOTE:
The vimrc file is the main configuration file of vim. It contains two versions: Global version and user version. We recommend that you modify the vimrc configuration file of the user version, the paths of these two versions can be viewed in vim normal mode, as shown below:
View the global version path:
$ Sudo vim
$: Echo $ VIM
Path:/usr/share/vim
View the user version path:
$ Sudo vim
$: Echo $ HOME
Path: $ HOME (pwd ~)
Note:
G: the port number of debuggerPort, which must be the same as xdebug. remote_port;
G: debuggerMaxDepth indicates the maximum depth of script debugging;
After modifying the php. ini and. vimrc configurations, restart php-fpm.
3. debugging
A. Prepare a php file.
<? Php
$ Value = 'use XDebug to debug the program immediately. Are you ready ';
Echo$ Value;
?>
Put the above file in your Web root directory. My access address is:
Http: // localhost/xdebug. php: Check whether the display is normal.
B. Use vim to open the PHP File
Open the PHP file in vim normal mode, move the cursor to the line to be debugged, and enter:
$: Bp
As follows:
Then, press F5 (Mac: Fn + F5) to start listening for debugging events. Then, at the bottom of the editing window, the system prompts you to access the PHP file to be debugged within five seconds. For example:
Http: // localhost/xdebug. php? XDEBUG_SESSION_START = 1
As follows:
Add the following commands to the debugging:
Type |
Function |
Description |
<Command Mode> |
|
|
: Bp |
Toggle breakpoint |
Breakpoint mark |
: Up |
Stack up |
|
: DN |
Stack down |
|
<Normal Mode> |
|
|
, E |
Eval |
|
<Function Keys> |
|
|
F1 |
Resize |
Adjust window size |
F2 |
Step |
Step-by-Step debugging |
F3 |
Step over |
Debug step into the next mark |
F4 |
Step out |
Debug the current tag |
F5 |
Run |
Debug and run |
F6 |
Quit debugging |
Exit debugging mode |
F11 |
Get all context |
Get all variable content |
F12 |
Get property of cursor |
Get current cursor variable |
Iii. tracking and analysis
1. code coverage analysis
Xdebug 2.2 began to support code coverage analysis, that is, through code coverage analysis, we can understand which lines of code are executed during IDE access, this helps you to understand and analyze the core code and unit test, and ultimately improve the quality of the Code.
A. Configuration involved
Xdebug. coverage_enable = 1
// This configuration is 1 by default, that is, it is enabled by default. If it is set to 0, code overwrite analysis will not be performed.
B. involved Functions
BooleanXdebug_code_coverage_started ()
// This function returns a Boolean value to determine whether the code overwrite analysis function is enabled. If the function is not enabled, false is returned.
VoidXdebug_start_code_coverage ([Int options])
// This function does not return any data. Its function is to collect and analyze the result set data. The data is based on a two-dimensional array. // The data exists. One-dimensional parameters are used as the name of the analysis file, two-dimensional parameters are the corresponding number of analysis rows. In addition, in the analysis File
// Each line of code will generate a result code, as shown below:
// 1: indicates that the code has been executed;
//-1: indicates that the Code is not executed, and the corresponding function parameter XDEBUG_CC_UNUSED is passed in;
//-2: indicates that no executable code exists, corresponding to XDEBUG_CC_DEAD_CODE and XDEBUG_CC_UNUSED
NOTE:
XDEBUG_CC_UNUSED: used to calculate and analyze unexecuted code;
XDEBUG_CC_DEAD_CODE: used to calculate whether a code line is executed in the analysis age;
The format is as follows:
Xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
ArrayXdebug_get_code_coverage ()
// This function returns an array value to collect and return code to overwrite the result set information of the analysis.
VoidXdebug_stop_code_coverage ([Int cleanup = true])
// This function does not return any value. It is used to stop overwriting analysis. If the input parameter is true, the analysis is stopped and the analysis result set in the memory is cleared. Otherwise, false is passed. Otherwise, you can also use // xdebug_start_code_coverage to retrieve the memory information.
C. Example Verification
Php code:
<? Php
Echo 'Overwrite the analysis... </br> ';
// Construct the encapsulated object
Class XdebugCoverageAnalysisModel {
Private $ _ coverage_info;
Private $ _ status;
Function _ construct (){
$ This-> _ coverage_info = xdebug_get_code_coverage ();
$ This-> _ status = xdebug_code_coverage_started ();
}
// Obtain the analysis result
Public functiongetCodeCoverageResult (){
Returnjson_encode (xdebug_get_code_coverage ());
}
// Enable coverage analysis
Public functionxdebustartcodecoverage (){
Xdebug_start_code_coverage (-1 |-2 );
}
// Whether the analysis is executed
Public functionxdebugCodeStarted (){
Return xdebug_code_coverage_started ();
}
}
// Initialization
$ ApiModel = new XdebugCoverageAnalysisModel ();
Echo 'Enable overwriting analysis... </br> ';
$ ApiModel-> xdebustartcodecoverage ();
// Define a test function
Function coverageSample ($ a, $ B ){
Echo 'function result: '. ($ a * $ B).' </br> ';
}
Echo 'determine whether to enable... </br> ';
$ Status = $ apiModel-> xdebugCodeStarted ();
If ($ status = '1 '){
Echo 'enabling overwriting analysis completed </br> ';
} Else {
Echo 'failed to enable overwriting analysis </br> ';
}
Echo 'test function enabling... </br> ';
CoverageSample (10, 10 );
Echo 'retrieve analysis results... </br> ';
$ Result = $ apiModel-> getCodeCoverageResult ();
Echo $ result. '</br> ';
Echo 'Turn off the analysis switch... </br> ';
Xdebug_stop_code_coverage ();
$ Status = $ apiModel-> xdebugCodeStarted ();
If ($ status = '1 '){
Echo 'Overwrite analysis completed </br> ';
} Else {
Echo 'Overwrite analysis disabled! </Br> ';
}
Unset ($ result );
Unset ($ apiModel );
?>
Browser result:
2. PHP script analysis
Xdebug's PHP script analysis function is more practical. It helps us analyze code bottlenecks and problems that affect slow performance, and provides feasible reference for code optimization.
A. Configuration involved
Xdebug. profiler_enable
// This configuration defaults to 0. If it is enabled, the profiler function is enabled if it is not set to 0.
Xdebug. profiler_output_dir
// This configuration is used to store the location of the generated analysis file after it is enabled. Ensure that the location can be written. The default value is/tmp.
Xdebug. profiler_enable_trigger
// If this option is enabled, a performance report file is generated only when the GET/POST or cookie contains the // XDEBUG_PROFILE variable name in each request.
// Xdebug. profiler_enable option; otherwise, this option does not work ).
Xdebug. profiler_output_name
// You can use this configuration to modify the generated analysis file. The default value isCachegrind. out. % p
NOTE:
RecommendedXdebug. profiler_enable_triggerSubstitutionXdebug. profiler_enable.
B. involved Functions
StringXdebug_get_profiler_filename ()
// The return type is a string used to return the name of the analysis File
C. Example Verification
When the analysis switch is enabled, an analysis file in the format of cachegrind. out. xxx is generated at the specified position when a script is run:
The content of this file is not intuitive, so you need to use a visual tool to view and analyze it. Xdebug itself supports the use of third-party visual profiler file content. In Linux, KCacheGrind can be used, while in Windows, QCacheGrind can be used. Of course, there are some online tools developed by enthusiasts, such as WebGrind. How to use these tools, refer:
Https://xdebug.org/docs/profiler
The following lists the effects of WebGrind:
WebGrind can be downloaded here:
Https://github.com/jokkedk/webgrind
Iv. Notes
1. Do not enable profiler and trace in the production environment. You only need to enable remote debugging;
2. Try to useXdebug. profiler_enable_triggerReplace xdebug. profiler_enable;
3. If you use webgrind to analyze profiler, we recommend that you do not add it to the production environment because it has no security restrictions and can be accessed by anyone;
4. Although Xdebug is powerful, it balances performance overhead;
5. Problems Encountered
Problem: Error ("DbgProtocol instance has no attribute 'stop '",)
The cause of the problem is roughly as follows:
A. the configuration file is incorrectly configured;
B. the ports in. vimrc and php. ini are different;
C. The port in vimrc and php. ini conflicts with the existing port;
Solution:
Check the configuration items carefully.
NOTE:
Some blogs say that XDEBUG_SESSION_START = 1 is not added after the URL.