Visual Studio Code - Xdebug无法工作

时间:2017-02-21 13:22:40

标签: php macos xampp visual-studio-code xdebug

在Visual Studio Code(1.9.1)(mac)中,我已经设置了 php-debug 插件。

在调试屏幕中,我开始' 收听Xdebug '。
在此之后,我在我的XAMPP服务器(本地)上打开index.php 但没有任何反应。

  • 屏幕底部的蓝色条变为橙色。
  • 步骤,步入和退出按钮显示为灰色。
  • 观察到的变量也会出现以下错误消息:
    无法在没有连接的情况下评估代码

我尝试在以下代码中使用断点:

<?php
$i = 0;

do {
$i++;
if (!($i % 1)) {
    echo('<p>$i = ' . $i . '</p>');
    }
}
while ($i < 100);
?>

我正在使用XAMPP,在我的 php.ini 文件中,我使用端口9000作为Xdebug。

zend_extension="/usr/local/Cellar/php71-xdebug/2.5.0/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote.port=9000

我使用自制软件安装了Xdebug 这是我的PHP信息: phpinfo.htm
Xdebug wizard告诉我Xdebug已正确安装。

我的 launch.json 文件如下所示:

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

有人知道我做错了吗?

在ini文件中设置xdebug.remote_connect_back = 1后 因为n00dl3建议大部分时间调试工作,但偶尔我得到以下
调试控制台中出错:

<- threadEvent
ThreadEvent {
  seq: 0,
  type: 'event',
  event: 'thread',
  body: { reason: 'exited', threadId: 1 } }

5 个答案:

答案 0 :(得分:1)

似乎需要设置服务器根目录 在launch.json中使用 localSourceRoot ,如下所示:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "localSourceRoot": "http://127.0.0.1/public_html/"
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

现在断点工作正常。

答案 1 :(得分:1)

我也遇到了这个问题,不是在相同的环境(NGINX服务器+ php-fpm)下,而是在相同的症状下。原来是我的xdebug配置引起的。

我如何诊断它:就像编写OP一样,编写一个简单的PHP脚本进行测试:

<?php

xdebug_info();

通过浏览它,我在设置中获得了很多信息,包括:

xdebug.client_host => localhost
xdebug.client_port => 9003

而我的xdebug在端口9900上侦听(默认为9000)。

修复步骤:只需将以下几行添加到您的php.inixdebug.ini中(无论xdebug配置的其余部分位于何处):

# This should match your xdebug.remote_host
xdebug.client_host=localhost
# This should match your xdebug.remote_port
xdebug.client_port=9900
xdebug.mode=debug

然后在VScode中重新运行调试会话,添加一些断点,然后重新浏览到脚本:我的VScode窗口弹出,在断点处暂停执行,并且可以按预期在调试面板中访问变量。< / p>

编辑:也不要忘记:

  • xdebug.mode=debug添加到您的php.ini
  • 重新启动网络服务器和php-fpm服务。

答案 2 :(得分:0)

这是我的launch.json,调试不起作用

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "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
        }
    ]
}

这是在我的php.in中

[xdebug] zend_extension =

"c:/wamp64/bin/php/php7.4.0/zend_ext/php_xdebug-2.8.0-7.4-vc15-x86_64.dll"
xdebug.remote_enable = on
xdebug.remote_autostart = on

答案 3 :(得分:0)

检查 xdebug.client_port 在页面 xdebug_info();或 phpinfo();

在 vscode 的 launch.json 中配置相同的端口

答案 4 :(得分:0)

我也遇到了这个问题。

不知何故,有一天,不知不觉中,我安装了 xdebug 的第 3 版,并且更改了很多 conf 参数名称,参见 this SO question

因此,例如使用 phpinfo 验证 xdebug 版本值得一试。