Re: catching SIGFPE signal
- From: scott@xxxxxxxxxxxxx (Scott Lurndal)
- Date: Mon, 28 Jan 2008 20:12:11 GMT
"subramanian100in@xxxxxxxxx, India" <subramanian100in@xxxxxxxxx> writes:
I am using Redhat Enterprise Linux kernel 2.6.9 and gcc 3.4.3
consider the following program:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void signal_handler_SIGFPE(int signum)
{
printf("SIGFPE caught: %d\n", signum);
return;
}
The output line "SIGFPE caught: 8" corresponds to the
signal_handler_SIGFPE() function. But after executing this handler,
control does not return to main(); but instead prints "Floating point
exception" and terminates the program.
Why doesn't the control go back to main() and continue processing ?
Does it mean that SIGFPE cannot be caught ?
The signal handler is called with two additional parameters in
addition to the signal number. The second parameter is a pointer
to a siginfo_t structure and the third parameter describes the
state of the thread at the time of the exception (ucontext_t
datatype, although POSIX lists it as void *).
The state at the time of the exception is recorded into the
ucontext_t structure (e.g. instruction pointer, general registers,
etc). When you return from the signal handler, the state will
be restored and execution will continue.
For SIGFPE, the state recorded in the ucontext_t structure will
include the starting address of the divide instruction that
generates your SIGFPE. When you return from the signal handler,
that instruction will be re-executed, and as you didn't re-install
the signal handler, your application terminates. If you had reinstalled
your signal handler, or used sigaction(2) instead of signal(2), you'd
find your application in an infinite loop.
The possibilities for recovery are:
1) Interpret the instruction that failed and fixup its arguments
such that when returning from the signal handler the divide won't fail, or
2) Bump the 'next instruction' address in the ucontext_t to the
next instruction and skip the div (although this will cause
unspecified bad things to happen because the div didn't happen), or
3) Use longjmp/siglongjmp to return to a known good state.
4) Abort the application.
Only (3) and (4) are viable choices in my opinion, particularly if
portability is desired.
scott
.
- References:
- catching SIGFPE signal
- From: subramanian100in@xxxxxxxxx, India
- catching SIGFPE signal
- Prev by Date: Re: Replacing read() by a debugging function
- Next by Date: Re: Replacing read() by a debugging function
- Previous by thread: Re: catching SIGFPE signal
- Index(es):
Relevant Pages
|
|