Xdebug Usage Summary of PHP debug trace:
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 interrupt points, tracking, debugging and analysis, compared to the common PHP Xdebug Debugging environment: Vim +xdebug.
· Installation configuration
· Debugging environment
· Tracking analysis
· Attention matters
· Encounter problems
First, installation configuration
1, installation
The installation of Xdebug is an extension of PHP, so you can refer to the PHP expansion 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:
Listed above are 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, debugging 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. 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 (This port must be the same as Xdebug.remote_port)
Let G:debuggermaxdepth = 5 (represents array debug depth configuration)
Note:
VIMRC file is the primary configuration of vim, it contains two versions: Global version and user version, we recommend to modify the user version of the VIMRC configuration file, the two versions of the path can be viewed in vim Normal mode, 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:
G:debuggerport the port number 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, debugging
A, prepare a PHP file
<?php
$value = ' Immediately use Xdebug debugger, are you ready? '
echo $value;
?>
Put the above file in your Web root directory, my address is:
http://localhost/xdebug.php whether the test is displayed properly.
B, use vim to open the PHP file
Use vim Normal mode to open the PHP file, move the mouse arrow to the line you want to debug, enter:
$:bp
Screenshot below:
Then, press F5 (MAC:FN+F5) and start listening for debugging events, and then at the bottom of the edit window you will be prompted for 5 seconds to access the PHP file you want to debug, for example:
Http://localhost/xdebug.php? Xdebug_session_start=1
Screenshot below:
For the actions 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 |
Debug Step Into |
F3 |
Step Over |
Debug Step Into next tag |
F4 |
Step out |
Debug step out of current tag |
F5 |
Run |
Debug Run |
F6 |
Quit debugging |
Exit Debug mode |
F11 |
Get all context |
Get all variable content |
h1| |
Get property of cursor |
Get current cursor variable |
Third, tracking analysis
1, Code Coverage analysis
Xdebug 2.2 begins to support analysis of code coverage, which means that by analyzing the code coverage, we can see which lines of code are executed during the IDE's visit, helping to understand and analyze core code and unit tests, and ultimately improve the quality of the code.
A, the configuration involved
xdebug.coverage_enable=1
This configuration defaults to 1, also is the default to open, if set to 0, code coverage analysis will not proceed.
B, the functions 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 or false when 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 in the two-dimensional array situation//existence, the one-dimensional parameter is the file name of the analysis, the two-dimensional parameter is the corresponding analytic row number; In addition, in the analysis file
Each line of code produces a result code, as follows:
1: The representative code has been executed;
-1: Represents the code has not been 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 compute the analysis contains the collection of code that was not executed;
Xdebug_cc_dead_code: is used to calculate whether the line of code is executed when parsing;
The form is as follows:
Xdebug_start_code_coverage (xdebug_cc_unused| Xdebug_cc_dead_code);
array xdebug_get_code_coverage ()
The function returns an array value that is used to collect 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 value to stop profiling, and if the passed parameter is true, it stops parsing and empties the analysis result set in memory, or if it is passed in false, otherwise, the memory information can be retrieved by using//xdebug_start_code_coverage.
C, examples of validation
PHP Code:
<?php
Echo ' coverage analysis conducted in ...</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 Coverage Analysis
Public Functionxdebugstartcodecoverage () {
Xdebug_start_code_coverage (-1 |-2);
}
Analyze whether to perform
Public functionxdebugcodestarted () {
return xdebug_code_coverage_started ();
}
}
Class
$apiModel = new Xdebugcoverageanalysismodel ();
Echo ' Open coverage Analysis ...</br> ';
$apiModel->xdebugstartcodecoverage ();
Define a test function
function coveragesample ($a, $b) {
echo ' function result: '. ($a * $b). ' </br> ';
}
Echo ' Judge whether to open ...</br> ';
$status = $apiModel->xdebugcodestarted ();
if ($status = = ' 1 ') {
Echo ' Open coverage analysis completed </br> ';
} else {
Echo ' Open coverage analysis failed </br> ';
}
echo ' Test function opens ...</br> ';
Coveragesample (10,10);
echo ' Get analysis result ...</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 closed. </br> ';
}
Unset ($result);
Unset ($apiModel);
?>
Browser results:
2, PHP script analysis
Xdebug PHP Script Analysis is practical, it can help us analyze the bottleneck of the code and slow down the problem of performance, to optimize the code to provide a feasibility reference.
A, the configuration involved
xdebug.profiler_enable
//This configuration defaults to 0, to open, set to not 0 after, that is, open Profiler function
Xdebug.profiler_output_dir
After the configuration is turned on, the location where the generated analysis files are stored is guaranteed to be writable, the default/tmp
Xdebug.profiler_enable_trigger
//If this option is turned on, the performance report file is generated on each request if the Get/post or cookie contains the//xdebug_profile variable name (provided that you must close
//xdebug.profiler_enable option, otherwise this option will not work.
Xdebug.profiler_output_name
//You can use this configuration to modify the generated profiling file, the default cachegrind.out.%p
Note:
It is recommended to use Xdebug.profiler_enable_trigger instead of xdebug.profiler_enable.
B, the functions involved
string Xdebug_get_profiler_filename ()
//return type is a string used to return the parsed file name
C, examples of validation
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 intuitive, so you need to use visual tools to view and analyze, and xdebug itself supports the use of Third-party Visual Profiler file content. Under Linux, you can use Kcachegrind, while on the Windows platform, you can use Qcachegrind, and of course there are some online tools developed by enthusiasts, such as: Webgrind, how to use these tools, you can refer to:
Https://xdebug.org/docs/profiler
The following list, Webgrind effect:
Webgrind can be downloaded here:
Https://github.com/jokkedk/webgrind
Iv. Matters of note
1, to avoid the production environment to open Profiler and trace, just open remote debugging;
2, try to use Xdebug.profiler_enable_trigger instead of xdebug.profiler_enable;
3, if the use of Webgrind Analysis Profiler, it is recommended not to put into production environment, because it has no security restrictions, anyone can access;
4, Xdebug function, although powerful, but to balance the performance overhead;
V. Problems encountered
Question: Error ("Dbgprotocol instance has no attribute ' stop '",)
The cause of the problem is generally as follows:
A, the configuration file configuration is not correct;
The port in B,. VIMRC and PHP.ini is not the same;
The port in the C,. VIMRC and php.ini conflicts with the existing port;
Solve:
Check the configuration carefully against the above few.
Note:
There is a blog that because the URL is not added after the xdebug_session_start=1, it is not.
Technical Discussion Group: 489451956 (New)