如何从 CLI 调试 PHP CLI 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34146594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug PHP CLI scripts from the CLI
提问by Andrew DeFaria
Does anybody know how to debug CLI PHP scripts from the CLI?!? I don't want to debug a PHP web page - I don't have a PHP web page. I don't want to debug a remote script either - I'm running/debugging right here on this system. I don't want to (at this time) try to get some IDE (Eclipse, PhpStorm or whatever) to debug a CLI PHP rather I just want to debug some PHP CLI script at the Linux command line itself. In Perl this would be simply perl -d <script.pl> <options>. Debugging a script, to me, is not figuring out compile errors or other simple things. To me it's setting break points, running code, examine the contents of variables and being able to arbitrarily execute or eval ('<php code>') at the debugger.
有人知道如何从 CLI 调试 CLI PHP 脚本吗?!?我不想调试 PHP 网页 - 我没有 PHP 网页。我也不想调试远程脚本 - 我正在这个系统上运行/调试。我不想(此时)尝试使用一些 IDE(Eclipse、PhpStorm 或其他)来调试 CLI PHP,而我只想在 Linux 命令行本身调试一些 PHP CLI 脚本。在 Perl 中,这将是简单的 perl -d <script.pl> <options>。对我来说,调试脚本并不是找出编译错误或其他简单的事情。对我来说,它是设置断点、运行代码、检查变量的内容并能够在调试器中任意执行或 eval ('<php code>')。
Sure later I'd like to configure this into my IDE of choice (at this time this is Eclipse) but I have not managed to get that working. Debugging from the CLI a PHP CLI script would be a good start for me.
当然,稍后我想将它配置到我选择的 IDE(此时这是 Eclipse)中,但我还没有设法让它工作。从 CLI 调试 PHP CLI 脚本对我来说是一个好的开始。
Thanks.
谢谢。
I don't know why I'm limited to a character count when posting a comment. Perhaps I can add more text here.
我不知道为什么我在发表评论时仅限于字符数。也许我可以在这里添加更多文字。
Here's what I have tried in order to use xdebug and/or Zend debugger with Eclipse:
这是我为了在 Eclipse 中使用 xdebug 和/或 Zend 调试器而尝试的:
- Base Eclipse version Mars.1 Release 4.5.
- Eclipse PDT UI Plugin version 3.7.0.2015112
- Tried installing xdebug using pecl install xdebug. Says I need to add "zend_extension=xdebug.so" to php.ini. Really? Which php.ini? I have several:
- 基础 Eclipse 版本 Mars.1 Release 4.5。
- Eclipse PDT UI 插件版本 3.7.0.2015112
- 尝试使用 pecl install xdebug 安装 xdebug。说我需要在 php.ini 中添加“zend_extension=xdebug.so”。真的吗?哪个php.ini?我有一些:
Andromeda:sudo find / -xdev -name php.ini /etc/php5/cli/php.ini /etc/php5/apache2/php.in /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php53/php.ini /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php.ini /opt/eclipse/plugins/com.zend.php.debug.debugger.php56.linux.x86_64_13.0.1.v20151112-2045/resources/php56/php.ini Andromeda:
仙女座:sudo find / -xdev -name php.ini /etc/php5/cli/php.ini /etc/php5/apache2/php.in /opt/eclipse/plugins/org.zend.php.debug.debugger.linux .x86_64_5.3.18.v20110322/resources/php53/php.ini /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php.ini /opt /plugins/com.zend.php.debug.debugger.php56.linux.x86_64_13.0.1.v20151112-2045/resources/php56/php.ini 仙女座:
- I put the zend_extension thing in both /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini. Made a phpinfo.php page and I see Xdebug in there (yea!). Configure a Debug Configuration in Eclipse to use xdebug and try to debug:
- 我把 zend_extension 的东西放在 /etc/php5/cli/php.ini 和 /etc/php5/apache2/php.ini 中。制作了一个 phpinfo.php 页面,我在那里看到了 Xdebug(是的!)。在 Eclipse 中配置一个 Debug Configuration 使用 xdebug 并尝试调试:
Launching renameUser has encountered a problem. An internal error occurred during "Launching renameUser" java.lang.NullPointerException.
启动 renameUser 遇到问题。“启动重命名用户”java.lang.NullPointerException 期间发生内部错误。
Oh goodie...
哦好人...
- I had also installed the Zend Debugger and added the following to those same two php.ini files:
- 我还安装了 Zend Debugger 并将以下内容添加到这两个相同的 php.ini 文件中:
zend_extension=/usr/lib/php5/20121212/ZendDebugger.so zend_debugger.allow_hosts=127.0.0.1/32, 192.168.0.0/16 zend_debugger.expose_remotely=always
zend_extension=/usr/lib/php5/20121212/ZendDebugger.so zend_debugger.allow_hosts=127.0.0.1/32, 192.168.0.0/16 zend_debugger.expose_remotely=always
- Changed debug configuration to use Zend Debugger and attempted to debug. Received:
- 将调试配置更改为使用 Zend Debugger 并尝试调试。已收到:
Error launching 'renameUser' The debug session could not be started. Please make sure that the debugger is properly configured as a php.ini directive.
启动“renameUser”时出错 无法启动调试会话。请确保调试器已正确配置为 php.ini 指令。
- Restarted Eclipse and now the debugger attempts to run but simply terminates with a 255 exit value attempting to run /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi. Why it's runnign php-cgi is beyond me. I said this was a CLI not a CGI. In any event I get the following when trying to run this from the command line:
- 重新启动 Eclipse,现在调试器尝试运行但只是以 255 退出值终止尝试运行 /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php -cgi。为什么它运行 php-cgi 超出我的范围。我说这是 CLI 而不是 CGI。无论如何,当我尝试从命令行运行它时,我得到以下信息:
Andromeda:/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
仙女座:/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi /opt/eclipse/plugins/org.zend.php.debug.debugger .linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi:加载共享库时出错:libiconv.so.2:无法打开共享对象文件:没有那个文件或目录
- Found a libiconv.so.2 in /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/lib and set LD_LIBRARY_PATH to include that but this just fails to launch the debugger at all stating to make sure that the debugger is properly configured as a php.ini directive... UGH!
- 在 /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/lib 中找到 libiconv.so.2 并设置 LD_LIBRARY_PATH 以包含它,但这只是无法启动调试器完全声明要确保调试器已正确配置为 php.ini 指令... 哎呀!
Other odd stuff:
其他奇怪的东西:
- When Eclipse starts up it fails to open my RSE based project thus displaying edit buffers from the last run as empty
- Eclipse will no longer exit! Select File: Exit. Nothing happens. Click on the X in the title bar - nothing happens. Now I have to kill it to close it!
- 当 Eclipse 启动时,它无法打开我基于 RSE 的项目,因此将上次运行的编辑缓冲区显示为空
- Eclipse 将不再退出!选择文件:退出。没发生什么事。单击标题栏中的 X - 没有任何反应。现在我必须杀死它才能关闭它!
回答by exussum
When debugging on the command line you need to tell PHP you want to debug also
在命令行上调试时,您需要告诉 PHP 您也想调试
says
说
export XDEBUG_CONFIG="idekey=session_name"
php myscript.php
assuming xdebug is enabled (it doesnt matter which .ini file its in, but there is a standard place per OS for this, usually in a conf.d folder called xdebug.ini which is auto included)
假设 xdebug 已启用(它在哪个 .ini 文件中无关紧要,但是每个操作系统都有一个标准位置,通常在一个名为 xdebug.ini 的 conf.d 文件夹中,它是自动包含的)
This allows you to debug a cmdline script.
这允许您调试 cmdline 脚本。
Personally I use Vim with Vdebug extention (xdebug for vim) to debug and nothing other than a command line is needed
我个人使用带有 Vdebug 扩展的 Vim(用于 vim 的 xdebug)进行调试,只需要命令行即可
回答by jm009
exussums answer works for me.
exussums 答案对我有用。
In addition I have the following in /etc/php/7.0/cli/conf.d/20-xdebug.ini
此外,我还有以下内容 /etc/php/7.0/cli/conf.d/20-xdebug.ini
zend_extension=xdebug.so
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_enable=true
#xdebug.remote_enable=false
And I had to do this: https://github.com/vim-vdebug/vdebug/issues/363
回答by Mr. S
What worked for me was using dephpugger. I found the steps to get CLI debugging working here:
对我有用的是使用 dephpugger。我在这里找到了让 CLI 调试工作的步骤:
https://hackernoon.com/how-debug-php-applications-with-dephpugger-98cc234d917c
https://hackernoon.com/how-debug-php-applications-with-dephpugger-98cc234d917c
I had previously installed xdebug and tried out exussum's answer.
我之前安装了 xdebug 并尝试了 exussum 的答案。
The steps I followed are included here for completeness.
为了完整起见,此处包含了我遵循的步骤。
Install dephpugger:
安装 dephpugger:
composer global require “tacnoman/dephpugger”:”dev-master”
Put dephpugger on the path:
将 dephpugger 放在路径上:
export PATH=$PATH:$HOME/.composer/vendor/bin
make sure to include in the php file at the breakpoint:
确保在断点处包含在 php 文件中:
xdebug_break();
open up 2 terminal instances. In one run:
打开2个终端实例。在一次运行:
dephpugger debug
In the other run
在另一个运行中
dephpugger cli /path/file.php
replacing /path/file.php with the path to your file. if you need command line arguments to the php script put the path and arguments in quotes. It still seems a little hacky. I think the real trick is to write in a language that has a better debugger built in.
将 /path/file.php 替换为您的文件路径。如果您需要 php 脚本的命令行参数,请将路径和参数放在引号中。它似乎仍然有点hacky。我认为真正的诀窍是用一种内置了更好的调试器的语言来编写。
回答by Sven
Being able to set break points in a PHP script requires some kind of debugger extension being loaded, either XDebug or Zend Debugger.
能够在 PHP 脚本中设置断点需要加载某种调试器扩展,XDebug 或 Zend Debugger。
And then you need some kind of interface to this debugger to make your wishes about break points known. I am unfamiliar with the Perl debugging, but I haven't heard about any PHP CLI based debugging - it is always taking place in an IDE that is able to handle the debugging protocol of one of the extensions.
然后你需要某种接口到这个调试器来让你知道断点的愿望。我不熟悉 Perl 调试,但我还没有听说过任何基于 PHP CLI 的调试——它总是在能够处理其中一个扩展的调试协议的 IDE 中进行。
Of course there is the good old var_dump();die('hi');
debugging, but this does not include continuing code execution after the script ended. ;)
当然还有旧的var_dump();die('hi');
调试,但这不包括脚本结束后继续执行代码。;)