Skip to content

Commit 13caae4

Browse files
pablogsalmaurycy
authored andcommitted
gh-138122: Simplify Tachyon's macOS and Windows inspection (GH-161956)
(cherry picked from commit d8bc6669c7f4bcf1a72599552b99fbcb4fa7a702) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
1 parent 062fc36 commit 13caae4

6 files changed

Lines changed: 336 additions & 443 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4556,6 +4556,28 @@ def test_dead_process_raises_not_degrades(self):
45564556
else:
45574557
self.fail("ProcessLookupError never raised for dead process")
45584558

4559+
def test_dead_process_before_first_async_read(self):
4560+
script_body = """\
4561+
import asyncio
4562+
sock.sendall(b"ready")
4563+
sock.recv(16)
4564+
"""
4565+
with self._target_process(script_body) as (p, client_socket, _):
4566+
_wait_for_signal(client_socket, b"ready")
4567+
unwinders = [
4568+
RemoteUnwinder(p.pid, debug=debug)
4569+
for debug in (False, True)
4570+
]
4571+
4572+
p.kill()
4573+
p.wait()
4574+
4575+
for debug, unwinder in zip((False, True), unwinders):
4576+
for method in ("get_async_stack_trace", "get_all_awaited_by"):
4577+
with self.subTest(debug=debug, method=method):
4578+
with self.assertRaises(ProcessLookupError):
4579+
getattr(unwinder, method)()
4580+
45594581

45604582
@requires_remote_subprocess_debugging()
45614583
class TestFrameChainLimits(RemoteInspectionTestBase):

‎Modules/_remote_debugging/_remote_debugging.h‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ extern "C" {
5656
# if TARGET_OS_OSX
5757
# include <libproc.h>
5858
# include <sys/types.h>
59-
# define MAX_NATIVE_THREADS 4096
6059
# endif
6160
#endif
6261

@@ -104,18 +103,7 @@ typedef struct {
104103

105104
#ifdef MS_WINDOWS
106105
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
107-
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
108106
#define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL)
109-
typedef enum _WIN32_THREADSTATE {
110-
WIN32_THREADSTATE_INITIALIZED = 0,
111-
WIN32_THREADSTATE_READY = 1,
112-
WIN32_THREADSTATE_RUNNING = 2,
113-
WIN32_THREADSTATE_STANDBY = 3,
114-
WIN32_THREADSTATE_TERMINATED = 4,
115-
WIN32_THREADSTATE_WAITING = 5,
116-
WIN32_THREADSTATE_TRANSITION = 6,
117-
WIN32_THREADSTATE_UNKNOWN = 7
118-
} WIN32_THREADSTATE;
119107
#endif
120108

121109
/* ============================================================================
@@ -387,14 +375,6 @@ typedef struct {
387375
#ifdef Py_GIL_DISABLED
388376
uint32_t tlbc_generation;
389377
_Py_hashtable_t *tlbc_cache;
390-
#endif
391-
#ifdef __APPLE__
392-
uint64_t thread_id_offset;
393-
int thread_id_offset_initialized;
394-
#endif
395-
#ifdef MS_WINDOWS
396-
PVOID win_process_buffer;
397-
ULONG win_process_buffer_size;
398378
#endif
399379
// Thread stopping state (only on platforms that support it)
400380
#ifdef Py_REMOTE_DEBUG_SUPPORTS_BLOCKING
@@ -681,8 +661,6 @@ extern int find_running_frame(
681661
uintptr_t *frame
682662
);
683663

684-
extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id);
685-
686664
extern PyObject* unwind_stack_for_thread(
687665
RemoteUnwinderObject *unwinder,
688666
uintptr_t *current_tstate,

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
2222
address = search_windows_map_for_section(handle, "AsyncioD", L"_asyncio",
2323
NULL);
2424
if (address == 0) {
25-
if (!_Py_RemoteDebug_HasPermissionError()) {
25+
if (!_Py_RemoteDebug_IsFatalReadError()) {
2626
PyObject *exc = PyErr_GetRaisedException();
2727
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
2828
_PyErr_ChainExceptions1(exc);
@@ -33,23 +33,16 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
3333
address = search_linux_map_for_section(handle, "AsyncioDebug", "python",
3434
NULL);
3535
if (address == 0) {
36-
if (!_Py_RemoteDebug_HasPermissionError()) {
36+
if (!_Py_RemoteDebug_IsFatalReadError()) {
3737
PyObject *exc = PyErr_GetRaisedException();
3838
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
3939
_PyErr_ChainExceptions1(exc);
4040
}
4141
}
4242
#elif defined(__APPLE__) && TARGET_OS_OSX
43-
// On macOS, try libpython first, then fall back to python
44-
address = search_map_for_section(handle, "AsyncioDebug", "libpython",
45-
NULL);
46-
if (address == 0 && !_Py_RemoteDebug_HasPermissionError()) {
47-
PyErr_Clear();
48-
address = search_map_for_section(handle, "AsyncioDebug", "python",
49-
NULL);
50-
}
43+
address = search_loaded_images_for_section(handle, "AsyncioDebug", NULL);
5144
if (address == 0) {
52-
if (!_Py_RemoteDebug_HasPermissionError()) {
45+
if (!_Py_RemoteDebug_IsFatalReadError()) {
5346
PyObject *exc = PyErr_GetRaisedException();
5447
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
5548
_PyErr_ChainExceptions1(exc);
@@ -99,12 +92,22 @@ ensure_async_debug_offsets(RemoteUnwinderObject *unwinder)
9992
return -1;
10093
}
10194
if (result < 0) {
102-
if (!_Py_RemoteDebug_HasPermissionError()) {
103-
PyErr_Clear();
104-
PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available");
105-
set_exception_cause(unwinder, PyExc_RuntimeError,
106-
"AsyncioDebug section unavailable - asyncio module may not be loaded in target process");
95+
if (_Py_RemoteDebug_IsFatalReadError()) {
96+
return -1;
10797
}
98+
PyErr_Clear();
99+
/* Image discovery can fail without reporting that the target exited.
100+
Read a known runtime address before reporting unavailable asyncio. */
101+
char runtime_byte;
102+
if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle,
103+
unwinder->runtime_start_address, sizeof(runtime_byte),
104+
&runtime_byte) < 0)
105+
{
106+
return -1;
107+
}
108+
PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available");
109+
set_exception_cause(unwinder, PyExc_RuntimeError,
110+
"AsyncioDebug section unavailable - asyncio module may not be loaded in target process");
108111
return -1;
109112
}
110113

‎Modules/_remote_debugging/module.c‎

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self,
384384
return -1;
385385
}
386386

387-
self->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&self->handle);
388-
if (self->runtime_start_address == 0) {
389-
set_exception_cause(self, PyExc_RuntimeError, "Failed to get Python runtime address");
390-
return -1;
391-
}
392-
393387
if (_Py_RemoteDebug_ReadDebugOffsets(&self->handle,
394388
&self->runtime_start_address,
395389
&self->debug_offsets) < 0)
@@ -404,20 +398,8 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self,
404398
return -1;
405399
}
406400

407-
// Try to read async debug offsets, but don't fail if they're not available
408-
self->async_debug_offsets_available = 1;
409-
int async_debug_result = read_async_debug(self);
410-
if (async_debug_result == PY_REMOTE_DEBUG_INVALID_ASYNC_DEBUG_OFFSETS) {
411-
return -1;
412-
}
413-
if (async_debug_result < 0) {
414-
if (_Py_RemoteDebug_HasPermissionError()) {
415-
return -1;
416-
}
417-
PyErr_Clear();
418-
memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets));
419-
self->async_debug_offsets_available = 0;
420-
}
401+
self->async_debug_offsets_available = 0;
402+
memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets));
421403

422404
if (populate_initial_state_data(all_threads, self, self->runtime_start_address,
423405
&self->interpreter_addr ,&self->tstate_addr) < 0)
@@ -456,15 +438,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self,
456438
}
457439
#endif
458440

459-
#if defined(__APPLE__)
460-
self->thread_id_offset = 0;
461-
self->thread_id_offset_initialized = 0;
462-
#endif
463-
464-
#ifdef MS_WINDOWS
465-
self->win_process_buffer = NULL;
466-
self->win_process_buffer_size = 0;
467-
#endif
468441
#ifdef __linux__
469442
self->thread_tids = NULL;
470443
self->thread_tids_capacity = 0;
@@ -1321,11 +1294,6 @@ RemoteUnwinder_dealloc(PyObject *op)
13211294
if (self->code_object_cache) {
13221295
_Py_hashtable_destroy(self->code_object_cache);
13231296
}
1324-
#ifdef MS_WINDOWS
1325-
if (self->win_process_buffer != NULL) {
1326-
PyMem_Free(self->win_process_buffer);
1327-
}
1328-
#endif
13291297

13301298
#ifdef Py_GIL_DISABLED
13311299
if (self->tlbc_cache) {
@@ -1380,11 +1348,6 @@ init_runtime_offsets(RuntimeOffsets *offsets, int pid, int debug)
13801348
set_exception_cause(offsets, PyExc_RuntimeError, "Failed to initialize process handle");
13811349
return -1;
13821350
}
1383-
offsets->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&offsets->handle);
1384-
if (offsets->runtime_start_address == 0) {
1385-
set_exception_cause(offsets, PyExc_RuntimeError, "Failed to get Python runtime address");
1386-
goto error;
1387-
}
13881351
if (_Py_RemoteDebug_ReadDebugOffsets(&offsets->handle,
13891352
&offsets->runtime_start_address,
13901353
&offsets->debug_offsets) < 0)

‎Modules/_remote_debugging/threads.c‎

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
#include <sys/wait.h>
1919
#endif
2020

21+
#ifdef MS_WINDOWS
22+
enum {
23+
/* ThreadSystemThreadInformation is not named in the Windows SDK. */
24+
WIN32_THREAD_SYSTEM_INFORMATION = 40,
25+
WIN32_THREAD_RUNNING = 2,
26+
};
27+
#endif
28+
2129
/* Bound traversal of corrupted remote exception chains. */
2230
#define MAX_EXCEPTION_CHAIN_DEPTH (2 << 15)
2331

@@ -158,45 +166,22 @@ find_running_frame(
158166
* THREAD STATUS FUNCTIONS
159167
* ============================================================================ */
160168

161-
int
162-
get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id) {
169+
static int
170+
get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
163171
#if defined(__APPLE__) && TARGET_OS_OSX
164-
if (!unwinder->thread_id_offset_initialized) {
165-
uint64_t *tids = (uint64_t *)PyMem_Malloc(MAX_NATIVE_THREADS * sizeof(uint64_t));
166-
if (!tids) {
167-
// Non-fatal: thread status is best-effort
168-
return THREAD_STATE_UNKNOWN;
169-
}
170-
int n = proc_pidinfo(unwinder->handle.pid, PROC_PIDLISTTHREADS, 0, tids, MAX_NATIVE_THREADS * sizeof(uint64_t)) / sizeof(uint64_t);
171-
if (n <= 0) {
172-
PyMem_Free(tids);
173-
return THREAD_STATE_UNKNOWN;
174-
}
175-
uint64_t min_offset = UINT64_MAX;
176-
for (int i = 0; i < n; i++) {
177-
uint64_t offset = tids[i] - pthread_id;
178-
if (offset < min_offset) {
179-
min_offset = offset;
180-
}
181-
}
182-
unwinder->thread_id_offset = min_offset;
183-
unwinder->thread_id_offset_initialized = 1;
184-
PyMem_Free(tids);
185-
}
186-
struct proc_threadinfo ti;
187-
uint64_t tid_with_offset = pthread_id + unwinder->thread_id_offset;
188-
if (proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADINFO, tid_with_offset, &ti, sizeof(ti)) != sizeof(ti)) {
172+
struct proc_threadinfo thread;
173+
int size = proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADID64INFO,
174+
tid, &thread, sizeof(thread));
175+
if (size != sizeof(thread)) {
189176
return THREAD_STATE_UNKNOWN;
190177
}
191-
if (ti.pth_run_state == TH_STATE_RUNNING) {
192-
return THREAD_STATE_RUNNING;
193-
}
194-
return THREAD_STATE_IDLE;
178+
return thread.pth_run_state == TH_STATE_RUNNING
179+
? THREAD_STATE_RUNNING : THREAD_STATE_IDLE;
195180
#elif defined(__linux__)
196181
char stat_path[256];
197182
char buffer[2048] = "";
198183

199-
snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%" PRIu64 "/stat", unwinder->handle.pid, tid);
184+
snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%lu/stat", unwinder->handle.pid, tid);
200185

201186
int fd = open(stat_path, O_RDONLY);
202187
if (fd == -1) {
@@ -232,46 +217,24 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread
232217
return THREAD_STATE_UNKNOWN;
233218
}
234219
#elif defined(MS_WINDOWS)
235-
ULONG n;
236-
NTSTATUS status = NtQuerySystemInformation(
237-
SystemProcessInformation,
238-
unwinder->win_process_buffer,
239-
unwinder->win_process_buffer_size,
240-
&n
241-
);
242-
if (status == STATUS_INFO_LENGTH_MISMATCH) {
243-
// Buffer was too small so we reallocate a larger one and try again.
244-
unwinder->win_process_buffer_size = n;
245-
PVOID new_buffer = PyMem_Realloc(unwinder->win_process_buffer, n);
246-
if (!new_buffer) {
247-
// Match Linux/macOS: degrade gracefully on alloc failure
248-
return THREAD_STATE_UNKNOWN;
249-
}
250-
unwinder->win_process_buffer = new_buffer;
251-
return get_thread_status(unwinder, tid, pthread_id);
252-
}
253-
if (status != STATUS_SUCCESS) {
220+
HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid);
221+
if (thread == NULL) {
254222
return THREAD_STATE_UNKNOWN;
255223
}
256-
257-
SYSTEM_PROCESS_INFORMATION *pi = (SYSTEM_PROCESS_INFORMATION *)unwinder->win_process_buffer;
258-
while ((ULONG)(ULONG_PTR)pi->UniqueProcessId != unwinder->handle.pid) {
259-
if (pi->NextEntryOffset == 0) {
260-
// Process not found (may have exited)
261-
return THREAD_STATE_UNKNOWN;
262-
}
263-
pi = (SYSTEM_PROCESS_INFORMATION *)(((BYTE *)pi) + pi->NextEntryOffset);
264-
}
265-
266-
SYSTEM_THREAD_INFORMATION *ti = (SYSTEM_THREAD_INFORMATION *)((char *)pi + sizeof(SYSTEM_PROCESS_INFORMATION));
267-
for (size_t i = 0; i < pi->NumberOfThreads; i++, ti++) {
268-
if (ti->ClientId.UniqueThread == (HANDLE)tid) {
269-
return ti->ThreadState != WIN32_THREADSTATE_RUNNING ? THREAD_STATE_IDLE : THREAD_STATE_RUNNING;
270-
}
224+
SYSTEM_THREAD_INFORMATION info;
225+
ULONG size = 0;
226+
NTSTATUS result = NtQueryInformationThread(
227+
thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION,
228+
&info, sizeof(info), &size);
229+
CloseHandle(thread);
230+
if (result != STATUS_SUCCESS || size != sizeof(info) ||
231+
(ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid ||
232+
(ULONG_PTR)info.ClientId.UniqueThread != tid)
233+
{
234+
return THREAD_STATE_UNKNOWN;
271235
}
272-
273-
// Thread not found (may have exited)
274-
return THREAD_STATE_UNKNOWN;
236+
return info.ThreadState == WIN32_THREAD_RUNNING
237+
? THREAD_STATE_RUNNING : THREAD_STATE_IDLE;
275238
#else
276239
return THREAD_STATE_UNKNOWN;
277240
#endif
@@ -398,7 +361,8 @@ unwind_stack_for_thread(
398361
STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME);
399362
}
400363

401-
long tid = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.native_thread_id);
364+
unsigned long tid = GET_MEMBER(unsigned long, ts,
365+
unwinder->debug_offsets.thread_state.native_thread_id);
402366

403367
// Calculate thread status using flags (always)
404368
int status_flags = 0;
@@ -490,13 +454,9 @@ unwind_stack_for_thread(
490454
status_flags |= THREAD_STATUS_HAS_EXCEPTION;
491455
}
492456

493-
// Check CPU status
494-
long pthread_id = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.thread_id);
495-
496-
// Optimization: only check CPU status if needed by mode because it's expensive
497457
int cpu_status = THREAD_STATE_UNKNOWN;
498458
if (unwinder->mode == PROFILING_MODE_CPU || unwinder->mode == PROFILING_MODE_ALL) {
499-
cpu_status = get_thread_status(unwinder, tid, pthread_id);
459+
cpu_status = get_thread_status(unwinder, tid);
500460
}
501461

502462
if (cpu_status == THREAD_STATE_UNKNOWN) {
@@ -597,7 +557,7 @@ unwind_stack_for_thread(
597557
}
598558
}
599559
if (thread_id == NULL) {
600-
thread_id = PyLong_FromLongLong(tid);
560+
thread_id = PyLong_FromUnsignedLong(tid);
601561
if (thread_id == NULL) {
602562
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID");
603563
goto error;

0 commit comments

Comments
 (0)