Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a segmentation fault when I do ctrl c

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.

like image 822
Eduardo Avatar asked Feb 16 '09 17:02

Eduardo


People also ask

Why do I keep getting segmentation fault in C?

In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).

How can segmentation fault be resolved?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

What does segmentation fault mean in C?

A common run-time error for C programs by beginners is a "segmentation violation" or "segmentation fault." When you run your program and the system reports a "segmentation violation," it means your program has attempted to access an area of memory that it is not allowed to access.


2 Answers

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.

like image 99
Charlie Martin Avatar answered Sep 17 '22 02:09

Charlie Martin


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

like image 30
ruds Avatar answered Sep 18 '22 02:09

ruds