PHP Debug Tracking Xdebug Use summary:
Xdebug is an open source PHP program debugging tool that you can use to debug, track, and analyze program running status. Of course, Xdebug need to combine PHP's editing tools to break point, tracking, debugging and analysis, compare common PHP Xdebug Debugging environment: Vim +xdebug.
· Installation configuration
· Debugging environment
· Tracking analysis
· Precautions
· Encounter problems
First, installation configuration
1. Installation
Xdebug is installed as a php extension, so you can refer to the PHP extension article:
http://blog.csdn.net/why_2012_gogo/article/details/51120645
2. Configuration
Ini:
[Xdebug]
; Basic Debug 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 above list is the most commonly used configuration options, as for other configuration options and the corresponding meaning, please refer to:
Https://xdebug.org/docs/all_settings#auto_trace
Second, the commissioning environment
Vim + Xdebug:
1. Download
http://www.vim.org/scripts/script.php?script_id=1929
2. Configuration
$ cd ~
$ sudo mkdir ~/.vim
Copy the files from the Xdebug plugin downloaded above to the. Vim:
$ sudo cp–r/php/ext/plugin.
Create the. vimrc file in the user's home directory:
$ sudo touch/usr/share/vim/vimrc ~/.VIMRC
$ sudo vim ~/.VIMRC
Add the following to. VIMRC:
Let G:debuggerport = 9010 (the port must be the same as Xdebug.remote_port)
Let G:debuggermaxdepth = 5 (represents array debug depth configuration)
Note:
VIMRC file is the main configuration file of Vim, it contains two versions: Global version and user version, we recommend to modify the user version of the VIMRC profile, the two versions of the path can be viewed in vim Normal mode (Mac System), as follows:
Global version Path view:
$ sudo vim
$: Echo $VIM
Path Address:/usr/share/vim
User Version Path view:
$ sudo vim
$: Echo $HOME
Path Address: $HOME (pwd ~)
Attention:
The port number of the G:debuggerport must be the same as the Xdebug.remote_port;
G:debuggermaxdepth represents the maximum depth level of script debugging;
Finally, after modifying the php.ini,. VIMRC configuration, remember to restart the PHP-FPM.
3. Commissioning
A. Prepare a PHP file
<?php
$value = ' Use Xdebug Debug program immediately, are you ready ';
echo $value;
?>
Put the above file into your Web root directory, my access address is:
The http://localhost/xdebug.php test is displayed properly.
B. Use vim to open PHP files
Use vim Normal mode to open the PHP file, move the mouse arrow to the line you want to debug, enter:
$:bp
As follows:
Then, press F5 (MAC:FN+F5) to start listening for debug events, and at the bottom of the edit window, prompt for 5 seconds to access the PHP file you want to debug, for example:
Http://localhost/xdebug.php? Xdebug_session_start=1
As follows:
For operations in debugging, attach the following:
Type |
Function |
Description |
<command mode> |
|
|
: Bp |
Toggle Breakpoint |
Breakpoint Markers |
: Up |
Stack up |
|
:D N |
Stack down |
|
<normal mode> |
|
|
E |
Eval |
|
<function keys> |
|
|
F1 |
Resize |
resizing windows |
F2 |
Step Into |
Commissioning Step Into |
F3 |
Step Over |
Debug Step into next marker |
F4 |
Step out |
Debug step Out Current Tag |
F5 |
Run |
Debug Run |
F6 |
Quit debugging |
Exit Debug mode |
F11 |
Get all context |
Get all variable contents |
F12 |
Get property of cursor |
Get the current cursor variable |
Third, follow-up analysis
1. Code Coverage Analysis
Xdebug 2.2 began to support the analysis of code coverage, that is, through an overlay analysis of the code, we can see which lines of code were executed during the IDE's visit, helping to provide targeted knowledge and analysis of core code and unit tests, and ultimately improve the quality of the code.
A, the configuration involved
Xdebug.coverage_enable=1
The configuration defaults to 1, which is enabled by default, and if set to 0, the code overlay analysis will not proceed.
B, the function involved
boolean xdebug_code_coverage_started ()
The function returns a Boolean value that is used to determine whether the code overwrite analysis function is turned on and returns false if it is not turned on.
void xdebug_start_code_coverage ( [int options] )
This function does not have any return, its function is to begin to collect the analysis result set data, the data is the two-dimensional array situation//existence, one-dimensional parameters for the analysis of the file name, two-dimensional parameters for the corresponding analysis of the number of rows; Additionally, in the analysis file
Each line of code will produce a result code, as follows:
1: The representative code has been executed;
-1: The representative code is not executed, the corresponding function parameter xdebug_cc_unused incoming;
-2: Represents no executable code exists, corresponding to Xdebug_cc_dead_code and xdebug_cc_unused
Note:
Xdebug_cc_unused: Used to calculate the analysis contains the collection of code not executed;
Xdebug_cc_dead_code: The line of code that is used to calculate the analysis is executed;
The form is as follows:
Xdebug_start_code_coverage (xdebug_cc_unused| Xdebug_cc_dead_code);
array xdebug_get_code_coverage ()
The function returns the array value used to gather and return the result set information for the Code coverage analysis.
void xdebug_stop_code_coverage ( [int cleanup=true] )
The function does not return any values to stop the overlay analysis, and if the incoming parameter is true, the parse result set will be stopped and emptied in memory, the false is passed in, or the memory information can be retrieved using//xdebug_start_code_coverage.
C, validation of the example
PHP Code:
<?php
echo ' Coverage analysis in Progress ...</br> ';
Building encapsulated Objects
Class Xdebugcoverageanalysismodel {
Private $_coverage_info;
Private $_status;
function __construct () {
$this->_coverage_info = Xdebug_get_code_coverage ();
$this->_status =xdebug_code_coverage_started ();
}
Get analysis Results
Public Functiongetcodecoverageresult () {
Returnjson_encode (Xdebug_get_code_coverage ());
}
Open Overlay Analysis
Public Functionxdebugstartcodecoverage () {
Xdebug_start_code_coverage (-1 |-2);
}
Analyze whether to perform
Public functionxdebugcodestarted () {
return xdebug_code_coverage_started ();
}
}
Initialization
$apiModel = new Xdebugcoverageanalysismodel ();
Echo ' Open overlay analysis ...</br> ';
$apiModel->xdebugstartcodecoverage ();
Define a test function
function coveragesample ($a, $b) {
echo ' function result: '. ($a * $b). ' </br> ';
}
Echo ' Judge whether to turn on ...</br> ';
$status = $apiModel->xdebugcodestarted ();
if ($status = = ' 1 ') {
Echo ' Open overlay analysis completed </br> ';
} else {
Echo ' Open overlay analysis failed </br> ';
}
Echo ' Test function turns on ...</br> ';
Coveragesample (10,10);
echo ' Get analysis results ...</br> ';
$result = $apiModel->getcodecoverageresult ();
echo $result. ' </br> ';
echo ' Off analysis switch ...</br> ';
Xdebug_stop_code_coverage ();
$status = $apiModel->xdebugcodestarted ();
if ($status = = ' 1 ') {
Echo ' coverage analysis has been completed </br> ';
} else {
Echo ' coverage analysis has been turned off! </br> ';
}
Unset ($result);
Unset ($apiModel);
?>
Browser results:
2. PHP Script Analysis
Xdebug's PHP scripting analysis function is useful, it can help us to analyze the bottleneck of the code and affect the slow performance of the problem, to optimize the code to provide a feasible reference.
A, the configuration involved
Xdebug.profiler_enable
The configuration defaults to 0, which is on, set to non-0, that is, the profiler function is turned on
Xdebug.profiler_output_dir
After the configuration is turned on, the location where the analysis file is generated is required to ensure that the location is writable and the default/tmp
Xdebug.profiler_enable_trigger
If this option is turned on, the performance report file will be generated if the Get/post or cookie contains the//xdebug_profile variable name in each request (provided that you must close
xdebug.profiler_enable option, otherwise this option does not work).
Xdebug.profiler_output_name
You can use this configuration to modify the generated analysis files by default cachegrind.out.%p
Note:
It is recommended to use Xdebug.profiler_enable_trigger instead of xdebug.profiler_enable.
B, the function involved
string xdebug_get_profiler_filename ()
The return type is a string used to return the parsed file name
C, validation of the example
When we turn on the analysis switch, when a script runs it generates an analysis file formatted as CACHEGRIND.OUT.XXX in the specified location:
The content of this file is not very intuitive, so you need to use visual tools to view and analyze, while Xdebug itself supports the use of third-party visual profiler files. Under Linux, you can use Kcachegrind, while on the Windows platform, you can use Qcachegrind, and of course there are some online enthusiasts-developed tools, such as: Webgrind, specifically how to use these tools, you can refer to:
Https://xdebug.org/docs/profiler
Below is a list of webgrind effects:
Webgrind can be downloaded here:
Https://github.com/jokkedk/webgrind
Iv. Matters of note
1, to avoid the production environment to open the profiler and trace, just open remote debugging;
2, try to use Xdebug.profiler_enable_trigger instead of xdebug.profiler_enable;
3, if using Webgrind Analysis Profiler, it is recommended not to put into production environment, because there is no security restrictions, anyone can access;
4, although the function of Xdebug is strong, but to balance the performance cost;
V. Encounter problems
Issue: Error ("Dbgprotocol instance has no attribute ' stop '",)
The problem is caused by the following:
A, configuration file configuration is not correct;
B,. VIMRC and php.ini are not the same port;
Port conflicts with existing ports in C,. VIMRC and PHP.ini;
Solve:
Check the configuration carefully against the above few.
Note:
There is blog said that because the URL is not added after the xdebug_session_start=1, it is not.
Technical Discussion Group: 489451956(new)
Summary of Xdebug Usage of PHP debug tracking