Rust Debug Library with GDB

I have lib inside /src/lib.rs . I want to prepare this for debugging using GDB.

 rustc -g --crate-type lib libr.rs 

Have I been looking for a way to do this. The output file has the following name liblib.rlib

Now running GDB - executing file lib.rs tells me that it is not in an executable format and the same with the strange liblib.rlib file. I know that this is not an executable file, but I also do not know the alternative file that I can start.

So how can I start debugging lib in Rust now?

+5
source share
1 answer

You cannot debug anything but an executable file. Debuggers work by checking the memory of a running process; without an executable, you cannot have a process.

Assuming these two files:

Src / lib.rs

 pub fn add_one(i: u8) -> u8 { i + 2 } #[test] fn inline_test() { assert_eq!(2, foo::add_one(1)); } 

tests /awesome.rs

 extern crate foo; #[test] fn a_test() { assert_eq!(6, foo::add_one(5)); } 

When you run cargo build or cargo test , test binaries will be created in the target/debug/ directory. In this case, there is one binary code called foo-69521add8c82059a , and one is awesome-4a24b21e22bc042a . Running any program launches a set of tests. All Rust tests work as follows: the executable executable is generated and runs it (possibly with the correct set of command line flags) will run the test.

This executable is what you need to debug in GDB or LLDB:

 $ rust-lldb target/debug/awesome-4a24b21e22bc042a (lldb) br set -r '.*add_one.*' (lldb) r Process 59413 launched: '/private/tmp/foo/target/debug/awesome-4a24b21e22bc042a' (x86_64) running 1 test Process 59413 stopped * thread #2: tid = 0xe9637, 0x0000000100038a3e awesome-4a24b21e22bc042a`foo::add_one::ha28bd7bf9dda9f1d + 14 at lib.rs:2, name = 'a_test', stop reason = breakpoint 1.1 frame #0: 0x0000000100038a3e awesome-4a24b21e22bc042a`foo::add_one::ha28bd7bf9dda9f1d + 14 at lib.rs:2 1 pub fn add_one(i: u8) -> u8 { -> 2 i + 2 3 } 

rustc -g --crate-type lib libr.rs

This avoids the use of Cargo, which most people will not want to do. An important aspect of this line is the -g flag, which instructs the compiler to add debugging information. cargo build or cargo test are compiled in debug mode by default. You can also create your tests in release mode .

+5
source

All Articles