Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/fex_log.c
+++ new/usr/src/lib/libm/common/m9x/fex_log.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #pragma weak fex_get_log = __fex_get_log
31 31 #pragma weak fex_set_log = __fex_set_log
32 32 #pragma weak fex_get_log_depth = __fex_get_log_depth
33 33 #pragma weak fex_set_log_depth = __fex_set_log_depth
34 34 #pragma weak fex_log_entry = __fex_log_entry
35 35
36 36 #include "fenv_synonyms.h"
37 37 #include <stdio.h>
38 38 #include <stdlib.h>
39 39 #include <unistd.h>
40 40 #include <string.h>
41 41 #include <signal.h>
42 42 #include <ucontext.h>
43 43 #include <sys/frame.h>
44 44 #include <fenv.h>
45 45 #include <sys/ieeefp.h>
46 46 #include <thread.h>
47 47 #include "fex_handler.h"
48 48
49 49 #if !defined(PC)
50 50 #if defined(REG_PC)
51 51 #define PC REG_PC
52 52 #else
53 53 #error Neither PC nor REG_PC is defined!
54 54 #endif
55 55 #endif
56 56
57 57 static FILE *log_fp = NULL;
58 58 static mutex_t log_lock = DEFAULTMUTEX;
59 59 static int log_depth = 100;
60 60
61 61 FILE *fex_get_log(void)
62 62 {
63 63 FILE *fp;
64 64
65 65 mutex_lock(&log_lock);
66 66 fp = log_fp;
67 67 mutex_unlock(&log_lock);
68 68 return fp;
69 69 }
70 70
71 71 int fex_set_log(FILE *fp)
72 72 {
73 73 mutex_lock(&log_lock);
74 74 log_fp = fp;
75 75 mutex_unlock(&log_lock);
76 76 __fex_update_te();
77 77 return 1;
78 78 }
79 79
80 80 int fex_get_log_depth(void)
81 81 {
82 82 int d;
83 83
84 84 mutex_lock(&log_lock);
85 85 d = log_depth;
86 86 mutex_unlock(&log_lock);
87 87 return d;
88 88 }
89 89
90 90 int fex_set_log_depth(int d)
91 91 {
92 92 if (d < 0)
93 93 return 0;
94 94 mutex_lock(&log_lock);
95 95 log_depth = d;
96 96 mutex_unlock(&log_lock);
97 97 return 1;
98 98 }
99 99
100 100 static struct exc_list {
101 101 struct exc_list *next;
102 102 char *addr;
103 103 unsigned long code;
104 104 int nstack;
105 105 char *stack[1]; /* actual length is max(1,nstack) */
106 106 } *list = NULL;
107 107
108 108 #ifdef __sparcv9
109 109 #define FRAMEP(X) (struct frame *)((char*)(X)+(((long)(X)&1)?2047:0))
110 110 #else
111 111 #define FRAMEP(X) (struct frame *)(X)
112 112 #endif
113 113
114 114 #ifdef _LP64
↓ open down ↓ |
114 lines elided |
↑ open up ↑ |
115 115 #define PDIG "16"
116 116 #else
117 117 #define PDIG "8"
118 118 #endif
119 119
120 120 /* look for a matching exc_list; return 1 if one is found,
121 121 otherwise add this one to the list and return 0 */
122 122 static int check_exc_list(char *addr, unsigned long code, char *stk,
123 123 struct frame *fp)
124 124 {
125 - struct exc_list *l, *ll;
125 + struct exc_list *l, *ll = NULL;
126 126 struct frame *f;
127 127 int i, n;
128 128
129 129 if (list) {
130 130 for (l = list; l; ll = l, l = l->next) {
131 131 if (l->addr != addr || l->code != code)
132 132 continue;
133 133 if (log_depth < 1 || l->nstack < 1)
134 134 return 1;
135 135 if (l->stack[0] != stk)
136 136 continue;
137 137 n = 1;
138 138 for (i = 1, f = fp; i < log_depth && i < l->nstack &&
139 139 f && f->fr_savpc; i++, f = FRAMEP(f->fr_savfp))
140 140 if (l->stack[i] != (char *)f->fr_savpc) {
141 141 n = 0;
142 142 break;
143 143 }
144 144 if (n)
145 145 return 1;
146 146 }
147 147 }
148 148
149 149 /* create a new exc_list structure and tack it on the list */
150 150 for (n = 1, f = fp; n < log_depth && f && f->fr_savpc;
151 151 n++, f = FRAMEP(f->fr_savfp)) ;
152 152 if ((l = (struct exc_list *)malloc(sizeof(struct exc_list) +
153 153 (n - 1) * sizeof(char *))) != NULL) {
154 154 l->next = NULL;
155 155 l->addr = addr;
156 156 l->code = code;
157 157 l->nstack = ((log_depth < 1)? 0 : n);
158 158 l->stack[0] = stk;
159 159 for (i = 1; i < n; i++) {
160 160 l->stack[i] = (char *)fp->fr_savpc;
161 161 fp = FRAMEP(fp->fr_savfp);
162 162 }
163 163 if (list)
164 164 ll->next = l;
165 165 else
166 166 list = l;
167 167 }
168 168 return 0;
169 169 }
170 170
171 171 /*
172 172 * Warning: cleverness ahead
173 173 *
174 174 * In the following code, the use of sprintf+write rather than fprintf
175 175 * to send output to the log file is intentional. The reason is that
176 176 * fprintf is not async-signal-safe. "But," you protest, "SIGFPE is
177 177 * not an asynchronous signal! It's always handled by the same thread
178 178 * that executed the fpop that provoked it." That's true, but a prob-
179 179 * lem arises because (i) base conversion in fprintf can cause a fp
180 180 * exception and (ii) my signal handler acquires a mutex lock before
181 181 * sending output to the log file (so that outputs for entries from
182 182 * different threads aren't interspersed). Therefore, if the code
183 183 * were to use fprintf, a deadlock could occur as follows:
184 184 *
185 185 * Thread A Thread B
186 186 *
187 187 * Incurs a fp exception, Calls fprintf,
188 188 * acquires log_lock acquires file rmutex lock
189 189 *
190 190 * Calls fprintf, Incurs a fp exception,
191 191 * waits for file rmutex lock waits for log_lock
192 192 *
193 193 * (I could just verify that fprintf doesn't hold the rmutex lock while
194 194 * it's doing the base conversion, but since efficiency is of little
195 195 * concern here, I opted for the safe and dumb route.)
196 196 */
197 197
198 198 static void print_stack(int fd, char *addr, struct frame *fp)
199 199 {
200 200 int i;
201 201 char *name, buf[30];
202 202
203 203 for (i = 0; i < log_depth && addr != NULL; i++) {
204 204 if (__fex_sym(addr, &name) != NULL) {
205 205 write(fd, buf, sprintf(buf, " 0x%0" PDIG "lx ",
206 206 (long)addr));
207 207 write(fd, name, strlen(name));
208 208 write(fd, "\n", 1);
209 209 if (!strcmp(name, "main"))
210 210 break;
211 211 } else {
212 212 write(fd, buf, sprintf(buf, " 0x%0" PDIG "lx\n",
213 213 (long)addr));
214 214 }
215 215 if (fp == NULL)
216 216 break;
217 217 addr = (char *)fp->fr_savpc;
218 218 fp = FRAMEP(fp->fr_savfp);
219 219 }
220 220 }
221 221
222 222 void fex_log_entry(const char *msg)
223 223 {
224 224 ucontext_t uc;
225 225 struct frame *fp;
226 226 char *stk;
227 227 int fd;
228 228
229 229 /* if logging is disabled, just return */
230 230 mutex_lock(&log_lock);
231 231 if (log_fp == NULL) {
232 232 mutex_unlock(&log_lock);
233 233 return;
234 234 }
235 235
236 236 /* get the frame pointer from the current context and
237 237 pop our own frame */
238 238 getcontext(&uc);
239 239 #if defined(__sparc) || defined(__amd64)
240 240 fp = FRAMEP(uc.uc_mcontext.gregs[REG_SP]);
241 241 #elif defined(__i386) /* !defined(__amd64) */
242 242 fp = FRAMEP(uc.uc_mcontext.gregs[EBP]);
243 243 #else
244 244 #error Unknown architecture
245 245 #endif
246 246 if (fp == NULL) {
247 247 mutex_unlock(&log_lock);
248 248 return;
249 249 }
250 250 stk = (char *)fp->fr_savpc;
251 251 fp = FRAMEP(fp->fr_savfp);
252 252
253 253 /* if we've already logged this message here, don't make an entry */
254 254 if (check_exc_list(stk, (unsigned long)msg, stk, fp)) {
255 255 mutex_unlock(&log_lock);
256 256 return;
257 257 }
258 258
259 259 /* make an entry */
260 260 fd = fileno(log_fp);
261 261 write(fd, "fex_log_entry: ", 15);
262 262 write(fd, msg, strlen(msg));
263 263 write(fd, "\n", 1);
264 264 __fex_sym_init();
265 265 print_stack(fd, stk, fp);
266 266 mutex_unlock(&log_lock);
267 267 }
268 268
269 269 static const char *exception[FEX_NUM_EXC] = {
270 270 "inexact result",
271 271 "division by zero",
272 272 "underflow",
273 273 "overflow",
274 274 "invalid operation (0/0)",
275 275 "invalid operation (inf/inf)",
276 276 "invalid operation (inf-inf)",
277 277 "invalid operation (0*inf)",
278 278 "invalid operation (sqrt)",
279 279 "invalid operation (snan)",
280 280 "invalid operation (int)",
281 281 "invalid operation (cmp)"
282 282 };
283 283
284 284 void
285 285 __fex_mklog(ucontext_t *uap, char *addr, int f, enum fex_exception e,
286 286 int m, void *p)
287 287 {
288 288 struct frame *fp;
289 289 char *stk, *name, buf[30];
290 290 int fd;
291 291
292 292 /* if logging is disabled, just return */
293 293 mutex_lock(&log_lock);
294 294 if (log_fp == NULL) {
295 295 mutex_unlock(&log_lock);
296 296 return;
297 297 }
298 298
299 299 /* get stack info */
300 300 #if defined(__sparc)
301 301 stk = (char*)uap->uc_mcontext.gregs[REG_PC];
302 302 fp = FRAMEP(uap->uc_mcontext.gregs[REG_SP]);
303 303 #elif defined(__amd64)
304 304 stk = (char*)uap->uc_mcontext.gregs[REG_PC];
305 305 fp = FRAMEP(uap->uc_mcontext.gregs[REG_RBP]);
306 306 #elif defined(__i386) /* !defined(__amd64) */
307 307 stk = (char*)uap->uc_mcontext.gregs[PC];
308 308 fp = FRAMEP(uap->uc_mcontext.gregs[EBP]);
309 309 #else
310 310 #error Unknown architecture
311 311 #endif
312 312
313 313 /* if the handling mode is the default and this exception's
314 314 flag is already raised, don't make an entry */
315 315 if (m == FEX_NONSTOP) {
316 316 switch (e) {
317 317 case fex_inexact:
318 318 if (f & FE_INEXACT) {
319 319 mutex_unlock(&log_lock);
320 320 return;
321 321 }
322 322 break;
323 323 case fex_underflow:
324 324 if (f & FE_UNDERFLOW) {
325 325 mutex_unlock(&log_lock);
326 326 return;
327 327 }
328 328 break;
329 329 case fex_overflow:
330 330 if (f & FE_OVERFLOW) {
331 331 mutex_unlock(&log_lock);
332 332 return;
333 333 }
334 334 break;
335 335 case fex_division:
336 336 if (f & FE_DIVBYZERO) {
337 337 mutex_unlock(&log_lock);
338 338 return;
339 339 }
340 340 break;
341 341 default:
342 342 if (f & FE_INVALID) {
343 343 mutex_unlock(&log_lock);
344 344 return;
345 345 }
346 346 break;
347 347 }
348 348 }
349 349
350 350 /* if we've already logged this exception at this address,
351 351 don't make an entry */
352 352 if (check_exc_list(addr, (unsigned long)e, stk, fp)) {
353 353 mutex_unlock(&log_lock);
354 354 return;
355 355 }
356 356
357 357 /* make an entry */
358 358 fd = fileno(log_fp);
359 359 write(fd, "Floating point ", 15);
360 360 write(fd, exception[e], strlen(exception[e]));
361 361 write(fd, buf, sprintf(buf, " at 0x%0" PDIG "lx", (long)addr));
362 362 __fex_sym_init();
363 363 if (__fex_sym(addr, &name) != NULL) {
364 364 write(fd, " ", 1);
365 365 write(fd, name, strlen(name));
366 366 }
367 367 switch (m) {
368 368 case FEX_NONSTOP:
369 369 write(fd, ", nonstop mode\n", 15);
370 370 break;
371 371
372 372 case FEX_ABORT:
373 373 write(fd, ", abort\n", 8);
374 374 break;
375 375
376 376 case FEX_NOHANDLER:
377 377 if (p == (void *)SIG_DFL) {
378 378 write(fd, ", handler: SIG_DFL\n", 19);
379 379 break;
380 380 }
381 381 else if (p == (void *)SIG_IGN) {
382 382 write(fd, ", handler: SIG_IGN\n", 19);
383 383 break;
384 384 }
385 385 /* fall through*/
386 386 default:
387 387 write(fd, ", handler: ", 11);
388 388 if (__fex_sym((char *)p, &name) != NULL) {
389 389 write(fd, name, strlen(name));
390 390 write(fd, "\n", 1);
391 391 } else {
392 392 write(fd, buf, sprintf(buf, "0x%0" PDIG "lx\n",
393 393 (long)p));
394 394 }
395 395 break;
396 396 }
397 397 print_stack(fd, stk, fp);
398 398 mutex_unlock(&log_lock);
399 399 }
↓ open down ↓ |
264 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX