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 */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 82 | #ifndef NSIG |
| 83 | # if defined(_NSIG) |
| 84 | # define NSIG _NSIG /* For BSD/SysV */ |
| 85 | # elif defined(_SIGMAX) |
| 86 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
| 87 | # elif defined(SIGMAX) |
| 88 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
| 89 | # else |
| 90 | # define NSIG 64 /* Use a reasonable default value */ |
| 91 | # endif |
| 92 | #endif |
| 93 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 94 | static void faulthandler_user(int signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 95 | #endif /* FAULTHANDLER_USER */ |
| 96 | |
| 97 | |
| 98 | static fault_handler_t faulthandler_handlers[] = { |
| 99 | #ifdef SIGBUS |
| 100 | {SIGBUS, 0, "Bus error", }, |
| 101 | #endif |
| 102 | #ifdef SIGILL |
| 103 | {SIGILL, 0, "Illegal instruction", }, |
| 104 | #endif |
| 105 | {SIGFPE, 0, "Floating point exception", }, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 106 | {SIGABRT, 0, "Aborted", }, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 107 | /* define SIGSEGV at the end to make it the default choice if searching the |
| 108 | handler fails in faulthandler_fatal_error() */ |
| 109 | {SIGSEGV, 0, "Segmentation fault", } |
| 110 | }; |
| 111 | static const unsigned char faulthandler_nsignals = \ |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 112 | Py_ARRAY_LENGTH(faulthandler_handlers); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 113 | |
| 114 | #ifdef HAVE_SIGALTSTACK |
| 115 | static stack_t stack; |
| 116 | #endif |
| 117 | |
| 118 | |
| 119 | /* Get the file descriptor of a file by calling its fileno() method and then |
| 120 | call its flush() method. |
| 121 | |
| 122 | If file is NULL or Py_None, use sys.stderr as the new file. |
| 123 | |
| 124 | On success, return the new file and write the file descriptor into *p_fd. |
| 125 | On error, return NULL. */ |
| 126 | |
| 127 | static PyObject* |
| 128 | faulthandler_get_fileno(PyObject *file, int *p_fd) |
| 129 | { |
| 130 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 131 | _Py_IDENTIFIER(fileno); |
| 132 | _Py_IDENTIFIER(flush); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 144 | result = _PyObject_CallMethodId(file, &PyId_fileno, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 162 | result = _PyObject_CallMethodId(file, &PyId_flush, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 262 | (void)sigaction(signum, &handler->previous, NULL); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 263 | #else |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 264 | (void)signal(signum, handler->previous); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 462 | cancel_dump_traceback_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* |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 509 | faulthandler_dump_traceback_later(PyObject *self, |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 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, |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 524 | "d|iOi:dump_traceback_later", kwlist, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 */ |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 552 | cancel_dump_traceback_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* |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 582 | faulthandler_cancel_dump_traceback_later_py(PyObject *self) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 583 | { |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 584 | cancel_dump_traceback_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 |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 590 | static int |
| 591 | faulthandler_register(int signum, int chain, _Py_sighandler_t *p_previous) |
| 592 | { |
| 593 | #ifdef HAVE_SIGACTION |
| 594 | struct sigaction action; |
| 595 | action.sa_handler = faulthandler_user; |
| 596 | sigemptyset(&action.sa_mask); |
| 597 | /* if the signal is received while the kernel is executing a system |
| 598 | call, try to restart the system call instead of interrupting it and |
| 599 | return EINTR. */ |
| 600 | action.sa_flags = SA_RESTART; |
| 601 | if (chain) { |
| 602 | /* do not prevent the signal from being received from within its |
| 603 | own signal handler */ |
| 604 | action.sa_flags = SA_NODEFER; |
| 605 | } |
| 606 | #ifdef HAVE_SIGALTSTACK |
| 607 | if (stack.ss_sp != NULL) { |
| 608 | /* Call the signal handler on an alternate signal stack |
| 609 | provided by sigaltstack() */ |
| 610 | action.sa_flags |= SA_ONSTACK; |
| 611 | } |
| 612 | #endif |
| 613 | return sigaction(signum, &action, p_previous); |
| 614 | #else |
| 615 | _Py_sighandler_t previous; |
| 616 | previous = signal(signum, faulthandler_user); |
| 617 | if (p_previous != NULL) |
| 618 | *p_previous = previous; |
| 619 | return (previous == SIG_ERR); |
| 620 | #endif |
| 621 | } |
| 622 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 623 | /* Handler of user signals (e.g. SIGUSR1). |
| 624 | |
| 625 | Dump the traceback of the current thread, or of all threads if |
| 626 | thread.all_threads is true. |
| 627 | |
| 628 | This function is signal safe and should only call signal safe functions. */ |
| 629 | |
| 630 | static void |
| 631 | faulthandler_user(int signum) |
| 632 | { |
| 633 | user_signal_t *user; |
| 634 | PyThreadState *tstate; |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 635 | int save_errno = errno; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 636 | |
| 637 | user = &user_signals[signum]; |
| 638 | if (!user->enabled) |
| 639 | return; |
| 640 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 641 | #ifdef WITH_THREAD |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 642 | /* PyThreadState_Get() doesn't give the state of the current thread if |
| 643 | the thread doesn't hold the GIL. Read the thread local storage (TLS) |
| 644 | instead: call PyGILState_GetThisThreadState(). */ |
| 645 | tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 646 | #else |
| 647 | tstate = PyThreadState_Get(); |
| 648 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 649 | |
| 650 | if (user->all_threads) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 651 | _Py_DumpTracebackThreads(user->fd, user->interp, tstate); |
| 652 | else { |
Victor Stinner | 98a387b | 2012-08-01 19:36:36 +0200 | [diff] [blame] | 653 | if (tstate != NULL) |
| 654 | _Py_DumpTraceback(user->fd, tstate); |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 655 | } |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 656 | #ifdef HAVE_SIGACTION |
| 657 | if (user->chain) { |
| 658 | (void)sigaction(signum, &user->previous, NULL); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 659 | errno = save_errno; |
| 660 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 661 | /* call the previous signal handler */ |
| 662 | raise(signum); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 663 | |
| 664 | save_errno = errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 665 | (void)faulthandler_register(signum, user->chain, NULL); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 666 | errno = save_errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 667 | } |
| 668 | #else |
| 669 | if (user->chain) { |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 670 | errno = save_errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 671 | /* call the previous signal handler */ |
| 672 | user->previous(signum); |
| 673 | } |
| 674 | #endif |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static int |
| 678 | check_signum(int signum) |
| 679 | { |
| 680 | unsigned int i; |
| 681 | |
| 682 | for (i=0; i < faulthandler_nsignals; i++) { |
| 683 | if (faulthandler_handlers[i].signum == signum) { |
| 684 | PyErr_Format(PyExc_RuntimeError, |
| 685 | "signal %i cannot be registered, " |
| 686 | "use enable() instead", |
| 687 | signum); |
| 688 | return 0; |
| 689 | } |
| 690 | } |
| 691 | if (signum < 1 || NSIG <= signum) { |
| 692 | PyErr_SetString(PyExc_ValueError, "signal number out of range"); |
| 693 | return 0; |
| 694 | } |
| 695 | return 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static PyObject* |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 699 | faulthandler_register_py(PyObject *self, |
| 700 | PyObject *args, PyObject *kwargs) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 701 | { |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 702 | static char *kwlist[] = {"signum", "file", "all_threads", "chain", NULL}; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 703 | int signum; |
| 704 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 705 | int all_threads = 1; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 706 | int chain = 0; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 707 | int fd; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 708 | user_signal_t *user; |
| 709 | _Py_sighandler_t previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 710 | PyThreadState *tstate; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 711 | int err; |
| 712 | |
| 713 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 714 | "i|Oii:register", kwlist, |
| 715 | &signum, &file, &all_threads, &chain)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 716 | return NULL; |
| 717 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 718 | if (!check_signum(signum)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 719 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 720 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 721 | tstate = get_thread_state(); |
| 722 | if (tstate == NULL) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 723 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 724 | |
| 725 | file = faulthandler_get_fileno(file, &fd); |
| 726 | if (file == NULL) |
| 727 | return NULL; |
| 728 | |
| 729 | if (user_signals == NULL) { |
| 730 | user_signals = calloc(NSIG, sizeof(user_signal_t)); |
| 731 | if (user_signals == NULL) |
| 732 | return PyErr_NoMemory(); |
| 733 | } |
| 734 | user = &user_signals[signum]; |
| 735 | |
| 736 | if (!user->enabled) { |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 737 | err = faulthandler_register(signum, chain, &previous); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 738 | if (err) { |
| 739 | PyErr_SetFromErrno(PyExc_OSError); |
| 740 | return NULL; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | Py_XDECREF(user->file); |
| 745 | Py_INCREF(file); |
| 746 | user->file = file; |
| 747 | user->fd = fd; |
| 748 | user->all_threads = all_threads; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 749 | user->chain = chain; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 750 | user->previous = previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 751 | user->interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 752 | user->enabled = 1; |
| 753 | |
| 754 | Py_RETURN_NONE; |
| 755 | } |
| 756 | |
| 757 | static int |
| 758 | faulthandler_unregister(user_signal_t *user, int signum) |
| 759 | { |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 760 | if (!user->enabled) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 761 | return 0; |
| 762 | user->enabled = 0; |
| 763 | #ifdef HAVE_SIGACTION |
| 764 | (void)sigaction(signum, &user->previous, NULL); |
| 765 | #else |
| 766 | (void)signal(signum, user->previous); |
| 767 | #endif |
| 768 | Py_CLEAR(user->file); |
| 769 | user->fd = -1; |
| 770 | return 1; |
| 771 | } |
| 772 | |
| 773 | static PyObject* |
| 774 | faulthandler_unregister_py(PyObject *self, PyObject *args) |
| 775 | { |
| 776 | int signum; |
| 777 | user_signal_t *user; |
| 778 | int change; |
| 779 | |
| 780 | if (!PyArg_ParseTuple(args, "i:unregister", &signum)) |
| 781 | return NULL; |
| 782 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 783 | if (!check_signum(signum)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 784 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 785 | |
Victor Stinner | cfa7123 | 2011-04-08 12:48:15 +0200 | [diff] [blame] | 786 | if (user_signals == NULL) |
| 787 | Py_RETURN_FALSE; |
| 788 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 789 | user = &user_signals[signum]; |
| 790 | change = faulthandler_unregister(user, signum); |
| 791 | return PyBool_FromLong(change); |
| 792 | } |
| 793 | #endif /* FAULTHANDLER_USER */ |
| 794 | |
| 795 | |
| 796 | static PyObject * |
| 797 | faulthandler_read_null(PyObject *self, PyObject *args) |
| 798 | { |
Victor Stinner | a247720 | 2012-01-30 00:07:43 +0100 | [diff] [blame] | 799 | volatile int *x; |
| 800 | volatile int y; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 801 | int release_gil = 0; |
| 802 | if (!PyArg_ParseTuple(args, "|i:_read_null", &release_gil)) |
| 803 | return NULL; |
Victor Stinner | a247720 | 2012-01-30 00:07:43 +0100 | [diff] [blame] | 804 | |
| 805 | x = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 806 | if (release_gil) { |
| 807 | Py_BEGIN_ALLOW_THREADS |
| 808 | y = *x; |
| 809 | Py_END_ALLOW_THREADS |
| 810 | } else |
| 811 | y = *x; |
| 812 | return PyLong_FromLong(y); |
| 813 | |
| 814 | } |
| 815 | |
| 816 | static PyObject * |
| 817 | faulthandler_sigsegv(PyObject *self, PyObject *args) |
| 818 | { |
| 819 | #if defined(MS_WINDOWS) |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 820 | /* For SIGSEGV, faulthandler_fatal_error() restores the previous signal |
| 821 | handler and then gives back the execution flow to the program (without |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 822 | explicitly calling the previous error handler). In a normal case, the |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 823 | SIGSEGV was raised by the kernel because of a fault, and so if the |
| 824 | program retries to execute the same instruction, the fault will be |
| 825 | raised again. |
| 826 | |
| 827 | Here the fault is simulated by a fake SIGSEGV signal raised by the |
| 828 | application. We have to raise SIGSEGV at lease twice: once for |
| 829 | faulthandler_fatal_error(), and one more time for the previous signal |
| 830 | handler. */ |
| 831 | while(1) |
| 832 | raise(SIGSEGV); |
| 833 | #else |
| 834 | raise(SIGSEGV); |
| 835 | #endif |
| 836 | Py_RETURN_NONE; |
| 837 | } |
| 838 | |
| 839 | static PyObject * |
| 840 | faulthandler_sigfpe(PyObject *self, PyObject *args) |
| 841 | { |
| 842 | /* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on |
| 843 | PowerPC. Use volatile to disable compile-time optimizations. */ |
| 844 | volatile int x = 1, y = 0, z; |
| 845 | z = x / y; |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 846 | /* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC), |
| 847 | raise it manually. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 848 | raise(SIGFPE); |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 849 | /* This line is never reached, but we pretend to make something with z |
| 850 | to silence a compiler warning. */ |
Victor Stinner | e0c9a75 | 2011-05-09 14:44:26 +0200 | [diff] [blame] | 851 | return PyLong_FromLong(z); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 854 | static PyObject * |
| 855 | faulthandler_sigabrt(PyObject *self, PyObject *args) |
| 856 | { |
Victor Stinner | 00bc6cc | 2011-05-10 01:30:03 +0200 | [diff] [blame] | 857 | #ifdef _MSC_VER |
| 858 | /* Visual Studio: configure abort() to not display an error message nor |
| 859 | open a popup asking to report the fault. */ |
| 860 | _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 861 | #endif |
Victor Stinner | 00bc6cc | 2011-05-10 01:30:03 +0200 | [diff] [blame] | 862 | abort(); |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 863 | Py_RETURN_NONE; |
| 864 | } |
| 865 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 866 | #ifdef SIGBUS |
| 867 | static PyObject * |
| 868 | faulthandler_sigbus(PyObject *self, PyObject *args) |
| 869 | { |
| 870 | raise(SIGBUS); |
| 871 | Py_RETURN_NONE; |
| 872 | } |
| 873 | #endif |
| 874 | |
| 875 | #ifdef SIGILL |
| 876 | static PyObject * |
| 877 | faulthandler_sigill(PyObject *self, PyObject *args) |
| 878 | { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 879 | raise(SIGILL); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 880 | Py_RETURN_NONE; |
| 881 | } |
| 882 | #endif |
| 883 | |
| 884 | static PyObject * |
| 885 | faulthandler_fatal_error_py(PyObject *self, PyObject *args) |
| 886 | { |
| 887 | char *message; |
| 888 | if (!PyArg_ParseTuple(args, "y:fatal_error", &message)) |
| 889 | return NULL; |
| 890 | Py_FatalError(message); |
| 891 | Py_RETURN_NONE; |
| 892 | } |
| 893 | |
| 894 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
Victor Stinner | 9b49304 | 2011-05-23 12:29:10 +0200 | [diff] [blame] | 895 | static void* |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 896 | stack_overflow(void *min_sp, void *max_sp, size_t *depth) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 897 | { |
| 898 | /* allocate 4096 bytes on the stack at each call */ |
| 899 | unsigned char buffer[4096]; |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 900 | void *sp = &buffer; |
| 901 | *depth += 1; |
| 902 | if (sp < min_sp || max_sp < sp) |
| 903 | return sp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 904 | buffer[0] = 1; |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 905 | buffer[4095] = 0; |
| 906 | return stack_overflow(min_sp, max_sp, depth); |
| 907 | } |
| 908 | |
| 909 | static PyObject * |
| 910 | faulthandler_stack_overflow(PyObject *self) |
| 911 | { |
| 912 | size_t depth, size; |
Victor Stinner | 425fcd3 | 2011-09-07 16:18:56 +0200 | [diff] [blame] | 913 | char *sp = (char *)&depth, *stop; |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 914 | |
| 915 | depth = 0; |
| 916 | stop = stack_overflow(sp - STACK_OVERFLOW_MAX_SIZE, |
| 917 | sp + STACK_OVERFLOW_MAX_SIZE, |
| 918 | &depth); |
| 919 | if (sp < stop) |
| 920 | size = stop - sp; |
| 921 | else |
| 922 | size = sp - stop; |
| 923 | PyErr_Format(PyExc_RuntimeError, |
| 924 | "unable to raise a stack overflow (allocated %zu bytes " |
| 925 | "on the stack, %zu recursive calls)", |
| 926 | size, depth); |
| 927 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 928 | } |
| 929 | #endif |
| 930 | |
| 931 | |
| 932 | static int |
| 933 | faulthandler_traverse(PyObject *module, visitproc visit, void *arg) |
| 934 | { |
| 935 | #ifdef FAULTHANDLER_USER |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 936 | unsigned int signum; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 937 | #endif |
| 938 | |
| 939 | #ifdef FAULTHANDLER_LATER |
| 940 | Py_VISIT(thread.file); |
| 941 | #endif |
| 942 | #ifdef FAULTHANDLER_USER |
| 943 | if (user_signals != NULL) { |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 944 | for (signum=0; signum < NSIG; signum++) |
| 945 | Py_VISIT(user_signals[signum].file); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 946 | } |
| 947 | #endif |
| 948 | Py_VISIT(fatal_error.file); |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | PyDoc_STRVAR(module_doc, |
| 953 | "faulthandler module."); |
| 954 | |
| 955 | static PyMethodDef module_methods[] = { |
| 956 | {"enable", |
| 957 | (PyCFunction)faulthandler_enable, METH_VARARGS|METH_KEYWORDS, |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 958 | PyDoc_STR("enable(file=sys.stderr, all_threads=True): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 959 | "enable the fault handler")}, |
| 960 | {"disable", (PyCFunction)faulthandler_disable_py, METH_NOARGS, |
| 961 | PyDoc_STR("disable(): disable the fault handler")}, |
| 962 | {"is_enabled", (PyCFunction)faulthandler_is_enabled, METH_NOARGS, |
| 963 | PyDoc_STR("is_enabled()->bool: check if the handler is enabled")}, |
| 964 | {"dump_traceback", |
| 965 | (PyCFunction)faulthandler_dump_traceback_py, METH_VARARGS|METH_KEYWORDS, |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 966 | PyDoc_STR("dump_traceback(file=sys.stderr, all_threads=True): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 967 | "dump the traceback of the current thread, or of all threads " |
| 968 | "if all_threads is True, into file")}, |
| 969 | #ifdef FAULTHANDLER_LATER |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 970 | {"dump_traceback_later", |
| 971 | (PyCFunction)faulthandler_dump_traceback_later, METH_VARARGS|METH_KEYWORDS, |
| 972 | 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] | 973 | "dump the traceback of all threads in timeout seconds,\n" |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 974 | "or each timeout seconds if repeat is True. If exit is True, " |
| 975 | "call _exit(1) which is not safe.")}, |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 976 | {"cancel_dump_traceback_later", |
| 977 | (PyCFunction)faulthandler_cancel_dump_traceback_later_py, METH_NOARGS, |
| 978 | PyDoc_STR("cancel_dump_traceback_later():\ncancel the previous call " |
| 979 | "to dump_traceback_later().")}, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 980 | #endif |
| 981 | |
| 982 | #ifdef FAULTHANDLER_USER |
| 983 | {"register", |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 984 | (PyCFunction)faulthandler_register_py, METH_VARARGS|METH_KEYWORDS, |
| 985 | PyDoc_STR("register(signum, file=sys.stderr, all_threads=True, chain=False): " |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 986 | "register an handler for the signal 'signum': dump the " |
| 987 | "traceback of the current thread, or of all threads if " |
| 988 | "all_threads is True, into file")}, |
| 989 | {"unregister", |
| 990 | faulthandler_unregister_py, METH_VARARGS|METH_KEYWORDS, |
| 991 | PyDoc_STR("unregister(signum): unregister the handler of the signal " |
| 992 | "'signum' registered by register()")}, |
| 993 | #endif |
| 994 | |
| 995 | {"_read_null", faulthandler_read_null, METH_VARARGS, |
| 996 | PyDoc_STR("_read_null(release_gil=False): read from NULL, raise " |
| 997 | "a SIGSEGV or SIGBUS signal depending on the platform")}, |
| 998 | {"_sigsegv", faulthandler_sigsegv, METH_VARARGS, |
| 999 | PyDoc_STR("_sigsegv(): raise a SIGSEGV signal")}, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 1000 | {"_sigabrt", faulthandler_sigabrt, METH_VARARGS, |
| 1001 | PyDoc_STR("_sigabrt(): raise a SIGABRT signal")}, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1002 | {"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS, |
| 1003 | PyDoc_STR("_sigfpe(): raise a SIGFPE signal")}, |
| 1004 | #ifdef SIGBUS |
| 1005 | {"_sigbus", (PyCFunction)faulthandler_sigbus, METH_NOARGS, |
| 1006 | PyDoc_STR("_sigbus(): raise a SIGBUS signal")}, |
| 1007 | #endif |
| 1008 | #ifdef SIGILL |
| 1009 | {"_sigill", (PyCFunction)faulthandler_sigill, METH_NOARGS, |
| 1010 | PyDoc_STR("_sigill(): raise a SIGILL signal")}, |
| 1011 | #endif |
| 1012 | {"_fatal_error", faulthandler_fatal_error_py, METH_VARARGS, |
| 1013 | PyDoc_STR("_fatal_error(message): call Py_FatalError(message)")}, |
| 1014 | #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) |
| 1015 | {"_stack_overflow", (PyCFunction)faulthandler_stack_overflow, METH_NOARGS, |
| 1016 | PyDoc_STR("_stack_overflow(): recursive call to raise a stack overflow")}, |
| 1017 | #endif |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 1018 | {NULL, NULL} /* sentinel */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1019 | }; |
| 1020 | |
| 1021 | static struct PyModuleDef module_def = { |
| 1022 | PyModuleDef_HEAD_INIT, |
| 1023 | "faulthandler", |
| 1024 | module_doc, |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 1025 | 0, /* non-negative size to be able to unload the module */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1026 | module_methods, |
| 1027 | NULL, |
| 1028 | faulthandler_traverse, |
| 1029 | NULL, |
| 1030 | NULL |
| 1031 | }; |
| 1032 | |
| 1033 | PyMODINIT_FUNC |
| 1034 | PyInit_faulthandler(void) |
| 1035 | { |
| 1036 | return PyModule_Create(&module_def); |
| 1037 | } |
| 1038 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 1039 | /* Call faulthandler.enable() if the PYTHONFAULTHANDLER environment variable |
| 1040 | is defined, or if sys._xoptions has a 'faulthandler' key. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1041 | |
| 1042 | static int |
| 1043 | faulthandler_env_options(void) |
| 1044 | { |
| 1045 | PyObject *xoptions, *key, *module, *res; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1046 | _Py_IDENTIFIER(enable); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1047 | |
| 1048 | if (!Py_GETENV("PYTHONFAULTHANDLER")) { |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1049 | int has_key; |
| 1050 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1051 | xoptions = PySys_GetXOptions(); |
| 1052 | if (xoptions == NULL) |
| 1053 | return -1; |
| 1054 | |
| 1055 | key = PyUnicode_FromString("faulthandler"); |
| 1056 | if (key == NULL) |
| 1057 | return -1; |
| 1058 | |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1059 | has_key = PyDict_Contains(xoptions, key); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1060 | Py_DECREF(key); |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1061 | if (!has_key) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1064 | |
| 1065 | module = PyImport_ImportModule("faulthandler"); |
| 1066 | if (module == NULL) { |
| 1067 | return -1; |
| 1068 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1069 | res = _PyObject_CallMethodId(module, &PyId_enable, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1070 | Py_DECREF(module); |
| 1071 | if (res == NULL) |
| 1072 | return -1; |
| 1073 | Py_DECREF(res); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | int _PyFaulthandler_Init(void) |
| 1078 | { |
| 1079 | #ifdef HAVE_SIGALTSTACK |
| 1080 | int err; |
| 1081 | |
| 1082 | /* Try to allocate an alternate stack for faulthandler() signal handler to |
| 1083 | * be able to allocate memory on the stack, even on a stack overflow. If it |
| 1084 | * fails, ignore the error. */ |
| 1085 | stack.ss_flags = 0; |
| 1086 | stack.ss_size = SIGSTKSZ; |
| 1087 | stack.ss_sp = PyMem_Malloc(stack.ss_size); |
| 1088 | if (stack.ss_sp != NULL) { |
| 1089 | err = sigaltstack(&stack, NULL); |
| 1090 | if (err) { |
| 1091 | PyMem_Free(stack.ss_sp); |
| 1092 | stack.ss_sp = NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | #endif |
| 1096 | #ifdef FAULTHANDLER_LATER |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1097 | thread.file = NULL; |
| 1098 | thread.cancel_event = PyThread_allocate_lock(); |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1099 | thread.running = PyThread_allocate_lock(); |
| 1100 | if (!thread.cancel_event || !thread.running) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1101 | PyErr_SetString(PyExc_RuntimeError, |
| 1102 | "could not allocate locks for faulthandler"); |
| 1103 | return -1; |
| 1104 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1105 | PyThread_acquire_lock(thread.cancel_event, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1106 | #endif |
| 1107 | |
| 1108 | return faulthandler_env_options(); |
| 1109 | } |
| 1110 | |
| 1111 | void _PyFaulthandler_Fini(void) |
| 1112 | { |
| 1113 | #ifdef FAULTHANDLER_USER |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1114 | unsigned int signum; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1115 | #endif |
| 1116 | |
| 1117 | #ifdef FAULTHANDLER_LATER |
| 1118 | /* later */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1119 | if (thread.cancel_event) { |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 1120 | cancel_dump_traceback_later(); |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1121 | PyThread_release_lock(thread.cancel_event); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1122 | PyThread_free_lock(thread.cancel_event); |
| 1123 | thread.cancel_event = NULL; |
| 1124 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1125 | if (thread.running) { |
| 1126 | PyThread_free_lock(thread.running); |
| 1127 | thread.running = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1128 | } |
| 1129 | #endif |
| 1130 | |
| 1131 | #ifdef FAULTHANDLER_USER |
| 1132 | /* user */ |
| 1133 | if (user_signals != NULL) { |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1134 | for (signum=0; signum < NSIG; signum++) |
| 1135 | faulthandler_unregister(&user_signals[signum], signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1136 | free(user_signals); |
| 1137 | user_signals = NULL; |
| 1138 | } |
| 1139 | #endif |
| 1140 | |
| 1141 | /* fatal */ |
| 1142 | faulthandler_disable(); |
| 1143 | #ifdef HAVE_SIGALTSTACK |
| 1144 | if (stack.ss_sp != NULL) { |
| 1145 | PyMem_Free(stack.ss_sp); |
| 1146 | stack.ss_sp = NULL; |
| 1147 | } |
| 1148 | #endif |
| 1149 | } |