2011-03-31 07:31:06 +08:00
|
|
|
#include "Python.h"
|
2021-01-19 01:24:29 +08:00
|
|
|
#include "pycore_initconfig.h" // _PyStatus_ERR
|
2021-01-19 03:47:13 +08:00
|
|
|
#include "pycore_pyerrors.h" // _Py_DumpExtensionModules
|
2021-02-19 20:21:28 +08:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
2022-04-26 06:13:31 +08:00
|
|
|
#include "pycore_signal.h" // Py_NSIG
|
2023-08-25 04:02:09 +08:00
|
|
|
#include "pycore_sysmodule.h" // _PySys_GetAttr()
|
2024-02-21 00:50:43 +08:00
|
|
|
#include "pycore_time.h" // _PyTime_FromSecondsObject()
|
2021-01-19 01:24:29 +08:00
|
|
|
#include "pycore_traceback.h" // _Py_DumpTracebackThreads
|
2021-10-14 01:25:53 +08:00
|
|
|
|
2023-10-01 04:06:45 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h> // _exit()
|
|
|
|
#endif
|
|
|
|
#include <signal.h> // sigaction()
|
2021-10-14 01:25:53 +08:00
|
|
|
#include <stdlib.h> // abort()
|
2022-01-13 16:46:04 +08:00
|
|
|
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
|
2014-09-30 19:40:12 +08:00
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
2023-10-01 04:06:45 +08:00
|
|
|
# include <sys/resource.h> // setrlimit()
|
2011-06-30 05:28:02 +08:00
|
|
|
#endif
|
|
|
|
|
2022-03-18 20:03:22 +08:00
|
|
|
#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H) && defined(HAVE_SYS_AUXV_H)
|
|
|
|
# include <linux/auxvec.h> // AT_MINSIGSTKSZ
|
|
|
|
# include <sys/auxv.h> // getauxval()
|
2022-03-12 06:19:35 +08:00
|
|
|
#endif
|
|
|
|
|
2023-10-01 04:06:45 +08:00
|
|
|
|
2017-11-09 06:44:44 +08:00
|
|
|
/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
|
|
|
|
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
|
2011-04-07 17:37:19 +08:00
|
|
|
|
2023-08-22 22:28:20 +08:00
|
|
|
#define PUTS(fd, str) (void)_Py_write_noraise(fd, str, strlen(str))
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2022-03-04 07:25:03 +08:00
|
|
|
|
|
|
|
// clang uses __attribute__((no_sanitize("undefined")))
|
|
|
|
// GCC 4.9+ uses __attribute__((no_sanitize_undefined))
|
|
|
|
#if defined(__has_feature) // Clang
|
|
|
|
# if __has_feature(undefined_behavior_sanitizer)
|
|
|
|
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#if defined(__GNUC__) \
|
|
|
|
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))
|
|
|
|
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize_undefined))
|
|
|
|
#endif
|
|
|
|
#ifndef _Py_NO_SANITIZE_UNDEFINED
|
|
|
|
# define _Py_NO_SANITIZE_UNDEFINED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
typedef struct {
|
|
|
|
int signum;
|
|
|
|
int enabled;
|
|
|
|
const char* name;
|
|
|
|
_Py_sighandler_t previous;
|
|
|
|
int all_threads;
|
|
|
|
} fault_handler_t;
|
|
|
|
|
2022-12-13 00:58:46 +08:00
|
|
|
#define fatal_error _PyRuntime.faulthandler.fatal_error
|
|
|
|
#define thread _PyRuntime.faulthandler.thread
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
#ifdef FAULTHANDLER_USER
|
2022-12-13 00:58:46 +08:00
|
|
|
#define user_signals _PyRuntime.faulthandler.user_signals
|
|
|
|
typedef struct faulthandler_user_signal user_signal_t;
|
2011-07-14 05:39:53 +08:00
|
|
|
static void faulthandler_user(int signum);
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif /* FAULTHANDLER_USER */
|
|
|
|
|
|
|
|
|
|
|
|
static fault_handler_t faulthandler_handlers[] = {
|
|
|
|
#ifdef SIGBUS
|
|
|
|
{SIGBUS, 0, "Bus error", },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
|
|
{SIGILL, 0, "Illegal instruction", },
|
|
|
|
#endif
|
2024-07-19 16:06:02 +08:00
|
|
|
{SIGFPE, 0, "Floating-point exception", },
|
2011-04-01 18:13:55 +08:00
|
|
|
{SIGABRT, 0, "Aborted", },
|
2011-03-31 07:31:06 +08:00
|
|
|
/* define SIGSEGV at the end to make it the default choice if searching the
|
|
|
|
handler fails in faulthandler_fatal_error() */
|
|
|
|
{SIGSEGV, 0, "Segmentation fault", }
|
|
|
|
};
|
2016-03-23 17:39:17 +08:00
|
|
|
static const size_t faulthandler_nsignals = \
|
2011-09-29 06:42:28 +08:00
|
|
|
Py_ARRAY_LENGTH(faulthandler_handlers);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
2022-12-13 00:58:46 +08:00
|
|
|
# define stack _PyRuntime.faulthandler.stack
|
|
|
|
# define old_stack _PyRuntime.faulthandler.old_stack
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the file descriptor of a file by calling its fileno() method and then
|
|
|
|
call its flush() method.
|
|
|
|
|
|
|
|
If file is NULL or Py_None, use sys.stderr as the new file.
|
2015-03-12 22:32:03 +08:00
|
|
|
If file is an integer, it will be treated as file descriptor.
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
On success, return the file descriptor and write the new file into *file_ptr.
|
|
|
|
On error, return -1. */
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
static int
|
|
|
|
faulthandler_get_fileno(PyObject **file_ptr)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
long fd_long;
|
|
|
|
int fd;
|
2015-03-12 22:32:03 +08:00
|
|
|
PyObject *file = *file_ptr;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
if (file == NULL || file == Py_None) {
|
2022-02-09 04:39:07 +08:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
2011-03-31 07:31:06 +08:00
|
|
|
if (file == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
|
2015-03-12 22:32:03 +08:00
|
|
|
return -1;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
2014-05-14 23:15:50 +08:00
|
|
|
if (file == Py_None) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "sys.stderr is None");
|
2015-03-12 22:32:03 +08:00
|
|
|
return -1;
|
2014-05-14 23:15:50 +08:00
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
2015-03-12 22:32:03 +08:00
|
|
|
else if (PyLong_Check(file)) {
|
2024-02-06 04:51:11 +08:00
|
|
|
if (PyBool_Check(file)) {
|
|
|
|
if (PyErr_WarnEx(PyExc_RuntimeWarning,
|
|
|
|
"bool is used as a file descriptor", 1))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 07:01:30 +08:00
|
|
|
fd = PyLong_AsInt(file);
|
2015-03-12 22:32:03 +08:00
|
|
|
if (fd == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
2016-09-09 02:21:54 +08:00
|
|
|
if (fd < 0) {
|
2015-03-12 22:32:03 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2023-03-26 05:27:02 +08:00
|
|
|
"file is not a valid file descriptor");
|
2015-03-12 22:32:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*file_ptr = NULL;
|
|
|
|
return fd;
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2022-02-09 04:39:07 +08:00
|
|
|
result = PyObject_CallMethodNoArgs(file, &_Py_ID(fileno));
|
2011-03-31 07:31:06 +08:00
|
|
|
if (result == NULL)
|
2015-03-12 22:32:03 +08:00
|
|
|
return -1;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
fd = -1;
|
|
|
|
if (PyLong_Check(result)) {
|
|
|
|
fd_long = PyLong_AsLong(result);
|
|
|
|
if (0 <= fd_long && fd_long < INT_MAX)
|
|
|
|
fd = (int)fd_long;
|
|
|
|
}
|
|
|
|
Py_DECREF(result);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"file.fileno() is not a valid file descriptor");
|
2015-03-12 22:32:03 +08:00
|
|
|
return -1;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
2023-09-23 14:35:30 +08:00
|
|
|
if (_PyFile_Flush(file) < 0) {
|
2011-03-31 07:31:06 +08:00
|
|
|
/* ignore flush() error */
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2015-03-12 22:32:03 +08:00
|
|
|
*file_ptr = file;
|
|
|
|
return fd;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
2011-04-09 06:47:23 +08:00
|
|
|
/* Get the state of the current thread: only call this function if the current
|
|
|
|
thread holds the GIL. Raise an exception on error. */
|
|
|
|
static PyThreadState*
|
|
|
|
get_thread_state(void)
|
|
|
|
{
|
2021-02-19 20:21:28 +08:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2011-04-09 06:47:23 +08:00
|
|
|
if (tstate == NULL) {
|
2016-03-17 05:45:24 +08:00
|
|
|
/* just in case but very unlikely... */
|
2011-04-09 06:47:23 +08:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"unable to get the current thread state");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tstate;
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:48:58 +08:00
|
|
|
static void
|
|
|
|
faulthandler_dump_traceback(int fd, int all_threads,
|
|
|
|
PyInterpreterState *interp)
|
|
|
|
{
|
|
|
|
static volatile int reentrant = 0;
|
|
|
|
|
|
|
|
if (reentrant)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reentrant = 1;
|
|
|
|
|
|
|
|
/* SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals and
|
|
|
|
are thus delivered to the thread that caused the fault. Get the Python
|
|
|
|
thread state of the current thread.
|
|
|
|
|
|
|
|
PyThreadState_Get() doesn't give the state of the thread that caused the
|
|
|
|
fault if the thread released the GIL, and so this function cannot be
|
2017-10-06 18:41:34 +08:00
|
|
|
used. Read the thread specific storage (TSS) instead: call
|
2015-04-02 00:48:58 +08:00
|
|
|
PyGILState_GetThisThreadState(). */
|
2023-09-29 10:04:06 +08:00
|
|
|
PyThreadState *tstate = PyGILState_GetThisThreadState();
|
2015-04-02 00:48:58 +08:00
|
|
|
|
2016-03-17 05:45:24 +08:00
|
|
|
if (all_threads) {
|
|
|
|
(void)_Py_DumpTracebackThreads(fd, NULL, tstate);
|
|
|
|
}
|
2015-04-02 00:48:58 +08:00
|
|
|
else {
|
|
|
|
if (tstate != NULL)
|
|
|
|
_Py_DumpTraceback(fd, tstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
reentrant = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
static PyObject*
|
|
|
|
faulthandler_dump_traceback_py(PyObject *self,
|
|
|
|
PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
static char *kwlist[] = {"file", "all_threads", NULL};
|
|
|
|
PyObject *file = NULL;
|
2011-05-07 18:43:00 +08:00
|
|
|
int all_threads = 1;
|
2011-03-31 07:31:06 +08:00
|
|
|
PyThreadState *tstate;
|
|
|
|
const char *errmsg;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
|
2022-12-04 03:52:21 +08:00
|
|
|
"|Op:dump_traceback", kwlist,
|
2011-03-31 07:31:06 +08:00
|
|
|
&file, &all_threads))
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
fd = faulthandler_get_fileno(&file);
|
|
|
|
if (fd < 0)
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-09 06:47:23 +08:00
|
|
|
tstate = get_thread_state();
|
|
|
|
if (tstate == NULL)
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (all_threads) {
|
2016-03-17 05:45:24 +08:00
|
|
|
errmsg = _Py_DumpTracebackThreads(fd, NULL, tstate);
|
2011-03-31 07:31:06 +08:00
|
|
|
if (errmsg != NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, errmsg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_Py_DumpTraceback(fd, tstate);
|
|
|
|
}
|
2015-04-02 00:48:58 +08:00
|
|
|
|
|
|
|
if (PyErr_CheckSignals())
|
|
|
|
return NULL;
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:39:17 +08:00
|
|
|
static void
|
|
|
|
faulthandler_disable_fatal_handler(fault_handler_t *handler)
|
|
|
|
{
|
|
|
|
if (!handler->enabled)
|
|
|
|
return;
|
|
|
|
handler->enabled = 0;
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
(void)sigaction(handler->signum, &handler->previous, NULL);
|
|
|
|
#else
|
|
|
|
(void)signal(handler->signum, handler->previous);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
/* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals.
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
Display the current Python traceback, restore the previous handler and call
|
|
|
|
the previous handler.
|
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
On Windows, don't explicitly call the previous handler, because the Windows
|
2011-03-31 07:31:06 +08:00
|
|
|
signal handler would not be called (for an unknown reason). The execution of
|
|
|
|
the program continues at faulthandler_fatal_error() exit, but the same
|
|
|
|
instruction will raise the same fault (signal), and so the previous handler
|
|
|
|
will be called.
|
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
This function is signal-safe and should only call signal-safe functions. */
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
static void
|
2011-04-07 17:39:03 +08:00
|
|
|
faulthandler_fatal_error(int signum)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
|
|
|
const int fd = fatal_error.fd;
|
2016-03-23 17:39:17 +08:00
|
|
|
size_t i;
|
2011-03-31 07:31:06 +08:00
|
|
|
fault_handler_t *handler = NULL;
|
2011-05-07 18:20:11 +08:00
|
|
|
int save_errno = errno;
|
2022-11-07 09:19:25 +08:00
|
|
|
int found = 0;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
if (!fatal_error.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i=0; i < faulthandler_nsignals; i++) {
|
|
|
|
handler = &faulthandler_handlers[i];
|
2022-11-07 09:19:25 +08:00
|
|
|
if (handler->signum == signum) {
|
|
|
|
found = 1;
|
2011-03-31 07:31:06 +08:00
|
|
|
break;
|
2022-11-07 09:19:25 +08:00
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
if (handler == NULL) {
|
|
|
|
/* faulthandler_nsignals == 0 (unlikely) */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the previous handler */
|
2016-03-23 17:39:17 +08:00
|
|
|
faulthandler_disable_fatal_handler(handler);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2022-11-07 09:19:25 +08:00
|
|
|
if (found) {
|
|
|
|
PUTS(fd, "Fatal Python error: ");
|
|
|
|
PUTS(fd, handler->name);
|
|
|
|
PUTS(fd, "\n\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char unknown_signum[23] = {0,};
|
|
|
|
snprintf(unknown_signum, 23, "%d", signum);
|
|
|
|
PUTS(fd, "Fatal Python error from unexpected signum: ");
|
|
|
|
PUTS(fd, unknown_signum);
|
|
|
|
PUTS(fd, "\n\n");
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2015-04-02 00:48:58 +08:00
|
|
|
faulthandler_dump_traceback(fd, fatal_error.all_threads,
|
|
|
|
fatal_error.interp);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2021-01-19 03:47:13 +08:00
|
|
|
_Py_DumpExtensionModules(fd, fatal_error.interp);
|
|
|
|
|
2011-05-07 18:20:11 +08:00
|
|
|
errno = save_errno;
|
2011-04-01 18:08:57 +08:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (signum == SIGSEGV) {
|
2011-05-12 02:56:08 +08:00
|
|
|
/* don't explicitly call the previous handler for SIGSEGV in this signal
|
2011-04-01 18:08:57 +08:00
|
|
|
handler, because the Windows signal handler would not be called */
|
|
|
|
return;
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif
|
2013-12-14 09:52:19 +08:00
|
|
|
/* call the previous signal handler: it is called immediately if we use
|
2011-04-01 18:08:57 +08:00
|
|
|
sigaction() thanks to SA_NODEFER flag, otherwise it is deferred */
|
|
|
|
raise(signum);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:39:17 +08:00
|
|
|
#ifdef MS_WINDOWS
|
2017-10-10 00:52:32 +08:00
|
|
|
static int
|
|
|
|
faulthandler_ignore_exception(DWORD code)
|
|
|
|
{
|
|
|
|
/* bpo-30557: ignore exceptions which are not errors */
|
|
|
|
if (!(code & 0x80000000)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* bpo-31701: ignore MSC and COM exceptions
|
|
|
|
E0000000 + code */
|
|
|
|
if (code == 0xE06D7363 /* MSC exception ("Emsc") */
|
|
|
|
|| code == 0xE0434352 /* COM Callable Runtime exception ("ECCR") */) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* Interesting exception: log it with the Python traceback */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:39:17 +08:00
|
|
|
static LONG WINAPI
|
|
|
|
faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info)
|
|
|
|
{
|
|
|
|
const int fd = fatal_error.fd;
|
|
|
|
DWORD code = exc_info->ExceptionRecord->ExceptionCode;
|
2016-03-23 21:44:14 +08:00
|
|
|
DWORD flags = exc_info->ExceptionRecord->ExceptionFlags;
|
2016-03-23 17:39:17 +08:00
|
|
|
|
2017-10-10 00:52:32 +08:00
|
|
|
if (faulthandler_ignore_exception(code)) {
|
|
|
|
/* ignore the exception: call the next exception handler */
|
2016-03-23 21:44:14 +08:00
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUTS(fd, "Windows fatal exception: ");
|
2016-03-23 17:39:17 +08:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
/* only format most common errors */
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION: PUTS(fd, "access violation"); break;
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO: PUTS(fd, "float divide by zero"); break;
|
|
|
|
case EXCEPTION_FLT_OVERFLOW: PUTS(fd, "float overflow"); break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO: PUTS(fd, "int divide by zero"); break;
|
|
|
|
case EXCEPTION_INT_OVERFLOW: PUTS(fd, "integer overflow"); break;
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR: PUTS(fd, "page error"); break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW: PUTS(fd, "stack overflow"); break;
|
|
|
|
default:
|
2017-06-06 06:54:15 +08:00
|
|
|
PUTS(fd, "code 0x");
|
|
|
|
_Py_DumpHexadecimal(fd, code, 8);
|
2016-03-23 17:39:17 +08:00
|
|
|
}
|
|
|
|
PUTS(fd, "\n\n");
|
|
|
|
|
|
|
|
if (code == EXCEPTION_ACCESS_VIOLATION) {
|
|
|
|
/* disable signal handler for SIGSEGV */
|
2017-04-22 00:06:13 +08:00
|
|
|
for (size_t i=0; i < faulthandler_nsignals; i++) {
|
2016-03-23 17:39:17 +08:00
|
|
|
fault_handler_t *handler = &faulthandler_handlers[i];
|
|
|
|
if (handler->signum == SIGSEGV) {
|
|
|
|
faulthandler_disable_fatal_handler(handler);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
faulthandler_dump_traceback(fd, fatal_error.all_threads,
|
|
|
|
fatal_error.interp);
|
|
|
|
|
|
|
|
/* call the next exception handler */
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-21 20:40:42 +08:00
|
|
|
|
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
static int
|
|
|
|
faulthandler_allocate_stack(void)
|
|
|
|
{
|
|
|
|
if (stack.ss_sp != NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Allocate an alternate stack for faulthandler() signal handler
|
|
|
|
to be able to execute a signal handler on a stack overflow error */
|
|
|
|
stack.ss_sp = PyMem_Malloc(stack.ss_size);
|
|
|
|
if (stack.ss_sp == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = sigaltstack(&stack, &old_stack);
|
|
|
|
if (err) {
|
2023-08-27 05:35:06 +08:00
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2019-08-21 20:40:42 +08:00
|
|
|
/* Release the stack to retry sigaltstack() next time */
|
|
|
|
PyMem_Free(stack.ss_sp);
|
|
|
|
stack.ss_sp = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-04-01 18:13:55 +08:00
|
|
|
/* Install the handler for fatal signals, faulthandler_fatal_error(). */
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2016-05-18 07:06:01 +08:00
|
|
|
static int
|
2016-03-23 17:39:17 +08:00
|
|
|
faulthandler_enable(void)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2016-03-23 17:39:17 +08:00
|
|
|
if (fatal_error.enabled) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fatal_error.enabled = 1;
|
|
|
|
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
if (faulthandler_allocate_stack() < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-22 00:06:13 +08:00
|
|
|
for (size_t i=0; i < faulthandler_nsignals; i++) {
|
2016-03-23 22:19:12 +08:00
|
|
|
fault_handler_t *handler;
|
|
|
|
int err;
|
2016-03-23 17:39:17 +08:00
|
|
|
|
2016-03-23 22:19:12 +08:00
|
|
|
handler = &faulthandler_handlers[i];
|
|
|
|
assert(!handler->enabled);
|
2016-03-23 17:39:17 +08:00
|
|
|
#ifdef HAVE_SIGACTION
|
2019-08-21 20:40:42 +08:00
|
|
|
struct sigaction action;
|
2016-03-23 17:39:17 +08:00
|
|
|
action.sa_handler = faulthandler_fatal_error;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
/* Do not prevent the signal from being received from within
|
|
|
|
its own signal handler */
|
|
|
|
action.sa_flags = SA_NODEFER;
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
assert(stack.ss_sp != NULL);
|
|
|
|
/* Call the signal handler on an alternate signal stack
|
|
|
|
provided by sigaltstack() */
|
|
|
|
action.sa_flags |= SA_ONSTACK;
|
2016-03-23 17:39:17 +08:00
|
|
|
#endif
|
|
|
|
err = sigaction(handler->signum, &action, &handler->previous);
|
|
|
|
#else
|
|
|
|
handler->previous = signal(handler->signum,
|
2019-08-21 20:40:42 +08:00
|
|
|
faulthandler_fatal_error);
|
2016-03-23 17:39:17 +08:00
|
|
|
err = (handler->previous == SIG_ERR);
|
|
|
|
#endif
|
|
|
|
if (err) {
|
|
|
|
PyErr_SetFromErrno(PyExc_RuntimeError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler->enabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
2017-04-22 00:06:13 +08:00
|
|
|
assert(fatal_error.exc_handler == NULL);
|
|
|
|
fatal_error.exc_handler = AddVectoredExceptionHandler(1, faulthandler_exc_handler);
|
2016-03-23 17:39:17 +08:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
faulthandler_py_enable(PyObject *self, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
static char *kwlist[] = {"file", "all_threads", NULL};
|
|
|
|
PyObject *file = NULL;
|
|
|
|
int all_threads = 1;
|
2011-03-31 07:31:06 +08:00
|
|
|
int fd;
|
2011-04-09 06:47:23 +08:00
|
|
|
PyThreadState *tstate;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
|
2022-12-04 03:52:21 +08:00
|
|
|
"|Op:enable", kwlist, &file, &all_threads))
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
fd = faulthandler_get_fileno(&file);
|
|
|
|
if (fd < 0)
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-09 06:47:23 +08:00
|
|
|
tstate = get_thread_state();
|
|
|
|
if (tstate == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
Py_XINCREF(file);
|
2016-04-06 14:45:48 +08:00
|
|
|
Py_XSETREF(fatal_error.file, file);
|
2011-03-31 07:31:06 +08:00
|
|
|
fatal_error.fd = fd;
|
|
|
|
fatal_error.all_threads = all_threads;
|
2020-03-14 06:38:08 +08:00
|
|
|
fatal_error.interp = PyThreadState_GetInterpreter(tstate);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2016-03-23 17:39:17 +08:00
|
|
|
if (faulthandler_enable() < 0) {
|
|
|
|
return NULL;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
2016-03-23 17:39:17 +08:00
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
faulthandler_disable(void)
|
|
|
|
{
|
|
|
|
if (fatal_error.enabled) {
|
|
|
|
fatal_error.enabled = 0;
|
2017-04-22 00:06:13 +08:00
|
|
|
for (size_t i=0; i < faulthandler_nsignals; i++) {
|
|
|
|
fault_handler_t *handler;
|
2011-03-31 07:31:06 +08:00
|
|
|
handler = &faulthandler_handlers[i];
|
2016-03-23 17:39:17 +08:00
|
|
|
faulthandler_disable_fatal_handler(handler);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-22 00:06:13 +08:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (fatal_error.exc_handler != NULL) {
|
|
|
|
RemoveVectoredExceptionHandler(fatal_error.exc_handler);
|
|
|
|
fatal_error.exc_handler = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_CLEAR(fatal_error.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2018-04-30 02:59:33 +08:00
|
|
|
faulthandler_disable_py(PyObject *self, PyObject *Py_UNUSED(ignored))
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
|
|
|
if (!fatal_error.enabled) {
|
2017-01-23 15:47:21 +08:00
|
|
|
Py_RETURN_FALSE;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
faulthandler_disable();
|
2017-01-23 15:47:21 +08:00
|
|
|
Py_RETURN_TRUE;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2018-04-30 02:59:33 +08:00
|
|
|
faulthandler_is_enabled(PyObject *self, PyObject *Py_UNUSED(ignored))
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
|
|
|
return PyBool_FromLong(fatal_error.enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
faulthandler_thread(void *unused)
|
|
|
|
{
|
|
|
|
PyLockStatus st;
|
|
|
|
const char* errmsg;
|
|
|
|
int ok;
|
2011-04-20 05:30:57 +08:00
|
|
|
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
|
2011-04-04 17:05:21 +08:00
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
/* we don't want to receive any signal */
|
|
|
|
sigfillset(&set);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &set, NULL);
|
|
|
|
#endif
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
st = PyThread_acquire_lock_timed(thread.cancel_event,
|
2011-04-08 19:00:31 +08:00
|
|
|
thread.timeout_us, 0);
|
2011-03-31 07:31:06 +08:00
|
|
|
if (st == PY_LOCK_ACQUIRED) {
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_release_lock(thread.cancel_event);
|
2011-03-31 07:31:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Timeout => dump traceback */
|
|
|
|
assert(st == PY_LOCK_FAILURE);
|
|
|
|
|
2023-08-22 22:28:20 +08:00
|
|
|
(void)_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len);
|
2011-04-08 19:39:59 +08:00
|
|
|
|
2016-03-17 05:45:24 +08:00
|
|
|
errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, NULL);
|
2011-03-31 07:31:06 +08:00
|
|
|
ok = (errmsg == NULL);
|
|
|
|
|
|
|
|
if (thread.exit)
|
|
|
|
_exit(1);
|
|
|
|
} while (ok && thread.repeat);
|
|
|
|
|
|
|
|
/* The only way out */
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_release_lock(thread.running);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-09-22 14:58:55 +08:00
|
|
|
cancel_dump_traceback_later(void)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2019-08-30 00:30:04 +08:00
|
|
|
/* If not scheduled, nothing to cancel */
|
|
|
|
if (!thread.cancel_event) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
/* Notify cancellation */
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_release_lock(thread.cancel_event);
|
|
|
|
|
2011-04-01 09:00:05 +08:00
|
|
|
/* Wait for thread to join */
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_acquire_lock(thread.running, 1);
|
|
|
|
PyThread_release_lock(thread.running);
|
|
|
|
|
|
|
|
/* The main thread should always hold the cancel_event lock */
|
|
|
|
PyThread_acquire_lock(thread.cancel_event, 1);
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_CLEAR(thread.file);
|
2011-04-08 19:39:59 +08:00
|
|
|
if (thread.header) {
|
2013-07-08 05:30:24 +08:00
|
|
|
PyMem_Free(thread.header);
|
2011-04-08 19:39:59 +08:00
|
|
|
thread.header = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 22:27:12 +08:00
|
|
|
#define SEC_TO_US (1000 * 1000)
|
|
|
|
|
2011-04-08 19:39:59 +08:00
|
|
|
static char*
|
2024-02-20 23:02:27 +08:00
|
|
|
format_timeout(PyTime_t us)
|
2011-04-08 19:39:59 +08:00
|
|
|
{
|
2017-10-27 22:27:12 +08:00
|
|
|
unsigned long sec, min, hour;
|
2011-04-08 19:39:59 +08:00
|
|
|
char buffer[100];
|
|
|
|
|
2017-10-27 22:27:12 +08:00
|
|
|
/* the downcast is safe: the caller check that 0 < us <= LONG_MAX */
|
|
|
|
sec = (unsigned long)(us / SEC_TO_US);
|
|
|
|
us %= SEC_TO_US;
|
|
|
|
|
2011-04-08 19:39:59 +08:00
|
|
|
min = sec / 60;
|
|
|
|
sec %= 60;
|
|
|
|
hour = min / 60;
|
|
|
|
min %= 60;
|
|
|
|
|
2017-10-27 22:27:12 +08:00
|
|
|
if (us != 0) {
|
2011-04-08 19:39:59 +08:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
2017-10-27 22:27:12 +08:00
|
|
|
"Timeout (%lu:%02lu:%02lu.%06u)!\n",
|
|
|
|
hour, min, sec, (unsigned int)us);
|
|
|
|
}
|
|
|
|
else {
|
2011-04-08 19:39:59 +08:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"Timeout (%lu:%02lu:%02lu)!\n",
|
|
|
|
hour, min, sec);
|
2017-10-27 22:27:12 +08:00
|
|
|
}
|
2013-07-08 05:30:24 +08:00
|
|
|
return _PyMem_Strdup(buffer);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2012-09-22 14:58:55 +08:00
|
|
|
faulthandler_dump_traceback_later(PyObject *self,
|
2011-04-07 17:37:19 +08:00
|
|
|
PyObject *args, PyObject *kwargs)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
|
|
|
static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL};
|
2017-10-27 22:27:12 +08:00
|
|
|
PyObject *timeout_obj;
|
2024-02-20 23:02:27 +08:00
|
|
|
PyTime_t timeout, timeout_us;
|
2011-03-31 07:31:06 +08:00
|
|
|
int repeat = 0;
|
|
|
|
PyObject *file = NULL;
|
|
|
|
int fd;
|
|
|
|
int exit = 0;
|
2011-04-07 17:37:19 +08:00
|
|
|
PyThreadState *tstate;
|
2011-04-08 19:39:59 +08:00
|
|
|
char *header;
|
|
|
|
size_t header_len;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
|
2017-10-27 22:27:12 +08:00
|
|
|
"O|iOi:dump_traceback_later", kwlist,
|
|
|
|
&timeout_obj, &repeat, &file, &exit))
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
2017-10-27 22:27:12 +08:00
|
|
|
|
|
|
|
if (_PyTime_FromSecondsObject(&timeout, timeout_obj,
|
|
|
|
_PyTime_ROUND_TIMEOUT) < 0) {
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-27 22:27:12 +08:00
|
|
|
timeout_us = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_TIMEOUT);
|
2011-04-08 19:00:31 +08:00
|
|
|
if (timeout_us <= 0) {
|
2011-03-31 07:31:06 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "timeout must be greater than 0");
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-27 22:27:12 +08:00
|
|
|
/* Limit to LONG_MAX seconds for format_timeout() */
|
2021-09-30 16:16:51 +08:00
|
|
|
if (timeout_us > PY_TIMEOUT_MAX || timeout_us / SEC_TO_US > LONG_MAX) {
|
2017-10-27 22:27:12 +08:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"timeout value is too large");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2011-04-09 06:47:23 +08:00
|
|
|
tstate = get_thread_state();
|
2019-08-21 20:40:42 +08:00
|
|
|
if (tstate == NULL) {
|
2011-04-07 17:37:19 +08:00
|
|
|
return NULL;
|
2019-08-21 20:40:42 +08:00
|
|
|
}
|
2011-04-07 17:37:19 +08:00
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
fd = faulthandler_get_fileno(&file);
|
2019-08-21 20:40:42 +08:00
|
|
|
if (fd < 0) {
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
2019-08-21 20:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!thread.running) {
|
|
|
|
thread.running = PyThread_allocate_lock();
|
|
|
|
if (!thread.running) {
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!thread.cancel_event) {
|
|
|
|
thread.cancel_event = PyThread_allocate_lock();
|
|
|
|
if (!thread.cancel_event || !thread.running) {
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cancel_event starts to be acquired: it's only released to cancel
|
|
|
|
the thread. */
|
|
|
|
PyThread_acquire_lock(thread.cancel_event, 1);
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2011-04-08 19:39:59 +08:00
|
|
|
/* format the timeout */
|
2017-10-27 22:27:12 +08:00
|
|
|
header = format_timeout(timeout_us);
|
2019-08-21 20:40:42 +08:00
|
|
|
if (header == NULL) {
|
2011-04-08 19:39:59 +08:00
|
|
|
return PyErr_NoMemory();
|
2019-08-21 20:40:42 +08:00
|
|
|
}
|
2011-04-08 19:39:59 +08:00
|
|
|
header_len = strlen(header);
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
/* Cancel previous thread, if running */
|
2012-09-22 14:58:55 +08:00
|
|
|
cancel_dump_traceback_later();
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
Py_XINCREF(file);
|
2016-04-06 14:45:48 +08:00
|
|
|
Py_XSETREF(thread.file, file);
|
2011-03-31 07:31:06 +08:00
|
|
|
thread.fd = fd;
|
2017-10-27 22:27:12 +08:00
|
|
|
/* the downcast is safe: we check that 0 < timeout_us < PY_TIMEOUT_MAX */
|
|
|
|
thread.timeout_us = (PY_TIMEOUT_T)timeout_us;
|
2011-03-31 07:31:06 +08:00
|
|
|
thread.repeat = repeat;
|
2020-03-14 06:38:08 +08:00
|
|
|
thread.interp = PyThreadState_GetInterpreter(tstate);
|
2011-03-31 07:31:06 +08:00
|
|
|
thread.exit = exit;
|
2011-04-08 19:39:59 +08:00
|
|
|
thread.header = header;
|
|
|
|
thread.header_len = header_len;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
/* Arm these locks to serve as events when released */
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_acquire_lock(thread.running, 1);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2017-03-23 21:48:39 +08:00
|
|
|
if (PyThread_start_new_thread(faulthandler_thread, NULL) == PYTHREAD_INVALID_THREAD_ID) {
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_release_lock(thread.running);
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_CLEAR(thread.file);
|
2013-07-08 05:30:24 +08:00
|
|
|
PyMem_Free(header);
|
2011-04-08 19:39:59 +08:00
|
|
|
thread.header = NULL;
|
2011-03-31 07:31:06 +08:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"unable to start watchdog thread");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2018-04-30 02:59:33 +08:00
|
|
|
faulthandler_cancel_dump_traceback_later_py(PyObject *self,
|
|
|
|
PyObject *Py_UNUSED(ignored))
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2012-09-22 14:58:55 +08:00
|
|
|
cancel_dump_traceback_later();
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:40:42 +08:00
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
#ifdef FAULTHANDLER_USER
|
2011-07-14 05:39:53 +08:00
|
|
|
static int
|
2019-08-21 20:40:42 +08:00
|
|
|
faulthandler_register(int signum, int chain, _Py_sighandler_t *previous_p)
|
2011-07-14 05:39:53 +08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction action;
|
|
|
|
action.sa_handler = faulthandler_user;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
/* if the signal is received while the kernel is executing a system
|
|
|
|
call, try to restart the system call instead of interrupting it and
|
|
|
|
return EINTR. */
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
if (chain) {
|
|
|
|
/* do not prevent the signal from being received from within its
|
|
|
|
own signal handler */
|
|
|
|
action.sa_flags = SA_NODEFER;
|
|
|
|
}
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
assert(stack.ss_sp != NULL);
|
|
|
|
/* Call the signal handler on an alternate signal stack
|
|
|
|
provided by sigaltstack() */
|
|
|
|
action.sa_flags |= SA_ONSTACK;
|
2011-07-14 05:39:53 +08:00
|
|
|
#endif
|
2019-08-21 20:40:42 +08:00
|
|
|
return sigaction(signum, &action, previous_p);
|
2011-07-14 05:39:53 +08:00
|
|
|
#else
|
|
|
|
_Py_sighandler_t previous;
|
|
|
|
previous = signal(signum, faulthandler_user);
|
2019-08-21 20:40:42 +08:00
|
|
|
if (previous_p != NULL) {
|
|
|
|
*previous_p = previous;
|
|
|
|
}
|
2011-07-14 05:39:53 +08:00
|
|
|
return (previous == SIG_ERR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
/* Handler of user signals (e.g. SIGUSR1).
|
|
|
|
|
|
|
|
Dump the traceback of the current thread, or of all threads if
|
|
|
|
thread.all_threads is true.
|
|
|
|
|
|
|
|
This function is signal safe and should only call signal safe functions. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
faulthandler_user(int signum)
|
|
|
|
{
|
|
|
|
user_signal_t *user;
|
2011-05-07 18:20:11 +08:00
|
|
|
int save_errno = errno;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
user = &user_signals[signum];
|
|
|
|
if (!user->enabled)
|
|
|
|
return;
|
|
|
|
|
2015-04-02 00:48:58 +08:00
|
|
|
faulthandler_dump_traceback(user->fd, user->all_threads, user->interp);
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2011-07-14 05:39:53 +08:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
if (user->chain) {
|
|
|
|
(void)sigaction(signum, &user->previous, NULL);
|
2012-08-09 08:43:41 +08:00
|
|
|
errno = save_errno;
|
|
|
|
|
2011-07-14 05:39:53 +08:00
|
|
|
/* call the previous signal handler */
|
|
|
|
raise(signum);
|
2012-08-09 08:43:41 +08:00
|
|
|
|
|
|
|
save_errno = errno;
|
2011-07-14 05:39:53 +08:00
|
|
|
(void)faulthandler_register(signum, user->chain, NULL);
|
2012-08-09 08:43:41 +08:00
|
|
|
errno = save_errno;
|
2011-07-14 05:39:53 +08:00
|
|
|
}
|
|
|
|
#else
|
2022-09-08 18:20:22 +08:00
|
|
|
if (user->chain && user->previous != NULL) {
|
2012-08-09 08:43:41 +08:00
|
|
|
errno = save_errno;
|
2011-07-14 05:39:53 +08:00
|
|
|
/* call the previous signal handler */
|
|
|
|
user->previous(signum);
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-01 21:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_signum(int signum)
|
|
|
|
{
|
2017-04-22 00:06:13 +08:00
|
|
|
for (size_t i=0; i < faulthandler_nsignals; i++) {
|
2011-04-01 21:37:12 +08:00
|
|
|
if (faulthandler_handlers[i].signum == signum) {
|
|
|
|
PyErr_Format(PyExc_RuntimeError,
|
|
|
|
"signal %i cannot be registered, "
|
|
|
|
"use enable() instead",
|
|
|
|
signum);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 06:13:31 +08:00
|
|
|
if (signum < 1 || Py_NSIG <= signum) {
|
2011-04-01 21:37:12 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "signal number out of range");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2011-07-14 05:39:53 +08:00
|
|
|
faulthandler_register_py(PyObject *self,
|
|
|
|
PyObject *args, PyObject *kwargs)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2011-07-14 05:39:53 +08:00
|
|
|
static char *kwlist[] = {"signum", "file", "all_threads", "chain", NULL};
|
2011-03-31 07:31:06 +08:00
|
|
|
int signum;
|
|
|
|
PyObject *file = NULL;
|
2011-05-07 18:43:00 +08:00
|
|
|
int all_threads = 1;
|
2011-07-14 05:39:53 +08:00
|
|
|
int chain = 0;
|
2011-03-31 07:31:06 +08:00
|
|
|
int fd;
|
|
|
|
user_signal_t *user;
|
|
|
|
_Py_sighandler_t previous;
|
2011-04-01 21:37:12 +08:00
|
|
|
PyThreadState *tstate;
|
2011-03-31 07:31:06 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
|
2022-12-04 03:52:21 +08:00
|
|
|
"i|Opp:register", kwlist,
|
2011-07-14 05:39:53 +08:00
|
|
|
&signum, &file, &all_threads, &chain))
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-01 21:37:12 +08:00
|
|
|
if (!check_signum(signum))
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-09 06:47:23 +08:00
|
|
|
tstate = get_thread_state();
|
|
|
|
if (tstate == NULL)
|
2011-04-01 21:37:12 +08:00
|
|
|
return NULL;
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
fd = faulthandler_get_fileno(&file);
|
|
|
|
if (fd < 0)
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (user_signals == NULL) {
|
2022-04-26 06:13:31 +08:00
|
|
|
user_signals = PyMem_Calloc(Py_NSIG, sizeof(user_signal_t));
|
2011-03-31 07:31:06 +08:00
|
|
|
if (user_signals == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
user = &user_signals[signum];
|
|
|
|
|
|
|
|
if (!user->enabled) {
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
if (faulthandler_allocate_stack() < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-14 05:39:53 +08:00
|
|
|
err = faulthandler_register(signum, chain, &previous);
|
2011-03-31 07:31:06 +08:00
|
|
|
if (err) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-02 06:14:56 +08:00
|
|
|
|
|
|
|
user->previous = previous;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 22:32:03 +08:00
|
|
|
Py_XINCREF(file);
|
2016-04-06 14:45:48 +08:00
|
|
|
Py_XSETREF(user->file, file);
|
2011-03-31 07:31:06 +08:00
|
|
|
user->fd = fd;
|
|
|
|
user->all_threads = all_threads;
|
2011-07-14 05:39:53 +08:00
|
|
|
user->chain = chain;
|
2020-03-14 06:38:08 +08:00
|
|
|
user->interp = PyThreadState_GetInterpreter(tstate);
|
2011-03-31 07:31:06 +08:00
|
|
|
user->enabled = 1;
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
faulthandler_unregister(user_signal_t *user, int signum)
|
|
|
|
{
|
2011-04-01 18:56:17 +08:00
|
|
|
if (!user->enabled)
|
2011-03-31 07:31:06 +08:00
|
|
|
return 0;
|
|
|
|
user->enabled = 0;
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
(void)sigaction(signum, &user->previous, NULL);
|
|
|
|
#else
|
|
|
|
(void)signal(signum, user->previous);
|
|
|
|
#endif
|
|
|
|
Py_CLEAR(user->file);
|
|
|
|
user->fd = -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
faulthandler_unregister_py(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
int signum;
|
|
|
|
user_signal_t *user;
|
|
|
|
int change;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i:unregister", &signum))
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-01 21:37:12 +08:00
|
|
|
if (!check_signum(signum))
|
2011-03-31 07:31:06 +08:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-08 18:48:15 +08:00
|
|
|
if (user_signals == NULL)
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
user = &user_signals[signum];
|
|
|
|
change = faulthandler_unregister(user, signum);
|
|
|
|
return PyBool_FromLong(change);
|
|
|
|
}
|
|
|
|
#endif /* FAULTHANDLER_USER */
|
|
|
|
|
|
|
|
|
2014-09-30 19:40:12 +08:00
|
|
|
static void
|
|
|
|
faulthandler_suppress_crash_report(void)
|
|
|
|
{
|
2023-03-10 05:09:12 +08:00
|
|
|
#ifdef MS_WINDOWS_DESKTOP
|
2014-09-30 19:40:12 +08:00
|
|
|
UINT mode;
|
|
|
|
|
|
|
|
/* Configure Windows to not display the Windows Error Reporting dialog */
|
|
|
|
mode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
|
|
|
|
SetErrorMode(mode | SEM_NOGPFAULTERRORBOX);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
struct rlimit rl;
|
|
|
|
|
|
|
|
/* Disable creation of core dump */
|
2017-12-11 20:57:12 +08:00
|
|
|
if (getrlimit(RLIMIT_CORE, &rl) == 0) {
|
2014-09-30 19:40:12 +08:00
|
|
|
rl.rlim_cur = 0;
|
|
|
|
setrlimit(RLIMIT_CORE, &rl);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Visual Studio: configure abort() to not display an error message nor
|
|
|
|
open a popup asking to report the fault. */
|
|
|
|
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-04 07:25:03 +08:00
|
|
|
static PyObject* _Py_NO_SANITIZE_UNDEFINED
|
2011-03-31 07:31:06 +08:00
|
|
|
faulthandler_read_null(PyObject *self, PyObject *args)
|
|
|
|
{
|
2012-01-30 07:07:43 +08:00
|
|
|
volatile int *x;
|
|
|
|
volatile int y;
|
|
|
|
|
2014-09-30 19:40:12 +08:00
|
|
|
faulthandler_suppress_crash_report();
|
2012-01-30 07:07:43 +08:00
|
|
|
x = NULL;
|
2014-09-30 19:54:14 +08:00
|
|
|
y = *x;
|
2011-03-31 07:31:06 +08:00
|
|
|
return PyLong_FromLong(y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-30 19:54:14 +08:00
|
|
|
static void
|
|
|
|
faulthandler_raise_sigsegv(void)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2014-09-30 19:40:12 +08:00
|
|
|
faulthandler_suppress_crash_report();
|
2011-03-31 07:31:06 +08:00
|
|
|
#if defined(MS_WINDOWS)
|
2011-04-01 18:08:57 +08:00
|
|
|
/* For SIGSEGV, faulthandler_fatal_error() restores the previous signal
|
|
|
|
handler and then gives back the execution flow to the program (without
|
2011-05-12 02:56:08 +08:00
|
|
|
explicitly calling the previous error handler). In a normal case, the
|
2011-03-31 07:31:06 +08:00
|
|
|
SIGSEGV was raised by the kernel because of a fault, and so if the
|
|
|
|
program retries to execute the same instruction, the fault will be
|
|
|
|
raised again.
|
|
|
|
|
|
|
|
Here the fault is simulated by a fake SIGSEGV signal raised by the
|
|
|
|
application. We have to raise SIGSEGV at lease twice: once for
|
|
|
|
faulthandler_fatal_error(), and one more time for the previous signal
|
|
|
|
handler. */
|
|
|
|
while(1)
|
|
|
|
raise(SIGSEGV);
|
|
|
|
#else
|
|
|
|
raise(SIGSEGV);
|
|
|
|
#endif
|
2014-09-30 19:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
faulthandler_sigsegv(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
int release_gil = 0;
|
2016-03-17 05:45:24 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "|i:_sigsegv", &release_gil))
|
2014-09-30 19:54:14 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (release_gil) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
faulthandler_raise_sigsegv();
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} else {
|
|
|
|
faulthandler_raise_sigsegv();
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-01-30 20:09:11 +08:00
|
|
|
static void _Py_NO_RETURN
|
2016-03-17 05:45:24 +08:00
|
|
|
faulthandler_fatal_error_thread(void *plock)
|
|
|
|
{
|
|
|
|
Py_FatalError("in new thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
faulthandler_fatal_error_c_thread(PyObject *self, PyObject *args)
|
|
|
|
{
|
2022-12-13 00:58:46 +08:00
|
|
|
long tid;
|
2016-03-17 05:45:24 +08:00
|
|
|
PyThread_type_lock lock;
|
|
|
|
|
|
|
|
faulthandler_suppress_crash_report();
|
|
|
|
|
|
|
|
lock = PyThread_allocate_lock();
|
|
|
|
if (lock == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
|
|
|
|
PyThread_acquire_lock(lock, WAIT_LOCK);
|
|
|
|
|
2022-12-13 00:58:46 +08:00
|
|
|
tid = PyThread_start_new_thread(faulthandler_fatal_error_thread, lock);
|
|
|
|
if (tid == -1) {
|
2016-03-17 05:45:24 +08:00
|
|
|
PyThread_free_lock(lock);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until the thread completes: it will never occur, since Py_FatalError()
|
2017-12-14 19:04:53 +08:00
|
|
|
exits the process immediately. */
|
2016-03-17 05:45:24 +08:00
|
|
|
PyThread_acquire_lock(lock, WAIT_LOCK);
|
|
|
|
PyThread_release_lock(lock);
|
|
|
|
PyThread_free_lock(lock);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2022-03-04 04:45:01 +08:00
|
|
|
static PyObject* _Py_NO_SANITIZE_UNDEFINED
|
2011-03-31 07:31:06 +08:00
|
|
|
faulthandler_sigfpe(PyObject *self, PyObject *args)
|
|
|
|
{
|
2022-03-04 04:45:01 +08:00
|
|
|
faulthandler_suppress_crash_report();
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
/* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
|
|
|
|
PowerPC. Use volatile to disable compile-time optimizations. */
|
|
|
|
volatile int x = 1, y = 0, z;
|
|
|
|
z = x / y;
|
2022-03-04 04:45:01 +08:00
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
/* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
|
|
|
|
raise it manually. */
|
2011-03-31 07:31:06 +08:00
|
|
|
raise(SIGFPE);
|
2022-03-04 04:45:01 +08:00
|
|
|
|
2011-05-12 02:56:08 +08:00
|
|
|
/* This line is never reached, but we pretend to make something with z
|
|
|
|
to silence a compiler warning. */
|
2011-05-09 20:44:26 +08:00
|
|
|
return PyLong_FromLong(z);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
2011-04-01 18:13:55 +08:00
|
|
|
static PyObject *
|
|
|
|
faulthandler_sigabrt(PyObject *self, PyObject *args)
|
|
|
|
{
|
2014-09-30 19:40:12 +08:00
|
|
|
faulthandler_suppress_crash_report();
|
2011-05-10 07:30:03 +08:00
|
|
|
abort();
|
2011-04-01 18:13:55 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:40:42 +08:00
|
|
|
#if defined(FAULTHANDLER_USE_ALT_STACK)
|
2016-03-23 17:39:17 +08:00
|
|
|
#define FAULTHANDLER_STACK_OVERFLOW
|
|
|
|
|
2019-12-05 04:10:06 +08:00
|
|
|
static uintptr_t
|
2016-09-07 04:47:26 +08:00
|
|
|
stack_overflow(uintptr_t min_sp, uintptr_t max_sp, size_t *depth)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2019-12-05 04:10:06 +08:00
|
|
|
/* Allocate (at least) 4096 bytes on the stack at each call.
|
|
|
|
|
|
|
|
bpo-23654, bpo-38965: use volatile keyword to prevent tail call
|
|
|
|
optimization. */
|
|
|
|
volatile unsigned char buffer[4096];
|
2016-09-07 04:47:26 +08:00
|
|
|
uintptr_t sp = (uintptr_t)&buffer;
|
2011-03-31 17:34:08 +08:00
|
|
|
*depth += 1;
|
2016-03-23 22:19:12 +08:00
|
|
|
if (sp < min_sp || max_sp < sp)
|
2011-03-31 17:34:08 +08:00
|
|
|
return sp;
|
2016-03-23 22:19:12 +08:00
|
|
|
buffer[0] = 1;
|
|
|
|
buffer[4095] = 0;
|
|
|
|
return stack_overflow(min_sp, max_sp, depth);
|
2011-03-31 17:34:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-30 02:59:33 +08:00
|
|
|
faulthandler_stack_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
|
2011-03-31 17:34:08 +08:00
|
|
|
{
|
|
|
|
size_t depth, size;
|
2016-09-07 04:47:26 +08:00
|
|
|
uintptr_t sp = (uintptr_t)&depth;
|
2019-05-12 01:13:23 +08:00
|
|
|
uintptr_t stop, lower_limit, upper_limit;
|
2011-03-31 17:34:08 +08:00
|
|
|
|
2014-09-30 19:40:12 +08:00
|
|
|
faulthandler_suppress_crash_report();
|
2011-03-31 17:34:08 +08:00
|
|
|
depth = 0;
|
2019-05-12 01:13:23 +08:00
|
|
|
|
|
|
|
if (STACK_OVERFLOW_MAX_SIZE <= sp) {
|
|
|
|
lower_limit = sp - STACK_OVERFLOW_MAX_SIZE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lower_limit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UINTPTR_MAX - STACK_OVERFLOW_MAX_SIZE >= sp) {
|
|
|
|
upper_limit = sp + STACK_OVERFLOW_MAX_SIZE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
upper_limit = UINTPTR_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop = stack_overflow(lower_limit, upper_limit, &depth);
|
2011-03-31 17:34:08 +08:00
|
|
|
if (sp < stop)
|
|
|
|
size = stop - sp;
|
|
|
|
else
|
|
|
|
size = sp - stop;
|
|
|
|
PyErr_Format(PyExc_RuntimeError,
|
|
|
|
"unable to raise a stack overflow (allocated %zu bytes "
|
|
|
|
"on the stack, %zu recursive calls)",
|
|
|
|
size, depth);
|
|
|
|
return NULL;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
2019-08-21 20:40:42 +08:00
|
|
|
#endif /* defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_SIGACTION) */
|
2011-03-31 07:31:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
faulthandler_traverse(PyObject *module, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(thread.file);
|
|
|
|
#ifdef FAULTHANDLER_USER
|
|
|
|
if (user_signals != NULL) {
|
2022-04-26 06:13:31 +08:00
|
|
|
for (size_t signum=0; signum < Py_NSIG; signum++)
|
2011-04-07 17:37:19 +08:00
|
|
|
Py_VISIT(user_signals[signum].file);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Py_VISIT(fatal_error.file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:39:17 +08:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
static PyObject *
|
|
|
|
faulthandler_raise_exception(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
unsigned int code, flags = 0;
|
|
|
|
if (!PyArg_ParseTuple(args, "I|I:_raise_exception", &code, &flags))
|
|
|
|
return NULL;
|
|
|
|
faulthandler_suppress_crash_report();
|
|
|
|
RaiseException(code, flags, 0, NULL);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-31 07:31:06 +08:00
|
|
|
PyDoc_STRVAR(module_doc,
|
|
|
|
"faulthandler module.");
|
|
|
|
|
|
|
|
static PyMethodDef module_methods[] = {
|
|
|
|
{"enable",
|
2022-05-04 03:42:14 +08:00
|
|
|
_PyCFunction_CAST(faulthandler_py_enable), METH_VARARGS|METH_KEYWORDS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("enable($module, /, file=sys.stderr, all_threads=True)\n--\n\n"
|
|
|
|
"Enable the fault handler.")},
|
2018-04-30 02:59:33 +08:00
|
|
|
{"disable", faulthandler_disable_py, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("disable($module, /)\n--\n\n"
|
|
|
|
"Disable the fault handler.")},
|
2018-04-30 02:59:33 +08:00
|
|
|
{"is_enabled", faulthandler_is_enabled, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("is_enabled($module, /)\n--\n\n"
|
|
|
|
"Check if the handler is enabled.")},
|
2011-03-31 07:31:06 +08:00
|
|
|
{"dump_traceback",
|
2022-05-04 03:42:14 +08:00
|
|
|
_PyCFunction_CAST(faulthandler_dump_traceback_py), METH_VARARGS|METH_KEYWORDS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("dump_traceback($module, /, file=sys.stderr, all_threads=True)\n--\n\n"
|
|
|
|
"Dump the traceback of the current thread, or of all threads "
|
|
|
|
"if all_threads is True, into file.")},
|
2012-09-22 14:58:55 +08:00
|
|
|
{"dump_traceback_later",
|
2022-05-04 03:42:14 +08:00
|
|
|
_PyCFunction_CAST(faulthandler_dump_traceback_later), METH_VARARGS|METH_KEYWORDS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("dump_traceback_later($module, /, timeout, repeat=False, file=sys.stderr, exit=False)\n--\n\n"
|
|
|
|
"Dump the traceback of all threads in timeout seconds,\n"
|
2011-04-07 17:37:19 +08:00
|
|
|
"or each timeout seconds if repeat is True. If exit is True, "
|
|
|
|
"call _exit(1) which is not safe.")},
|
2012-09-22 14:58:55 +08:00
|
|
|
{"cancel_dump_traceback_later",
|
2018-04-30 02:59:33 +08:00
|
|
|
faulthandler_cancel_dump_traceback_later_py, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("cancel_dump_traceback_later($module, /)\n--\n\n"
|
|
|
|
"Cancel the previous call to dump_traceback_later().")},
|
2011-03-31 07:31:06 +08:00
|
|
|
#ifdef FAULTHANDLER_USER
|
|
|
|
{"register",
|
2022-05-04 03:42:14 +08:00
|
|
|
_PyCFunction_CAST(faulthandler_register_py), METH_VARARGS|METH_KEYWORDS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("register($module, /, signum, file=sys.stderr, all_threads=True, chain=False)\n--\n\n"
|
|
|
|
"Register a handler for the signal 'signum': dump the "
|
2011-03-31 07:31:06 +08:00
|
|
|
"traceback of the current thread, or of all threads if "
|
2024-04-12 18:14:31 +08:00
|
|
|
"all_threads is True, into file.")},
|
2011-03-31 07:31:06 +08:00
|
|
|
{"unregister",
|
2024-04-12 18:14:31 +08:00
|
|
|
_PyCFunction_CAST(faulthandler_unregister_py), METH_VARARGS,
|
|
|
|
PyDoc_STR("unregister($module, signum, /)\n--\n\n"
|
|
|
|
"Unregister the handler of the signal "
|
|
|
|
"'signum' registered by register().")},
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif
|
2014-09-30 19:54:14 +08:00
|
|
|
{"_read_null", faulthandler_read_null, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_read_null($module, /)\n--\n\n"
|
|
|
|
"Read from NULL, raise "
|
|
|
|
"a SIGSEGV or SIGBUS signal depending on the platform.")},
|
2011-03-31 07:31:06 +08:00
|
|
|
{"_sigsegv", faulthandler_sigsegv, METH_VARARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_sigsegv($module, release_gil=False, /)\n--\n\n"
|
|
|
|
"Raise a SIGSEGV signal.")},
|
2016-03-17 05:45:24 +08:00
|
|
|
{"_fatal_error_c_thread", faulthandler_fatal_error_c_thread, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_fatal_error_c_thread($module, /)\n--\n\n"
|
|
|
|
"Call Py_FatalError() in a new C thread.")},
|
2014-09-30 19:49:09 +08:00
|
|
|
{"_sigabrt", faulthandler_sigabrt, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_sigabrt($module, /)\n--\n\n"
|
|
|
|
"Raise a SIGABRT signal.")},
|
2011-03-31 07:31:06 +08:00
|
|
|
{"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_sigfpe($module, /)\n--\n\n"
|
|
|
|
"Raise a SIGFPE signal.")},
|
2016-03-23 17:39:17 +08:00
|
|
|
#ifdef FAULTHANDLER_STACK_OVERFLOW
|
2018-04-30 02:59:33 +08:00
|
|
|
{"_stack_overflow", faulthandler_stack_overflow, METH_NOARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_stack_overflow($module, /)\n--\n\n"
|
|
|
|
"Recursive call to raise a stack overflow.")},
|
2016-03-23 17:39:17 +08:00
|
|
|
#endif
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
{"_raise_exception", faulthandler_raise_exception, METH_VARARGS,
|
2024-04-12 18:14:31 +08:00
|
|
|
PyDoc_STR("_raise_exception($module, code, flags=0, /)\n--\n\n"
|
|
|
|
"Call RaiseException(code, flags).")},
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif
|
2011-05-12 02:56:08 +08:00
|
|
|
{NULL, NULL} /* sentinel */
|
2011-03-31 07:31:06 +08:00
|
|
|
};
|
|
|
|
|
2020-07-04 00:36:47 +08:00
|
|
|
static int
|
|
|
|
PyExec_faulthandler(PyObject *module) {
|
2016-03-23 17:39:17 +08:00
|
|
|
/* Add constants for unit tests */
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* RaiseException() codes (prefixed by an underscore) */
|
2020-07-04 00:36:47 +08:00
|
|
|
if (PyModule_AddIntConstant(module, "_EXCEPTION_ACCESS_VIOLATION",
|
2019-11-20 07:13:05 +08:00
|
|
|
EXCEPTION_ACCESS_VIOLATION)) {
|
2020-07-04 00:36:47 +08:00
|
|
|
return -1;
|
2019-11-20 07:13:05 +08:00
|
|
|
}
|
2020-07-04 00:36:47 +08:00
|
|
|
if (PyModule_AddIntConstant(module, "_EXCEPTION_INT_DIVIDE_BY_ZERO",
|
2019-11-20 07:13:05 +08:00
|
|
|
EXCEPTION_INT_DIVIDE_BY_ZERO)) {
|
2020-07-04 00:36:47 +08:00
|
|
|
return -1;
|
2019-11-20 07:13:05 +08:00
|
|
|
}
|
2020-07-04 00:36:47 +08:00
|
|
|
if (PyModule_AddIntConstant(module, "_EXCEPTION_STACK_OVERFLOW",
|
2019-11-20 07:13:05 +08:00
|
|
|
EXCEPTION_STACK_OVERFLOW)) {
|
2020-07-04 00:36:47 +08:00
|
|
|
return -1;
|
2019-11-20 07:13:05 +08:00
|
|
|
}
|
2016-03-23 17:39:17 +08:00
|
|
|
|
|
|
|
/* RaiseException() flags (prefixed by an underscore) */
|
2020-07-04 00:36:47 +08:00
|
|
|
if (PyModule_AddIntConstant(module, "_EXCEPTION_NONCONTINUABLE",
|
2019-11-20 07:13:05 +08:00
|
|
|
EXCEPTION_NONCONTINUABLE)) {
|
2020-07-04 00:36:47 +08:00
|
|
|
return -1;
|
2019-11-20 07:13:05 +08:00
|
|
|
}
|
2020-07-04 00:36:47 +08:00
|
|
|
if (PyModule_AddIntConstant(module, "_EXCEPTION_NONCONTINUABLE_EXCEPTION",
|
2019-11-20 07:13:05 +08:00
|
|
|
EXCEPTION_NONCONTINUABLE_EXCEPTION)) {
|
2020-07-04 00:36:47 +08:00
|
|
|
return -1;
|
2019-11-20 07:13:05 +08:00
|
|
|
}
|
2016-03-23 17:39:17 +08:00
|
|
|
#endif
|
2020-07-04 00:36:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-23 17:39:17 +08:00
|
|
|
|
2020-07-04 00:36:47 +08:00
|
|
|
static PyModuleDef_Slot faulthandler_slots[] = {
|
|
|
|
{Py_mod_exec, PyExec_faulthandler},
|
2023-05-06 05:11:27 +08:00
|
|
|
// XXX gh-103092: fix isolation.
|
|
|
|
//{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2024-05-03 23:30:55 +08:00
|
|
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
2020-07-04 00:36:47 +08:00
|
|
|
{0, NULL}
|
|
|
|
};
|
2019-11-20 07:13:05 +08:00
|
|
|
|
2020-07-04 00:36:47 +08:00
|
|
|
static struct PyModuleDef module_def = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "faulthandler",
|
|
|
|
.m_doc = module_doc,
|
|
|
|
.m_methods = module_methods,
|
|
|
|
.m_traverse = faulthandler_traverse,
|
|
|
|
.m_slots = faulthandler_slots
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_faulthandler(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&module_def);
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-11-16 07:48:08 +08:00
|
|
|
faulthandler_init_enable(void)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2022-06-14 12:15:26 +08:00
|
|
|
PyObject *enable = _PyImport_GetModuleAttrString("faulthandler", "enable");
|
|
|
|
if (enable == NULL) {
|
2017-11-16 07:48:08 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2022-06-14 12:15:26 +08:00
|
|
|
PyObject *res = PyObject_CallNoArgs(enable);
|
|
|
|
Py_DECREF(enable);
|
2017-11-16 07:48:08 +08:00
|
|
|
if (res == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
2011-05-26 19:47:08 +08:00
|
|
|
|
2017-11-16 07:48:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2019-05-27 22:39:22 +08:00
|
|
|
PyStatus
|
2017-11-16 10:11:45 +08:00
|
|
|
_PyFaulthandler_Init(int enable)
|
2011-03-31 07:31:06 +08:00
|
|
|
{
|
2019-08-21 20:40:42 +08:00
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
|
|
|
memset(&stack, 0, sizeof(stack));
|
2011-03-31 07:31:06 +08:00
|
|
|
stack.ss_flags = 0;
|
2019-08-15 05:35:27 +08:00
|
|
|
/* bpo-21131: allocate dedicated stack of SIGSTKSZ*2 bytes, instead of just
|
|
|
|
SIGSTKSZ bytes. Calling the previous signal handler in faulthandler
|
|
|
|
signal handler uses more than SIGSTKSZ bytes of stack memory on some
|
|
|
|
platforms. */
|
|
|
|
stack.ss_size = SIGSTKSZ * 2;
|
2022-03-12 06:19:35 +08:00
|
|
|
#ifdef AT_MINSIGSTKSZ
|
|
|
|
/* bpo-46968: Query Linux for minimal stack size to ensure signal delivery
|
|
|
|
for the hardware running CPython. This OS feature is available in
|
|
|
|
Linux kernel version >= 5.14 */
|
|
|
|
unsigned long at_minstack_size = getauxval(AT_MINSIGSTKSZ);
|
|
|
|
if (at_minstack_size != 0) {
|
|
|
|
stack.ss_size = SIGSTKSZ + at_minstack_size;
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-31 07:31:06 +08:00
|
|
|
#endif
|
2019-08-21 20:40:42 +08:00
|
|
|
|
|
|
|
memset(&thread, 0, sizeof(thread));
|
2011-03-31 07:31:06 +08:00
|
|
|
|
2017-11-16 07:48:08 +08:00
|
|
|
if (enable) {
|
|
|
|
if (faulthandler_init_enable() < 0) {
|
2019-05-27 22:39:22 +08:00
|
|
|
return _PyStatus_ERR("failed to enable faulthandler");
|
2017-11-16 07:48:08 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 22:39:22 +08:00
|
|
|
return _PyStatus_OK();
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void _PyFaulthandler_Fini(void)
|
|
|
|
{
|
|
|
|
/* later */
|
|
|
|
if (thread.cancel_event) {
|
2012-09-22 14:58:55 +08:00
|
|
|
cancel_dump_traceback_later();
|
2011-04-08 18:57:06 +08:00
|
|
|
PyThread_release_lock(thread.cancel_event);
|
2011-03-31 07:31:06 +08:00
|
|
|
PyThread_free_lock(thread.cancel_event);
|
|
|
|
thread.cancel_event = NULL;
|
|
|
|
}
|
2011-04-08 18:57:06 +08:00
|
|
|
if (thread.running) {
|
|
|
|
PyThread_free_lock(thread.running);
|
|
|
|
thread.running = NULL;
|
2011-03-31 07:31:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FAULTHANDLER_USER
|
|
|
|
/* user */
|
|
|
|
if (user_signals != NULL) {
|
2022-04-26 06:13:31 +08:00
|
|
|
for (size_t signum=0; signum < Py_NSIG; signum++) {
|
2011-04-01 18:56:17 +08:00
|
|
|
faulthandler_unregister(&user_signals[signum], signum);
|
2017-04-22 00:06:13 +08:00
|
|
|
}
|
2013-07-08 05:30:24 +08:00
|
|
|
PyMem_Free(user_signals);
|
2011-03-31 07:31:06 +08:00
|
|
|
user_signals = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fatal */
|
|
|
|
faulthandler_disable();
|
2019-08-21 20:40:42 +08:00
|
|
|
|
|
|
|
#ifdef FAULTHANDLER_USE_ALT_STACK
|
2011-03-31 07:31:06 +08:00
|
|
|
if (stack.ss_sp != NULL) {
|
2017-03-24 01:14:29 +08:00
|
|
|
/* Fetch the current alt stack */
|
2019-04-30 18:19:34 +08:00
|
|
|
stack_t current_stack;
|
|
|
|
memset(¤t_stack, 0, sizeof(current_stack));
|
2017-03-24 01:14:29 +08:00
|
|
|
if (sigaltstack(NULL, ¤t_stack) == 0) {
|
|
|
|
if (current_stack.ss_sp == stack.ss_sp) {
|
|
|
|
/* The current alt stack is the one that we installed.
|
|
|
|
It is safe to restore the old stack that we found when
|
|
|
|
we installed ours */
|
|
|
|
sigaltstack(&old_stack, NULL);
|
|
|
|
} else {
|
|
|
|
/* Someone switched to a different alt stack and didn't
|
|
|
|
restore ours when they were done (if they're done).
|
|
|
|
There's not much we can do in this unlikely case */
|
|
|
|
}
|
|
|
|
}
|
2011-03-31 07:31:06 +08:00
|
|
|
PyMem_Free(stack.ss_sp);
|
|
|
|
stack.ss_sp = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|