简体   繁体   English

使用 Xdebug 进行远程调试会导致 PHP 脚本执行停止

[英]Remote debugging with Xdebug causes PHP script execution to halt

I am trying to debug remote PHP scripts using xdebug .我正在尝试使用xdebug调试远程 PHP 脚本。 When xdebug is enabled for a website, if xdebug is able to make a tcp connection, the PHP script freezes as though waiting for a breakpoint to be executed.当为网站启用xdebug ,如果xdebug能够建立tcp连接,则 PHP 脚本会冻结,就像在等待执行断点一样。 The JavaScript portion of the webpage is displayed correctly, just waiting on the PHP code to return data required to complete the page.网页的 JavaScript 部分显示正确,只是等待 PHP 代码返回完成页面所需的数据。

This has worked in the past.这在过去有效。 I can't for the life of me figure out why.我终其一生都无法弄清楚原因。

If xdebug is unable to make establish a TCP session with the remote host, all works as expected.如果xdebug无法与远程主机建立 TCP 会话,则一切正常。

I am using Angular 10 as my development environment and PhpStorm as my IDE.我使用 Angular 10 作为我的开发环境,使用 PhpStorm 作为我的 IDE。

Environment:环境:

Debian bullseye (testing)
Apache2.4
PHP 7.4.11
mpm_prefork
xdebug.so 2.9.8

I have also tried xdebug.so v2.8.x and v3.0.0 (Beta) to no effect.我也试过xdebug.so v2.8.x and v3.0.0 (Beta)没有效果。

Current /etc/php/7.4/mods-available/xdebug.ini :当前/etc/php/7.4/mods-available/xdebug.ini

#set xdebug flags/values
zend_extension=xdebug.so    

xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_connect_back=0
xdebug.remote_host=MyClientIP    
xdebug.remote_port=9099
xdebug.idekey=PHPSTORM
    
#xdebug logger
xdebug.remote_log = /var/log/xdebug/xdebug.log

While I am using PhpStorm to develop my application, this issue it is not related to my IDE.当我使用 PhpStorm 开发我的应用程序时,这个问题与我的 IDE 无关。 To illustrate this I downloaded dbgbClient.exe from xdebug.org.为了说明这一点,我下载dbgbClient.exe从xdebug.org。

When using the script a session is created between the client and server.使用脚本时,会在客户端和服务器之间创建会话。

Client output:客户端输出:

dbgpClient.exe  -p 9099 -f
Xdebug Simple DBGp client (0.4.1)
Copyright 2019-2020 by Derick Rethans

Waiting for debug server to connect on port 9099.
Connect from 10.130.189.1:39144
DBGp/1.0: Xdebug 2.9.8 — For PHP 7.4.11
Debugging file:///home/init/DEV/MT4/php-services/ClassAPI.php (ID: 70831/PHPSTORM)
1 | feature_get
1 | supports_async: supported > 0

xdebug.log from server:来自服务器的 xdebug.log:

[70831] Log opened at 2020-10-14 20:59:24
[70831] I: Connecting to configured address/port: myClientIP:9099.
[70830] Log opened at 2020-10-14 20:59:24
[70830] I: Connecting to configured address/port: MyClientIP:9099.
[70831] I: Connected to client. :-)
[70831] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///home/init/DEV/MT4/php-services/ClassAPI.php" language="PHP" xdebug:language_version="7.4.11" protocol_version="1.0" appid="70831" idekey="PHPSTORM"><engine version="2.9.8"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>

[70830] I: Connected to client. :-)
[70830] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///home/init/DEV/MT4/php-services/ClassAPI.php" language="PHP" xdebug:language_version="7.4.11" protocol_version="1.0" appid="70830" idekey="PHPSTORM"><engine version="2.9.8"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>

At this point the beginnings of the web page is created.在这一点上,创建了网页的开头。

If I issue a run command and an extra carriage return or two, the debug session continues and the screen finishes.如果我发出一个run命令和一个或两个额外的回车,调试会话将继续并且屏幕结束。

Client example:客户端示例:

(cmd) run
2 | run > stopping/ok

(cmd)
Error while handling connection: Error reading length: EOF
Disconnect
Connect from 10.130.189.1:39146
DBGp/1.0: Xdebug 2.9.8 — For PHP 7.4.11
Debugging file:///home/init/DEV/MT4/php-services/ClassAPI.php (ID: 70830/PHPSTORM)
1 | feature_get
1 | supports_async: supported > 0

(cmd) run
2 | run > stopping/ok

(cmd)
Error while handling connection: Error reading length: EOF
Disconnect

xdebug log for the above:上面的 xdebug 日志:

[70831] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_get" transaction_id="1" feature_name="supports_async" supported="1"><![CDATA[0]]></response>

[70831] <- run -i 2
[70831] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="2" status="stopping" reason="ok"></response>

[70831] <- run -i 3
[70831] Log closed at 2020-10-14 21:00:32

[70830] <- feature_get -i 1 -n supports_async
[70830] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_get" transaction_id="1" feature_name="supports_async" supported="1"><![CDATA[0]]></response>

[70830] <- run -i 2
[70830] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="2" status="stopping" reason="ok"></response>

[70830] <- run -i 3
[70830] Log closed at 2020-10-14 21:00:41

[70829] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="4" status="stopping" reason="ok"></response>

[70829] Log closed at 2020-10-14 21:13:17

[70832] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="4" status="stopping" reason="ok"></response>

[70832] Log closed at 2020-10-14 21:13:38

Thank you for reading this far.感谢您阅读到这里。 I'm sure someone out there has a solution.我敢肯定有人在那里有解决方案。

This is the relevant part:这是相关部分:

Error while handling connection: Error reading length: EOF Disconnect处理连接时出错:错误读取长度: EOF断开

xdebug is not meant to connect to remote (live) sites, to begin with. xdebug并不意味着连接到远程(实时)站点。

And even if stunnel is working, it might still fail to connect back for some reason ...eg.即使stunnel正在工作,它也可能由于某种原因仍然无法连接回来……例如。

xdebug.remote_host=EXTERNAL_ROUTER_IP
xdebug.remote_connect_back=1

And the port-forward :9099 to the internal MyClientIP in the router.并将端口转发:9099到路由器中的内部MyClientIP

Well, it turns out there is something between my development PC connected to my companies network via VPN.好吧,事实证明,我的开发 PC 通过 VPN 连接到我的公司网络之间存在某些问题。 I all worked at one time.我都在一次工作。 Thanks to IT tightening the security screws ( firewall rules ).感谢 IT 拧紧安全螺丝( firewall rules )。

Although my PhpStorm would see the PHPSTORM cookie and establish a TCP session on the correct port, the protocol would not continue to completion.尽管我的 PhpStorm 会看到PHPSTORM cookie 并在正确的端口上建立 TCP 会话,但该协议不会继续完成。 I'm guessing there is some type if smart firewall on the network.我猜网络上有某种类型的智能防火墙。

The secret was to establish a ssh tunnel between my PC and the linux server like so:秘诀是在我的 PC 和 linux 服务器之间建立一个 ssh 隧道,如下所示:

ssh -R 9000:localhost:9000 username_goes_here@hostname_goes_here

I also modified my /etc/php/7.4/mods-available/xdebug.ini as so: (The magic sauce is xdebug.remote_host=127.0.0.1我还修改了我的/etc/php/7.4/mods-available/xdebug.ini :(神奇的酱汁是xdebug.remote_host=127.0.0.1

zend_extension=xdebug.so    
xdebug.remote_log=/var/log/xdebug/xdebug.log    
xdebug.remote_host=127.0.0.1
xdebug.remote_enable=1
xdebug.idekey=PHPSTORM

Thus the ssh tunnel will connect to port 9000 on the server with local port 9000 on the PC as the other end of the tunnel.因此,ssh 隧道将连接到服务器上的端口 9000,而 PC 上的本地端口 9000 作为隧道的另一端。

Then all that had to be done is to setup PhpStorm.然后要做的就是设置 PhpStorm。 For that (and where these instructions originated) see here .为此(以及这些指令的来源)请参见此处

Just a note - I only tested a php script access though a web browser.只是一个说明 - 我只通过网络浏览器测试了 php 脚本访问。 I have not tested a CLI based PHP script, but I'm guessing the answer is much the same.我还没有测试过基于 CLI 的 PHP 脚本,但我猜答案是一样的。

If you need more information on ssh port forwarding (tunneling) see here .如果您需要有关 ssh 端口转发(隧道)的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM