Under rust-gdb, how do you stop a Rust service at the moment it panics and find the line that caused the panic?
answer
- a function std reserves for breakpoints
- the hook runs before it
- walk the backtrace up to your frame
- panic_fmt for an earlier stop
basics
~20 sBreak on std's rust_panic, the function std's source marks for panic breakpoints, then run bt and select the first frame in your crate. The hook has already printed the message by then; core::panicking::panic_fmt stops earlier for formatted panics.
solid answer
~40 sSet a breakpoint on `rust_panic` before `run`. Std keeps it `#[inline(never)]` and its source comment calls it the function on which to slap your breakpoints; in a 1.99 binary the demangled symbol is `__rustc::rust_panic`. Ordinary panics all reach it: `panic!`, a failed `unwrap`, an out-of-bounds index, `panic_any`, even `resume_unwind`, and also builds with `panic = "abort"`. When it fires, `bt` shows std's frames on top and your code below; select the first frame in your crate to see the line and its locals. The panic hook has already printed the message by then. To stop before that, break on `core::panicking::panic_fmt`, which formatted panics, failed unwraps and bounds checks pass through.
code
plaintext · 11 linesframe #0: __rustc::rust_panic at panicking.rs:887:25 [opt]
frame #1: std::panicking::panic_with_hook at panicking.rs:840:5 [opt]
frame #2: std::panicking::panic_handler::{closure#0} at panicking.rs:688:13 [opt]
frame #3: std::sys::backtrace::__rust_end_short_backtrace::<std::panicking::panic_handler::{closure#0}, !> at backtrace.rs:182:18 [opt]
frame #4: __rustc::rust_begin_unwind at panicking.rs:679:5 [opt]
frame #5: core::panicking::panic_fmt at panicking.rs:80:14 [opt]
frame #6: core::panicking::panic_bounds_check at panicking.rs:271:5 [opt]
frame #7: <usize as core::slice::index::SliceIndex<[orders::Order]>>::index(self=3, slice=size=2) at index.rs:238:10
frame #8: <[orders::Order] as core::ops::index::Index<usize>>::index(self={data_ptr:{id:1, sku:"A-100", qty:2}, length:2}, index=3) at index.rs:19:15 [inlined]
frame #9: <alloc::vec::Vec<orders::Order> as core::ops::index::Index<usize>>::index(self=size=2, index=3) at mod.rs:3934:9
frame #10: orders::main at orders.rs:42:26go deeper
Recall that you break on std's rust_panic before running, then use bt to find your own frame below std's.
Explain the order inside std: panic_fmt, the panic handler, the hook printing the message, then rust_panic, and why panic_fmt stops earlier for formatted panics.
Cover the edges: abort builds still pass through rust_panic, panic_any and resume_unwind skip panic_fmt, other threads need thread apply all bt, and v0 names need a current GDB.
Weigh keeping production binaries debuggable enough to stop on a panic in a reproduction, with symbols and line tables, against size and the habit of relying on logged backtraces alone.
## The path a panic takes through std A **panic** in Rust is a normal function call chain inside the standard library, which is why a debugger can stop on it. Reading `std/src/panicking.rs` in Rust 1.99 and a backtrace of an out-of-bounds index gives this order: 1. Your code fails, for example `orders[idx]` with `idx` past the end, and calls into core: `core::panicking::panic_bounds_check`, then `core::panicking::panic_fmt`. `Option::unwrap` on `None` goes through `core::panicking::panic`, which also calls `panic_fmt`. 2. `panic_fmt` calls std's `#[panic_handler]`, whose symbol is `rust_begin_unwind`. 3. The handler calls `panic_with_hook`, which runs the **panic hook**: the default one prints `thread 'main' ... panicked at ...` and the message. 4. Only then does it call `rust_panic`, which hands the payload to the panic runtime to unwind or abort. Two other entry points join at the end: `std::panic::panic_any` goes through std's `begin_panic` and then `panic_with_hook`. `std::panic::resume_unwind` skips the hook and calls `rust_panic` directly. ## Choosing the breakpoint | Breakpoint | Fires for | Message already printed? | |---|---|---| | `rust_panic` | every ordinary panic, including `panic_any` and `resume_unwind` | yes, the hook ran first | | `core::panicking::panic_fmt` | `panic!`, failed `unwrap`/`expect`, bounds checks, assertion failures | no | `rust_panic` is the one std itself recommends. Its source carries the comment "A function with a fixed suffix ... on which to slap yer breakpoints", and it is marked `#[inline(never)]` so that it always exists as a real function. In GDB you set it with `break` before `run`. If your GDB does not match the bare name, `info functions rust_panic` shows how it spells the symbol. In a 1.99 binary the demangled name is `__rustc::rust_panic`, and GDB's linespecs treat the first path segment as a crate name. Since Rust 1.97, Rust symbols use the **v0 mangling** scheme by default. The release notes warn that older debuggers may fail to demangle them, so an old GDB is the first thing to suspect when Rust names look like `_RNv...` strings. ## Finding your line When the breakpoint fires, the top frames belong to std and core. Run `bt` and look for the first frame in your crate. For the index bug, frames 0 to 6 are `rust_panic`, `panic_with_hook`, the handler closure, a short-backtrace marker, `rust_begin_unwind`, `panic_fmt` and `panic_bounds_check`. Then come the slice and `Vec` indexing impls, and then `orders::main at orders.rs:42`. Select that frame (`frame` with its number, or `up` repeatedly) and print the locals. Under rust-gdb, the `Vec` you indexed shows its real size, which is usually the whole story. ## Edge cases worth knowing - **Abort builds.** With `-C panic=abort` the hook still runs and `rust_panic` is still called; the abort happens inside the panic runtime after it. An unstable immediate-abort mode, available only on nightly, compiles std's panic path differently and is outside this answer. - **Panics that must abort.** Std aborts before `rust_panic` in two cases. One is a panic raised while the panic hook is running. The other is a panic that cannot unwind, which std's source describes as a thread panicking while running destructors or unwinding through a nounwind function such as an `extern "C"` one. - **Threads.** A panic on a spawned thread stops in `rust_panic` on that thread. `thread apply all bt` shows what the other threads were doing at that moment. - **No breakpoint at all.** If the panic unwinds out of `main`, the process simply exits with code 101 and there is nothing left to inspect. That is why you set the breakpoint before `run`. - **Optimised builds.** The std frames are optimised in every build, but your own frame's locals are only reliable in a build with full debug info and no optimisation. ## Doing it for a failing test A failing `assert_eq!` in a `#[test]` unit test is a panic too. The test harness runs each test on its own thread and catches the panic so the next test can run. The panic still passes through `rust_panic` on that thread first. `cargo test` prints the path of the test executable it runs, under `target/debug/deps/`. Start that file under rust-gdb with the test's name as a program argument, set the breakpoint, run, and walk up from `rust_panic` to the assertion. GDB stops the whole process, so the other test threads freeze where they are. What the hook prints, when a program should panic and how unwinding differs from abort are questions about the language. Under rust-gdb the job is narrower: stop at std's choke point and walk up to your own frame.
- Why does the panic message appear on stderr before the `rust_panic` breakpoint fires?In std's `panic_with_hook`, the hook runs first, the default hook printing the location and message, and `rust_panic` is called only afterwards to start unwinding or abort. To stop before anything is printed, break on `core::panicking::panic_fmt`, which formatted panics, failed unwraps and bounds checks pass through on their way to the handler.
- A breakpoint on `core::panicking::panic_fmt` never fires, yet the program panics; how?Some panics bypass it. `std::panic::panic_any` goes through std's `begin_panic` straight to `panic_with_hook`, and `std::panic::resume_unwind` skips the hook and calls `rust_panic` directly. A breakpoint on `rust_panic` catches those as well, at the cost of stopping after the hook.
- Rust frame names in your GDB look like `_RNvCs...` strings; what changed?Since Rust 1.97, rustc uses the v0 symbol mangling scheme by default, and the release notes warn that older debuggers and profilers may fail to demangle it. Use a newer GDB. Breakpoints by source line still work, because they come from the debug info rather than from symbol names.
- Breaking on rust_panic stops before the panic message is printed
- With panic = "abort" there is no std function left to break on
- set_hook is the function to put the debugger breakpoint on
- The top frame at the breakpoint is the line that panicked
- resume_unwind and panic_any never reach rust_panic
Assumes Rust 1.99; since Rust 1.97 symbols use v0 mangling by default, which older debuggers may fail to demangle.