Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping gdb while loop when receiving signal

Tags:

c

debugging

gdb

I'm trying to find a segmentation fault in my program that doesn't happen all the time. I'm trying to run my program in a loop in gdb until the segmentation fault happens.

My problem is that the gdb continues the while loop after receiving the seg fault and doesn't prompt me with the gdb shell.

when I run my gdb I use:

set $i=0
while($i<100)
  set $i = $i+1
  r
end

Anybody know how to make the gdb stop at first segfault and not run 100 times??

Thanks!

like image 897
Netap Avatar asked Apr 20 '26 09:04

Netap


1 Answers

The gdb documentation is huge and it's difficult to find what you want but I could make that happen, and just by tweaking your script slightly.

Upon completion, gdb sets $_exitcode to the exit code value.

If segv occurs, the value isn't changed. So my idea was to set it to some stupid value (I chose 244) and run. But if return code is still 244 after the run command, then exit the loop (maybe there's another way to do it)

Warning: hack ahead (but that works)

set $i=0
while($i<100)
  set $i = $i+1
  set $_exitcode = 244
  r
  if $_exitcode==244
    set $i = 200
  end
end

I tested that with an interactive program. Type n for normal execution, and y to trigger segfault (well it would not trigger it, but there's a good chance for that to happen)

#include <stdio.h>
#include <stdlib.h>

int main()
{
   printf("want segfault?\n");
   char c = getchar();
   if (c=='y')
   {
    printf("%s", 'a');  // this is broken on purpose, to trigger segfault
   }
   return 0;
}

testing in a gdb session:

(gdb) source gdbloop.txt
[New Thread 6216.0x1d2c]
want segfault?
n
[Inferior 1 (process 6216) exited normally]
[New Thread 7008.0x1264]
want segfault?
n
[Inferior 1 (process 7008) exited normally]
[New Thread 8000.0x2754]
want segfault?
y

Breakpoint 1, 0x76b2d193 in wtoi () from C:\windows\syswow64\msvcrt.dll
(gdb) 

so I get the prompt back when a segfault is triggered.

like image 140
Jean-François Fabre Avatar answered Apr 22 '26 02:04

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!