Hollands Spoor

The Notebooks

Debugging on Demand

When developing on a XAMPP stack, I find myself switching Xdebug on and off because of Xdebug’s performance penalty.

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

I work in a Windows environment so to that end, 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 thereby injects a block of Xdebug directives in the PHP.ini file.

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 is also 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 only for debugging sessions, a typical approach is:

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

   – one to run `c:\xampp\apache_debug.ps1` (before debugging starts),

   – one to run `c:\xampp\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 can 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 debug-session.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *