How does Xdebug know about breakpoints in an IDE? - php

I am a big proponent of Xdebug and love PhpStorm. I have worked with many different setups for local environments and configuration seems to be a bit different each time.
My current environment is a docker container running on my local machine. I understand that PhpStorm is listening to 0.0.0.0, aka - to all network interfaces, and on a specified port (I'm using 9004). I also see that I can ping my local machine from the docker container hosting Xdebug.
What I don't understand is: when I put a breakpoint in PhpStorm, how does Xdebug know that it exists, so that when it hits that line of execution it should tell PhpStorm to stop?

When Xdebug first connects to the IDE, the IDE sends over a list of breakpoints that it wants Xdebug to interrupt PHP at. If you have a look at a log file (made with xdebug.log=/tmp/xdebug.log), then you will see commands being send starting with breakpoint_set. There is then a type (-t), often line for a file/line breakpoint, as well as the file name and line number.
When PHP runs code, Xdebug intercepts the execution, and when it sees a match filename/line number combination, it pauses the execution and goes into command mode so that the IDE can send commands to inspect the current scope (by sending content_get) as well as further information about variables (with property_get). Then when you click "Continue" or "Step Over", the IDE sends a command to Xdebug to do the equivalent action.
To see a full description of the protocol, give the DBGp protocol specification a read.

Related

With an HTML form, how can I start debugging the action PHP script with proper GET/POST data? (using PhpStorm and XAMPP)

I'm using PhpStorm 2021.1.3 as an IDE with XAMPP as a PHP7.4 engine. I have installed Xdebug 2.9.2, and it works properly since I can start the debugger on specific PHP scripts.
My issue is that I have an HTML form in a PHP webpage (let's call it "mainpage.php", which sends its data to another PHP file (let's call it "analyze.php").
The issue is that if I start the debugger on that other PHP file ("analyze.php"), it doesn't get the GET/POST data, since I didn't use the form at all. Is there a way to pass the GET/POST data when launching the debugger?
I've looked into the run configurations, but they only seem to mention PHP options, and not GET/POST data.
I tried using the HTTP Request, but the only examples I've seen use actual web hosts and production environments, as far as I could tell. I only have this XAMPP development environment at the moment. My PHP files are not in the www folder of XAMPP, since PhpStorm seemed to have no problem so far copying the file at the right place when needed.
Any idea where I should look for?
Ok, thanks to #waterloomatt I managed to find a workaround. The crux of the issue in my case is this PhpStorm bug: https://youtrack.jetbrains.com/issue/WI-54542 Therefore, this answer will probably be irrelevant once this bug is resolved.
Here's the solution on Linux :
Step 1 : Create a server on PhpStorm.
In File->Settings->PHP->Servers, click on the "+" symbol to add a new server. Fill the server infos.
The port is 63342, which is the port PhpStorm uses for running the code. Note that I gave this server the name "localhost63342". This detail is important.
Step 2 : Launch XAMPP normally.
Launch XAMPP as you do normally. The issue is with PhpStorm, not XAMPP.
Step 3 : Before launching PhpStorm, add an environment variable
Open a command prompt, and run the following command:
export PHP_IDE_CONFIG=serverName=localhost63342
Note the server name, which is the same as the name of the server we defined in PhpStorm.
I could make that environment variable more permanent, but since it is a soon-to-be-resolved PhpStorm bug, I think that'll do.
Step 4 : In the same command prompt, launch PhpStorm
We defined an environment variable, which is valid as long as the command prompt remains open. We then can launch PhpStorm with this command prompt. In my case, with the following command:
./$HOME/phpstorm/PhpStorm-203.7148.74/bin/phpstorm.sh
Step 5 : Launch a debugging session
My Chrome browser already has the "Xdebug helper" extension: https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=en That extension is already configured to use the PHPSTORM IDE Key, as shown here:
In PhpStorm, I put a breakpoint where I want in my code. I click the button to make PhpStorm listen for PHP Debug Connections. I then launch the code from the page I want using the launcher button (Chrome is Alt+F2 on my computer).
The code runs normally, and I can navigate from page to page. When, on a specific page, the code hits a PHP breakpoints, the code is suspended, and I can execute it line by line using PhpStorm's debugger. Since I filled the forms to get to this page, I get the GET/POST data.
It's not as direct as a PHP HTTP Request I suppose, but it works.

Cannot examine breakpoints in PhpStorm with Xdebug

I have a simple Apache + PHP 7.1 web server running locally.
I've got Xdebug helper installed in Chrome, and I click "Debug" in that.
I've got Xdebug 2.9.2 extension running successfully in PHP, with correct output from phpinfo();
When I run "validate" in PhpStorm I get green ticks for all but the last item, "Debug protocol '' is not supported", which has a yellow triangle.
I set a debug point in my code, and hit the green bug in the toolbar to begin a debugging session, and the only thing that's output in the Debugger "Variables" pane in PhpStorm is "Waiting for incoming connection with ide key '13508'" (this number changes every time).
Within php.ini I have set xdebug.idekey="PHPSTORM", and I've mirrored this in my Chrome Xdebug Helper.
I am not running Zend Debug.
I would really like to be able to debug properly. Any help is greatly appreciated.
Accordingly to your Xdebug log, you already have some service running on TCP 9000 port and that does not seem to be PhpStorm. Most likely it's php-fpm (it also uses the same port by default).
You can check that with netstat or alike (e.g. sudo lsof -nP -iTCP -sTCP:LISTEN if you are on Mac) -- it can show associated process ID (or even process name straight away).
Solution: change Xdebug port in both php.ini and PhpStorm to be some another number -- e.g. 9001 or so; restart web server once done.

Debug CLI PHP script in PhpStorm, cannot establish connection to Xdebug 2.6.0

I'm trying to run a CLI script in PhpStorm and use the debugger to step through it.
It took some doing, but I now have Xdebug installed and the PhpStorm CLI Interpreters dialog confirms that:
I created a Run/Debug config and when I try to execute/debug it, the script runs all the way through without breaking at my breakpoints and I get this error at the bottom of PhpStorm:
The PhpStorm docs say that xdebug for CLI scripts can configured with an environment variable like this:
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
I've confirmed that PhpStorm sees that environment variable:
Here is my Run/Debug config:
I'm at a complete loss here. How the heck can I get this thing to debug a CLI php script? Any help is appreciated.
Gah, I hate it when I spend a couple of hours trying to fix something and discover it's totally unrelated.
The problem was that I'm using short tags and my cli php.ini file had short_open_tag=Off. The code I'm running is normally run in a web context and the php.ini for that web context had short_open_tag=On so the code was working.
This code is an api endpoint and I needed to be able to step through and see what was going on and there's nothing that inherently requires it to run in the web context (except for the input params, but I'm faking those for testing purposes anyway).
So, my debug configuration was fine.
I discovered it by trying to reduce the problem further and further and I just tried running the code directly from a command line (php sync.php) to see that it was at least working and producing the correct output.
It wasn't producing the normal api json output, it was just echoing all of the code in the file. That's what led me down the path to realizing my stupid mistake.

PHPStorm local debugging with xdebug never catching and can't validate

I can't seem to use xdebug in PHPStorm when running a local server. The server document root is the direct parent of the PHPStorm project root and I can run the project on a web browser and through the "Tools | Test RESTful Web Server" option.
I:
am running my Apache server on a mac (OSX 10.10.5) and used brew to install php 5.5 and xdebug 2.3.3 (which was honestly a huge hassle, as nothing wanted to work). The xdebug shows up properly in my phpinfo() and when I use the terminal command $ php -i so I think it is installed correctly.
have followed several guides, tutorials, and SO questions that either do not pertain to my situation and/or their answers were non-working or absent
Configuring Xdebug (PHPStorm) and may more from the PHPStorm website...
Connection Between PHP and Xdebug (SO) this one was the closest I could find to my situation and it did not resolve my issue either. All the other ones I could find were not very helpful because they were working with the MAMP stack.
can't validate my xdebug with PHPStorm in "Run | Web Server Debug Validation". Everytime I try, no matter how I fiddle with the settings, it is either a connection refused error (which I believe is because I am trying unopened ports) or this:
Web Server Debug Validation returns unexpected format error.
Both the bookmarklets that XDebugger supplies on their website and the browser extensions fail as well.
The weird thing to me is, if I create a virtual host in the apache httpd.conf with the servername as my external IP address and with the same document root, I can access it (not the weird part) but I can also run the debugger perfectly fine through it. This is undesirable as it seems that anyone can now run my code and receive responses with sensitive information. They might also be able to start my debugger, but that would be more of an annoyance if they could.
I have been able to, up until this point, work without a debugger, but there have been instances where it would have been great to have one. Looking toward the future I can say for certain that having a debugger will help immensely with my projects, so any help would be greatly appreciated.
I would also prefer not to reinstall everything as it was a huge pain to set it all up in the first place, but if it's a last resort I may have to.
I can provide more information if needed.
Thanks,
Jacob
I managed to get the step debugging to work with a couple changes:
For the validation test, instead of just localhost I attempted to validate a sub-folder that contains my code for the project. The PHPStorm project itself was set to this folder. I think that because my document root was outside of the project that it refused to validate the xdebug extension.
I also configured my DBGp Proxy under Tools to accept the IDE key that xdebug had set as default and updated the IDE key in the xdebug bookmarks.
I can now step debug with Run > Start Listening for PHP Debug Connections and in-browser using the XDebug Debugger bookmarks.
Jacob
In addition to the accepted answer, you need to download and run the debug proxy on the server itself
Download an install the Python Remote Debugging package from Komodo (http://code.activestate.com/komodo/remotedebugging/)
Start the DBGp proxy on the web server.
In PHPStorm, start listening for the debugger: "Run | Start Listening for PHP Debug Connections" (You may need to register the debug server: "Tools | DBGp Proxy | Register IDE")
Jet Brains how-to: https://confluence.jetbrains.com/display/PhpStorm/Multi-user+debugging+in+PhpStorm+with+Xdebug+and+DBGp+proxy#Multi-userdebugginginPhpStormwithXdebugandDBGpproxy-2.DBGpproxy
None of the suggestions worked for me, so I kept digging.
Even though IPV6 was disabled on my network adapter, it is still a preference on the loopback adapter.
A ping localhost would result in Reply from ::1: time<1ms
I've fixed this by preferring IPV4 over IPV6 in the Windows registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCPIP6\Parameters
Set DisabledComponents to Hex 0x20 or decimal 32.
Now ping localhost comes back as Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 and the debugger validation works fine.

How to debugging php on a VPS using phpStorm and Xdebug?

I have trying to get this done for days...and reading through about 20 tutorials, documents, etc...but still no luck.
Here is the thing. I installed wordpress(just for example, can be any php program) on my VPS, and hoping to debug it from my macbook and my desktop. This is clearly a remote debugging thing. So I here is what I done:
1.Installed right version of xdebug and located its .so file on VPS.
2.Changing php.ini and 20-xdebug.ini file on VPS, my settings currently are:
zend_extension=/usr/lib/php5/20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9200
xdebug.show_local_vars=0
xdebug.var_display_max_data=10000
xdebug.var_display_max_depth=20
xdebug.show_exception_trace=0
xdebug.remote_log=/var/log/xdebug.log
;xdebug.remote_host=202.84.93.66
The last line, which is comment out is my desktop IP address, where I run phpStorm. Based on xdebug's documents, if you set remote_connect_back=1, you won't need this host ip to be specifici.
On phpStorm Side, I "start new project from existing code, and specificy the source root on VPS, map it with one of my local dest. PhpStorm just downloaded all the files and after setting up "Automatic upload", the sync is perfect.
On phpStorm side, I specificy the PHP intercepter as the remote one on my VPS.
On phpStorm side, I changed my debugging port to 9200, as same as the one I using on VPS.
Using the booklet method phpStorm provide, I put them on my firefox bootkmarklet.
Click listen button on phpStorm, set breakpoint, go to firefox open the page, click 'start debuging', refresh.....BUT NOTHING HAPPENS!
I also tried to using the tranditional methods, which set a run configration way as both php-webapplication or php-remote-debug...but still not working.
At php storm, the web debug validation, I could pass all the testing there though....
Can anyone help me with this? I think I must be doing wrong on some very fundation part since I saw all the tutorial are so simple set, but this already took me about 3 days to figure out....
Thanks!

Categories