Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
6f6b4cd
gh-131591: Implement PEP 768
pablogsal Mar 22, 2025
9b86022
Add tests and use io.open_code
pablogsal Mar 23, 2025
af84100
Some fixes
pablogsal Mar 23, 2025
19ef7ae
Prepare for windows support
pablogsal Mar 24, 2025
444453c
Update windows files
pablogsal Mar 24, 2025
1d3ad3c
Fix syntax error
ivonastojanovic Mar 25, 2025
eeec1f6
Merge pull request #97 from ivonastojanovic/fix_windows_files
pablogsal Mar 25, 2025
fd993e3
Locate PyRuntime on Windows
ivonastojanovic Mar 27, 2025
96798c3
Read process memory on Windows
ivonastojanovic Mar 27, 2025
075ca65
Write to process memory on Windows
ivonastojanovic Mar 27, 2025
45e73c5
Prevent attaching to a process with a different architecture
ivonastojanovic Mar 28, 2025
ed2f325
Revert "Prevent attaching to a process with a different architecture"
godlygeek Mar 28, 2025
6076548
Merge pull request #98 from ivonastojanovic/external_debugger_windows…
godlygeek Mar 28, 2025
a9d3ea9
Remove unused struct member
godlygeek Mar 28, 2025
e235e62
Set an explicit size for a struct member
godlygeek Mar 28, 2025
d51dda0
Revert an accidental whitespace change
godlygeek Mar 28, 2025
38a4d51
Factor running a debugger script into a helper function
godlygeek Mar 28, 2025
a98898d
Harden remote debugging integration
godlygeek Mar 28, 2025
997b557
Use _fdopen to open the debugger script on Windows
godlygeek Mar 28, 2025
f6dec59
Ensure the debugger script is always closed
godlygeek Mar 28, 2025
d273c5b
Fix incorrect docstring
godlygeek Mar 29, 2025
c9a2146
Document restrictions for remote_exec
godlygeek Mar 29, 2025
4af1744
Simplify handling of Windows paths
godlygeek Mar 29, 2025
5c0b8b9
Make remote_exec accept `bytes` paths
godlygeek Mar 29, 2025
c8779cd
Refactor to avoid duplicate error handling
godlygeek Mar 26, 2025
6889042
Check for debug offset compatibility before using the offsets
godlygeek Mar 26, 2025
7f7aa8b
Give a some variables shorter names
godlygeek Mar 26, 2025
fbecfdb
Have read_memory/write_memory return 0 on success
godlygeek Mar 27, 2025
fa98f64
Improve an error message
godlygeek Mar 27, 2025
5b4cb00
Remove a debugging printf
godlygeek Mar 29, 2025
0dd7797
Fix a remote read to use the correct type
godlygeek Mar 29, 2025
b8a0503
Improve the error message when we can't find the requested thread
godlygeek Mar 29, 2025
9344d1d
Remove a useless check
godlygeek Mar 29, 2025
9368d38
Only accept a flag of 1 to mean remote debugging is enabled
godlygeek Mar 29, 2025
166f4d6
Merge remote-tracking branch 'upstream/main'
pablogsal Mar 31, 2025
d253966
Lint
pablogsal Mar 31, 2025
8e04fdd
simplify socket handling
pablogsal Mar 31, 2025
0c2b275
Add NEWS entry
pablogsal Mar 31, 2025
80856d3
Add more docs
pablogsal Mar 31, 2025
f01d8d3
Small fixes
pablogsal Mar 31, 2025
cca28c5
Small fixes
pablogsal Mar 31, 2025
727a02f
windows fix
pablogsal Mar 31, 2025
6ec528c
fix windows warnings
pablogsal Mar 31, 2025
f7e3963
Go back to previous test socket handling
pablogsal Mar 31, 2025
40bda7c
Fix docs lint
pablogsal Mar 31, 2025
780561b
Add defined(__FreeBSD__)
pablogsal Mar 31, 2025
2ab5d70
Small fixes
pablogsal Mar 31, 2025
3a5f004
windows fix
pablogsal Mar 31, 2025
6346063
Update 3.14.rst
pablogsal Mar 31, 2025
b442b1c
Update sys.rst
pablogsal Mar 31, 2025
3a4ed23
Address doc review
pablogsal Mar 31, 2025
8514d6e
Fix WASI
pablogsal Mar 31, 2025
72d3ece
whooopsy
pablogsal Mar 31, 2025
9130bb8
Fixes
pablogsal Mar 31, 2025
373d4c4
Update Doc/whatsnew/3.14.rst
pablogsal Mar 31, 2025
bccc8a8
Update Doc/using/configure.rst
pablogsal Mar 31, 2025
90ab65f
Fixes
pablogsal Mar 31, 2025
ffd340f
Address code review
pablogsal Mar 31, 2025
247e753
Apply suggestions from code review
pablogsal Mar 31, 2025
3c4c41c
Update Doc/library/sys.rst
pablogsal Mar 31, 2025
18c2e64
lint
pablogsal Mar 31, 2025
139b234
address code review from Steve
pablogsal Apr 1, 2025
4306a73
Merge remote-tracking branch 'upstream/main' into pep-768
pablogsal Apr 1, 2025
35003a8
Merge remote-tracking branch 'upstream/main' into pep-768
pablogsal Apr 1, 2025
80fab75
Use PyUnicode_FSDecoder instead of os.fsdecode
godlygeek Apr 1, 2025
0905105
Clean up #includes in remote_debugging.c
godlygeek Apr 1, 2025
8db68e3
Mark (read|write)_memory unreachable on platforms w/o remote debugging
godlygeek Apr 1, 2025
25cc486
Add error code to exception when failing to write memory on macOS
godlygeek Apr 1, 2025
0557de0
Fix the size for one of our remote reads
godlygeek Apr 1, 2025
6dd7530
Move reading eval_breaker closer to where it's written
godlygeek Apr 1, 2025
7c340e1
Merge remote-tracking branch 'upstream/main' into pep-768
pablogsal Apr 2, 2025
9096375
Handle arbitrarily long paths in /proc/<PID>/maps
godlygeek Apr 2, 2025
6516356
Ensure we munmap before closing the fd
godlygeek Apr 2, 2025
e6cb8fb
Merge branch 'main' into pep-768
pablogsal Apr 2, 2025
8b3ffa9
Update Doc/whatsnew/3.14.rst
pablogsal Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Locate PyRuntime on Windows
The PyRuntime section can be found in either the main executable (python.exe) or the Python DLL (pythonXY.dll, where X and Y represent the major and minor version numbers). Scan the modules of the process to locate the PyRuntime address in one of these modules.
  • Loading branch information
ivonastojanovic authored and godlygeek committed Mar 28, 2025
commit fd993e3d59fab50e2f5e8e5febb84b30f166695c
111 changes: 102 additions & 9 deletions Python/remote_debugging.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,26 +422,119 @@ search_map_for_section(proc_handle_t *handle, const char* secname, const char* m

#ifdef MS_WINDOWS

static void* analyze_pe(const wchar_t* mod_path, BYTE* remote_base, const char* secname) {
HANDLE hFile = CreateFileW(mod_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, 0);
if (!hMap) {
PyErr_SetFromWindowsErr(0);
CloseHandle(hFile);
return NULL;
}

BYTE* mapView = (BYTE*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if (!mapView) {
PyErr_SetFromWindowsErr(0);
CloseHandle(hMap);
CloseHandle(hFile);
return NULL;
}

IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)mapView;
if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE) {
PyErr_SetString(PyExc_RuntimeError, "Invalid DOS signature.");
UnmapViewOfFile(mapView);
CloseHandle(hMap);
CloseHandle(hFile);
return NULL;
}

IMAGE_NT_HEADERS* pNTHeaders = (IMAGE_NT_HEADERS*)(mapView + pDOSHeader->e_lfanew);
if (pNTHeaders->Signature != IMAGE_NT_SIGNATURE) {
PyErr_SetString(PyExc_RuntimeError, "Invalid NT signature.");
UnmapViewOfFile(mapView);
CloseHandle(hMap);
CloseHandle(hFile);
return NULL;
}

IMAGE_SECTION_HEADER* pSection_header = (IMAGE_SECTION_HEADER*)(mapView + pDOSHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
void* runtime_addr = NULL;

for (int i = 0; i < pNTHeaders->FileHeader.NumberOfSections; i++) {
const char* name = (const char*)pSection_header[i].Name;
if (strncmp(name, secname, IMAGE_SIZEOF_SHORT_NAME) == 0) {
runtime_addr = remote_base + pSection_header[i].VirtualAddress;
break;
}
}
Comment thread
pablogsal marked this conversation as resolved.

UnmapViewOfFile(mapView);
CloseHandle(hMap);
CloseHandle(hFile);

return runtime_addr;
}


static uintptr_t
search_map_for_section(proc_handle_t* handle, const char* secname, const char* substr) {
//TODO: Implement this function
PyErr_SetString(PyExc_RuntimeError, "search_map_for_section not implemented on Windows");
return 0;
search_windows_map_for_section(proc_handle_t* handle, const char* secname, const wchar_t* substr) {
HANDLE hProcSnap;
do {
hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, handle->pid);
} while (hProcSnap == INVALID_HANDLE_VALUE && GetLastError() == ERROR_BAD_LENGTH);

if (hProcSnap == INVALID_HANDLE_VALUE) {
PyErr_SetString(PyExc_PermissionError, "Unable to create module snapshot. Check permissions or PID.");
Comment thread
pablogsal marked this conversation as resolved.
return 0;
}

MODULEENTRY32W moduleEntry;
moduleEntry.dwSize = sizeof(moduleEntry);
void* runtime_addr = NULL;

for (BOOL hasModule = Module32FirstW(hProcSnap, &moduleEntry); hasModule; hasModule = Module32NextW(hProcSnap, &moduleEntry)) {
// Look for either python executable or DLL
if (wcsstr(moduleEntry.szModule, substr)) {
runtime_addr = analyze_pe(moduleEntry.szExePath, moduleEntry.modBaseAddr, secname);
if (runtime_addr != NULL) {
break;
}
}
}

CloseHandle(hProcSnap);
return (uintptr_t)runtime_addr;
}

#endif // MS_WINDOWS

// Get the PyRuntime section address for any platform
static uintptr_t
get_py_runtime(proc_handle_t *handle)
get_py_runtime(proc_handle_t* handle)
{
// Try libpython first, then fall back to python
uintptr_t address = search_map_for_section(handle, "PyRuntime", "libpython");
uintptr_t address = 0;

#ifndef MS_WINDOWS
// On non-Windows platforms, try libpython first, then fall back to python
address = search_map_for_section(handle, "PyRuntime", "libpython");
if (address == 0) {
// TODO: Differentiate between not found and error
PyErr_Clear();
address = search_map_for_section(handle, "PyRuntime", "python");
}
#else
// On Windows, search for 'python' in executable or DLL
address = search_windows_map_for_section(handle, "PyRuntime", L"python");
if (address == 0) {
// Error out: 'python' substring covers both executable and DLL
PyErr_SetString(PyExc_RuntimeError, "Failed to find the PyRuntime section in the process.");
Comment thread
zooba marked this conversation as resolved.
}
#endif

return address;
}

Expand All @@ -452,7 +545,7 @@ read_memory(proc_handle_t *handle, uint64_t remote_address, size_t len, void* ds
#ifdef MS_WINDOWS
// TODO: Implement this function
PyErr_SetString(PyExc_RuntimeError, "Memory reading is not supported on Windows");
return -1;
return -1;
#elif defined(__linux__) && HAVE_PROCESS_VM_READV
struct iovec local[1];
struct iovec remote[1];
Expand Down Expand Up @@ -512,7 +605,7 @@ write_memory(proc_handle_t *handle, uintptr_t remote_address, size_t len, const
#ifdef MS_WINDOWS
// TODO: Implement this function
PyErr_SetString(PyExc_RuntimeError, "Memory writing is not supported on Windows");
return -1;
return -1;
#elif defined(__linux__) && HAVE_PROCESS_VM_READV
struct iovec local[1];
struct iovec remote[1];
Expand Down