How to debug code while using my browser on PhpStorm - php

I cannot debug Laravel via PhpStorm and Xdebug.
When I try to debug a single file via PhpStorm, it works fine, but when I set a breakpoint and try it on the browser, it doesn't work.
PhpStorm waits for incoming connection with ide key and it stops there.

The workflow that I've set:
Use something like Xdebug helper.
Configure it to use PhpStorm IDE Key.
When you want to debug:
Activate Xdebug Helper (Mark Debug)
Go to PhpStorm and Run > Start listening for PHP Debug Connections
Put a breakpoint at your code
Refresh your page in Chrome (or go to another one, just make a request).
Basically, the Xdebug Helper will be your trigger. If you disable it, PhpStorm no longer will debug your code.
If you don't want to relay on an extension, you can go to your Xdebug config and set:
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
And "Start listening for PHP Debug Connections" will be your trigger.

Related

VSCode PHP debug in Launch currently open script that breakpoint cannot work

I use PHP debug of VSCode in the Launch currently open script mode.
When I use breakpoint to stop my PHP script, it will go to the end of my script.
These are my settings and my code:
I find the answer. If we want to debug in the cli mode, we have to configure xdebug in the /etc/php/7.2/cli/php.ini. Previously, I just set /etc/php/7.2/fpm/php.ini!
It is my setting:

Xdebug remote.host IP changed when VPN Is active

I'm using PhpStorm with a docker container on a MacBook (Sierra 10.12.5) as my dev environment. When I'm not connected to my company VPN, the PhpStorm debug connection in PhpStorm looks like this:
docker://docker.companyurl.com/docker-developer:latest/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=192.168.10.1 /opt/project/testscript.php
This works fine, breakpoints are working and everything is good.
However, once the VPN connects (which I need to do if I'm accessing internal databases), the connect string changes to:
docker://docker.companyurl.com/docker-developer:latest/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=10.75.5.4 /opt/project/testscript.php
No breakpoints get triggered and the scripts completes without any debug session starting.
Does anyone know why the IP address changes? I've tried manually specifying the correct IP in the Xdebug options within PhpStorm, but instead of overriding the command line option it duplicates it, and it still doesn't break.
I am not able to work productively as long as this goes on, so any help is appreciated!
Apparently the problem is that phpstorm tries to read the IP address and other vars at run time-- if you're on the VPN it picks up your VPN-assigned IP instead of the actual IP.
So what appears to be a solution at the moment is:
1) open phpstorm preferences / Languages and Frameworks / PHP / Debug / Advanced Settings. Uncheck the box for "Pass configuration options through command line"
2) open phpstorm preferences / Languages and Frameworks / PHP, then click the ... near the PHP CLI interpreter. Click the ... near the Additional / Configuration Options item, and add a new param for xdebug.remote_host with a value of 192.168.10.1

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.

How to debug PHP CLI scripts from the CLI

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. ;)

How to run to breakpoint in xdebug in sublime text 2 (without adjusting the xdebug URL settings)?

I have xdebug installed and working sublime text 2 and PHP 5.3.26 on OS X 10.8.5. I am able to set a breakpoint and "start debugging." I am able to run to a breakpoint by adjusting the settings in the xdebug menu, going to settings-default and adjusting the json to tell xdebug to automatically open a browser that opens the URL of the file I have open in sublime text.
But how do I run to a breakpoint without opening up the menus and changing the settings? I don't want to go fiddle with the internal xdebug settings every time I run a new file.
The docs say to open the "debugger control menu" but I don't know where that is or how to open it. Where do I click? How do I run to a breakpoint without adjusting the menu?
I think that you can use the shortcut
Ctrl+Shift+f5:
Run to next breakpoint
More information can be gathered here:
https://github.com/Kindari/SublimeXdebug

Categories