Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-run program in GDB several times?

Tags:

debugging

gdb

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.

like image 394
sotona Avatar asked May 18 '16 12:05

sotona


People also ask

How do I rerun a program in GDB?

if you change and recompile your program in another window, you don't need to restart gdb. Just type "run" again, and gdb will notice the changes and load the new copy of your program. pressing enter executes the last command again. This makes it easily to step through your program line by line.

Can you recompile in GDB?

A key point is that you should not exit gdb before recompiling. After recompiling, when you issue the r command to rerun your program, gdb will notice that the source file is now newer than the binary executable file which it had been using, and thus will automatically reload the new binary before the rerun.

How do I complete a loop in GDB?

Is there a gdb command to finish a loop construct? Execute until on the last line of the loop, or until NNN where NNN is the last line of the loop. (gdb) help until Execute until the program reaches a source line greater than the current or a specified location (same args as break command) within the current frame.

What is batch mode in GDB?

According to GDB documentation : Batch mode disables pagination, sets unlimited terminal width and height see Screen Size, and acts as if set confirm off were in effect (see Messages/Warnings).


2 Answers

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
like image 166
ks1322 Avatar answered Sep 27 '22 19:09

ks1322


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
like image 38
Mark Plotnick Avatar answered Sep 27 '22 19:09

Mark Plotnick