Installation and use of Xdebug


foreword

Software debugging generally refers to the process of reproducing software defects, locating and finding the root cause of the problem, and finally solving the problem. 编写的程序不可能一直不出错,所以调试很重要Debugging usually has the following two different definitions:

  • Definition 1:
软件调试是为了发现并排除软件程序中 的错误,可以通过某种方法控制被调试程序的执行过 程,以便随时查看和修改被调试程序执行状态的方法。 在该定义中,软件测试属于软件调试的一部分,与 牛津词典中的调试定义类似。 在牛津词典中调试定义 为:“识别和排除计算机硬件或软件中错误的过程。”
  • Definition 2:
调试是执行一次成功的测试之后所要进 行的工作。 所谓成功的测试,是指它可以证明程序没 有实现预期的功能。 调试包含两个步骤,从执行了一个成功测试用例,发现问题后开始;第一步,确定程序 中可疑错误的准确性质和位置;第二步,修改错误。 在该定义中软件测试从调试工作中分离出来。

1. How to configure Xdebug

Download XDebug Download address: http://www.xdebug.org/ You must download the version that matches the php installed on the machine. The specific download method is as follows: copy the source code of the phpinfo web page to http://www.xdebug.org/find-binary.php, and then follow the instructions to install it.

  1. Download Xdebug, download the Xdebug version corresponding to PHP
    insert image description here
  2. Put the downloaded file into the specified directory
    insert image description here
  3. Modify the php.ini configuration file (add the following code at the bottom of the php.ini file)
[XDebug] 
xdebug.default_enable=1
; 如下三条选项是远程调试配置
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.mode=debug
xdebug.remote_handler=dbgp
xdebug.remote_host="www.loophole.com"  
xdebug.remote_port=9003
xdebug.extended_info=1
xdebug.remote_autostart=1
zend_extension="D:\phpstudy_pro\Extensions\php\php7.3.9nts\ext\php_xdebug-3.1.6-7.3-vc15-nts-x86_64.dll"
xdebug.idekey="PHPSTORM"

There are two modified positions as above: xdebug.remote_host="www.loophole.com" , www.loophole.comis the domain name information, which can be any domain name, as follows:

insert image description here

zend_extension="D:\phpstudy_pro\Extensions\php\php7.3.9nts\ext\php_xdebug-3.1.6-7.3-vc15-nts-x86_64.dll", D:\phpstudy_pro\Extensions\php\php7.3.9nts\ext\php_xdebug-3.1.6-7.3-vc15-nts-x86_64.dllis where your Xdebug file is placed

Other Xdebug configurations are as follows:

[Xdebug]
;指定Xdebug扩展文件的绝对路径
zend_extension="d:/wamp/bin/php/php5.5.12/zend_ext/php_xdebug-2.2.5-5.5-vc11.dll"
;启用性能检测分析
xdebug.profiler_enable=on
;启用代码自动跟踪
xdebug.auto_trace=on
;允许收集传递给函数的参数变量
xdebug.collect_params=on
;允许收集函数调用的返回值
xdebug.collect_return=on
;指定堆栈跟踪文件的存放目录
xdebug.trace_output_dir="D:/debug"
;指定性能分析文件的存放目录
xdebug.profiler_output_dir="D:/debug"
xdebug.profiler_output_name = cachegrind.out.%t.%p
备注:在上述配置内容中,zend_extension用于指定Xdebug扩展模块的文件路径(由于Xdebug是基于Zend引擎的扩展,因此必须使用zend_extension配置指令,而不是extension。zend_extension指令的配置路径必须是绝对路径)。之后的6个配置语句是Xdebug的配置参数。
此时,我们重启服务器,通过访问含有phpinfo()函数的PHP文件,即可看到Xdebug模块的相关信息。


3、配置参数选项附录
在上面的Xdebug配置中,我们只配置了6个常用的配置选项。当然,Xdebug还支持更多的配置参数,详情见下表。
配置参数选项

  参数值类型与默认值 参数选项描述
xdebug.auto_trace boolean类型,默认值=0 是否在脚本运行之前自动调用相关追踪函数。
xdebug.cli_color integer类型,默认值=0 该参数自2.2版本开始引入。如果值=1,当处于CLI模式或连接虚拟控制台时,Xdebug将高亮显示var_dumps()和堆栈输出,;在Windows中,这需要安装ANSICON工具。如果值=2,不管是否处于CLI模式或连接虚拟控制台,Xdebug都会高亮显示var_dumps()或堆栈输出;这种情况下,你可能会看到转义后的代码。
xdebug.collect_assignments boolean类型,默认值=0 该参数自2.1版本开始引入。用于控制是否为函数跟踪添加变量赋值功能。
xdebug.collect_includes boolean类型,默认值=1 控制是否在跟踪文件中写入include()、include_once()、require()、require_once()等函数中用到的文件名。
xdebug.collect_params integer类型,默认值=0
控制在调用函数时,是否收集传递给函数的参数信息。如果参数值过大,这可能会占用大量的内存;不过,在Xdebug 2中不会出现该问题,因为Xdebug 2将相关数据写入磁盘中,而不是占用内存。

如果值=0,则不显示任何信息。
如果值=1,只显示类型和大小信息,例如:string(6)、array(8)。
如果值=2,将显示类型和大小,以及全部信息的工具提示。
如果值=3,将显示变量的全部内容。
如果值=4,将显示变量的全部内容和变量名。

xdebug.collect_return boolean类型,默认值=0 控制是否在追踪文件中写入函数调用的返回值。
xdebug.collect_vars boolean类型,默认值=0 控制是否收集指定作用域中的变量信息。由于需要反向工程PHP的操作码数组,因此Xdebug的分析速度可能比较慢。
xdebug.coverage_enable boolean类型,默认值=1 该参数自2.2版本开始引入。控制是否允许通过设置内部结构来启用代码覆盖率功能。
xdebug.default_enable boolean类型,默认值=1 当发生异常或错误时,是否默认显示堆栈信息。
xdebug.dump.* string类型,默认值=Empty 这里的*可以是COOKIE, FILES, GET, POST, REQUEST, SERVER, SESSION中的任意一个。用于指定发生错误时是否显示超全局变量数组中的索引变量信息。比如,你想要显示请求的IP地址和请求方式,可以设置为
xdebug.dump.SERVER=REMOTE_ADD,REQUEST_METHOD
多个索引变量用英文逗号隔开,如果要输出其中的所有变量,可以直接用*,例如:
xdebug.dump.GET=*
xdebug.dump_globals boolean类型,默认值=1 控制是否显示通过xdebug.dump.*定义的所有超全局变量的信息。
xdebug.dump_once boolean类型,默认值=1 如果出现多个错误,控制超全局变量信息是在所有错误中显示,还是只在第一个错误中显示。
xdebug.dump_undefined boolean类型,默认值=1 控制是否显示超全局变量中未定义的值。
xdebug.extended_info integer类型,默认值=1 是否强制进入PHP解析器的"extended_info"模式,这将允许Xdebug以远程调试器对文件或行添加断点。开启此模式将拖慢脚本的允许速度,该参数只能在php.ini中设置。
xdebug.file_link_format string类型,默认值=, 自2.2版本开始引入。用于指定堆栈信息中用到的文件名称的链接样式,这允许IDE通过设置链接协议,直接点击堆栈信息中的文件名称,即可快速打开指定的文件。例如:ZendStudio://%f@%l(%f表示文件路径,%f表示行号)。
xdebug.force_display_errors integer类型,默认值=0 自2.3版本开始引入。是否强制显示错误信息。
xdebug.force_error_reporting integer类型,默认值=0 自2.3版本开始引入。是否强制显示所有错误级别的信息。
xdebug.halt_level integer类型,默认值=0 自2.3版本开始引入。指定出现那些错误级别的错误时,中止程序运行。例如:xdebug.halt_level=E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE(也仅支持上述4种错误级别)。
xdebug.idekey string类型,默认值=*complex* 指定传递给DBGp调试器处理程序的IDE Key。
xdebug.manual_url string类型,默认值=http://www.php.net 仅2.2.1以下版本可用,用于指定从函数堆栈和错误信息链接到的帮助手册的基本URL。
xdebug.max_nesting_level integer类型,默认值=100 指定递归的嵌套层级数。
xdebug.overload_var_dump boolean类型,默认值=1 自2.2版本开始引入,当php.ini中的html_error设为1时,Xdebug是否默认使用自身的改进版本来重载var_dump()。
xdebug.profiler_append integer类型,默认值=0 当多个请求映射到相同文件时,指定是覆盖之前的调试信息文件还是追加内容到该文件中。
xdebug.profiler_enable integer类型,默认值=0 指定是否启用Xdebug的性能分析,并创建性能信息文件。
xdebug.profiler_output_dir string类型,默认值=/tmp 指定性能分析信息文件的输出目录
xdebug.profiler_output_name string类型,默认值=cachegrind.out.%p 指定性能分析信息文件的名称
xdebug.remote_enable boolean类型,默认值=0 是否开启远程调试
xdebug.remote_handler string类型,默认值=dbgp 指定远程调试的处理协议
xdebug.remote_host string类型,默认值=localhost 指定远程调试的主机名
xdebug.remote_log string类型,默认值= 指定远程调试的日志文件名
xdebug.remote_mode string类型,默认值=req 可以设为req或jit,req表示脚本一开始运行就连接远程客户端,jit表示脚本出错时才连接远程客户端。
xdebug.remote_port integer类型,默认值=9000 指定远程调试的端口号
xdebug.trace_options integer类型,默认值=0 指定对于之后的请求,追踪文件是追加内容还是覆盖之前内容。
xdebug.trace_output_dir string类型,默认值=/tmp 指定追踪文件的存放目录
xdebug.trace_output_name string类型,默认值=trace.%c 指定追踪文件的名称

Two, PHPstrom configuration

  1. First open the settings option
    insert image description here
  2. add server

insert image description here
3. DBGP proxy settings
insert image description here
4. Edit configuration
insert image description here
insert image description here

Three, the use of Xdebug

1. Panel function explanation

insert image description here

2. Detailed explanation of debugging function

insert image description here

Each function is as follows:

  1. Show execution point: If you are looking at other lines or other pages, click this button to jump to the place where the current code is executed
  2. Step by step: execute the code line by line offline 如果这一行里面有调用其他的方法的话,不会进入该方法里面,只会将该方法对应的值取到. If there is a loop, it will be executed multiple times.
  3. Step into: 如果当前行执行了call其他方法,可以进入该方法的内部, a group of methods used to enter custom methods that will not perform core methods,和步过有细微的区别,注意区分
  4. Forced step-in: If the current line executes a call to other methods, you can enter the interior of the method, 查看底层源码的时候可以用这个进入.
  5. Step out: Exit the method, from the entered method 退出到方法的调用出, the method is executed at this time, and the assignment is completed, and the execution goes to the next line.
  6. Run to the current mouse click position: you can 将鼠标点击到你需要的查看的哪一行,然后使用这个功能,代码就会运行到那一行, and there is no need to set a breakpoint
  7. Calculation expression
  8. continue program execution, say there are two breakpoints at lines 2 and 5, while 当前运行至2行,按一下,则运行到下一个断点(即5行)
  9. Pause the program: force stop the program while the program is running
  10. Stop: Press twice to close the program. Sometimes you will find that the port is occupied when the program is started. This is because the debugger has not been completely closed.
  11. View breakpoints: View all breakpoint information, as follows:
    insert image description here
  12. Enable breakpoints:将所有的断点变为灰色并使它们失效,按继续执行程序可以直接运行完调试程序,再次点击这个按钮可以是所有无效的断点变为有效

Four, Xdebug principle

  1. first of all PhpStorm中已经集成了一个遵循 BGDp 的 Xdebug 插件. When debugging, start this plugin. 该插件会启动一个 9000 的端口监听远程服务器发过来的 debug 信息. (Listen to the port to see if it is Debug)
  2. When debugging, the browser sends a request with the XDEBUG_SESSION_START parameter to the Httpd server, and the server sends the request to the backend PHP (with the xdebug module enabled) for processing after receiving the request.
  3. The request received by Php has the XDEBUG_SESSION_START parameter. At this time, Xdebug will send a debug request to the source ip client's port 9000 (port 9000 by default), and then the client's port 9000 will respond to the request, and then the debug will start.
  4. Php knows that Xdebug is ready, so it starts to execute the code line by line, but each line will be filtered by Xdebug. When Xdebug filters each line of code, it will suspend the execution of the code, and then send it to the client's port 9000 The execution status of this line of code, waiting for the decision of the client (whether it is a code or the next breakpoint)
  5. Correspondingly, the client (IDE) can display the information to the developer after receiving the execution status sent by Xdebug, including the values ​​of some variables. Also send Xdebug what should happen next.

Guess you like

Origin blog.csdn.net/qq_53568983/article/details/128652184