Search code examples
cdebugginggccgdbcoredump

Inspecting caller frames with gdb


Suppose I have:

#include <stdlib.h>

int main()
{
    int a = 2, b = 3;
    if (a!=b)
        abort();
}

Compiled with:

 gcc -g c.c

Running this, I'll get a coredump (due to the SIGABRT raised by abort()), which I can debug with:

gdb a.out core

How can I get gdb to print the values of a and b from this context?


Solution

  • Here's the another way to specifically get a and b values by moving to the interested frame and then info locals would give you the values. a.out was compiled with your code. (frame 2 is what you are interested in i.e., main()).

    $ gdb ./a.out core
    [ removed some not-so-interesting info here ]
    Reading symbols from ./a.out...done.
    [New LWP 14732]
    Core was generated by `./a.out'.
    Program terminated with signal SIGABRT, Aborted.
    #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
    51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
    (gdb) bt
    #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
    #1  0x00007fac16269f5d in __GI_abort () at abort.c:90
    #2  0x00005592862f266d in main () at f.c:7
    (gdb) frame 2
    #2  0x00005592862f266d in main () at f.c:7
    7               abort();
    (gdb) info locals
    a = 2
    b = 3
    (gdb) q
    

    You can also use print once frame 2:

    (gdb) print a
    $1 = 2
    (gdb) print b
    $2 = 3