linux gdb调试动态库(so)的方法

原文地址:

http://www.gonwan.com/?tag=gdb

在本例中使用了libcurl.so.4库

step1:

编译libcurl.so.4的可调试版本:

# sudo apt-get source libcurl3-dbg
# cd curl-7.19.7/
# ./configure --prefix=/usr/local --enable-debug --enable-static=0
# make
编译好的带调试信息的动态库放在了这个 隐藏目录:

LD_LIBRARY_PATH=/home/gonwan/testgdb/curl-7.19.7/lib/.libs/ gdb ./testcurl


step2:编写调试程序并编译

#include 
int main() {
    curl_easy_init();
    return 0;
}
 gcc -g testcurl.c -o testcurl /usr/lib/libcurl.so.4

step3:启动gdb,调试上述代码

LD_LIBRARY_PATH=/home/gonwan/testgdb/curl-7.19.7/lib/.libs/ gdb ./testcurl
LD_LIBRARY_PATH的作用只制定加载动态库的位置,在这里指定了程序运行加载的动态库的位置

关于这个环境变量的详细解释,参见这里:

http://linuxmafia.com/faq/Admin/ld-lib-path.html


step4:调试程序:

root@localhost:/home/smb/curl-7.26.0# LD_LIBRARY_PATH=/home/smb/curl-7.26.0/lib/.libs gdb ./testcurl
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 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 "i486-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/smb/curl-7.26.0/testcurl...done.
(gdb) r
Starting program: /home/smb/curl-7.26.0/testcurl 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/i686/cmov/libthread_db.so.1".
[Inferior 1 (process 30604) exited normally]
(gdb) l
1       #include "./include/curl/curl.h" 
2       int main() {
3           curl_easy_init();
4           return 0;
5       }
(gdb) b 3
Breakpoint 1 at 0x80485da: file testcurl.c, line 3.
(gdb) c
The program is not being run.
(gdb) r
Starting program: /home/smb/curl-7.26.0/testcurl 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/i686/cmov/libthread_db.so.1".

Breakpoint 1, main () at testcurl.c:3
3           curl_easy_init();
(gdb) s
curl_easy_init () at easy.c:351
351       if(!initialized) {
(gdb) 
352         res = curl_global_init(CURL_GLOBAL_DEFAULT);
(gdb) q
A debugging session is active.

可见已经进入了动态库进行调试了


总结:可见可以通过让应用程序加载带调试信息的动态库,进行动态库的调试





你可能感兴趣的:(程序调试)