1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
'\" te
.\" Copyright (c) 1996, Sun Microsystems, Inc.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH SIGFPE 3C "May 4, 2004"
.SH NAME
sigfpe \- signal handling for specific SIGFPE codes
.SH SYNOPSIS
.LP
.nf
#include <floatingpoint.h>
#include <siginfo.h>
\fBsigfpe_handler_type\fR \fBsigfpe\fR(\fBsigfpe_code_type\fR \fIcode\fR,
\fBsigfpe_handler_type\fR \fIhdl\fR);
.fi
.SH DESCRIPTION
.LP
The \fBsigfpe()\fR function allows signal handling to be specified for
particular \fBSIGFPE\fR codes. A call to \fBsigfpe()\fR defines a new handler
\fIhdl\fR for a particular \fBSIGFPE\fR \fIcode\fR and returns the old handler
as the value of the function \fBsigfpe()\fR. Normally handlers are specified as
pointers to functions; the special cases \fBSIGFPE_IGNORE\fR,
\fBSIGFPE_ABORT\fR, and \fBSIGFPE_DEFAULT\fR allow ignoring, dumping core using
\fBabort\fR(3C), or default handling respectively. Default handling is to dump
core using \fBabort\fR(3C).
.sp
.LP
The \fIcode\fR argument is usually one of the five \fBIEEE\|754-related\fR
\fBSIGFPE\fR codes:
.sp
.in +2
.nf
FPE_FLTRES fp_inexact \(mi floating-point inexact result
FPE_FLTDIV fp_division \(mi floating-point division by zero
FPE_FLTUND fp_underflow \(mi floating-point underflow
FPE_FLTOVF fp_overflow \(mi floating-point overflow
FPE_FLTINV fp_invalid \(mi floating-point invalid operation
.fi
.in -2
.LP
And additionally on the x86 architecture:
.sp
.in +2
.nf
FPE_FLTDEN fp_denormalized \(mi floating-point denormalized result
.fi
.in -2
.sp
.LP
Three steps are required to intercept an \fBIEEE\|754-related\fR \fBSIGFPE\fR
code with \fBsigfpe()\fR:
.RS +4
.TP
1.
Set up a handler with \fBsigfpe()\fR.
.RE
.RS +4
.TP
2.
Enable the relevant \fBIEEE\|754\fR trapping capability in the hardware,
perhaps by using assembly-language instructions.
.RE
.RS +4
.TP
3.
Perform a floating-point operation that generates the intended
\fBIEEE\|754\fR exception.
.RE
.sp
.LP
The \fBsigfpe()\fR function never changes floating-point hardware mode bits
affecting \fBIEEE\|754\fR trapping. No \fBIEEE\|754-related\fR \fBSIGFPE\fR
signals will be generated unless those hardware mode bits are enabled.
.sp
.LP
\fBSIGFPE\fR signals can be handled using \fBsigfpe()\fR, \fBsigaction\fR(2) or
\fBsignal\fR(3C). In a particular program, to avoid confusion, use only one of
these interfaces to handle \fBSIGFPE\fR signals.
.SH EXAMPLES
.LP
\fBExample 1 \fRExample Of A User-Specified Signal Handler
.sp
.LP
A user-specified signal handler might look like this:
.sp
.in +2
.nf
#include <floatingpoint.h>
#include <siginfo.h>
#include <ucontext.h>
/*
* The sample_handler prints out a message then commits suicide.
*/
void
sample_handler(int sig, siginfo_t *sip, ucontext_t *uap) {
char *label;
switch (sip\(mi>si_code) {
case FPE_FLTINV: label = "invalid operand"; break;
case FPE_FLTRES: label = "inexact"; break;
case FPE_FLTDIV: label = "division-by-zero"; break;
case FPE_FLTUND: label = "underflow"; break;
case FPE_FLTOVF: label = "overflow"; break;
default: label = "???"; break;
}
fprintf(stderr,
"FP exception %s (0x%x) occurred at address %p.\en",
label, sip\(mi>si_code, (void *) sip\(mi>si_addr);
abort();
}
.fi
.in -2
.sp
.LP
and it might be set up like this:
.sp
.in +2
.nf
#include <floatingpoint.h>
#include <siginfo.h>
#include <ucontext.h>
extern void sample_handler(int, siginfo_t *, ucontext_t *);
main(void) {
sigfpe_handler_type hdl, old_handler1, old_handler2;
/*
* save current fp_overflow and fp_invalid handlers; set the new
* fp_overflow handler to sample_handler(\|) and set the new
* fp_invalid handler to SIGFPE_ABORT (abort on invalid)
*/
hdl = (sigfpe_handler_type) sample_handler;
old_handler1 = sigfpe(FPE_FLTOVF, hdl);
old_handler2 = sigfpe(FPE_FLTINV, SIGFPE_ABORT);
.\|.\|.
/*
* restore old fp_overflow and fp_invalid handlers
*/
sigfpe(FPE_FLTOVF, old_handler1);
sigfpe(FPE_FLTINV, old_handler2);
}
.fi
.in -2
.SH FILES
.ne 2
.na
\fB\fB/usr/include/floatingpoint.h\fR\fR
.ad
.sp .6
.RS 4n
.RE
.sp
.ne 2
.na
\fB\fB/usr/include/siginfo.h\fR\fR
.ad
.sp .6
.RS 4n
.RE
.SH ATTRIBUTES
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
MT-Level Safe
.TE
.SH SEE ALSO
.LP
\fBsigaction\fR(2), \fBabort\fR(3C), \fBsignal\fR(3C), \fBattributes\fR(5),
\fBfloatingpoint.h\fR(3HEAD)
.SH DIAGNOSTICS
.LP
The \fBsigfpe()\fR function returns (void(*)())-1 if \fIcode\fR is not zero or
a defined \fBSIGFPE\fR code.
|