Visual Studio Code PHP调试不工作

38

我已经安装了VS CodePHP调试器

我正在使用xampp。

我尝试以两种方式运行代码(侦听和启动)。

侦听仅仅坐在那里,而启动在调试控制台中显示 spawn php ENOENT

这是我的launch.json文件。

    {
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

我做错了什么?


2
你在php.ini中启用了远程调试吗? - Alex Slipknot
1
你有什么问题?对我来说似乎不太清楚。 - node_modules
[XDebug] zend_extension = "php_xdebug-2.5.3-5.5-vc11-nts.dll" ;xdebug.profiler_append = 0 ;xdebug.profiler_enable = 1 ;xdebug.profiler_enable_trigger = 0 ;xdebug.profiler_output_dir = "C:\xampp\tmp" ;xdebug.profiler_output_name = "cachegrind.out.%t-%s" xdebug.remote_enable = 1 xdebug.remote_autostart = 1 xdebug.remote_handler = "dbgp" xdebug.remote_host = localhost xdebug.remote_port = 9000 xdebug.trace_output_dir = "C:\xampp\tmp" - GaryK4
1
我不知道如何格式化注释,而且已经达到了5分钟的时间限制。 - GaryK4
2
我刚刚写了一个公共的gist,其中包含这个和其他信息,以便我不会忘记。如果有帮助,请查看: https://gist.github.com/MetalFatigue82/850c39a8e1f9d87715c2a62421e40c04 - Pedro Costa
1
我在这里写了一份完整的指南:https://www.cloudways.com/blog/php-debug-with-xdebug/ - Shahroze Nawaz
11个回答

40

刚刚制作完成:

  1. We have OS Windows with Microsoft Visual Code and we have installed 'PHP Debug' module https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug#overview Steps to make it working already wroten here, but I'll write again:

  2. Install XDebug. Online Wizard https://xdebug.org/wizard.php helps you, just post here phpinfo() data. Save in phpinfo.php this:

    <?php phpinfo(); ?>
    

然后在浏览器中打开localhost/phpinfo.php,复制您看到的所有内容(Ctrl+A然后Ctrl+C)到向导中。它会显示您获得的信息和要执行的步骤: enter image description here

  1. Open shown in instruction php.ini (there are always many php.ini - apache folder has one php.ini, php has php.ini and phpForApache.ini. But phpinfo makes it clear showing which one is really used)

    2.1. and configure it with xdebug.remote_enable = 1 and xdebug.remote_autostart = 1. My working example:

    [XDebug]
    zend_extension = "c:\wamp64\bin\php\php7.1.9\ext\php_xdebug-2.6.0beta1-7.1-vc14-x86_64.dll"    
    xdebug.stopOnEntry = true
    xdebug.profiler_enable = off
    xdebug.profiler_enable_trigger = Off
    xdebug.profiler_output_name = cachegrind.out.%t.%p
    xdebug.profiler_output_dir ="c:/wamp64/tmp"
    xdebug.show_local_vars=0
    ;xdebug.profiler_output_name = "cachegrind.out.%t-%s"
    xdebug.remote_enable = 1
    xdebug.remote_autostart = 1
    xdebug.remote_handler = "dbgp"
    xdebug.remote_host = "127.0.0.1"
    xdebug.remote_log = "C:\wamp64\tmp\xdebug.txt"
    xdebug.remote_port = 9000
    xdebug.trace_output_dir = "C:\wamp64\tmp"
    xdebug.remote_cookie_expire_time = 36000
    
  2. Restart Apache2 webserver.

还有:

由于某种原因,我首先在Netbeans上使xdebug工作,然后才发现手册中的选项xdebug.remote_autostart = 1也可以使它在VS Code中工作。

如果您对XDebug的安装不确定,请检查phpinfo页面上是否有“with Xdebug”一词,并检查logs/apache_errors.log中是否有“failed loading php_xdebug”(下载的DDL不正确)。然后再使用任何其他IDE进行检查。

祝好!


1
刚刚救了我的一天!我想知道为什么XDebug的文档没有像这个答案一样明确。 - cbuchart
7
整天的努力之后,最终在遵循您的注释 xdebug.remote_autostart = 1 后得以运作。 - Rashid Shaikh
2
现在已在Ubuntu上进行了测试,只需设置zend_extensionxdebug.remote_autostartxdebug.remote_enable即可。 - Eluny
XDebug 3 对我来说无法工作,可能是因为新设置的原因,所以我安装了2.7.0alpha1版本,现在它可以完美地工作了。 - s0up
每次我需要调试的时间间隔超过一个月,我就必须花费整整一天的时间来让xdebug正常工作。不要太自责了。 - Dan Chase
显示剩余3条评论

24

使用xdebug(v3.x)在2021年完成,一些步骤不同,因此我将引用更多细节:

在您的PHP上安装xdebug模块

  1. 运行调用“phpinfo();”的PHP页面,并复制输出内容(是的,您可以选择所有文本和Ctrl+C),然后粘贴到https://xdebug.org/wizard中。

  2. xdebug.org网站将为您回答正确的xdebug模块版本以下载(例如:php_xdebug-3.0.4-7.4-vc15-x86_64.dll"),还会告诉您如何安装它,哪个目录以及在PHP.ini中包含的参考信息。

  3. 在PHP.ini上启用xdebug设置以调试模式并启用远程步进调试器,如下所示的示例

[XDebug]
xdebug.mode=debug
xdebug.remote_enable=on
xdebug.start_with_request=yes
zend_extension = C:\xampp\php\ext\php_xdebug-3.0.4-7.4-vc15-x86_64.dll
你可能需要根据自己的情况(例如使用另一个客户端口)更改这些xdebug设置,可以通过运行“phpinfo()”查看所有可能的参数列表。这个{{技巧将帮助你解决问题}},同时你还必须确保“Step debugger is shown as enabled”,它还提供了与每个主题相关的右列链接。

Xdebug settings shown on phpinfo()

因此,在之前的设置中,您可以检查主机和端口是否符合您在VS Code中要使用的预期。 设置您的Visual Studio Code
  1. 使用扩展选项卡,搜索“PHP Debug”并安装它(值得一提的是,扩展页面上的详细信息也有自己的教程)。 enter image description here
  2. 在VS Code菜单“文件->首选项->设置”中,我已经为我的PHP可执行路径添加了标记: enter image description here
  3. 最后,在VS Code调试选项卡中,您将被要求创建一个新的launch.json文件,只需单击它并选择PHP即可。这里的重要步骤是项目“Listen for Xdebug”中的“configurations”部分,请确保端口与XDebug上先前phpinfo()报告中显示的端口相同。 enter image description here
  4. 确保您的VS Code项目文件夹正确,并使用“Lister for Xdebug”启动新的调试。 enter image description here
如果出现错误或没有任何反应,请专注于检查“phpinfo()”中显示的XDebug设置,还要检查“Listen for XDebug”部分的“launch.json”,或者检查Web服务器的错误日志以获取可能的错误详细信息。
最好的问候!

13

对于任何想要找答案的人,对我有帮助的是更改php.ini中[XDebug]部分为以下内容:

xdebug.mode = debug
xdebug.start_with_request = yes
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.stopOnEntry = true
xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = Off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.output_dir ="c:\xampp\tmp"
xdebug.show_local_vars=0
xdebug.remote_handler = "dbgp"
xdebug.client_host = "127.0.0.1"
xdebug.log = "C:\xampp\tmp\xdebug.txt"
xdebug.client_port = 9000
xdebug.remote_cookie_expire_time = 36000

我使用了@Eluny的帖子中提供的设置,但是随着XDebug的某些更新版本,属性名称已经发生了变化。在文档页面上,您可以找到关于更改的信息。


对于每个版本的xdebug,它们都会更改参数的名称,因此您需要查看文档以了解更改。 - Gadziu
1
哦,经过一千次尝试终于成功了。非常感谢! - rustyBucketBay

7

您只需要在php.ini文件中进行以下更改。
对于Xdebug v3.x.x:(请注意,此版本中的端口号已更改为9003)。

[XDebug]
xdebug.mode = debug
xdebug.start_with_request = yes

对于Xdebug v2.x.x:

[XDebug]
xdebug.remote_enable = 1 xdebug.remote_autostart = 1
xdebug.remote_port = 9000


7
在您的lunch.json文件中添加以下行: "runtimeExecutable": "c:\\xampp\\php\\php.exe"
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000,
            "runtimeExecutable": "C:\\Program Files\\PHP\\v7.3\\php.exe"
        }
    ]
}

"runtimeExecutable" 这一行解决了我的问题,我已经找了好几个小时了! - Luc vd Enden
这对我也起作用,但仅适用于“启动当前打开的脚本”模式,而不是“侦听XDebug”,因此我无法调试整个应用程序,只能确定选定的文件:/ 无论如何非常有用! - rustyBucketBay

2

我使用的是 Windows 机器上的 VS Code,而 Web 服务器和 PHP 则在 Linux 机器上。按照之前的帖子中提到的设置一切都已经完成,但在 launch.json 文件中,我需要设置路径映射,例如:

 {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/var/www/html": "z:\\"
      }
    },

也许有人有类似的问题...

1
在新版的XDebug 3.0.X中,一些XDebug参数已经发生了变化。如果您的客户端端口为9000,则还需要在php.ini文件中设置xdebug.client_port = 9000。在Visual Studio Code中打开您的PHP项目,然后单击运行 -> 添加配置并将端口更改为9000。现在您可以使用“侦听XDebug”选项。

1

您是否检查过防火墙是否允许端口9000的入站连接?我在我的一台开发机上遇到了完全相同的问题。尽管所有其他设置都正确,但我无法触发断点。一旦我在防火墙中添加了一个例外,它就开始工作了。

因此,如果这是您设置的问题,您有两个选项来解决它。

  1. 完全关闭防火墙,或者在您处理项目时至少暂时关闭它。
  2. 在您的防火墙设置中添加一个例外:
    • 转到Windows控制面板并键入“防火墙”。
    • 从左侧导航中选择“入站规则”,然后单击“新建规则”。
    • 选择“端口”,然后点击“下一步”。
    • 确保选择了“特定本地端口”选项,并输入端口号“9000”。
    • 选择“允许连接”,然后点击“下一步”。
    • 选中“域”,“私人”和“公共”,然后点击“下一步”。
    • 输入一个描述性名称,如“XDebug Port”,然后点击“完成”。
    • 重新启动Visual Studio Code,然后再次尝试调试。
假设您已正确设置了配置的Web服务器部分,这应该允许您进行调试。
希望这有所帮助。

1

我已经重启了一切,但仍无法在断点处停止。 最终,我在php.ini中添加了这一行: xdebug.remote_port = "9000" 解决了我的问题。


这是一个常见的问题。在 XDebug 3.0 中,当 php.ini 中不存在 xdebug.remote_port 条目时,默认端口从 9000 更改为 9003,因此如果您相应地配置了 launch.json,则它们也将匹配。 - Nimral

0

我会使用NetBeans,因为它们有一个更主动的Xdebug界面。另外,xdebug V3有一个不同的端口(端口9003),所以你应该进行调整。在NetBeans和其他调试器中,可能会显示端口9000,但现在是9003。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接