Skip to content

Commit

Permalink
pythongh-129223: Do not allow the compiler to optimise away symbols f…
Browse files Browse the repository at this point in the history
…or debug sections

Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Jan 23, 2025
1 parent 5c9a63f commit d2973c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 12 additions & 10 deletions Include/internal/pycore_debug_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@ extern "C" {

// Macros to burn global values in custom sections so out-of-process
// profilers can locate them easily.

#define GENERATE_DEBUG_SECTION(name, declaration) \
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
_GENERATE_DEBUG_SECTION_APPLE(name) \
declaration \
_GENERATE_DEBUG_SECTION_LINUX(name)
#define GENERATE_DEBUG_SECTION(name, declaration) \
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
_GENERATE_DEBUG_SECTION_APPLE(name) \
declaration \
_GENERATE_DEBUG_SECTION_LINUX(name)

#if defined(MS_WINDOWS)
#define _GENERATE_DEBUG_SECTION_WINDOWS(name) \
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
__declspec(allocate(Py_STRINGIFY(name)))
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
__declspec(allocate(Py_STRINGIFY(name))) \
__declspec(used)
#else
#define _GENERATE_DEBUG_SECTION_WINDOWS(name)
#endif

#if defined(__APPLE__)
#define _GENERATE_DEBUG_SECTION_APPLE(name) \
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name))))
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name)))) \
__attribute__((used))
#else
#define _GENERATE_DEBUG_SECTION_APPLE(name)
#endif

#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
#define _GENERATE_DEBUG_SECTION_LINUX(name) \
__attribute__((section("." Py_STRINGIFY(name))))
__attribute__((section("." Py_STRINGIFY(name)))) \
__attribute__((used))
#else
#define _GENERATE_DEBUG_SECTION_LINUX(name)
#endif
Expand Down
6 changes: 5 additions & 1 deletion Modules/_testexternalinspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ get_py_runtime(pid_t pid)
static uintptr_t
get_async_debug(pid_t pid)
{
return search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
uintptr_t result = search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
if (result == 0 && !PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError, "Cannot find AsyncioDebug section");
}
return result;
}


Expand Down

0 comments on commit d2973c6

Please sign in to comment.