20150502 调试分析之 使用gdb远程调试ARM开发板
2015-05-02 Lover雪儿
今天我们要学习的是使用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 <http://gnu.org/licenses/gpl.html> 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: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. 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 <stdio.h> 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=%d\n",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=%d\n",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, 5 unsigned int fsr, unsigned int sig, int code, 6 struct pt_regs *regs) 7 { 8 struct siginfo si; 9 10 #ifdef CONFIG_DEBUG_USER //1.配置内核 11 if (user_debug & UDBG_SEGV) { 12 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n", 13 tsk->comm, sig, addr, fsr); 14 show_pte(tsk->mm, addr); 15 show_regs(regs); 16 } 17 #endif 18 ############################################################# 19 arch/alpha/kernel/traps.c 20 21 2.uboot: set bootargs console=ttySAC0 user_debug=0xff //全部输出debug信息
配置的具体步骤:
root@Lover雪:/home/study/nfs_home/system/linux-2.6.31# make menuconfig 输入/搜索 DEBUG_USER 结果: │ Symbol: DEBUG_USER [=n] │ │ Prompt: Verbose user fault messages │ │ Defined at arch/arm/Kconfig.debug:29 │ │ Location: │ │ -> Kernel hacking │
首先选中 [*] Kernel debugging [*] Verbose user fault messages //显示用户的错误信息 重新编译 root@Lover雪:/home/study/nfs_home/system/linux-2.6.31# make uImage 烧写内核:
烧写完毕后,重新启动 进入开发板的u-boot 原来的参数保留,增加一项 user_debug=0xff set bootargs user_debug=0xff bootm 80800000 启动内核
执行应用程序,则会打印全部的错误信息. |
当发生错误的时候,打印出栈信息
1 在arch/arm/mm/fault.c中增加 2 3 static void 4 __do_user_fault(struct task_struct *tsk, unsigned long addr, 5 unsigned int fsr, unsigned int sig, int code, 6 struct pt_regs *regs) 7 { 8 struct siginfo si; 9 10 #ifdef CONFIG_DEBUG_USER //1.配置内核 11 if (user_debug & UDBG_SEGV) { 12 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n", 13 tsk->comm, sig, addr, fsr); 14 show_pte(tsk->mm, addr); 15 show_regs(regs); 16 } 17 #endif 18 19 int i=0; 20 //////////////////////////////////////////////////////////// 21 while(i<1024) 22 { 23 if(copy_from_user(&val, (const void __user *)(regs->ARM_sp + i*4),4)) 24 break; 25 printk("%08x",val); 26 i++; 27 if(i % 8 == 0) 28 printk("\n"); 29 } 30 printk("\n End of Statck\n"); 31 /////////////////////////////////////////////////////////////
* 博客园: http://www.cnblogs.com/lihaiyan/
* 邮箱:1063385677@qq.com
* QQ: 1063385677
* Copyright ©2014 Lover雪儿
********************************************
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。