Vscode远程调试

 

使用 vscode 的图形界面调试跨架构的远程 cpp 程序

背景

  1. gdb 自带 gdbserver,官方方案,支持通过网络,串口,调试远程设备
  2. gdb-multiarch 支持跨架构调试
  3. vscode 的 c++插件支持图形化调用以上工具

配置

远程机器

在远程机器需要启动 gdbserver,支持传入参数,支持设置远程 ip

gdbserver :1234 build/test/test --gtest_filter=MyTest.removeNthFromEnd

本地机器

手动调试

在本地启动 gdb,target 设置为远程端口

gdb-multiarch --silent
target remote 127.0.0.1:1234

注意此时不能使用run,直接continue即可

vscode 调试

正常设置 c++调试项目

特殊处理miDebuggerServerAddresssourceFileMap即可

{
    "name": "leetcode remote",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/build/test/test",
    "args": [],
    "stopAtEntry": true,
    "cwd": "${workspaceFolder}/build",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerServerAddress": "10.221.117.228:1234",
    "sourceFileMap": {
        "/home/ycc/code/samples/leetcode": "/home/ycc/code/samples/leetcode_new"
    },
    "setupCommands": [
        {
            "description": "为 gdb 启用整齐打印",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },
        {
            "description": "将反汇编风格设置为 Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true
        },
        {
            "description": "防止gdb打开标准库函数",
            "text": "-interpreter-exec console \"skip -rfu std::.*\"",
            "ignoreFailures": false
        }
    ]
}

小技巧

假设你的目标调试机器是 C,需要通过 B 中转

可以在 B 设置通过 localforward,转发自己的端口,记得打开 GatewayPorts

StrictHostKeyChecking no
NoHostAuthenticationForLocalhost yes
GatewayPorts yes

Host self
    Hostname 127.0.0.1
    User self
    LocalForward 1234 ip_C:1234