本文以PC机为例演了整个过程,在ARM板上的调试方法类似:
在服务器端(或者目标板)上:运行gdbserver,并记住打印出来的PID,后面用它来获得进程的maps文件。
gdbserver localhost:2222 networkmanager &Process networkmanager created; pid = 24753
Listening on port 2222
在客户端(或者主机端)上:运行gdb,加载可执行文件的符号文件,在主函数处设置断点,并执行到这里。gdbserver刚运行起来时,共享库还没有加载,所以要执行到main函数才能进行后面的操作。
lixianjing@localhost server>gdbGNU gdb Red Hat Linux (6.3.0.0-1.122rh)
Copyright 2004 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-redhat-linux-gnu".
(gdb) target remote localhost:2222Remote debugging using localhost:2222
0x00293840 in ?? ()
(gdb) symbol-file networkmanagerReading symbols from /home/lixianjing/work/groupware/network-
anager/server/networkmanager...done.
(gdb) b mainBreakpoint 1 at 0x804a565: file main.c, line 16.
(gdb) cContinuing.
Breakpoint 1, main (argc=Cannot access memory at address 0xa396940a
) at main.c:16
16 {
在客户端(或者主机端)上:用脚本计算共享库的偏移量,这里由于gdbserver和gdb在同一台机器上,所以直接取了maps文件的内容,如果是调试ARM版,要把maps文件从ARM板拷贝到主机上再运行。
./gen_add_symbol_file.sh /proc/24753/maps
...
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libgobject-2.0.so.0.1200.10 0x8fc140
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libgmodule-2.0.so.0.1200.10 0x938d30
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libbasecommon.so.0.0.0 0xad9680
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libglib-2.0.so.0.1200.10 0xb652c0
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libdbus-1.so.3.2.0 0xc34b20
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libdbus-glib-1.so.2.0.0 0xdeaab0
在客户端(或者主机端)上: 用前面生成的add-symbol-file加载共享库符号文件到指定的偏移量,然后设置断点,并进入正常调试。
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libgobject-2.0.so.0.1200.10 0x8fc140
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libgmodule-2.0.so.0.1200.10 0x938d30
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libbasecommon.so.0.0.0 0xad9680
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libglib-2.0.so.0.1200.10 0xb652c0
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libdbus-1.so.3.2.0 0xc34b20
Y
add-symbol-file /home/lixianjing/work/pcemu/broncho/usr/local/lib/libdbus-glib-1.so.2.0.0 0xdeaab0
(gdb) b g_main_loop_new
Breakpoint 2 at 0xb84ae4: file ../.././glib/gmain.c, line 2756.
(gdb) c
Continuing.
Breakpoint 2, 0x00b84ae4 in IA__g_main_loop_new (context=0x0, is_running=0) at ../.././glib/gmain.c:2756
2756 {
脚本文件gen_add_symbol_file.sh的内容为(对于ARM,objdump要换成对应的ARM版工具arm-linux-objdump):
#!/bin/bash
#Copyright (c) 2007 Li XianJing <xianjimli@hotmail.com>
if [ "$1" = "" ]
Then
echo "usage: " $0 " [maps file]"
exit 1
Fi
grep r-xp $1 |grep \.so >all_so.tmp.log
awk 'BEGIN{i=0} {print i " " strtonum("0x"substr($1, 0, 8)) " " $6; i++}'
all_so.tmp.log >baseaddr_so.tmp.log
awk '{system("objdump -h " $3 "| grep text");}' baseaddr_so.tmp.log |
awk 'BEGIN{i=0}{print i " " strtonum("0x" $4); i++}' >offset.tmp.log
join offset.tmp.log baseaddr_so.tmp.log >offset_baseaddr_so.tmp.log
awk '{printf("add-symbol-file %s 0x%x y ", $4, $2 + $3)}' offset_baseaddr_so.tmp.log
rm -f *.tmp.log 查看本文来源