mar*_*ark 6 c++ debugging gdb shared-libraries
我正在尝试使用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/>.
Run Code Online (Sandbox Code Playgroud)
告诉GDB要调试的程序:
(gdb) file /home/me/build-path/my-program
Reading symbols from /home/me/build-path/my-program...done.
Run Code Online (Sandbox Code Playgroud)
在应用程序内设置一个断点:
(gdb) my-program-src.cpp:57
breakpoint 1 at 0x819df9b: file src/my-program-src.cpp, line 57
Run Code Online (Sandbox Code Playgroud)
运行程序:
(gdb) run
Starting program: /home/me/build-path/my-program
Run Code Online (Sandbox Code Playgroud)
程序按预期在断点处停止:
Breakpoint 1 MyClass:func(this-0xffffc1c0) at src/my-program-src.cpp:235
Run Code Online (Sandbox Code Playgroud)
的235行是in中my-program-src.cpp的构造函数调用。class DerivedMySharedLib1.so
“派生类”派生自“基础类” MySharedLib2.so
如果现在执行此步骤,程序将以SIG SEGV(这是我要调试的内容)在“ MySharedLib2.so”中退出,即:
Program received signal SIGSEGV, Segmentation fault.
0x0024c2fa in osal::MsgQMsg::id(unsigned int) () from /home/me/build-path/lib/libMySharedLib2.so
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
共享库与以下选项链接:
gcc -o /home/me/build-path/lib/libMySharedLib1.so -shared /home/me/build-path/obj/file.o -L/home/me/build-path/lib/ -m32
Run Code Online (Sandbox Code Playgroud)
如果更改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
Run Code Online (Sandbox Code Playgroud)
(注意:add-symbol-file一旦击中断点,我将得到相同的响应)
如果可以在共享库中的函数中设置断点,则GDB会按预期中断,但是如果我键入listGDB,则会在主应用程序代码中显示调用行(即,不在共享库中的调用函数)。GDB不会抱怨找不到源文件。
为什么我不能进入共享库?
为什么我不能单步执行共享库中的代码?
如果共享库没有调试符号/信息,默认情况下 gdb 将跳过它而不是进入它。您可以使用stepi(abbrev si) 来代替单个指令。我发现 .gdbinit 文件中的以下命令很有用:
define sx
si
x /1i $pc
end
document sx
Step one instruction and print next instruction
end
define nx
ni
x /1i $pc
end
document nx
Step one instruction running through calls and print next instruction
end
Run Code Online (Sandbox Code Playgroud)
它定义了命令sx/nx以单条指令逐步执行,然后反汇编要运行的下一条指令。在尝试通过没有调试信息的机器代码进行调试时非常有用。
这可能是一个拼写错误,但当您尝试加载符号时,您使用了libMySharedLib2.soa 而2不是 a 。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);
Run Code Online (Sandbox Code Playgroud)
(gdb 输出稍作编辑)
| 归档时间: |
|
| 查看次数: |
4415 次 |
| 最近记录: |