From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sun, 24 May 2026 13:39:29 +0000 (+0200) Subject: [3.15] gh-149619: Fix `_remote_debugging` permissions error on Linux (GH-150012)... X-Git-Tag: v3.15.0b2~75 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d52dad6989eb79fd01378654954cd79c363c1179;p=thirdparty%2FPython%2Fcpython.git [3.15] gh-149619: Fix `_remote_debugging` permissions error on Linux (GH-150012) (#150339) gh-149619: Fix `_remote_debugging` permissions error on Linux (GH-150012) When running profiling on Linux without sudo, attempts to read process memory would fail with the misleading error 'Failed to find the PyRuntime section in process on Linux platform'. The actual issue is a permissions error because profiling was not run with sudo. We were clearing the exception on Linux when trying to read memory, instead, we should bubble up the permissions error and show it properly. (cherry picked from commit 0563890872b3c63f94953e983fe396615b708540) Co-authored-by: ivonastojanovic <80911834+ivonastojanovic@users.noreply.github.com> --- diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 7b2c4f3bcb80..53bbd571ad3c 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -170,7 +170,9 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address } char buf[sizeof(_Py_Debug_Cookie) - 1]; if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) { - PyErr_Clear(); + if (!PyErr_ExceptionMatches(PyExc_PermissionError)) { + PyErr_Clear(); + } return 0; } return memcmp(buf, _Py_Debug_Cookie, sizeof(buf)) == 0; @@ -785,6 +787,10 @@ search_linux_map_for_section(proc_handle_t *handle, const char* secname, const c } if (strstr(filename, substr)) { + if (PyErr_ExceptionMatches(PyExc_PermissionError)) { + retval = 0; + break; + } PyErr_Clear(); retval = search_elf_file_for_section(handle, secname, start, path); if (retval @@ -960,12 +966,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle) address = search_linux_map_for_section(handle, "PyRuntime", "python", _Py_RemoteDebug_ValidatePyRuntimeCookie); if (address == 0) { - // Error out: 'python' substring covers both executable and DLL - PyObject *exc = PyErr_GetRaisedException(); - PyErr_Format(PyExc_RuntimeError, - "Failed to find the PyRuntime section in process %d on Linux platform", - handle->pid); - _PyErr_ChainExceptions1(exc); + if (!PyErr_ExceptionMatches(PyExc_PermissionError)) { + // Error out: 'python' substring covers both executable and DLL + PyObject *exc = PyErr_GetRaisedException(); + PyErr_Format(PyExc_RuntimeError, + "Failed to find the PyRuntime section in process %d on Linux platform", + handle->pid); + _PyErr_ChainExceptions1(exc); + } } #elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX // On macOS, try libpython first, then fall back to python