【问题标题】:Using vscode debug console with rust使用带有 rust 的 vscode 调试控制台
【发布时间】:2021-02-17 08:01:51
【问题描述】:

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

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

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

vscode 的调试器为我提供了 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

What I found in the 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")?

最后是它从错误中得到的建议:

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

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

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

我的语法是什么?

更新

@ForceBru 感谢有关 python 的提示。

我正在与之交互的是vscode-lldb debugger api

似乎在将"sourceLanguages": ["rust"]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([])

【问题讨论】:

  • 看起来 VSCode 正在尝试使用 Python 解释器运行 Rust 代码
  • script 是运行内置 python 解释器的 lldb 命令:因此脚本无法运行 rust 代码也就不足为奇了。不幸的是,我无法解决“如何调试 rust”问题

标签: debugging visual-studio-code rust vscode-extensions lldb


【解决方案1】:

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

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

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

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

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

如果您的 crate 是二进制文件,您需要将 --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: Attach by Name”,它会生成:

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

或“LLDB:按 PID 附加”:

{
    "type": "lldb",
    "request": "attach",
    "name": "Attach",
    "pid": "${command:pickMyProcess}" // use ${command:pickProcess} to pick other users' processes
},

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

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

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多