Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active October 10, 2019 11:54
Show Gist options
  • Save dotherightthing/f0ea4bdfee453f3f0cae6d33c8f67003 to your computer and use it in GitHub Desktop.
Save dotherightthing/f0ea4bdfee453f3f0cae6d33c8f67003 to your computer and use it in GitHub Desktop.
[Setting up Xdebug] #mamp #php #wordpress

Setting up Xdebug

https://xdebug.org

MAMP Pro 5.1.1

Default version > 7.0.31 Languages > PHP > Extensions > Xdebug (Debugger) > Check

Check settings:

File > Edit Template > PHP > 7.0.31 > OK

[xdebug]
MAMP_Xdebug_MAMPzend_extension="/Applications/MAMP/bin/php/php7.0.31/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so"
 xdebug.remote_enable=1
 xdebug.remote_host=localhost
 xdebug.remote_port=9000
 xdebug.remote_autostart=1
 ;xdebug.profiler_enable=0
 ;xdebug.profiler_output_dir="/Applications/MAMP/tmp"

Remote debugging

Xdebug provides an interface for debugger clients that interact with running PHP scripts.

Sublime Text + Chrome Xdebug helper

http://www.vaerenbergh.com/blog/xdebug-and-sublime-text

Sublime knows when to listen to your browser when a certain cookie is set. For sublime this cookie is "XDEBUG_SESSION = sublime.xdebug". To avoid setting this cookie by hand, we can add a helper addon in our browser so we can enable and disable Xdebug whenever we want.

  1. Install Chrome Xdebug helper
  2. Right click on the bug icon and select Options
  3. In IDE key select Other and enter XDEBUG_SESSION = sublime.xdebug
  4. Left click on the bug icon and select Debug (green)

Configure the path mapping:

Configuration The following settings can be configured in Xdebug.sublime-settings or in *.sublime-project files:

Features Remote debugging by configuring path mapping

path_mapping For remote debugging to resolve the file locations it is required to configure the path mapping with the server path as key and local path as value.

Source

  1. SublimeText > Preferences > Package Settings > Xdebug > Settings - User
  2. Add a path mapping: ::1 is taken from MAMP Pro > Tools > show /etc/hosts File
{
  // Example:
  // "/absolute/path/to/file/on/server" : "/path/to/file/on/computer",
  // "/var/www/htdocs/example/" : "C:/git/websites/example/"
  "path_mapping": {
    "::1" : "/Volumes/DanBackup/Websites/www.dontbelievethehype.co.nz/"
  }
}

Run the Xdebug debugger in Sublime:

  1. Open Sublime
  2. Install the Xdebug Client (SublimeTextXdebug)
  3. Tools > Xdebug > Start Debugging

Load the local website www.dontbelievethehype.co.nz in Chrome

An error generating file automatically opens in Sublime with line 30 highlighted with a green arrow.

I expect to see a stack trace in Sublime, but there's nothing.

However when I open /Volumes/DanBackup/Websites/www.dontbelievethehype.co.nz/wp-content/debug.log the stack trace is there, with an error for line 30.

Disabling WP_DEBUG_LOG (preventing output to debug.log) doesn't prevent Sublime from auto opening the file:

// outputs errors to ./debug.log
define('WP_DEBUG', true);

if ( WP_DEBUG ) {
  define('WP_DEBUG_LOG', false);
  define('WP_DEBUG_DISPLAY', false);
  @ini_set('display_errors', 0);
}

Disable other WordPress debug plugins

Disabling the Query Monitor caused the following to be respected (because the output wasn't being redirected to the plugin):

// wp-config.php
define('WP_DEBUG_DISPLAY', true);

MacGDBp

https://www.bluestatic.org/software/macgdbp/

A Cocoa/Mac OS X debugging client for PHP using the Xdebug protocol

As suggested in the MAMP Pro interface next to the XDebug checkbox.

Works first time if there is no XDebug session running in Sublime Text, else shows socket error.

However, MacGDBp raises different errors from Sublime Text debug.log.

The naming is somewhat similar to the XDebug option:

xdebug.remote_handler Type: string, Default value: dbgp Can be either 'php3' which selects the old PHP 3 style debugger output, 'gdb' which enables the GDB like debugger interface or 'dbgp' - the debugger protocol. The DBGp protocol is the only supported protocol.

Note: Xdebug 2.1 and later only support 'dbgp' as protocol.

Other things I tried

https://stackoverflow.com/questions/42839981/configuring-xdebug-php-path-mapping-and-php-ini-for-sublime3

xdebug.remote_connect_back=1

and

https://stackoverflow.com/questions/1868568/xdebug-configuration-with-php-fastcgi-and-eclipse

and outputting an XDebug log:

Log opened at 2018-11-09 20:52:53
I: Connecting to configured address/port: localhost:9000.
W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (19).
W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (19).
E: Could not connect to client. :-(
Log closed at 2018-11-09 20:52:53

and watching http://schurpf.com/xdebug-phpstorm-mamp/

Further reading

@enderandpeter
Copy link

enderandpeter commented Apr 29, 2019

In my case, the poll success, but error was due to how my IDE was not properly configured to successfully connect to Xdebug on the server, which was also localhost. It turned out that I had earlier changed the remote_port setting in the INI and forgot the restart to web server. However, even more important was to make sure PHPStorm, my IDE, had a Languages and Frameworks -> PHP -> Server config for the localhost site with Use path mappings unchecked.

Also, I had to disable remote_connect_back, as this discussion explains.

@dotherightthing
Copy link
Author

@dotherightthing
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment