【轉】gdb調試動態鏈接庫


【原文地址】http://www.microsuncn.com/index.php?title=%E7%94%A8gdb%E8%B0%83%E8%AF%95%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93

在 Linux 可以用 gdb 來調試應用程序,當然前提是用 gcc 編譯程序時要加上 -g 參數。我這篇文章里將討論一下用 gdb 來調試動態鏈接庫的問題。首先,假設我們准備這樣的一個動態鏈接庫:引用:庫名稱是: ggg 動態鏈接庫文件名是: libggg.so 頭文件是: get.h 提供這樣兩個函數調用接口:

   int get ();
   int set (int a);

要生成這樣一個動態鏈接庫,我們首先編寫這樣一個頭文件:

/************關於本文檔********************************************
*filename: get.h
*********************************************************************/
int get ();
int set (int a);

然后准備這樣一個生成動態鏈接庫的源文件:

/************關於本文檔********************************************
*filename:  get.cpp
*********************************************************************/
#include <stdio.h>
#include "get.h"

static int x=0;
int get ()
{
        printf ("get x=%d\n", x);
        return x;
}

int set (int a)
{
        printf ("set a=%d\n", a);
        x = a;
        return x;
}

然后我們用 GNU 的 C/C++ 編譯器來生成動態鏈接庫,編譯命令如下:

g++ get.cpp -shared -g -DDEBUG -o libggg.so
g++ get.cpp -shared -g -fPIC -DDEBUG -o libggg.so (64位機器)

這樣我們就准備好了動態鏈接庫了,下面我們編寫一個應用程序來調用此動態鏈接庫,源代碼如下:

/************關於本文檔********************************************
*filename: pk.cpp
*********************************************************************/
#include <stdio.h>
#include "get.h"
int main (int argc, char** argv)
{
        int a = 100;
        int b = get ();
        int c = set (a);
        int d = get ();

        printf ("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

        return 0;
}

編譯此程序用下列命令,如果已經把上面生成的 libggg.so 放到了庫文件搜索路徑指定的文件目錄,比如 /lib 或 /usr/lib 之類的,就用下面這條命令:

g++ pk.cpp -o app -Wall -g -lggg

否則就用下面這條命令:

g++ pk.cpp -o app -Wall -g -lggg -L`pwd`

下面我們就開始調試上面命令生成的 app 程序吧。如果已經把上面生成的 libggg.so 放到了庫文件搜索路徑指定的文件目錄,比如 /lib 或 /usr/lib 之類的,調試就順利完成,如下:

#gdb ./app
GNU gdb 6.4-debian
Copyright 2005 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 "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

(gdb) b main    /* 這是在程序的 main 處設置斷點 */
Breakpoint 1 at 0x804853c: file pk.cpp, line 7.
(gdb) b set      /* 這是在程序的 set 處設置斷點 */
Function "set" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y /* 這里必須選擇 y 調試程序才會跟蹤到動態鏈接庫內部去 */
Breakpoint 2 (set) pending.
(gdb) run /* 開始運行我們的程序,直到遇見斷點時暫停 */
Starting program: /data/example/c/app
Breakpoint 3 at 0xb7f665f8: file get.cpp, line 11.
Pending breakpoint "set" resolved

Breakpoint 1, main (argc=1, argv=0xbf990504) at pk.cpp:7
7               int a = 100;
(gdb) n     /* 繼續執行程序的下一行代碼 */
8               int b = get ();
(gdb) n      /* 程序執行到了我們斷點所在的動態鏈接庫了 */
get x=0
9               int c = set (a);
(gdb) n

Breakpoint 3, set (a=100) at get.cpp:11
11              printf ("set a=%d\n", a);
(gdb) list   /* 查看當前代碼行周圍的代碼,證明我們已經跟蹤到動態鏈接庫的源代碼里面了 */
6               printf ("get x=%d\n", x);
7               return x;
8       }
9       int set (int a)
10      {
11              printf ("set a=%d\n", a);
12              x = a;
13              return x;
14      }
(gdb) n
set a=100
12              x = a;
(gdb) n
13              return x;
(gdb) n
14      }
(gdb) n
main (argc=1, argv=0xbf990504) at pk.cpp:10
10              int d = get ();
(gdb) n
get x=100
11              printf ("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
(gdb) n
a=100,b=0,c=100,d=100
12              return 0;
(gdb) c
Continuing.

Program exited normally.
(gdb) quit  /* 程序順利執行結束 */

如果我們沒有把動態鏈接庫放到指定目錄,比如/lib里面,調試就會失敗,過程如下:

# gdb ./app
GNU gdb 6.4-debian
Copyright 2005 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 "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

(gdb) b main
Breakpoint 1 at 0x804853c: file pk.cpp, line 7.
(gdb) b set
Function "set" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (set) pending.
(gdb) run  /* 雖然調試操作都一樣,但程序執行失敗 */
Starting program: /data/example/c/app
/data/example/c/app: error while loading shared libraries: libggg.so: cannot open shared object file: No such file or directory

Program exited with code 0177.
(gdb) quit 

調試失敗的原因是因為gdb不能找到libggg.so,可以通過下面的方法解決:

1) 將庫路徑加到LD_LIBRARY_PATH里
2) 執行:ldconfig YOUR_LIB_PATH
3) 在/etc/ld.so.conf里加入庫所在路徑。然后執行:ldconfig

上面3個方法任意一個都可以,然后再去用gdb調試就沒有問題了。

另:

1、假設我的可執行程序是ServerName,共享庫為worker.so
2、我用gdb調試ServerName,想在B的某個源文件(比如worker.cpp,worker.cpp與ServerName不在同一個目錄下)中設置斷點,使用下面的命令行break worker.cpp:123

若找不到源文件可使用如下命令設定源文件目錄:

設定gdb環境變量 LD_PRELOAD,在執行程序前先把共享庫代碼load進來
指定你的鏈接庫的位置,可以通過設定環境變量LD_LIBRARY_PATH來實現

拷貝到標准的lib搜尋目錄下,例如/usr/lib等

b main, r,然后再設置斷點就可以了,共享庫只有當程序運行才開始加載的 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM