22

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));
3
  • 2
    If malloc is failing the most likely cause is heap corruption. Have you tried running your program under valgrind?
    – JaredPar
    Feb 10, 2012 at 0:03
  • 4
    use 'next' instead of 'step'. You are trying to step into malloc, and gdb is complaining that it doesn't have access to the source code for malloc. You really do not want to step into malloc. Feb 10, 2012 at 0:12
  • 2
    How about including the source of malloc on gdb? How can we do this??
    – Paschalis
    Apr 29, 2015 at 22:06

1 Answer 1

26

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.