【问题标题】:Xdebug doesn't terminate PHP script when debugging stops调试停止时 Xdebug 不会终止 PHP 脚本
【发布时间】:2021-11-01 06:44:18
【问题描述】:

当我停止调试时,我的 PHP 守护程序脚本继续在后台运行。当我停止调试时,如何让 xdebug 自动终止/终止我的 PHP 脚本?

我的环境:

  • 带有 xdebug 扩展的 PHP 8 (cli)。
  • 带有 PHP Debug 扩展名的 Visual Studio 代码。

Visual Studio 代码(settings.json):

"launch": {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PHP Debug",
            "type": "php",
            "request": "launch",
            "program": "${workspaceFolder}/xdebug.php",
            "cwd": "${workspaceFolder}",
            "runtimeArgs": [
                "-dxdebug.log=/tmp/xdebug.log",
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_TRIGGER": "true",
                "XDEBUG_CONFIG": "client_port=${port}",
            },
            "internalConsoleOptions": "openOnSessionStart",
        },
    ],
},

调试测试脚本(/path/to/xdebug.php):

<?php

sleep(15);

Xdebug 日志(/tmp/xdebug.log):

[101075] Log opened at 2021-09-03 18:34:25.062284
[101075] [Step Debug] INFO: Connecting to configured address/port: localhost:9003.
[101075] [Step Debug] INFO: Connected to debugging client: localhost:9003 (through xdebug.client_host/xdebug.client_port). :-)
[101075] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///path/to/xdebug.php" language="PHP" xdebug:language_version="8.0.5" protocol_version="1.0" appid="101075" idekey="true"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[101075] [Step Debug] <- feature_set -i 1 -n resolved_breakpoints -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="1" feature="resolved_breakpoints" success="1"></response>

[101075] [Step Debug] <- feature_set -i 2 -n notify_ok -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="2" feature="notify_ok" success="1"></response>

[101075] [Step Debug] <- feature_set -i 3 -n extended_properties -v 1
[101075] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="3" feature="extended_properties" success="1"></response>

[101075] [Step Debug] <- run -i 4

******** I stopped debugging at 18:34:30 ********

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

[101075] Log closed at 2021-09-03 18:34:40.243542

【问题讨论】:

  • VSCode 是否发送“停止”或“分离”协议命令?要找出答案,请使用 xdebug.log 设置创建日志:xdebug.org/docs/all_settings#log 并找出答案:-)
  • 感谢@Detick 的回复。日志 (/tmp/xdebug.log) 中没有任何内容表明收到了停止/分离命令。在我停止调试然后记录“日志关闭”后,它完成了sleep(15);的执行。
  • 日志中不应该有sleep(15) - 你能显示日志的最后 10 行吗?
  • 抱歉,sleep(15); 是我用来测试 xdebug 的脚本,并且不包含在日志中。我在上面编辑过的问题中添加了/tmp/xdebug.log,在Log closed 消息前5 秒左右我停止了调试。
  • [57177] [Step Debug] &lt;- stop -i 5 — 你说“日志 (/tmp/xdebug.log) 中没有任何内容表明停止/分离命令”。它里面有stop。 Xdebug 也回复为stopped,下一个日志行是Log closed。看起来脚本终止得很好。

标签: visual-studio-code xdebug xdebug-3


【解决方案1】:

这里是 PHP Debug 扩展维护者。

由于 Xdebug 的“同步”特性,这是不可能的。你的脚本就是一个很好的例子。

Xdebug/DBGp 一次只能处理一个请求,直到旧请求完成后才能接收到新请求。这里的请求是“运行”,响应是 &lt;response ... command="run" ... status="stopping" reason="ok"&gt;&lt;/response&gt; - 当脚本停止时收到。

因此,在这两者之间,您不能发送任何其他命令,例如 stop

换句话说,您只能在脚本处于暂停状态(即断点)时停止脚本。

当 PHP Debug 收到断开连接请求表单 VS 代码时,它会尝试发送stop 命令 - 如果它在 500 毫秒内未能发送(连接忙于处理不同的请求),它将简单地断开与进程的连接。

这将允许 PHP 进程继续执行代码。

但是!如果您使用launch.jsonprogram 字段启动脚本,PHP 调试扩展也将终止正在运行的进程。

这在此处并不明显,但脚本的执行方式可能存在一些差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 2021-01-29
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2019-07-16
    相关资源
    最近更新 更多