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