【问题标题】:How to attach to remote gdb with vscode?如何使用 vscode 附加到远程 gdb?
【发布时间】:2019-04-30 08:58:35
【问题描述】:

我想用 vscode 进行远程 C/C++ gdb 调试。我在进行配置时使用“本机调试”扩展。这是我的 launch.json 配置

{
        "type": "gdb",
        "request": "launch",
        "name": "Launch Program (SSH)",
        "target": "./hello",
        "cwd": "/home/root/test1/",
        "ssh": {
            "host": "192.168.15.130",
            "cwd": "/home/root/test1/",
            "password": "",
            "user": "root"
} 

我在目标上运行

gdbserver localhost:2000 ./hello

不幸的是,我无法连接远程设备进行调试。有没有对此有配置经验的人?

【问题讨论】:

    标签: debugging visual-studio-code


    【解决方案1】:

    我在这里找到了这个问题的答案: Is it possible to attach to a remote gdb target with vscode?

    总结:

    首先你需要为 VS Code 安装 Native Debug 扩展。

    然后编辑launch.json文件并添加:

    {
        "type": "gdb",
        "request": "attach",
        "name": "Attach to gdbserver",
        "executable": "<path to the elf/exe file relative to workspace root in order to load the symbols>",
        "target": "X.X.X.X:9999",
        "remote": true,
        "cwd": "${workspaceRoot}", 
        "gdbpath": "path/to/your/gdb",
        "autorun": [
                "any gdb commands to initiate your environment, if it is needed"
            ]
    }
    

    然后你可以重新启动 VS Code 并开始调试。 确保没有其他客户端连接到 gdb-server,否则会出现超时错误。

    【讨论】:

    • 我有 tqo 后续问题。我们需要在远程机器还是本地机器上安装扩展?源将驻留在本地或远程计算机上的什么位置?我想源和构建都将在远程机器上,对吧?
    • @irsis 可以,但通常情况相反,因为远程机器被视为生产(或接近生产)sourceware.org/gdb/current/onlinedocs/gdb/…
    【解决方案2】:

    你必须在远程机器上安装 gdbserver。例如

    apt-get install gdbserver
    

    在远程机器上启动 gdbserver

    gdbserver :1234 ./mybinary
    

    选择你喜欢的任何端口 - 这里是1234

    通过键入从本地计算机测试与 gdbserver 的连接

    gdb
    (gdb) target remote myremotehostname:1234
    

    触发任何 gdb 命令以检查它是否有效 - 例如c(或continue)继续运行mybinary。

    将以下内容放入您的.vscode/launch.json

        {
          "name": "C++ Launch",
          "type": "cppdbg",
          "request": "launch",
          "program": "${workspaceRoot}/mybinary",
          "miDebuggerServerAddress": "myremotehostname:1234",
          "cwd": "${workspaceRoot}",
          "externalConsole": true,
          "linux": {
            "MIMode": "gdb"
          }
        }
    

    或者使用其他答案中给出的"type": "gdb" 启动配置。

    为了让代码浏览和工作正常,使源目录在本地和远程端同步很重要。使用网络文件系统、手动复制、git 触发器或类似的东西来设置它。

    【讨论】:

    • 使用 cppdbg 与“本机调试扩展”相比有优势吗?我不清楚为什么有两个扩展在做同样的事情。
    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2015-10-22
    • 2017-12-23
    • 2012-12-31
    • 2014-07-06
    • 2016-10-30
    相关资源
    最近更新 更多