在 gdb 会话中调用 malloc 失败

发布于 2025-01-03 19:39:25 字数 1007 浏览 0 评论 0原文

我正在尝试调试 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

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

#include <stdlib.h>.

这是源代码:

    // 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));

I am trying to debug a C program and gdb is telling me there is a segfault on line 329 of a certain function. So I set a break point for that function and I am trying to step through it. However, whenever I hit line 68 I get this complaint from 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

I don't know what this means. The program runs perfectly on all but one set of inputs so this call to malloc clearly succeeds during other executions of the program. And, of course, I have:

#include <stdlib.h>.

Here is the source code:

    // 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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2025-01-10 19:39:25

您可以安全地忽略这一点。 gdb 抱怨它没有 malloc 的源代码 - 几乎可以肯定您不想单步执行源代码。

两个简单的解决方案:

  • 使用next而不是step - 它不会下降到函数

  • 如果您已经不小心单步进入了某个函数,请使用finish 运行到该函数的 return 语句。

另一种方法是:

  • 您也可以在出现段错误之前稍微中断一下,而不是单步执行整个代码。

    • 您可以通过使用 break: 在特定行上放置断点来实现此目的(例如 break foo.c:320 在 foo.c 的第 320 行处中断。
    • 或者您可以使用 break 在特定函数上中断(例如 break foo 将在 foo() 的顶部中断 函数)。

You can safely ignore this. gdb is complaining that it doesn't have the source for malloc - and it's almost certain you don't want to step through the source.

Two easy solutions:

  • Use next instead of step - it won't descend into functions

  • If you've accidentally steped into a function already, use finish to run to the return statement of the function.

And an alternative approach:

  • You could also break a bit before the segfault, rather than stepping through the whole code.

    • You can do this by putting a breakpoint on a particular line with break <source file>:<line num> (for example break foo.c:320 to break on line 320 of foo.c).
    • Or you can break on a particular function with break <function name> (for example break foo will break at the top of the foo() function).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文