windows下使用VSCode debug开发板多应用C程序


前言

由于一直在使用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图形化调试环境搭建与使用

好的,这里给出一个例子,介绍如何使用VS Code进行SSH连接并调试开发板应用。 假设我们要连接到一个名为"myboard"的开发板,并调试其上的C++应用程序。我们需要完成以下步骤: 1. 在开发板上安装OpenSSH服务,并配置允许远程SSH连接。 2. 在本地机器上安装VS Code,并安装Remote Development扩展。 3. 在VS Code中打开一个空文件夹,并按下F1键打开命令面板。输入“Remote-SSH: Connect to Host”,选择“Add New SSH Host...”并输入连接信息,如下所示: ``` Host myboard HostName 192.168.1.100 //开发板的IP地址 User pi //开发板的用户名 Port 22 //SSH端口号 ``` 4. 连接到开发板后,使用VS Code的远程文件浏览器功能打开开发板上的源代码文件夹。 5. 在VS Code中打开应用程序的源代码文件,并在需要调试的位置设置断点。 6. 按下F5键启动调试会话,并选择“C++ (GDB/LLDB)”作为调试环境。 7. 在弹出的调试配置文件中,设置“targetRemote”选项为开发板的IP地址和端口号,并设置“cwd”选项为应用程序所在的文件夹。 ``` { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/myapp", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}/build", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable gdb pretty-printing for gdb 7.2 and later", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "targetRemote": "192.168.1.100:22" } ] } ``` 8. 点击调试工具栏上的“启动调试”按钮,开始调试应用程序。 以上就是一个简单的使用VS Code进行SSH连接并调试开发板应用的例子。当然,具体的步骤可能因不同的开发板应用程序而异,需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值