调试分析之 使用gdb远程调试ARM开发板

2024-08-13  

今天我们要学习的是使用gdb和gdbserver来远程调试开发板程序.


下面是本人的一些具体步骤:


下载gdb-7.9.tar.gz地址:


http://ftp.gnu.org/gnu/gdb/gdb-7.9.tar.gz


安装gdb


tar -jxvf gdb-7.9.tar.bz2


./configure -target=arm-none-linux-gnueabi --prefix=/home/study/nfs_home/rootfs_imx257/arm-linux-gdb –program-prefix=arm-none-linux-gnueabi-


make


make install


安装gdbserver


./configure -target=arm-none-linux-gnueabi --prefix=/home/study/nfs_home/rootfs_imx257/arm-linux-gdbserver --host=arm-none-linux-gnueabi


make


make install


具体安装步骤如下:


编译安装gdb


root@Lover雪:/home/study/nfs_home/system# tar -jxvf gdb-7.9.tar.bz2


root@Lover雪:/home/study/nfs_home/system# cd gdb-7.9


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# mkdir ../../rootfs_imx257/arm-linux-gdb


root@Lover雪:/home/study/nfs_home/system/gdb-7.9#


./configure -target=arm-none-linux-gnueabi --prefix=/home/study/nfs_home/rootfs_imx257/arm-linux-gdb --program-prefix=arm-none-linux-gnueabi-


checking build system type... x86_64-unknown-linux-gnu


checking host system type... x86_64-unknown-linux-gnu


checking target system type... arm-none-linux-gnueabi


###########################


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# make


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# make install


安装完毕发现多了一些文件夹


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# ll ../../rootfs_imx257/arm-linux-gdb/


总用量 24


drwxr-xr-x 6 root root 4096 5月 2 06:34 ./


drwxrwxrwx 14 root root 4096 5月 2 06:11 ../


drwxr-xr-x 2 root root 4096 5月 2 06:34 bin/


drwxr-xr-x 3 root root 4096 5月 2 06:34 include/


drwxr-xr-x 2 root root 4096 5月 2 06:34 lib/


drwxr-xr-x 5 root root 4096 5月 2 06:34 share/


root@Lover雪:/home/study/nfs_home/system/gdb-7.9#


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# cd gdb


###########################


编译安装gdbserver


root@Lover雪:/home/study/nfs_home/system/gdb-7.9/gdb# cd gdbserver/


root@Lover雪:/home/study/nfs_home/system/gdb-7.9/gdb/gdbserver#


mkdir /home/study/nfs_home/rootfs_imx257/arm-linux-gdbserver


root@Lover雪:/home/study/nfs_home/system/gdb-7.9/gdb/gdbserver#


./configure -target=arm-none-linux-gnueabi --prefix=/home/study/nfs_home/rootfs_imx257/arm-linux-gdbserver --host=arm-none-linux-gnueabi


configure: WARNING: If you wanted to set the --build type, don't use --host.


If a cross compiler is detected then cross compile mode will be used.


checking whether to enable maintainer-specific portions of Makefiles... no


checking for arm-none-linux-gnueabi-gcc... arm-none-linux-gnueabi-gcc


checking for C compiler default output file name... a.out


###########################


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# make


root@Lover雪:/home/study/nfs_home/system/gdb-7.9# make install



进入安装目录运行gdb查看版本


loverxueer@Lover雪:/home/study/nfs_home/rootfs_imx257$ ./arm-linux-gdb/bin/arm-none-linux-gnueabi-gdb


GNU gdb (GDB) 7.9


Copyright (C) 2015 Free Software Foundation, Inc.


License GPLv3+: GNU GPL version 3 or later


This is free software: you are free to change and redistribute it.


There is NO WARRANTY, to the extent permitted by law. Type 'show copying'


and 'show warranty' for details.


This GDB was configured as '--host=x86_64-unknown-linux-gnu --target=arm-none-linux-gnueabi'.


Type 'show configuration' for configuration details.


For bug reporting instructions, please see:


.


Find the GDB manual and other documentation resources online at:


.


For help, type 'help'.


Type 'apropos word' to search for commands related to 'word'.


(gdb) q


进入开发板目录,再开发板上运行gdbserver查看版本


root@EasyARM-iMX257 /mnt/nfs/rootfs_imx257# ./arm-linux-gdbserver/bin/arm-none-l


inux-gnueabi-gdbserver --version


GNU gdbserver (GDB) 7.9


Copyright (C) 2015 Free Software Foundation, Inc.


gdbserver is free software, covered by the GNU General Public License.


This gdbserver was configured as 'arm-none-linux-gnueabi'


root@EasyARM-iMX257 /mnt/nfs/rootfs_imx257#


使用gdb和gdbserver远程调试程序


附上应用程序gdb_test.c



 1 #include

 2 

 3 char *p = 'hello,this is test gdb & gdbserver ';

 4 

 5 void delay(int m)

 6 {

 7     int n;

 8     while(m--)

 9         for(n =0; n<100; n++);            

10 }

11 

12 void print(char *p, int cnt)

13 {

14     printf('%s cnt=%dn',p,cnt);

15 }

16 

17 int  main(void)

18 {

19     int cnt = 0;

20     while(1)

21     {

22         cnt++;

23         print(p,cnt);

24         delay(100000);

25     }

26     return 0;

27 }


首先再开发板写好写程序.编译


root@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test# vi gdb_test.c


root@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test#


arm-none-linux-gnueabi-gcc -g gdb_test.c -o gdb_test


这里注意一定要加 -g 选项


进入我们安装的gdb的目录


root@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test#


root@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test#


cd /home/study/nfs_home/rootfs_imx257/arm-linux-gdb/bin/


root@Lover雪:/home/study/nfs_home/rootfs_imx257/arm-linux-gdb/bin# ./arm-none-linux-gnueabi-gdb


 


连接开发板,在开发板操作步骤如下:


 


root@EasyARM-iMX257 /mnt/nfs/rootfs_imx257#


cp arm-linux-gdbserver/bin/arm-none-linux-gnueabi-gdbserver /bin/


进入要运行的程序的目录


root@EasyARM-iMX257 /mnt/nfs/rootfs_imx257# cd ../module/40_gdb_gdbserver_test/


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# ls


gdb_test gdb_test.c make.sh


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test#


获取开发板当前的ip


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# ifconfig


eth0 Link encap:Ethernet H


inet addr:192.168.31.181


Bcast:192.168.31.255 Mask:255.255.255.0


UP BROADCAST ric:1


RX packets:720 errors:0 dropped:0 overruns:0 frame:0


TX packets:105 errors:0 dropped:0 overruns:0 carrier:0


collisions:0 txqueuelen:1000


RX bytes:950466 (928.1 KiB) TX bytes:18426 (17.9 KiB)


Base address:0x2000


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test#


开始运行gdbserver监控,监控端口随便写,此处为1234


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# arm-none-linux-gnueab


i-gdbserver 192.168.31.181:1234 ./gdb_test


Process ./gdb_test created; pid = 1816


Listening on port 1234


######################################################################################


当宿主机连接成功后


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# arm-none-linux-gnueab


i-gdbserver 192.168.31.181:1234 ./gdb_test


Process ./gdb_test created; pid = 1816


Listening on port 1234


Remote debugging from host 192.168.31.179 远程连接来自 192.168.31.179


######################################################################################


当宿主机敲完命令后


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# arm-none-linux-gnueab


i-gdbserver 192.168.31.181:1234 ./gdb_test


Process ./gdb_test created; pid = 1816


Listening on port 1234


Remote debugging from host 192.168.31.179


hello,this is test gdb & gdbserver cnt=1


hello,this is test gdb & gdbserver cnt=2


hello,this is test gdb & gdbserver cnt=3


 


接着在 宿主机上运行gdb来连接gdbserver,进行调试.


 


步骤如下:


 


注意gdb的目录


运行gdb


loverxueer@Lover雪:/home/study/nfs_home/rootfs_imx257$ ./arm-linux-gdb/bin/arm-none-linux-gnueabi-gdb


GNU gdb (GDB) 7.9


Copyright (C) 2015 Free Software Foundation, Inc.


Type 'apropos word' to search for commands related to 'word'.


(gdb)


开始连接gdbserver,IP:端口号


(gdb) target remote 192.168.31.181:1234


Remote debugging using 192.168.31.181:1234


0x400007e0 in ?? ()


(gdb) l


没有符号表被读取。请使用 'file' 命令。


(gdb) c


######################################################################################


大家有没有发现,这样是连通了,也可以使用C命令运行,但是却无法使用list和break命令


答案:


因为调试的信息是从本地的文件中读取的,所以,宿主目录和开发板要进入同一个程序目录下,进行调试


所以再开发部上进入我的程序目录


root@Lover雪:/home/study/nfs_home/rootfs_imx257/arm-linux-gdb/bin#


cd /home/study/nfs_home/module/40_gdb_gdbserver_test/


注意gdb的目录


loverxueer@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test$


/home/study/nfs_home/rootfs_imx257/arm-linux-gdb/bin/arm-none-linux-gnueabi-gdb


GNU gdb (GDB) 7.9


Copyright (C) 2015 Free Software Foundation, Inc.


(gdb) target remote 192.168.31.181:1234


Remote debugging using 192.168.31.181:1234


0x400007e0 in ?? ()


(gdb) l


没有符号表被读取。请使用 'file' 命令。


######################################################################################


(gdb) symbol-file ./gdb_test


加载本地目录下的文件的信号表


Reading symbols from ./gdb_test...done.


(gdb) l


5 void delay(int m)


6 {


7 int n;


8 while(m--)


9 for(n =0; n<100; n++);


10 }


11


12 void print(char *p, int cnt)


13 {


14 printf('%s cnt=%dn',p,cnt);


(gdb) b 22


Breakpoint 1 at 0x83fc: file gdb_test.c, line 22.


(gdb)


(gdb) c


Continuing.



Breakpoint 1, main () at gdb_test.c:22


22 cnt++;


(gdb) c


Continuing.



Breakpoint 1, main () at gdb_test.c:22


22 cnt++;


(gdb) c


Continuing.



Breakpoint 1, main () at gdb_test.c:22


22 cnt++;


(gdb) c


Continuing.



Breakpoint 1, main () at gdb_test.c:22


22 cnt++;


(gdb) print cnt


$1 = 3


(gdb) target exec


A program is being debugged already. Kill it? (y or n) y


No executable file now.


(gdb)


此时再看我们的开发板的串口窗口


######################################################################################


######################################################################################



如下所示:


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test# arm-none-linux-gnueab


i-gdbserver 192.168.31.181:1234 ./gdb_test


Process ./gdb_test created; pid = 1816


Listening on port 1234


Remote debugging from host 192.168.31.179


hello,this is test gdb & gdbserver cnt=1


hello,this is test gdb & gdbserver cnt=2


hello,this is test gdb & gdbserver cnt=3


Killing all inferiors


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test#


 


总结命令:


 


开发板的命令:


root@EasyARM-iMX257 /mnt/nfs/module/40_gdb_gdbserver_test#


arm-none-linux-gnueabi-gdbserver 192.168.31.181:1234 ./gdb_test



宿主机的命令:


loverxueer@Lover雪:/home/study/nfs_home/module/40_gdb_gdbserver_test$


/home/study/nfs_home/rootfs_imx257/arm-linux-gdb/bin/arm-none-linux-gnueabi-gdb



(gdb) target remote 192.168.31.181:1234



(gdb) symbol-file ./gdb_test 注意这个是调试程序的文件所在


(gdb) l


(gdb) b 20


(gdb) c


(gdb) target exec


 


配置内核输出应用程序的段错误信息


 1 arch/arm/mm/fault.c

 2 

 3 static void

 4 __do_user_fault(struct task_struct *tsk, unsigned long addr,

文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。