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