使用 vscode 远程调试目标板的 c 语言代码
- 本文实现了使用 vscode 作为前端,使用网络连接目标板进行 c 语言调试。局限是需要目标板有网络接口,有gdbserver 程序,有 -g 编译参数生成的 a.out 的可执行文件。
1. vscode 添加 launch.json
- 添加 launch.json,该配置添加完成之后,即可在 vscode 中选择该 name 进行运行调试。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 附加",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb-multiarch",
"miDebuggerServerAddress": "192.168.16.101:1234",
"cwd":"${workspaceFolder}",
// if you want to connect at entry point (requires remote program to start paused)
"stopAtEntry": true,
"stopAtConnect": true,
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
重点:
"program": "${workspaceFolder}/a.out",
指定的调试的可执行文件"miDebuggerPath": "/usr/bin/gdb-multiarch",
必须指定用 gdb-multiarch 进行调试"miDebuggerServerAddress": "192.168.16.101:1234",
指定目标板的 ip 与端口
2. 目标板上运行
在目标板上输入命令 gdbserver localhost:1234 ./a.out
, 指示使用 gdbserver 调试 a.out,并监听 1234 端口的远程调试器。
3. 在 vscode 进行调试
- 点击 vscode 的侧边栏
调试与运行
,点击到运行(gdb) 附加
。若网络连接成功,则可以正常进行断点调试,并可以显示汇编代码和cpu寄存器进行调试。