DevNull DevNull - 5 months ago 32
Linux Question

GDB: Re-run program until it faults, answering prompts along the way

Issue






I am attempting to have automatically debug a program that I am reviewing, where there is a small, but real chance (ie: one in
10,000
chance) of a crash occurring due to a known bug. I contend with another engineer that it's serious enough to address, so I need to actually cause the crash to occur to justify putting the time into fixing it. To speed up testing, I plan to write a script to have the application run in GDB, for days if need be, until it crashes.




Prior Research






I've found out how to run simple applications through GDB until segfaulting via a separate answer on StackOverflow, but I need to extend the problem a bit. So, I know I can run a program repeatedly until it crashes via:




set pagination off
break exit
commands
run
end





Problem






What I need to do is figure out how to issue certain commands. After my program runs for a few seconds, it will ask the user to enter (via
stdin
) a number in the range of
[0,100]
, and hit ENTER. I would like it to enter
0
,ENTER, on the first iteration,
1
,ENTER, on the second iteration, etc. I realize this would b a lot easier if I added command-line arguments to the application, but I'm not allowed to change it at this time, and must rely on the interactive mode of operation it provides.




Question






How would I create a command to automatically generate these keystrokes/patterns with GDB? I'm guessing I could write some sort of
GDBINIT
script, but I mostly use GDB interactively for getting backtraces, analyzing memory, etc, and am not overly familiar with automating tasks like these.

Thank you.

Answer

Because you program reads its input from stdin, your task is straightforward. Simply create your input file as follows:

1
2
3
...

Then, your gdb command sequence changes its run phase to include the input file (say input.txt):

set pagination off
break exit
commands
run < input.txt
end