Visual Studio 代码 - Xdebug 不起作用

2022-01-14 00:00:00 visual-studio-code macos xampp php xdebug

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

In Visual Studio Code (1.9.1) (mac) i have setup the php-debug plugin.

在调试屏幕中,我开始listen for Xdebug".
在此之后,我在我的 XAMPP 服务器(本地)上打开 index.php.
但是什么也没发生.

In the debug screen i start 'listen for Xdebug'.
After this i open the index.php on my XAMPP server (local).
But nothing happens.

  • 屏幕底部的蓝色条变为橙色.
  • 跨步、进入和退出按钮显示为灰色.
  • 在监视变量中还会出现以下错误消息:
    无法在没有连接的情况下评估代码

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

I try to use breakpoints on the following code:

<?php
$i = 0;

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

我正在使用 XAMPP,在我的 php.ini 文件中,我使用端口 9000 进行 Xdebug.

I am using XAMPP and in my php.ini file i use port 9000 for 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 向导 告诉我 Xdebug 安装正确.

I installed Xdebug using homebrew.
Here is my php info: phpinfo.htm
Xdebug wizard tells me Xdebug is installed correctly.

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

my launch.json file looks like this:

{
"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
    }
]
}

有人知道我做错了什么吗?

Would anyone know what i am doing wrong?

在ini文件中设置xdebug.remote_connect_back = 1
正如 n00dl3 建议的那样,调试大部分时间都有效,但偶尔我会得到以下信息
调试控制台中的错误:

After setting the xdebug.remote_connect_back = 1 in the ini file
as n00dl3 suggested debugging most of the time works, but once in a while i get the following
error in the debug console:

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

推荐答案

我也遇到过这个问题,不是同一个环境(NGINX server + php-fpm),但是症状一样.原来是我的 xdebug 配置造成的.

I encountered this problem as well, not with the same environment (NGINX server + php-fpm) but the same symptoms. It turned out to be caused by my xdebug configuration.

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

How I managed to diagnose it : by writing a simple PHP script for test just like OP did :

<?php

xdebug_info();

通过浏览它,我得到了很多关于我的设置的信息,包括:

By browsing to it, I got a bunch of info on my setup, including :

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

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

whereas my xdebug was listening on port 9900 (default being 9000).

修复步骤:只需将以下行添加到您的 php.inixdebug.ini (无论您的 xdebug 配置的其余部分)谎言):

Steps to fix : just add the following lines to your php.ini or xdebug.ini (wherever the rest of your xdebug configuration lies) :

# 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 窗口,执行在断点处暂停,并且可以在调试面板中访问变量,就像预期的那样.

Then rerun a debug session in VScode, add some breakpoints, and re-browse to your script : my VScode window popped up, the execution was paused on the breakpoint and the variables where accessible in the debug pannel just like expected.

编辑:别忘了也:

  • xdebug.mode=debug 添加到您的 php.ini
  • 重启网络服务器和php-fpm服务.
  • add xdebug.mode=debug to your php.ini
  • restart webserver and php-fpm services.

相关文章