Skip to content

Commit 972f19a

Browse files
ivonastojanovicmiss-islington
authored andcommitted
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 <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 1b7ab11 commit 972f19a

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Python/remote_debug.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address
170170
}
171171
char buf[sizeof(_Py_Debug_Cookie) - 1];
172172
if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) {
173-
PyErr_Clear();
173+
if (!PyErr_ExceptionMatches(PyExc_PermissionError)) {
174+
PyErr_Clear();
175+
}
174176
return 0;
175177
}
176178
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
785787
}
786788

787789
if (strstr(filename, substr)) {
790+
if (PyErr_ExceptionMatches(PyExc_PermissionError)) {
791+
retval = 0;
792+
break;
793+
}
788794
PyErr_Clear();
789795
retval = search_elf_file_for_section(handle, secname, start, path);
790796
if (retval
@@ -960,12 +966,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle)
960966
address = search_linux_map_for_section(handle, "PyRuntime", "python",
961967
_Py_RemoteDebug_ValidatePyRuntimeCookie);
962968
if (address == 0) {
963-
// Error out: 'python' substring covers both executable and DLL
964-
PyObject *exc = PyErr_GetRaisedException();
965-
PyErr_Format(PyExc_RuntimeError,
966-
"Failed to find the PyRuntime section in process %d on Linux platform",
967-
handle->pid);
968-
_PyErr_ChainExceptions1(exc);
969+
if (!PyErr_ExceptionMatches(PyExc_PermissionError)) {
970+
// Error out: 'python' substring covers both executable and DLL
971+
PyObject *exc = PyErr_GetRaisedException();
972+
PyErr_Format(PyExc_RuntimeError,
973+
"Failed to find the PyRuntime section in process %d on Linux platform",
974+
handle->pid);
975+
_PyErr_ChainExceptions1(exc);
976+
}
969977
}
970978
#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
971979
// On macOS, try libpython first, then fall back to python

0 commit comments

Comments
 (0)