Skip to content

Commit 2471dee

Browse files
gh-149619 Fix _remote_debugging permissions error on Linux
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.
1 parent acefff9 commit 2471dee

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
@@ -169,7 +169,9 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address
169169
}
170170
char buf[sizeof(_Py_Debug_Cookie) - 1];
171171
if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) {
172-
PyErr_Clear();
172+
if (!PyErr_ExceptionMatches(PyExc_PermissionError)) {
173+
PyErr_Clear();
174+
}
173175
return 0;
174176
}
175177
return memcmp(buf, _Py_Debug_Cookie, sizeof(buf)) == 0;
@@ -781,6 +783,10 @@ search_linux_map_for_section(proc_handle_t *handle, const char* secname, const c
781783
}
782784

783785
if (strstr(filename, substr)) {
786+
if (PyErr_ExceptionMatches(PyExc_PermissionError)) {
787+
retval = 0;
788+
break;
789+
}
784790
PyErr_Clear();
785791
retval = search_elf_file_for_section(handle, secname, start, path);
786792
if (retval
@@ -956,12 +962,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle)
956962
address = search_linux_map_for_section(handle, "PyRuntime", "python",
957963
_Py_RemoteDebug_ValidatePyRuntimeCookie);
958964
if (address == 0) {
959-
// Error out: 'python' substring covers both executable and DLL
960-
PyObject *exc = PyErr_GetRaisedException();
961-
PyErr_Format(PyExc_RuntimeError,
962-
"Failed to find the PyRuntime section in process %d on Linux platform",
963-
handle->pid);
964-
_PyErr_ChainExceptions1(exc);
965+
if (!PyErr_ExceptionMatches(PyExc_PermissionError)) {
966+
// Error out: 'python' substring covers both executable and DLL
967+
PyObject *exc = PyErr_GetRaisedException();
968+
PyErr_Format(PyExc_RuntimeError,
969+
"Failed to find the PyRuntime section in process %d on Linux platform",
970+
handle->pid);
971+
_PyErr_ChainExceptions1(exc);
972+
}
965973
}
966974
#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
967975
// On macOS, try libpython first, then fall back to python

0 commit comments

Comments
 (0)