2

I am trying to get XDebug working with Visual Studio Code on Windows 10. I have Apache/PHP server running on my local machine (Apache/2.4.41 (Win64) PHP/7.3.9RC1). It works without any problem. I have Visual Studio Code running on the same machine. I've installed PHP XDebug 1.13 in it. I have a small test script in the htdocs directory. I set a couple of breakpoints and launch the debugger and get this in the Debug Console...

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

I then switch to Chrome which, at that moment, has an arrow-hourglass so I can tell something is trying to work. I refresh the page and the debugger does not stop at the breakpoints.

I've read through a bunch of posts already. Here's the relevant entries in my php.ini file...

[XDebug]
xdebug.stopOnEntry = true
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port=9001
xdebug.remote_connect_back=1
xdebug.remote_enable = 1
xdebug.remote_autorestart = 1
zend_extension = "d:/php/ext/php_xdebug-2.7.2-7.3-vc15-x86_64.dll"

Here's my current launch.json file...

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "hostname": "127.0.0.1",
            "log": true,
         },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9001,


        }
    ]
}

I've tried:

  • Turning off the firewall
  • Changing the port number
  • Using "localhost" or 127.0.0.1 in both the URL and php.ini (I've made sure to always reboot Apache after making any php.ini file changes.)
  • I've gradually added more settings (which you can see in the php.ini and the launch.json but the behavior hasn't changed...launching the debugger causes an arrow-hourglass for about ten seconds. I keep refreshing the webpage but it just runs through the script without stopping.

Here's my test script...

<?php
$a = 3;
$b = 19;
$c = $a + $b;
echo "Answer: $c";
?>

I, obviously, expect the debugger to stop at the first breakpoint (which is on the line "$a = 3;" It doesn't. I've tried adding more breakpoints. The browser goes straight to the answer "Answer: 22"

One other thing, I tried entering echo "1" in the prompt at the bottom of the Debug Console and got this:

echo "1"
Cannot evaluate code without a connection
-> evaluateRequest
{ command: 'evaluate',
  arguments: { expression: 'echo "1"', context: 'repl' },
  type: 'request',
  seq: 3 }

So I'm guessing that the web server is not connecting. I've checked the error.log file but there are no errors...no indications of anything related to debugging or connections.

NOTE: I opened inbound and outbound ports on the firewall and ran netstat -ano and verified that port 9001 is being listened on...

 TCP    127.0.0.1:9001         0.0.0.0:0              LISTENING       16144

Any help would be MUCH appreciated!!

Gregg Seipp
  • 173
  • 2
  • 11
  • 1) Check `phpinfo()`; output to ensure that Xdebug is there and uses correct settings (check it via web page -- same way as you planning to debug the code) 2) Enable Xdebug log and see what it will write there (if it tries to connect at all) – LazyOne Aug 26 '19 at 00:41
  • In phpinfo()... This program makes use of the Zend Scripting Language Engine: Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans In xdebug.log,.. [20356] Log opened at 2019-08-26 01:36:50 [20356] I: Checking remote connect back address. [20356] I: Checking header 'HTTP_X_FORWARDED_FOR'. [20356] I: Checking header 'REMOTE_ADDR'. [20356] I: Remote address found, connecting to ::1:9001. [20356] E: Time-out connecting to client (Waited: 200 ms). :-( [20356] Log closed at 2019-08-26 01:36:50 [20356] – Gregg Seipp Aug 26 '19 at 01:37
  • I seem to have found the solution in this post: https://stackoverflow.com/questions/11563280/php-remote-debugging-xdebug-cant-connect-to-jetbrains-php-storm-client The clue was the ::1:9001 line. I googled and found someone that suggested "localhost" wasn't being resolved. I do have localhost in my hosts file with the proper IP address, along with 2 more virtual servers. When I tried xdebug.remote_host = "127.0.0.1" in php.ini this time it worked.. The bad news is I need to use the virtual host names. So the question now is, why isn't it finding the IP address from my hosts file? – Gregg Seipp Aug 26 '19 at 01:55
  • 1) `::1:9001` is IPv6 for localhost and `127.0.0.1` is IPv4 for the same. In your OS setup IPv6 seems to have priority over IPv4; 2) You have `xdebug.remote_connect_back=1` which means that the value from `xdebug.remote_host` is IGNORED or only used if no IP detected from request headers. If you need to use "virtual host names" then disable "connect_back" option. – LazyOne Aug 26 '19 at 08:15
  • I did remove the xdebug.remote_connect_back=1 but still can't use the virtual host. Perhaps I should disable IPv6? – Gregg Seipp Aug 26 '19 at 11:22
  • Try that .. or make IPv4 as priority over v6, if there is a way of specifying address resolving. From your `netstat` output your VSCode listens on IPv4 only .. so it needs to use IPv4 addresses anyway. *"why isn't it finding the IP address from my hosts file"* If the host name is written in actual `hosts` file then it should take it from there and not from any DNS server, even local (at least that's how stuff normally works on Windows). – LazyOne Aug 26 '19 at 12:15
  • Try `ping your.domain.name` and see to what address it resolves. – LazyOne Aug 26 '19 at 12:19
  • I do seem to have it working. I installed it on a second machine but ONLY got it working once I installed the Chrome XDebug Helper (I'd had it installed on the first machine as well). I have no idea why that works. – Gregg Seipp Aug 28 '19 at 00:36
  • i have exactly the same problem – kenjiro jaucian Dec 04 '20 at 01:33

1 Answers1

4

Here's the launch.json I used to get this working on my Windows + Windows Linux Subsystem (Ubuntu) setup.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,                
            "stopOnEntry": true, // Once working, comment out this line
            "pathMappings": {
                // eg your web files in C:\Path\To\Code
                "/mnt/c/Path/To/Code": "${workspaceFolder}"
            },
            "log": true            
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
} 

and my xdebug.ini is:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
Silas Palmer
  • 2,687
  • 1
  • 29
  • 30
  • https://stackoverflow.com/users/1675523/chudasamachirag made the point that above .ini is for xdebug2, xdebug.ini for xDebug3 is: `zend_extension=xdebug.so` `[Xdebug]` `xdebug.mode=debug` `xdebug.start_with_request=yes` [1]: https://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_autostart – Silas Palmer Feb 23 '22 at 23:11