简体   繁体   English

如何在Windows中设置GDB以调试Rust程序?

[英]How to set up GDB for debugging Rust programs in Windows?

如何配置GDB以在Windows中调试Rust程序,包括设置Rust pretty-printers,以及在IDE或命令行中进行调试?

Rust installation Rust安装

First, you need to compile your programs with the Windows GNU ABI Rust installation. 首先,您需要使用Windows GNU ABI Rust安装来编译程序。 The MSVC ABI uses a different debugging format than the one GDB understands, so that won't work. MSVC ABI使用与GDB理解的不同的调试格式,因此不起作用。 MSVC ABI compiled programs would have to debugged with Visual Studio (or possibly LLDB, in the future). MSVC ABI编译的程序必须使用Visual Studio(或将来可能的LLDB)进行调试。

GDB GDB

Second step is to get GDB itself. 第二步是获取GDB本身。 The recommended option is to get it from either TDM-GCC or mingw-w64: 建议的选择是从TDM-GCC或mingw-w64获取它:

  • TDM-GCC ( http://tdm-gcc.tdragon.net/ ): Has available a download package with GDB only (without GCC or the other tools, which you don't need). TDM-GCC( http://tdm-gcc.tdragon.net/ ): 提供带GDB的下载包(没有GCC或其他您不需要的工具)。 Special keys work in Windows terminal only. 特殊键仅适用于Windows终端。 Recommended GDB for use with Eclipse/RustDT. 推荐的GDB与Eclipse / RustDT一起使用。
  • Mingw-w64 ( http://mingw-w64.org/ ): Special keys work in Windows terminal only. Mingw-w64( http://mingw-w64.org/ ):特殊键仅适用于Windows终端。 Recent versions seems to have a bug: command-line arguments with spaces in them are parsed incorrectly. 最近的版本似乎有一个错误:其中包含空格的命令行参数被错误地解析。
  • Cygwin: Not recommended. Cygwin:不推荐。 Special keys work in Windows terminal and bash terminal. 特殊键可在Windows终端和bash终端中使用。 Paths have to be specified in Cygwin format, and this seems to break a few things. 路径必须以Cygwin格式指定,这似乎打破了一些事情。 Doesn't work properly with Eclipse/RustDT. 使用Eclipse / RustDT无法正常工作。

Enabling pretty-printers 启用漂亮的打印机

Rust provides some extensions to GDB to enable a better display of certain Rust native types, such as enums, slices, and vectors. Rust为GDB提供了一些扩展,以便更好地显示某些Rust本机类型,例如枚举,切片和向量。 With the pretty-printers, variables of this type will be displayed in a structured way, instead of the low-level representation. 使用漂亮的打印机,这种类型的变量将以结构化方式显示,而不是低级表示。 For more info see https://michaelwoerister.github.io/2015/03/27/rust-xxdb.html . 有关详细信息,请参阅https://michaelwoerister.github.io/2015/03/27/rust-xxdb.html

The pretty-printers are only included in the Linux (and Mac OS?) distributions of Rust, not the Windows one ( Issue reported ). 漂亮的打印机只包含在Rust(而不是Windows)的Linux(和Mac OS?)发行版中( 问题报道 )。 But they can be made to work in Windows. 但它们可以在Windows中运行。

Download the Linux Rust archive ( https://www.rust-lang.org/downloads.html ), extract and locate the rustc/lib/rustlib/etc directory inside. 下载Linux Rust存档( https://www.rust-lang.org/downloads.html ),解压缩并找到里面的rustc/lib/rustlib/etc目录。 Now copy the etc folder to $RUST/bin/rustlib , where $RUST is the location of your Rust installation. 现在将etc文件夹复制到$RUST/bin/rustlib ,其中$ RUST是Rust安装的位置。 The Python scripts there will then be located in $RUST/bin/rustlib/etc . 那里的Python脚本将位于$RUST/bin/rustlib/etc

If you only intend to use GDB from within RustDT, and have RustDT 0.4.1 or above, you can skip to the next section: "Using GDB in Eclipse with RustDT" . 如果您只打算在RustDT中使用GDB,并且具有RustDT 0.4.1或更高版本,则可以跳到下一节:“在Eclipse中使用带有RustDT的GDB”

Now, GDB needs to be configured to load these scripts. 现在,需要配置GDB来加载这些脚本。 Locate the gdbinit file of your GDB installation (for TDM-GCC, should be gdb64\\bin\\gdbinit , for mingw-w64: mingw64\\etc\\gdbinit ). 找到GDB安装的gdbinit文件(对于TDM-GCC,应该是gdb64\\bin\\gdbinit ,对于mingw-w64: mingw64\\etc\\gdbinit )。 Now add the following text to the end of the file: 现在将以下文本添加到文件的末尾:

python
print "---- Loading Rust pretty-printers ----"

sys.path.insert(0, "$RUST_GDB_ETC")
import gdb_rust_pretty_printing
gdb_rust_pretty_printing.register_printers(gdb)

end

But replace $RUST_GDB_ETC with the location of the etc directory with the Python files, for example D:/devel/tools.Rust/rust/bin/rustlib/etc . 但是将$RUST_GDB_ETC替换$RUST_GDB_ETC带有Python文件的etc目录的位置,例如D:/devel/tools.Rust/rust/bin/rustlib/etc Note, even though it's a Windows path, make sure you use the forward-slash ('/') as a path separator, to avoid escape issues in that string literal. 请注意,即使它是Windows路径,也要确保使用正斜杠('/')作为路径分隔符,以避免该字符串文字中的转义问题。

To verify this works, start gdb. 要验证这是否有效,请启动gdb。 If you see the "---- Loading Rust pretty-printers ----" message before the prompt and no Python errors after, things should be working. 如果你在提示之前看到“----加载Rust pretty-printers ----”消息并且之后没有Python错误,那么事情就应该正常了。 To confirm, type the command info pretty-printer . 要确认,请键入命令info pretty-printer There should be a line with "rust_pretty_printer_lookup_function" in the output if the pretty-printers were loaded successfully. 如果漂亮的打印机成功加载,输出中应该有一行“rust_pretty_printer_lookup_function”。

Using GDB in Eclipse with RustDT 在Eclipse中使用带有RustDT的GDB

If you successfully completed the steps before, you are nearly good to go to use GDB from within RustDT. 如果您之前成功完成了这些步骤,那么您可以从RustDT中使用GDB。 Just a few details: 只是一些细节:

  • If using TDM GDB, the GDB executable to be started must be the one at $TDM_ROOT/gdb64/bin/gdb.exe , not the ones at $TDM_ROOT/bin/gdb.exe or $TDM_ROOT/bin/gdb64.exe , because these last two are wrappers for the correct executable, and they don't work properly when RustDT/CDT starts the GDB process. 如果使用TDM GDB,要启动的GDB可执行文件必须是$TDM_ROOT/gdb64/bin/gdb.exe ,而不是$TDM_ROOT/bin/gdb.exe$TDM_ROOT/bin/gdb64.exe 。最后两个是正确可执行文件的包装器,当RustDT / CDT启动GDB进程时它们无法正常工作。

  • If using RustDT 0.4.1 or above, the pretty-printers will be configured automatically, as long as RustDT finds them in ${RUST_ROOT}/lib/rustlib/etc . 如果使用RustDT 0.4.1或更高版本,只要RustDT在${RUST_ROOT}/lib/rustlib/etc找到它们,就会自动配置漂亮的打印机。 You can verify this worked by starting a debug launch, opening the corresponding "gdb traces" console page in the Console view, and searching for the string "Registering Rust pretty-printers for Windows": 您可以通过启动调试启动,在Console视图中打开相应的“gdb traces”控制台页面,并搜索字符串“Registering Rust pretty-printers for Windows”来验证这一点: RustDT GDB调试跟踪

  • For RustDT versions prior to 0.4.1, to enable the pretty-printers, you much configure the launch configuration to run the gdbinit file that you just modified in the previous section. 对于0.4.1之前的RustDT版本,为了启用漂亮的打印机,您需要配置启动配置以运行刚刚在上一节中修改过的gdbinit文件。 The default gdbinit is not executed when GDB is started by RustDT/CDT, only the one you specify in the configuration. 当RustDT / CDT启动GDB时,不会执行默认的gdbinit,只会在配置中指定。 So change the "GDB command file" field from .gdbinit to, for example D:\\devel\\tools\\TDM-GDB\\gdb64\\bin\\gdbinit : 因此,将.gdbinit的“GDB命令文件”字段.gdbinit为,例如D:\\devel\\tools\\TDM-GDB\\gdb64\\bin\\gdbinit

RustDT调试启动配置

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

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