标题: GDB在ARM上的调试实战 [打印本页] 作者: piaoyizu 时间: 2009-08-04 17:21 标题: GDB在ARM上的调试实战 gdb的简单使用和gdb+gdbserver方式进行ARM程序
gdb的简单使用
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。一般来说,GDB主要帮忙你完成下面四个方面的功能: 1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、动态的改变你程序的执行环境。从上面看来,GDB和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。
一个调试示例
—————— 源程序:tst.c 1 #include
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i cc -g tst.c -o tst 使用GDB调试: hchen/test> gdb tst GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) l
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; igdb) gdb) break 16 gdb) break func gdb) info break gdb) r gdb) n gdb) n
20 result += i;
(gdb) n
18 for(i=1; igdb) n
20 result += i;
(gdb) c gdb) n
6 for(i=1; igdb) p i gdb) n
8 sum+=i;
(gdb) n
6 for(i=1; igdb) p sum
$2 = 1
(gdb) n
8 sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6 for(i=1; igdb) p sum
$4 = 3
(gdb) bt gdb) finish gdb) c gdb) q gdb。
hchen/test>
进行调试
(gdb)list or l
(gdb)break func
(gdb)break 22
(gdb)info br
(gdb)continue or c // 这里不能用 run
(gdb)next or n
(gdb)print or p result
(gdb) finish // 跳出func函数
(gdb) next
(gdb) quit
建立连接后进行gdb远程调试和gdb本地调试方法相同
然后把/usr/include/sys/reg.h copy到/usr/local/armv5l-2.6.x/3.3.2/armv5l-linux/include/sys/reg.h,即将该文件拷贝到交叉编译器的include目录下,再make,显示错误:
/usr/local/armv5l/3.3.2/bin/armv5l-linux-gcc -c -Wall -g -O2 -I. -I. -I./../regformats -I./../../include -I../../bfd -I./../../bfd thread-db.c
thread-db.c: In function `thread_db_err_str':
thread-db.c:95: error: `TD_VERSION' undeclared (first use in this function)
thread-db.c:95: error: (Each undeclared identifier is reported only once
thread-db.c:95: error: for each function it appears in.)
thread-db.c: In function `thread_db_get_tls_address':
thread-db.c:336: warning: implicit declaration of function `td_thr_tls_get_addr'
thread-db.c:336: warning: cast to pointer from integer of different size
thread-db.c:340: warning: cast from pointer to integer of different size
make: *** [thread-db.o] 错误 1
本想继续fix error,但是感觉不太对,请问各位,是什么原因呢?
是不是CC的target写错了?应该是arm-linux还是armv5l-linux?
1.
make: *** [linux-arm-low.o] Error 1
[root@dding gdbserver]#
[root@dding gdbserver]# gedit config.h
/* Define to 1 if you have the header file. */
/*define HAVE_SYS_REG_H 1 */
/*have no header file. so undefine 20070402 dding */
2.
thread-db.c: In function `thread_db_err_str': gdb6.5
thread-db.c:95: `TD_VERSION' undeclared (first use in this function)
[root@dding gdbserver]# gedit config.h
94 #ifdef HAVE_TD_VERSION
95 case TD_VERSION:
96 return "version mismatch between libthread_db and libpthread";
97 #endif
/* Define if TD_VERSION is available. */
/*#define HAVE_TD_VERSION 1 */
/*have no TD_VERSION. so undefine 20070402 dding */
gdb6.1 没有此问题
3.
[root@AT91RM9200DK arm]$./gdbserver 192.168.0.12:2345 mainparacarm gdb6.5
./gdbserver: error in loading shared libraries: libthread_db.so.1: cannot open
[root@AT91RM9200DK arm]$./gdbserver 192.168.0.14:2345 mainparacarm gdb6.1
./gdbserver: error in loading shared libraries: libthread_db.so.1: cannot open shared object file: No such file or directory
我已经加了libthread_db.so.1共享库为什么还打不开呢????共享库和cpu类型有关吗?
gdbserver: error while loading shared libraries: libthread_db.so.1: cannot open
shared object file: No such file or director
***/lib there is no libthread_db.so.1 Can i use nfs to copy libthread_db.so.1 to /lib? But now i cannot find this file, and is there any for cross 3.3.2?
libpthread-0.8.so
libpthread.so libpthread.so.0 libresolv-2.1.3.so
libresolv.so.2 libstdc++.a.2.10.0 libtermcap.so.2
[root@AT91RM9200DK arm]$cp libthread_db-1.0.so libthread_db.so.1
[root@AT91RM9200DK arm]$cp libthread_db.so.1 /lib/
[root@AT91RM9200DK arm]$./gdbserver 192.168.0.12:2345 mainparacarm
./gdbserver: /lib/libc.so.6: version `GLIBC_2.2' not found (required by /lib/li)