| 1 |
#include "faulthandler.h" |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <signal.h> |
| 5 |
#include <assert.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <string.h> |
| 8 |
#ifndef _WIN32 |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/resource.h> |
| 11 |
#endif |
| 12 |
#include <math.h> |
| 13 |
|
| 14 |
#ifdef RPYTHON_LL2CTYPES |
| 15 |
# include "../../../rpython/rlib/rvmprof/src/rvmprof.h" |
| 16 |
#else |
| 17 |
# include "common_header.h" |
| 18 |
# include "structdef.h" |
| 19 |
# include "rvmprof.h" |
| 20 |
#endif |
| 21 |
#include "src/threadlocal.h" |
| 22 |
|
| 23 |
#define MAX_FRAME_DEPTH 100 |
| 24 |
#define FRAME_DEPTH_N RVMPROF_TRACEBACK_ESTIMATE_N(MAX_FRAME_DEPTH) |
| 25 |
|
| 26 |
#ifndef _WIN32 |
| 27 |
#define HAVE_SIGACTION 1 |
| 28 |
#define HAVE_SIGALTSTACK 1 |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef HAVE_SIGACTION |
| 32 |
typedef struct sigaction _Py_sighandler_t; |
| 33 |
#else |
| 34 |
typedef void (*_Py_sighandler_t)(int); |
| 35 |
#endif |
| 36 |
|
| 37 |
typedef struct { |
| 38 |
const int signum; |
| 39 |
volatile int enabled; |
| 40 |
const char* name; |
| 41 |
_Py_sighandler_t previous; |
| 42 |
} fault_handler_t; |
| 43 |
|
| 44 |
static struct { |
| 45 |
int initialized; |
| 46 |
int enabled; |
| 47 |
volatile int fd, all_threads; |
| 48 |
volatile pypy_faulthandler_cb_t dump_traceback; |
| 49 |
} fatal_error; |
| 50 |
|
| 51 |
#ifdef HAVE_SIGALTSTACK |
| 52 |
static stack_t stack; |
| 53 |
#endif |
| 54 |
|
| 55 |
|
| 56 |
static fault_handler_t faulthandler_handlers[] = { |
| 57 |
#ifdef SIGBUS |
| 58 |
{SIGBUS, 0, "Bus error", }, |
| 59 |
#endif |
| 60 |
#ifdef SIGILL |
| 61 |
{SIGILL, 0, "Illegal instruction", }, |
| 62 |
#endif |
| 63 |
{SIGFPE, 0, "Floating point exception", }, |
| 64 |
{SIGABRT, 0, "Aborted", }, |
| 65 |
|
| 66 |
|
| 67 |
{SIGSEGV, 0, "Segmentation fault", } |
| 68 |
}; |
| 69 |
static const int faulthandler_nsignals = |
| 70 |
sizeof(faulthandler_handlers) / sizeof(fault_handler_t); |
| 71 |
|
| 72 |
RPY_EXTERN |
| 73 |
void pypy_faulthandler_write(int fd, const char *str) |
| 74 |
{ |
| 75 |
ssize_t n, count; |
| 76 |
count = 0; |
| 77 |
while (str[count] != 0) |
| 78 |
count++; |
| 79 |
|
| 80 |
while (count > 0) { |
| 81 |
n = write(fd, str, count); |
| 82 |
if (n < 0) { |
| 83 |
if (errno != EINTR) |
| 84 |
return; |
| 85 |
n = 0; |
| 86 |
} |
| 87 |
str += n; |
| 88 |
count -= n; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
RPY_EXTERN |
| 93 |
void pypy_faulthandler_write_uint(int fd, unsigned long uvalue, int min_digits) |
| 94 |
{ |
| 95 |
char buf[48], *p = buf + 48; |
| 96 |
*--p = 0; |
| 97 |
while (uvalue || min_digits > 0) { |
| 98 |
assert(p > buf); |
| 99 |
*--p = '0' + (uvalue % 10UL); |
| 100 |
uvalue /= 10UL; |
| 101 |
min_digits--; |
| 102 |
} |
| 103 |
|
| 104 |
pypy_faulthandler_write(fd, p); |
| 105 |
} |
| 106 |
|
| 107 |
static void pypy_faulthandler_write_hex(int fd, Unsigned uvalue) |
| 108 |
{ |
| 109 |
char buf[48], *p = buf + 48; |
| 110 |
*--p = 0; |
| 111 |
do { |
| 112 |
Unsigned byte = uvalue % 16UL; |
| 113 |
assert(p > buf); |
| 114 |
if (byte < 10) |
| 115 |
*--p = '0' + byte; |
| 116 |
else |
| 117 |
*--p = 'A' + byte - 10; |
| 118 |
uvalue /= 16UL; |
| 119 |
} while (uvalue > 0UL); |
| 120 |
|
| 121 |
pypy_faulthandler_write(fd, p); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
RPY_EXTERN |
| 126 |
void pypy_faulthandler_dump_traceback(int fd, int all_threads, |
| 127 |
void *ucontext) |
| 128 |
{ |
| 129 |
pypy_faulthandler_cb_t fn; |
| 130 |
intptr_t array_p[FRAME_DEPTH_N], array_length; |
| 131 |
|
| 132 |
fn = fatal_error.dump_traceback; |
| 133 |
if (!fn) |
| 134 |
return; |
| 135 |
|
| 136 |
#ifndef RPYTHON_LL2CTYPES |
| 137 |
if (all_threads && _RPython_ThreadLocals_AcquireTimeout(10000) == 0) { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
struct pypy_threadlocal_s *my, *p; |
| 143 |
int blankline = 0; |
| 144 |
|
| 145 |
my = (struct pypy_threadlocal_s *)_RPy_ThreadLocals_Get(); |
| 146 |
p = _RPython_ThreadLocals_Head(); |
| 147 |
p = _RPython_ThreadLocals_Enum(p); |
| 148 |
while (p != NULL) { |
| 149 |
if (blankline) |
| 150 |
pypy_faulthandler_write(fd, "\n"); |
| 151 |
blankline = 1; |
| 152 |
|
| 153 |
pypy_faulthandler_write(fd, my == p ? "Current thread 0x" |
| 154 |
: "Thread 0x"); |
| 155 |
pypy_faulthandler_write_hex(fd, (Unsigned)p->thread_ident); |
| 156 |
pypy_faulthandler_write(fd, " (most recent call first," |
| 157 |
" approximate line numbers):\n"); |
| 158 |
|
| 159 |
array_length = vmprof_get_traceback(p->vmprof_tl_stack, |
| 160 |
my == p ? ucontext : NULL, |
| 161 |
(void **)array_p, FRAME_DEPTH_N); |
| 162 |
fn(fd, array_p, array_length); |
| 163 |
|
| 164 |
p = _RPython_ThreadLocals_Enum(p); |
| 165 |
} |
| 166 |
_RPython_ThreadLocals_Release(); |
| 167 |
} |
| 168 |
else { |
| 169 |
pypy_faulthandler_write(fd, "Stack (most recent call first," |
| 170 |
" approximate line numbers):\n"); |
| 171 |
array_length = vmprof_get_traceback(NULL, ucontext, |
| 172 |
(void **)array_p, FRAME_DEPTH_N); |
| 173 |
fn(fd, array_p, array_length); |
| 174 |
} |
| 175 |
#else |
| 176 |
pypy_faulthandler_write(fd, "(no traceback when untranslated)\n"); |
| 177 |
#endif |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
faulthandler_dump_traceback(int fd, int all_threads, void *ucontext) |
| 182 |
{ |
| 183 |
static volatile int reentrant = 0; |
| 184 |
|
| 185 |
if (reentrant) |
| 186 |
return; |
| 187 |
reentrant = 1; |
| 188 |
pypy_faulthandler_dump_traceback(fd, all_threads, ucontext); |
| 189 |
reentrant = 0; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#ifdef PYPY_FAULTHANDLER_LATER |
| 197 |
#include "src/thread.h" |
| 198 |
static struct { |
| 199 |
int fd; |
| 200 |
long long microseconds; |
| 201 |
int repeat, exit; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
struct RPyOpaque_ThreadLock cancel_event; |
| 206 |
|
| 207 |
struct RPyOpaque_ThreadLock running; |
| 208 |
} thread_later; |
| 209 |
|
| 210 |
static void faulthandler_thread(void) |
| 211 |
{ |
| 212 |
#ifndef _WIN32 |
| 213 |
|
| 214 |
sigset_t set; |
| 215 |
sigfillset(&set); |
| 216 |
pthread_sigmask(SIG_SETMASK, &set, NULL); |
| 217 |
#endif |
| 218 |
|
| 219 |
RPyLockStatus st; |
| 220 |
unsigned long hours, minutes, seconds, fraction; |
| 221 |
long long t; |
| 222 |
int fd; |
| 223 |
|
| 224 |
do { |
| 225 |
st = RPyThreadAcquireLockTimed(&thread_later.cancel_event, |
| 226 |
thread_later.microseconds, 0); |
| 227 |
if (st == RPY_LOCK_ACQUIRED) { |
| 228 |
RPyThreadReleaseLock(&thread_later.cancel_event); |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
assert(st == RPY_LOCK_FAILURE); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
t = thread_later.microseconds; |
| 238 |
fraction = (unsigned long)(t % 1000000); |
| 239 |
t /= 1000000; |
| 240 |
seconds = (unsigned long)(t % 60); |
| 241 |
t /= 60; |
| 242 |
minutes = (unsigned long)(t % 60); |
| 243 |
t /= 60; |
| 244 |
hours = (unsigned long)t; |
| 245 |
|
| 246 |
fd = thread_later.fd; |
| 247 |
pypy_faulthandler_write(fd, "Timeout ("); |
| 248 |
pypy_faulthandler_write_uint(fd, hours, 1); |
| 249 |
pypy_faulthandler_write(fd, ":"); |
| 250 |
pypy_faulthandler_write_uint(fd, minutes, 2); |
| 251 |
pypy_faulthandler_write(fd, ":"); |
| 252 |
pypy_faulthandler_write_uint(fd, seconds, 2); |
| 253 |
if (fraction != 0) { |
| 254 |
pypy_faulthandler_write(fd, "."); |
| 255 |
pypy_faulthandler_write_uint(fd, fraction, 6); |
| 256 |
} |
| 257 |
pypy_faulthandler_write(fd, ")!\n"); |
| 258 |
pypy_faulthandler_dump_traceback(fd, 1, NULL); |
| 259 |
|
| 260 |
if (thread_later.exit) |
| 261 |
_exit(1); |
| 262 |
} while (thread_later.repeat); |
| 263 |
|
| 264 |
|
| 265 |
RPyThreadReleaseLock(&thread_later.running); |
| 266 |
} |
| 267 |
|
| 268 |
RPY_EXTERN |
| 269 |
char *pypy_faulthandler_dump_traceback_later(long long microseconds, int repeat, |
| 270 |
int fd, int exit) |
| 271 |
{ |
| 272 |
pypy_faulthandler_cancel_dump_traceback_later(); |
| 273 |
|
| 274 |
thread_later.fd = fd; |
| 275 |
thread_later.microseconds = microseconds; |
| 276 |
thread_later.repeat = repeat; |
| 277 |
thread_later.exit = exit; |
| 278 |
|
| 279 |
RPyThreadAcquireLock(&thread_later.running, 1); |
| 280 |
|
| 281 |
if (RPyThreadStart(&faulthandler_thread) == -1) { |
| 282 |
RPyThreadReleaseLock(&thread_later.running); |
| 283 |
return "unable to start watchdog thread"; |
| 284 |
} |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
RPY_EXTERN |
| 290 |
void pypy_faulthandler_cancel_dump_traceback_later(void) |
| 291 |
{ |
| 292 |
#ifdef PYPY_FAULTHANDLER_LATER |
| 293 |
|
| 294 |
RPyThreadReleaseLock(&thread_later.cancel_event); |
| 295 |
|
| 296 |
|
| 297 |
RPyThreadAcquireLock(&thread_later.running, 1); |
| 298 |
RPyThreadReleaseLock(&thread_later.running); |
| 299 |
|
| 300 |
|
| 301 |
RPyThreadAcquireLock(&thread_later.cancel_event, 1); |
| 302 |
#endif |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
#ifdef PYPY_FAULTHANDLER_USER |
| 310 |
typedef struct { |
| 311 |
int enabled; |
| 312 |
int fd; |
| 313 |
int all_threads; |
| 314 |
int chain; |
| 315 |
_Py_sighandler_t previous; |
| 316 |
} user_signal_t; |
| 317 |
|
| 318 |
static user_signal_t *user_signals; |
| 319 |
|
| 320 |
#ifndef NSIG |
| 321 |
# if defined(_NSIG) |
| 322 |
# define NSIG _NSIG |
| 323 |
# elif defined(_SIGMAX) |
| 324 |
# define NSIG (_SIGMAX + 1) |
| 325 |
# elif defined(SIGMAX) |
| 326 |
# define NSIG (SIGMAX + 1) |
| 327 |
# else |
| 328 |
# define NSIG 64 |
| 329 |
# endif |
| 330 |
#endif |
| 331 |
|
| 332 |
#ifdef HAVE_SIGACTION |
| 333 |
static void faulthandler_user(int signum, siginfo_t *info, void *ucontext); |
| 334 |
#else |
| 335 |
static void faulthandler_user(int signum); |
| 336 |
#endif |
| 337 |
|
| 338 |
static int |
| 339 |
faulthandler_register(int signum, int chain, _Py_sighandler_t *p_previous) |
| 340 |
{ |
| 341 |
#ifdef HAVE_SIGACTION |
| 342 |
struct sigaction action; |
| 343 |
action.sa_sigaction = faulthandler_user; |
| 344 |
sigemptyset(&action.sa_mask); |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
action.sa_flags = SA_RESTART | SA_SIGINFO; |
| 349 |
if (chain) { |
| 350 |
|
| 351 |
|
| 352 |
action.sa_flags = SA_NODEFER; |
| 353 |
} |
| 354 |
if (stack.ss_sp != NULL) { |
| 355 |
|
| 356 |
|
| 357 |
action.sa_flags |= SA_ONSTACK; |
| 358 |
} |
| 359 |
return sigaction(signum, &action, p_previous); |
| 360 |
#else |
| 361 |
_Py_sighandler_t previous; |
| 362 |
previous = signal(signum, faulthandler_user); |
| 363 |
if (p_previous != NULL) |
| 364 |
*p_previous = previous; |
| 365 |
return (previous == SIG_ERR); |
| 366 |
#endif |
| 367 |
} |
| 368 |
|
| 369 |
#ifdef HAVE_SIGACTION |
| 370 |
static void faulthandler_user(int signum, siginfo_t *info, void *ucontext) |
| 371 |
{ |
| 372 |
#else |
| 373 |
static void faulthandler_user(int signum) |
| 374 |
{ |
| 375 |
void *ucontext = NULL; |
| 376 |
#endif |
| 377 |
int save_errno; |
| 378 |
user_signal_t *user = &user_signals[signum]; |
| 379 |
|
| 380 |
if (!user->enabled) |
| 381 |
return; |
| 382 |
|
| 383 |
save_errno = errno; |
| 384 |
faulthandler_dump_traceback(user->fd, user->all_threads, ucontext); |
| 385 |
|
| 386 |
#ifdef HAVE_SIGACTION |
| 387 |
if (user->chain) { |
| 388 |
(void)sigaction(signum, &user->previous, NULL); |
| 389 |
errno = save_errno; |
| 390 |
|
| 391 |
|
| 392 |
raise(signum); |
| 393 |
|
| 394 |
save_errno = errno; |
| 395 |
(void)faulthandler_register(signum, user->chain, NULL); |
| 396 |
} |
| 397 |
|
| 398 |
errno = save_errno; |
| 399 |
#else |
| 400 |
if (user->chain) { |
| 401 |
errno = save_errno; |
| 402 |
|
| 403 |
user->previous(signum); |
| 404 |
} |
| 405 |
#endif |
| 406 |
} |
| 407 |
|
| 408 |
RPY_EXTERN |
| 409 |
int pypy_faulthandler_check_signum(long signum) |
| 410 |
{ |
| 411 |
unsigned int i; |
| 412 |
|
| 413 |
for (i = 0; i < faulthandler_nsignals; i++) { |
| 414 |
if (faulthandler_handlers[i].signum == signum) { |
| 415 |
return -1; |
| 416 |
} |
| 417 |
} |
| 418 |
if (signum < 1 || NSIG <= signum) { |
| 419 |
return -2; |
| 420 |
} |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
RPY_EXTERN |
| 425 |
char *pypy_faulthandler_register(int signum, int fd, int all_threads, int chain) |
| 426 |
{ |
| 427 |
user_signal_t *user; |
| 428 |
_Py_sighandler_t previous; |
| 429 |
int err; |
| 430 |
|
| 431 |
if (user_signals == NULL) { |
| 432 |
user_signals = malloc(NSIG * sizeof(user_signal_t)); |
| 433 |
if (user_signals == NULL) |
| 434 |
return "out of memory"; |
| 435 |
memset(user_signals, 0, NSIG * sizeof(user_signal_t)); |
| 436 |
} |
| 437 |
|
| 438 |
user = &user_signals[signum]; |
| 439 |
user->fd = fd; |
| 440 |
user->all_threads = all_threads; |
| 441 |
user->chain = chain; |
| 442 |
|
| 443 |
if (!user->enabled) { |
| 444 |
err = faulthandler_register(signum, chain, &previous); |
| 445 |
if (err) |
| 446 |
return strerror(errno); |
| 447 |
|
| 448 |
user->previous = previous; |
| 449 |
user->enabled = 1; |
| 450 |
} |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
|
| 454 |
RPY_EXTERN |
| 455 |
int pypy_faulthandler_unregister(int signum) |
| 456 |
{ |
| 457 |
user_signal_t *user; |
| 458 |
|
| 459 |
if (user_signals == NULL) |
| 460 |
return 0; |
| 461 |
|
| 462 |
user = &user_signals[signum]; |
| 463 |
if (user->enabled) { |
| 464 |
user->enabled = 0; |
| 465 |
(void)sigaction(signum, &user->previous, NULL); |
| 466 |
user->fd = -1; |
| 467 |
return 1; |
| 468 |
} |
| 469 |
else |
| 470 |
return 0; |
| 471 |
} |
| 472 |
#endif |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
static void |
| 492 |
#ifdef HAVE_SIGACTION |
| 493 |
faulthandler_fatal_error(int signum, siginfo_t *info, void *ucontext) |
| 494 |
{ |
| 495 |
#else |
| 496 |
faulthandler_fatal_error(int signum) |
| 497 |
{ |
| 498 |
void *ucontext = NULL; |
| 499 |
#endif |
| 500 |
int fd = fatal_error.fd; |
| 501 |
int i; |
| 502 |
fault_handler_t *handler = NULL; |
| 503 |
int save_errno = errno; |
| 504 |
|
| 505 |
for (i = 0; i < faulthandler_nsignals; i++) { |
| 506 |
handler = &faulthandler_handlers[i]; |
| 507 |
if (handler->signum == signum) |
| 508 |
break; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
if (handler->enabled) { |
| 514 |
#ifdef HAVE_SIGACTION |
| 515 |
(void)sigaction(signum, &handler->previous, NULL); |
| 516 |
#else |
| 517 |
(void)signal(signum, handler->previous); |
| 518 |
#endif |
| 519 |
handler->enabled = 0; |
| 520 |
} |
| 521 |
|
| 522 |
pypy_faulthandler_write(fd, "Fatal Python error: "); |
| 523 |
pypy_faulthandler_write(fd, handler->name); |
| 524 |
pypy_faulthandler_write(fd, "\n\n"); |
| 525 |
|
| 526 |
faulthandler_dump_traceback(fd, fatal_error.all_threads, ucontext); |
| 527 |
|
| 528 |
errno = save_errno; |
| 529 |
#ifdef _WIN32 |
| 530 |
if (signum == SIGSEGV) { |
| 531 |
|
| 532 |
|
| 533 |
return; |
| 534 |
} |
| 535 |
#endif |
| 536 |
|
| 537 |
|
| 538 |
raise(signum); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
RPY_EXTERN |
| 543 |
char *pypy_faulthandler_setup(pypy_faulthandler_cb_t dump_callback) |
| 544 |
{ |
| 545 |
if (fatal_error.initialized) |
| 546 |
return NULL; |
| 547 |
assert(!fatal_error.enabled); |
| 548 |
fatal_error.dump_traceback = dump_callback; |
| 549 |
|
| 550 |
#ifdef HAVE_SIGALTSTACK |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
stack.ss_flags = 0; |
| 555 |
stack.ss_size = SIGSTKSZ; |
| 556 |
stack.ss_sp = malloc(stack.ss_size); |
| 557 |
if (stack.ss_sp != NULL) { |
| 558 |
int err = sigaltstack(&stack, NULL); |
| 559 |
if (err) { |
| 560 |
free(stack.ss_sp); |
| 561 |
stack.ss_sp = NULL; |
| 562 |
} |
| 563 |
} |
| 564 |
#endif |
| 565 |
|
| 566 |
#ifdef PYPY_FAULTHANDLER_LATER |
| 567 |
if (!RPyThreadLockInit(&thread_later.cancel_event) || |
| 568 |
!RPyThreadLockInit(&thread_later.running)) |
| 569 |
return "failed to initialize locks"; |
| 570 |
RPyThreadAcquireLock(&thread_later.cancel_event, 1); |
| 571 |
#endif |
| 572 |
|
| 573 |
fatal_error.fd = -1; |
| 574 |
fatal_error.initialized = 1; |
| 575 |
|
| 576 |
return NULL; |
| 577 |
} |
| 578 |
|
| 579 |
RPY_EXTERN |
| 580 |
void pypy_faulthandler_teardown(void) |
| 581 |
{ |
| 582 |
if (fatal_error.initialized) { |
| 583 |
|
| 584 |
#ifdef PYPY_FAULTHANDLER_LATER |
| 585 |
pypy_faulthandler_cancel_dump_traceback_later(); |
| 586 |
RPyThreadReleaseLock(&thread_later.cancel_event); |
| 587 |
RPyOpaqueDealloc_ThreadLock(&thread_later.running); |
| 588 |
RPyOpaqueDealloc_ThreadLock(&thread_later.cancel_event); |
| 589 |
#endif |
| 590 |
|
| 591 |
#ifdef PYPY_FAULTHANDLER_USER |
| 592 |
int signum; |
| 593 |
for (signum = 0; signum < NSIG; signum++) |
| 594 |
pypy_faulthandler_unregister(signum); |
| 595 |
|
| 596 |
|
| 597 |
#endif |
| 598 |
|
| 599 |
pypy_faulthandler_disable(); |
| 600 |
fatal_error.initialized = 0; |
| 601 |
#ifdef HAVE_SIGALTSTACK |
| 602 |
if (stack.ss_sp) { |
| 603 |
stack.ss_flags = SS_DISABLE; |
| 604 |
sigaltstack(&stack, NULL); |
| 605 |
free(stack.ss_sp); |
| 606 |
stack.ss_sp = NULL; |
| 607 |
} |
| 608 |
#endif |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
RPY_EXTERN |
| 613 |
char *pypy_faulthandler_enable(int fd, int all_threads) |
| 614 |
{ |
| 615 |
|
| 616 |
int i; |
| 617 |
fatal_error.fd = fd; |
| 618 |
fatal_error.all_threads = all_threads; |
| 619 |
|
| 620 |
if (!fatal_error.enabled) { |
| 621 |
fatal_error.enabled = 1; |
| 622 |
|
| 623 |
for (i = 0; i < faulthandler_nsignals; i++) { |
| 624 |
int err; |
| 625 |
fault_handler_t *handler = &faulthandler_handlers[i]; |
| 626 |
#ifdef HAVE_SIGACTION |
| 627 |
struct sigaction action; |
| 628 |
action.sa_sigaction = faulthandler_fatal_error; |
| 629 |
sigemptyset(&action.sa_mask); |
| 630 |
|
| 631 |
|
| 632 |
action.sa_flags = SA_NODEFER | SA_SIGINFO; |
| 633 |
if (stack.ss_sp != NULL) { |
| 634 |
|
| 635 |
|
| 636 |
action.sa_flags |= SA_ONSTACK; |
| 637 |
} |
| 638 |
err = sigaction(handler->signum, &action, &handler->previous); |
| 639 |
#else |
| 640 |
handler->previous = signal(handler->signum, |
| 641 |
faulthandler_fatal_error); |
| 642 |
err = (handler->previous == SIG_ERR); |
| 643 |
#endif |
| 644 |
if (err) { |
| 645 |
return strerror(errno); |
| 646 |
} |
| 647 |
handler->enabled = 1; |
| 648 |
} |
| 649 |
} |
| 650 |
return NULL; |
| 651 |
} |
| 652 |
|
| 653 |
RPY_EXTERN |
| 654 |
void pypy_faulthandler_disable(void) |
| 655 |
{ |
| 656 |
int i; |
| 657 |
if (fatal_error.enabled) { |
| 658 |
fatal_error.enabled = 0; |
| 659 |
for (i = 0; i < faulthandler_nsignals; i++) { |
| 660 |
fault_handler_t *handler = &faulthandler_handlers[i]; |
| 661 |
if (!handler->enabled) |
| 662 |
continue; |
| 663 |
#ifdef HAVE_SIGACTION |
| 664 |
(void)sigaction(handler->signum, &handler->previous, NULL); |
| 665 |
#else |
| 666 |
(void)signal(handler->signum, handler->previous); |
| 667 |
#endif |
| 668 |
handler->enabled = 0; |
| 669 |
} |
| 670 |
} |
| 671 |
fatal_error.fd = -1; |
| 672 |
} |
| 673 |
|
| 674 |
RPY_EXTERN |
| 675 |
int pypy_faulthandler_is_enabled(void) |
| 676 |
{ |
| 677 |
return fatal_error.enabled; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
static void |
| 687 |
faulthandler_suppress_crash_report(void) |
| 688 |
{ |
| 689 |
#ifdef _WIN32 |
| 690 |
UINT mode; |
| 691 |
|
| 692 |
|
| 693 |
mode = SetErrorMode(SEM_NOGPFAULTERRORBOX); |
| 694 |
SetErrorMode(mode | SEM_NOGPFAULTERRORBOX); |
| 695 |
#endif |
| 696 |
|
| 697 |
#ifndef _WIN32 |
| 698 |
struct rlimit rl; |
| 699 |
|
| 700 |
|
| 701 |
if (getrlimit(RLIMIT_CORE, &rl) != 0) { |
| 702 |
rl.rlim_cur = 0; |
| 703 |
setrlimit(RLIMIT_CORE, &rl); |
| 704 |
} |
| 705 |
#endif |
| 706 |
|
| 707 |
#ifdef _MSC_VER |
| 708 |
|
| 709 |
|
| 710 |
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
| 711 |
#endif |
| 712 |
} |
| 713 |
|
| 714 |
RPY_EXTERN |
| 715 |
int pypy_faulthandler_read_null(void) |
| 716 |
{ |
| 717 |
int *volatile x; |
| 718 |
|
| 719 |
faulthandler_suppress_crash_report(); |
| 720 |
x = NULL; |
| 721 |
return *x; |
| 722 |
} |
| 723 |
|
| 724 |
RPY_EXTERN |
| 725 |
void pypy_faulthandler_sigsegv(void) |
| 726 |
{ |
| 727 |
faulthandler_suppress_crash_report(); |
| 728 |
#ifdef _WIN32 |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
while(1) |
| 741 |
raise(SIGSEGV); |
| 742 |
#else |
| 743 |
raise(SIGSEGV); |
| 744 |
#endif |
| 745 |
} |
| 746 |
|
| 747 |
RPY_EXTERN |
| 748 |
int pypy_faulthandler_sigfpe(void) |
| 749 |
{ |
| 750 |
|
| 751 |
|
| 752 |
volatile int x = 1, y = 0, z; |
| 753 |
faulthandler_suppress_crash_report(); |
| 754 |
z = x / y; |
| 755 |
|
| 756 |
|
| 757 |
raise(SIGFPE); |
| 758 |
|
| 759 |
|
| 760 |
return z; |
| 761 |
} |
| 762 |
|
| 763 |
RPY_EXTERN |
| 764 |
void pypy_faulthandler_sigabrt(void) |
| 765 |
{ |
| 766 |
faulthandler_suppress_crash_report(); |
| 767 |
abort(); |
| 768 |
} |
| 769 |
|
| 770 |
static double fh_stack_overflow(double levels) |
| 771 |
{ |
| 772 |
if (levels > 2.5) { |
| 773 |
return (sqrt(fh_stack_overflow(levels - 1.0)) |
| 774 |
+ fh_stack_overflow(levels * 1e-10)); |
| 775 |
} |
| 776 |
return 1e100 + levels; |
| 777 |
} |
| 778 |
|
| 779 |
RPY_EXTERN |
| 780 |
double pypy_faulthandler_stackoverflow(double levels) |
| 781 |
{ |
| 782 |
faulthandler_suppress_crash_report(); |
| 783 |
return fh_stack_overflow(levels); |
| 784 |
} |