Search code examples
phpvisual-studio-codexdebug

Xdebug + VSCode on Ubuntu sees strange value for global supervariables


I am a PHP newbie and trying to install Xdebug for debugging. Following is my setup:

  • PHP 8.1
  • Ubuntu 22.04
  • Visual Studio Code

This is the result of command php -v:

PHP 8.1.2 (cli) (built: Jul 21 2022 12:10:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

And the launch config for running debug in VSCode:

{
    // 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": 9003
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000,
            "runtimeExecutable": "/usr/bin/php",
            "pathMappings": {
                "/var/www": "${workspaceFolder}"
            }
        }
    ]
}

Xdebug seems to be working since I can run step in PHP code, however, super globals have strange value, different from accessing from web browsers, for example, $_SERVER:

From Firefox localhost:

[HTTP_HOST] => localhost[HTTP_USER_AGENT] => Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0  
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8 
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5 [HTTP_ACCEPT_ENCODING] => gzip, deflate, br 
[HTTP_CONNECTION] => keep-alive 
[SERVER_SIGNATURE] =>Apache/2.4.52 (Ubuntu) Server at localhost Port 80
[SERVER_SOFTWARE] => Apache/2.4.52 (Ubuntu) 
[SERVER_NAME] => localhost 
[SERVER_ADDR] => 127.0.0.1 
[SERVER_PORT] => 80 [REMOTE_ADDR] => 127.0.0.1 
[DOCUMENT_ROOT] => /var/www/html
....

However, this if the $_SERVER from Xdebug:

[GJS_DEBUG_TOPICS] => JS ERROR;JS LOG
[LANGUAGE] => en_US:en
[USER] => viet
[LC_TIME] => en_CA.UTF-8
[XDG_SESSION_TYPE] => x11
[SHLVL] => 0
[HOME] => /home/viet
[DESKTOP_SESSION] => ubuntu
[GIO_LAUNCHED_DESKTOP_FILE] => /usr/share/applications/code.desktop
[GTK_MODULES] => gail:atk-bridge
[GNOME_SHELL_SESSION_MODE] => ubuntu
...
[VSCODE_AMD_ENTRYPOINT] => vs/workbench/api/node/extensionHostProcess
[VSCODE_HANDLES_UNCAUGHT_ERRORS] => true
[XDEBUG_MODE] => debug,develop
[XDEBUG_CONFIG] => client_port=40151
[PHP_SELF] => /var/www/html/pages/automation/index.php
[SCRIPT_NAME] => /var/www/html/pages/automation/index.php
[SCRIPT_FILENAME] => /var/www/html/pages/automation/index.php
[PATH_TRANSLATED] => /var/www/html/pages/automation/index.php
[DOCUMENT_ROOT] => 
[REQUEST_TIME_FLOAT] => 1659989701.6132
[REQUEST_TIME] => 1659989701

Please notice that DOCUMENT_ROOT is empty here. It seems that it parse directly from my Linux global variable, not the localhost super variables?

May I know how can I fix this one so I have the correct super variables?

[Update]

As @LazyOne mentioned, I should use the "listen to Xdebug" config. However, when I run it, VS code keep listening without jump to breakpoint even after I browse the page by Chrome with Xdebug helper extension. Following is some photos: VS code keep listening

Debug extension is running

and the content of my /etc/php/8.1/cli/php.ini:

zend_extension="xdebug.so";
xdebug.remote_autostart=1
xdebug.remote_log=/var/log/apache2/xdebug.log
xdebug.default_enable = 1
xdebug.remote_enable=1
xdebug.remote_port=9003
xdebug.idekey = VSCODE
xdebug.mode=debug
xdebug.start_with_request = yes

Solution

  • Apparently, as I have xdebug options in file /etc/php/8.1/cli/php.ini, only cli sessions will work correctly. The resolution is to add same options to apache2 init file at /etc/php/8.1/apache2/php.ini