Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging 如何使用GDB或LLDB之类的调试器在Rust中调试板条箱?_Debugging_Rust_Gdb_Lldb - Fatal编程技术网

Debugging 如何使用GDB或LLDB之类的调试器在Rust中调试板条箱?

Debugging 如何使用GDB或LLDB之类的调试器在Rust中调试板条箱?,debugging,rust,gdb,lldb,Debugging,Rust,Gdb,Lldb,我正在写一个新箱子。我为它编写了一些测试,并使用cargo test运行测试。之后,在target文件夹中生成一些test\u xxx可执行文件。我已经在Cargo.toml中启用了调试选项。通过运行gdb targets/test\u xxx,我可以在test\u xxx可执行文件中列出并调试代码。但是,我无法进入板条箱中的功能。没有调试信息。如何构建/链接板条箱以包含其调试信息?您的问题有点模糊,因此我将描述我所做的: 创建一个新的板条箱: cargo new--lib so cd so/

我正在写一个新箱子。我为它编写了一些测试,并使用
cargo test
运行测试。之后,在
target
文件夹中生成一些
test\u xxx
可执行文件。我已经在
Cargo.toml
中启用了调试选项。通过运行
gdb targets/test\u xxx
,我可以在
test\u xxx
可执行文件中列出并调试代码。但是,我无法进入板条箱中的功能。没有调试信息。如何构建/链接板条箱以包含其调试信息?

您的问题有点模糊,因此我将描述我所做的:

创建一个新的板条箱:

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)
}
#[test]
fn it_works() {
    assert_eq!(1, so::do_a_thing(1, 2))
}
pub fn do_another_thing() -> bool {
    time::precise_time_ns() % 2 == 0
}
#[test]
fn it_works2() {
    assert_eq!(true, so::do_another_thing())
}
创建一个外部测试;生活在
测试中的人。这与您关于
测试XXX
的评论相匹配,我可以猜到:

测试/alpha.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)
}
#[test]
fn it_works() {
    assert_eq!(1, so::do_a_thing(1, 2))
}
pub fn do_another_thing() -> bool {
    time::precise_time_ns() % 2 == 0
}
#[test]
fn it_works2() {
    assert_eq!(true, so::do_another_thing())
}
运行测试(输出修剪):

%货物测试
运行target/debug/deps/alpha-b839f50a40d747a9
运行1测试
测试它是否有效。。。好啊
测试结果:可以。1人通过;0失败;忽略0;0测量值;0被过滤掉
在我的调试器中打开它:

%lldb目标/debug/alpha-b839f50a40d747a9
在板条箱中的方法上设置一个正则表达式断点并运行它:

(lldb)br set-r“做一件事”
断点1:where=alpha-b839f50a40d747a9`so::do_a_'u thing::hd55d34fb5a87e372+14位于库中。rs:10:11,地址=0x0000000100001f9e
(lldb)r
启动进程53895:'/tmp/so/target/debug/alpha-b839f50a40d747a9'(x86_64)
运行1测试
进程53895已停止
*线程#2,名称='it#u works',停止原因=断点1.1
帧#0:0x0000000100001f9e alpha-b839f50a40d747a9`so::do#a#u thing::hd55d34fb5a87e372(a=1,b=2)在库中。rs:10:11
7    }
8.
9 pub fn do_a_u o_o(a:i32,b:i32)->i32{
->10件2(b)-件1(a)
11   }
目标0:(alpha-b839f50a40d747a9)停止。
(法改会)临立会
(int)$0=2
(法改会)私人执业律师
(int)$1=1
(lldb)br set-r'thing2'
断点2:where=alpha-b839f50a40d747a9`so::thing2::hf3cb7124851a556+11位于库rs:6:4,地址=0x0000000100001f5b
(lldb)c
进程53895恢复
进程53895已停止
*线程#2,名称='it#u works',停止原因=断点2.1
帧#0:0x0000000100001f5b alpha-b839f50a40d747a9`so::thing2::hf3cb71248518a556(a=2)位于库rs:6:4
3    }
4.
5 fn thing2(a:i32)->i32{
->6 a*a
7    }
8.
9 pub fn do_a_u o_o(a:i32,b:i32)->i32{
目标0:(alpha-b839f50a40d747a9)停止。
(法改会)私人执业律师
(int)$2=2
这表明我能够在我的板条箱中设置断点并进行调试

对于从属板条箱 我将此添加到我的
货物中。toml

[dependencies]
time = "0.1.0"
与此代码一起:

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)
}
#[test]
fn it_works() {
    assert_eq!(1, so::do_a_thing(1, 2))
}
pub fn do_another_thing() -> bool {
    time::precise_time_ns() % 2 == 0
}
#[test]
fn it_works2() {
    assert_eq!(true, so::do_another_thing())
}
和此测试(仍在外部测试文件中):

测试/alpha.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)
}
#[test]
fn it_works() {
    assert_eq!(1, so::do_a_thing(1, 2))
}
pub fn do_another_thing() -> bool {
    time::precise_time_ns() % 2 == 0
}
#[test]
fn it_works2() {
    assert_eq!(true, so::do_another_thing())
}
像以前一样生成并运行测试,然后像以前一样在调试器中打开它:

(lldb)br set-r“精确时间”
断点1:where=alpha-25aace4e290c57ee`time::precision_time_ns::h21114d10b3e2c8e8+8位于库中。rs:161:4,地址=0x0000000100002278
(lldb)r
启动进程54043:“/tmp/so/target/debug/alpha-25aace4e290c57ee”(x86_64)
运行2个测试
测试它是否工作…进程54043已停止
*线程#2,名称='it#u works2',停止原因=断点1.1
帧0:0x0000000100002278 alpha-25aace4e290c57ee`时间::精确时间\u ns::h21114d10b3e2c8e8在库rs:161:4
158   */
159#[内联]
160发布fn精确时间()->u64{
->161系统::获取精确值()
162  }
163
164
为什么使用正则表达式断点? 函数名被损坏,名称空间被占用。我们的函数实际上被称为
so::do_a__'u thing::hd55d34fb5a87e372
time::precise_time\ns::h21114d10b3e2c8e8
。使用正则表达式断点可以轻松地选择这些函数,而不必考虑确切的名称

您还可以使用LLDB的自动完成功能

(lldb)b so::
可用的完成时间:
所以::做一件事::hfb57d28ba1650245
所以:做另一件事:hace29914503d7a2f
所以::thing1::ha6f7818d54de28d4
so::thing2::h2518577906df58fd
(lldb)b时间::
可用的完成时间:
时间:精确时间:h21114d10b3e2c8e8
时间::系统::内部::mac::获取精确数据::h64c88ed88da4ac18
时间::系统::内部::mac::信息:7b$$u7b$结束$u7d$$u7d$::ha03be28d018f231b
时间::系统::内部::mac::信息::h364c1a0ef2ef1f0c
使用GDB代替LLDB 您也可以使用GDB,但看起来可能有点难。在主板条箱的代码中设置断点后,我介入并看到函数名似乎是损坏的版本。不过,您可以将其用作断点

请注意,我的正则表达式断点不能按预期工作,还要注意实际断点的行号:

(gdb)读取精确时间
u64 _zn4time15精确时间_nsE();
静态u64时间精确时间ns18os精确时间nsE();
断点1(“ZN4time15precise\uNS18OS\uPrecise\uNS13Close.24322E”)挂起。
时间::精确的时间::操作系统精确的时间::关闭。24322;
(gdb)b_zn4时间15精确时间
断点2位于0x1000052a0:文件lib.rs,第172行。
(gdb)r
启动程序:/private/tmp/so/target/alpha-e0c6f11f426d14d2
运行2个测试
测试它是否有效…好的
[切换到进程49131线程0xd03]
断点1,lib.rs:172处的时间精确
172发布fn精确时间()->u64{
(gdb)清单
167
168 /**
169*返回高分辨率性能计数器的当前值
170*纳秒,从一个未指定的时代开始。
171  */
172发布fn精确时间()->u64{
173返回精确时间();
174
175#[cfg(窗口)]
176 fn操作系统精确时间()->u64{
调试器包装器 Rust附带了两个包装程序:
Rust lldb
Rust gdb
。它们旨在提高每个de的实用性