blob: 1450d9463798870f8149b67cd82ace1170f7d96f [file] [log] [blame]
Victor Stinner024e37a2011-03-31 01:31:06 +02001#include "Python.h"
2#include "pythread.h"
3#include <signal.h>
4#include <object.h>
5#include <frameobject.h>
6#include <signal.h>
Victor Stinner0aafa4f2011-06-29 23:28:02 +02007#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
8#include <pthread.h>
9#endif
10
Victor Stinner024e37a2011-03-31 01:31:06 +020011
Victor Stinner96994402011-04-07 11:37:19 +020012/* Allocate at maximum 100 MB of the stack to raise the stack overflow */
13#define STACK_OVERFLOW_MAX_SIZE (100*1024*1024)
14
Victor Stinner024e37a2011-03-31 01:31:06 +020015#ifdef WITH_THREAD
16# define FAULTHANDLER_LATER
17#endif
18
19#ifndef MS_WINDOWS
Victor Stinnerd727e232011-04-01 12:13:55 +020020 /* register() is useless on Windows, because only SIGSEGV, SIGABRT and
21 SIGILL can be handled by the process, and these signals can only be used
22 with enable(), not using register() */
Victor Stinner024e37a2011-03-31 01:31:06 +020023# define FAULTHANDLER_USER
24#endif
25
26#define PUTS(fd, str) write(fd, str, strlen(str))
27
28#ifdef HAVE_SIGACTION
29typedef struct sigaction _Py_sighandler_t;
30#else
31typedef PyOS_sighandler_t _Py_sighandler_t;
32#endif
33
34typedef struct {
35 int signum;
36 int enabled;
37 const char* name;
38 _Py_sighandler_t previous;
39 int all_threads;
40} fault_handler_t;
41
42static struct {
43 int enabled;
44 PyObject *file;
45 int fd;
46 int all_threads;
Victor Stinnera4de6d82011-04-09 00:47:23 +020047 PyInterpreterState *interp;
Victor Stinner024e37a2011-03-31 01:31:06 +020048} fatal_error = {0, NULL, -1, 0};
49
50#ifdef FAULTHANDLER_LATER
51static struct {
52 PyObject *file;
53 int fd;
Victor Stinner94189322011-04-08 13:00:31 +020054 PY_TIMEOUT_T timeout_us; /* timeout in microseconds */
Victor Stinner024e37a2011-03-31 01:31:06 +020055 int repeat;
Victor Stinner024e37a2011-03-31 01:31:06 +020056 PyInterpreterState *interp;
57 int exit;
Victor Stinnerc790a532011-04-08 13:39:59 +020058 char *header;
59 size_t header_len;
Victor Stinner410dd7d2011-05-11 20:56:08 +020060 /* The main thread always holds this lock. It is only released when
61 faulthandler_thread() is interrupted before this thread exits, or at
Victor Stinnerde10f402011-04-08 12:57:06 +020062 Python exit. */
Victor Stinner024e37a2011-03-31 01:31:06 +020063 PyThread_type_lock cancel_event;
64 /* released by child thread when joined */
Victor Stinnerde10f402011-04-08 12:57:06 +020065 PyThread_type_lock running;
Victor Stinner024e37a2011-03-31 01:31:06 +020066} thread;
67#endif
68
69#ifdef FAULTHANDLER_USER
70typedef struct {
71 int enabled;
72 PyObject *file;
73 int fd;
74 int all_threads;
75 _Py_sighandler_t previous;
Victor Stinner44378d42011-04-01 15:37:12 +020076 PyInterpreterState *interp;
Victor Stinner024e37a2011-03-31 01:31:06 +020077} user_signal_t;
78
79static user_signal_t *user_signals;
80
81/* the following macros come from Python: Modules/signalmodule.c */
82#if defined(PYOS_OS2) && !defined(PYCC_GCC)
83#define NSIG 12
84#endif
85#ifndef NSIG
86# if defined(_NSIG)
87# define NSIG _NSIG /* For BSD/SysV */
88# elif defined(_SIGMAX)
89# define NSIG (_SIGMAX + 1) /* For QNX */
90# elif defined(SIGMAX)
91# define NSIG (SIGMAX + 1) /* For djgpp */
92# else
93# define NSIG 64 /* Use a reasonable default value */
94# endif
95#endif
96
97#endif /* FAULTHANDLER_USER */
98
99
100static fault_handler_t faulthandler_handlers[] = {
101#ifdef SIGBUS
102 {SIGBUS, 0, "Bus error", },
103#endif
104#ifdef SIGILL
105 {SIGILL, 0, "Illegal instruction", },
106#endif
107 {SIGFPE, 0, "Floating point exception", },
Victor Stinnerd727e232011-04-01 12:13:55 +0200108 {SIGABRT, 0, "Aborted", },
Victor Stinner024e37a2011-03-31 01:31:06 +0200109 /* define SIGSEGV at the end to make it the default choice if searching the
110 handler fails in faulthandler_fatal_error() */
111 {SIGSEGV, 0, "Segmentation fault", }
112};
113static const unsigned char faulthandler_nsignals = \
114 sizeof(faulthandler_handlers) / sizeof(faulthandler_handlers[0]);
115
116#ifdef HAVE_SIGALTSTACK
117static stack_t stack;
118#endif
119
120
121/* Get the file descriptor of a file by calling its fileno() method and then
122 call its flush() method.
123
124 If file is NULL or Py_None, use sys.stderr as the new file.
125
126 On success, return the new file and write the file descriptor into *p_fd.
127 On error, return NULL. */
128
129static PyObject*
130faulthandler_get_fileno(PyObject *file, int *p_fd)
131{
132 PyObject *result;
133 long fd_long;
134 int fd;
135
136 if (file == NULL || file == Py_None) {
137 file = PySys_GetObject("stderr");
138 if (file == NULL) {
139 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
140 return NULL;
141 }
142 }
143
144 result = PyObject_CallMethod(file, "fileno", "");
145 if (result == NULL)
146 return NULL;
147
148 fd = -1;
149 if (PyLong_Check(result)) {
150 fd_long = PyLong_AsLong(result);
151 if (0 <= fd_long && fd_long < INT_MAX)
152 fd = (int)fd_long;
153 }
154 Py_DECREF(result);
155
156 if (fd == -1) {
157 PyErr_SetString(PyExc_RuntimeError,
158 "file.fileno() is not a valid file descriptor");
159 return NULL;
160 }
161
162 result = PyObject_CallMethod(file, "flush", "");
163 if (result != NULL)
164 Py_DECREF(result);
165 else {
166 /* ignore flush() error */
167 PyErr_Clear();
168 }
169 *p_fd = fd;
170 return file;
171}
172
Victor Stinnera4de6d82011-04-09 00:47:23 +0200173/* Get the state of the current thread: only call this function if the current
174 thread holds the GIL. Raise an exception on error. */
175static PyThreadState*
176get_thread_state(void)
177{
178 PyThreadState *tstate = PyThreadState_Get();
179 if (tstate == NULL) {
180 PyErr_SetString(PyExc_RuntimeError,
181 "unable to get the current thread state");
182 return NULL;
183 }
184 return tstate;
185}
186
Victor Stinner024e37a2011-03-31 01:31:06 +0200187static PyObject*
188faulthandler_dump_traceback_py(PyObject *self,
189 PyObject *args, PyObject *kwargs)
190{
191 static char *kwlist[] = {"file", "all_threads", NULL};
192 PyObject *file = NULL;
Victor Stinner7bba62f2011-05-07 12:43:00 +0200193 int all_threads = 1;
Victor Stinner024e37a2011-03-31 01:31:06 +0200194 PyThreadState *tstate;
195 const char *errmsg;
196 int fd;
197
198 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
199 "|Oi:dump_traceback", kwlist,
200 &file, &all_threads))
201 return NULL;
202
203 file = faulthandler_get_fileno(file, &fd);
204 if (file == NULL)
205 return NULL;
206
Victor Stinnera4de6d82011-04-09 00:47:23 +0200207 tstate = get_thread_state();
208 if (tstate == NULL)
Victor Stinner024e37a2011-03-31 01:31:06 +0200209 return NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200210
211 if (all_threads) {
212 errmsg = _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
213 if (errmsg != NULL) {
214 PyErr_SetString(PyExc_RuntimeError, errmsg);
215 return NULL;
216 }
217 }
218 else {
219 _Py_DumpTraceback(fd, tstate);
220 }
221 Py_RETURN_NONE;
222}
223
224
Victor Stinner410dd7d2011-05-11 20:56:08 +0200225/* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals.
Victor Stinner024e37a2011-03-31 01:31:06 +0200226
227 Display the current Python traceback, restore the previous handler and call
228 the previous handler.
229
Victor Stinner410dd7d2011-05-11 20:56:08 +0200230 On Windows, don't explicitly call the previous handler, because the Windows
Victor Stinner024e37a2011-03-31 01:31:06 +0200231 signal handler would not be called (for an unknown reason). The execution of
232 the program continues at faulthandler_fatal_error() exit, but the same
233 instruction will raise the same fault (signal), and so the previous handler
234 will be called.
235
Victor Stinner410dd7d2011-05-11 20:56:08 +0200236 This function is signal-safe and should only call signal-safe functions. */
Victor Stinner024e37a2011-03-31 01:31:06 +0200237
238static void
Victor Stinner44e31ba2011-04-07 11:39:03 +0200239faulthandler_fatal_error(int signum)
Victor Stinner024e37a2011-03-31 01:31:06 +0200240{
241 const int fd = fatal_error.fd;
242 unsigned int i;
243 fault_handler_t *handler = NULL;
244 PyThreadState *tstate;
Victor Stinnerc9256172011-05-07 12:20:11 +0200245 int save_errno = errno;
Victor Stinner024e37a2011-03-31 01:31:06 +0200246
247 if (!fatal_error.enabled)
248 return;
249
250 for (i=0; i < faulthandler_nsignals; i++) {
251 handler = &faulthandler_handlers[i];
252 if (handler->signum == signum)
253 break;
254 }
255 if (handler == NULL) {
256 /* faulthandler_nsignals == 0 (unlikely) */
257 return;
258 }
259
260 /* restore the previous handler */
261#ifdef HAVE_SIGACTION
262 (void)sigaction(handler->signum, &handler->previous, NULL);
263#else
264 (void)signal(handler->signum, handler->previous);
265#endif
266 handler->enabled = 0;
267
268 PUTS(fd, "Fatal Python error: ");
269 PUTS(fd, handler->name);
270 PUTS(fd, "\n\n");
271
Victor Stinnerff4cd882011-04-07 11:50:25 +0200272#ifdef WITH_THREAD
Victor Stinnerd727e232011-04-01 12:13:55 +0200273 /* SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals and
Victor Stinner410dd7d2011-05-11 20:56:08 +0200274 are thus delivered to the thread that caused the fault. Get the Python
Victor Stinnerd727e232011-04-01 12:13:55 +0200275 thread state of the current thread.
Victor Stinner024e37a2011-03-31 01:31:06 +0200276
277 PyThreadState_Get() doesn't give the state of the thread that caused the
278 fault if the thread released the GIL, and so this function cannot be
279 used. Read the thread local storage (TLS) instead: call
280 PyGILState_GetThisThreadState(). */
281 tstate = PyGILState_GetThisThreadState();
Victor Stinnerff4cd882011-04-07 11:50:25 +0200282#else
283 tstate = PyThreadState_Get();
284#endif
Victor Stinner024e37a2011-03-31 01:31:06 +0200285
286 if (fatal_error.all_threads)
Victor Stinnera4de6d82011-04-09 00:47:23 +0200287 _Py_DumpTracebackThreads(fd, fatal_error.interp, tstate);
288 else {
289 if (tstate != NULL)
290 _Py_DumpTraceback(fd, tstate);
291 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200292
Victor Stinnerc9256172011-05-07 12:20:11 +0200293 errno = save_errno;
Victor Stinnerbc6a4db2011-04-01 12:08:57 +0200294#ifdef MS_WINDOWS
295 if (signum == SIGSEGV) {
Victor Stinner410dd7d2011-05-11 20:56:08 +0200296 /* don't explicitly call the previous handler for SIGSEGV in this signal
Victor Stinnerbc6a4db2011-04-01 12:08:57 +0200297 handler, because the Windows signal handler would not be called */
298 return;
299 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200300#endif
Victor Stinnerbc6a4db2011-04-01 12:08:57 +0200301 /* call the previous signal handler: it is called immediatly if we use
302 sigaction() thanks to SA_NODEFER flag, otherwise it is deferred */
303 raise(signum);
Victor Stinner024e37a2011-03-31 01:31:06 +0200304}
305
Victor Stinnerd727e232011-04-01 12:13:55 +0200306/* Install the handler for fatal signals, faulthandler_fatal_error(). */
Victor Stinner024e37a2011-03-31 01:31:06 +0200307
308static PyObject*
309faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs)
310{
311 static char *kwlist[] = {"file", "all_threads", NULL};
312 PyObject *file = NULL;
Victor Stinner7bba62f2011-05-07 12:43:00 +0200313 int all_threads = 1;
Victor Stinner024e37a2011-03-31 01:31:06 +0200314 unsigned int i;
315 fault_handler_t *handler;
316#ifdef HAVE_SIGACTION
317 struct sigaction action;
318#endif
319 int err;
320 int fd;
Victor Stinnera4de6d82011-04-09 00:47:23 +0200321 PyThreadState *tstate;
Victor Stinner024e37a2011-03-31 01:31:06 +0200322
323 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
324 "|Oi:enable", kwlist, &file, &all_threads))
325 return NULL;
326
327 file = faulthandler_get_fileno(file, &fd);
328 if (file == NULL)
329 return NULL;
330
Victor Stinnera4de6d82011-04-09 00:47:23 +0200331 tstate = get_thread_state();
332 if (tstate == NULL)
333 return NULL;
334
Victor Stinner024e37a2011-03-31 01:31:06 +0200335 Py_XDECREF(fatal_error.file);
336 Py_INCREF(file);
337 fatal_error.file = file;
338 fatal_error.fd = fd;
339 fatal_error.all_threads = all_threads;
Victor Stinnera4de6d82011-04-09 00:47:23 +0200340 fatal_error.interp = tstate->interp;
Victor Stinner024e37a2011-03-31 01:31:06 +0200341
342 if (!fatal_error.enabled) {
343 fatal_error.enabled = 1;
344
345 for (i=0; i < faulthandler_nsignals; i++) {
346 handler = &faulthandler_handlers[i];
347#ifdef HAVE_SIGACTION
Victor Stinner44e31ba2011-04-07 11:39:03 +0200348 action.sa_handler = faulthandler_fatal_error;
Victor Stinner024e37a2011-03-31 01:31:06 +0200349 sigemptyset(&action.sa_mask);
350 /* Do not prevent the signal from being received from within
351 its own signal handler */
352 action.sa_flags = SA_NODEFER;
353#ifdef HAVE_SIGALTSTACK
354 if (stack.ss_sp != NULL) {
355 /* Call the signal handler on an alternate signal stack
356 provided by sigaltstack() */
357 action.sa_flags |= SA_ONSTACK;
358 }
359#endif
360 err = sigaction(handler->signum, &action, &handler->previous);
361#else
362 handler->previous = signal(handler->signum,
363 faulthandler_fatal_error);
364 err = (handler->previous == SIG_ERR);
365#endif
366 if (err) {
367 PyErr_SetFromErrno(PyExc_RuntimeError);
368 return NULL;
369 }
370 handler->enabled = 1;
371 }
372 }
373 Py_RETURN_NONE;
374}
375
376static void
377faulthandler_disable(void)
378{
379 unsigned int i;
380 fault_handler_t *handler;
381
382 if (fatal_error.enabled) {
383 fatal_error.enabled = 0;
384 for (i=0; i < faulthandler_nsignals; i++) {
385 handler = &faulthandler_handlers[i];
386 if (!handler->enabled)
387 continue;
388#ifdef HAVE_SIGACTION
389 (void)sigaction(handler->signum, &handler->previous, NULL);
390#else
391 (void)signal(handler->signum, handler->previous);
392#endif
393 handler->enabled = 0;
394 }
395 }
396
397 Py_CLEAR(fatal_error.file);
398}
399
400static PyObject*
401faulthandler_disable_py(PyObject *self)
402{
403 if (!fatal_error.enabled) {
404 Py_INCREF(Py_False);
405 return Py_False;
406 }
407 faulthandler_disable();
408 Py_INCREF(Py_True);
409 return Py_True;
410}
411
412static PyObject*
413faulthandler_is_enabled(PyObject *self)
414{
415 return PyBool_FromLong(fatal_error.enabled);
416}
417
418#ifdef FAULTHANDLER_LATER
419
420static void
421faulthandler_thread(void *unused)
422{
423 PyLockStatus st;
424 const char* errmsg;
425 PyThreadState *current;
426 int ok;
Victor Stinnercf2a8072011-04-19 23:30:57 +0200427#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Victor Stinnerda9edae2011-04-04 11:05:21 +0200428 sigset_t set;
429
430 /* we don't want to receive any signal */
431 sigfillset(&set);
Victor Stinnerda9edae2011-04-04 11:05:21 +0200432 pthread_sigmask(SIG_SETMASK, &set, NULL);
Victor Stinnerda9edae2011-04-04 11:05:21 +0200433#endif
Victor Stinner024e37a2011-03-31 01:31:06 +0200434
435 do {
436 st = PyThread_acquire_lock_timed(thread.cancel_event,
Victor Stinner94189322011-04-08 13:00:31 +0200437 thread.timeout_us, 0);
Victor Stinner024e37a2011-03-31 01:31:06 +0200438 if (st == PY_LOCK_ACQUIRED) {
Victor Stinnerde10f402011-04-08 12:57:06 +0200439 PyThread_release_lock(thread.cancel_event);
Victor Stinner024e37a2011-03-31 01:31:06 +0200440 break;
441 }
442 /* Timeout => dump traceback */
443 assert(st == PY_LOCK_FAILURE);
444
445 /* get the thread holding the GIL, NULL if no thread hold the GIL */
446 current = _Py_atomic_load_relaxed(&_PyThreadState_Current);
447
Victor Stinnerc790a532011-04-08 13:39:59 +0200448 write(thread.fd, thread.header, thread.header_len);
449
Victor Stinner024e37a2011-03-31 01:31:06 +0200450 errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current);
451 ok = (errmsg == NULL);
452
453 if (thread.exit)
454 _exit(1);
455 } while (ok && thread.repeat);
456
457 /* The only way out */
Victor Stinnerde10f402011-04-08 12:57:06 +0200458 PyThread_release_lock(thread.running);
Victor Stinner024e37a2011-03-31 01:31:06 +0200459}
460
461static void
Victor Stinnerde10f402011-04-08 12:57:06 +0200462cancel_dump_tracebacks_later(void)
Victor Stinner024e37a2011-03-31 01:31:06 +0200463{
Victor Stinner410dd7d2011-05-11 20:56:08 +0200464 /* Notify cancellation */
Victor Stinnerde10f402011-04-08 12:57:06 +0200465 PyThread_release_lock(thread.cancel_event);
466
Victor Stinnera4d4f1b2011-04-01 03:00:05 +0200467 /* Wait for thread to join */
Victor Stinnerde10f402011-04-08 12:57:06 +0200468 PyThread_acquire_lock(thread.running, 1);
469 PyThread_release_lock(thread.running);
470
471 /* The main thread should always hold the cancel_event lock */
472 PyThread_acquire_lock(thread.cancel_event, 1);
473
Victor Stinner024e37a2011-03-31 01:31:06 +0200474 Py_CLEAR(thread.file);
Victor Stinnerc790a532011-04-08 13:39:59 +0200475 if (thread.header) {
476 free(thread.header);
477 thread.header = NULL;
478 }
479}
480
481static char*
482format_timeout(double timeout)
483{
484 unsigned long us, sec, min, hour;
485 double intpart, fracpart;
486 char buffer[100];
487
488 fracpart = modf(timeout, &intpart);
489 sec = (unsigned long)intpart;
490 us = (unsigned long)(fracpart * 1e6);
491 min = sec / 60;
492 sec %= 60;
493 hour = min / 60;
494 min %= 60;
495
496 if (us != 0)
497 PyOS_snprintf(buffer, sizeof(buffer),
498 "Timeout (%lu:%02lu:%02lu.%06lu)!\n",
499 hour, min, sec, us);
500 else
501 PyOS_snprintf(buffer, sizeof(buffer),
502 "Timeout (%lu:%02lu:%02lu)!\n",
503 hour, min, sec);
504
505 return strdup(buffer);
Victor Stinner024e37a2011-03-31 01:31:06 +0200506}
507
508static PyObject*
Victor Stinner96994402011-04-07 11:37:19 +0200509faulthandler_dump_tracebacks_later(PyObject *self,
510 PyObject *args, PyObject *kwargs)
Victor Stinner024e37a2011-03-31 01:31:06 +0200511{
512 static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL};
513 double timeout;
Victor Stinner94189322011-04-08 13:00:31 +0200514 PY_TIMEOUT_T timeout_us;
Victor Stinner024e37a2011-03-31 01:31:06 +0200515 int repeat = 0;
516 PyObject *file = NULL;
517 int fd;
518 int exit = 0;
Victor Stinner96994402011-04-07 11:37:19 +0200519 PyThreadState *tstate;
Victor Stinnerc790a532011-04-08 13:39:59 +0200520 char *header;
521 size_t header_len;
Victor Stinner024e37a2011-03-31 01:31:06 +0200522
523 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
524 "d|iOi:dump_tracebacks_later", kwlist,
525 &timeout, &repeat, &file, &exit))
526 return NULL;
Victor Stinnerc790a532011-04-08 13:39:59 +0200527 if ((timeout * 1e6) >= (double) PY_TIMEOUT_MAX) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200528 PyErr_SetString(PyExc_OverflowError, "timeout value is too large");
529 return NULL;
530 }
Victor Stinnerc790a532011-04-08 13:39:59 +0200531 timeout_us = (PY_TIMEOUT_T)(timeout * 1e6);
Victor Stinner94189322011-04-08 13:00:31 +0200532 if (timeout_us <= 0) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200533 PyErr_SetString(PyExc_ValueError, "timeout must be greater than 0");
534 return NULL;
535 }
536
Victor Stinnera4de6d82011-04-09 00:47:23 +0200537 tstate = get_thread_state();
538 if (tstate == NULL)
Victor Stinner96994402011-04-07 11:37:19 +0200539 return NULL;
Victor Stinner96994402011-04-07 11:37:19 +0200540
Victor Stinner024e37a2011-03-31 01:31:06 +0200541 file = faulthandler_get_fileno(file, &fd);
542 if (file == NULL)
543 return NULL;
544
Victor Stinnerc790a532011-04-08 13:39:59 +0200545 /* format the timeout */
546 header = format_timeout(timeout);
547 if (header == NULL)
548 return PyErr_NoMemory();
549 header_len = strlen(header);
550
Victor Stinner024e37a2011-03-31 01:31:06 +0200551 /* Cancel previous thread, if running */
Victor Stinnerde10f402011-04-08 12:57:06 +0200552 cancel_dump_tracebacks_later();
Victor Stinner024e37a2011-03-31 01:31:06 +0200553
554 Py_XDECREF(thread.file);
555 Py_INCREF(file);
556 thread.file = file;
557 thread.fd = fd;
Victor Stinner94189322011-04-08 13:00:31 +0200558 thread.timeout_us = timeout_us;
Victor Stinner024e37a2011-03-31 01:31:06 +0200559 thread.repeat = repeat;
Victor Stinner96994402011-04-07 11:37:19 +0200560 thread.interp = tstate->interp;
Victor Stinner024e37a2011-03-31 01:31:06 +0200561 thread.exit = exit;
Victor Stinnerc790a532011-04-08 13:39:59 +0200562 thread.header = header;
563 thread.header_len = header_len;
Victor Stinner024e37a2011-03-31 01:31:06 +0200564
565 /* Arm these locks to serve as events when released */
Victor Stinnerde10f402011-04-08 12:57:06 +0200566 PyThread_acquire_lock(thread.running, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +0200567
Victor Stinner024e37a2011-03-31 01:31:06 +0200568 if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) {
Victor Stinnerde10f402011-04-08 12:57:06 +0200569 PyThread_release_lock(thread.running);
Victor Stinner024e37a2011-03-31 01:31:06 +0200570 Py_CLEAR(thread.file);
Victor Stinnerc790a532011-04-08 13:39:59 +0200571 free(header);
572 thread.header = NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200573 PyErr_SetString(PyExc_RuntimeError,
574 "unable to start watchdog thread");
575 return NULL;
576 }
577
578 Py_RETURN_NONE;
579}
580
581static PyObject*
Victor Stinner702624e2011-03-31 03:42:34 +0200582faulthandler_cancel_dump_tracebacks_later_py(PyObject *self)
Victor Stinner024e37a2011-03-31 01:31:06 +0200583{
Victor Stinnerde10f402011-04-08 12:57:06 +0200584 cancel_dump_tracebacks_later();
Victor Stinner024e37a2011-03-31 01:31:06 +0200585 Py_RETURN_NONE;
586}
Victor Stinner410dd7d2011-05-11 20:56:08 +0200587#endif /* FAULTHANDLER_LATER */
Victor Stinner024e37a2011-03-31 01:31:06 +0200588
589#ifdef FAULTHANDLER_USER
590/* Handler of user signals (e.g. SIGUSR1).
591
592 Dump the traceback of the current thread, or of all threads if
593 thread.all_threads is true.
594
595 This function is signal safe and should only call signal safe functions. */
596
597static void
598faulthandler_user(int signum)
599{
600 user_signal_t *user;
601 PyThreadState *tstate;
Victor Stinnerc9256172011-05-07 12:20:11 +0200602 int save_errno = errno;
Victor Stinner024e37a2011-03-31 01:31:06 +0200603
604 user = &user_signals[signum];
605 if (!user->enabled)
606 return;
607
Victor Stinnerff4cd882011-04-07 11:50:25 +0200608#ifdef WITH_THREAD
Victor Stinner024e37a2011-03-31 01:31:06 +0200609 /* PyThreadState_Get() doesn't give the state of the current thread if
610 the thread doesn't hold the GIL. Read the thread local storage (TLS)
611 instead: call PyGILState_GetThisThreadState(). */
612 tstate = PyGILState_GetThisThreadState();
Victor Stinnerff4cd882011-04-07 11:50:25 +0200613#else
614 tstate = PyThreadState_Get();
615#endif
Victor Stinner024e37a2011-03-31 01:31:06 +0200616
617 if (user->all_threads)
Victor Stinner44378d42011-04-01 15:37:12 +0200618 _Py_DumpTracebackThreads(user->fd, user->interp, tstate);
619 else {
620 if (tstate == NULL)
621 return;
Victor Stinner024e37a2011-03-31 01:31:06 +0200622 _Py_DumpTraceback(user->fd, tstate);
Victor Stinner44378d42011-04-01 15:37:12 +0200623 }
Victor Stinnerc9256172011-05-07 12:20:11 +0200624 errno = save_errno;
Victor Stinner44378d42011-04-01 15:37:12 +0200625}
626
627static int
628check_signum(int signum)
629{
630 unsigned int i;
631
632 for (i=0; i < faulthandler_nsignals; i++) {
633 if (faulthandler_handlers[i].signum == signum) {
634 PyErr_Format(PyExc_RuntimeError,
635 "signal %i cannot be registered, "
636 "use enable() instead",
637 signum);
638 return 0;
639 }
640 }
641 if (signum < 1 || NSIG <= signum) {
642 PyErr_SetString(PyExc_ValueError, "signal number out of range");
643 return 0;
644 }
645 return 1;
Victor Stinner024e37a2011-03-31 01:31:06 +0200646}
647
648static PyObject*
649faulthandler_register(PyObject *self,
650 PyObject *args, PyObject *kwargs)
651{
652 static char *kwlist[] = {"signum", "file", "all_threads", NULL};
653 int signum;
654 PyObject *file = NULL;
Victor Stinner7bba62f2011-05-07 12:43:00 +0200655 int all_threads = 1;
Victor Stinner024e37a2011-03-31 01:31:06 +0200656 int fd;
Victor Stinner024e37a2011-03-31 01:31:06 +0200657 user_signal_t *user;
658 _Py_sighandler_t previous;
659#ifdef HAVE_SIGACTION
660 struct sigaction action;
661#endif
Victor Stinner44378d42011-04-01 15:37:12 +0200662 PyThreadState *tstate;
Victor Stinner024e37a2011-03-31 01:31:06 +0200663 int err;
664
665 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
666 "i|Oi:register", kwlist,
667 &signum, &file, &all_threads))
668 return NULL;
669
Victor Stinner44378d42011-04-01 15:37:12 +0200670 if (!check_signum(signum))
Victor Stinner024e37a2011-03-31 01:31:06 +0200671 return NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200672
Victor Stinnera4de6d82011-04-09 00:47:23 +0200673 tstate = get_thread_state();
674 if (tstate == NULL)
Victor Stinner44378d42011-04-01 15:37:12 +0200675 return NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200676
677 file = faulthandler_get_fileno(file, &fd);
678 if (file == NULL)
679 return NULL;
680
681 if (user_signals == NULL) {
682 user_signals = calloc(NSIG, sizeof(user_signal_t));
683 if (user_signals == NULL)
684 return PyErr_NoMemory();
685 }
686 user = &user_signals[signum];
687
688 if (!user->enabled) {
689#ifdef HAVE_SIGACTION
690 action.sa_handler = faulthandler_user;
691 sigemptyset(&action.sa_mask);
692 /* if the signal is received while the kernel is executing a system
693 call, try to restart the system call instead of interrupting it and
694 return EINTR */
695 action.sa_flags = SA_RESTART;
696#ifdef HAVE_SIGALTSTACK
697 if (stack.ss_sp != NULL) {
698 /* Call the signal handler on an alternate signal stack
699 provided by sigaltstack() */
700 action.sa_flags |= SA_ONSTACK;
701 }
702#endif
703 err = sigaction(signum, &action, &previous);
704#else
705 previous = signal(signum, faulthandler_user);
706 err = (previous == SIG_ERR);
707#endif
708 if (err) {
709 PyErr_SetFromErrno(PyExc_OSError);
710 return NULL;
711 }
712 }
713
714 Py_XDECREF(user->file);
715 Py_INCREF(file);
716 user->file = file;
717 user->fd = fd;
718 user->all_threads = all_threads;
719 user->previous = previous;
Victor Stinner44378d42011-04-01 15:37:12 +0200720 user->interp = tstate->interp;
Victor Stinner024e37a2011-03-31 01:31:06 +0200721 user->enabled = 1;
722
723 Py_RETURN_NONE;
724}
725
726static int
727faulthandler_unregister(user_signal_t *user, int signum)
728{
Victor Stinnera01ca122011-04-01 12:56:17 +0200729 if (!user->enabled)
Victor Stinner024e37a2011-03-31 01:31:06 +0200730 return 0;
731 user->enabled = 0;
732#ifdef HAVE_SIGACTION
733 (void)sigaction(signum, &user->previous, NULL);
734#else
735 (void)signal(signum, user->previous);
736#endif
737 Py_CLEAR(user->file);
738 user->fd = -1;
739 return 1;
740}
741
742static PyObject*
743faulthandler_unregister_py(PyObject *self, PyObject *args)
744{
745 int signum;
746 user_signal_t *user;
747 int change;
748
749 if (!PyArg_ParseTuple(args, "i:unregister", &signum))
750 return NULL;
751
Victor Stinner44378d42011-04-01 15:37:12 +0200752 if (!check_signum(signum))
Victor Stinner024e37a2011-03-31 01:31:06 +0200753 return NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200754
Victor Stinnercfa71232011-04-08 12:48:15 +0200755 if (user_signals == NULL)
756 Py_RETURN_FALSE;
757
Victor Stinner024e37a2011-03-31 01:31:06 +0200758 user = &user_signals[signum];
759 change = faulthandler_unregister(user, signum);
760 return PyBool_FromLong(change);
761}
762#endif /* FAULTHANDLER_USER */
763
764
765static PyObject *
766faulthandler_read_null(PyObject *self, PyObject *args)
767{
768 int *x = NULL, y;
769 int release_gil = 0;
770 if (!PyArg_ParseTuple(args, "|i:_read_null", &release_gil))
771 return NULL;
772 if (release_gil) {
773 Py_BEGIN_ALLOW_THREADS
774 y = *x;
775 Py_END_ALLOW_THREADS
776 } else
777 y = *x;
778 return PyLong_FromLong(y);
779
780}
781
782static PyObject *
783faulthandler_sigsegv(PyObject *self, PyObject *args)
784{
785#if defined(MS_WINDOWS)
Victor Stinnerbc6a4db2011-04-01 12:08:57 +0200786 /* For SIGSEGV, faulthandler_fatal_error() restores the previous signal
787 handler and then gives back the execution flow to the program (without
Victor Stinner410dd7d2011-05-11 20:56:08 +0200788 explicitly calling the previous error handler). In a normal case, the
Victor Stinner024e37a2011-03-31 01:31:06 +0200789 SIGSEGV was raised by the kernel because of a fault, and so if the
790 program retries to execute the same instruction, the fault will be
791 raised again.
792
793 Here the fault is simulated by a fake SIGSEGV signal raised by the
794 application. We have to raise SIGSEGV at lease twice: once for
795 faulthandler_fatal_error(), and one more time for the previous signal
796 handler. */
797 while(1)
798 raise(SIGSEGV);
799#else
800 raise(SIGSEGV);
801#endif
802 Py_RETURN_NONE;
803}
804
805static PyObject *
806faulthandler_sigfpe(PyObject *self, PyObject *args)
807{
808 /* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
809 PowerPC. Use volatile to disable compile-time optimizations. */
810 volatile int x = 1, y = 0, z;
811 z = x / y;
Victor Stinner410dd7d2011-05-11 20:56:08 +0200812 /* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
813 raise it manually. */
Victor Stinner024e37a2011-03-31 01:31:06 +0200814 raise(SIGFPE);
Victor Stinner410dd7d2011-05-11 20:56:08 +0200815 /* This line is never reached, but we pretend to make something with z
816 to silence a compiler warning. */
Victor Stinnere0c9a752011-05-09 14:44:26 +0200817 return PyLong_FromLong(z);
Victor Stinner024e37a2011-03-31 01:31:06 +0200818}
819
Victor Stinnerd727e232011-04-01 12:13:55 +0200820static PyObject *
821faulthandler_sigabrt(PyObject *self, PyObject *args)
822{
Victor Stinner00bc6cc2011-05-10 01:30:03 +0200823#ifdef _MSC_VER
824 /* Visual Studio: configure abort() to not display an error message nor
825 open a popup asking to report the fault. */
826 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
Victor Stinnerd727e232011-04-01 12:13:55 +0200827#endif
Victor Stinner00bc6cc2011-05-10 01:30:03 +0200828 abort();
Victor Stinnerd727e232011-04-01 12:13:55 +0200829 Py_RETURN_NONE;
830}
831
Victor Stinner024e37a2011-03-31 01:31:06 +0200832#ifdef SIGBUS
833static PyObject *
834faulthandler_sigbus(PyObject *self, PyObject *args)
835{
836 raise(SIGBUS);
837 Py_RETURN_NONE;
838}
839#endif
840
841#ifdef SIGILL
842static PyObject *
843faulthandler_sigill(PyObject *self, PyObject *args)
844{
Victor Stinner024e37a2011-03-31 01:31:06 +0200845 raise(SIGILL);
Victor Stinner024e37a2011-03-31 01:31:06 +0200846 Py_RETURN_NONE;
847}
848#endif
849
850static PyObject *
851faulthandler_fatal_error_py(PyObject *self, PyObject *args)
852{
853 char *message;
854 if (!PyArg_ParseTuple(args, "y:fatal_error", &message))
855 return NULL;
856 Py_FatalError(message);
857 Py_RETURN_NONE;
858}
859
860#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
Victor Stinner9b493042011-05-23 12:29:10 +0200861static void*
Victor Stinnerf0480752011-03-31 11:34:08 +0200862stack_overflow(void *min_sp, void *max_sp, size_t *depth)
Victor Stinner024e37a2011-03-31 01:31:06 +0200863{
864 /* allocate 4096 bytes on the stack at each call */
865 unsigned char buffer[4096];
Victor Stinnerf0480752011-03-31 11:34:08 +0200866 void *sp = &buffer;
867 *depth += 1;
868 if (sp < min_sp || max_sp < sp)
869 return sp;
Victor Stinner024e37a2011-03-31 01:31:06 +0200870 buffer[0] = 1;
Victor Stinnerf0480752011-03-31 11:34:08 +0200871 buffer[4095] = 0;
872 return stack_overflow(min_sp, max_sp, depth);
873}
874
875static PyObject *
876faulthandler_stack_overflow(PyObject *self)
877{
878 size_t depth, size;
879 void *sp = &depth, *stop;
880
881 depth = 0;
882 stop = stack_overflow(sp - STACK_OVERFLOW_MAX_SIZE,
883 sp + STACK_OVERFLOW_MAX_SIZE,
884 &depth);
885 if (sp < stop)
886 size = stop - sp;
887 else
888 size = sp - stop;
889 PyErr_Format(PyExc_RuntimeError,
890 "unable to raise a stack overflow (allocated %zu bytes "
891 "on the stack, %zu recursive calls)",
892 size, depth);
893 return NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +0200894}
895#endif
896
897
898static int
899faulthandler_traverse(PyObject *module, visitproc visit, void *arg)
900{
901#ifdef FAULTHANDLER_USER
Victor Stinner96994402011-04-07 11:37:19 +0200902 unsigned int signum;
Victor Stinner024e37a2011-03-31 01:31:06 +0200903#endif
904
905#ifdef FAULTHANDLER_LATER
906 Py_VISIT(thread.file);
907#endif
908#ifdef FAULTHANDLER_USER
909 if (user_signals != NULL) {
Victor Stinner96994402011-04-07 11:37:19 +0200910 for (signum=0; signum < NSIG; signum++)
911 Py_VISIT(user_signals[signum].file);
Victor Stinner024e37a2011-03-31 01:31:06 +0200912 }
913#endif
914 Py_VISIT(fatal_error.file);
915 return 0;
916}
917
918PyDoc_STRVAR(module_doc,
919"faulthandler module.");
920
921static PyMethodDef module_methods[] = {
922 {"enable",
923 (PyCFunction)faulthandler_enable, METH_VARARGS|METH_KEYWORDS,
Victor Stinner7bba62f2011-05-07 12:43:00 +0200924 PyDoc_STR("enable(file=sys.stderr, all_threads=True): "
Victor Stinner024e37a2011-03-31 01:31:06 +0200925 "enable the fault handler")},
926 {"disable", (PyCFunction)faulthandler_disable_py, METH_NOARGS,
927 PyDoc_STR("disable(): disable the fault handler")},
928 {"is_enabled", (PyCFunction)faulthandler_is_enabled, METH_NOARGS,
929 PyDoc_STR("is_enabled()->bool: check if the handler is enabled")},
930 {"dump_traceback",
931 (PyCFunction)faulthandler_dump_traceback_py, METH_VARARGS|METH_KEYWORDS,
Victor Stinner7bba62f2011-05-07 12:43:00 +0200932 PyDoc_STR("dump_traceback(file=sys.stderr, all_threads=True): "
Victor Stinner024e37a2011-03-31 01:31:06 +0200933 "dump the traceback of the current thread, or of all threads "
934 "if all_threads is True, into file")},
935#ifdef FAULTHANDLER_LATER
936 {"dump_tracebacks_later",
Victor Stinner96994402011-04-07 11:37:19 +0200937 (PyCFunction)faulthandler_dump_tracebacks_later, METH_VARARGS|METH_KEYWORDS,
938 PyDoc_STR("dump_tracebacks_later(timeout, repeat=False, file=sys.stderrn, exit=False):\n"
Victor Stinner024e37a2011-03-31 01:31:06 +0200939 "dump the traceback of all threads in timeout seconds,\n"
Victor Stinner96994402011-04-07 11:37:19 +0200940 "or each timeout seconds if repeat is True. If exit is True, "
941 "call _exit(1) which is not safe.")},
Victor Stinner024e37a2011-03-31 01:31:06 +0200942 {"cancel_dump_tracebacks_later",
Victor Stinner702624e2011-03-31 03:42:34 +0200943 (PyCFunction)faulthandler_cancel_dump_tracebacks_later_py, METH_NOARGS,
Victor Stinner024e37a2011-03-31 01:31:06 +0200944 PyDoc_STR("cancel_dump_tracebacks_later():\ncancel the previous call "
945 "to dump_tracebacks_later().")},
946#endif
947
948#ifdef FAULTHANDLER_USER
949 {"register",
950 (PyCFunction)faulthandler_register, METH_VARARGS|METH_KEYWORDS,
Victor Stinner7bba62f2011-05-07 12:43:00 +0200951 PyDoc_STR("register(signum, file=sys.stderr, all_threads=True): "
Victor Stinner024e37a2011-03-31 01:31:06 +0200952 "register an handler for the signal 'signum': dump the "
953 "traceback of the current thread, or of all threads if "
954 "all_threads is True, into file")},
955 {"unregister",
956 faulthandler_unregister_py, METH_VARARGS|METH_KEYWORDS,
957 PyDoc_STR("unregister(signum): unregister the handler of the signal "
958 "'signum' registered by register()")},
959#endif
960
961 {"_read_null", faulthandler_read_null, METH_VARARGS,
962 PyDoc_STR("_read_null(release_gil=False): read from NULL, raise "
963 "a SIGSEGV or SIGBUS signal depending on the platform")},
964 {"_sigsegv", faulthandler_sigsegv, METH_VARARGS,
965 PyDoc_STR("_sigsegv(): raise a SIGSEGV signal")},
Victor Stinnerd727e232011-04-01 12:13:55 +0200966 {"_sigabrt", faulthandler_sigabrt, METH_VARARGS,
967 PyDoc_STR("_sigabrt(): raise a SIGABRT signal")},
Victor Stinner024e37a2011-03-31 01:31:06 +0200968 {"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS,
969 PyDoc_STR("_sigfpe(): raise a SIGFPE signal")},
970#ifdef SIGBUS
971 {"_sigbus", (PyCFunction)faulthandler_sigbus, METH_NOARGS,
972 PyDoc_STR("_sigbus(): raise a SIGBUS signal")},
973#endif
974#ifdef SIGILL
975 {"_sigill", (PyCFunction)faulthandler_sigill, METH_NOARGS,
976 PyDoc_STR("_sigill(): raise a SIGILL signal")},
977#endif
978 {"_fatal_error", faulthandler_fatal_error_py, METH_VARARGS,
979 PyDoc_STR("_fatal_error(message): call Py_FatalError(message)")},
980#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
981 {"_stack_overflow", (PyCFunction)faulthandler_stack_overflow, METH_NOARGS,
982 PyDoc_STR("_stack_overflow(): recursive call to raise a stack overflow")},
983#endif
Victor Stinner410dd7d2011-05-11 20:56:08 +0200984 {NULL, NULL} /* sentinel */
Victor Stinner024e37a2011-03-31 01:31:06 +0200985};
986
987static struct PyModuleDef module_def = {
988 PyModuleDef_HEAD_INIT,
989 "faulthandler",
990 module_doc,
Victor Stinner410dd7d2011-05-11 20:56:08 +0200991 0, /* non-negative size to be able to unload the module */
Victor Stinner024e37a2011-03-31 01:31:06 +0200992 module_methods,
993 NULL,
994 faulthandler_traverse,
995 NULL,
996 NULL
997};
998
999PyMODINIT_FUNC
1000PyInit_faulthandler(void)
1001{
1002 return PyModule_Create(&module_def);
1003}
1004
Victor Stinner410dd7d2011-05-11 20:56:08 +02001005/* Call faulthandler.enable() if the PYTHONFAULTHANDLER environment variable
1006 is defined, or if sys._xoptions has a 'faulthandler' key. */
Victor Stinner024e37a2011-03-31 01:31:06 +02001007
1008static int
1009faulthandler_env_options(void)
1010{
1011 PyObject *xoptions, *key, *module, *res;
Victor Stinner024e37a2011-03-31 01:31:06 +02001012
1013 if (!Py_GETENV("PYTHONFAULTHANDLER")) {
Victor Stinner25095b22011-05-26 13:47:08 +02001014 int has_key;
1015
Victor Stinner024e37a2011-03-31 01:31:06 +02001016 xoptions = PySys_GetXOptions();
1017 if (xoptions == NULL)
1018 return -1;
1019
1020 key = PyUnicode_FromString("faulthandler");
1021 if (key == NULL)
1022 return -1;
1023
Victor Stinner25095b22011-05-26 13:47:08 +02001024 has_key = PyDict_Contains(xoptions, key);
Victor Stinner024e37a2011-03-31 01:31:06 +02001025 Py_DECREF(key);
Victor Stinner25095b22011-05-26 13:47:08 +02001026 if (!has_key)
Victor Stinner024e37a2011-03-31 01:31:06 +02001027 return 0;
1028 }
Victor Stinner024e37a2011-03-31 01:31:06 +02001029
1030 module = PyImport_ImportModule("faulthandler");
1031 if (module == NULL) {
1032 return -1;
1033 }
1034 res = PyObject_CallMethod(module, "enable", "");
1035 Py_DECREF(module);
1036 if (res == NULL)
1037 return -1;
1038 Py_DECREF(res);
1039 return 0;
1040}
1041
1042int _PyFaulthandler_Init(void)
1043{
1044#ifdef HAVE_SIGALTSTACK
1045 int err;
1046
1047 /* Try to allocate an alternate stack for faulthandler() signal handler to
1048 * be able to allocate memory on the stack, even on a stack overflow. If it
1049 * fails, ignore the error. */
1050 stack.ss_flags = 0;
1051 stack.ss_size = SIGSTKSZ;
1052 stack.ss_sp = PyMem_Malloc(stack.ss_size);
1053 if (stack.ss_sp != NULL) {
1054 err = sigaltstack(&stack, NULL);
1055 if (err) {
1056 PyMem_Free(stack.ss_sp);
1057 stack.ss_sp = NULL;
1058 }
1059 }
1060#endif
1061#ifdef FAULTHANDLER_LATER
Victor Stinner024e37a2011-03-31 01:31:06 +02001062 thread.file = NULL;
1063 thread.cancel_event = PyThread_allocate_lock();
Victor Stinnerde10f402011-04-08 12:57:06 +02001064 thread.running = PyThread_allocate_lock();
1065 if (!thread.cancel_event || !thread.running) {
Victor Stinner024e37a2011-03-31 01:31:06 +02001066 PyErr_SetString(PyExc_RuntimeError,
1067 "could not allocate locks for faulthandler");
1068 return -1;
1069 }
Victor Stinnerde10f402011-04-08 12:57:06 +02001070 PyThread_acquire_lock(thread.cancel_event, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +02001071#endif
1072
1073 return faulthandler_env_options();
1074}
1075
1076void _PyFaulthandler_Fini(void)
1077{
1078#ifdef FAULTHANDLER_USER
Victor Stinnera01ca122011-04-01 12:56:17 +02001079 unsigned int signum;
Victor Stinner024e37a2011-03-31 01:31:06 +02001080#endif
1081
1082#ifdef FAULTHANDLER_LATER
1083 /* later */
Victor Stinnerde10f402011-04-08 12:57:06 +02001084 cancel_dump_tracebacks_later();
Victor Stinner024e37a2011-03-31 01:31:06 +02001085 if (thread.cancel_event) {
Victor Stinnerde10f402011-04-08 12:57:06 +02001086 PyThread_release_lock(thread.cancel_event);
Victor Stinner024e37a2011-03-31 01:31:06 +02001087 PyThread_free_lock(thread.cancel_event);
1088 thread.cancel_event = NULL;
1089 }
Victor Stinnerde10f402011-04-08 12:57:06 +02001090 if (thread.running) {
1091 PyThread_free_lock(thread.running);
1092 thread.running = NULL;
Victor Stinner024e37a2011-03-31 01:31:06 +02001093 }
1094#endif
1095
1096#ifdef FAULTHANDLER_USER
1097 /* user */
1098 if (user_signals != NULL) {
Victor Stinnera01ca122011-04-01 12:56:17 +02001099 for (signum=0; signum < NSIG; signum++)
1100 faulthandler_unregister(&user_signals[signum], signum);
Victor Stinner024e37a2011-03-31 01:31:06 +02001101 free(user_signals);
1102 user_signals = NULL;
1103 }
1104#endif
1105
1106 /* fatal */
1107 faulthandler_disable();
1108#ifdef HAVE_SIGALTSTACK
1109 if (stack.ss_sp != NULL) {
1110 PyMem_Free(stack.ss_sp);
1111 stack.ss_sp = NULL;
1112 }
1113#endif
1114}