I am writing a new crate. I wrote some tests for it and run the tests using cargo test
. After that, some test_xxx executables are generated in target
folder. I have enabled the debug option in Cargo.toml
. By running gdb targets/test_xxx
, I can list and debug the code in test_xxx file. However, I could not step into the functions in the crate. There is no debug information. How can build/link the crate to include its debug information?
Follow up
I did more reasearch and found my original question is inaccurate. It doesn't happen to all methods in an external crate, but only those generic ones. I filed an issue with the detail steps: https://github.com/rust-lang/rust/issues/19228. Please read comments there.
This is what I am doing to debug the generic method:
- Write the tests in the tests folder as usual. Run "cargo test" to check if it is successful.
- If it is failed, I move the test code into the crate lib.rs file and build a binary using "rustc -g src/lib.rs".
- Using
gdb
to debuglib
.
Your question is a bit fuzzy, so I'll describe what I did:
Create a new crate:
Add a small bit of code:
Created an external test; one that lives in
tests
. This matches your comment abouttest_XXX
, as best I can guess:Run the tests:
Open it up in my debugger:
Set a breakpoint on methods in the crate and run it:
This indicates that I was able to set breakpoints and debug in my crate.
EDIT For the followup comment about dependent crates:
I added this to my
Cargo.toml
:Along with this code:
And this test (still in the external test file):
Built and ran the tests as before, then opened it in the debugger as before:
Using GDB instead of LLDB
You can also use GDB, but it looks like it may be a bit harder. After setting a breakpoint in the primary crate's code, I stepped in and saw the function name seem to be the mangled version. You can use that as a breakpoint though.
Note my regex breakpoint doesn't work as intended, and also note the line number of the real breakpoint: