15

I have a program which fails sporadically, but with the same error. To debug it I'd like to run it under GDB until it fails, set breakpoints and re-run it. what do I do:

gdb --args /path/to/program <program args>

But I can't find anywhere how do I tell GDB "run this program 100 times" for example.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
sotona
  • 1,731
  • 2
  • 24
  • 34
  • 2
    Rename `main()` to `my_program()` and add a `int main()` that calls `my_program()` 100 times? (I know this might break for some of the trickier programs that do things with `atexit()` etc., but it might serve as a workaround.) – DevSolar May 18 '16 at 13:14
  • 3
    write a bash or dos script to run it 100 times. – Gregg May 18 '16 at 13:34
  • @Gregg this appears to be the only solution – sotona May 18 '16 at 13:35
  • 1
    I would suggest gdb scripts. See this question for instance: http://stackoverflow.com/questions/10748501/what-are-the-best-ways-to-automate-a-gdb-debugging-session – Aif May 18 '16 at 13:54
  • Is there any terminating condition or do you want to run the program exactly 100 times? – Mark Plotnick May 18 '16 at 15:17
  • @MarkPlotnick I don't need to re-run the program exactly N times, but until certain event occurs, for example sigsegv. In my case it occurs once per 80-100 runs – sotona May 18 '16 at 15:55
  • Possible duplicate of [How can I rerun a program with gdb until a segmentation fault occurs?](https://stackoverflow.com/questions/6545763/how-can-i-rerun-a-program-with-gdb-until-a-segmentation-fault-occurs) – ks1322 Sep 12 '17 at 10:33

2 Answers2

16

The simplest solution I can think of is to run program in infinite while loop until it fails or you press Ctrl+C to break the loop.

(gdb) while 1
 >run
 >end
ks1322
  • 33,961
  • 14
  • 109
  • 164
14

This gdb script will run the program 100 times, or until it receives a signal. $_siginfo is non-void if the program is stopped due to a signal, and is void if the program exited. As far as I can tell, any stop of the process, including breakpoints, watchpoints, and single-stepping, will set $_siginfo to something.

set $n = 100
while $n-- > 0
  printf "starting program\n"
  run
  if $_siginfo
    printf "Received signal %d, stopping\n", $_siginfo.si_signo
    loop_break
  else
    printf "program exited\n"
  end
end
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40