将 vscode 调试控制台与 Rust 结合使用

anv*_*lkv 4 debugging rust lldb visual-studio-code vscode-extensions

我有点不确定我正在使用谁的控制台。但假设我有以下调试配置:

{
    "version": "0.2.0",
    "inputs": [
        ...
    ],
    "configurations": [
        {
            "type": "lldb",
            "request": "attach",
            "name": "Attach to...",
            "program": "${workspaceFolder}/src/lib/${input:pickPackage}/target/debug/${input:pickPackage}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的程序正在 macos 终端中运行cargo run -p ...

vscode 的调试器为我提供了 lldb 的调试控制台

比如说,我正处于断点,想尝试一下......

到目前为止我尝试过生锈

script println!("{:?}", &t[op_end_index..])
  File "<input>", line 1
    println!("{:?}", &t[op_end_index..])
           ^
SyntaxError: invalid syntax
script &t[op_end_index..]
  File "<input>", line 1
    &t[op_end_index..]
    ^
SyntaxError: invalid syntax
script fn main () {println!("{:?}", &t[op_end_index..])}
  File "<input>", line 1
    fn main () {println!("{:?}", &t[op_end_index..])}
       ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我在 lldb dos 中发现了什么

script print "Here is some text"
  File "<input>", line 1
    print "Here is some text"
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Here is some text")?
Run Code Online (Sandbox Code Playgroud)

最后是错误的建议:

script print ("Here is some text")
Here is some text
Run Code Online (Sandbox Code Playgroud)

你好这个词很好,但我如何才能达到我的实际范围?

我应该使用 lldb 的script命令吗?

我的语法是什么?

更新

@ForceBru 感谢您关于 python 的提示。

我正在交互的是vscode-lldb 调试器 api

看来我应该能够在从https://github.com/vadimcn/vscode-lldb/blob/v1.6.0/MANUAL.md#rust-language-supportscript debugger.evaluate("/se t[0]")添加到我的配置后执行类似的操作"sourceLanguages": ["rust"]

但没有运气

script debugger.evaluate("/se t[0]")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/anvlkv/.vscode/extensions/vadimcn.vscode-lldb-1.6.0/adapter/debugger.py", line 8, in evaluate
    value = codelldb.evaluate_in_context(expr, True, exec_context)
  File "/Users/anvlkv/.vscode/extensions/vadimcn.vscode-lldb-1.6.0/adapter/codelldb.py", line 276, in evaluate_in_context
    return eval(code, eval_globals, eval_locals)
  File "<string>", line 1
    /se t[0]
    ^
SyntaxError: invalid syntax

Run Code Online (Sandbox Code Playgroud)

我还在Python中

script debugger.evaluate("locals().keys()")
dict_keys([])
Run Code Online (Sandbox Code Playgroud)

Pet*_*all 8

正如有人评论的那样,您的配置似乎不正确,它正在尝试将您的源代码调试为 Python。获得正确配置的最简单方法是使用提供的模板。就您而言,您可能可以跳到此答案的末尾并使用“附加”配置文件之一,但其余的可能对其他人有用。

在调试面板中,有一个下拉菜单:

VS Code LLDB:添加配置

如果您选择“添加配置...”,您将可以选择要添加的模板:

配置模板

选择“LLDB:调试货物输出”会将其添加到您的launch.json文件中:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo launch",
    "cargo": {
        "args": [
            "build",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},
Run Code Online (Sandbox Code Playgroud)

如果您的板条箱是二进制文件,您需要将其更改--lib--bin并指定要运行的二进制文件:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo launch",
    "cargo": {
        "args": [
            "build",
            "--bin=foo"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},
Run Code Online (Sandbox Code Playgroud)

对于测试,您可以选择“LLDB:调试货物测试”。它会生成类似这样的东西:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo test",
    "cargo": {
        "args": [
            "test",
            "--no-run",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},
Run Code Online (Sandbox Code Playgroud)

您可能想要删除该--lib参数,以便它将运行项目中的所有测试。您可以通过将其添加为尾随参数来过滤它以仅调试您感兴趣的测试。例如,仅运行名称包含“foo”的测试:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo test",
    "cargo": {
        "args": [
            "test",
            "--no-run",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": ["foo"]
},
Run Code Online (Sandbox Code Playgroud)

要调试已经运行的程序,可以使用模板“LLDB:按名称附加”,它会生成:

{
    "type": "lldb",
    "request": "attach",
    "name": "Attach",
    "program": "${workspaceFolder}/foo"
},
Run Code Online (Sandbox Code Playgroud)

或“LLDB:通过 PID 连接”:

{
    "type": "lldb",
    "request": "attach",
    "name": "Attach",
    "pid": "${command:pickMyProcess}" // use ${command:pickProcess} to pick other users' processes
},
Run Code Online (Sandbox Code Playgroud)

这将为您提供正在运行的进程的可过滤列表,以便您可以选择要调试的进程。

对于最后两个配置,您可能会遇到权限问题(至少在 Linux 上,但也可能在 Mac 上)。您可以通过以不同用户身份运行可执行文件或提升 VS Code 的权限(例如通过使用 启动它sudo)来解决该问题。