用gdbserver调试共享库(改进版)
用gdbserver调试共享库(改进版)
转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>
我在<嵌入式linux调试:用gdbserver调试共享库>一文中介绍了用gdbserver调试共享库的方法,虽然可行,但是由于计算偏移量很麻烦,除了在非常必要的情况下,我们都很少使用它。昨天我们讨论了一下,看能不能修改gdbserver/gdb来实现自动计算。理论上这是可行的,不过修改gdbserver和gdb之间的通信协议,我们不想搞得那么复杂,最后决定用半自动的方式实现:用脚本计算偏移量,但要手工去增加符号文件。
本文以PC机为例演了整个过程,在ARM板上的调试方法类似:
在服务器端(或者目标板)上:运行gdbserver,并记住打印出来的PID,后面用它来获得进程的maps文件。
gdbserver localhost:2222 networkmanager &
Process networkmanager created; pid = 24753
Listening on port 2222
在客户端(或者主机端)上:运行gdb,加载可执行文件的符号文件,在主函数处设置断点,并执行到这里。gdbserver刚运行起来时,共享库还没有加载,所以要执行到main函数才能进行后面的操作。
lixianjing@localhost server>gdb
GNU 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:2222
Remote debugging using localhost:2222
0x00293840 in ?? ()
(gdb) symbol-file networkmanager
Reading symbols from /home/lixianjing/work/groupware/network-
anager/server/networkmanager...done.
(gdb) b main
Breakpoint 1 at 0x804a565: file main.c, line 16.
(gdb) c
Continuing.
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):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
一直没有找到比较完美的方法,若哪位高手知道,请不吝赐教。
~~end~~