• 搭建嵌入式GDB调试环境以及VSCode+gdbserver 图形化调试


    目录

    1 搭建嵌入式gdb调试环境

    1.1 交叉编译工具链自带的gdb和gdbserver

    1.2 使用gdb进行嵌入式程序调试

    1.2.1编写简单测试程序

    1.2.2 gdb调试程序

    1.3 源码编译gdb和gdbserver

    1.3.1 下载gdb和gdbserver源码

     1.3.2 编译gdb

    1.3.3 移植gdbserver

    2 VSCode+gdbserver 图形化调试


    1 搭建嵌入式gdb调试环境

    一般的桌面 Linux 系统,比如 ubuntu、centos 等,我们可以直接运行 gdb 来调试程序。但是嵌入式中芯片性能一般比较弱,所以直接在嵌入式系统中运行 gdb 不太现实(性能强大的嵌入式芯片可以这么做)。嵌入式系统中一般在 PC 端运行 gdb 工具,源码也是在 PC 端,源码对应的可执行文件放到开发板中运行。为此我们需要在开发板中运行 gdbserver,通过网络与 PC 端的 gdb 进行通信。因此要想在 PC 上通过 gdb 调试嵌入式程序,那么需要两个东西:gdb 和gdbserver,其中gdb 是运行在 PC 上的,gdbserver 需要我们移植到开发板上。

    1.1 交叉编译工具链自带的gdb和gdbserver

    一般来说交叉编译工具链都自带了gdb和gdbserver,因此可以不用移植,我去试了下,发现gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf里面自带的gdbserver不能用

     我把他放到开发板上执行,提示

    1. [root@imx6ull:/mnt]# ./gdbserver
    2. -bash: ./gdbserver: No such file or directory

    然后我又试了下gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf的gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/bin这个目录下的gdbserver放到开发板上是可以用的

    那么gdb我也用7.5.0里面的gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb。

    1.2 使用gdb进行嵌入式程序调试

    1.2.1编写简单测试程序

    首先编写一个简单的测试程序,用于调试

    1. #include
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. unsigned int times = 0;
    6. while(1)
    7. {
    8. printf("runing times:%d\r\n", times);
    9. times++;
    10. sleep(1);
    11. }
    12. }

    然后使用如下命令进行编译

    arm-linux-gnueabihf-gcc test.c -o test -g      //要使用-g选项才会有调试信息 

    然后我把他放到了开发板的/mnt目录下

    1.2.2 gdb调试程序

    首先在开发板上执行如下命令

    ./gdbserver :1234 test

    在开发版上执行命令的时候,是不需要指定IP地址的,只需要指定端口号即可,然后他就会一直监听

    然后在电脑上用下面的命令开始调试

    ./arm-linux-gnueabihf-gdb test  #我这里加了./是因为我没把这个交叉编译工具链加到环境变量中

    然后再用下面的命令去连接开发板

    target remote 172.31.8.195:1234

     这时候开发板上也会发现被连接了

    接下来就可以在电脑上调试程序了

    比如看一下源码

     比如我在main函数的第一行设置断点

    1.3 源码编译gdb和gdbserver

    如果交叉编译工具链里面没有gdb和gdbserver,那么就要自己源码编译gdb和gdbserver,方法如下。

    1.3.1 下载gdb和gdbserver源码

    去网址:  http://www.gnu.org/software/gdb/download/

    然后解压

    tar xzvf gdb-13.2.tar.gz

     1.3.2 编译gdb

    在交叉编译configure时,通常会需要设置--build、--host和--target选项。各个选项的含义如下:

    --build:编译所用的机器的平台。

    --host:编译出的代码运行的平台。

    --target:编译出来的工具链生成的代码的运行平台。这个选项不常用,一般只在编译gcc、ld等工具链的过程中用到,编译gdb,gdbserver时也会用到。

    在不涉及到交叉编译的时候,--build、--host、--target是一样的,不需要特别设置。交叉编译的时候,比如需要在x86平台编译arm程序,就需要设置--build和--host选项。

    通常,本机上的gcc、ld等工具是没有前缀的,这个时候--build可以设置为空。交叉编译工具链通常都有一个前缀,比如arm-unknown-linux-gnueabi-gcc,这个时候,--host就要设置为arm-unknown-linux-gnueabi。

    Example:

    ./configure --build='' --host=arm-unknown-gnueabi

    然后用如下命令开始编译、安装

    1. cd gdb-13.2/
    2. mkdir /data/chw/imx6ull_20230512/nfs_rootfs/gdb_install
    3. ./configure --target=arm-linux-gnueabihf --prefix=/data/chw/imx6ull_20230512/nfs_rootfs/gdb_install
    4. make -j8
    5. make install

    上面编译的过程中如果报错,

    1. gdb-13.2/missing: makeinfo: not found
    2. WARNING: 'makeinfo' is missing on your system.
    3. You should only need it if you modified a '.texi' file, or
    4. any other file indirectly affecting the aspect of the manual.
    5. You might want to install the Texinfo package:
    6. The spurious makeinfo call might also be the consequence of
    7. using a buggy 'make' (AIX, DU, IRIX), in which case you might
    8. want to install GNU make:
    9. Makefile:1794: recipe for target 'doc/bfd.info' failed

    那就

    sudo apt-get install texinfo

    如果报错

    1. configure: error: GMP is missing or unusable
    2. Makefile:11803: recipe for target 'configure-gdb' failed
    3. make[1]: *** [configure-gdb] Error 1
    4. make[1]: *** Waiting for unfinished jobs....

    那就用下面的方法

    apt-get install libgmp-dev

    编译安装完之后,得到

    这里就不修改PATH环境变量了,测试的时候直接加路径。

    1.3.3 移植gdbserver

    gdbserver是在开发板上运行的,需要交叉编译,gdbserver源码在./gdb-13.2/gdbserver里面

    1. cd ./gdb-13.2/gdbserver
    2. ./configure --target=arm-linux-gnueabihf --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar LD=arm-linux-gnueabihf-ld --prefix=/data/chw/imx6ull_20230512/nfs_rootfs/gdbserver_install //配置
    3. make

    报错

    1. ../gdbsupport/libgdbsupport.a: error adding symbols: File format not recognized
    2. collect2: error: ld returned 1 exit status
    3. Makefile:361: recipe for target 'gdbreplay' failed
    4. make: *** [gdbreplay] Error 1
    5. make: *** Waiting for unfinished jobs....

    这个问题我是这么想的,这个libgdnsupport.a库是在前面编译gdb的时候产生的,但由于gdb是在X86电脑上运行的,所以这个libgdnsupport.a库是x86格式的,

    所以我要在重新配置编译一遍gdb,只不过这次配置要用下面的命令,要编译交叉编译的版本,但是不要make install,要是install那么前面编译的X86版本的gdb就被覆盖了,这一次其实不是为了编译gdb,只是为了使用编译gdb过程中产生的库,

    1. tar xzvf gdb-13.2.tar.gz
    2. cd ./gdb-13.2
    3. ./configure --target=arm-linux-gnueabihf --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar LD=arm-linux-gnueabihf-ld
    4. make -j8 //这次gdb就不要安装了,要不然吧前面编译的覆盖了

    然后再编译gdbserver

    1. cd ./gdb-13.2/gdbserver
    2. ./configure --target=arm-linux-gnueabihf --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar LD=arm-linux-gnueabihf-ld --prefix=/data/chw/imx6ull_20230512/nfs_rootfs/gdbserver_install  //配置                    
    3. make -j8

    这时候就会在当前目录下生成一个gdbserver,把它拷贝到开发板上即可

    然后在开发板上执行

    然后电脑上执行

    这说明自己编译出来的gdb和gdbserver也是可以用的。

    2 VSCode+gdbserver 图形化调试

    前面介绍的是用gdb命令进行调试的,接下来看一下直接用vscode调试,这样要比gdb命令方便。

    先按照下面的博客安装必要的插件:ubuntu上安装vscode,并远程开发与远程调试服务器代码_vscode ssh远程连接ubuntu后,怎么打开项目代码-CSDN博客

    我现在调试是这样的,我的vscode是安装在windows上的,然后我要调试的代码test.c是在ubuntu服务器上的,所以我要先vscode远程打开ubuntu服务器上的test.c代码,就根据上面链接的博客操作就可以,然后run--Add Configuration---

    会生成一个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": "(gdb) Launch",

                "type": "cppdbg",

                "request": "launch",

                "program": "${workspaceFolder}/test",

                "args": [],

                "stopAtEntry": false,

                "cwd": "${fileDirname}",

                "environment": [],

                "externalConsole": false,

                "MIMode": "gdb",

                "miDebuggerPath": "/data/chw/imx6ull_20230512/nfs_rootfs/gdb_install/bin/arm-linux-gnueabihf-gdb",

                "miDebuggerServerAddress": "172.31.8.195:1234"

            }

        ]

    }

    其中

    • miDebuggerPath:此项需要手动添加,用于指定所使用的交叉编译器 gdb 路径。 
    • miDebuggerServerAddress:此项需要手动添加,远程 gdbserver 服务器地址。 

    然后开发板上还是老样子,运行gdbserver

    然后vscode上直接按F5,然后开发板上打印出了被连接的log。

    然后就可以在vscode上进行调试了。

  • 相关阅读:
    小程序源码:云之道知识付费独立线传版V2-2.4.9
    网站收录量与索引量有什么区别?
    vscode远程linux安装codelldb
    SpringBoot: Controller层的优雅实现
    双指针算法
    列表、元组和字典
    Swagger使用详解
    2022/09/06 day03:命令2
    lv7 嵌入式开发-网络编程开发 10 TCP协议是如何实现可靠传输的
    java面试技巧-职业规划有技巧
  • 原文地址:https://blog.csdn.net/u013171226/article/details/134249608