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.
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.
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.
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.
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).
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With