【问题标题】:Trouble debugging functions in linked libraries with gdb使用 gdb 调试链接库中的函数时遇到问题
【发布时间】:2017-11-25 02:52:23
【问题描述】:

我正在阅读有关 gdb 的一些信息,但我无法让 gdb(我正在运行 7.11.1)调试库中的函数。
用于了解调试器的示例代码非常简单:

#include <stdio.h>
#include <string.h>

int main() {
    char str_a[20];

    strcpy(str_a, "Hello, world!\n");
    printf(str_a);
}

我在启用调试符号的情况下编译它,启动 GDB,并设置一些断点:

(gdb) list
1   #include <stdio.h>
2   #include <string.h>
3   
4   int main() {
5       char str_a[20];
6   
7       strcpy(str_a, "Hello, world!\n");
8       printf(str_a);
9   }
(gdb) break 7
Breakpoint 1 at 0x4005ad: file char_array2.c, line 7.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (strcpy) pending.
(gdb) break 8
Breakpoint 3 at 0x4005cf: file char_array2.c, line 8.
(gdb) run
Starting program: /home/david/hacking_the_art_of_exploitation/Chapter_2/char_array2 

Breakpoint 1, main () at char_array2.c:7
7       strcpy(str_a, "Hello, world!\n");
(gdb) continue
Continuing.

Breakpoint 3, main () at char_array2.c:8
8       printf(str_a);
(gdb) continue
Continuing.
Hello, world!
[Inferior 1 (process 7061) exited normally]

如您所见,调试器永远不会进入 strcpy 函数。

我尝试将 set stop-on-solib-events 1 添加到我的 .gdbinit 中。这会导致不同但仍然不受欢迎的结果:

(gdb) run
Starting program: /home/david/hacking_the_art_of_exploitation/Chapter_2/char_array2 
Stopped due to shared library event (no libraries added or removed)

我在这里有点茫然。提前感谢您的帮助。

【问题讨论】:

  • 为什么要调试strcpy函数?看到调用结果还不够吗?就像从调试器内部打印str_a 的内容一样?你实际上有什么问题?
  • 另请注意,编译器甚至可能不会调用“strcpy”函数,它可能会用高度优化的内联代码替换它。
  • @Someprogrammerdude 离题,但是,当您说编译器可能会使用内联代码优化库调用时,这很整洁!你能提供一个参考吗?我对编译器优化的这方面感兴趣?
  • @Miket25: 编译include &lt;string.h&gt; / void foo(int *a, int *b) { memcpy(a, b, sizeof *a); } 完全优化(-O3 与 GCC)和汇编输出(-S 与 GCC)。使用任何好的编译器,生成的程序集都不会调用memcpy,而是调用加载指令和存储指令,或者如果体系结构有内存移动指令。

标签: c debugging gdb


【解决方案1】:

我相信您需要 libc-dbg 包和 libc 源包来调试 libc 函数。在 Ubuntu 上,您可以通过

安装它
sudo apt-get install libc6-dbg
mkdir ~/libc ; cd ~/libc
apt-get source libc6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多