2012-12-18 43 views
4

我正嘗試調試使用GDB從大量共享庫構建的應用程序。無法使用GDB步入共享庫中的函數

啓動gdb的:

prompt$ gdb 
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-50.el6) 
Copyright (C) 2010 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
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 "x86_64-redhat-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 

告訴GDB程序調試:

(gdb) file /home/me/build-path/my-program 
Reading symbols from /home/me/build-path/my-program...done. 

設置應用程序中的斷點:

(gdb) my-program-src.cpp:57 
breakpoint 1 at 0x819df9b: file src/my-program-src.cpp, line 57 

運行程序:

(gdb) run 
Starting program: /home/me/build-path/my-program 

程序在斷點處停止,符合市場預期:

Breakpoint 1 MyClass:func(this-0xffffc1c0) at src/my-program-src.cpp:235 

my-program-src.cpp 235是一個構造函數調用的class Derived這是MySharedLib1.so

「類派生」從「類基地」,這是在MySharedLib2.so

如果我現在步驟,裏面的程序退出衍生`MySharedLib2.so」與SIG SEGV(這是我試圖調試),即:

Program received signal SIGSEGV, Segmentation fault. 
0x0024c2fa in osal::MsgQMsg::id(unsigned int)() from /home/me/build-path/lib/libMySharedLib2.so 

GDB沒有進入任何一個共享庫。

bt給出現問題的函數的名稱,但list顯示my-program-src.cpp

代碼的所有代碼與下面的選項編譯:

gcc -MD -D__LINUX__ -g -Wall -Wextra -Iinc -m32 -fpic -I../../public_inc /home/me/src/file.c -o /home/me/build-path/obj/file.o 

共享庫與以下鏈接選項:

gcc -o /home/me/build-path/lib/libMySharedLib1.so -shared /home/me/build-path/obj/file.o -L/home/me/build-path/lib/ -m32 

如果我更改Makefiles以便構建存檔庫(即.a)我可以按照預期進入功能。

更多信息:

如果我嘗試手動從共享庫,我得到以下添加符號:

(gdb) add-symbol-file /home/me/build-path/lib/libMySharedLib2.so 
The address where /home/me/build-path/lib/libMySharedLib2.so has been loaded is missing 

(注:我從add-symbol-file相同的反應,一旦斷點已經點擊)

如果我可以在共享庫中的函數中設置斷點,GDB按預期中斷,但是如果我輸入list GDB在主應用程序代碼中顯示調用行(即調用函數不在共享庫中)。 GDB不會抱怨找不到源文件。

爲什麼我不能進入我的共享庫?

爲什麼我無法逐步瀏覽共享庫中的代碼?

+0

GDB不會在構造函數中的一步。你有沒有嘗試加入共享庫中的其他功能?還有你用-g選項編譯共享庫嗎? –

+1

所有代碼都使用'-g'選項進行編譯並且不進行優化。 GDB不會進入共享庫中的任何函數。它將在共享庫中設置的斷點處停止,但我無法列出源代碼行,並且我無法查看GDB停止的類/函數本地的任何變量。 – mark

+2

這可能是一個32/64位的問題。您正在調試64位版本的GDB的32位程序。 GDB並不是世界上最穩定的軟件,也有錯誤。嘗試使用GDB的32位版本。 –

回答

0

它可能是一個錯字,但是當您嘗試加載符號時,使用libMySharedLib2.so而不是2而不是1

在任何情況下,您都應該使用g++來編譯和鏈接C++代碼。此外,主程序不一定是圖片,儘管它可能不會受傷。

它爲我如下:

$ cat >lib.h 
class Base 
{ 
     int _x; 
    public: 
     Base(int); 
}; 

class Derived : public Base 
{ 
    public: 
     Derived(int x); 
}; 
$ cat >lib.cpp 
#include "lib.h" 

Base::Base(int x) 
{ 
    _x = *reinterpret_cast<int*>(x); 
} 

Derived::Derived(int x) : Base(x) 
{ 
} 
$ cat >main.cpp 
#include "lib.h" 

int main(int, char**) 
{ 
    Derived d(0); 
    return 0; 
} 
$ g++ -shared -fpic -m32 -g -Wall -o libMySharedLib1.so lib.cpp 
$ g++ -m32 -g -Wall -L. -l MySharedLib1 main.cpp 
$ LD_LIBRARY_PATH=$PWD gdb ./a.out 
GNU gdb (GDB) 7.3.50.20111117-cvs-debian 
Copyright (C) 2011 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
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 "x86_64-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>... 
Reading symbols from a.out...done. 
(gdb) r 
Starting program: a.out  

Program received signal SIGSEGV, Segmentation fault. 
0xf7fdb552 in Base::Base (this=0xffffd83c, x=0) at lib.cpp:5 
5   _x = *reinterpret_cast<int*>(x); 
(gdb) bt 
#0 0xf7fdb552 in Base::Base (this=0xffffd83c, x=0) at lib.cpp:5 
#1 0xf7fdb5ba in Derived::Derived (this=0xffffd83c, x=0) at lib.cpp:8 
#2 0x08048591 in main() at main.cpp:5 
(gdb) br main 
Breakpoint 1 at 0x804857d: file main.cpp, line 5. 
(gdb) r 
The program being debugged has been started already. 
Start it from the beginning? (y or n) y 
Starting program: a.out  

Breakpoint 1, main() at main.cpp:5 
5   Derived d(0); 
(gdb) s 
Derived::Derived (this=0xffffd83c, x=0) at lib.cpp:8 
8  Derived::Derived(int x) : Base(x) 
(gdb) s 
Base::Base (this=0xffffd83c, x=0) at lib.cpp:5 
5   _x = *reinterpret_cast<int*>(x); 

(GDB輸出稍微改動過)