温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中怎么使用gdb和gdbserver构建在线调试环境

发布时间:2021-08-05 17:56:30 来源:亿速云 阅读:255 作者:Leah 栏目:互联网科技

这篇文章给大家介绍Linux中怎么使用gdb和gdbserver构建在线调试环境,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1.引言

单片机一般使用Jlink通过SWD或者JTAG接口直接在IDE中在线调试,Linux应用程序通常是加printf输出log去调试,这种方式简单,但是有些隐藏的程序bug只通过加打印信息不那么容易定位,这时可以通过类似单片机调试的gdb调试来实现,本篇为大家介绍linux环境下在线调试环境的搭建,希望对大家有所帮助。

GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.                                              

2.环境介绍

2.1.硬件

1) 网上的一个第三方做的NUC972开发板:

Linux中怎么使用gdb和gdbserver构建在线调试环境

有兴趣购买的朋友,可以去他们的淘宝店购买:

https://s.click.taobao.com/X8mza8w

2.2.软件

1) Uboot继续使用之前文章用的,无须改动。

2) Kernel在上一篇基础上,无须改动。

3) Rootfs在上一篇用Buildroot生成的基础上,需要做一定的改动,用来生成gdbserver。

3.Buildroot配置

Buildroot里需要做一定的配置,用来生成gdb和gdbserver,步骤如下:

1) 确认Toolchain | Build cross gdb for the host 是否选中,这个默认是选中的。

Linux中怎么使用gdb和gdbserver构建在线调试环境

这个的作用是:Build a cross gdb that runs on the host machine and debugs programs running on the target. It requires 'gdbserver' installed on the target。

2) 选中Toolchain下的Thread library debugging,注意一定得先选中这个,不然第三步无法执行。

Linux中怎么使用gdb和gdbserver构建在线调试环境

3) 选中Target packages | Debugging, profiling and benchmark->gdb和gdbserver

Linux中怎么使用gdb和gdbserver构建在线调试环境

上面的作用是:

This option allows to build gdbserver and/or the gdb debugger for the target.For embedded development, the most common solution is to build only 'gdbserver' for the target, and use a cross-gdb on the host.

4) 保存,编译即可。

生成的gdb位于:/home/topsemic/nuc972/buildroot/NUC970_Buildroot/output/host/usr/bin

目录中

生成的gdbserver位于:

/home/topsemic/nuc972/buildroot/NUC970_Buildroot/output/target/usr/bin 目录中

5) 将上述gdbserver直接放到板子的/usr/bin目录里即可,然后登录板子输入gdbserver,可以看到如下信息,说明板子的gdbserver已经搭建好了。

Linux中怎么使用gdb和gdbserver构建在线调试环境

4.新建测试程序

1)新建一个测试程序gdbtest.c

#include <stdio.h>int main(){    char s[64] = "Welcome to www.topsemic.com";    int a = 1;    int c = a*2;    int *ptr = NULL;    printf("s is :%s ", s);    printf("c is : %d ", c);    *ptr = 20;    printf("%d ",*ptr);    return 0;}

2)交叉编译

topsemic@topsemic-virtual-machine:~/nuc972/examples/gdbserver$ arm-linux-gcc gdbtest.c -o gdbtest -g

注:arm-linux-gcc gdbtest.c -o gdbtest -g其中”-g”参数表示进行 GDB 编译。

这个程序放到板子里运行结果如下:

Linux中怎么使用gdb和gdbserver构建在线调试环境

我们用下面的在线调试方法去看看什么原因导致的Segmentation fault

5.在线调试

调试前,将板子和PC之间通过网线相连接,步骤如下:

1) 在开发板可执行程序所在的目录下,执行如下命令启动gdbserver:

Linux中怎么使用gdb和gdbserver构建在线调试环境

命令格式:gdbserver <Host_IP>:<Ports><Program><Arguments...>

192.168.0.80 为Ubuntu 的 IP 地址, 1234 为连接的端口号

注:需要先将虚拟机Ubuntu的IP配置为固定的192.168.0.80,这个设置方法在《Linux学习系列八:操作网口》中有介绍

Linux中怎么使用gdb和gdbserver构建在线调试环境

Linux中怎么使用gdb和gdbserver构建在线调试环境

2) 在Ubuntu下启动gdb调试,命令格式:

<GDB 可执行程序路径> <应用程序路径>

topsemic@topsemic-virtual-machine:~/nuc972/examples/gdbserver$ /home/topsemic/nuc972/buildroot/NUC970_Buildroot/output/host/usr/bin/arm-linux-gdb gdbtest

Linux中怎么使用gdb和gdbserver构建在线调试环境

3) 在弹出的上述对话框(gdb)后输入以下命令,连接开发板

(gdb)target remote 192.168.0.100:1234

Linux中怎么使用gdb和gdbserver构建在线调试环境其中192.168.0.100 是开发板的IP地址

4)之后就可输入如下 GDB 调试命令,其他调试命令的详细用法请输入”help 命令名称”查阅。

命令:l,参看代码。

命令:b main,在 main处设置断点。

命令:b 6,在第六行设置断点。

命令:c,继续执行。

命令:n,单步执行。

命令:q,退出gdb。

一直输入 c, 直到程序结束。

Linux中怎么使用gdb和gdbserver构建在线调试环境

Linux中怎么使用gdb和gdbserver构建在线调试环境

单步调试,同时查看板子上打印的信息

Linux中怎么使用gdb和gdbserver构建在线调试环境

可以看到板子程序执行的过程和Ubuntu上加的断点运行的进度一致,另外可以

发现是因为line 10 导致的Segmentation fault,这样就定位到了出问题的地方。

关于Linux中怎么使用gdb和gdbserver构建在线调试环境就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI