0Pricing
WebAssembly (WASM) for High Performance Apps · 课时

调试 WebAssembly 模块

利用浏览器开发者工具和其他调试技术检查并排查 WASM 应用中的问题

调试 WebAssembly 模块 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebAssembly (WASM) for High Performance Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Debugging WASM: An Overview

Ever wondered how to peek inside your WebAssembly code while it runs? Debugging WASM lets you find and fix issues in your high-performance modules.

It's a bit different from debugging pure JavaScript, but modern browser developer tools offer powerful capabilities to help you out.

Browser DevTools: Sources Tab

Your browser's developer tools are your best friend for WASM debugging. In Chrome, open DevTools (F12 or Cmd+Option+I).

  • Navigate to the Sources tab.
  • Look for your WASM module in the file tree, often under wasm:// or similar.
  • Without source maps, you'll see raw WebAssembly text format – which is very hard to read!

Source Maps: From WASM to Source

Debugging raw WebAssembly text is incredibly tough! Source maps are special files that link the compiled WASM code back to your original C, C++, or Rust source code.

When present, your browser can display your original source files, allowing you to debug them directly, just like JavaScript.

Rust & wasm-pack Source Maps

When compiling Rust to WASM, tools like wasm-pack can automatically generate source maps. The key is to compile in debug mode.

A simple Rust function that adds one to a number:

// src/lib.rs
#[no_mangle]
pub extern "C" fn add_one(num: i32) -> i32 {
    // This function adds 1 to the input number.
    // It will be exported from the WASM module.
    num + 1
}

Loading WASM & Source Maps

Let's see how a browser loads a WASM module. When compiled with source maps (e.g., wasm-pack build --target web --dev), the browser uses them to show your original Rust/C/C++ files in the Sources tab.

Here's how JavaScript loads a tiny, conceptual WASM module:

async function loadAndRunWasm() {
  // This is a conceptual WASM binary for 'add(a, b) => a + b'
  const wasmCode = new Uint8Array([
    0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
    0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
    0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20,
    0x00, 0x20, 0x01, 0x6a, 0x0b
  ]);

  const wasmModule = await WebAssembly.instantiate(wasmCode, {});
  const add = wasmModule.instance.exports.add;
  console.log("WASM add(5, 7):", add(5, 7)); // Expected: 12
}
loadAndRunWasm();

Pausing Execution with Breakpoints

Just like JavaScript, you can set breakpoints directly in your original C/C++/Rust source files shown in the Sources tab of your browser's DevTools.

  • Click on the line number in the gutter where you want to pause execution.
  • When your WASM code executes that line, the browser will pause, letting you inspect variables and the call stack.

Inspect Variables, Memory & Stack

When execution pauses at a breakpoint, you gain powerful insights:

  • Scope pane: View local variables and function arguments, often with their original names if source maps are good.
  • Memory Inspector: Use the dedicated 'Memory' tab or right-click a memory address to inspect WASM's linear memory buffer.
  • Call Stack pane: See the sequence of function calls that led to the current point, including both JavaScript and WASM frames.

Step-by-Step Execution

Once paused at a breakpoint, use the debugger controls to navigate your code line by line:

  • Step Over (F10): Execute the current line and move to the next.
  • Step Into (F11): Dive into a function call on the current line.
  • Step Out (Shift+F11): Finish executing the current function and return to where it was called.
  • Resume (F8): Continue execution until the next breakpoint or the program ends.

Debugging JS-WASM Interaction

Often, bugs occur at the boundary where JavaScript calls WASM functions or vice-versa. Set breakpoints in both your JavaScript and WASM code.

You can step seamlessly between JS and WASM execution frames in the DevTools call stack, making it easy to trace data flow and identify issues in the interop layer.

Debugging Knowledge Check

Test your understanding of WebAssembly debugging!

Debugging WASM: Recap

Today, you learned how to leverage browser developer tools to debug your WebAssembly applications. We covered:

  • Using source maps to see original source code.
  • Setting breakpoints and stepping through execution.
  • Inspecting variables, memory, and the call stack.
  • Debugging interactions between JavaScript and WASM.

Mastering these techniques is crucial for building robust and performant WASM applications. Keep practicing!

常见问题解答

「调试 WebAssembly 模块」课时是免费的吗?

是的 — 「调试 WebAssembly 模块」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

「调试 WebAssembly 模块」这节课中我会学到什么?

利用浏览器开发者工具和其他调试技术检查并排查 WASM 应用中的问题 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebAssembly (WASM) for High Performance Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebAssembly (WASM) for High Performance Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「调试 WebAssembly 模块」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebAssembly (WASM) for High Performance Apps 课中编写并运行代码吗?

能。每节 WebAssembly (WASM) for High Performance Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. WASM 性能基准测试
  2. 优化用于 WASM 的 Rust 代码
  3. 调试 WebAssembly 模块
  4. SIMD 与多线程实现最大吞吐量
← 返回 WebAssembly (WASM) for High Performance Apps