我找到了多个指南,解释如何调试由
cargo build
生成的可执行文件,最好的是使用 Visual Studio Code 和 MSVC 调试器在 Windows 上调试 Rust。 但是,当我遇到问题时,我倾向于编写单元测试(我已经在 Java、JavaScript、Ruby 等中完成了),然后进行调试。不幸的是,我在 Rust 中找不到任何方法来做到这一点。如何配置环境来调试测试?
我不是在谈论在我的测试中添加
println!
语句,因为我已经知道如何做到这一点。我也不是在谈论添加新的断言,因为它们驻留在测试中,而不是在测试的代码中。
我想要的是在我的测试中称为
的代码上使用 VS Code 调试器。
。编译后,它们位于./target/debug/$name-$hash
。
Visual Studio 代码
任务.json
{
"type": "shell",
"label": "cargo test build",
"command": "cargo",
"args": [
"test", "--no-run"
],
"problemMatcher": [
"$rustc"
]
}
启动.json
{
"name": "Run Test Debugger",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/buggin-70708b3916187eeb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cargo test build",
}
工作
cargo test --no-run
在 Windbg 中打开构建的可执行文件并打开源文件。查找哈希值是最烦人的方面。我所知道的最好的解决方案是编写一个小脚本来构建测试,然后根据最新的测试可执行文件查找测试可执行文件。我的 Powershell 技能不足以完成这项任务,我也不知道如何直接将其与 VS Code 或 Windbg 集成。
Cargo 有一些未解决的问题可以帮助识别文件:
build --test
和 test --no-run
生成确定性文件名 (#1924)
在
nushell 你可以做这样的事情。
export def open-in-windbg [executable: path] {
# NOTE: change it to your executable location if it differs
~/AppData/Local/Microsoft/WindowsApps/WinDbgX.exe $executable
}
export def --wrapped "cargo test-windbg" [...args: string] {
let executable = (cargo t ...$args --no-run e>| parse --regex 'Executable.*\((?<name>.+)\)' | get 0.name)
open-in-windbg $executable
}
然后打电话
cargo test-windb
额外的货物测试旗帜也应该没问题
cargo test-windbg --features myfeat
注意:我还没有设法让 Windbg 打开源文件。