xdebug / vscode-php-debug

PHP Debug Adapter for Visual Studio Code 🐞⛔
MIT License
763 stars 178 forks source link

Feature Request: Auto Attach Filter setting #957

Closed trymeouteh closed 3 months ago

trymeouteh commented 3 months ago

The JavaScript Debug extension has a setting named "Auto Attach Filter". It allows you to choose from one of the three settings

Having this setting in PHP Debug can allow a user to run a PHP script in the terminal, with custom arguements and have the debugger.

trymeouteh commented 3 months ago

I found a way around this.

I created these three PHP VSCode debug launch configurations. The PHP: Console one will allow you to run the currently opened PHP script in the VSCode debug console. The PHP: Terminal one will allow you to run any PHP script on your device using the terminal, when you start debugging it will wait for you to run the script and then start debugging, allowing you to use arguments within the script.

    "launch": {
        "configurations": [
            {
                "name": "PHP: Console",
                "type": "php",
                "request": "launch",
                "program": "${file}",
                "cwd": "${fileDirname}"
            },
            {
                "name": "PHP: Terminal",
                "type": "php",
                "request": "launch"
            }
        ]
    },

And this is what I added to my php.ini on the system

xdebug.mode = debug
xdebug.start_with_request = yes

I am using xdebug v3

The only minor downside to using the PHP: Terminal is that the outputs will only be in the terminal and not in the debug console. You can still use the debug console to change variable values for example, but any echo calls will appear in the terminal and not in the debug console.

zobo commented 3 months ago

Hi @trymeouteh sorry for not getting back to you earlier.

I did not have much time to look at how JS-debug's "Auto Attach Filter" works, but from the documentation it seems like it sets some environment variables in the terminal that it opens.

PHP/Xdebug work in similar but not completely same way.

It's good to understand that VSCODE IDE listens for incoming debug connections and PHP/Xdebug initiates the debug connection. It does so when it is correctly configured. The XDEBUG_MODE has to contain the "debug" switch and there must either be a xdebug.start_with_request setting or a trigger env variable XDEBUG_SESSION. See https://xdebug.org/docs/step_debug#activate-debugger-cmd

So if Xdebug is loaded, but not configured, you probably could get it to connect to the IDE if you set environment variables XDEBUG_MODE=debug and XDEBUG_SESSIONS=1.

There are options how this could be implemented, but I do not really see a benefit over using launch.json to start a program. Is there any reason you want to do it directly from the terminal?

Regarding the output. Yes, the stdout and strerr will be printed in the terminal, where the PHP script is executing. If you start the script via the launch.json program option the script will be executed by the php-debug extension and so it can capture the output and display it in the debug console.

There is also an option to redirect STDOUT of the script via a DBGP command, but this is not exposed in any way in the current php-debug extension. STDOUT redirection is not possible though...