1

XDebug stopped working in VSCode after I upgraded my Mac M1 to Monterey 12.6 and VSCode to 1.71.1.

When I start the debug session with "Listen for Xdebug" selected, VSCode just seems to hang, showing the debug control bar with a the pause button (||), the restart, and the stop buttons (no Step In or other buttons enabled). I've tried moving to an earlier version of VSCode (that had worked prior to the upgrades of MacOS and VSCode), uninstalling/reinstalling Xdebug extension, restarting VSCode, and restart MacOS. I've confirmed with NMAP that 9003 is listening when VSCode is hung in debug mode (it stops listening when I stop debugging).

I can get PHP scripts to run in VSCode if I select "Launch currently open script" but then I can use any breakpoints.

Here are the contents of my xdebug.ini:

zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9000
xdebug.log=/tmp/xdebug.log
xdebug.client_host=127.0.0.1

Here are the other results from xdebug_info():

Feature => Enabled/Disabled
Development Helpers => ✔ enabled
Coverage => ✘ disabled
GC Stats => ✘ disabled
Profiler => ✘ disabled
Step Debugger => ✔ enabled
Tracing => ✘ disabled

                            Optional Features                            
Compressed File Support => yes (gzip)
Clock Source => clock_gettime_nsec_np

                              Diagnostic Log                              
No messages

                              Step Debugging                              
Debugger is active
Connected Client => 127.0.0.1:55058

DBGp Settings
Max Children => 100
Max Data => 1024
Max Depth => 1
Show Hidden Properties => No
Extended Properties => Yes
Notifications => Yes
Resolved Breakpoints => Yes
Breakpoint Details => No

                                   PHP                                   
                           Build Configuration                           
Version (Run Time) => 8.1.10
Version (Compile Time) => 8.1.10
Debug Build => no
Thread Safety => disabled
                                 Settings                                 
Configuration File (php.ini) Path => /opt/homebrew/etc/php/8.1
Loaded Configuration File => /opt/homebrew/etc/php/8.1/php.ini
Scan this dir for additional .ini files => /opt/homebrew/etc/php/8.1/conf.d
Additional .ini files parsed => /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini,
/opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini


Directive => Local Value => Master Value
xdebug.mode (through XDEBUG_MODE) => debug,develop => debug
xdebug.start_with_request => yes => yes
xdebug.start_upon_error => default => default
xdebug.output_dir => /var/tmp/ => /var/tmp/
xdebug.use_compression => 1 => 1
xdebug.trigger_value => no value => no value
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.log => /tmp/xdebug.log => /tmp/xdebug.log
xdebug.log_level => 7 => 7
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3
xdebug.max_nesting_level => 256 => 256
xdebug.cli_color => 0 => 0
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.max_stack_frames => -1 => -1
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.profiler_append => Off => Off
xdebug.cloud_id => no value => no value
xdebug.client_host => 127.0.0.1 => 127.0.0.1
xdebug.client_port => 55058 => 9000
xdebug.discover_client_host => Off => Off
xdebug.client_discovery_header => no value => no value
xdebug.idekey => no value => no value
xdebug.connect_timeout_ms => 200 => 200
xdebug.scream => Off => Off
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_return => Off => Off

Here's the output of php -v

PHP 8.1.9 (cli) (built: Aug  4 2022 14:00:20) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
    with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies

Finally, here are the contents of my launch.json:

{
  // 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,
      "log": true
    },
    {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 0,
      "runtimeArgs": [
        "-dxdebug.start_with_request=yes"
      ],
      "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
      }
    },
    {
      "name": "Launch Built-in web server",
      "type": "php",
      "request": "launch",
      "runtimeArgs": [
        "-dxdebug.mode=debug",
        "-dxdebug.start_with_request=yes",
        "-S",
        "localhost:0"
      ],
      "program": "",
      "cwd": "${workspaceRoot}",
      "port": 9000,
      "serverReadyAction": {
        "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

Any help is appreciated.

Bubba
  • 71
  • 6
  • I was helping a new member on our team get up and running with VSCode and Xdebug on Windows and he is encountering the same problem. He can run code with "Launch currently open script" but no breakpoints that he's set are hit. If we set it to Listen for Xdebug, VSCode displays the debug panel but the script is never run. Is it possible that a recent change broke the integration between VSCode and Xdebug? – Bubba Sep 16 '22 at 16:39
  • 1) *"Here are the other results from `xdebug_info():`"* How did you get it? Is that from a script executed in CLI or a web page? It has to be a web page (as you may have different php.ini used in CLI and web server). 2) Overall it looks OK to me at a quick glance. 3) When you use "Listen for Xdebug", open your OS terminal/console and check if VSCode is listening on that `9003` port. For Windows that would be `netstat -abn` or similar to that. Or Mac something like `sudo lsof -nP -iTCP -sTCP:LISTEN`. Main point: it has to be VSCode that listens on that port and not another app/service. – LazyOne Sep 18 '22 at 13:53
  • Thank you, @LazyOne. I got the xdebug_info() results from a script I ran in VSCode. I'm not running the php code in a web server, only in VSCode. The PHP code is performing a migration from one website to another and all runs on a local system (e.g., my laptop). The results of `lsof` are: `php-fpm 27336 Bubba 8u IPv4 0x789ad1badcbef9f7 0t0 TCP 127.0.0.1:9000 (LISTEN)` `php-fpm 27337 Bubba 9u IPv4 0x789ad1badcbef9f7 0t0 TCP 127.0.0.1:9000 (LISTEN)` `php-fpm 27338 Bubba 9u IPv4 0x789ad1badcbef9f7 0t0 TCP 127.0.0.1:9000 (LISTEN)` – Bubba Sep 20 '22 at 02:52
  • *"I'm not running the php code in a web server, only in VSCode. The PHP code is performing a migration from one website to another and all runs on a local system (e.g., my laptop)."* But is it accessed via a **web page / browser** or in a **CLI only**? *"The results of lsof"* I see nothing for Xdebug `9003` port. Did you run it when VSCode supposed to listen? I only see php-fpm that uses `9000` default port (the default port of Xdebug v2) – LazyOne Sep 20 '22 at 08:10
  • It is not accessed via a web page / browser, only from VSCode for REST, SQL, etc. I did notice that the port was 9000 when I pasted. That was the result of more troubleshooting after I'd posted my question. I've changed the config & confirmed that the ports match between launch.json and what I see in lsof. Still doesn't work. I've un/reinstalled VSCode (and all config) and reinstalled xdebug but still no luck. Is there a log in VSCode that would show what the debug is attempting to do (when it is hung)? Thanks again, LazyOne. – Bubba Sep 21 '22 at 12:23
  • I'm not using VSCode myself (happy PhpStorm user here), using it for testing only so cannot give detailed instructions. But https://github.com/xdebug/vscode-php-debug#troubleshooting says _"Set `log": true` in your launch.json and observe Debug Console panel"_ – LazyOne Sep 21 '22 at 12:39
  • *"I've changed the config & confirmed that the ports match between launch.json and what I see in lsof"* Can you show the output when 9003 port is used? It's hard to suggest something if you do not know what is actually happening on the other end. – LazyOne Sep 21 '22 at 12:41
  • @LazyOne, I've updated my original question with the latest xdebug_info and config information. One of the things I noticed when I turned "log": true is the following: `Listening on { address: '::', family: 'IPv6', port: 9000 }`, which I saw you mention in response to someone else's question. I'm looking into how to change that to IPv4. – Bubba Sep 21 '22 at 15:00
  • 1) I say do not use 9000 as it conflicts with php-fpm that you have. better stick to 9003. 2) It listens on IPv6. Not on IPv4. This might be the reason. 3) *"Connected Client => 127.0.0.1:55058"* Xdebug info says it's connected.... – LazyOne Sep 21 '22 at 15:19
  • @LazyOne, I made the changes you suggested. It still hangs when the debug configuration is set to "Listen for Xdebug". However, breakpoints are now working when it is set to "Launch currently open script". I have to confess that I don't know what it was set to when it was previously working. I do know that breakpoints were not working in the "Launch currently..." configuration before you had me make those changes. I have a working configuration now. Thank you very much for your help! How do I vote your answer correct? – Bubba Sep 22 '22 at 01:57
  • Better post your own answer with your current working settings/config -- it will be useful for other users in a similar situation. You can accept your own answer in a day of two (that depends on your rep). Accepted answer is more visible when searching/browsing though results. – LazyOne Sep 22 '22 at 12:57

1 Answers1

2

After a lot of help from LazyOne, here is the configuration that finally got the VSCode debugger to work with PHP on my MacBook Pro M1:

  1. Made sure that Xdebug was installed locally: pecl install xdebug

  2. Installed the following VSCode plugins (both from Xdebug):

PHP Debug
PHP Extensions
  1. Configured /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini:
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.client_host=127.0.0.1
  1. Configured launch.json 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,
      "log": true
    },
    {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 0,
      "runtimeArgs": [
        "-dxdebug.start_with_request=yes"
      ],
      "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
      }
    },
    {
      "name": "Launch Built-in web server",
      "type": "php",
      "request": "launch",
      "runtimeArgs": [
        "-dxdebug.mode=debug",
        "-dxdebug.start_with_request=yes",
        "-S",
        "localhost:0"
      ],
      "program": "",
      "cwd": "${workspaceRoot}",
      "port": 9003,
      "serverReadyAction": {
        "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

With this configuration, I'm able to run PHP scripts in VSCode with the debugger and configure breakpoints.

FWIW, you can double check your configuration by creating and following script:

<?php
xdebug_info();

Here is the output on my system:

Feature => Enabled/Disabled
Development Helpers => ✔ enabled
Coverage => ✘ disabled
GC Stats => ✘ disabled
Profiler => ✘ disabled
Step Debugger => ✔ enabled
Tracing => ✘ disabled

                            Optional Features                            
Compressed File Support => yes (gzip)
Clock Source => clock_gettime_nsec_np

                              Diagnostic Log                              
No messages

                              Step Debugging                              
Debugger is active
Connected Client => 127.0.0.1:62508

DBGp Settings
Max Children => 100
Max Data => 1024
Max Depth => 1
Show Hidden Properties => No
Extended Properties => Yes
Notifications => Yes
Resolved Breakpoints => Yes
Breakpoint Details => No

                                   PHP                                   
                           Build Configuration                           
Version (Run Time) => 8.1.10
Version (Compile Time) => 8.1.10
Debug Build => no
Thread Safety => disabled
                                 Settings                                 
Configuration File (php.ini) Path => /opt/homebrew/etc/php/8.1
Loaded Configuration File => /opt/homebrew/etc/php/8.1/php.ini
Scan this dir for additional .ini files => /opt/homebrew/etc/php/8.1/conf.d
Additional .ini files parsed => /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini,
/opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini


Directive => Local Value => Master Value
xdebug.mode (through XDEBUG_MODE) => debug,develop => debug
xdebug.start_with_request => yes => yes
xdebug.start_upon_error => default => default
xdebug.output_dir => /var/tmp/ => /var/tmp/
xdebug.use_compression => 1 => 1
xdebug.trigger_value => no value => no value
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.log => /Users/Paul/Documents/docs/repos/xdebug.log => /Users/Paul/Documents/docs/repos/xdebug.log
xdebug.log_level => 7 => 7
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3
xdebug.max_nesting_level => 256 => 256
xdebug.cli_color => 0 => 0
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.max_stack_frames => -1 => -1
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.profiler_append => Off => Off
xdebug.cloud_id => no value => no value
xdebug.client_host => 127.0.0.1 => 127.0.0.1
xdebug.client_port => 62508 => 9003
xdebug.discover_client_host => Off => Off
xdebug.client_discovery_header => no value => no value
xdebug.idekey => no value => no value
xdebug.connect_timeout_ms => 200 => 200
xdebug.scream => Off => Off
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_return => Off => Off
Bubba
  • 71
  • 6