Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rerun a program with gdb until a segmentation fault occurs?

Tags:

gdb

My program has a segmentation fault problem, but it faults rarely(once in 20 times or more), and to debug it in GDB, I need to manually rerun the program until the segmentation fault occurs (during a half day of reruns only once it fails :( ).

So the questions is, is there any way to tell the GDB to rerun program until some segfault?

like image 297
MKo Avatar asked Jul 01 '11 09:07

MKo


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.

How do you trigger a segmentation fault?

The following are some typical causes of a segmentation fault: Attempting to access a nonexistent memory address (outside process's address space) Attempting to access memory the program does not have rights to (such as kernel structures in process context) Attempting to write read-only memory (such as code segment)

How do I restart GDB?

When you're doing a usual gdb session on an executable file on the same computer, you can give the run command and it will start the program over again.


2 Answers

Put a breakpoint at the exit of your program that triggers the run command, and don't forget set pagination off. Information on settings commands is available in the Breakpoint Command Lists section of the gdb documentation. In short:

set pagination off break _exit commands run end 

After the commands line you'll see that the next two lines are being entered as the command to execute when the breakpoint is reached.

like image 64
borrible Avatar answered Sep 21 '22 18:09

borrible


(gdb) set pagination off (gdb) break exit (gdb) commands >run >end (gdb) run 
like image 29
Oleksandr Kozlov Avatar answered Sep 21 '22 18:09

Oleksandr Kozlov