How to debug PHP CLI scripts from the CLI - php

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!

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

What worked for me was using dephpugger. I found the steps to get CLI debugging working here:
https://hackernoon.com/how-debug-php-applications-with-dephpugger-98cc234d917c
I had previously installed xdebug and tried out exussum's answer.
The steps I followed are included here for completeness.
Install dephpugger:
composer global require “tacnoman/dephpugger”:”dev-master”
Put dephpugger on the path:
export PATH=$PATH:$HOME/.composer/vendor/bin
make sure to include in the php file at the breakpoint:
xdebug_break();
open up 2 terminal instances. In one run:
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.

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

Being able to set break points in a PHP script requires some kind of debugger extension being loaded, either XDebug or 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.
Of course there is the good old var_dump();die('hi'); debugging, but this does not include continuing code execution after the script ended. ;)

Related

Xdebug PhpStorm: debugging scripts started with exec("php index.php")

I'm trying to add debugging to an old project that uses exec() to start a new session asynchronously from within another PHP script:
exec("php /var/www/html/validata/index.php",$result)
The normal PHP script is fully debugable with Xdebug but the script started with the exec command isn't because it can't map from file:///var/www/html/index.php to a local file location since it's started within CLI shell. The session started this way does trigger the debugger but can't find the file locally:
Cannot find file '/var/www/html/validata/index.php' locally.
To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session.
I've followed the instructions to add the PHP_IDE_CONFIG to the env. I've also added this to the server with 127.0.0.1 replaced with the desktop PC IP address (server is running in a docker container):
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
Any pointers are greatly appreciated!
UPDATE
Solution:
Use the cli interpreter set to the docker container (in settings > Languages & Frameworks > PHP > CLI interpreter, add new, select docker and point it to the php binary) so that a debug session can be started with a new debugging configuration. I've copied the arguments from the exec command into the new configuration and it can now fully debug the script. I have to prepare a database table to make it fully testable but this is a working solution for me.
Thanks for all the replies!
You can solve the issue about the wrong php.ini file being loaded by specifying it in the command line:
exec("php -c " . escapeshellarg(php_ini_loaded_file()) .
" /var/www/html/validata/index.php",$result);
Though I doubt that would make much of a difference as far as xdebug is concerned.
I think a better solution would be to just require the file, which would cause xdebug to not become lost when you fork a new process.
require_once "/var/www/html/validata/index.php";
I've made a mistake how to refer to the server in PHP_IDE_CONFIG:
export PHP_IDE_CONFIG="serverName=SomeName"
should be run in the container where php runs, and SomeName should exactly match what is in PHPStorm/IntelliJ Settings > Languages & Frameworks > php > Servers > Name (not host). It's not a fqdn, just whatever is in the name field.

PHP xdebug not working for Visual Studio Code

I have followed to the letter instructions for setting up xdebug with Visual Studio Code: https://blogs.msdn.microsoft.com/nicktrog/2016/02/11/configuring-visual-studio-code-for-php-development/
So all config files, files, etc are installed and tested as working outside of PHP.
Currently I am running PHP using the built in server (php -S localhost:8000 -t).
This works fine and I can see output in my browser. Similarly within VS Code I can run my code fine by running Ctrl + Shift + B. It also works inside Eclipse after installing PDT plugin.
Using phpinfo() I can see that xdebug is there and working fine. However, when I 'listen for XDebug' (I have changed launch.json to use port 8000) or press F5 all that happens is my status bar turns orange, the debug pane shows nothing and the debug buttons show pause/stop and I have to click one of these to quit the program, i.e. it does nothing.
What am I doing wrong?
Keep in mind that if you running PHP using the built in server, you must configure the php.ini file of the CLI (in my case is sudo nano /etc/php/5.6/cli/php.ini), not of Apache 2.
You shouldn't change launch.json to use port 8000, unless you've set xdebug.remote_port=8000 in your php.ini file. The port of the php server and xdebug are two different things. I guess that your xdebugremote_port is 9000 since using port 8000 for both things is impossible, so just change launch.json back to 9000.

Shortcut to turn PHP xdebug ON

A script to turn xdebug On works but I cannot figure out out to create a shortcut to run it. If I open administrator PowerShell in C:\Users\George\Desktop\Development\Config Files, the command .\PHPxDebugOn will restart Apache with the modified php.ini. (There's also a PHPxDebugOff.ps1 to turn xdebug off.) Clicking on the shortcut (with Run as administrator ON) just flashes a black screen but no change to Apache.
script:
$iniFile = "C:\Users\George\Desktop\Development\Config Files\php.ini"
$xFile = "C:\Users\George\Desktop\Development\Config Files\xdebug.ini"
$phpIniFile = "C:\PHP\php.ini"
get-content $iniFile, $xFile | set-content $phpIniFile
C:\Apache24\bin\httpd -k restart
shortcut target:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file "&C:\Users\George\Desktop\Development\Config Files\PHPxDebugOn.ps1"
The problem is in the shortcut target.
Removing the & symbol should resolve the script not running, provided the path is correct.
PowerShell exits without attempting to run the script if the target file in the shortcut is invalid in any manner. Unless everything in the command can be evaluated/parsed/retrieved, the default behavior is to stop everything and report the error. That explains why the window disappeared, even with the -NoExit parameter specified. PowerShell does present the error in the brief moment that window is visible. Screen refresh rates and processor speeds being what they are, screenshots were a bit of a challenge to catch.
Sample 1:%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -file "&C:\scripts\hello.ps1"
Sample 2:%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -file "C:\scripts\hello.ps2"
Sample 3(successful):%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -file "C:\scripts\hello.ps1"
Notes:
Repro environment: Windows 7 Enterprise with Powershell 5.0 April 2015 preview
The .ps2 file extension may be valid on some systems
You can skip loading PowerShell profiles by using the -NoProfile parameter
File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters
Seeing as my comment is, according to the OP, a better approach to the problem, I'll post it here as an answer:
What I'd do is create a second virtual host that points to the same source, but loads separate php.ini files to enable xdebug on that vhost. That way, you can simply use http://local.project.dev and http://local.project.debug to run the same code with xdebug enabled. This way, you don't have to restart apache each time you want to use xdebug to step through the code.

PHP from source - Questions about Extension-Folder and Interaction with existing repo-version

As a Linux beginner I'm currently a bit confused building my own PHP from source.
I have a Ubuntu with a LAMP-Stack where all packages comes from a Repository.
(PHP as FastCGI)
Now I'm trying to set up a second PHP-Version and actually it works well but I have a few questions.
First of all some specs:
Ubuntu 10.04 (I need this older Version)
PHP 5.3.2-1ubuntu4.2.5 (Repo)
For my own PHP-Version I use the following "./configure" parameters (Reduced to the essential):
--prefix=/opt/php5310
--with-config-file-path=/opt/php5310/etc
--with-config-file-scan-dir=/opt/php5310/conf.d
--with-libdir=lib64
--with-mysql=/usr/bin/mysql
...
First of all executing "sudo make test" show up a few failed tests (about 10-14). Unfortunately I can't find any information what I have to do or what excactly goes wrong but "sudo make install" works nevertheless.
Is it normal that not all tests passed?
Then I noticed (phpinfo) the "imagick" extension was not loaded.
Looking at the "extension_dir" shows why.
On the Repo-Version:
extension_dir = /usr/lib/php5/20090626
On my own Version:
extension_dir = /opt/php5310/lib/php/extensions/no-debug-non-zts-20090626
(This folder doesn't even exist)
When I put this inside the php.ini:
extension_dir = /usr/lib/php5/20090626
It's working but is it the right way or just a creepy workaround?
And what about "--with-libdir=lib64" doesn't it belong to "extension_dir"?
On my System /usr/lib64 is a symlink to /usr/lib.
And last but not least my Repo-Version has a folder "/etc/php5/cgi/conf.d" with many ".ini" files e.g. "curl.ini", "gd.ini" and so on.
My Solution was:
"--with-config-file-scan-dir=/opt/php5310/conf.d
And then create the "conf.d" Folder and copy everthing from "/etc/php5/cgi/conf.d" to "/opt/php5310/conf.d" to be not dependent on the PHP Repo-Version.
Would this be a correct solution for multiple PHP-Versions or should it be a symlink or
"--with-config-file-scan-dir=/etc/php5/cgi/conf.d
Thank you for your help
Failing tests generally means failing functionality. Of course, for something as large as PHP you may never touch that functionality. It may be that your best bet here is to search for the names of the tests which failed to see if anyone has had the same issue (and how to fix it or disable the functionality), and to find the source of the tests in PHP and understand why they failed.

Netbeans file debugging

I have netbeans setup with xdebug so it can debug php. However, this only works if I create a php project. It will not work if I try opening a stand alone php file. So my question is, is it possible to debug a stand alone php file which is not part of a netbeans php project?
If that is not possible, how do I debug stand alone php files with netbeans?
No, There is none that I am aware of. As Myrddin mentioned the debugger needs some configurations that is a part of netbeans project.
but the best way you can debug a single file is to copy it on a project folder, and click the debug project, once the debug session is set then you can browse the PHP File that you want to debug and it will actually go through xdebug.
Good Luck!
Each project can have it's own configuration (you can have 1 project that has PHP5.4 interpreter, one the is PHP5.6, one that is a command line and another that is a web project), but if you configure a general PHP 5 Interpreter:
If you work on a windows machine you can use this code (filename is php.cmd)
set XDEBUG_CONFIG="idekey=netbeans-xdebug"
#php.exe %*
If you want to be able to debug, your interpreter should have the XDEBUG_CONFIG system variable and make sure it's connected to netbeans. You should set this to the same value in your Debbugging section of the PHP's config:
Next thing - if you right click inside the editor you will have the Debug File option, and a prompt window will pop:
You don't really need anything here. Just hit the "OK" button.
As you can see, this final result is debug session of the t1.php file within c:\TEMP\ (which is not a working project):
Short answer: CTRL + SHIFT + F5
You can find the answer here:
https://blogs.oracle.com/netbeansphp/entry/run_file_without_project
I'm not entirely sure, but I think it is not possible, because you need some configuration to get the debugging working, and this configuration is part of a project.
You can always use print_r and var_dump to debug a single file. But that is probably not the answer you're looking for.
xdebug is very heavy and old tool you can use Kint php debuger here.
its free, so you can download Here
it's pretty replacement for var_dump(), print_r() and debug_backtrace().
you need to add kint.class.php file using include or require function.
require '/kint/Kint.class.php';
that's it.
and you can use like
########## DUMP VARIABLE ###########################
Kint::dump($GLOBALS, $_SERVER); // pass any number of parameters
// or simply use d() as a shorthand:
d($_SERVER);
########## DEBUG BACKTRACE #########################
Kint::trace();
more help is available on https://github.com/raveren/kint/
Good Luck :)

Categories