How to Debug WebAssembly in a Web Browser

Debugging WebAssembly (Wasm) modules running inside a web browser has become significantly easier thanks to modern browser developer tools and source-level debugging standards. This article provides a practical, step-by-step guide on how to debug your Wasm code directly inside the browser using source maps and DWARF debug information, allowing you to set breakpoints, step through code, and inspect variables in your original source language like C++, Rust, or Go.

1. Compile with Debug Symbols

To debug WebAssembly at the source level, you must compile your source code with debugging symbols enabled. This embeds DWARF debug information into the Wasm binary or generates source maps.

2. Enable Wasm Debugging in Your Browser

Modern browsers like Google Chrome, Microsoft Edge, and Mozilla Firefox support WebAssembly debugging natively or via extensions.

Open your browser’s Developer Tools (F12 or Ctrl+Shift+I / Cmd+Option+I), go to Settings (gear icon), navigate to Preferences, and ensure that JavaScript source maps and Associated files are enabled.

3. Locate Source Files in DevTools

Once your application is running in the browser with DevTools open:

  1. Navigate to the Sources (or Debugger in Firefox) tab.
  2. In the file navigator on the left, look for a folder containing your original source files (e.g., C++, Rust, or Go files). These are loaded dynamically via the debug symbols embedded in the .wasm file.
  3. Click on a source file to open it in the editor pane.

4. Set Breakpoints and Step Through Code

With your original source code visible in the browser, you can debug it just like standard JavaScript:

5. Inspect Variables and Call Stacks

When paused at a breakpoint, the DevTools sidebar provides vital information about the state of your Wasm module: