12

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

の 235 行目my-program-src.cppは、 のコンストラクター呼び出しclass DerivedですMySharedLib1.so

「class Derived」は、「class Base」にある「class Base」から派生しています。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

アーカイブ ライブラリがビルドされるように Makefile を変更すると (つまり.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 は期待どおりに中断しますが、GDB と入力listすると、メイン アプリケーション コードの呼び出し行 (つまり、共有ライブラリにない呼び出し関数) が表示されます。GDB は、ソース ファイルが見つからないことについて文句を言いません。

共有ライブラリにステップインできないのはなぜですか?

共有ライブラリのコードをステップ実行できないのはなぜですか?

4

3 に答える 3

2

タイプミスかもしれませんが、シンボルを読み込もうとしたときにnotを使用libMySharedLib2.soしました。21

いずれにせよ、g++C++ コードのコンパイルとリンクには を使用する必要があります。さらに、メイン プログラムは pic である必要はありませんが、おそらく問題はありません。

それは次のように私のために働きます:

$ 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 の出力を少し編集)

于 2012-12-19T22:30:27.087 に答える