在VSCode上使用gdb,gdbServer远程调试树莓派程序

背景

前期使用阿里的LinkSDK开发一个程序,主要在树莓派上运行,主要实现一个网关的功能,让本地Zigbee传感器网络的数据的阿里的云平台互相通讯;在调试的过程中频繁的要在Windows上交叉编译后,然后在树莓派上去运行,虽然开发工作已经完成了,但是着实的不方便.

最近又想开发一个在树莓派上的应用,主要实现用天猫精灵对房间里的电脑进行开关机的功能;当时还是用阿里的LinkSDK;想着之前很不方便,因此决定把环境配好,一键编译调试.

开发环境

使用Windows的WSL功能,安装了Ubuntu.

安装完后,安装一些必要的工具

sudo apt install make gcc gdb sshpass

查看树莓派的gcc和gdb环境并安装gdbserver

树莓派中gdb,gcc,gdbserver直接用命令安装

sudo apt install gcc gdb gdbserver

在WSL中Ubuntu中安装arm gcc和arm gdb

在arm官网上下载已编译好的包:Downloads | GNU-A Downloads – Arm Developer

下载arm-linux-gnueabihf这个包

将这三个包解压到/usr/local/bin/gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf中:

sudo tar -xf gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf.tar.xz

安装一些依赖库(可以尝试运行一下,缺少哪些库,就安装)

sudo apt install libtinfo5 libncursesw5 libpython2.7

添加环境变量(添加到/etc/profile的尾部)

export PATH="$PATH:/usr/local/bin/gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/bin"

让环境变量立即生效

source /etc/profile

查看arm gdb是否已经ok了

VSCode配置

vscode中需要安装remote wsl插件

在vscode中连上wsl,然后打开本地工程,然后安装wsl-Ubuntu中的一些vscode插件

在vscode根目录配置好配置文件

c_cpp_properties.json

{    "configurations": [        {            "name": "Linux",            "includePath": [                "${workspaceFolder}/**"            ],            "defines": [],            "compilerPath": "/usr/bin/gcc",            "cStandard": "gnu17",            "cppStandard": "c++17",            "intelliSenseMode": "linux-gcc-x64"        },        {            "name": "Linux_gnueabihf",            "includePath": [                "${workspaceFolder}/**"            ],            "defines": [],            "compilerPath": "/usr/local/bin/gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/binarm-linux-gnueabihf-gcc",            "cStandard": "gnu17",            "cppStandard": "c++17",            "intelliSenseMode": "linux-gcc-x64"        }    ],    "version": 4}

launch.json(配置debug)

{  // 使用 IntelliSense 了解相关属性。  // 悬停以查看现有属性的描述。  // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387  "version": "0.2.0",  "configurations": [    {      "name": "Debug_gcc",      "request": "launch",      "cwd": "${workspaceFolder}",      "program": "${workspaceFolder}/output/main",      "type": "cppdbg",      "launchCompleteCommand": "exec-run",      "miDebuggerPath": "/usr/bin/gdb",      "preLaunchTask": "make-gcc-debug"    },    {      "name": "Debug_arm_gcc",      "request": "launch",      "cwd": "${workspaceFolder}",      "program": "${workspaceFolder}/output/main",      "type": "cppdbg",      "launchCompleteCommand": "exec-run",      "preLaunchTask": "make-arm-gcc-debug",           "miDebuggerPath": "/usr/local/bin/gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",      "miDebuggerServerAddress": "192.168.12.116:1234"    }  ] }

tasks.json(配置build)

{  "tasks": [    {      "label": "make-gcc-debug",      "type": "shell",      "command": "make -j16 CC=gcc CFLAGS='-g -Og'",      "options": {        "cwd": "${workspaceFolder}"      },      "problemMatcher": [        "$msCompile"      ]    },    {      "label": "make-gcc-release",      "type": "shell",      "command": "make -j16 CC=gcc CFLAGS='-Os'",      "options": {        "cwd": "${workspaceFolder}"      },      "problemMatcher": [        "$msCompile"      ]    },    {      "label": "make-clean",      "type": "shell",      "command": "make clean",      "options": {        "cwd": "${workspaceFolder}"      },      "problemMatcher": [        "$msCompile"      ]    },    {      "label": "make-arm-gcc-debug",      "type": "shell",      "command": "make -j16 CC=arm-linux-gnueabihf-gcc CFLAGS='-g -Og' && sshpass -p '树莓派密码' rsync -a output/main pi@192.168.12.116:/home/pi/main && sshpass -p '树莓派密码' ssh pi@192.168.12.116 'pkill -9 gdbserver ; nohup gdbserver 192.168.12.116:1234 /home/pi/main > debug.log 2>&1  &'",      "options": {        "cwd": "${workspaceFolder}"      },      "problemMatcher": [        "$msCompile"      ]    },    {      "label": "make-arm-gcc-release",      "type": "shell",      "command": "make -j16 CC=arm-linux-gnueabihf-gcc CFLAGS='-Os'",      "options": {        "cwd": "${workspaceFolder}"      },      "problemMatcher": [        "$msCompile"      ]    }  ],  "version": "2.0.0"}

启动调试

启动调试前可以先用ssh连一下树莓派,yes一下.

点击运行,即可进入调试了

效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值