From 250035d134ad482e724f73ce654682254b513ee0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 18 Jan 2021 20:47:13 +0100 Subject: bpo-42923: Dump extension modules on fatal error (GH-24207) The Py_FatalError() function and the faulthandler module now dump the list of extension modules on a fatal error. Add _Py_DumpExtensionModules() and _PyModule_IsExtension() internal functions. --- Python/pylifecycle.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'Python/pylifecycle.c') diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index c02071780b..ee64b0f125 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2496,6 +2496,45 @@ fatal_error_exit(int status) } +// Dump the list of extension modules of sys.modules into fd file descriptor. +// This function is called by a signal handler in faulthandler: avoid memory +// allocations and keep the implementation simple. For example, the list +// is not sorted on purpose. +void +_Py_DumpExtensionModules(int fd, PyInterpreterState *interp) +{ + if (interp == NULL) { + return; + } + PyObject *modules = interp->modules; + if (!PyDict_Check(modules)) { + return; + } + + PUTS(fd, "\nExtension modules: "); + + Py_ssize_t pos = 0; + PyObject *key, *value; + int comma = 0; + while (PyDict_Next(modules, &pos, &key, &value)) { + if (!PyUnicode_Check(key)) { + continue; + } + if (!_PyModule_IsExtension(value)) { + continue; + } + + if (comma) { + PUTS(fd, ", "); + } + comma = 1; + + _Py_DumpASCII(fd, key); + } + PUTS(fd, "\n"); +} + + static void _Py_NO_RETURN fatal_error(int fd, int header, const char *prefix, const char *msg, int status) @@ -2557,6 +2596,8 @@ fatal_error(int fd, int header, const char *prefix, const char *msg, _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); } + _Py_DumpExtensionModules(fd, interp); + /* The main purpose of faulthandler is to display the traceback. This function already did its best to display a traceback. Disable faulthandler to prevent writing a second traceback -- cgit v1.2.1