Search code examples
clinuxgdbubuntu-10.04

Make gdb quit automatically on successful termination?


I use a debugging script that runs several related processes in succession with the debugger. I'm currently using -x to execute several commands automatically (such as run). How can I make gdb quit automatically when the debugged process successfully terminates? Adding a quit command to the command file will cause that command to be handled not just on successful termination, but when errors occur also (when I'd rather take over at that point).

Here's an extract of what's going on:

+ gdb -return-child-result -x gdbbatch --args ./mkfs.cpfs /dev/loop0
GNU gdb (GDB) 7.1-ubuntu
Reading symbols from /home/matt/cpfs/mkfs.cpfs...done.

Program exited normally.
Breakpoint 2 at 0x805224f: file log.c, line 32.
(gdb)

Contents of gdbbatch:

start
b cpfs_log if level >= WARNING

Solution

  • I think I have found a complete solution to your question in connection to looking for something similar in How to make gdb send an external notification on receiving a signal?. None of the other guys here seem to have mentioned or discovered gdb hooks.

    Based on Matthew's tip about $_exitcode, this is now my app/.gdbinit that achieves exactly the behavior wanted; normal quit on successful termination and drop to gdb prompt, send email, whatnot on everything else:

    set $_exitcode = -999
    set height 0
    handle SIGTERM nostop print pass
    handle SIGPIPE nostop
    define hook-stop
        if $_exitcode != -999
            quit
        else
            shell echo | mail -s "NOTICE: app has stopped on unhandled signal" root
        end
    end
    echo .gdbinit: running app\n
    run