有没有办法在gdb或lldb中直接运行 Cargo构建的程序?

Is there a way to directly run the program built by Cargo in gdb or lldb?

有没有办法在gdb中立即运行Cargo构建的程序? cargo 功能很多,而且可以运行 程序,所以看起来很有道理。

预期的命令类似于 cargo debug

不,Cargo 中目前没有这样的功能。

有几个问题 (1, 2) 可以更好地支持类似问题。

你现在能做的最好的事情就是写一个 Cargo subcommand 完全满足你的需要。

解决方法

无需创建子命令,您可以将一些功能粘合在一起以获得接近的效果。

首先为您的架构配置

.cargo/config

[target.x86_64-apple-darwin]
runner = ["/tmp/gg/debugger.sh"]

然后写一个小脚本作为测试运行ner。如果设置了环境变量,它将启动调试器,否则它将只是 运行 程序:

#!/bin/bash

if [[ -z $DEBUG ]]; then
    exec $*
else
    exec lldb $*
fi

那么你只需要设置变量:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running target/debug/deps/gg-e5d6c92730ca3c30

running 0 tests

$ DEBUG=1 cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/gg-e5d6c92730ca3c30
(lldb) target create "/private/tmp/gg/target/debug/deps/gg-e5d6c92730ca3c30"
Current executable set to '/private/tmp/gg/target/debug/deps/gg-e5d6c92730ca3c30' (x86_64).
(lldb)

另请参阅: