HeadlinesBriefing favicon HeadlinesBriefing.com

Writing a Debugger in Rust: Part 1

Hacker News •
×

The author, having previously worked on Microsoft's Debugger Platform team, is writing a new debugger in Rust to deepen their understanding of the language and to demystify how debuggers function. This "DbgRs" project aims to leverage Rust crates for tasks like symbol handling and disassembly, allowing focus on core debugging concepts.

The article begins by defining a debugger as a tool for analyzing running systems or static snapshots, with examples like GDB, LLDB, Visual Studio, and WinDbg. The focus for this series is live usermode debugging on Windows, utilizing fundamental OS APIs rather than higher-level ones like those used by WinDbg's DbgEng.

Central to live debugging is an event loop. A debugger "attaches" to a target process, which is then frozen by the OS upon debug events. The debugger can then inspect or modify the process state before resuming execution. For Windows, attachment can occur via `DebugActiveProcess` for existing processes or by creating a new process with specific flags like `DEBUG_ONLY_THIS_PROCESS` using `CreateProcessW`. The debugger then enters a loop, waiting for events using `WaitForDebugEventEx` and resuming the process with `ContinueDebugEvent`. The `DEBUG_EVENT` structure provides details like process ID, thread ID, and event code, dictating which data union is valid.