Rust Debugging

mohitmishra786/low-level-dev-skills/skills/rust/rust-debugging

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

Rust debugging skill for systems programming. Use when debugging Rust binaries with GDB or LLDB, enabling Rust pretty-printers, interpreting panics and backtraces, debugging async/await with tokio-console, stepping through no_std code, or using dbg! and tracing macros effectively. Activates on queries about rust-gdb, rust-lldb, RUST_BACKTRACE, Rust panics, debugging async Rust, tokio-console, or pretty-printers.

AI 產生的概覽

指導使用 GDB/LLDB、回溯、panic 排查、非同步追蹤與 no_std 策略來偵錯 Rust 程式。

功能
此技能提供偵錯 Rust 二進位檔的操作指引:帶偵錯資訊建置、使用 rust-gdb 與 rust-lldb 及 Rust pretty-printer、設定 RUST_BACKTRACE、排查常見 panic 訊息,以及使用 dbg! 巨集與 tracing 進行結構化日誌記錄。它也涵蓋使用 tokio-console 偵錯非同步程式,以及逐步偵錯 no_std 程式碼。產出為指引與命令參考,而非可執行工具,並附有一份關於 GDB pretty-printer 設定的參考文件。
適用情境
適用於排查 Rust 當機、panic 或回溯,GDB 或 LLDB 的 pretty-printer 無法正確顯示 String、Vec 或 Option 值時,或偵錯非同步 Tokio 任務與 no_std 目標時。
執行需求
需要 Rust 工具鏈(含 cargo),以及 GDB 或 LLDB 等偵錯工具與 rust-gdb/rust-lldb 包裝指令。選用相依項目包括 tracing 與 tracing-subscriber crate、console-subscriber 與 tokio,以及 tokio-console 執行檔。此技能不附帶指令碼,僅為說明文件加一份參考文件。

Rust Debugging

Purpose

Guide agents through debugging Rust programs: GDB/LLDB with Rust pretty-printers, backtrace configuration, panic triage, async debugging with tokio-console, and #[no_std] debugging strategies.

Triggers

  • "How do I use GDB/LLDB to debug a Rust binary?"
  • "How do I get a full backtrace from a Rust panic?"
  • "How do I debug async Rust / Tokio?"
  • "Rust pretty-printers aren't working in GDB"
  • "How do I debug a Rust panic in production?"
  • "How do I use dbg! and tracing in Rust?"

Workflow

1. Build for debugging

bash
# Debug build (default) — full debug info, no optimizationcargo build
# Release with debug info (for profiling real workloads)cargo build --release --profile release-with-debug# Or configure in Cargo.toml:# [profile.release-with-debug]# inherits = "release"# debug = true
# Run directlycargo runcargo run -- arg1 arg2

2. GDB with Rust pretty-printers

bash
# Use rust-gdb wrapper (sets up pretty-printers automatically)rust-gdb target/debug/myapp
# Or set up manually in ~/.gdbinit:# python# import subprocess, sys# ...

Common GDB session for Rust:

gdb
# Basic(gdb) break main(gdb) run arg1 arg2(gdb) next           # step over(gdb) step           # step into(gdb) continue
# Rust-aware inspection(gdb) print my_string     # Shows String content via pretty-printer(gdb) print my_vec        # Shows Vec elements(gdb) print my_option     # Shows Some(value) or None(gdb) info locals
# Break on panic(gdb) break rust_panic(gdb) break core::panicking::panic
# Backtrace(gdb) bt              # Short backtrace(gdb) bt full         # Full with locals

3. LLDB with Rust pretty-printers

bash
# Use rust-lldb wrapperrust-lldb target/debug/myapp
# Manual setuplldb target/debug/myapp(lldb) command script import /path/to/rust/lib/rustlib/etc/lldb_lookup.py(lldb) command source /path/to/rust/lib/rustlib/etc/lldb_commands

Common LLDB session:

lldb
(lldb) b main::main(lldb) r arg1 arg2(lldb) n              # next (step over)(lldb) s              # step into(lldb) c              # continue(lldb) frame variable # show locals(lldb) p my_string    # print variable with pretty-printer(lldb) bt             # backtrace(lldb) bt all         # all threads

4. Backtrace configuration

bash
# Short backtrace (default on panic)RUST_BACKTRACE=1 ./myapp
# Full backtrace with all framesRUST_BACKTRACE=full ./myapp
# With symbols (requires debug build or separate debug info)RUST_BACKTRACE=full ./target/debug/myapp
# Capture backtrace programmaticallyuse std::backtrace::Backtrace;let bt = Backtrace::capture();eprintln!("{bt}");

For release binaries, keep debug symbols in a separate file:

bash
# Build release with debug infocargo build --releaseobjcopy --only-keep-debug target/release/myapp target/release/myapp.debugstrip --strip-debug target/release/myappobjcopy --add-gnu-debuglink=target/release/myapp.debug target/release/myapp

5. Panic triage

rust
// Set a custom panic hook for structured logginguse std::panic;
panic::set_hook(Box::new(|info| {    let backtrace = std::backtrace::Backtrace::force_capture();    eprintln!("PANIC: {info}");    eprintln!("{backtrace}");    // Log to file, send to Sentry, etc.}));

Common panic patterns:

Panic messageLikely cause
index out of bounds: the len is N but the index is MArray/vec OOB access
called Option::unwrap() on a None valueUnwrap on None
called Result::unwrap() on an Err valueUnwrap on error
attempt to subtract with overflowInteger underflow (debug build)
assertion failedFailed assert! or assert_eq!
stack overflowInfinite recursion

Use panic = "abort" in release to get a crash dump instead of unwind.

6. The dbg! macro

rust
// dbg! prints file, line, value and returns the valuelet result = dbg!(some_computation(x));// prints: [src/main.rs:15] some_computation(x) = 42
// Chain multiple valueslet (a, b) = dbg!((compute_a(), compute_b()));
// Inspect inside iterator chainslet sum: i32 = (0..10)    .filter(|x| dbg!(x % 2 == 0))    .map(|x| dbg!(x * x))    .sum();

7. Structured logging with tracing

toml
[dependencies]tracing = "0.1"tracing-subscriber = { version = "0.3", features = ["env-filter"] }
rust
use tracing::{debug, error, info, instrument, warn};
#[instrument]  // Auto-traces function entry/exit with argumentsfn process(id: u64, data: &str) -> Result<(), Error> {    debug!("Processing item");    info!(item_id = id, "Started processing");
    if data.is_empty() {        warn!(item_id = id, "Empty data");        return Err(Error::EmptyData);    }
    error!(item_id = id, err = ?some_result, "Failed");    Ok(())}
// Initialize in maintracing_subscriber::fmt()    .with_env_filter("myapp=debug,warn")    .init();
bash
# Control log levels at runtimeRUST_LOG=debug ./myappRUST_LOG=myapp::module=trace,warn ./myapp

8. Async debugging with tokio-console

toml
[dependencies]console-subscriber = "0.3"tokio = { version = "1", features = ["full", "tracing"] }
rust
// In mainconsole_subscriber::init();
bash
# Install and run tokio-consolecargo install tokio-consoletokio-console  # Connects to running Rust process at port 6669

tokio-console shows: task states, waker activity, blocked tasks, poll durations.

For GDB/LLDB command reference and pretty-printer setup, see references/rust-gdb-pretty-printers.md [blocked].

Related skills

  • Use skills/rust/rustc-basics for debug info flags and build configuration
  • Use skills/debuggers/gdb for GDB fundamentals
  • Use skills/debuggers/lldb for LLDB fundamentals
  • Use skills/rust/rust-sanitizers-miri for memory safety and undefined behaviour

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/rust/rust-debugging提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架