文章目录
前言
由于一直在使用VScode,而且因为Clion不是免费的,前段时间在想vscode可不可以也像Clion这种IDE一样debug板端应用程序,于是查了一些VScode的帮助文档,和一些网上的资料,基于目前自己的项目,自己搭建了一个可以使用gdb debug板端多应用的环境,记录一下。
一、环境
windows11(Vscode安装在Windows中)
vmware_workstation(内装Ubuntu18.04,应用在Ubuntu里面进行编译)
应用:8个待调试应用
嵌入式端:arm板,带网口,通过以太网和PC相连
二、步骤
1.VSCode 用SSH连接到虚拟机中的Ubuntu系统
打开VSCode,通过SSH连接到虚拟机Ubuntu中,并打开应用文件夹
2.编译应用
由于要使用gdb进行debug,那么编译应用的时候需要加上-g选项,修改CMakeList.txt
add_definitions ("-Wall -g")
编译应用
cmake ..
make
3.放gdbserver到板端
将编译出来的8个带调试信息的应用,放入板端
将gdbserver放入板端/data/目录下
这里gdbserver的路径在我的SDK工程里是这个路径,预先编译好的
./toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/bin/gdbserver
4.编辑launch.json
在VScode工程的根目录下添加并编辑launch.json,代码如下(示例):
/.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "bsp",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/bsp",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1001"
},
{
"name": "codec",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/codec",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1002"
},
{
"name": "iot",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/iot",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1003"
},
{
"name": "webs",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/webs",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1004"
},
{
"name": "rtsps",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/rtsps",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1005"
},
{
"name": "rtmps",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/rtmps",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1006"
},
{
"name": "onvif",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/onvif",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1007"
},
{
"name": "rec",
"type": "cppdbg",
"request": "launch",
"program": "/home/levi/work/gitee_project/fork/aipcam-20240521-1510/build/out/rec",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/home/levi/work/eeasytech/sv822_820_bova/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.1.123:1008"
}
]
}
program:是要调试的应用的名字(在Ubuntu中)
miDebuggerPath:为交叉工具链中的gdb工具
miDebuggerServerAddress:为板子ip和端口号,端口号要与板端输入的监听的端口号一致
5. 开始调试
开发板通过ssh连接到Windows10的终端,每一个应用使用一个SSH终端来进行调试,所以一共打开8个SSH终端,分别在每一个终端中输入如下命令开始监听gdb调试命令
命令格式为:
gdbserver $HostIP:$Port $execute_file <param1> ... <paramN>
第一个SSH终端输入:
/data/gdbserver 192.168.30.128:1001 /root/app/bin/bsp
第二个SSH终端输入:
/data/gdbserver 192.168.30.128:1002 /root/app/bin/codec
第三个SSH终端输入:
/data/gdbserver 192.168.30.128:1003 /root/app/bin/iot
第四个SSH终端输入:
/data/gdbserver 192.168.30.128:1004 /root/app/bin/webs
第五个SSH终端输入:
/data/gdbserver 192.168.30.128:1005 /root/app/bin/rtsps
第六个SSH终端输入:
/data/gdbserver 192.168.30.128:1006 /root/app/bin/rtmps
第七个SSH终端输入:
/data/gdbserver 192.168.30.128:1007 /root/app/bin/onvif
第八个SSH终端输入:
/data/gdbserver 192.168.30.128:1008 /root/app/bin/rec
命令执行成功大概是如下的log:
gdbserver 192.168.30.128:1002/root/app/bin/codec
Process /root/app/bin/codec created; pid = 1947
Listening on port 1002
由于我不需要同时启动所有应用,而是需要一个接一个应用顺序启动,所以这里不涉及launch.json中"compounds"属性的问题。
这里,我在Vscode中打开一个应用的源码,按F5开始debug一个应用,之后打开另一个应用的源码,再按F5开始debug另一个应用,依次debug8个应用,如下所示:
可以看到8个应用都被启动debug了,可以选择某一个应用执行debug命令,run,pause,step into,step over,step out,打断点等,查看变量的值等等。
总结
今天总结了如果使用Vscode debug 板端C应用的方法。
参考
在Visual Studio Code中调试
【笔记】基于VSCode-gdbserver的嵌入式Linux开发板调试环境搭建
解决了VScode 写C语言断点无效的问题
VScode 调试教程 tasks.json和launch.json的设置(超详细)
嵌入式VSCode+gdbserver图形化调试环境搭建与使用