Debugging Tools
Combine dbg!, tracing, debuggers (lldb/gdb), rust-analyzer, and cargo expand for compile-time and runtime issues.
Recipe
use tracing::{info, instrument};
#[instrument]
async fn handle(id: u64) -> Result<(), Error> {
info!(%id, "start");
dbg!(&config);
Ok(())
}RUST_LOG=debug cargo run
rust-lldb target/debug/myappWorking Example
| Tool | Use |
|---|---|
dbg! | quick stderr print of value+location |
tracing | structured logs and spans in async |
rust-lldb / rust-gdb | breakpoints, backtrace |
rust-analyzer | types, borrow errors inline |
cargo expand | macro debugging |
samply / perf | CPU profiles |
# backtrace on panic
RUST_BACKTRACE=1 cargo runDeep Dive
Enable debug = 1 in release profile for symbolicated prod profiles. tracing-subscriber with EnvFilter. tokio-console for async task introspection.
Gotchas
- println in hot loop - I/O noise. Fix: tracing with level filter.
- Debug release without symbols - useless prod traces. Fix: split debuginfo.
- Breakpoint in optimized code - skipped lines. Fix: debug build or limited opt.
- Logging secrets - PII in spans. Fix: redact fields.
- dbg in committed code - remove before merge or CI grep.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
eprintln! | tiny scripts | production services |
log crate | legacy | new projects use tracing |
| printf debug | quick | team services |
FAQs
lldb vs gdb?
macOS lldb common; linux either; use rust-lldb wrapper.
vscode debug?
CodeLLDB extension with launch.json cargo build.
pretty backtrace?
color-eyre in binaries for human panic reports.
heap debug?
dhat/heaptrack separate from lldb.
remote debug?
gdbserver in container; attach from host.
wasm?
browser devtools not lldb.
tracing layers?
fmt, json, opentelemetry subscriber layers stack.
test debug?
cargo test -- --nocapture plus lldb on test binary.
miri vs lldb?
Miri UB; lldb runtime state.
sanitize logs?
Filter crates sqlx=warn etc.
Related
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.