28

I get a segmentation fault when I cancel my program. I am using gdb for debugging, the problem is that when I press Ctrl-C while I am debugging gdb does not cancel the program and do what it is supposed to do that is stop it.

What I want is do the Ctrl-C and do not allow gdb to stop it. Is there any way to do this? Any other recommendations for debugging? I cannot use printf because sincerely I have not a clear idea where the problem comes from.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
Eduardo
  • 19,928
  • 23
  • 65
  • 73

4 Answers4

37

gdb is intercepting the signal. When you press CTRL-C, you're actually causing the terminal driver to generate a SIGINT.

What you need to do is have GDB generate the SIGINT using the signal command. the syntax is

signal num

and man signal will tell you the signal number (in this case, SIGINT is signal 2, so signal 2 will do it.)

Update

Sure enough, you can use the symbolic name. info signal will tell you all the names etc.

Oh, by the way, odds are that you have a signal handler installed for SIGINT and the arguments aren't right somehow.

Community
  • 1
  • 1
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 2
    You can also use symbolic names for the signals, like "signal SIGINT". – sth Feb 16 '09 at 18:10
  • 1
    it was a little non obvious to me - we need to drop into gdb prompt (say by ctrl+c itself) followed by `signal SIGINT` would resume and send signal to program. – catholicon Apr 11 '20 at 15:22
27

An alternative is to stop gdb from catching the SIGINT by typing handle SIGINT noprint pass at the gdb prompt before running the program.

ruds
  • 786
  • 4
  • 8
  • 1
    Well, yeah, but sometimes being able to interrupt gdb is handy. – Charlie Martin Oct 25 '12 at 22:46
  • This should be the accepted answer being preferable by impacting program timing and therefore behaviour the least, plus it actually answers the op's question. – Nick May 13 '20 at 12:47
7

One option is to load the core file produced when not running in the debugger into gdb. From within gdb, type core-file [filename].

Scottie T
  • 11,729
  • 10
  • 45
  • 59
3

You can also send the CTRL-C (aka SIGINT) from another terminal: kill -INT

Mikeage
  • 6,424
  • 4
  • 36
  • 54