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