preface
Xdebug is a PHP extension to assist with debugging and development.
- It contains a debugger for the IDE
- It’s an upgrade to PHP
var_dump()
function - It adds a stack trace for notifications, warnings, errors, and exceptions
- It has the ability to log every function call and disk variable assignment
- It contains a parser
- It provides code coverage for use with PHPUnit.
An essential tool for ape life. It is not recommended to use Xdebug in production because it is too heavy.
The installation
PECL install
pecl install xdebug
zend_extension="/usr/local/php/modules/xdebug.so"
Copy the code
Source package installation
Xdebug.org/download.ph… Find the corresponding package
wget xxx.gz
./configure
make && make install
zend_extension="/usr/local/php/modules/xdebug.so"
Copy the code
Docker installation
Here are some of the contents of the PHP dockerfile
RUN pecl install xdebug
RUN echo 'zend_extension=xdebug.so' >> /usr/local/etc/php/conf.d/xdebug.ini
Copy the code
The working principle of
IDE (such as PHPStorm) already includes a BGDP-compliant XDebug plug-in. When enabled, an XDebug service is opened locally, listening on the port set in the debugger (default is 9000), and this service listens for all links to 9000 ports.
When the browser sends a request to the server with the XDEBUG_SESSION_START parameter, the server accepts it and forwards it to PHP at the back end. If PHP has XDebug enabled, the debug message is forwarded to the IDE debug port at the client IP address.
If there is no XDEBUG_SESSION_START in the parameter or cookie information, debugging will not start. This opens up the possibility of adding switches to the browser later.
The basic configuration
In most cases, you just need to know, you don’t need to modify.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.default_enable | boolean | 1 | Stack tracing, enabled by default, is one of the basic features of XDebug |
xdebug.force_display_errors | integer | 0 | This is off by default, and if this setting is set to 1, errors will always be displayed no matter what PHP’s display_errors setting is. |
xdebug.force_error_reporting | integer | 0 | It is off by default, allowing certain errors to be forcibly displayed |
xdebug.halt_level | integer | 0 | Default off, set to receive certain specified errors |
xdebug.max_nesting_level | integer | 256 | Protection mechanism for controlling infinite recursion (infinite loop), default is 256 |
xdebug.max_stack_frames | integer | – 1 | Controls how many stack frames are displayed in the stack trace, on the command line of the PHP error stack trace, and in the display of the HTML trace in the browser. |
xdebug.scream | boolean | 0 | Off by default, if this is set to 1, Xdebug disables the @(close) operator so that notifications, warnings, and errors are no longer hidden. |
For details, please visit xdebug.org/docs/basic
Print the configuration
Xdebug replaces PHP’s var_dump() function to display variables. The Xdebug version includes different colors for different types and limits on the number of array elements/object attributes, maximum depth, and string length. There are other functions that also handle variable display.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.cli_color | integer | 0 | Whether to set the color of the result in CLI mode |
xdebug.overload_var_dump | boolean | 2 | Whether xdebug is allowed to override var_dump |
xdebug.var_display_max_children | integer | 128 | Var_dump limits the number of display layers for the children of array objects |
xdebug.var_display_max_data | integer | 512 | Var_dump limits the length of results |
xdebug.var_display_max_depth | integer | 3 | Var_dump limits the number of nested layers that can be displayed by default |
For details, please visit xdebug.org/docs/displa…
Stack trace configuration
When Xdebug is enabled, it displays a stack trace when PHP decides to display a notification, warning, error, etc. The information displayed by the stack trace and how it is displayed can be configured to suit your needs.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.cli_color | integer | 0 | Whether to set the color of the result in CLI mode |
xdebug.collect_includes | boolean | 1 | Controls whether Xdebug should write file names in include(), include_once(), require(), or require_once() to the trace file |
xdebug.collect_params | integer | 0 | This setting defaults to 0 and controls whether Xdebug should collect arguments passed to a function when a function trace or stack trace records a function call |
xdebug.collect_vars | boolean | 0 | This setting tells Xdebug which variables to use within a particular scope. Because Xdebug must reverse engineer PHP’s opcode arrays, this analysis can be quite slow. With xdebug.collect_params, this setting does not record the values of different variables. This setting needs to be enabled only if you want to use xdebug_get_declared_vars(). |
xdebug.dump.* | string | empty | * It can be any COOKIE, file, GET, POST, REQUEST, SERVER, SESSION. These seven Settings control what data from the superglobal is displayed in the event of an error. |
xdebug.dump_globals | boolean | 1 | When this setting is set to true, Xdebug adds the value * of the super global variable configured via xdebug.dump to the on-screen stack trace and error log. |
xdebug.dump_once | boolean | 1 | Controls whether the value of the super global should be dumped on all error cases (set to 0) or only on the first error case (set to 1) |
xdebug.dump_undefined | boolean | 0 | If you want to dump undefined values from super global variables, you should set this setting to 1, otherwise set it to 0. |
xdebug.file_link_format | string | File link format |
For details, please visit xdebug.org/docs/stack_…
Function debugging configuration
Xdebug allows you to log all function calls, including parameters and values returned in different formats.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.auto_trace | boolean | 0 | When this setting is set to true, tracing of function calls is enabled before the script runs |
xdebug.collect_assignments | boolean | 0 | This setting defaults to 0 and controls whether Xdebug should add variable assignments to the function trace. |
xdebug.collect_includes | boolean | 1 | This setting defaults to 1 and controls whether Xdebug should write file names in include(), include_once(), require(), or require_once() to the trace file. |
xdebug.collect_params | integer | 0 | This setting defaults to 0 and controls whether Xdebug should collect arguments passed to a function when a function trace or stack trace records a function call. |
xdebug.collect_return | boolean | 0 | This setting defaults to 0 and controls whether Xdebug should write the return value of the function call to the trace file. |
xdebug.show_mem_delta | integer | 0 | The trace file generated by Xdebug shows the difference in memory usage between function calls |
xdebug.trace_format | integer | 0 | Keep track of file formats |
xdebug.trace_options | integer | 0 | When set to “1”, trace files are appended to subsequent requests rather than overwritten. |
xdebug.trace_output_dir | string | /tmp | Write to the directory of the trace file, ensuring that the user running PHP has write permission to the directory. |
For details, please visit xdebug.org/docs/execut…
Garbage collection statistics
Xdebug’s built-in garbage collection statistics analyzer allows you to find out when the PHP internal garbage collector fires, how many variables it can clean up, how long it takes, and how much memory it actually frees.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.gc_stats_enable | bool | false | If this setting is enabled, the statistics for the garbage collection run are automatically collected to the given directory set with xdebug.gc_stats_output_DIR, using the automatically generated name configured by xdebug.gc_stats_output_name. |
xdebug.gc_stats_output_dir | string | /tmp | Write to the directory where garbage collection statistics are output, ensuring that the user who will be running PHP has write permission to that directory. You cannot set this setting in a script using ini_set(). |
xdebug.gc_stats_output_name | string | gcstats.%p | This setting determines the name of the file to dump garbage collection statistics to. This setting uses a format specifier to specify the format, much like sprintf() and strftime(). Several format specifiers can be used to format file names. |
For details, please visit xdebug.org/docs/garbag…
Remote debug configuration
Xdebug provides an interface for debugger clients that interact with running PHP scripts.
The name of the | type | The default value | annotations |
---|---|---|---|
xdebug.extended_info | integer | 1 | Controls whether Xdebug should enforce the ‘extended_info’ mode for the PHP parser; This allows Xdebug to execute file/line breakpoints using a remote debugger. You usually want to turn this option off when tracing or analyzing scripts, because opArray generated by PHP increases in size by about a third, slowing down the script. You cannot set this setting in a script using ini_set (), but only in php.ini. |
xdebug.idekey | string | complex | Controls which IDE Key Xdebug should be passed to the DBGp debugger handler. Is the key that communicates with the client |
xdebug.remote_addr_header | string | “” | This value will be used as a key in the $SERVER super global array to determine the header to use to find the IP address or host name to use for “connect back” |
xdebug.remote_autostart | boolean | 0 | You need to use specific HTTP GET/POST variables to start remote debugging |
xdebug.remote_connect_back | boolean | 0 | If you don’t set the IP address, it’s up to XDebug to find it, and XDebug will try to connect to the client that made the HTTP request. It checksAnd the $_SERVER [‘ HTTP_X_FORWARDED_FOR ‘]_SERVER[‘REMOTE_ADDR’] variable to find out which IP address to use |
xdebug.remote_cookie_expire_time | integer | 3600 | Set the cookie life cycle |
xdebug.remote_enable | boolean | 0 | Whether to enable remote debugging |
xdebug.remote_handler | string | dbgp | Debug the communication protocol |
xdebug.remote_host | string | localhost | Network address for debugging, current configuration invalid when remote_CONNECt_back is enabled |
xdebug.remote_log | string | Debug logs | |
xdebug.remote_port | integer | 9000 | Debug port |
xdebug.remote_timeout | integer | 200 | The wait time for debugging communication links |
For details, please visit xdebug.org/docs/remote
The official link
For full configuration, please go to xdebug.org/docs/all_se… For all functions, please go to xdebug.org/docs/all_fu…
PHPSTORM DEBUG
Configuration DEBUG articles on the network are too messy and jumpy, jumping around in PHPSTROM, which really makes me upset. This chapter reorganizes the description.
configuration
In fact, there is a very detailed tutorial in PHPSTROM, right? It’s just that most people go straight to Baidu /Google. It’s a good habit to search, but it depends.
Languages & Frameworks > PHP > Debug
The first step
Download the XDEBUG extension, which was covered at the beginning of this chapter and won’t be covered here. After the installation is complete, PHPSTROM provides validation scripts. Script address for gist.github.com/litzinger/8… Under normal circumstances, the detection is successful
See this and you can rest assured to debug.
The second step
Install a browser plug-in, and the user asks to listen. XDEBUG_SESSION_START=ID_KEY = XDEBUG_SESSION_START=ID_KEY = XDEBUG_SESSION_START=ID_KEY = XDEBUG_SESSION_START=ID_KEY
You can choose whether to enable debug or not. The following is a list of browser plugins
The browser | download |
---|---|
Chrome | [Xdebug Helper][1] |
Firefox | [The easiest Xdebug][2] or [Xdebug Helper][3] |
Safari | [Xdebug Toggler][4] |
Opera | [Xdebug launcher][5] |
Internet Explorer | [PhpStorm bookmarklets generator][6] |
The third step
Enable listening.
You can just click the Listen button on the picture.
At this point, the IDE is configured.
The fourth step
Create a DEBUG configuration file, configured for each project. You’re not gonna get away with this.
You see, PHPSTORM is very personal and gives you a simple tutorial at every step describing how to configure DEBUG.
Add a Server and IDE key(xdebug.idekey) to debug.
conclusion
The following steps to configure DEBUG for the IDE are actually quite simple.
- Download and Install Debug
- Verify that the communication is normal
- Add browser plug-in (can be omitted)
- Configuration run file
- Happy the debug
Note that if you do not need to access the local machine to debug some classes or code blocks, you only need to install XDEBUG on the local machine, omit all the above configuration, directly click the bug icon to debug. The default DEBUG port is 9000. If you change it in the configuration file, you need to change it in the IDE as well.
debugging
This section describes what the buttons on the PHPSTORM panel are used for during actual debugging. When you start listening, you will see the following image
Describe the function of each icon in x and y coordinates according to the position of the icon in the figure above.
icon | Position (x, y) | function |
---|---|---|
0, 0 | Re-run DEBUG (Rerun test.php) | |
0, 1 | Jump to the original debug file (Show Execution Point) | |
0, 2 | Step over | |
0, 3 | Go to the next Step (Step Info) | |
0, 4 | Jump into libraries, constructors, and other methods or classes (Force Step Info) | |
0, 5 | Execute a function body or class method, and Step out if it is in the outermost layer. | |
0, 6 | Jump to the next breakpoint of the current node (Run to Cursor) | |
0, 7 | Perform modified variables or return results for Evaluate arbitrary expression | |
0, 8 | Show Values Addresses | |
0, 9 | Whether to display null values. The default value is not displayed | |
0, 10 | Add method to Skip List | |
1, 0 | Restart DEBUG (Resume Program) | |
2, 0 | Stop the DEBUG (Step process) | |
3, 0 | View and Manage All breakpoints | |
4, 0 | Ignore all Breakpoints |
other
Docker PHP XDEBUG
Some people install the Docker on the host, the container to run this PHP, this time how to DEBUG? If you read this article carefully, you will find it very simple. XDEBUG itself is remote debugging. First you have to promise
- The container does port mapping to native 80
- The project directory in the container is mounted on disk
Put the container into PHP XDEBUG
xdebug.remote_host=local_ip
Copy the code
Don’t worry about the hosts file
Local IP = 127.0.0.1 = localhostCopy the code
Once configured, the container can be treated as if it did not exist, just like native debugging.
Thank you
Thank you for reading this. I hope this chapter helps you. Thank you.
communication
There is life, there is code.
Spread the positive energy of technology and continue to learn new knowledge.