Skip to content

Commit 35002aa

Browse files
bpo-32280: Store _PyRuntime in a named section (pythonGH-4802)
This commit stores the _PyRuntime structure in a section of the same name. This allows a debugging or crash reporting tool to quickly locate this structure at runtime without requiring the symbol table. Co-authored-by: Pablo Galindo <[email protected]>
1 parent b250f89 commit 35002aa

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Python/pylifecycle.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
#include <locale.h> // setlocale()
1919

20+
#if defined(__APPLE__)
21+
#include <mach-o/loader.h>
22+
#endif
23+
2024
#ifdef HAVE_SIGNAL_H
2125
# include <signal.h> // SIG_IGN
2226
#endif
@@ -34,7 +38,6 @@
3438
(PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
3539
#endif
3640

37-
3841
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
3942

4043

@@ -59,7 +62,30 @@ static void wait_for_thread_shutdown(PyThreadState *tstate);
5962
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
6063

6164
int _Py_UnhandledKeyboardInterrupt = 0;
62-
_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
65+
66+
/* The following places the `_PyRuntime` structure in a location that can be
67+
* found without any external information. This is meant to ease access to the
68+
* interpreter state for various runtime debugging tools, but is *not* an
69+
* officially supported feature */
70+
71+
#if defined(MS_WINDOWS)
72+
73+
#pragma section("PyRuntime", read, write)
74+
__declspec(allocate("PyRuntime"))
75+
76+
#elif defined(__APPLE__)
77+
78+
__attribute__((
79+
section(SEG_DATA ",PyRuntime")
80+
))
81+
82+
#endif
83+
84+
_PyRuntimeState _PyRuntime
85+
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
86+
__attribute__ ((section (".PyRuntime")))
87+
#endif
88+
= _PyRuntimeState_INIT;
6389
static int runtime_initialized = 0;
6490

6591
PyStatus

0 commit comments

Comments
 (0)