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 | |
Victor Stinner | 56cb125 | 2012-10-31 00:33:57 +0100 | [diff] [blame] | 25 | /* cast size_t to int because write() takes an int on Windows |
| 26 | (anyway, the length is smaller than 30 characters) */ |
| 27 | #define PUTS(fd, str) write(fd, str, (int)strlen(str)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_SIGACTION |
| 30 | typedef struct sigaction _Py_sighandler_t; |
| 31 | #else |
| 32 | typedef PyOS_sighandler_t _Py_sighandler_t; |
| 33 | #endif |
| 34 | |
| 35 | typedef struct { |
| 36 | int signum; |
| 37 | int enabled; |
| 38 | const char* name; |
| 39 | _Py_sighandler_t previous; |
| 40 | int all_threads; |
| 41 | } fault_handler_t; |
| 42 | |
| 43 | static struct { |
| 44 | int enabled; |
| 45 | PyObject *file; |
| 46 | int fd; |
| 47 | int all_threads; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 48 | PyInterpreterState *interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 49 | } fatal_error = {0, NULL, -1, 0}; |
| 50 | |
| 51 | #ifdef FAULTHANDLER_LATER |
| 52 | static struct { |
| 53 | PyObject *file; |
| 54 | int fd; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 55 | PY_TIMEOUT_T timeout_us; /* timeout in microseconds */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 56 | int repeat; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 57 | PyInterpreterState *interp; |
| 58 | int exit; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 59 | char *header; |
| 60 | size_t header_len; |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 61 | /* The main thread always holds this lock. It is only released when |
| 62 | faulthandler_thread() is interrupted before this thread exits, or at |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 63 | Python exit. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 64 | PyThread_type_lock cancel_event; |
| 65 | /* released by child thread when joined */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 66 | PyThread_type_lock running; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 67 | } thread; |
| 68 | #endif |
| 69 | |
| 70 | #ifdef FAULTHANDLER_USER |
| 71 | typedef struct { |
| 72 | int enabled; |
| 73 | PyObject *file; |
| 74 | int fd; |
| 75 | int all_threads; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 76 | int chain; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 77 | _Py_sighandler_t previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 78 | PyInterpreterState *interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 79 | } user_signal_t; |
| 80 | |
| 81 | static user_signal_t *user_signals; |
| 82 | |
| 83 | /* the following macros come from Python: Modules/signalmodule.c */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 84 | #ifndef NSIG |
| 85 | # if defined(_NSIG) |
| 86 | # define NSIG _NSIG /* For BSD/SysV */ |
| 87 | # elif defined(_SIGMAX) |
| 88 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
| 89 | # elif defined(SIGMAX) |
| 90 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
| 91 | # else |
| 92 | # define NSIG 64 /* Use a reasonable default value */ |
| 93 | # endif |
| 94 | #endif |
| 95 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 96 | static void faulthandler_user(int signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 97 | #endif /* FAULTHANDLER_USER */ |
| 98 | |
| 99 | |
| 100 | static fault_handler_t faulthandler_handlers[] = { |
| 101 | #ifdef SIGBUS |
| 102 | {SIGBUS, 0, "Bus error", }, |
| 103 | #endif |
| 104 | #ifdef SIGILL |
| 105 | {SIGILL, 0, "Illegal instruction", }, |
| 106 | #endif |
| 107 | {SIGFPE, 0, "Floating point exception", }, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 108 | {SIGABRT, 0, "Aborted", }, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 109 | /* define SIGSEGV at the end to make it the default choice if searching the |
| 110 | handler fails in faulthandler_fatal_error() */ |
| 111 | {SIGSEGV, 0, "Segmentation fault", } |
| 112 | }; |
| 113 | static const unsigned char faulthandler_nsignals = \ |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 114 | Py_ARRAY_LENGTH(faulthandler_handlers); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 115 | |
| 116 | #ifdef HAVE_SIGALTSTACK |
| 117 | static stack_t stack; |
| 118 | #endif |
| 119 | |
| 120 | |
| 121 | /* Get the file descriptor of a file by calling its fileno() method and then |
| 122 | call its flush() method. |
| 123 | |
| 124 | If file is NULL or Py_None, use sys.stderr as the new file. |
| 125 | |
| 126 | On success, return the new file and write the file descriptor into *p_fd. |
| 127 | On error, return NULL. */ |
| 128 | |
| 129 | static PyObject* |
| 130 | faulthandler_get_fileno(PyObject *file, int *p_fd) |
| 131 | { |
| 132 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 133 | _Py_IDENTIFIER(fileno); |
| 134 | _Py_IDENTIFIER(flush); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 135 | long fd_long; |
| 136 | int fd; |
| 137 | |
| 138 | if (file == NULL || file == Py_None) { |
| 139 | file = PySys_GetObject("stderr"); |
| 140 | if (file == NULL) { |
| 141 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr"); |
| 142 | return NULL; |
| 143 | } |
| 144 | } |
| 145 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 146 | result = _PyObject_CallMethodId(file, &PyId_fileno, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 147 | if (result == NULL) |
| 148 | return NULL; |
| 149 | |
| 150 | fd = -1; |
| 151 | if (PyLong_Check(result)) { |
| 152 | fd_long = PyLong_AsLong(result); |
| 153 | if (0 <= fd_long && fd_long < INT_MAX) |
| 154 | fd = (int)fd_long; |
| 155 | } |
| 156 | Py_DECREF(result); |
| 157 | |
| 158 | if (fd == -1) { |
| 159 | PyErr_SetString(PyExc_RuntimeError, |
| 160 | "file.fileno() is not a valid file descriptor"); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 164 | result = _PyObject_CallMethodId(file, &PyId_flush, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 165 | if (result != NULL) |
| 166 | Py_DECREF(result); |
| 167 | else { |
| 168 | /* ignore flush() error */ |
| 169 | PyErr_Clear(); |
| 170 | } |
| 171 | *p_fd = fd; |
| 172 | return file; |
| 173 | } |
| 174 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 175 | /* Get the state of the current thread: only call this function if the current |
| 176 | thread holds the GIL. Raise an exception on error. */ |
| 177 | static PyThreadState* |
| 178 | get_thread_state(void) |
| 179 | { |
| 180 | PyThreadState *tstate = PyThreadState_Get(); |
| 181 | if (tstate == NULL) { |
| 182 | PyErr_SetString(PyExc_RuntimeError, |
| 183 | "unable to get the current thread state"); |
| 184 | return NULL; |
| 185 | } |
| 186 | return tstate; |
| 187 | } |
| 188 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 189 | static PyObject* |
| 190 | faulthandler_dump_traceback_py(PyObject *self, |
| 191 | PyObject *args, PyObject *kwargs) |
| 192 | { |
| 193 | static char *kwlist[] = {"file", "all_threads", NULL}; |
| 194 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 195 | int all_threads = 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 196 | PyThreadState *tstate; |
| 197 | const char *errmsg; |
| 198 | int fd; |
| 199 | |
| 200 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 201 | "|Oi:dump_traceback", kwlist, |
| 202 | &file, &all_threads)) |
| 203 | return NULL; |
| 204 | |
| 205 | file = faulthandler_get_fileno(file, &fd); |
| 206 | if (file == NULL) |
| 207 | return NULL; |
| 208 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 209 | tstate = get_thread_state(); |
| 210 | if (tstate == NULL) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 211 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 212 | |
| 213 | if (all_threads) { |
| 214 | errmsg = _Py_DumpTracebackThreads(fd, tstate->interp, tstate); |
| 215 | if (errmsg != NULL) { |
| 216 | PyErr_SetString(PyExc_RuntimeError, errmsg); |
| 217 | return NULL; |
| 218 | } |
| 219 | } |
| 220 | else { |
| 221 | _Py_DumpTraceback(fd, tstate); |
| 222 | } |
| 223 | Py_RETURN_NONE; |
| 224 | } |
| 225 | |
| 226 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 227 | /* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 228 | |
| 229 | Display the current Python traceback, restore the previous handler and call |
| 230 | the previous handler. |
| 231 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 232 | On Windows, don't explicitly call the previous handler, because the Windows |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 233 | signal handler would not be called (for an unknown reason). The execution of |
| 234 | the program continues at faulthandler_fatal_error() exit, but the same |
| 235 | instruction will raise the same fault (signal), and so the previous handler |
| 236 | will be called. |
| 237 | |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 238 | This function is signal-safe and should only call signal-safe functions. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 239 | |
| 240 | static void |
Victor Stinner | 44e31ba | 2011-04-07 11:39:03 +0200 | [diff] [blame] | 241 | faulthandler_fatal_error(int signum) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 242 | { |
| 243 | const int fd = fatal_error.fd; |
| 244 | unsigned int i; |
| 245 | fault_handler_t *handler = NULL; |
| 246 | PyThreadState *tstate; |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 247 | int save_errno = errno; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 248 | |
| 249 | if (!fatal_error.enabled) |
| 250 | return; |
| 251 | |
| 252 | for (i=0; i < faulthandler_nsignals; i++) { |
| 253 | handler = &faulthandler_handlers[i]; |
| 254 | if (handler->signum == signum) |
| 255 | break; |
| 256 | } |
| 257 | if (handler == NULL) { |
| 258 | /* faulthandler_nsignals == 0 (unlikely) */ |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | /* restore the previous handler */ |
| 263 | #ifdef HAVE_SIGACTION |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 264 | (void)sigaction(signum, &handler->previous, NULL); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 265 | #else |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 266 | (void)signal(signum, handler->previous); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 267 | #endif |
| 268 | handler->enabled = 0; |
| 269 | |
| 270 | PUTS(fd, "Fatal Python error: "); |
| 271 | PUTS(fd, handler->name); |
| 272 | PUTS(fd, "\n\n"); |
| 273 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 274 | #ifdef WITH_THREAD |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 275 | /* SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals and |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 276 | 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] | 277 | thread state of the current thread. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 278 | |
| 279 | PyThreadState_Get() doesn't give the state of the thread that caused the |
| 280 | fault if the thread released the GIL, and so this function cannot be |
| 281 | used. Read the thread local storage (TLS) instead: call |
| 282 | PyGILState_GetThisThreadState(). */ |
| 283 | tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 284 | #else |
| 285 | tstate = PyThreadState_Get(); |
| 286 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 287 | |
| 288 | if (fatal_error.all_threads) |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 289 | _Py_DumpTracebackThreads(fd, fatal_error.interp, tstate); |
| 290 | else { |
| 291 | if (tstate != NULL) |
| 292 | _Py_DumpTraceback(fd, tstate); |
| 293 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 294 | |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 295 | errno = save_errno; |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 296 | #ifdef MS_WINDOWS |
| 297 | if (signum == SIGSEGV) { |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 298 | /* don't explicitly call the previous handler for SIGSEGV in this signal |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 299 | handler, because the Windows signal handler would not be called */ |
| 300 | return; |
| 301 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 302 | #endif |
Victor Stinner | bc6a4db | 2011-04-01 12:08:57 +0200 | [diff] [blame] | 303 | /* call the previous signal handler: it is called immediatly if we use |
| 304 | sigaction() thanks to SA_NODEFER flag, otherwise it is deferred */ |
| 305 | raise(signum); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 308 | /* Install the handler for fatal signals, faulthandler_fatal_error(). */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 309 | |
| 310 | static PyObject* |
| 311 | faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs) |
| 312 | { |
| 313 | static char *kwlist[] = {"file", "all_threads", NULL}; |
| 314 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 315 | int all_threads = 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 316 | unsigned int i; |
| 317 | fault_handler_t *handler; |
| 318 | #ifdef HAVE_SIGACTION |
| 319 | struct sigaction action; |
| 320 | #endif |
| 321 | int err; |
| 322 | int fd; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 323 | PyThreadState *tstate; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 324 | |
| 325 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 326 | "|Oi:enable", kwlist, &file, &all_threads)) |
| 327 | return NULL; |
| 328 | |
| 329 | file = faulthandler_get_fileno(file, &fd); |
| 330 | if (file == NULL) |
| 331 | return NULL; |
| 332 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 333 | tstate = get_thread_state(); |
| 334 | if (tstate == NULL) |
| 335 | return NULL; |
| 336 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 337 | Py_XDECREF(fatal_error.file); |
| 338 | Py_INCREF(file); |
| 339 | fatal_error.file = file; |
| 340 | fatal_error.fd = fd; |
| 341 | fatal_error.all_threads = all_threads; |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 342 | fatal_error.interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 343 | |
| 344 | if (!fatal_error.enabled) { |
| 345 | fatal_error.enabled = 1; |
| 346 | |
| 347 | for (i=0; i < faulthandler_nsignals; i++) { |
| 348 | handler = &faulthandler_handlers[i]; |
| 349 | #ifdef HAVE_SIGACTION |
Victor Stinner | 44e31ba | 2011-04-07 11:39:03 +0200 | [diff] [blame] | 350 | action.sa_handler = faulthandler_fatal_error; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 351 | sigemptyset(&action.sa_mask); |
| 352 | /* Do not prevent the signal from being received from within |
| 353 | its own signal handler */ |
| 354 | action.sa_flags = SA_NODEFER; |
| 355 | #ifdef HAVE_SIGALTSTACK |
| 356 | if (stack.ss_sp != NULL) { |
| 357 | /* Call the signal handler on an alternate signal stack |
| 358 | provided by sigaltstack() */ |
| 359 | action.sa_flags |= SA_ONSTACK; |
| 360 | } |
| 361 | #endif |
| 362 | err = sigaction(handler->signum, &action, &handler->previous); |
| 363 | #else |
| 364 | handler->previous = signal(handler->signum, |
| 365 | faulthandler_fatal_error); |
| 366 | err = (handler->previous == SIG_ERR); |
| 367 | #endif |
| 368 | if (err) { |
| 369 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 370 | return NULL; |
| 371 | } |
| 372 | handler->enabled = 1; |
| 373 | } |
| 374 | } |
| 375 | Py_RETURN_NONE; |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | faulthandler_disable(void) |
| 380 | { |
| 381 | unsigned int i; |
| 382 | fault_handler_t *handler; |
| 383 | |
| 384 | if (fatal_error.enabled) { |
| 385 | fatal_error.enabled = 0; |
| 386 | for (i=0; i < faulthandler_nsignals; i++) { |
| 387 | handler = &faulthandler_handlers[i]; |
| 388 | if (!handler->enabled) |
| 389 | continue; |
| 390 | #ifdef HAVE_SIGACTION |
| 391 | (void)sigaction(handler->signum, &handler->previous, NULL); |
| 392 | #else |
| 393 | (void)signal(handler->signum, handler->previous); |
| 394 | #endif |
| 395 | handler->enabled = 0; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | Py_CLEAR(fatal_error.file); |
| 400 | } |
| 401 | |
| 402 | static PyObject* |
| 403 | faulthandler_disable_py(PyObject *self) |
| 404 | { |
| 405 | if (!fatal_error.enabled) { |
| 406 | Py_INCREF(Py_False); |
| 407 | return Py_False; |
| 408 | } |
| 409 | faulthandler_disable(); |
| 410 | Py_INCREF(Py_True); |
| 411 | return Py_True; |
| 412 | } |
| 413 | |
| 414 | static PyObject* |
| 415 | faulthandler_is_enabled(PyObject *self) |
| 416 | { |
| 417 | return PyBool_FromLong(fatal_error.enabled); |
| 418 | } |
| 419 | |
| 420 | #ifdef FAULTHANDLER_LATER |
| 421 | |
| 422 | static void |
| 423 | faulthandler_thread(void *unused) |
| 424 | { |
| 425 | PyLockStatus st; |
| 426 | const char* errmsg; |
| 427 | PyThreadState *current; |
| 428 | int ok; |
Victor Stinner | cf2a807 | 2011-04-19 23:30:57 +0200 | [diff] [blame] | 429 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 430 | sigset_t set; |
| 431 | |
| 432 | /* we don't want to receive any signal */ |
| 433 | sigfillset(&set); |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 434 | pthread_sigmask(SIG_SETMASK, &set, NULL); |
Victor Stinner | da9edae | 2011-04-04 11:05:21 +0200 | [diff] [blame] | 435 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 436 | |
| 437 | do { |
| 438 | st = PyThread_acquire_lock_timed(thread.cancel_event, |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 439 | thread.timeout_us, 0); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 440 | if (st == PY_LOCK_ACQUIRED) { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 441 | PyThread_release_lock(thread.cancel_event); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | /* Timeout => dump traceback */ |
| 445 | assert(st == PY_LOCK_FAILURE); |
| 446 | |
| 447 | /* get the thread holding the GIL, NULL if no thread hold the GIL */ |
| 448 | current = _Py_atomic_load_relaxed(&_PyThreadState_Current); |
| 449 | |
Victor Stinner | 56cb125 | 2012-10-31 00:33:57 +0100 | [diff] [blame] | 450 | write(thread.fd, thread.header, (int)thread.header_len); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 451 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 452 | errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current); |
| 453 | ok = (errmsg == NULL); |
| 454 | |
| 455 | if (thread.exit) |
| 456 | _exit(1); |
| 457 | } while (ok && thread.repeat); |
| 458 | |
| 459 | /* The only way out */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 460 | PyThread_release_lock(thread.running); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static void |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 464 | cancel_dump_traceback_later(void) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 465 | { |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 466 | /* Notify cancellation */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 467 | PyThread_release_lock(thread.cancel_event); |
| 468 | |
Victor Stinner | a4d4f1b | 2011-04-01 03:00:05 +0200 | [diff] [blame] | 469 | /* Wait for thread to join */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 470 | PyThread_acquire_lock(thread.running, 1); |
| 471 | PyThread_release_lock(thread.running); |
| 472 | |
| 473 | /* The main thread should always hold the cancel_event lock */ |
| 474 | PyThread_acquire_lock(thread.cancel_event, 1); |
| 475 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 476 | Py_CLEAR(thread.file); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 477 | if (thread.header) { |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 478 | PyMem_Free(thread.header); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 479 | thread.header = NULL; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static char* |
| 484 | format_timeout(double timeout) |
| 485 | { |
| 486 | unsigned long us, sec, min, hour; |
| 487 | double intpart, fracpart; |
| 488 | char buffer[100]; |
| 489 | |
| 490 | fracpart = modf(timeout, &intpart); |
| 491 | sec = (unsigned long)intpart; |
| 492 | us = (unsigned long)(fracpart * 1e6); |
| 493 | min = sec / 60; |
| 494 | sec %= 60; |
| 495 | hour = min / 60; |
| 496 | min %= 60; |
| 497 | |
| 498 | if (us != 0) |
| 499 | PyOS_snprintf(buffer, sizeof(buffer), |
| 500 | "Timeout (%lu:%02lu:%02lu.%06lu)!\n", |
| 501 | hour, min, sec, us); |
| 502 | else |
| 503 | PyOS_snprintf(buffer, sizeof(buffer), |
| 504 | "Timeout (%lu:%02lu:%02lu)!\n", |
| 505 | hour, min, sec); |
| 506 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 507 | return _PyMem_Strdup(buffer); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static PyObject* |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 511 | faulthandler_dump_traceback_later(PyObject *self, |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 512 | PyObject *args, PyObject *kwargs) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 513 | { |
| 514 | static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL}; |
| 515 | double timeout; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 516 | PY_TIMEOUT_T timeout_us; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 517 | int repeat = 0; |
| 518 | PyObject *file = NULL; |
| 519 | int fd; |
| 520 | int exit = 0; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 521 | PyThreadState *tstate; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 522 | char *header; |
| 523 | size_t header_len; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 524 | |
| 525 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 526 | "d|iOi:dump_traceback_later", kwlist, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 527 | &timeout, &repeat, &file, &exit)) |
| 528 | return NULL; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 529 | if ((timeout * 1e6) >= (double) PY_TIMEOUT_MAX) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 530 | PyErr_SetString(PyExc_OverflowError, "timeout value is too large"); |
| 531 | return NULL; |
| 532 | } |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 533 | timeout_us = (PY_TIMEOUT_T)(timeout * 1e6); |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 534 | if (timeout_us <= 0) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 535 | PyErr_SetString(PyExc_ValueError, "timeout must be greater than 0"); |
| 536 | return NULL; |
| 537 | } |
| 538 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 539 | tstate = get_thread_state(); |
| 540 | if (tstate == NULL) |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 541 | return NULL; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 542 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 543 | file = faulthandler_get_fileno(file, &fd); |
| 544 | if (file == NULL) |
| 545 | return NULL; |
| 546 | |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 547 | /* format the timeout */ |
| 548 | header = format_timeout(timeout); |
| 549 | if (header == NULL) |
| 550 | return PyErr_NoMemory(); |
| 551 | header_len = strlen(header); |
| 552 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 553 | /* Cancel previous thread, if running */ |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 554 | cancel_dump_traceback_later(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 555 | |
| 556 | Py_XDECREF(thread.file); |
| 557 | Py_INCREF(file); |
| 558 | thread.file = file; |
| 559 | thread.fd = fd; |
Victor Stinner | 9418932 | 2011-04-08 13:00:31 +0200 | [diff] [blame] | 560 | thread.timeout_us = timeout_us; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 561 | thread.repeat = repeat; |
Victor Stinner | 9699440 | 2011-04-07 11:37:19 +0200 | [diff] [blame] | 562 | thread.interp = tstate->interp; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 563 | thread.exit = exit; |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 564 | thread.header = header; |
| 565 | thread.header_len = header_len; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 566 | |
| 567 | /* Arm these locks to serve as events when released */ |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 568 | PyThread_acquire_lock(thread.running, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 569 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 570 | if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) { |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 571 | PyThread_release_lock(thread.running); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 572 | Py_CLEAR(thread.file); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 573 | PyMem_Free(header); |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 574 | thread.header = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 575 | PyErr_SetString(PyExc_RuntimeError, |
| 576 | "unable to start watchdog thread"); |
| 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | Py_RETURN_NONE; |
| 581 | } |
| 582 | |
| 583 | static PyObject* |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 584 | faulthandler_cancel_dump_traceback_later_py(PyObject *self) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 585 | { |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 586 | cancel_dump_traceback_later(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 587 | Py_RETURN_NONE; |
| 588 | } |
Victor Stinner | 410dd7d | 2011-05-11 20:56:08 +0200 | [diff] [blame] | 589 | #endif /* FAULTHANDLER_LATER */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 590 | |
| 591 | #ifdef FAULTHANDLER_USER |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 592 | static int |
| 593 | faulthandler_register(int signum, int chain, _Py_sighandler_t *p_previous) |
| 594 | { |
| 595 | #ifdef HAVE_SIGACTION |
| 596 | struct sigaction action; |
| 597 | action.sa_handler = faulthandler_user; |
| 598 | sigemptyset(&action.sa_mask); |
| 599 | /* if the signal is received while the kernel is executing a system |
| 600 | call, try to restart the system call instead of interrupting it and |
| 601 | return EINTR. */ |
| 602 | action.sa_flags = SA_RESTART; |
| 603 | if (chain) { |
| 604 | /* do not prevent the signal from being received from within its |
| 605 | own signal handler */ |
| 606 | action.sa_flags = SA_NODEFER; |
| 607 | } |
| 608 | #ifdef HAVE_SIGALTSTACK |
| 609 | if (stack.ss_sp != NULL) { |
| 610 | /* Call the signal handler on an alternate signal stack |
| 611 | provided by sigaltstack() */ |
| 612 | action.sa_flags |= SA_ONSTACK; |
| 613 | } |
| 614 | #endif |
| 615 | return sigaction(signum, &action, p_previous); |
| 616 | #else |
| 617 | _Py_sighandler_t previous; |
| 618 | previous = signal(signum, faulthandler_user); |
| 619 | if (p_previous != NULL) |
| 620 | *p_previous = previous; |
| 621 | return (previous == SIG_ERR); |
| 622 | #endif |
| 623 | } |
| 624 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 625 | /* Handler of user signals (e.g. SIGUSR1). |
| 626 | |
| 627 | Dump the traceback of the current thread, or of all threads if |
| 628 | thread.all_threads is true. |
| 629 | |
| 630 | This function is signal safe and should only call signal safe functions. */ |
| 631 | |
| 632 | static void |
| 633 | faulthandler_user(int signum) |
| 634 | { |
| 635 | user_signal_t *user; |
| 636 | PyThreadState *tstate; |
Victor Stinner | c925617 | 2011-05-07 12:20:11 +0200 | [diff] [blame] | 637 | int save_errno = errno; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 638 | |
| 639 | user = &user_signals[signum]; |
| 640 | if (!user->enabled) |
| 641 | return; |
| 642 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 643 | #ifdef WITH_THREAD |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 644 | /* PyThreadState_Get() doesn't give the state of the current thread if |
| 645 | the thread doesn't hold the GIL. Read the thread local storage (TLS) |
| 646 | instead: call PyGILState_GetThisThreadState(). */ |
| 647 | tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 648 | #else |
| 649 | tstate = PyThreadState_Get(); |
| 650 | #endif |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 651 | |
| 652 | if (user->all_threads) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 653 | _Py_DumpTracebackThreads(user->fd, user->interp, tstate); |
| 654 | else { |
Victor Stinner | 98a387b | 2012-08-01 19:36:36 +0200 | [diff] [blame] | 655 | if (tstate != NULL) |
| 656 | _Py_DumpTraceback(user->fd, tstate); |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 657 | } |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 658 | #ifdef HAVE_SIGACTION |
| 659 | if (user->chain) { |
| 660 | (void)sigaction(signum, &user->previous, NULL); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 661 | errno = save_errno; |
| 662 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 663 | /* call the previous signal handler */ |
| 664 | raise(signum); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 665 | |
| 666 | save_errno = errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 667 | (void)faulthandler_register(signum, user->chain, NULL); |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 668 | errno = save_errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 669 | } |
| 670 | #else |
| 671 | if (user->chain) { |
Victor Stinner | 3cc635d | 2012-08-09 02:43:41 +0200 | [diff] [blame] | 672 | errno = save_errno; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 673 | /* call the previous signal handler */ |
| 674 | user->previous(signum); |
| 675 | } |
| 676 | #endif |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static int |
| 680 | check_signum(int signum) |
| 681 | { |
| 682 | unsigned int i; |
| 683 | |
| 684 | for (i=0; i < faulthandler_nsignals; i++) { |
| 685 | if (faulthandler_handlers[i].signum == signum) { |
| 686 | PyErr_Format(PyExc_RuntimeError, |
| 687 | "signal %i cannot be registered, " |
| 688 | "use enable() instead", |
| 689 | signum); |
| 690 | return 0; |
| 691 | } |
| 692 | } |
| 693 | if (signum < 1 || NSIG <= signum) { |
| 694 | PyErr_SetString(PyExc_ValueError, "signal number out of range"); |
| 695 | return 0; |
| 696 | } |
| 697 | return 1; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static PyObject* |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 701 | faulthandler_register_py(PyObject *self, |
| 702 | PyObject *args, PyObject *kwargs) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 703 | { |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 704 | static char *kwlist[] = {"signum", "file", "all_threads", "chain", NULL}; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 705 | int signum; |
| 706 | PyObject *file = NULL; |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 707 | int all_threads = 1; |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 708 | int chain = 0; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 709 | int fd; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 710 | user_signal_t *user; |
| 711 | _Py_sighandler_t previous; |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 712 | PyThreadState *tstate; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 713 | int err; |
| 714 | |
| 715 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 716 | "i|Oii:register", kwlist, |
| 717 | &signum, &file, &all_threads, &chain)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 718 | return NULL; |
| 719 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 720 | if (!check_signum(signum)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 721 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 722 | |
Victor Stinner | a4de6d8 | 2011-04-09 00:47:23 +0200 | [diff] [blame] | 723 | tstate = get_thread_state(); |
| 724 | if (tstate == NULL) |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 725 | return NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 726 | |
| 727 | file = faulthandler_get_fileno(file, &fd); |
| 728 | if (file == NULL) |
| 729 | return NULL; |
| 730 | |
| 731 | if (user_signals == NULL) { |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 732 | user_signals = PyMem_Malloc(NSIG * sizeof(user_signal_t)); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 733 | if (user_signals == NULL) |
| 734 | return PyErr_NoMemory(); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 735 | memset(user_signals, 0, NSIG * sizeof(user_signal_t)); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 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 | 8898350 | 2013-09-08 11:36:23 +0200 | [diff] [blame] | 1051 | char *p; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1052 | |
Victor Stinner | 8898350 | 2013-09-08 11:36:23 +0200 | [diff] [blame] | 1053 | if (!((p = Py_GETENV("PYTHONFAULTHANDLER")) && *p != '\0')) { |
| 1054 | /* PYTHONFAULTHANDLER environment variable is missing |
| 1055 | or an empty string */ |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1056 | int has_key; |
| 1057 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1058 | xoptions = PySys_GetXOptions(); |
| 1059 | if (xoptions == NULL) |
| 1060 | return -1; |
| 1061 | |
| 1062 | key = PyUnicode_FromString("faulthandler"); |
| 1063 | if (key == NULL) |
| 1064 | return -1; |
| 1065 | |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1066 | has_key = PyDict_Contains(xoptions, key); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1067 | Py_DECREF(key); |
Victor Stinner | 25095b2 | 2011-05-26 13:47:08 +0200 | [diff] [blame] | 1068 | if (!has_key) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1071 | |
| 1072 | module = PyImport_ImportModule("faulthandler"); |
| 1073 | if (module == NULL) { |
| 1074 | return -1; |
| 1075 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1076 | res = _PyObject_CallMethodId(module, &PyId_enable, ""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1077 | Py_DECREF(module); |
| 1078 | if (res == NULL) |
| 1079 | return -1; |
| 1080 | Py_DECREF(res); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | int _PyFaulthandler_Init(void) |
| 1085 | { |
| 1086 | #ifdef HAVE_SIGALTSTACK |
| 1087 | int err; |
| 1088 | |
| 1089 | /* Try to allocate an alternate stack for faulthandler() signal handler to |
| 1090 | * be able to allocate memory on the stack, even on a stack overflow. If it |
| 1091 | * fails, ignore the error. */ |
| 1092 | stack.ss_flags = 0; |
| 1093 | stack.ss_size = SIGSTKSZ; |
| 1094 | stack.ss_sp = PyMem_Malloc(stack.ss_size); |
| 1095 | if (stack.ss_sp != NULL) { |
| 1096 | err = sigaltstack(&stack, NULL); |
| 1097 | if (err) { |
| 1098 | PyMem_Free(stack.ss_sp); |
| 1099 | stack.ss_sp = NULL; |
| 1100 | } |
| 1101 | } |
| 1102 | #endif |
| 1103 | #ifdef FAULTHANDLER_LATER |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1104 | thread.file = NULL; |
| 1105 | thread.cancel_event = PyThread_allocate_lock(); |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1106 | thread.running = PyThread_allocate_lock(); |
| 1107 | if (!thread.cancel_event || !thread.running) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1108 | PyErr_SetString(PyExc_RuntimeError, |
| 1109 | "could not allocate locks for faulthandler"); |
| 1110 | return -1; |
| 1111 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1112 | PyThread_acquire_lock(thread.cancel_event, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1113 | #endif |
| 1114 | |
| 1115 | return faulthandler_env_options(); |
| 1116 | } |
| 1117 | |
| 1118 | void _PyFaulthandler_Fini(void) |
| 1119 | { |
| 1120 | #ifdef FAULTHANDLER_USER |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1121 | unsigned int signum; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1122 | #endif |
| 1123 | |
| 1124 | #ifdef FAULTHANDLER_LATER |
| 1125 | /* later */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1126 | if (thread.cancel_event) { |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 1127 | cancel_dump_traceback_later(); |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1128 | PyThread_release_lock(thread.cancel_event); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1129 | PyThread_free_lock(thread.cancel_event); |
| 1130 | thread.cancel_event = NULL; |
| 1131 | } |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 1132 | if (thread.running) { |
| 1133 | PyThread_free_lock(thread.running); |
| 1134 | thread.running = NULL; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1135 | } |
| 1136 | #endif |
| 1137 | |
| 1138 | #ifdef FAULTHANDLER_USER |
| 1139 | /* user */ |
| 1140 | if (user_signals != NULL) { |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 1141 | for (signum=0; signum < NSIG; signum++) |
| 1142 | faulthandler_unregister(&user_signals[signum], signum); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1143 | PyMem_Free(user_signals); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1144 | user_signals = NULL; |
| 1145 | } |
| 1146 | #endif |
| 1147 | |
| 1148 | /* fatal */ |
| 1149 | faulthandler_disable(); |
| 1150 | #ifdef HAVE_SIGALTSTACK |
| 1151 | if (stack.ss_sp != NULL) { |
| 1152 | PyMem_Free(stack.ss_sp); |
| 1153 | stack.ss_sp = NULL; |
| 1154 | } |
| 1155 | #endif |
| 1156 | } |