Skip to content

Commit 6911327

Browse files
Add WITH_DEBUG option for building php-wasm with DWARF debug info (#47)
## Motivation for the change, related issues Prior to this PR, we have built with DWARF debug info by manually editing our php/Dockerfile, and it would be much better for devs to just supply a command line option for creating such builds. ## Implementation details This PR adds a `WITH_DEBUG` option that can be used like: `npx nx recompile-php:asyncify php-wasm-web -- --PHP_VERSION=8.4 --WITH_DEBUG=yes` It recompiles php-wasm to include DWARF debug info which may be used for more detailed debugging in Chrome Canary. In contrast to debugging with source maps, this way allows you to see more info like named variable values in the step debugger. See https://developer.chrome.com/docs/devtools/wasm for more info. I considered merging the `WITH_SOURCEMAPS` option with this one, but in my testing, it seemed like building with both DWARF info and sourcemaps weakened the debug experience. For example, when building with just DWARF debug info and debugging in Chrome canary, I noticed that I could see the value of both `size` and `alignment` vars in the PHP memory allocation code, but when building both debug info and sourcemaps, I could only see the value of the `size` var. It's possible the difference is a coincidence or due to some other issue, but I observed this difference two or three times when testing different build types. ## Testing Instructions (or ideally a Blueprint) - Install Chrome Canary if needed - Add [the C/C++ Dev Tools Support extension](https://chromewebstore.google.com/detail/cc++-devtools-support-dwa/pdcpmagijalfljmkmjngeonclgbbannb) to Chrome Canary - Run `npm run dev` - Test building with source maps - Run `npx nx recompile-php:asyncify php-wasm-web -- --PHP_VERSION=8.4 --WITH_SOURCEMAPS=yes` - Make sure Playground web loads from the local server with PHP 8.4 selected - Open dev tools to confirm you can open PHP source files like `main.c` - Test building and using DWARF debug info - Run `npx nx recompile-php:asyncify php-wasm-web -- --PHP_VERSION=8.4 --WITH_DEBUG=yes` - Open the [debug extension options](chrome-extension://pdcpmagijalfljmkmjngeonclgbbannb/ExtensionOptions.html) - Add a path substitution to map `/root/php-src/` to the absolute path the the php-src directory created by the php-wasm build. - Note: Within your Playground repo, the relative path will be: `packages/php-wasm/web/public/php/asyncify/8_4_0/php-src/` - Make sure Playground web loads from the local server with PHP 8.4 selected. - Open dev tools. - Reload Playground (reloading often seems to be needed to properly see the C source files). - Open the PHP source file `zend_alloc.c` - Locate the function `static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)` - Set a breakpoint on the first line that permits breakpoints - Reload Playground and observe that you hit that breakpoint - Confirm you can observe the function param values for `size` and `alignment` - Test normal php-wasm build - Run `npx nx recompile-php:asyncify php-wasm-web -- --PHP_VERSION=8.4` and confirm that Playground loads from the local server with PHP 8.4 selected.
1 parent 5066fc8 commit 6911327

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,18 @@ npx nx start php-wasm-cli
113113
# Build latest WordPress releases
114114
npx nx bundle-wordpress:all playground-wordpress-builds
115115

116-
# Recompile PHP 7.0 - 8.3 releases to .wasm for web
116+
# Recompile PHP 7.0 - 8.4 releases to .wasm for web
117117
npx nx recompile-php:all php-wasm-web
118118

119-
# Recompile PHP 7.0 - 8.3 releases to .wasm for node
119+
# Recompile PHP 7.0 - 8.4 releases to .wasm for node
120120
npx nx recompile-php:all php-wasm-node
121121

122+
## Recompile with DWARF debug info for debugging
123+
npx nx recompile-php:all php-wasm-node --WITH_DEBUG=yes
124+
125+
## Recompile with source maps for debugging
126+
npx nx recompile-php:all php-wasm-node --WITH_SOURCEMAPS=yes
127+
122128
# Builds the documentation site
123129
npx nx build docs-site
124130

packages/php-wasm/compile/build.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ const argParser = yargs(process.argv.slice(2))
9191
choices: ['yes', 'no'],
9292
description: 'Build with source maps',
9393
},
94+
WITH_DEBUG: {
95+
type: 'string',
96+
choices: ['yes', 'no'],
97+
description: 'Build with DWARF debug information.',
98+
},
9499
WITH_ICONV: {
95100
type: 'string',
96101
choices: ['yes', 'no'],
@@ -220,6 +225,8 @@ await asyncSpawn(
220225
'--build-arg',
221226
`OUTPUT_DIR_FOR_SOURCE_MAP_BASE=${outputDir}`,
222227
'--build-arg',
228+
getArg('WITH_DEBUG'),
229+
'--build-arg',
223230
getArg('WITH_ICONV'),
224231
'--build-arg',
225232
getArg('WITH_MYSQL'),

packages/php-wasm/compile/php/Dockerfile

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ ARG WITH_OPENSSL
8181
ARG WITH_ICONV
8282
ARG WITH_SOURCEMAPS
8383
ARG OUTPUT_DIR_FOR_SOURCE_MAP_BASE
84+
ARG WITH_DEBUG
8485
ARG WITH_WS_NETWORKING_PROXY
8586

8687
# The platform to build for: web or node
@@ -407,18 +408,22 @@ RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then
407408

408409
ARG WITH_SOURCEMAPS
409410
ARG OUTPUT_DIR_FOR_SOURCE_MAP_BASE
411+
ARG WITH_DEBUG
410412
RUN set -euxo pipefail; \
411413
if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \
412414
# Add nodefs when building for node.js
413415
echo -n ' -lnodefs.js ' >> /root/.emcc-php-wasm-flags; \
414416
fi; \
415-
if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \
416-
echo -n ' -g3 -gsource-map' >> /root/.emcc-php-wasm-flags; \
417-
# Ensure source maps are loaded from the correct URL on the web.
418-
# Without this, browsers will try the wasm:// protocol and never load the source map.
419-
if [ "$EMSCRIPTEN_ENVIRONMENT" = "web" ]; then \
420-
export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}"; \
421-
echo -n " --source-map-base http://127.0.0.1:5400/@fs${OUTPUT_DIR_FOR_SOURCE_MAP_BASE}/${PHP_VERSION_ESCAPED}/" >> /root/.emcc-php-wasm-flags; \
417+
if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \
418+
echo -n ' -g3 ' >> /root/.emcc-php-wasm-flags; \
419+
if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \
420+
echo -n ' -gsource-map' >> /root/.emcc-php-wasm-flags; \
421+
# Ensure source maps are loaded from the correct URL on the web.
422+
# Without this, browsers will try the wasm:// protocol and never load the source map.
423+
if [ "$EMSCRIPTEN_ENVIRONMENT" = "web" ]; then \
424+
export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}"; \
425+
echo -n " --source-map-base http://127.0.0.1:5400/@fs${OUTPUT_DIR_FOR_SOURCE_MAP_BASE}/${PHP_VERSION_ESCAPED}/" >> /root/.emcc-php-wasm-flags; \
426+
fi; \
422427
fi; \
423428
else \
424429
# Preserve symbol names in node.js build – the bundle size doesn't matter as much
@@ -957,6 +962,7 @@ export ASYNCIFY_ONLY=$'"zif_phar_fopen",\
957962
RUN mkdir /root/output
958963
COPY ./php/phpwasm-emscripten-library.js /root/phpwasm-emscripten-library.js
959964
ARG WITH_SOURCEMAPS
965+
ARG WITH_DEBUG
960966
RUN set -euxo pipefail; \
961967
mkdir -p /build/output; \
962968
source /root/emsdk/emsdk_env.sh; \
@@ -996,7 +1002,7 @@ RUN set -euxo pipefail; \
9961002
"_wasm_set_request_uri", \n\
9971003
"_wasm_set_skip_shebang" '"$(cat /root/.EXPORTED_FUNCTIONS)"']'; \
9981004
export OPTIMIZATION_FLAGS="-O3"; \
999-
if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \
1005+
if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \
10001006
export OPTIMIZATION_FLAGS="-O0"; \
10011007
fi; \
10021008
emcc $OPTIMIZATION_FLAGS \
@@ -1156,6 +1162,9 @@ RUN set -euxo pipefail; \
11561162
# Make the source map paths relative to the .wasm file, not the build directory
11571163
sed -i 's#"\.\./\.\./root/#"#g' /root/output/php.wasm.map; \
11581164
mv /root/output/php.wasm.map "/root/output/${PHP_VERSION_ESCAPED}/php.wasm.map"; \
1165+
fi; \
1166+
if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \
1167+
# Make PHP source available for use with step debugger
11591168
rm -rf /root/php-src/.git; \
11601169
cp -r /root/php-src "/root/output/${PHP_VERSION_ESCAPED}/php-src"; \
11611170
cp -r /root/emsdk/upstream/emscripten/system "/root/output/${PHP_VERSION_ESCAPED}/php-src/system"; \

0 commit comments

Comments
 (0)