使用VSCode调试控制台与Rust

3

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

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

我的程序在macOS终端上使用cargo run -p ...命令运行。

VS Code的调试器为我提供了与lldb交互的调试控制台。

比如,我在一个断点停留,想尝试一些东西...

到目前为止,我已经尝试过Rust。

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

我在lldb文档中找到的内容

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")?

最后,这个错误提示想要表达什么:

script print ("Here is some text")
Here is some text

Hello world虽然很好,但我怎么进入我的实际范围?

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

那里的语法是什么?

更新

@ForceBru感谢您提供有关python的提示。

我正在与vscode-lldb调试器api进行交互。

看起来我应该能够在添加了https://github.com/vadimcn/vscode-lldb/blob/v1.6.0/MANUAL.md#rust-language-support到我的配置后,执行像script debugger.evaluate(“/ se t [0]”)这样的操作,但运气不佳。

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

我仍在使用Python

script debugger.evaluate("locals().keys()")
dict_keys([])

1
看起来 VSCode 正试图使用 Python 解释器运行 Rust 代码。 - ForceBru
“script”是lldb命令,用于运行内置的Python解释器:因此,脚本无法运行Rust代码并不奇怪。不幸的是,我无法帮助解决“如何调试Rust”的问题。 - Jussi Kukkonen
1个回答

8

正如某人所评论的那样,看起来您的配置不正确,它正在尝试将您的源代码调试为Python。获取正确的配置最简单的方法是使用提供的模板。在您的情况下,您可能可以跳到本答案的末尾并使用其中一个“附加”配置文件,但其余部分可能对其他人有用。

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

VS Code LLDB: add configuration

如果您选择“添加配置...”,您将获得要添加的模板选项:

configuration templates

选择“LLDB:Debug Cargo Output”将把以下内容添加到您的launch.json文件中:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo launch",
    "cargo": {
        "args": [
            "build",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},

如果您的板条箱是二进制文件,您需要将 --lib 改为 --bin 并指定要运行的二进制文件:
{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo launch",
    "cargo": {
        "args": [
            "build",
            "--bin=foo"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},

对于测试,您可以选择 "LLDB:Debug Cargo Tests"。它将生成类似于以下内容:

{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo test",
    "cargo": {
        "args": [
            "test",
            "--no-run",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": []
},

您可能希望删除--lib参数,以便运行您项目中的所有测试。您可以将其过滤为仅调试您感兴趣的测试,方法是将其作为尾随参数添加。例如,仅运行名称包含“foo”的测试:
{
    "type": "lldb",
    "request": "launch",
    "name": "Cargo test",
    "cargo": {
        "args": [
            "test",
            "--no-run",
            "--lib"
        ]
    },
    "program": "${cargo:program}",
    "args": ["foo"]
},

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

{
    "type": "lldb",
    "request": "attach",
    "name": "Attach",
    "program": "${workspaceFolder}/foo"
},

或者“LLDB:按进程标识符附加”:
{
    "type": "lldb",
    "request": "attach",
    "name": "Attach",
    "pid": "${command:pickMyProcess}" // use ${command:pickProcess} to pick other users' processes
},

这会给你一个可筛选的正在运行的进程列表,以便你选择要调试的进程。
对于这最后两个配置,你可能会遇到权限问题(至少在Linux上,但很可能在Mac上也是如此)。你可以通过以不同的用户身份运行可执行文件或提升VS Code的权限(例如通过使用sudo启动)来解决该问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接