如何使用vscode连接到远程gdb



我想用vscode进行远程C/C++gdb调试。我在进行配置时使用"本机调试"扩展。这是我的启动.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

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

我在这里找到了这个问题的答案:是否可以使用vscode连接到远程gdb目标?

摘要:

首先,您需要安装VS代码的本机调试扩展。

然后编辑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代码并开始调试。请确保没有其他客户端连接到gdb服务器,否则会出现超时错误。

您必须在远程机器上安装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触发器或类似的东西来设置它。

这就是我的工作原理:

{
"version": "0.2.0",
"configurations": [{
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/vmlinux", // for loading symbols from running program
"cwd": "${workspaceFolder}",
// if you want to connect at entry point (requires remote program to start paused)
"stopAtEntry": true,
"stopAtConnect": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerServerAddress": "localhost:1234",
"setupCommands": [{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
}]
}]
}
安装了VS代码(本地(和gdb(远程(
  • 安装ssh远程插件
  • 使用远程ssh连接在Vs代码中打开工作区
  • 在vs_code debug选项卡中,单击设置图标,它应该会打开launch.json/create one(如果不存在(
  • 在launch.json中添加以下代码(编辑二进制、工作区根、process_id(
  • {
    
    "version": "0.2.0",
    "configurations": [      
    {
    "name": "(gdb) Attach",
    "type": "cppdbg",
    "request": "attach",
    "program": "${path to binary}",
    "processId": "${process id to attach to}",
    "MIMode": "gdb",
    "cwd":"${workspaceRoot}",
    "setupCommands": [
    {
    "description": "Enable pretty-printing for gdb",
    "text": "-enable-pretty-printing",
    "ignoreFailures": true
    }
    ]
    }
    ]
    }

    1. 保存更改,然后单击"运行和调试"选项卡上的"绿色启动"按钮

    类似的问题https://stackoverflow.com/a/72580854/19317199

    最新更新