Hollands Spoor

The Notebooks

Tag: VSCode

  • Debugging on Demand

    When developing on a XAMPP stack, I find myself switching Xdebug on and off because of Xdebug’s performance penalty. When not debugging, i switch Xdebug off because it slows down PHP.

    There are good recommendations on how to improve Xdebug’s performance, e.g. here or here. However i prefer to have the Xdebugger boldly switched off unless explicitly needed.

    Working in a Windows environment, i wrote two batch files that I place inside my XAMPP directory next to XAMPP’s native apache_start.bat and apache_stop.bat.

    The first script starts apache with the debugger activated: apache_debug.ps1. It does so by injecting block of Xdebug directives in the PHP.ini file before it starts up Apache.

    The second script restarts Apache without the debugger. Here the Xdebug directives in PHP.ini are commented out before Apache is started.

    For convenience, there I also have a third script to check the current Xdebug mode.

    The scripts also work for running Apache as a service.

    How to Install:

    Copy the three scripts to your XAMPP installation, typically c:\xampp. If your XAMPP is in another location, change the $xamppDir declaration at the start in each of the three scripts to what’s applicable.

    How to use:

    Run the scripts from a terminal.

    How to improve:

    Here is an optional VS Code integration:

    If you want VS Code to auto-toggle your Xdebug mode when entering and leaving a debugging session, a typical approach is:

    1. Add two shell tasks in `.vscode/tasks.json`:

       – one to run `apache_debug.ps1` before debugging starts,

       – one to run `apache_restart.ps1` after debugging ends.

    2. Reference those tasks from your PHP debug configuration in `.vscode/launch.json` using:

       – preLaunchTask

       – postDebugTask

    Example `.vscode/tasks.json`

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "xampp:enable-debug",
          "type": "shell",
          "command": "powershell",
          "args": [
            "-ExecutionPolicy",
            "Bypass",
            "-File",
            "c:\\\\xampp\\\\apache_debug.ps1"
          ],
          "problemMatcher": []
        },
        {
          "label": "xampp:disable-debug",
          "type": "shell",
          "command": "powershell",
          "args": [
            "-ExecutionPolicy",
            "Bypass",
            "-File",
            "c:\\\\xampp\\\\apache_restart.ps1"
          ],
          "problemMatcher": []
        }
      ]
    }

    Now these tasks will be called during launch of the debugger:

    Example `.vscode/launch.json`(PHP Listen)

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Listen for Xdebug",
          "type": "php",
          "request": "launch",
          "port": 9003,
          "preLaunchTask": "xampp:enable-debug",
          "postDebugTask": "xampp:disable-debug"
        }
      ]
    }

    Now Xdebug starts and stops in accordance to your VSCode Xdebugger-session.