Detailed installation and configuration of php code debugging tool Xdebug in Windows and Linux

  • 2021-06-28 08:58:06
  • OfStack

1. Why do you need Debugger?

Many PHP programmers debug using echo, print_r(), var_dump(), printf(), and so on, are actually enough for programmers with more experience in development. They can often judge whether the program is executed correctly or not by outputting the values of specific variables during the execution of the program, or even the efficiency (of course, they may need to use a time function).So why do we need a special debugger to monitor our programs?The answer to this question may be left to the later stage.

2. What is Xdebug?

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

3. XDebug Installation and Configuration under Windows

1. Download the XDebug2 binary: http://www.xdebug.org/download.php
Please select a download based on the php version, for example:

5.2 http://www.xdebug.org/files/php_xdebug-2.1.2-5.2-vc6.dll
5.3 http://www.xdebug.org/files/php_xdebug-2.1.2-5.3-vc6.dll

2. Find and open the php.ini file
3. If you have configured ZendOptimizer, you need to shield the ZendOptimizer-related configuration first, usually as follows:
[Zend]
zend_extension_manager.optimizer_ts= " path\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0 ″ 
zend_extension_ts= " path\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll " 

Delete it or comment it out with a semicolon, such as:
;[Zend]
;zend_extension_manager.optimizer_ts= " path\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0 ″ 
;zend_extension_ts= " path\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll " 

4. Join the XDebug configuration.References are as follows:
[Xdebug]
zend_extension_ts= " path/xdebug/php_xdebug-2.1.2-5.2-vc6.dll " 
xdebug.auto_trace=on
xdebug.trace_output_dir= " path\xdebug " 
xdebug.profiler_enable=on
xdebug.profiler_output_dir= " path\xdebug " 
xdebug.collect_params=on
xdebug.collect_return=on
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000

Explain:
The above "path" location needs to be modified to your own local path.
Explanation of parameters:


zend_extension_ts= " c:/webserver/php5/ext/php_xdebug.dll " 
; Load xdebug Modular.Not available here extension=php_xdebug.dll Loading must be done in zend Otherwise, after installation, phpinfo Is not visible xdebug This item. 
xdebug.auto_trace=on;
; Automatically turn on the power mode of Monitoring Function Call Procedures.This function can output monitoring information for function calls as a file in the directory you specify.The default value for this configuration item is off . 
xdebug.collect_params=on;
; Turn on the ability to collect Function Parameters.Include the parameter values of function calls in the monitoring information for function procedure calls.The default value for this configuration item is off . 
xdebug.collect_return=on
; Turn on the ability to collect Function Return Values.Include the return value of a function in the monitoring information for function procedure calls.The default value for this configuration item is off . 
xdebug.trace_output_dir=
; Sets the path to the output file of the function call monitoring information. 
xdebug.profiler_enable=on
; Turn on the performance monitor. 
xdebug.profiler_output_dir=
; Set the path to the output file of the performance monitoring information. 

There are also some more specific parameter settings, as detailed in: http://www.xdebug.org/docs-settings.php
5. Restart the web server, such as Apache or IIS
6. Check the output of phpinfo and if you see the option of XDebug, the configuration is successful.
7. Debugging information file view.
Running the php program locally will generate a file of debugging information in the directory you set up, mainly including:
a.Function call process monitoring information file, file name format: trace.××××××.xt.This file can be viewed directly and contains information such as when a function runs, the parameter values of a function call, the return values, the file and location where it is located.The content format is relatively intuitive.
b.Performance monitoring file, file name format: cachegrind.out.××××××××。
The file can also be viewed directly, but the format of the information is not easy to understand. We can install the wincachegrind software to read it formatted.The download and installation methods are as follows:
Download: http://sourceforge.net/projects/wincachegrind/
Install and run after downloading, then click Tools- > options, set working folder (php.ini xdebug.profiler_output_Value of dir)
This allows you to view the performance monitoring file information more intuitively.

4. Installation and configuration of XDebug under linux

Source code compilation and installation can be downloaded under linux using the following method references.
1. Download the source code source:http://www.xdebug.org/download.php corresponding to php version
For example, xdebug-2.1.2.tgz version: http://www.xdebug.org/files/xdebug-2.1.2.tgz
2. Compile and Install

tar -xvzf xdebug-2.1.2.tgz
cd xdebug-2.1.2
./configure
make
make install

If there is an error phpize does not have this command, install it:
sudo apt-get install php5-dev

3. Move the xdebug.so file below php5
cp modules/xdebug.so /usr/lib/php5/

4. Edit php.ini and add the following lines:
[Xdebug]
zend_extension= /usr/lib/php5/xdebug.so
xdebug.profiler_enable=on
xdebug.trace_output_dir= " ../xdebug " 
xdebug.profiler_output_dir= " ../xdebug " 

5. Restart Apache to test for successful installation

If you see xdebug in the output, the installation is successfully configured.


Related articles: