debugginggdbvalgrindcore-file

Get valgrind to generate a core file


I am trying to debug an error which i am hitting very rarely in a program. The program is running under valgrind

valgring --num-caller=50 testprog.out

I want to generate a core file only when this program hits the error.

I know there is an option to invoke gdb when we hit a error from valgring, But is there a way to give inputs to valgrind/gdb to generate core file without manual intervention?

Can I give inputs to gdb in a file?


Solution

  • Valgrind should be able to generate vgcore. when the program crashes. Unless core dump is not enabled (check ulimit if you are using linux)

    If the error does not cause the program to crash, then you can raise a SIGABRT/SIGSEGV/etc. signal inside the error block (See How to programmatically cause a core dump in C/C++).

    Example:

    if (foo() == ERR_CODE_FAILURE) // Your error is foo()
    {
        raise(SIGABRT);
    }