c++ - 无法使用 GDB 进入共享库中的函数

标签 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/>.

告诉 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 .

“class Derived”派生自“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给出发生问题的函数的名称,但是 listmy-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 会按预期中断,但如果我输入 list GDB 显示主应用程序代码中的调用行(即不在共享库中的调用函数)。 GDB 不会提示找不到源文件。

为什么我不能进入我的共享库?

为什么我不能单步执行共享库中的代码?

最佳答案

如果共享库没有调试符号/信息,gdb 将默认跳过它而不是进入它。您可以使用 stepi(缩写 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

定义了命令sx/nx单步执行,然后反汇编下一条要运行的指令。在没有调试信息的情况下尝试通过机器代码进行调试时非常有用。

关于c++ - 无法使用 GDB 进入共享库中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13931242/

相关文章:

c# - C# 中的 C++ 非托管 DLL

c++ - 我怎样才能 "override"[] 在 C++ 中接受两个参数?

c++ - 如何在 C++ 中定义自定义浮点格式(类型)?

c++ - 使用rapidJson压缩json文件后如何将输出存储在c++字符串中

debugging - 断言编程的好处

c#以编程方式强制 Debug模式

javascript - Chrome 调试器注入(inject) javascript

gdb - 使用gdb在多屏窗口中调试MPI

assembly - 使用 GDB 在本地标签处中断 NASM 程序集

linux - 为什么附加会停止 Linux 中的进程/线程?