1sigfpe(3C) Standard C Library Functions sigfpe(3C)
2
3
4
6 sigfpe - signal handling for specific SIGFPE codes
7
9 #include <floatingpoint.h>
10 #include <siginfo.h>
11
12 sigfpe_handler_type sigfpe(sigfpe_code_type code,
13 sigfpe_handler_type hdl);
14
15
17 The sigfpe() function allows signal handling to be specified for par‐
18 ticular SIGFPE codes. A call to sigfpe() defines a new handler hdl for
19 a particular SIGFPE code and returns the old handler as the value of
20 the function sigfpe(). Normally handlers are specified as pointers to
21 functions; the special cases SIGFPE_IGNORE, SIGFPE_ABORT, and
22 SIGFPE_DEFAULT allow ignoring, dumping core using abort(3C), or default
23 handling respectively. Default handling is to dump core using
24 abort(3C).
25
26
27 The code argument is usually one of the five IEEE754-related SIGFPE
28 codes:
29
30 FPE_FLTRES fp_inexact − floating-point inexact result
31 FPE_FLTDIV fp_division − floating-point division by zero
32 FPE_FLTUND fp_underflow − floating-point underflow
33 FPE_FLTOVF fp_overflow − floating-point overflow
34 FPE_FLTINV fp_invalid − floating-point invalid operation
35
36
37
38 Three steps are required to intercept an IEEE754-related SIGFPE code
39 with sigfpe():
40
41 1. Set up a handler with sigfpe().
42
43 2. Enable the relevant IEEE754 trapping capability in the hard‐
44 ware, perhaps by using assembly-language instructions.
45
46 3. Perform a floating-point operation that generates the
47 intended IEEE754 exception.
48
49
50 The sigfpe() function never changes floating-point hardware mode bits
51 affecting IEEE754 trapping. No IEEE754-related SIGFPE signals will be
52 generated unless those hardware mode bits are enabled.
53
54
55 SIGFPE signals can be handled using sigfpe(), sigaction(2) or sig‐
56 nal(3C). In a particular program, to avoid confusion, use only one of
57 these interfaces to handle SIGFPE signals.
58
60 Example 1 Example Of A User-Specified Signal Handler
61
62
63 A user-specified signal handler might look like this:
64
65
66 #include <floatingpoint.h>
67 #include <siginfo.h>
68 #include <ucontext.h>
69 /*
70 * The sample_handler prints out a message then commits suicide.
71 */
72 void
73 sample_handler(int sig, siginfo_t *sip, ucontext_t *uap) {
74 char *label;
75 switch (sip−>si_code) {
76 case FPE_FLTINV: label = "invalid operand"; break;
77 case FPE_FLTRES: label = "inexact"; break;
78 case FPE_FLTDIV: label = "division-by-zero"; break;
79 case FPE_FLTUND: label = "underflow"; break;
80 case FPE_FLTOVF: label = "overflow"; break;
81 default: label = "???"; break;
82 }
83 fprintf(stderr,
84 "FP exception %s (0x%x) occurred at address %p.\n",
85 label, sip−>si_code, (void *) sip−>si_addr);
86 abort();
87 }
88
89
90
91 and it might be set up like this:
92
93
94 #include <floatingpoint.h>
95 #include <siginfo.h>
96 #include <ucontext.h>
97 extern void sample_handler(int, siginfo_t *, ucontext_t *);
98 main(void) {
99 sigfpe_handler_type hdl, old_handler1, old_handler2;
100 /*
101 * save current fp_overflow and fp_invalid handlers; set the new
102 * fp_overflow handler to sample_handler() and set the new
103 * fp_invalid handler to SIGFPE_ABORT (abort on invalid)
104 */
105 hdl = (sigfpe_handler_type) sample_handler;
106 old_handler1 = sigfpe(FPE_FLTOVF, hdl);
107 old_handler2 = sigfpe(FPE_FLTINV, SIGFPE_ABORT);
108 ...
109 /*
110 * restore old fp_overflow and fp_invalid handlers
111 */
112 sigfpe(FPE_FLTOVF, old_handler1);
113 sigfpe(FPE_FLTINV, old_handler2);
114 }
115
116
118 /usr/include/floatingpoint.h
119
120
121
122
123 /usr/include/siginfo.h
124
125
126
127
129 See attributes(5) for descriptions of the following attributes:
130
131
132
133
134 ┌─────────────────────────────┬─────────────────────────────┐
135 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
136 ├─────────────────────────────┼─────────────────────────────┤
137 │MT-Level │Safe │
138 └─────────────────────────────┴─────────────────────────────┘
139
141 sigaction(2), abort(3C), signal(3C), attributes(5), floating‐
142 point.h(3HEAD)
143
145 The sigfpe() function returns (void(*)())-1 if code is not zero or a
146 defined SIGFPE code.
147
148
149
150SunOS 5.11 4 May 2004 sigfpe(3C)