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