summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-09-18 12:10:16 (GMT)
committerGitHub <noreply@github.com>2019-09-18 12:10:16 (GMT)
commit47bbab9f76735acc1991e541d12fd18be6b13b16 (patch)
treededa23c37efb7ae3a1d084cd4b4388e773e450d1 /Python
parent7a2f68776a77c782c0abf40dc9e3fb687787e730 (diff)
downloadcpython-47bbab9f76735acc1991e541d12fd18be6b13b16.zip
cpython-47bbab9f76735acc1991e541d12fd18be6b13b16.tar.gz
cpython-47bbab9f76735acc1991e541d12fd18be6b13b16.tar.bz2
[3.8] bpo-38070: Py_FatalError() logs runtime state (GH-16258)
* bpo-38070: _Py_DumpTraceback() writes <no Python frame> (GH-16244) When a Python thread has no frame, _Py_DumpTraceback() and _Py_DumpTracebackThreads() now write "<no Python frame>", rather than writing nothing. (cherry picked from commit 8fa3e1740b3f03ea65ddb68411c2238c5f98eec2) * bpo-38070: Enhance _PyObject_Dump() (GH-16243) _PyObject_Dump() now dumps the object address for freed objects and objects with ob_type=NULL. (cherry picked from commit b39afb78768418d9405c4b528c80fa968ccc974d) * bpo-38070: Add _PyRuntimeState.preinitializing (GH-16245) Add _PyRuntimeState.preinitializing field: set to 1 while Py_PreInitialize() is running. _PyRuntimeState: rename also pre_initialized field to preinitialized. (cherry picked from commit d3b904144e86e2442961de6a7dccecbe133d5c6d) * bpo-38070: Py_FatalError() logs runtime state (GH-16246) (cherry picked from commit 1ce16fb0977283ae42a9f8917bbca5f44aa69324)
Diffstat (limited to 'Python')
-rw-r--r--Python/pylifecycle.c96
-rw-r--r--Python/traceback.c13
2 files changed, 75 insertions, 34 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 751c4d6..97bb992 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -719,11 +719,15 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
}
_PyRuntimeState *runtime = &_PyRuntime;
- if (runtime->pre_initialized) {
+ if (runtime->preinitialized) {
/* If it's already configured: ignored the new configuration */
return _PyStatus_OK();
}
+ /* Note: preinitialized remains 1 on error, it is only set to 0
+ at exit on success. */
+ runtime->preinitializing = 1;
+
PyPreConfig config;
_PyPreConfig_InitFromPreConfig(&config, src_config);
@@ -737,7 +741,8 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
return status;
}
- runtime->pre_initialized = 1;
+ runtime->preinitializing = 0;
+ runtime->preinitialized = 1;
return _PyStatus_OK();
}
@@ -777,7 +782,7 @@ _Py_PreInitializeFromConfig(const PyConfig *config,
}
_PyRuntimeState *runtime = &_PyRuntime;
- if (runtime->pre_initialized) {
+ if (runtime->preinitialized) {
/* Already initialized: do nothing */
return _PyStatus_OK();
}
@@ -1961,13 +1966,14 @@ done:
static void
-_Py_FatalError_DumpTracebacks(int fd)
+_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
+ PyThreadState *tstate)
{
fputc('\n', stderr);
fflush(stderr);
/* display the current Python stack */
- _Py_DumpTracebackThreads(fd, NULL, NULL);
+ _Py_DumpTracebackThreads(fd, interp, tstate);
}
/* Print the current exception (if an exception is set) with its traceback,
@@ -2062,10 +2068,39 @@ fatal_output_debug(const char *msg)
}
#endif
+
+static void
+fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
+{
+ fprintf(stream, "Python runtime state: ");
+ if (runtime->finalizing) {
+ fprintf(stream, "finalizing (tstate=%p)", runtime->finalizing);
+ }
+ else if (runtime->initialized) {
+ fprintf(stream, "initialized");
+ }
+ else if (runtime->core_initialized) {
+ fprintf(stream, "core initialized");
+ }
+ else if (runtime->preinitialized) {
+ fprintf(stream, "preinitialized");
+ }
+ else if (runtime->preinitializing) {
+ fprintf(stream, "preinitializing");
+ }
+ else {
+ fprintf(stream, "unknown");
+ }
+ fprintf(stream, "\n");
+ fflush(stream);
+}
+
+
static void _Py_NO_RETURN
fatal_error(const char *prefix, const char *msg, int status)
{
- const int fd = fileno(stderr);
+ FILE *stream = stderr;
+ const int fd = fileno(stream);
static int reentrant = 0;
if (reentrant) {
@@ -2075,45 +2110,48 @@ fatal_error(const char *prefix, const char *msg, int status)
}
reentrant = 1;
- fprintf(stderr, "Fatal Python error: ");
+ fprintf(stream, "Fatal Python error: ");
if (prefix) {
- fputs(prefix, stderr);
- fputs(": ", stderr);
+ fputs(prefix, stream);
+ fputs(": ", stream);
}
if (msg) {
- fputs(msg, stderr);
+ fputs(msg, stream);
}
else {
- fprintf(stderr, "<message not set>");
+ fprintf(stream, "<message not set>");
}
- fputs("\n", stderr);
- fflush(stderr); /* it helps in Windows debug build */
+ fputs("\n", stream);
+ fflush(stream); /* it helps in Windows debug build */
- /* Check if the current thread has a Python thread state
- and holds the GIL */
- PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
- if (tss_tstate != NULL) {
- PyThreadState *tstate = _PyThreadState_GET();
- if (tss_tstate != tstate) {
- /* The Python thread does not hold the GIL */
- tss_tstate = NULL;
- }
- }
- else {
- /* Py_FatalError() has been called from a C thread
- which has no Python thread state. */
+ _PyRuntimeState *runtime = &_PyRuntime;
+ fatal_error_dump_runtime(stream, runtime);
+
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ PyInterpreterState *interp = NULL;
+ if (tstate != NULL) {
+ interp = tstate->interp;
}
- int has_tstate_and_gil = (tss_tstate != NULL);
+ /* Check if the current thread has a Python thread state
+ and holds the GIL.
+
+ tss_tstate is NULL if Py_FatalError() is called from a C thread which
+ has no Python thread state.
+
+ tss_tstate != tstate if the current Python thread does not hold the GIL.
+ */
+ PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
+ int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
if (has_tstate_and_gil) {
/* If an exception is set, print the exception with its traceback */
if (!_Py_FatalError_PrintExc(fd)) {
/* No exception is set, or an exception is set without traceback */
- _Py_FatalError_DumpTracebacks(fd);
+ _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
}
}
else {
- _Py_FatalError_DumpTracebacks(fd);
+ _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
}
/* The main purpose of faulthandler is to display the traceback.
diff --git a/Python/traceback.c b/Python/traceback.c
index 0463eb6..8e2f15e 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -797,12 +797,15 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
PyFrameObject *frame;
unsigned int depth;
- if (write_header)
+ if (write_header) {
PUTS(fd, "Stack (most recent call first):\n");
+ }
frame = _PyThreadState_GetFrame(tstate);
- if (frame == NULL)
+ if (frame == NULL) {
+ PUTS(fd, "<no Python frame>\n");
return;
+ }
depth = 0;
while (frame != NULL) {
@@ -870,9 +873,9 @@ _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
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 used. Read the thread specific storage (TSS) instead: call
- PyGILState_GetThisThreadState(). */
+ the fault if the thread released the GIL, and so
+ _PyThreadState_GET() cannot be used. Read the thread specific
+ storage (TSS) instead: call PyGILState_GetThisThreadState(). */
current_tstate = PyGILState_GetThisThreadState();
}