Xdebug结合IDE远程调试

1.安装Xdebug

这里有各个操作系统的安装说明
我以win7为例:
在这个页面文本框内输入phpinfo()内容(注意是view-source:不是网页)
Xdebug会为你的开发环境提供最好的建议来安装

2.php.ini设置

[xdebug]
;你下载的dll路径
zend_extension ="F:/php/php7.0.10/zend_ext/php_xdebug-2.4.1-7.0-vc14-x86_64.dll"
;这个一定要设置为on,否则无法调试
xdebug.remote_enable = on
;这个可以不用设置,默认为localhost
xdebug.remote_host=127.0.0.1
;这个一定要设置,默认为9000
xdebug.remote_port=9001
;下面是是否开启性能分析,可以不用开启
xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir ="F:/www/tmp"
xdebug.show_local_vars=0
;下面是ide秘钥,可以不用设置
;xdebug.idekey=money

3.IDE设置

配置好调试端口和上面的remote_port保持一致,其他的可以不用设置

4.调试使用

以我的IDE为例
可以按F7一步步的追踪代码位置(对于嵌套多层的很方便)以及对于的变量变化

5.Xdebug参数说明

利用xdebug检测项目性能

xdebug提供了一个叫profiler的功能,可以用来检测项目的性能,以便能够找出项目的瓶颈,使用该功能,首先需要了解一些配置参数:

xdebug.profiler_enable
boolean类型,默认值0。用于设定是否开启生成报告文件,如果设定为1,每次请求都会生成一个性能报告文件。

xdebug.profiler_enable_trigger
boolean类型,默认值0。如果开启该选项,则在每次请求中如果GET/POST或cookie中包含XDEBUG_PROFILE变量名,则才会生成性能报告文件(前提是必须关闭xdebug.profiler_enable选项,否则该选项不起作用)。

xdebug.profiler_output_dir
字符串类型。用于设定生成的报告文件的存放的路径。

xdebug.profiler_output_name
字符串类型。用于设定生成的报告文件的名字,如cachegrind.out.%t.%p
在linux下很好用
代码跟踪相关配置选项:
xdebug.auto_trace
boolean类型,默认值0。用于设定在脚本运行前是否自动跟踪方法的调用信息。

xdebug.collect_assignments

xdebug.collect_return
boolean类型,默认值0。用于设定是否返回调用方法的返回值。

xdebug.show_mem_delta
boolean类型,默认值0。如果启用该选项,并且输出格式为易于阅读式,则会在日志文件中显示调用两个方法之间的内存使用差异情况。

xdebug.trace_enable_trigger
boolean类型,默认值0。如果开启该选项,可以通过向POST或GET传递XDEBUG_TRACE参数来启用系统跟中功能,或者设定一个名为XDEBUG_TRACE的cookie信息,为了防止每次请求都会生成一个日志文件,需要将xdebug.auto_track设置为0。

xdebug.trace_format

xdebug.trace_options
boolean类型,默认值0。如果启用该选项,则之后的请求生成的日志信息会追加到日志文件中而不是替换当前日志文件中的内容。

xdebug.trace_output_dir
字符串类型,用于设定保存日志文件的路径信息

xdebug.trace_output_name
用于设定生成的日志文件的名字。

更多介绍点这里

常用配置

;xdebug.auto_trace = on
xdebug.trace_enable_trigger=on
;通过设置collect_params选项值为3开启参数记录
xdebug.collect_params=3
xdebug.collect_return=on
;以$_SERVER['REQUEST_URI']这个URI部分命名 trace.%R trace._test_xdebug_test_php_var=1_var2=2.xt
xdebug.trace_output_name="%R"
xdebug.trace_output_dir="F:/www/tmp"

测试代码

<?php
xx('a', 'b');
function xx($a, $b){
    xdebug_start_trace(); //开始记录回溯
    $x = array();
    array_push($x, $a);
    print(222);
    array_push($x, $b);
    xdebug_stop_trace(); //结束记录回溯
    yy();
    return $x;
}
如果你开启了xdebug.auto_trace其实相当于让PHP启动时就自动执行xdebug_start_trace函数,于是会报错说这个函数已经执行过了,所以为避免麻烦请不要开启auto_trace,在设置xdebug.trace_enable_trigger=on时候
这个调试更加精准,因为如果整个运行周期都回溯下来,起码有成千上万行,查找成为了艰难的事情
而函数触发时就缩小了你需要的范围,查找就快捷了很多,只用在你有问题的函数或者方法内加入start和stop就可以追踪问题

trace日志

TRACE START [2018-05-15 06:38:52]
    0.2040     357088       -> array_push(array (), 'a') F:\www\xdebug.php:9
    0.2040     357408        >=> 1
    0.2040     357520       -> array_push(array (0 => 'a'), 'b') F:\www\xdebug.php:11
    0.2040     357520        >=> 2
    0.2040     357520       -> xdebug_stop_trace() F:\www\xdebug.php:12
    0.2040     357584
TRACE END   [2018-05-15 06:38:52]

内容中每一行都显示了在哪个文件的哪一行执行了哪个函数
换成运行你自己的项目代码,相信就能看到成千上万行回溯代码了,包括调用了哪个对象的哪个方法也是这么看
这个回溯追踪的好处是你如果平时发现页面突然变空白了,怎么输出东西都看不见,于是只要通过这个回溯追踪调试,看看输出信息文件的最后几行调用的函数和文件行号你就大概知道出错位置在哪里了
最后提醒:它只记录有函数执行的地方,没有函数执行的地方是不会语录的,比如echo,include,isset等语句是不会记录的,于是你也能从中确认哪些是函数,哪些是语句了

6.原理图

A.With a static IP/single developer
With remote debugging, Xdebug embedded in PHP acts like the client, and the IDE as the server. The following animation shows how the communication channel is set-up:
这里写图片描述
The IP of the server is 10.0.1.2 with HTTP on port 80
The IDE is on IP 10.0.1.42, so xdebug.remote_host is set to 10.0.1.42
The IDE listens on port 9000, so xdebug.remote_port is set to 9000
The HTTP request is started on the machine running the IDE
Xdebug connects to 10.0.1.42:9000
Debugging runs, HTTP Response provided
B.With an unknown IP/multiple developers
If xdebug.remote_connect_back is used, the set-up is slightly different:
这里写图片描述
The IP of the server is 10.0.1.2 with HTTP on port 80
The IDE is on an unknown IP, so xdebug.remote_connect_back is set to 1
The IDE listens on port 9000, so xdebug.remote_port is set to 9000
The HTTP request is made, Xdebug detects the IP addres from the HTTP headers
Xdebug connects to the detected IP (10.0.1.42) on port 9000
Debugging runs, HTTP Response provided
更多介绍可以点这里

猜你喜欢

转载自blog.csdn.net/z15818264727/article/details/80368676