Windows下使用GDB + VS Code调试Rust程序

在Windows下调试Rust程序,我就在这里写个简单的指南(以Rust 1.10为例)。

本文参考Stackoverflow
http://stackoverflow.com/questions/33570021/how-to-set-up-gdb-for-debugging-rust-programs-in-windows

Rust目前支持使用LLDB和GDB调试(貌似LLVM工具链越来越受欢迎),可惜LLDB目前没有Windows版(后面会有的,耐心等待),所以在Windows下调试Rust程序,你需要GDB。

一、下载GDB
使用TDM-GCC的GDB(需要支持Python扩展,MinGW64的GDB貌似不支持)
https://sourceforge.net/projects/tdm-gcc/files/GDB/
我是64位系统,所以下载的是GDB64,32位的请下载GDB32

二、解压GDB
压缩包里有bin、gdb64、share,直接解压到Rust安装目录(我的是D:\Programming\Rust,下面以此为例)。

三、配置rust-gdb
下载rust源代码,将src目录下的etc目录解压到D:\Programming\Rust\lib\rustlib目录下。
修改D:\Programming\Rust\gdb64\bin目录下的gdbinit,将下面代码加入gdbini末尾:
python
print "---- Loading Rust pretty-printers ----"

sys.path.insert(0, "D:/Programming/Rust/lib/rustlib/etc")
import gdb_rust_pretty_printing
gdb_rust_pretty_printing.register_printers(gdb)

end

四、测试
执行gdb,如果没有什么错误且输出:
---- Loading Rust pretty-printers ----
说明gdb配置好了。

五、使用gdb
写一个hello world:
notepad hello.rs
rustc -g hello.rs
gdb hello.exe
 

命令行实用GDB调试Rust程序

命令行实用GDB调试Rust程序




六、配置VS Code
安装Native Debug插件(https://marketplace.visualstudio.com/items?itemName=webfreak.debug
ext install debug

用Cargo创建一个工程
cargo hello --bin

用VS Code打开该工程,然后点击左侧面板上的调试按钮,调试配置中类型选择GDB。
然后修改Native Debug插件生成的launch.json文件,将target指向Debug版本的exe程序。launch.json文件大致如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "gdb",
"request": "launch",
"target": "./target/debug/hello.exe",
"cwd": "${workspaceRoot}"
}
]
}

然后在源文件中设断点,开始调试吧。
 

最终界面

最终界面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值