WordPress / wordpress-playground

Run WordPress in the browser via WebAssembly PHP
https://w.org/playground/
GNU General Public License v2.0
1.59k stars 227 forks source link

Support Xdebug #314

Open danielbachhuber opened 1 year ago

danielbachhuber commented 1 year ago

Xdebug is a common developer tool. It would be nice to (optionally) support it.

Done is:

adamziel commented 1 year ago

There's a decent chance XDebug would just work if PHP was built with the right flags. Anyone keen to give it a try?

ibrokemycomputer commented 1 year ago

@adamziel I'd love to give this a go, but due to my full-time job it might be a slow process.

However... "boss man" has been wanting us to start supporting WP projects since we've become so heavily invested in the CMS for our agency clients. So, we'll see.

A couple of things stand out right off the bat:

ibrokemycomputer commented 1 year ago

@adamziel Actually, I take that back. It seems like consideration for this is tied to Issue WordPress/playground-tools#28

adamziel commented 1 year ago

@ibrokemycomputer actually XDebug support would be fantastic regardless of WordPress/playground-tools#28. You are the most welcome to give it a go!

It would require updating the Dockerfile which builds PHP - you will notice some PHP version-dependent behaviors are already in there.

As for config flags, Playground’s php-wasm supports setting php.ini flags so that would only be a matter of supporting custom php.ini in wp-now.

adamziel commented 9 months ago

I was able to build xdebug extension to wasm:

$ git clone https://github.com/xdebug/xdebug.git
$ phpize
$ emconfigure ./configure
$ emmake make all
$ wasm2wat .libs/xdebug.so
// legit wat output

I had to patch the signature of these two functions and remove the final int from it:

I then tried to load it in @php-wasm/cli and got an error:

$ npx @php-wasm/cli -z ./.libs/xdebug.so
RuntimeError: Aborted(To use dlopen, you need enable dynamic linking, see https://github.com/emscripten-core/emscripten/wiki/Linking). Build with -sASSERTIONS for more info.

I looked for some answers on the dynamic linking page, but didn't find any.

Emscripten changelogs suggest dlopen is available for modules compiled with the -sMAIN_MODULE flag.

Edit: I used the -sMAIN_MODULE flag and now PHP isn't crashing anymore. Yay! It still doesn't load the extension, but the error is now different:

Failed loading .libs/xdebug.so:  (null)
adamziel commented 9 months ago

I've built a minimal dlopen() example that works with Emscripten:

https://gist.github.com/adamziel/9c7d3302407294e12d9c1d1eaa54cdba

So it can work, yay! The key is indeed building with -sMAIN_MODULE.

The question now becomes: What goes wrong when we use it with PHP?

adamziel commented 7 months ago

Other than step-debugger, XDebug seems to be working in https://github.com/WordPress/wordpress-playground/pull/831.

Anyone following this issue – would you rather get such a limited version of XDebug early and the step-by-step debugger support in one of the next iterations? Or is that step-by-step debugging the sole reason you're interested in XDebug?

alexstandiford commented 6 months ago

For me, step debugging is a pretty key component in that, but just having formatted var_dump would be a pretty big win, in my opinion.

I don't see any reason to hold off until it's all included, personally.

adamziel commented 6 months ago

Perfect! Let's not block the XDebug PR on the step debugger support. I can't promise any timeline, but the next step here is cleaning up and merging that PR 🔥