Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run GDB repeatedly on an executable file (and halt on crashes/errors)

Tags:

c++

gdb

I am trying to figure out a way to run an executable with GDB repeatedly and preferably halt on errors.

I could not find a way to do this in the manual!

Thanks

like image 963
theAlse Avatar asked May 04 '12 09:05

theAlse


People also ask

How do I stop continuing gdb?

To stop your program while it is running, type "(ctrl) + c" (hold down the ctrl key and press c). gdb will stop your program at whatever line it has just executed. From here you can examine variables and move through your program. To specify other places where gdb should stop, see the section on breakpoints below.

How do I list breakpoints in gdb?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

Why use gdb?

GDB allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.


2 Answers

I tried (in Bash):

while true ; do gdb -ex run a.out -ex quit ; done ;

Unfortunately, this turned out to be rather tricky to kill off, but it did automate running, and when the program crashed (I tested with an old program that causes a SIGABRT), gdb asks "The program is running. Exit anyway? (y or n)", so just hit n.

like image 163
BoBTFish Avatar answered Oct 05 '22 14:10

BoBTFish


I ran into this same problem, and came up with what I think is a fairly nice way of stopping the infinite loop that BoBTFish suggested.

Instead of looping while true, you can use the existence of a dummy file to control the loop, e.g:

touch loopfile; while [ -f loopfile ] ; do gdb -ex run a.out -ex quit ; done ;

When you want to stop your infinite debugging session, you can either open a new terminal in the same directory and rm loopfile, or you can exit from the same terminal by interrupting with control-c and then deleting loopfile from within gdb:

^CQuit
A debugging session is active.

    Inferior 1 [process 11136] will be killed.

Quit anyway? (y or n) n
Not confirmed.
(gdb) shell rm loopfile 
(gdb) quit
A debugging session is active.

    Inferior 1 [process 11136] will be killed.

Quit anyway? (y or n) y

Hopefully this is useful to someone in the future, it seems like a nice (if hackish) way of debugging intermittent problems.

like image 35
Gordon Bailey Avatar answered Oct 05 '22 14:10

Gordon Bailey