如何使用 xdebug 调试 PHP CLI 脚本?

问题描述 投票:0回答:5

我还没有完全弄清楚这一点。我找到的每一篇文档都涵盖了如何使用 xdebug 调试在 Apache 中运行的脚本。我需要调试 php CLI 脚本。

那么,例如,如何传递 XDEBUG_SESSION_START 变量来启动 xdebug?

我专门尝试调试 CakePHP shell。因此,如果有人对此有任何额外的见解,我将非常感激。

谢谢。

cakephp xdebug php
5个回答
41
投票

Xdebug 手册中有一些关于此的注释,例如(引用):

export XDEBUG_CONFIG="idekey=session_name"
php myscript.php

如果您使用 Eclipse PDT 来开发和调试 PHP 脚本,Apache 或 CLI 之间没有太大区别:配置完全相同,您只是不必配置 Web 服务器,也不必指定 URL ;相反,您必须指示 PHP 可执行文件的路径。

关于

XDEBUG_SESSION_START
变量:好吧,你在“调试模式”下启动整个脚本,所以我想说,你没有任何“调试会话”的概念。


例如,这就是

Window > Preference > PHP > PHP executables
现在对我来说的样子,在右侧,我点击第一个按钮的
Edit
按钮时得到的结果:


(来源:pascal-martin.fr

(来源:pascal-martin.fr

还有

debug configurations
窗口:


(来源:pascal-martin.fr

并启动调试:它就可以工作了:


(来源:pascal-martin.fr


希望这有帮助:-)

另外,您遇到什么具体问题?


9
投票

如果您使用 bash(或类似的 shell),这个小脚本可能会派上用场:

alias drush-debug=drd
function drd {
    export XDEBUG_CONFIG="idekey=cli_session"
    export SERVER_NAME="developer.machine"
    export SERVER_PORT="9000"
    drush "$@"
    unset XDEBUG_CONFIG
    unset SERVER_NAME
    unset SERVER_PORT
};

或按照下面评论者的建议

alias drd='XDEBUG_CONFIG="idekey=PHPSTORM" drush "$@"'

这样您就不必在每次调试时手动设置和取消设置触发变量。


4
投票

只需将以下部分放入您的 php.ini

[XDebug]
xdebug.max_nesting_level = 200
xdebug.remote_enable=1
xdebug.remote_port=9000
;xdebug.profiler_enable=1
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1

并将 PHPSTORM 替换为您的 ide 密钥


1
投票

对于 Windows 和 Visual Studio Code,操作方法如下:

  1. https://xdebug.org/download 下载 xdebug。例如 php 7.4 Windows 64 位 https://xdebug.org/files/php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll

  2. 将 xdebug dll 复制到您的 php 扩展目录 (ext)。

  3. 添加到php.ini末尾

    [XDebug]
    zend_extension=php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll

    xdebug.remote_enable=1
    xdebug.remote_autostart=1
  1. 打开 VSCode 并安装 https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug

  2. 在 VSCode 中打开项目工作区,转到“运行”选项卡,单击齿轮并添加这些行

    {
      "name": "listen CLI",
      "type": "php",
      "request": "launch",
      "port": 9000
    },
    {
      "name": "run CLI",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 9000
    }
  1. 在要调试的脚本中放置断点

  2. 选择“运行CLI”并点击“开始调试”

调试愉快!


0
投票

PHP配置:

zend_extension=xdebug.so
xdebug.remote_handler=dbgp
xdebug.mode=debug

在我的系统上它是文件

/etc/php/conf.d/xdebug.ini
。配置也可以在主
php.ini
中。

如果我想使用 XDebug 运行脚本,我会内联传递环境变量:

XDEBUG_SESSION=1 php arguments
© www.soinside.com 2019 - 2024. All rights reserved.