Debugging Symfony2 Console Commands with XDebug and PHPStorm - php

How should I configure PHPStorm so that I can trace my code using XDebug when running console commands?
I know I have the debug configuration correct, because I can debug web requests and trace the code. I can also trace regular php command line scripts as long as I set an environment variable like this:
export XDEBUG_CONFIG="idekey=my-xdebug"
However, I am unable to trace/debug Symfony2 console commands (those run with app/console bundle:console_command). PhpStorm sees the connection, however, it can't seem to locate the code that is being run. I know my file mapping is correct because web requests work flawlessly.
Is this possible?

You should provide SERVER_NAME and SERVER_PORT. also you should enable xdebug.remote_autostart. Try this:
SERVER_PORT=<Your port> SERVER_NAME='<Your server> php \
-dxdebug.remote_autostart=On app/console test

Make sure you have enabled xdebug inside php.ini CLI version not only apache/cgi php.ini.
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM

I'm suggesting to try following BASH helper command:
https://github.com/torinaki/phpdebug-cli
phpdebug mostly autodetects all required configurations and you will not require to provide environment variables.
Just run:
phpdebug app/console bundle:console_command

Related

PHP can't connect to Xdebug client on Docker

I am using WSL2 with installed docker-ce with PHP 8 and Xdebug 3. After a lot of debugging I figured out that simply PHP can't connect to Xdebug client for whatever reasons. It works when I am using telnet. Logs are empty, not information at all why connection does not work.
I downloaded dbgpClient to make sure it's not IDE issue (PhpStorm) from Xdebug page. ./dbgpClient -1 started it like this. When I do telnet host.docker.internal 9003 inside the container i have:
Connect from 172.20.0.5:45144
However if I try running for example php8 -dxdebug.mode=debug -dxdebug.client_host=host.docker.internal -dxdebug.client_port=9003 -dzend_extension=xdebug.so -dxdebug.start_with_request=yes index.php it just doesn't work, it never says it connected. Any idea what's going on?
I already checked things like php -m, it says Xdebug is there.
I had to add XDEBUG_MODE="debug" environment variable, from docs:
string xdebug.mode = develop #
This setting controls which Xdebug features are enabled.
This setting can only be set in php.ini or files like 99-xdebug.ini that are read when a PHP process starts (directly, or through php-fpm), but not in .htaccess and .user.ini files, which are read per-request.
Source - https://xdebug.org/docs/all_settings#mode

Debugging with Xdebug for cli php on vagrant machine

I use a Centos vagrant machine on which the project resides. I have enabled synced folder to the project root folder so that I can edit directly on the machine from Windows. I use VSCode or NetBeans as editor.
Now how do I debug in this situation? The code is in PHP. Local machine does not have any environments like php/apache set up. Everything is on the vagrant machine. The php script is run on vagrant while providing parameters through command line, eg -
/path/index.php folderName scriptname function_name param1 param2 param3
I already installed xdebug on vagrant. Xdebug options in php.ini
zend_extension = /usr/lib/php/modules/xdebug.so
xdebug.remote_autostart = 1
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
Now how do i debug php scripts and see step by step what changes in VSCode or NetBeans? The php codes are cli. Tried several guides, no luck. The php files have no webpage view.
Just found that $ netstat -aon | grep 9000 returns nothing.
A simple Google search finds plenty of howtos and articles:
https://www.codewall.co.uk/debug-php-in-vscode-with-xdebug/
https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug
https://medium.com/#jaythedeveloper/ultimate-guide-debug-php-iis-in-visual-studio-code-using-xdebug-14fced013f22
https://jonathansblog.co.uk/remote-debugging-php-in-visual-studio-code-with-xdebug
https://www.cloudways.com/blog/php-debug-with-xdebug/

Using xdebug with PhpStorm

I can't get my PhpStorm IDE to work with xdebug.
My current setup is the following:
PhpStorm 2017.1.4
MacOS Sierra 10.12.5
Here are the steps that I followed.
I have installed php with the following command. I have added the postgres parameter because I need it later to connect to a PostgreSQL database.
brew install php71 --with-postgresql
The next step is to install XDebug with the following command
brew install php71-xdebug
So the next step that I got from the documentation (https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html) is to edit the php.ini file with the following content:
[Xdebug]
zend_extension="<path to php_xdebug.dll>"
xdebug.remote_enable=1
xdebug.remote_port="<the port for Xdebug to listen to>" (the default port is 9000)
xdebug.profiler_enable=1
xdebug.profiler_output_dir="<AMP home\tmp>"
Just some questions about those fields and XDebug.
So I guess XDebug is some kind of service that runs on the remote_port and that PhpStorm is using to write data to? Or do you have to specify the port where the application you want to test is running?
What exactly is the profiler thing? And can the output dir be anything I can choose?
So this is my php.ini file that I think it should be:
[xdebug]
zend_extension="/usr/local/Cellar/php71-xdebug/2.5.4/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/usr/tmp"
That's it for the php.ini. So I have to check the settings in my PhpStorm IDE. And those are the following:
So that is my setup. In my project I just have one index.php with <?php echo phpinfo(); ?> I just click the Chrome icon so it opens directly in the browser to check if XDebug is there. I see the following result.
So I thought to myself ok let's try some debugging. So I changed my index.php file to the following
$i = 2;
$j = $i + 2;
echo $j
I've put a breakpoint on the 2nd line. And when I run it, it never stops at the breakpoint. What is needed to do that or am I wrong in my configuration somewhere?
To make Xdebug remote debugging working on your page, you need to set a cookie in your browser, telling the server you want to debug the page, there are many extensions for that, the most known is Xdebug helper in Chrome.
I also suggest you to follow this tutorial Zero-configuration debugging, it is very detailed and clear IMO.
If you still have problems i'll try to help you with pleasure :)

xdebug in a laravel project in ubuntu 16.04 using sublimetext 3

I'm moreless new with this kind of things, I'm trying to setup xdebug to debug a laravel project I'm developing in my Ubuntu 16.04, I have installed sublime text with the package control and the xdebug client, I installed from apt-get the packages
php (7.0) php-xdebug php-all-dev php-fpm
the laravel project already works with the comand
php artisan serve
I saved the sublime text 3 project with the code
{
"folders":
[
{
"follow_symlinks": true,
"path": "."
}
],
"settings": {
"xdebug": {
"url": "http://localhost/",
}
}
}
my /etc/php/7.0/fpm/conf.d/20-xdebug.ini is:
zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port = 8000
xdebug.remote_log="/var/log/xdebug/xdebug.log"
I dont know if I need to use the "php artisan" to debug or just with the xdebug plugin in sublime text, nothing apear to work
any ideas?
thanks for everything
Here is my config for xdebug:
zend_extension=/usr/lib/php/modules/xdebug.so
xdebug.remote_enable=on
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
xdebug.remote_autostart=1
Note the remote_autostart, which made all my debugging work correctly.
I don't use sublime, but this config worked correctly in Atom, PhpStorm, VSCode and Netbeans, and I assume it will work virtually for all compatible debugger clients. Make sure that the port, host and idekey fields are the same in sublime and your config file, that's all.
The thing is, you have to know how xdebug works. Then you can set it up everywhere easily.
xdebug works but connecting to the remote_host and remote_port you specify in its config. That means, when a PHP script is going to be executed, first, if loaded, xdebug tries to connect to that address. If a compatible debugger is listening on the other side, then connection is made, and the debugger can do its debugging. Note that, xdebug, as in sockets terminology, is a client to your server (ide, debugger, ...). So your ide must be listening first, before PHP script is executed. In sublime, find something like listening for connections.
For debugging if problem is with your ide or xdebug itself, you can use debugclient, a tool that acts as a server for xdebug. Just run debugclient and execute a PHP script, with xdebug loaded (which seems you have it). If it shows that connection is made, than sublime has a configuration problem. If not, check xdebug config again, and make sure everything is ok.
I have seen systems where debugclient is not installed by default. You can also test with nc -l 9000 or whatever port to test it too.

PHPStorm 10, XAMMP, Xdebug/ZendDebugger

Over the past few days I have been trying to get debugging working with PHPStorm 10 on my local machine (Mac OS). I have tried both ZendDebugger and xdebug.
I seem to be hitting the same problem with both. I setup my interpreter and it says that the correct Debugger is installed on XAMPP but then when I run the "Validate Debugger Configuration on Web Server" tool in PHPStorm it gives the error: No debug extension is loaded.
The following is my php.ini file settings for Xdebug:
[Xdebug]
zend_extension=/Applications/XAMPP/etc/xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir=/Applications/XAMPP/xamppfiles/htdocs/sam-files/
xdebug.idekey=PHPStorm
I have also been using the following recourses online:
https://www.jetbrains.com/phpstorm/help/configuring-xdebug.html
https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm
Any guidance on this would be great.
Thanks,
Sam
Check if the correct extension are loaded using the following command in the PHPStorm terminal
php -i | grep xdebug
You might want to check to make sure you're loading the correct php.ini file as well, by using
php -i | grep ini

Categories