Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "pythread.h" |
| 3 | #include <signal.h> |
| 4 | #include <object.h> |
| 5 | #include <frameobject.h> |
| 6 | #include <signal.h> |
Victor Stinner | 0aafa4f | 2011-06-29 23:28:02 +0200 | [diff] [blame^] | 7 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
| 8 | #include <pthread.h> |
| 9 | #endif |
| 10 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 11 | |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 12 | /* Allocate at maximum 100 MB of the stack to raise the stack overflow */ |
| 13 | #define STACK_OVERFLOW_MAX_SIZE (100*1024*1024) |
| 14 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 15 | #ifdef WITH_THREAD |
| 16 | # define FAULTHANDLER_LATER |
| 17 | #endif |
| 18 | |
| 19 | #ifndef MS_WINDOWS |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 20 | /* register() is useless on Windows, because only SIGSEGV, SIGABRT and |
| 21 | SIGILL can be handled by the process, and these signals can only be used |
| 22 | with enable(), not using register() */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 23 | # define FAULTHANDLER_USER |
| 24 | #endif |
| 25 | |
| 26 | #define PUTS(fd, str) write(fd, str, strlen(str)) |
| 27 | |
| 28 | #ifdef HAVE_SIGACTION |
| 29 | typedef struct sigaction _Py_sighandler_t; |
| 30 | #else |
| 31 | typedef PyOS_sighandler_t _Py_sighandler_t; |
| 32 | #endif |
| 33 | |
| 34 | typedef struct { |
| 35 | int signum; |
| 36 | int enabled; |
| 37 | const char* name; |
| 38 | _Py_sighandler_t previous; |
| 39 | int all_threads; |
| 40 | } fault_handler_t; |
| 41 | |
| 42 | static struct { |
| 43 | int enabled; |
| 44 | PyObject *file; |
| 45 | int fd; |
| 46 | int all_threads; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 47 | PyInterpreterState *interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 48 | } fatal_error = {0, NULL, -1, 0}; |
| 49 | |
| 50 | #ifdef FAULTHANDLER_LATER |
| 51 | static struct { |
| 52 | PyObject *file; |
| 53 | int fd; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 54 | PY_TIMEOUT_T timeout_us; /* timeout in microseconds */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 55 | int repeat; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 56 | PyInterpreterState *interp; |
| 57 | int exit; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 58 | char *header; |
| 59 | size_t header_len; |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 60 | /* The main thread always holds this lock. It is only released when |
| 61 | faulthandler_thread() is interrupted before this thread exits, or at |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 62 | Python exit. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 63 | PyThread_type_lock cancel_event; |
| 64 | /* released by child thread when joined */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 65 | PyThread_type_lock running; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 66 | } thread; |
| 67 | #endif |
| 68 | |
| 69 | #ifdef FAULTHANDLER_USER |
| 70 | typedef struct { |
| 71 | int enabled; |
| 72 | PyObject *file; |
| 73 | int fd; |
| 74 | int all_threads; |
| 75 | _Py_sighandler_t previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 76 | PyInterpreterState *interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 77 | } user_signal_t; |
| 78 | |
| 79 | static user_signal_t *user_signals; |
| 80 | |
| 81 | /* the following macros come from Python: Modules/signalmodule.c */ |
| 82 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 83 | #define NSIG 12 |
| 84 | #endif |
| 85 | #ifndef NSIG |
| 86 | # if defined(_NSIG) |
| 87 | # define NSIG _NSIG /* For BSD/SysV */ |
| 88 | # elif defined(_SIGMAX) |
| 89 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
| 90 | # elif defined(SIGMAX) |
| 91 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
| 92 | # else |
| 93 | # define NSIG 64 /* Use a reasonable default value */ |
| 94 | # endif |
| 95 | #endif |
| 96 | |
| 97 | #endif /* FAULTHANDLER_USER */ |
| 98 | |
| 99 | |
| 100 | static fault_handler_t faulthandler_handlers[] = { |
| 101 | #ifdef SIGBUS |
| 102 | {SIGBUS, 0, "Bus error", }, |
| 103 | #endif |
| 104 | #ifdef SIGILL |
| 105 | {SIGILL, 0, "Illegal instruction", }, |
| 106 | #endif |
| 107 | {SIGFPE, 0, "Floating point exception", }, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 108 | {SIGABRT, 0, "Aborted", }, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 109 | /* define SIGSEGV at the end to make it the default choice if searching the |
| 110 | handler fails in faulthandler_fatal_error() */ |
| 111 | {SIGSEGV, 0, "Segmentation fault", } |
| 112 | }; |
| 113 | static const unsigned char faulthandler_nsignals = \ |
| 114 | sizeof(faulthandler_handlers) / sizeof(faulthandler_handlers[0]); |
| 115 | |
| 116 | #ifdef HAVE_SIGALTSTACK |
| 117 | static stack_t stack; |
| 118 | #endif |
| 119 | |
| 120 | |
| 121 | /* Get the file descriptor of a file by calling its fileno() method and then |
| 122 | call its flush() method. |
| 123 | |
| 124 | If file is NULL or Py_None, use sys.stderr as the new file. |
| 125 | |
| 126 | On success, return the new file and write the file descriptor into *p_fd. |
| 127 | On error, return NULL. */ |
| 128 | |
| 129 | static PyObject* |
| 130 | faulthandler_get_fileno(PyObject *file, int *p_fd) |
| 131 | { |
| 132 | PyObject *result; |
| 133 | long fd_long; |
| 134 | int fd; |
| 135 | |
| 136 | if (file == NULL || file == Py_None) { |
| 137 | file = PySys_GetObject("stderr"); |
| 138 | if (file == NULL) { |
| 139 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr"); |
| 140 | return NULL; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | result = PyObject_CallMethod(file, "fileno", ""); |
| 145 | if (result == NULL) |
| 146 | return NULL; |
| 147 | |
| 148 | fd = -1; |
| 149 | if (PyLong_Check(result)) { |
| 150 | fd_long = PyLong_AsLong(result); |
| 151 | if (0 <= fd_long && fd_long < INT_MAX) |
| 152 | fd = (int)fd_long; |
| 153 | } |
| 154 | Py_DECREF(result); |
| 155 | |
| 156 | if (fd == -1) { |
| 157 | PyErr_SetString(PyExc_RuntimeError, |
| 158 | "file.fileno() is not a valid file descriptor"); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | result = PyObject_CallMethod(file, "flush", ""); |
| 163 | if (result != NULL) |
| 164 | Py_DECREF(result); |
| 165 | else { |
| 166 | /* ignore flush() error */ |
| 167 | PyErr_Clear(); |
| 168 | } |
| 169 | *p_fd = fd; |
| 170 | return file; |
| 171 | } |
| 172 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 173 | /* Get the state of the current thread: only call this function if the current |
| 174 | thread holds the GIL. Raise an exception on error. */ |
| 175 | static PyThreadState* |
| 176 | get_thread_state(void) |
| 177 | { |
| 178 | PyThreadState *tstate = PyThreadState_Get(); |
| 179 | if (tstate == NULL) { |
| 180 | PyErr_SetString(PyExc_RuntimeError, |
| 181 | "unable to get the current thread state"); |
| 182 | return NULL; |
| 183 | } |
| 184 | return tstate; |
| 185 | } |
| 186 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 187 | static PyObject* |
| 188 | faulthandler_dump_traceback_py(PyObject *self, |
| 189 | PyObject *args, PyObject *kwargs) |
| 190 | { |
| 191 | static char *kwlist[] = {"file", "all_threads", NULL}; |
| 192 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 193 | int all_threads = 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 194 | PyThreadState *tstate; |
| 195 | const char *errmsg; |
| 196 | int fd; |
| 197 | |
| 198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 199 | "|Oi:dump_traceback", kwlist, |
| 200 | &file, &all_threads)) |
| 201 | return NULL; |
| 202 | |
| 203 | file = faulthandler_get_fileno(file, &fd); |
| 204 | if (file == NULL) |
| 205 | return NULL; |
| 206 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 207 | tstate = get_thread_state(); |
| 208 | if (tstate == NULL) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 209 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 210 | |
| 211 | if (all_threads) { |
| 212 | errmsg = _Py_DumpTracebackThreads(fd, tstate->interp, tstate); |
| 213 | if (errmsg != NULL) { |
| 214 | PyErr_SetString(PyExc_RuntimeError, errmsg); |
| 215 | return NULL; |
| 216 | } |
| 217 | } |
| 218 | else { |
| 219 | _Py_DumpTraceback(fd, tstate); |
| 220 | } |
| 221 | Py_RETURN_NONE; |
| 222 | } |
| 223 | |
| 224 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 225 | /* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 226 | |
| 227 | Display the current Python traceback, restore the previous handler and call |
| 228 | the previous handler. |
| 229 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 230 | On Windows, don't explicitly call the previous handler, because the Windows |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 231 | signal handler would not be called (for an unknown reason). The execution of |
| 232 | the program continues at faulthandler_fatal_error() exit, but the same |
| 233 | instruction will raise the same fault (signal), and so the previous handler |
| 234 | will be called. |
| 235 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 236 | This function is signal-safe and should only call signal-safe functions. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 237 | |
| 238 | static void |
Victor Stinner | 44e31ba | 2011-04-07 11:39:03 +0200 | [diff] [blame] | 239 | faulthandler_fatal_error(int signum) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 240 | { |
| 241 | const int fd = fatal_error.fd; |
| 242 | unsigned int i; |
| 243 | fault_handler_t *handler = NULL; |
| 244 | PyThreadState *tstate; |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 245 | int save_errno = errno; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 246 | |
| 247 | if (!fatal_error.enabled) |
| 248 | return; |
| 249 | |
| 250 | for (i=0; i < faulthandler_nsignals; i++) { |
| 251 | handler = &faulthandler_handlers[i]; |
| 252 | if (handler->signum == signum) |
| 253 | break; |
| 254 | } |
| 255 | if (handler == NULL) { |
| 256 | /* faulthandler_nsignals == 0 (unlikely) */ |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | /* restore the previous handler */ |
| 261 | #ifdef HAVE_SIGACTION |
| 262 | (void)sigaction(handler->signum, &handler->previous, NULL); |
| 263 | #else |
| 264 | (void)signal(handler->signum, handler->previous); |
| 265 | #endif |
| 266 | handler->enabled = 0; |
| 267 | |
| 268 | PUTS(fd, "Fatal Python error: "); |
| 269 | PUTS(fd, handler->name); |
| 270 | PUTS(fd, "\n\n"); |
| 271 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 272 | #ifdef WITH_THREAD |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 273 | /* SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals and |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 274 | are thus delivered to the thread that caused the fault. Get the Python |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 275 | thread state of the current thread. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 276 | |
| 277 | PyThreadState_Get() doesn't give the state of the thread that caused the |
| 278 | fault if the thread released the GIL, and so this function cannot be |
| 279 | used. Read the thread local storage (TLS) instead: call |
| 280 | PyGILState_GetThisThreadState(). */ |
| 281 | tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 282 | #else |
| 283 | tstate = PyThreadState_Get(); |
| 284 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 285 | |
| 286 | if (fatal_error.all_threads) |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 287 | _Py_DumpTracebackThreads(fd, fatal_error.interp, tstate); |
| 288 | else { |
| 289 | if (tstate != NULL) |
| 290 | _Py_DumpTraceback(fd, tstate); |
| 291 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 292 | |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 293 | errno = save_errno; |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 294 | #ifdef MS_WINDOWS |
| 295 | if (signum == SIGSEGV) { |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 296 | /* don't explicitly call the previous handler for SIGSEGV in this signal |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 297 | handler, because the Windows signal handler would not be called */ |
| 298 | return; |
| 299 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 300 | #endif |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 301 | /* call the previous signal handler: it is called immediatly if we use |
| 302 | sigaction() thanks to SA_NODEFER flag, otherwise it is deferred */ |
| 303 | raise(signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 306 | /* Install the handler for fatal signals, faulthandler_fatal_error(). */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 307 | |
| 308 | static PyObject* |
| 309 | faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs) |
| 310 | { |
| 311 | static char *kwlist[] = {"file", "all_threads", NULL}; |
| 312 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 313 | int all_threads = 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 314 | unsigned int i; |
| 315 | fault_handler_t *handler; |
| 316 | #ifdef HAVE_SIGACTION |
| 317 | struct sigaction action; |
| 318 | #endif |
| 319 | int err; |
| 320 | int fd; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 321 | PyThreadState *tstate; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 322 | |
| 323 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 324 | "|Oi:enable", kwlist, &file, &all_threads)) |
| 325 | return NULL; |
| 326 | |
| 327 | file = faulthandler_get_fileno(file, &fd); |
| 328 | if (file == NULL) |
| 329 | return NULL; |
| 330 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 331 | tstate = get_thread_state(); |
| 332 | if (tstate == NULL) |
| 333 | return NULL; |
| 334 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 335 | Py_XDECREF(fatal_error.file); |
| 336 | Py_INCREF(file); |
| 337 | fatal_error.file = file; |
| 338 | fatal_error.fd = fd; |
| 339 | fatal_error.all_threads = all_threads; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 340 | fatal_error.interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 341 | |
| 342 | if (!fatal_error.enabled) { |
| 343 | fatal_error.enabled = 1; |
| 344 | |
| 345 | for (i=0; i < faulthandler_nsignals; i++) { |
| 346 | handler = &faulthandler_handlers[i]; |
| 347 | #ifdef HAVE_SIGACTION |
Victor Stinner | 44e31ba | 2011-04-07 11:39:03 +0200 | [diff] [blame] | 348 | action.sa_handler = faulthandler_fatal_error; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 349 | sigemptyset(&action.sa_mask); |
| 350 | /* Do not prevent the signal from being received from within |
| 351 | its own signal handler */ |
| 352 | action.sa_flags = SA_NODEFER; |
| 353 | #ifdef HAVE_SIGALTSTACK |
| 354 | if (stack.ss_sp != NULL) { |
| 355 | /* Call the signal handler on an alternate signal stack |
| 356 | provided by sigaltstack() */ |
| 357 | action.sa_flags |= SA_ONSTACK; |
| 358 | } |
| 359 | #endif |
| 360 | err = sigaction(handler->signum, &action, &handler->previous); |
| 361 | #else |
| 362 | handler->previous = signal(handler->signum, |
| 363 | faulthandler_fatal_error); |
| 364 | err = (handler->previous == SIG_ERR); |
| 365 | #endif |
| 366 | if (err) { |
| 367 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 368 | return NULL; |
| 369 | } |
| 370 | handler->enabled = 1; |
| 371 | } |
| 372 | } |
| 373 | Py_RETURN_NONE; |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | faulthandler_disable(void) |
| 378 | { |
| 379 | unsigned int i; |
| 380 | fault_handler_t *handler; |
| 381 | |
| 382 | if (fatal_error.enabled) { |
| 383 | fatal_error.enabled = 0; |
| 384 | for (i=0; i < faulthandler_nsignals; i++) { |
| 385 | handler = &faulthandler_handlers[i]; |
| 386 | if (!handler->enabled) |
| 387 | continue; |
| 388 | #ifdef HAVE_SIGACTION |
| 389 | (void)sigaction(handler->signum, &handler->previous, NULL); |
| 390 | #else |
| 391 | (void)signal(handler->signum, handler->previous); |
| 392 | #endif |
| 393 | handler->enabled = 0; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | Py_CLEAR(fatal_error.file); |
| 398 | } |
| 399 | |
| 400 | static PyObject* |
| 401 | faulthandler_disable_py(PyObject *self) |
| 402 | { |
| 403 | if (!fatal_error.enabled) { |
| 404 | Py_INCREF(Py_False); |
| 405 | return Py_False; |
| 406 | } |
| 407 | faulthandler_disable(); |
| 408 | Py_INCREF(Py_True); |
| 409 | return Py_True; |
| 410 | } |
| 411 | |
| 412 | static PyObject* |
| 413 | faulthandler_is_enabled(PyObject *self) |
| 414 | { |
| 415 | return PyBool_FromLong(fatal_error.enabled); |
| 416 | } |
| 417 | |
| 418 | #ifdef FAULTHANDLER_LATER |
| 419 | |
| 420 | static void |
| 421 | faulthandler_thread(void *unused) |
| 422 | { |
| 423 | PyLockStatus st; |
| 424 | const char* errmsg; |
| 425 | PyThreadState *current; |
| 426 | int ok; |
Victor Stinner | cf2a807 | 2011-04-19 23:30:57 +0200 | [diff] [blame] | 427 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 428 | sigset_t set; |
| 429 | |
| 430 | /* we don't want to receive any signal */ |
| 431 | sigfillset(&set); |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 432 | pthread_sigmask(SIG_SETMASK, &set, NULL); |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 433 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 434 | |
| 435 | do { |
| 436 | st = PyThread_acquire_lock_timed(thread.cancel_event, |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 437 | thread.timeout_us, 0); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 438 | if (st == PY_LOCK_ACQUIRED) { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 439 | PyThread_release_lock(thread.cancel_event); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | /* Timeout => dump traceback */ |
| 443 | assert(st == PY_LOCK_FAILURE); |
| 444 | |
| 445 | /* get the thread holding the GIL, NULL if no thread hold the GIL */ |
| 446 | current = _Py_atomic_load_relaxed(&_PyThreadState_Current); |
| 447 | |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 448 | write(thread.fd, thread.header, thread.header_len); |
| 449 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 450 | errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current); |
| 451 | ok = (errmsg == NULL); |
| 452 | |
| 453 | if (thread.exit) |
| 454 | _exit(1); |
| 455 | } while (ok && thread.repeat); |
| 456 | |
| 457 | /* The only way out */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 458 | PyThread_release_lock(thread.running); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 462 | cancel_dump_tracebacks_later(void) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 463 | { |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 464 | /* Notify cancellation */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 465 | PyThread_release_lock(thread.cancel_event); |
| 466 | |
Victor Stinner | a4d4f1b | 2011-04-01 03:00:05 +0200 | [diff] [blame] | 467 | /* Wait for thread to join */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 468 | PyThread_acquire_lock(thread.running, 1); |
| 469 | PyThread_release_lock(thread.running); |
| 470 | |
| 471 | /* The main thread should always hold the cancel_event lock */ |
| 472 | PyThread_acquire_lock(thread.cancel_event, 1); |
| 473 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 474 | Py_CLEAR(thread.file); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 475 | if (thread.header) { |
| 476 | free(thread.header); |
| 477 | thread.header = NULL; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | static char* |
| 482 | format_timeout(double timeout) |
| 483 | { |
| 484 | unsigned long us, sec, min, hour; |
| 485 | double intpart, fracpart; |
| 486 | char buffer[100]; |
| 487 | |
| 488 | fracpart = modf(timeout, &intpart); |
| 489 | sec = (unsigned long)intpart; |
| 490 | us = (unsigned long)(fracpart * 1e6); |
| 491 | min = sec / 60; |
| 492 | sec %= 60; |
| 493 | hour = min / 60; |
| 494 | min %= 60; |
| 495 | |
| 496 | if (us != 0) |
| 497 | PyOS_snprintf(buffer, sizeof(buffer), |
| 498 | "Timeout (%lu:%02lu:%02lu.%06lu)!\n", |
| 499 | hour, min, sec, us); |
| 500 | else |
| 501 | PyOS_snprintf(buffer, sizeof(buffer), |
| 502 | "Timeout (%lu:%02lu:%02lu)!\n", |
| 503 | hour, min, sec); |
| 504 | |
| 505 | return strdup(buffer); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static PyObject* |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 509 | faulthandler_dump_tracebacks_later(PyObject *self, |
| 510 | PyObject *args, PyObject *kwargs) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 511 | { |
| 512 | static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL}; |
| 513 | double timeout; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 514 | PY_TIMEOUT_T timeout_us; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 515 | int repeat = 0; |
| 516 | PyObject *file = NULL; |
| 517 | int fd; |
| 518 | int exit = 0; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 519 | PyThreadState *tstate; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 520 | char *header; |
| 521 | size_t header_len; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 522 | |
| 523 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 524 | "d|iOi:dump_tracebacks_later", kwlist, |
| 525 | &timeout, &repeat, &file, &exit)) |
| 526 | return NULL; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 527 | if ((timeout * 1e6) >= (double) PY_TIMEOUT_MAX) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 528 | PyErr_SetString(PyExc_OverflowError, "timeout value is too large"); |
| 529 | return NULL; |
| 530 | } |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 531 | timeout_us = (PY_TIMEOUT_T)(timeout * 1e6); |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 532 | if (timeout_us <= 0) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 533 | PyErr_SetString(PyExc_ValueError, "timeout must be greater than 0"); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 537 | tstate = get_thread_state(); |
| 538 | if (tstate == NULL) |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 539 | return NULL; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 540 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 541 | file = faulthandler_get_fileno(file, &fd); |
| 542 | if (file == NULL) |
| 543 | return NULL; |
| 544 | |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 545 | /* format the timeout */ |
| 546 | header = format_timeout(timeout); |
| 547 | if (header == NULL) |
| 548 | return PyErr_NoMemory(); |
| 549 | header_len = strlen(header); |
| 550 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 551 | /* Cancel previous thread, if running */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 552 | cancel_dump_tracebacks_later(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 553 | |
| 554 | Py_XDECREF(thread.file); |
| 555 | Py_INCREF(file); |
| 556 | thread.file = file; |
| 557 | thread.fd = fd; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 558 | thread.timeout_us = timeout_us; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 559 | thread.repeat = repeat; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 560 | thread.interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 561 | thread.exit = exit; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 562 | thread.header = header; |
| 563 | thread.header_len = header_len; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 564 | |
| 565 | /* Arm these locks to serve as events when released */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 566 | PyThread_acquire_lock(thread.running, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 567 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 568 | if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 569 | PyThread_release_lock(thread.running); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 570 | Py_CLEAR(thread.file); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 571 | free(header); |
| 572 | thread.header = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 573 | PyErr_SetString(PyExc_RuntimeError, |
| 574 | "unable to start watchdog thread"); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | Py_RETURN_NONE; |
| 579 | } |
| 580 | |
| 581 | static PyObject* |
Victor Stinner | 702624e | 2011-03-31 03:42:34 +0200 | [diff] [blame] | 582 | faulthandler_cancel_dump_tracebacks_later_py(PyObject *self) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 583 | { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 584 | cancel_dump_tracebacks_later(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 585 | Py_RETURN_NONE; |
| 586 | } |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 587 | #endif /* FAULTHANDLER_LATER */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 588 | |
| 589 | #ifdef FAULTHANDLER_USER |
| 590 | /* Handler of user signals (e.g. SIGUSR1). |
| 591 | |
| 592 | Dump the traceback of the current thread, or of all threads if |
| 593 | thread.all_threads is true. |
| 594 | |
| 595 | This function is signal safe and should only call signal safe functions. */ |
| 596 | |
| 597 | static void |
| 598 | faulthandler_user(int signum) |
| 599 | { |
| 600 | user_signal_t *user; |
| 601 | PyThreadState *tstate; |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 602 | int save_errno = errno; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 603 | |
| 604 | user = &user_signals[signum]; |
| 605 | if (!user->enabled) |
| 606 | return; |
| 607 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 608 | #ifdef WITH_THREAD |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 609 | /* PyThreadState_Get() doesn't give the state of the current thread if |
| 610 | the thread doesn't hold the GIL. Read the thread local storage (TLS) |
| 611 | instead: call PyGILState_GetThisThreadState(). */ |
| 612 | tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 613 | #else |
| 614 | tstate = PyThreadState_Get(); |
| 615 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 616 | |
| 617 | if (user->all_threads) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 618 | _Py_DumpTracebackThreads(user->fd, user->interp, tstate); |
| 619 | else { |
| 620 | if (tstate == NULL) |
| 621 | return; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 622 | _Py_DumpTraceback(user->fd, tstate); |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 623 | } |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 624 | errno = save_errno; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int |
| 628 | check_signum(int signum) |
| 629 | { |
| 630 | unsigned int i; |
| 631 | |
| 632 | for (i=0; i < faulthandler_nsignals; i++) { |
| 633 | if (faulthandler_handlers[i].signum == signum) { |
| 634 | PyErr_Format(PyExc_RuntimeError, |
| 635 | "signal %i cannot be registered, " |
| 636 | "use enable() instead", |
| 637 | signum); |
| 638 | return 0; |
| 639 | } |
| 640 | } |
| 641 | if (signum < 1 || NSIG <= signum) { |
| 642 | PyErr_SetString(PyExc_ValueError, "signal number out of range"); |
| 643 | return 0; |
| 644 | } |
| 645 | return 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static PyObject* |
| 649 | faulthandler_register(PyObject *self, |
| 650 | PyObject *args, PyObject *kwargs) |
| 651 | { |
| 652 | static char *kwlist[] = {"signum", "file", "all_threads", NULL}; |
| 653 | int signum; |
| 654 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 655 | int all_threads = 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 656 | int fd; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 657 | user_signal_t *user; |
| 658 | _Py_sighandler_t previous; |
| 659 | #ifdef HAVE_SIGACTION |
| 660 | struct sigaction action; |
| 661 | #endif |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 662 | PyThreadState *tstate; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 663 | int err; |
| 664 | |
| 665 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 666 | "i|Oi:register", kwlist, |
| 667 | &signum, &file, &all_threads)) |
| 668 | return NULL; |
| 669 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 670 | if (!check_signum(signum)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 671 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 672 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 673 | tstate = get_thread_state(); |
| 674 | if (tstate == NULL) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 675 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 676 | |
| 677 | file = faulthandler_get_fileno(file, &fd); |
| 678 | if (file == NULL) |
| 679 | return NULL; |
| 680 | |
| 681 | if (user_signals == NULL) { |
| 682 | user_signals = calloc(NSIG, sizeof(user_signal_t)); |
| 683 | if (user_signals == NULL) |
| 684 | return PyErr_NoMemory(); |
| 685 | } |
| 686 | user = &user_signals[signum]; |
| 687 | |
| 688 | if (!user->enabled) { |
| 689 | #ifdef HAVE_SIGACTION |
| 690 | action.sa_handler = faulthandler_user; |
| 691 | sigemptyset(&action.sa_mask); |
| 692 | /* if the signal is received while the kernel is executing a system |
| 693 | call, try to restart the system call instead of interrupting it and |
| 694 | return EINTR */ |
| 695 | action.sa_flags = SA_RESTART; |
| 696 | #ifdef HAVE_SIGALTSTACK |
| 697 | if (stack.ss_sp != NULL) { |
| 698 | /* Call the signal handler on an alternate signal stack |
| 699 | provided by sigaltstack() */ |
| 700 | action.sa_flags |= SA_ONSTACK; |
| 701 | } |
| 702 | #endif |
| 703 | err = sigaction(signum, &action, &previous); |
| 704 | #else |
| 705 | previous = signal(signum, faulthandler_user); |
| 706 | err = (previous == SIG_ERR); |
| 707 | #endif |
| 708 | if (err) { |
| 709 | PyErr_SetFromErrno(PyExc_OSError); |
| 710 | return NULL; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | Py_XDECREF(user->file); |
| 715 | Py_INCREF(file); |
| 716 | user->file = file; |
| 717 | user->fd = fd; |
| 718 | user->all_threads = all_threads; |
| 719 | user->previous = previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 720 | user->interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 721 | user->enabled = 1; |
| 722 | |
| 723 | Py_RETURN_NONE; |
| 724 | } |
| 725 | |
| 726 | static int |
| 727 | faulthandler_unregister(user_signal_t *user, int signum) |
| 728 | { |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 729 | if (!user->enabled) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | user->enabled = 0; |
| 732 | #ifdef HAVE_SIGACTION |
| 733 | (void)sigaction(signum, &user->previous, NULL); |
| 734 | #else |
| 735 | (void)signal(signum, user->previous); |
| 736 | #endif |
| 737 | Py_CLEAR(user->file); |
| 738 | user->fd = -1; |
| 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | static PyObject* |
| 743 | faulthandler_unregister_py(PyObject *self, PyObject *args) |
| 744 | { |
| 745 | int signum; |
| 746 | user_signal_t *user; |
| 747 | int change; |
| 748 | |
| 749 | if (!PyArg_ParseTuple(args, "i:unregister", &signum)) |
| 750 | return NULL; |
| 751 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 752 | if (!check_signum(signum)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 753 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 754 | |
Victor Stinner | cfa7123 | 2011-04-08 12:48:15 +0200 | [diff] [blame] | 755 | if (user_signals == NULL) |
| 756 | Py_RETURN_FALSE; |
| 757 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 758 | user = &user_signals[signum]; |
| 759 | change = faulthandler_unregister(user, signum); |
| 760 | return PyBool_FromLong(change); |
| 761 | } |
| 762 | #endif /* FAULTHANDLER_USER */ |
| 763 | |
| 764 | |
| 765 | static PyObject * |
| 766 | faulthandler_read_null(PyObject *self, PyObject *args) |
| 767 | { |
| 768 | int *x = NULL, y; |
| 769 | int release_gil = 0; |
| 770 | if (!PyArg_ParseTuple(args, "|i:_read_null", &release_gil)) |
| 771 | return NULL; |
| 772 | if (release_gil) { |
| 773 | Py_BEGIN_ALLOW_THREADS |
| 774 | y = *x; |
| 775 | Py_END_ALLOW_THREADS |
| 776 | } else |
| 777 | y = *x; |
| 778 | return PyLong_FromLong(y); |
| 779 | |
| 780 | } |
| 781 | |
| 782 | static PyObject * |
| 783 | faulthandler_sigsegv(PyObject *self, PyObject *args) |
| 784 | { |
| 785 | #if defined(MS_WINDOWS) |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 786 | /* For SIGSEGV, faulthandler_fatal_error() restores the previous signal |
| 787 | handler and then gives back the execution flow to the program (without |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 788 | explicitly calling the previous error handler). In a normal case, the |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 789 | SIGSEGV was raised by the kernel because of a fault, and so if the |
| 790 | program retries to execute the same instruction, the fault will be |
| 791 | raised again. |
| 792 | |
| 793 | Here the fault is simulated by a fake SIGSEGV signal raised by the |
| 794 | application. We have to raise SIGSEGV at lease twice: once for |
| 795 | faulthandler_fatal_error(), and one more time for the previous signal |
| 796 | handler. */ |
| 797 | while(1) |
| 798 | raise(SIGSEGV); |
| 799 | #else |
| 800 | raise(SIGSEGV); |
| 801 | #endif |
| 802 | Py_RETURN_NONE; |
| 803 | } |
| 804 | |
| 805 | static PyObject * |
| 806 | faulthandler_sigfpe(PyObject *self, PyObject *args) |
| 807 | { |
| 808 | /* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on |
| 809 | PowerPC. Use volatile to disable compile-time optimizations. */ |
| 810 | volatile int x = 1, y = 0, z; |
| 811 | z = x / y; |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 812 | /* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC), |
| 813 | raise it manually. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 814 | raise(SIGFPE); |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 815 | /* This line is never reached, but we pretend to make something with z |
| 816 | to silence a compiler warning. */ |
Victor Stinner | e0c9a75 | 2011-05-09 14:44:26 +0200 | [diff] [blame] | 817 | return PyLong_FromLong(z); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 820 | static PyObject * |
| 821 | faulthandler_sigabrt(PyObject *self, PyObject *args) |
| 822 | { |
Victor Stinner | 00bc6cc | 2011-05-10 01:30:03 +0200 | [diff] [blame] | 823 | #ifdef _MSC_VER |
| 824 | /* Visual Studio: configure abort() to not display an error message nor |
| 825 | open a popup asking to report the fault. */ |
| 826 | _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 827 | #endif |
Victor Stinner | 00bc6cc | 2011-05-10 01:30:03 +0200 | [diff] [blame] | 828 | abort(); |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 829 | Py_RETURN_NONE; |
| 830 | } |
| 831 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 832 | #ifdef SIGBUS |
| 833 | static PyObject * |
| 834 | faulthandler_sigbus(PyObject *self, PyObject *args) |
| 835 | { |
| 836 | raise(SIGBUS); |
| 837 | Py_RETURN_NONE; |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | #ifdef SIGILL |
| 842 | static PyObject * |
| 843 | faulthandler_sigill(PyObject *self, PyObject *args) |
| 844 | { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 845 | raise(SIGILL); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 846 | Py_RETURN_NONE; |
| 847 | } |
| 848 | #endif |
| 849 | |
| 850 | static PyObject * |
| 851 | faulthandler_fatal_error_py(PyObject *self, PyObject *args) |
| 852 | { |
| 853 | char *message; |
| 854 | if (!PyArg_ParseTuple(args, "y:fatal_error", &message)) |
| 855 | return NULL; |
| 856 | Py_FatalError(message); |
| 857 | Py_RETURN_NONE; |
| 858 | } |
| 859 | |
| 860 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
Victor Stinner | 9b49304 | 2011-05-23 12:29:10 +0200 | [diff] [blame] | 861 | static void* |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 862 | stack_overflow(void *min_sp, void *max_sp, size_t *depth) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 863 | { |
| 864 | /* allocate 4096 bytes on the stack at each call */ |
| 865 | unsigned char buffer[4096]; |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 866 | void *sp = &buffer; |
| 867 | *depth += 1; |
| 868 | if (sp < min_sp || max_sp < sp) |
| 869 | return sp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 870 | buffer[0] = 1; |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 871 | buffer[4095] = 0; |
| 872 | return stack_overflow(min_sp, max_sp, depth); |
| 873 | } |
| 874 | |
| 875 | static PyObject * |
| 876 | faulthandler_stack_overflow(PyObject *self) |
| 877 | { |
| 878 | size_t depth, size; |
| 879 | void *sp = &depth, *stop; |
| 880 | |
| 881 | depth = 0; |
| 882 | stop = stack_overflow(sp - STACK_OVERFLOW_MAX_SIZE, |
| 883 | sp + STACK_OVERFLOW_MAX_SIZE, |
| 884 | &depth); |
| 885 | if (sp < stop) |
| 886 | size = stop - sp; |
| 887 | else |
| 888 | size = sp - stop; |
| 889 | PyErr_Format(PyExc_RuntimeError, |
| 890 | "unable to raise a stack overflow (allocated %zu bytes " |
| 891 | "on the stack, %zu recursive calls)", |
| 892 | size, depth); |
| 893 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 894 | } |
| 895 | #endif |
| 896 | |
| 897 | |
| 898 | static int |
| 899 | faulthandler_traverse(PyObject *module, visitproc visit, void *arg) |
| 900 | { |
| 901 | #ifdef FAULTHANDLER_USER |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 902 | unsigned int signum; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 903 | #endif |
| 904 | |
| 905 | #ifdef FAULTHANDLER_LATER |
| 906 | Py_VISIT(thread.file); |
| 907 | #endif |
| 908 | #ifdef FAULTHANDLER_USER |
| 909 | if (user_signals != NULL) { |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 910 | for (signum=0; signum < NSIG; signum++) |
| 911 | Py_VISIT(user_signals[signum].file); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 912 | } |
| 913 | #endif |
| 914 | Py_VISIT(fatal_error.file); |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | PyDoc_STRVAR(module_doc, |
| 919 | "faulthandler module."); |
| 920 | |
| 921 | static PyMethodDef module_methods[] = { |
| 922 | {"enable", |
| 923 | (PyCFunction)faulthandler_enable, METH_VARARGS|METH_KEYWORDS, |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 924 | PyDoc_STR("enable(file=sys.stderr, all_threads=True): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 925 | "enable the fault handler")}, |
| 926 | {"disable", (PyCFunction)faulthandler_disable_py, METH_NOARGS, |
| 927 | PyDoc_STR("disable(): disable the fault handler")}, |
| 928 | {"is_enabled", (PyCFunction)faulthandler_is_enabled, METH_NOARGS, |
| 929 | PyDoc_STR("is_enabled()->bool: check if the handler is enabled")}, |
| 930 | {"dump_traceback", |
| 931 | (PyCFunction)faulthandler_dump_traceback_py, METH_VARARGS|METH_KEYWORDS, |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 932 | PyDoc_STR("dump_traceback(file=sys.stderr, all_threads=True): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 933 | "dump the traceback of the current thread, or of all threads " |
| 934 | "if all_threads is True, into file")}, |
| 935 | #ifdef FAULTHANDLER_LATER |
| 936 | {"dump_tracebacks_later", |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 937 | (PyCFunction)faulthandler_dump_tracebacks_later, METH_VARARGS|METH_KEYWORDS, |
| 938 | PyDoc_STR("dump_tracebacks_later(timeout, repeat=False, file=sys.stderrn, exit=False):\n" |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 939 | "dump the traceback of all threads in timeout seconds,\n" |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 940 | "or each timeout seconds if repeat is True. If exit is True, " |
| 941 | "call _exit(1) which is not safe.")}, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 942 | {"cancel_dump_tracebacks_later", |
Victor Stinner | 702624e | 2011-03-31 03:42:34 +0200 | [diff] [blame] | 943 | (PyCFunction)faulthandler_cancel_dump_tracebacks_later_py, METH_NOARGS, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 944 | PyDoc_STR("cancel_dump_tracebacks_later():\ncancel the previous call " |
| 945 | "to dump_tracebacks_later().")}, |
| 946 | #endif |
| 947 | |
| 948 | #ifdef FAULTHANDLER_USER |
| 949 | {"register", |
| 950 | (PyCFunction)faulthandler_register, METH_VARARGS|METH_KEYWORDS, |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 951 | PyDoc_STR("register(signum, file=sys.stderr, all_threads=True): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 952 | "register an handler for the signal 'signum': dump the " |
| 953 | "traceback of the current thread, or of all threads if " |
| 954 | "all_threads is True, into file")}, |
| 955 | {"unregister", |
| 956 | faulthandler_unregister_py, METH_VARARGS|METH_KEYWORDS, |
| 957 | PyDoc_STR("unregister(signum): unregister the handler of the signal " |
| 958 | "'signum' registered by register()")}, |
| 959 | #endif |
| 960 | |
| 961 | {"_read_null", faulthandler_read_null, METH_VARARGS, |
| 962 | PyDoc_STR("_read_null(release_gil=False): read from NULL, raise " |
| 963 | "a SIGSEGV or SIGBUS signal depending on the platform")}, |
| 964 | {"_sigsegv", faulthandler_sigsegv, METH_VARARGS, |
| 965 | PyDoc_STR("_sigsegv(): raise a SIGSEGV signal")}, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 966 | {"_sigabrt", faulthandler_sigabrt, METH_VARARGS, |
| 967 | PyDoc_STR("_sigabrt(): raise a SIGABRT signal")}, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 968 | {"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS, |
| 969 | PyDoc_STR("_sigfpe(): raise a SIGFPE signal")}, |
| 970 | #ifdef SIGBUS |
| 971 | {"_sigbus", (PyCFunction)faulthandler_sigbus, METH_NOARGS, |
| 972 | PyDoc_STR("_sigbus(): raise a SIGBUS signal")}, |
| 973 | #endif |
| 974 | #ifdef SIGILL |
| 975 | {"_sigill", (PyCFunction)faulthandler_sigill, METH_NOARGS, |
| 976 | PyDoc_STR("_sigill(): raise a SIGILL signal")}, |
| 977 | #endif |
| 978 | {"_fatal_error", faulthandler_fatal_error_py, METH_VARARGS, |
| 979 | PyDoc_STR("_fatal_error(message): call Py_FatalError(message)")}, |
| 980 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
| 981 | {"_stack_overflow", (PyCFunction)faulthandler_stack_overflow, METH_NOARGS, |
| 982 | PyDoc_STR("_stack_overflow(): recursive call to raise a stack overflow")}, |
| 983 | #endif |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 984 | {NULL, NULL} /* sentinel */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 985 | }; |
| 986 | |
| 987 | static struct PyModuleDef module_def = { |
| 988 | PyModuleDef_HEAD_INIT, |
| 989 | "faulthandler", |
| 990 | module_doc, |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 991 | 0, /* non-negative size to be able to unload the module */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 992 | module_methods, |
| 993 | NULL, |
| 994 | faulthandler_traverse, |
| 995 | NULL, |
| 996 | NULL |
| 997 | }; |
| 998 | |
| 999 | PyMODINIT_FUNC |
| 1000 | PyInit_faulthandler(void) |
| 1001 | { |
| 1002 | return PyModule_Create(&module_def); |
| 1003 | } |
| 1004 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 1005 | /* Call faulthandler.enable() if the PYTHONFAULTHANDLER environment variable |
| 1006 | is defined, or if sys._xoptions has a 'faulthandler' key. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1007 | |
| 1008 | static int |
| 1009 | faulthandler_env_options(void) |
| 1010 | { |
| 1011 | PyObject *xoptions, *key, *module, *res; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1012 | |
| 1013 | if (!Py_GETENV("PYTHONFAULTHANDLER")) { |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1014 | int has_key; |
| 1015 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1016 | xoptions = PySys_GetXOptions(); |
| 1017 | if (xoptions == NULL) |
| 1018 | return -1; |
| 1019 | |
| 1020 | key = PyUnicode_FromString("faulthandler"); |
| 1021 | if (key == NULL) |
| 1022 | return -1; |
| 1023 | |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1024 | has_key = PyDict_Contains(xoptions, key); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1025 | Py_DECREF(key); |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1026 | if (!has_key) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1029 | |
| 1030 | module = PyImport_ImportModule("faulthandler"); |
| 1031 | if (module == NULL) { |
| 1032 | return -1; |
| 1033 | } |
| 1034 | res = PyObject_CallMethod(module, "enable", ""); |
| 1035 | Py_DECREF(module); |
| 1036 | if (res == NULL) |
| 1037 | return -1; |
| 1038 | Py_DECREF(res); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | int _PyFaulthandler_Init(void) |
| 1043 | { |
| 1044 | #ifdef HAVE_SIGALTSTACK |
| 1045 | int err; |
| 1046 | |
| 1047 | /* Try to allocate an alternate stack for faulthandler() signal handler to |
| 1048 | * be able to allocate memory on the stack, even on a stack overflow. If it |
| 1049 | * fails, ignore the error. */ |
| 1050 | stack.ss_flags = 0; |
| 1051 | stack.ss_size = SIGSTKSZ; |
| 1052 | stack.ss_sp = PyMem_Malloc(stack.ss_size); |
| 1053 | if (stack.ss_sp != NULL) { |
| 1054 | err = sigaltstack(&stack, NULL); |
| 1055 | if (err) { |
| 1056 | PyMem_Free(stack.ss_sp); |
| 1057 | stack.ss_sp = NULL; |
| 1058 | } |
| 1059 | } |
| 1060 | #endif |
| 1061 | #ifdef FAULTHANDLER_LATER |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1062 | thread.file = NULL; |
| 1063 | thread.cancel_event = PyThread_allocate_lock(); |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1064 | thread.running = PyThread_allocate_lock(); |
| 1065 | if (!thread.cancel_event || !thread.running) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1066 | PyErr_SetString(PyExc_RuntimeError, |
| 1067 | "could not allocate locks for faulthandler"); |
| 1068 | return -1; |
| 1069 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1070 | PyThread_acquire_lock(thread.cancel_event, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1071 | #endif |
| 1072 | |
| 1073 | return faulthandler_env_options(); |
| 1074 | } |
| 1075 | |
| 1076 | void _PyFaulthandler_Fini(void) |
| 1077 | { |
| 1078 | #ifdef FAULTHANDLER_USER |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1079 | unsigned int signum; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1080 | #endif |
| 1081 | |
| 1082 | #ifdef FAULTHANDLER_LATER |
| 1083 | /* later */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1084 | cancel_dump_tracebacks_later(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1085 | if (thread.cancel_event) { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1086 | PyThread_release_lock(thread.cancel_event); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1087 | PyThread_free_lock(thread.cancel_event); |
| 1088 | thread.cancel_event = NULL; |
| 1089 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1090 | if (thread.running) { |
| 1091 | PyThread_free_lock(thread.running); |
| 1092 | thread.running = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1093 | } |
| 1094 | #endif |
| 1095 | |
| 1096 | #ifdef FAULTHANDLER_USER |
| 1097 | /* user */ |
| 1098 | if (user_signals != NULL) { |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1099 | for (signum=0; signum < NSIG; signum++) |
| 1100 | faulthandler_unregister(&user_signals[signum], signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1101 | free(user_signals); |
| 1102 | user_signals = NULL; |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
| 1106 | /* fatal */ |
| 1107 | faulthandler_disable(); |
| 1108 | #ifdef HAVE_SIGALTSTACK |
| 1109 | if (stack.ss_sp != NULL) { |
| 1110 | PyMem_Free(stack.ss_sp); |
| 1111 | stack.ss_sp = NULL; |
| 1112 | } |
| 1113 | #endif |
| 1114 | } |