WebAssembly (WASM) for High Performance Apps · บทเรียน

การแก้ไขข้อบกพร่องของโมดูล WebAssembly

ใช้เครื่องมือสำหรับนักพัฒนาของเบราว์เซอร์และเทคนิคแก้ไขข้อบกพร่องอื่น ๆ เพื่อตรวจสอบและแก้ปัญหาแอปพลิเคชัน WASM

บทเรียน 3 จาก 411 ขั้นตอน

การแก้ไขข้อบกพร่องของโมดูล WebAssembly เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 (WASM) for High Performance Apps ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การแก้ไขข้อบกพร่องของโมดูล WebAssembly” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแก้ไขข้อบกพร่องของโมดูล WebAssembly” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแก้ไขข้อบกพร่องของโมดูล WebAssembly”

ใช้เครื่องมือสำหรับนักพัฒนาของเบราว์เซอร์และเทคนิคแก้ไขข้อบกพร่องอื่น ๆ เพื่อตรวจสอบและแก้ปัญหาแอปพลิเคชัน WASM คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแก้ไขข้อบกพร่องของโมดูล WebAssembly” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม

ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การวัดประสิทธิภาพ WASM
  2. การปรับโค้ด Rust ให้เหมาะกับ WASM
  3. การแก้ไขข้อบกพร่องของโมดูล WebAssembly
  4. SIMD และการทำงานหลายเธรดเพื่อปริมาณงานสูงสุด
← กลับไปที่ WebAssembly (WASM) for High Performance Apps