DevNull DevNull - 1 year ago 131
Linux Question

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


I am attempting to have automatically debug a program that I am reviewing, where there is a small, but real chance (ie: one in
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


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
) a number in the range of
, and hit ENTER. I would like it to enter
,ENTER, on the first iteration,
,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.


How would I create a command to automatically generate these keystrokes/patterns with GDB? I'm guessing I could write some sort of
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 Source

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


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

set pagination off
break exit
run < input.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download