cgdbvalgrindsymbolscore-file

How to examine a core file generated when program is running under valgrind


I have a program myprog which I have been running under valgrind:

/usr/bin/valgrind --tool=massif --tool=memcheck --leak-check=yes --track-origins=yes --log-file=/tmp/valgrind%p /opt/bin/myprog

I'm not exactly sure of the details of how valgrind works under the covers, but when run in this manner I see that valgrind has a pid, but myprog does not. Thus I assume valgrind runs myprog in the same process space and uses some magic to load the target program.

myprog appears to have hit an assert() and a corefile has been generated. I would like to examine this corefile using GDB so I can dig into the cause of the assert(). I'm trying to figure out how to load symbols from myprog necessary to use GDB.

If I use file valgrind, then this just loads valgrind symbols and nothing from myprog

If I use file myprog, then the corefile (generated by valgrind) won't match the executable (myprog).

Is there some way I can tell GDB that even though the "host" process here is valgrind, that we also want to load the myprog symbols, as this is the program we are actually interested in?

Looking through the GDB documentation, I'm guessing that I'd need something like add-symbol-file filename address, but I'm not sure how to figure out what the address would be.


Solution

  • I'm trying to figure out how to load symbols from myprog necessary to use GDB.

    The usual way: gdb ./myprog vgcore.1234.

    If I use file myprog, then the corefile (generated by valgrind) won't match the executable (myprog).

    It should match. Either you are doing something wrong (do tell exactly what you are doing), or you have a bug in valgrind or gdb.

    Example:

    $ cat foo.c
    int foo() { abort(); }
    int bar() { return foo(); }
    int main() { return bar(); }
    
    $ gcc -g -w foo.c
    $ valgrind ./a.out
    
    ==84263== Using Valgrind-3.9.0.SVN and LibVEX; rerun with -h for copyright info
    ==84263== Command: ./a.out
    ==84263==
    ==84263==
    ==84263== HEAP SUMMARY:
    ==84263==     in use at exit: 0 bytes in 0 blocks
    ==84263==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
    ...
    
    $ ls -lrt
    total 2116
    -rw-r----- 1 er eng      80 Dec 15 20:18 foo.c
    -rwxr-x--- 1 er eng    9558 Dec 15 20:21 a.out
    -rw------- 1 er eng 4243434 Dec 15 20:21 vgcore.84263
    
    $ gdb -q ./a.out vgcore.84263    
    Reading symbols from ./a.out...done.
    [New LWP 84263]
    Core was generated by `'.
    Program terminated with signal SIGABRT, Aborted.
    #0  0x0000000004a5bc37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
    56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
    (gdb) bt
    #0  0x0000000004a5bc37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
    #1  0x0000000004a5f028 in __GI_abort () at abort.c:89
    #2  0x0000000000400536 in foo () at foo.c:1
    #3  0x0000000000400544 in bar () at foo.c:2
    #4  0x0000000000400554 in main () at foo.c:3