如何使用GDB或LLDB等调试器在Rust中调试板条箱? [英] How to use a debugger like GDB or LLDB to debug a crate in Rust?
问题描述
我正在写一个新的板条箱.我为此编写了一些测试,并使用cargo test
运行测试.之后,在target
文件夹中会生成一些test_xxx
可执行文件.我已经在Cargo.toml
中启用了调试选项.通过运行gdb targets/test_xxx
,我可以列出和调试test_xxx
可执行文件中的代码.但是,我无法进入板条箱中的功能.没有调试信息.如何建立/链接板条箱以包含其调试信息?
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 the 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 the test_xxx
executable. However, I could not step into the functions in the crate. There is no debug information. How can I build/link the crate to include its debug information?
推荐答案
您的问题有点模糊,因此我将描述自己所做的事情:
Your question is a bit fuzzy, so I'll describe what I did:
创建一个新的箱子:
cargo new --lib so
cd so/
添加少量代码:
src/lib.rs
fn thing1(a: i32) -> i32 {
a + 2
}
fn thing2(a: i32) -> i32 {
a * a
}
pub fn do_a_thing(a: i32, b: i32) -> i32 {
thing2(b) - thing1(a)
}
创建了一个外部测试;居住在tests
中的一个.尽我所能,这与您对test_XXX
的评论相符:
Created an external test; one that lives in tests
. This matches your comment about test_XXX
, as best I can guess:
测试/alpha.rs
#[test]
fn it_works() {
assert_eq!(1, so::do_a_thing(1, 2))
}
运行测试(调整后的输出):
Run the tests (output trimmed):
% cargo test
Running target/debug/deps/alpha-b839f50a40d747a9
running 1 test
test it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
在调试器中将其打开:
% lldb target/debug/alpha-b839f50a40d747a9
在板条箱中的方法上设置正则表达式断点并运行它:
Set a regular expression breakpoint on a method in the crate and run it:
(lldb) br set -r 'do_a_thing'
Breakpoint 1: where = alpha-b839f50a40d747a9`so::do_a_thing::hd55d34fb5a87e372 + 14 at lib.rs:10:11, address = 0x0000000100001f9e
(lldb) r
Process 53895 launched: '/tmp/so/target/debug/alpha-b839f50a40d747a9' (x86_64)
running 1 test
Process 53895 stopped
* thread #2, name = 'it_works', stop reason = breakpoint 1.1
frame #0: 0x0000000100001f9e alpha-b839f50a40d747a9`so::do_a_thing::hd55d34fb5a87e372(a=1, b=2) at lib.rs:10:11
7 }
8
9 pub fn do_a_thing(a: i32, b: i32) -> i32 {
-> 10 thing2(b) - thing1(a)
11 }
Target 0: (alpha-b839f50a40d747a9) stopped.
(lldb) p b
(int) $0 = 2
(lldb) p a
(int) $1 = 1
(lldb) br set -r 'thing2'
Breakpoint 2: where = alpha-b839f50a40d747a9`so::thing2::hf3cb71248518a556 + 11 at lib.rs:6:4, address = 0x0000000100001f5b
(lldb) c
Process 53895 resuming
Process 53895 stopped
* thread #2, name = 'it_works', stop reason = breakpoint 2.1
frame #0: 0x0000000100001f5b alpha-b839f50a40d747a9`so::thing2::hf3cb71248518a556(a=2) at lib.rs:6:4
3 }
4
5 fn thing2(a: i32) -> i32 {
-> 6 a * a
7 }
8
9 pub fn do_a_thing(a: i32, b: i32) -> i32 {
Target 0: (alpha-b839f50a40d747a9) stopped.
(lldb) p a
(int) $2 = 2
这表明我能够在板条箱中设置断点并进行调试.
This indicates that I was able to set breakpoints and debug in my crate.
我将此添加到了Cargo.toml
:
[dependencies]
time = "0.1.0"
附带以下代码:
src/lib.rs
pub fn do_another_thing() -> bool {
time::precise_time_ns() % 2 == 0
}
此测试(仍在外部测试文件中):
And this test (still in the external test file):
测试/alpha.rs
#[test]
fn it_works2() {
assert_eq!(true, so::do_another_thing())
}
像以前一样构建并运行测试,然后像以前一样在调试器中将其打开:
Built and ran the tests as before, then opened it in the debugger as before:
(lldb) br set -r 'precise_time'
Breakpoint 1: where = alpha-25aace4e290c57ee`time::precise_time_ns::h21114d10b3e2c8e8 + 8 at lib.rs:161:4, address = 0x0000000100002278
(lldb) r
Process 54043 launched: '/tmp/so/target/debug/alpha-25aace4e290c57ee' (x86_64)
running 2 tests
test it_works ... Process 54043 stopped
* thread #2, name = 'it_works2', stop reason = breakpoint 1.1
frame #0: 0x0000000100002278 alpha-25aace4e290c57ee`time::precise_time_ns::h21114d10b3e2c8e8 at lib.rs:161:4
158 */
159 #[inline]
160 pub fn precise_time_ns() -> u64 {
-> 161 sys::get_precise_ns()
162 }
163
164
为什么要使用正则表达式断点?
函数名称被修饰和命名空间.我们的函数实际上称为so::do_a_thing::hd55d34fb5a87e372
或time::precise_time_ns::h21114d10b3e2c8e8
.使用正则表达式断点可以轻松选择那些,而无需考虑确切的名称.
Why a regex breakpoint?
Function names are mangled and namespaced. Our functions are actually called so::do_a_thing::hd55d34fb5a87e372
or time::precise_time_ns::h21114d10b3e2c8e8
. Using a regex breakpoint selects those easily without thinking about the exact name.
您还可以使用LLDB的自动完成功能
You can also use LLDB's autocompletion
(lldb) b so::<TAB>
Available completions:
so::do_a_thing::hfb57d28ba1650245
so::do_another_thing::hace29914503d7a2f
so::thing1::ha6f7818d54de28d4
so::thing2::h2518577906df58fd
(lldb) b time::<TAB>
Available completions:
time::precise_time_ns::h21114d10b3e2c8e8
time::sys::inner::mac::get_precise_ns::h64c88ed88da4ac18
time::sys::inner::mac::info::_$u7b$$u7b$closure$u7d$$u7d$::ha03be28d018f231b
time::sys::inner::mac::info::h364c1a0ef2ef1f0c
使用GDB代替LLDB
您也可以使用GDB,但看起来可能会有点困难.在主包装箱的代码中设置断点后,我介入,发现函数名称似乎是错误的版本.不过,您可以将其用作断点.
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:
(gdb) rbreak precise_time_ns
u64 _ZN4time15precise_time_nsE()();
static u64 _ZN4time15precise_time_ns18os_precise_time_nsE()();
Breakpoint 1 ('_ZN4time15precise_time_ns18os_precise_time_ns13closure.24322E') pending.
<function, no debug info> time::precise_time_ns::os_precise_time_ns::closure.24322;
(gdb) b _ZN4time15precise_time_nsE
Breakpoint 2 at 0x1000052a0: file lib.rs, line 172.
(gdb) r
Starting program: /private/tmp/so/target/alpha-e0c6f11f426d14d2
running 2 tests
test it_works ... ok
[Switching to process 49131 thread 0xd03]
Breakpoint 1, _ZN4time15precise_time_nsE () at lib.rs:172
172 pub fn precise_time_ns() -> u64 {
(gdb) list
167
168 /**
169 * Returns the current value of a high-resolution performance counter
170 * in nanoseconds since an unspecified epoch.
171 */
172 pub fn precise_time_ns() -> u64 {
173 return os_precise_time_ns();
174
175 #[cfg(windows)]
176 fn os_precise_time_ns() -> u64 {
这篇关于如何使用GDB或LLDB等调试器在Rust中调试板条箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!