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