调用malloc在gdb会话中失败

Sch*_*mer 22 c malloc gdb

我正在尝试调试一个C程序,gdb告诉我在某个函数的第329行有一个段错误.所以我为该功能设置了一个断点,我试图逐步完成它.但是,每当我点击第68行时,我都会从gdb收到此投诉:

(gdb) step
68              next_bb = (basic_block *)malloc(sizeof(basic_block));
(gdb) step
*__GI___libc_malloc (bytes=40) at malloc.c:3621
3621    malloc.c: No such file or directory.
in malloc.c
Run Code Online (Sandbox Code Playgroud)

我不知道这意味着什么.该程序在除一组输入之外的所有输入上运行完美,因此在执行该程序的其他执行期间,对malloc的调用显然成功.当然,我有:

#include <stdlib.h>.
Run Code Online (Sandbox Code Playgroud)

这是源代码:

    // Block currently being built.
    basic_block *next_bb = NULL;
    // Traverse the list of instructions in the procedure.
    while (curr_instr != NULL)
    {
        simple_op opcode = curr_instr->opcode;
        // If we are not currently building a basic_block then we must start a new one.
        // A new block can be started with any kind of instruction.
        if (!in_block)
        {
            // Create a new basic_block.
            next_bb = (basic_block *)malloc(sizeof(basic_block));
Run Code Online (Sandbox Code Playgroud)

Tim*_*nes 25

你可以放心地忽略这个.gdb抱怨它没有malloc的源代码 - 而且几乎可以肯定你不想逐步完成源代码.

两个简单的解决方

  • 使用next而不是步骤 - 它不会下降到函数中

  • 如果您已经意外地step编写了函数,请使用finish运行函数的return语句.

另一种方法是:

  • 你也可以在segfault之前打破一点,而不是单步执行整个代码.

    • 您可以通过在特定行上放置断点来实现此目的break <source file>:<line num>(例如break foo.c:320,在foo.c的第320行中断).
    • 或者您可以使用break <function name>(例如break foo将在foo()函数顶部中断)中断特定函数.