Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug PHP CLI scripts from the CLI

Tags:

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.

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.

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:

  • 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:

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:

  • 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:

Launching renameUser has encountered a problem. An internal error occurred during "Launching 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_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:

Error launching 'renameUser' The debug session could not be started. Please make sure that the debugger is properly configured as a php.ini directive.

  • 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:

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

  • 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!

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!
like image 719
Andrew DeFaria Avatar asked Dec 08 '15 01:12

Andrew DeFaria


People also ask

What is a PHP CLI script?

As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.

Is there a PHP debugger?

A: You can easily debug PHP in Chrome using a simple extension called PHP Console. Just install this PHP debugging tool from the Chrome web store and start logging errors, warnings, exceptions, and vars dump on your Chrome browser.


2 Answers

When debugging on the command line you need to tell PHP you want to debug also

http://xdebug.org/docs/remote

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)

This allows you to debug a cmdline script.

Personally I use Vim with Vdebug extention (xdebug for vim) to debug and nothing other than a command line is needed

like image 139
exussum Avatar answered Oct 01 '22 16:10

exussum


exussums answer works for me.

In addition I have the following in /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

like image 25
jm009 Avatar answered Oct 01 '22 16:10

jm009