Skip to content

Commit c533f18

Browse files
[3.14] gh-149619: Fix _remote_debugging permissions error on Linux (GH-150012) (#154825)
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 <pid> 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 0563890) Co-authored-by: ivonastojanovic <80911834+ivonastojanovic@users.noreply.github.com>
1 parent ffdd751 commit c533f18

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

Python/remote_debug.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address
159159
}
160160
char buf[sizeof(_Py_Debug_Cookie) - 1];
161161
if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) {
162-
PyErr_Clear();
162+
if (!_Py_RemoteDebug_HasPermissionError()) {
163+
PyErr_Clear();
164+
}
163165
return 0;
164166
}
165167
return memcmp(buf, _Py_Debug_Cookie, sizeof(buf)) == 0;
@@ -1062,12 +1064,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle)
10621064
address = search_linux_map_for_section(handle, "PyRuntime", "python",
10631065
_Py_RemoteDebug_ValidatePyRuntimeCookie);
10641066
if (address == 0) {
1065-
// Error out: 'python' substring covers both executable and DLL
1066-
PyObject *exc = PyErr_GetRaisedException();
1067-
PyErr_Format(PyExc_RuntimeError,
1068-
"Failed to find the PyRuntime section in process %d on Linux platform",
1069-
handle->pid);
1070-
_PyErr_ChainExceptions1(exc);
1067+
if (!_Py_RemoteDebug_HasPermissionError()) {
1068+
// Error out: 'python' substring covers both executable and DLL
1069+
PyObject *exc = PyErr_GetRaisedException();
1070+
PyErr_Format(PyExc_RuntimeError,
1071+
"Failed to find the PyRuntime section in process %d on Linux platform",
1072+
handle->pid);
1073+
_PyErr_ChainExceptions1(exc);
1074+
}
10711075
}
10721076
#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
10731077
// On macOS, try libpython first, then fall back to python

0 commit comments

Comments
 (0)