用途
rust-debugging 是一个面向系统编程的 Rust 调试技能,旨在引导使用者调试 Rust 程序,涵盖 GDB/LLDB 配合 Rust pretty-printers、回溯配置、panic 分类处理、使用 tokio-console 进行异步调试,以及 #[no_std] 调试策略。
触发场景
该技能在以下问题场景中被触发使用:
- 如何使用 GDB/LLDB 调试 Rust 二进制?
- 如何从 Rust panic 中获取完整回溯?
- 如何调试 async Rust / Tokio?
- Rust pretty-printers 在 GDB 中不工作怎么办?
- 如何调试生产环境中的 Rust panic?
- 如何在 Rust 中使用 dbg! 和 tracing?
主要能力与使用方式
1. 构建调试版本
- Debug 构建(默认):包含完整调试信息,无优化,使用
cargo build。 - 带调试信息的 Release 构建:
cargo build --release --profile release-with-debug,或在 Cargo.toml 中配置[profile.release-with-debug],设置inherits = "release"与debug = true。 - 直接运行:
cargo run,或cargo run -- arg1 arg2。
2. GDB 与 Rust pretty-printers
- 使用 rust-gdb 包装器自动设置 pretty-printers:
rust-gdb target/debug/myapp。 - 也可在 ~/.gdbinit 中手动设置。
- 常用 GDB 会话:
break main、run arg1 arg2、next、step、continue;使用print my_string、print my_vec、print my_option进行 Rust 感知检查,info locals查看局部变量;可在rust_panic、core::panicking::panic上设置断点;使用bt查看简短回溯,bt full查看包含局部变量的完整回溯。
3. LLDB 与 Rust pretty-printers
- 使用 rust-lldb 包装器:
rust-lldb target/debug/myapp。 - 手动设置:
lldb target/debug/myapp,然后执行command script import /path/to/rust/lib/rustlib/etc/lldb_lookup.py与command source /path/to/rust/lib/rustlib/etc/lldb_commands。 - 常用 LLDB 会话:
b main::main、r arg1 arg2、n、s、c、frame variable、p my_string、bt、bt all。
4. 回溯配置
RUST_BACKTRACE=1 ./myapp:短回溯(panic 默认)。RUST_BACKTRACE=full ./myapp:包含所有帧的完整回溯。- 带符号:
RUST_BACKTRACE=full ./target/debug/myapp(需 debug 构建或单独调试信息)。 - 程序内捕获回溯:使用
std::backtrace::Backtrace,调用Backtrace::capture()并通过eprintln!输出。 - 对于 release 二进制,可将调试符号保留在单独文件中:
cargo build --release后,使用objcopy --only-keep-debug、strip --strip-debug、objcopy --add-gnu-debuglink进行处理。
5. Panic 分类处理
- 通过
panic::set_hook设置自定义 panic hook,使用Backtrace::force_capture()进行结构化日志记录,可写入文件或发送到 Sentry 等。 - 常见 panic 模式对照:
index out of bounds: the len is N but the index is M:数组/向量越界访问。called Option::unwrap() on a None value:对 None 调用 unwrap。called Result::unwrap() on an Err value:对错误调用 unwrap。attempt to subtract with overflow:整数下溢(debug 构建)。assertion failed:assert! 或 assert_eq! 失败。stack overflow:无限递归。
- 在 release 中使用
panic = "abort"以获得崩溃转储而非 unwind。
6. dbg! 宏
dbg!(some_computation(x))打印文件、行号、表达式与值,并返回该值。- 可链式检查多个值,也可在迭代器链中检查中间结果。
7. tracing 结构化日志
- 依赖:
tracing = "0.1"与tracing-subscriber = { version = "0.3", features = ["env-filter"] }。 - 使用
debug!、info!、warn!、error!与#[instrument]记录函数入口/出口及参数。 - 在 main 中通过
tracing_subscriber::fmt().with_env_filter(...).init()初始化。 - 运行时控制日志级别:
RUST_LOG=debug ./myapp,或RUST_LOG=myapp::module=trace,warn ./myapp。
8. 使用 tokio-console 的异步调试
- 依赖:
console-subscriber = "0.3"与tokio = { version = "1", features = ["full", "tracing"] }。 - 在 main 中调用
console_subscriber::init()。 - 安装并运行:
cargo install tokio-console,然后tokio-console(连接到运行中 Rust 进程的 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:内存安全与未定义行为。
限制
资料未提供其他功能、支持的平台范围、收费或价格信息。