简体   繁体   English

使用gdb调试Rust

[英]Debugging Rust with gdb

I'm aware of the debugging Rust questions here on StackOverflow and I also used gdb before with Go. 我知道在StackOverflow上调试Rust问题,我之前也使用了gdb。 However, I'm running into a problem where it seems gdb is unable to locate the debug symbols. 但是,我遇到了一个问题,似乎gdb无法找到调试符号。

Consider this complex program in main.rs ? main.rs考虑这个复杂的程序?

pub fn main () {
    println!("run");
}

I compile it with debug symbols 我用调试符号编译它

rustc -g main.rs

Then I run gdb 然后我运行gdb

gdb main

This gives the first clue that something with the loading of debug symbols is not quite right. 这给出了第一个线索,即加载调试符号的东西不太正确。

在此输入图像描述

Now when I'm in gdb and type 现在,当我在gdb和键入

list

it leaves me with some C code which isn't what I expect. 它留给我一些C代码,这不是我所期望的。

在此输入图像描述

What am I doing wrong? 我究竟做错了什么? My gdb version is 7.7 and I'm on OS X 10.9.2 (13C64). 我的gdb版本是7.7,我在OS X 10.9.2(13C64)。 My rustc version is rustc 0.11.0-pre (3035d8dfb13077e195eb056568183911c90c1b4b 2014-07-02 21:26:40 +0000) 我的rustc版本是rustc 0.11.0-pre(3035d8dfb13077e195eb056568183911c90c1b4b 2014-07-02 21:26:40 +0000)

It may also be helpful to see the output of `gdb --configuration`` 查看`gdb --configuration``的输出可能也会有所帮助

$ gdb --configuration
This GDB was configured as follows:
   configure --host=x86_64-apple-darwin13.1.0 --target=x86_64-apple-darwin13.1.0
             --with-auto-load-dir=:${prefix}/share/auto-load
             --with-auto-load-safe-path=:${prefix}/share/auto-load
             --with-expat
             --with-gdb-datadir=/usr/local/share/gdb (relocatable)
             --with-jit-reader-dir=/usr/local/lib/gdb (relocatable)
             --without-libunwind-ia64
             --without-lzma
             --with-python=/System/Library/Frameworks/Python.framework/Versions/2.7
             --with-separate-debug-dir=/usr/local/lib/debug (relocatable)
             --with-zlib
             --without-babeltrace

Same question, later Rust version (1.0.0-beta), totally different answer: 同样的问题,后来Rust版本(1.0.0-beta),完全不同的答案:

In GDB, when debugging a Rust executable, break main sets a breakpoint in some setup code that is part of the Rust standard library. 在GDB中,在调试Rust可执行文件时, break main在某些设置代码中设置断点,该代码是Rust标准库的一部分。 This isn't what you want. 这不是你想要的。

Instead type: break $YOUR_CRATE::main , substituting the name of your program for $YOUR_CRATE . 相反,键入: break $YOUR_CRATE::main用于替换你的程序的名字$YOUR_CRATE

Ok, I figured out what was wrong. 好的,我弄清楚出了什么问题。 I have to manually emit the main.o file. 我必须手动发出main.o文件。 I thought the -g parameter would just cut it. 我认为-g参数只会削减它。

Now that I run 现在我跑了

rustc -g main.rs --emit="obj,link"

I can run 我可以跑

gdb main

And everything works like a charme. 一切都像魅力一样。

I created two aliases for my bash to make things simple: 我为我的bash创建了两个别名,使事情变得简单:

alias rd='rustc -g --emit="obj,link"'

compile_and_run() {
     rustc -g --emit="obj,link" $1 && gdb ${1%.*}
}

alias rdr=compile_and_run

Now I can just call rdr main.rs and it will start debugging main.rs with gdb. 现在我可以调用rdr main.rs ,它将开始使用gdb调试main.rs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM