ubuntu c c++ gdb make_嵌入式Linux系列第14篇:使用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.

它的工作原理是:在主机Ubuntu下运行gdb,在嵌入式板子上运行gdbserver,这样就可以在线调试了。

2.环境介绍

2.1.硬件

NUC972开发板

2.2.软件

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

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

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

3.Buildroot配置

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

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

018cffbdbc8c99ae03ccc101b73e5128.png

这个的作用是: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,注意一定得先选中这个,不然第三步无法执行。

a4fb7062b60e8c23f1d03df4fd1c6cc4.png

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

089a9c47e0c2eb3a4d469610fa4784e7.png

上面的作用是:

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已经搭建好了。

275e00daefbfdef09e45664b8c202bdb.png

4.新建测试程序

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

#include

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 编译。

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

f952c1fc755d25886028ee7035ee47a2.png

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

5.在线调试

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

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

4070c2cd677fc027a5ae30ce2af4bdcf.png

命令格式: gdbserver :

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

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

d922fbd44e5a1703707eb89bd10aeac4.png
dcdb19f1cf6c670849d206b2d9396c74.png

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

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

fa0ec76759c633aa5f6efca9614db720.png

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

(gdb)target remote 192.168.0.100:1234

226b133277ba19d9f83f9ef7e93dd905.png

其中192.168.0.100 是开发板的IP地址

4)之后就可输入如下 GDB 调试命令,其他调试命令的详细用法请输入"help 命令名称"查阅。命令: l,参看代码。命令: b main,在 main 处设置断点。命令: b 9,在第六行设置断点。命令: c,继续执行。

命令: n,单步执行。

命令: q,退出gdb。一直输入 c, 直到程序结束。

6ec99e1c77a2b4898d479f0bc7280312.png
d0266600161abe92ef3266a3a5d4b58b.png

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

3adf38dec0f245fe6cd87037146c1295.png

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

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

注:man.linuxde.net/gdb 可以看到详细的gdb命令用法。

6.结束语

本篇为大家介绍了Linux下使用gdb和gdbserver构建在线调试环境,欢迎大家多交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值