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.

  1. Run cargo test. If you want to work on my_test(), your best plan is

    cargo test my_test
  2. Look at the cargo test output. This will give the binary path starting with target/ for the test binary Cargo created.

  3. rust-gdb binary-path

  4. Set a breakpoint at the start of your test.

    break my_crate::my_test

    If you get an error that the function doesn't exist, something has gone wrong. Check the previous steps.

  5. Tell GDB

    run --test my_test

    You 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.