How to have gdb exit if program succeeds, break if program crashes?

前端 未结 7 1955
挽巷
挽巷 2020-12-10 02:23

I seem to have some kind of multithreading bug in my code that makes it crash once every 30 runs of its test suite. The test suite is non-interactive. I want to run my test

相关标签:
7条回答
  • 2020-12-10 02:27

    Are you not getting a core file when it crashes? Start gdb like this 'gdb -c core' and do a stack traceback.

    More likely you will want to be using Valgrind.

    0 讨论(0)
  • 2020-12-10 02:32

    You can also trigger a backtrace when the program crashes and let gdb exit with the return code of the child process:

    gdb -return-child-result -ex run -ex "thread apply all bt" -ex "quit" --args myProgram -myProgramArg
    
    0 讨论(0)
  • 2020-12-10 02:37

    This is a little hacky but you could do:

    gdb -ex='set confirm on' -ex=run -ex=quit --args ./a.out
    

    If a.out terminates normally, it will just drop you out of GDB. But if you crash, the program will still be active, so GDB will typically prompt if you really want to quit with an active inferior:

    Program received signal SIGABRT, Aborted.
    0x00007ffff72dad05 in raise (sig=...) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    64  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
        in ../nptl/sysdeps/unix/sysv/linux/raise.c
    A debugging session is active.
    
        Inferior 1 [process 15126] will be killed.
    
    Quit anyway? (y or n) 
    

    Like I said, not pretty, but it works, as long as you haven't toggled off the prompt to quit with an active process. There is probably a way to use gdb's quit command too: it takes a numeric argument which is the exit code for the debugging session. So maybe you can use --eval-command="quit stuff", where stuff is some GDB expression that reflects whether the inferior is running or not.

    This program can be used to test it out:

    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        if (time(NULL) % 2) {
            raise(SIGINT);
        }
        puts("no crash");
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
  • 2020-12-10 02:37

    Make it dump core when it crashes. If you're on linux, read the man core man page and also the ulimit builtin if you're running bash.

    This way when it crashes you'll find a nice corefile that you can feed to gdb:

    $ ulimit -c unlimited
    $ ... run program ..., gopher coffee (or reddit ;)
    $ gdb progname corefile
    
    0 讨论(0)
  • 2020-12-10 02:46

    If you put the following lines in your ~/.gdbinit file, gdb will exit when your program exits with a status code of 0.

    python
    
    def exit_handler ( event ):
      if event .exit_code == 0:
        gdb .execute ( "quit" )
    
    gdb .events .exited .connect ( exit_handler )
    
    end
    

    The above is a refinement of Kevin's answer.

    0 讨论(0)
  • 2020-12-10 02:48

    Create a file named .gdbinit and it will be used when gdb is launched.

    run
    quit
    

    Run with no options:

    gdb --args prog arg1...
    

    You are telling gdb to run and quit, but it should stop processing the file if an error occurs.

    0 讨论(0)
提交回复
热议问题