Debugging and Profiling

42.1 Browser Debugging

Chrome DevTools: step through WAT disassembly. Install the C/C++ DevTools Support (DWARF) extension to debug the original C/C++/Rust source (breakpoints, variables) when built with -g.

Firefox: source-level debugging with source maps (-gsource-map in Emscripten) and Wasm disassembly.

Name section (-g, or keep names in release builds via --profiling-funcs) gives meaningful stack traces.

emcc -g -O0 app.c -o app.js                    # full DWARF
emcc -gsource-map -O2 app.c -o app.js           # source maps
cargo build --target wasm32-unknown-unknown     # debug build includes DWARF; wasm-bindgen --keep-debug

42.2 Runtime Debugging

wasmtime run -D debug-info app.wasm              # DWARF → native debug info
lldb -- wasmtime run -D debug-info app.wasm      # source-level debugging with lldb/gdb
WASMTIME_BACKTRACE_DETAILS=1 wasmtime run app.wasm
wasm-tools print app.wasm | less                 # inspect WAT
wasm2wat --enable-all app.wasm -o app.wat
wasm-interp --trace app.wasm --run-all-exports   # instruction trace (WABT interpreter)

42.3 Logging and Panics

Rust: console_error_panic_hook, console_log/tracing-wasm, web_sys::console::log_1.

C/C++: printf → console.log (Emscripten), -sASSERTIONS=2, -fsanitize=address,undefined (ASan/UBSan in Emscripten), SAFE_HEAP.

WASI: stdout/stderr, and RUST_BACKTRACE-style traces via Wasmtime backtraces.

42.4 Profiling

Browser: the DevTools Performance panel, Firefox Profiler (Wasm frames).

Wasmtime: --profile=perfmap|jitdump|vtune|guest, then perf report or Firefox Profiler for guest profiles.

Memory: track memory.buffer.byteLength, allocator stats, heap snapshots (for JS-held references and WasmGC objects in DevTools).