Debugging A Failing Rust Test With GDB
2025-11-10 21:42:56 PST (last update 2025-11-15 15:36:05 PST)
Bart Massey 2025
[Updated after a few days with corrected info.]
This is a quick note to future me and others who may be wishing to use GDB to debug a failing Rust test.
When you run cargo test, Cargo builds a separate test
binary linked against the collection of tests in your source binary or
library, and another test binary for tests in the tests/
directory. It helpfully prints a path in your target/
directory for each test binary it creates. This is your way in.
Let's assume you are working in a crate named my_crate
(look at Cargo.toml for the crate name) with a test named
my_test.
Run
cargo test. If you want to work onmy_test(), your best plan iscargo test my_testLook at the
cargo testoutput. This will give the binary path starting withtarget/for the test binary Cargo created.rust-gdbbinary-pathSet a breakpoint at the start of your test.
break my_crate::my_testIf you get an error that the function doesn't exist, something has gone wrong. Check the previous steps.
Tell GDB
run --test my_testYou should now be looking at the start of your test.
Acknowledgements
The information here is largely obtained from this Stack Overflow answer. Many thanks to Chris Morgan there for reminding me how this process works.