mcpField
R
rust-debugging

rust-debugging

github.com/mohitmishra786开发269559 次安装18 周前更新
代码仓库

关于 rust-debugging

rust-debugging 指导 Agent 使用 GDB、LLDB、回溯信息、panic 分析、日志和异步任务检查来调试 Rust 程序。

主要功能

  1. 01GDB 和 LLDB 的 Rust 打印器
  2. 02panic 回溯配置
  3. 03panic 模式分析
  4. 04dbg! 与 tracing 调试方法
  5. 05使用 tokio-console 检查异步任务

适用场景

  1. 01在 GDB 或 LLDB 中检查 Rust 值
  2. 02捕获 panic 的回溯信息
  3. 03调查生产环境崩溃
  4. 04查找阻塞或运行缓慢的 Tokio 任务

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill rust-debugging

安装后在 Claude Code 中输入/rust-debugging确认已加载。

附带文件references/rust-gdb-pretty-printers.md

详细介绍

Rust 调试

用途

指导 Agent 调试 Rust 程序:使用 GDB/LLDB 和 Rust pretty-printer、配置回溯、排查 panic、使用 tokio-console 调试异步代码,以及 #[no_std] 调试策略。

触发条件

  • “如何使用 GDB/LLDB 调试 Rust 二进制程序?”
  • “如何获取 Rust panic 的完整回溯?”
  • “如何调试异步 Rust / Tokio?”
  • “GDB 中的 Rust pretty-printer 无法正常工作”
  • “如何在生产环境中调试 Rust panic?”
  • “如何在 Rust 中使用 dbg! 和 tracing?”

工作流

1. 构建调试版本
# Debug build (default) — full debug info, no optimization
cargo 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 directly
cargo run
cargo run -- arg1 arg2
2. 使用 GDB 和 Rust pretty-printer
# Use rust-gdb wrapper (sets up pretty-printers automatically)
rust-gdb target/debug/myapp

# Or set up manually in ~/.gdbinit:
# python
# import subprocess, sys
# ...

常见的 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 和 Rust pretty-printer
# Use rust-lldb wrapper
rust-lldb target/debug/myapp

# Manual setup
lldb 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

常见的 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. 配置回溯
# Short backtrace (default on panic)
RUST_BACKTRACE=1 ./myapp

# Full backtrace with all frames
RUST_BACKTRACE=full ./myapp

# With symbols (requires debug build or separate debug info)
RUST_BACKTRACE=full ./target/debug/myapp

# Capture backtrace programmatically
use std::backtrace::Backtrace;
let bt = Backtrace::capture();
eprintln!("{bt}");

对于 release 二进制程序,将调试符号保存在单独的文件中:

# Build release with debug info
cargo build --release
objcopy --only-keep-debug target/release/myapp target/release/myapp.debug
strip --strip-debug target/release/myapp
objcopy --add-gnu-debuglink=target/release/myapp.debug target/release/myapp
5. 排查 panic
// Set a custom panic hook for structured logging
use 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.
}));

常见 panic 模式:

panic 信息可能原因
index out of bounds: the len is N but the index is M数组/vec 越界访问
called Option::unwrap() on a None value对 None 调用 unwrap
called Result::unwrap() on an Err value对错误调用 unwrap
attempt to subtract with overflow整数下溢(调试构建)
assertion failedassert! 或 assert_eq! 断言失败
stack overflow无限递归

在 release 中使用 panic = "abort",以生成崩溃转储,而不是展开堆栈。

6. dbg! 宏
// dbg! prints file, line, value and returns the value
let result = dbg!(some_computation(x));
// prints: [src/main.rs:15] some_computation(x) = 42

// Chain multiple values
let (a, b) = dbg!((compute_a(), compute_b()));

// Inspect inside iterator chains
let sum: i32 = (0..10)
    .filter(|x| dbg!(x % 2 == 0))
    .map(|x| dbg!(x * x))
    .sum();
7. 使用 tracing 进行结构化日志记录
[dependencies]
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
use tracing::{debug, error, info, instrument, warn};

#[instrument]  // Auto-traces function entry/exit with arguments
fn 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 main
tracing_subscriber::fmt()
    .with_env_filter("myapp=debug,warn")
    .init();
# Control log levels at runtime
RUST_LOG=debug ./myapp
RUST_LOG=myapp::module=trace,warn ./myapp
8. 使用 tokio-console 调试异步代码
[dependencies]
console-subscriber = "0.3"
tokio = { version = "1", features = ["full", "tracing"] }
// In main
console_subscriber::init();
# Install and run tokio-console
cargo install tokio-console
tokio-console  # Connects to running Rust process at port 6669

tokio-console 会显示:任务状态、waker 活动、阻塞的任务、轮询耗时。

有关 GDB/LLDB 命令参考和 pretty-printer 设置,请参阅 references/rust-gdb-pretty-printers.md。

相关技能

  • 使用 skills/rust/rustc-basics 了解调试信息标志和构建配置
  • 使用 skills/debuggers/gdb 了解 GDB 基础知识
  • 使用 skills/debuggers/lldb 了解 LLDB 基础知识
  • 使用 skills/rust/rust-sanitizers-miri 了解内存安全和未定义行为

评论

支持 Markdown