Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,29 @@ def test_unsupported_platform_error(self):

@requires_remote_subprocess_debugging()
class TestDetectionOfThreadStatus(RemoteInspectionTestBase):
@unittest.skipIf(
sys.platform not in ("linux", "darwin", "win32"),
"Test only runs on supported platforms (Linux, macOS, or Windows)",
)
@unittest.skipIf(
sys.platform == "android", "Android raises Linux-specific exception"
)
def test_cpu_mode_refreshes_thread_status(self):
script = """\
sock.recv(1)
while True:
pass
"""
with self._target_process(script) as (p, sock, _):
unwinder = RemoteUnwinder(p.pid, mode=PROFILING_MODE_CPU)
_get_stack_trace_with_retry(
unwinder, condition=lambda traces: not traces[0].threads
)
sock.sendall(b"x")
_get_stack_trace_with_retry(
unwinder, condition=lambda traces: bool(traces[0].threads)
)

def _run_thread_status_test(self, mode, check_condition):
"""
Common pattern for thread status detection tests.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix empty CPU profiles in :mod:`profiling.sampling` when running 32-bit
Python on Windows Server 2022.
8 changes: 8 additions & 0 deletions Modules/_remote_debugging/_remote_debugging.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ typedef struct {

#ifdef MS_WINDOWS
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L)
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
#define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL)
#endif

Expand Down Expand Up @@ -371,6 +373,12 @@ typedef struct {
UnwinderStats stats; // statistics for performance analysis
InterpreterTstateCacheEntry cached_tstates[INTERPRETER_THREAD_CACHE_SIZE];
InterpreterGenerationCacheEntry cached_generations[INTERPRETER_THREAD_CACHE_SIZE];
#ifdef MS_WINDOWS
int win_use_process_snapshot;
ULONG win_process_buffer_size;
void *win_process_buffer;
SYSTEM_PROCESS_INFORMATION *win_process_info; // Points into win_process_buffer
#endif
#ifdef Py_GIL_DISABLED
uint32_t tlbc_generation;
_Py_hashtable_t *tlbc_cache;
Expand Down
7 changes: 7 additions & 0 deletions Modules/_remote_debugging/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self
return NULL;
}

#ifdef MS_WINDOWS
self->win_process_info = NULL;
#endif

// Iterate over all interpreters
uintptr_t current_interpreter = self->interpreter_addr;
while (current_interpreter != 0) {
Expand Down Expand Up @@ -1315,6 +1319,9 @@ RemoteUnwinder_dealloc(PyObject *op)
RemoteUnwinderObject *self = RemoteUnwinder_CAST(op);
PyTypeObject *tp = Py_TYPE(self);

#ifdef MS_WINDOWS
PyMem_Free(self->win_process_buffer);
#endif
#ifdef Py_REMOTE_DEBUG_SUPPORTS_BLOCKING
if (self->threads_stopped) {
_Py_RemoteDebug_ResumeAllThreads(self, &self->threads_state);
Expand Down
62 changes: 62 additions & 0 deletions Modules/_remote_debugging/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,60 @@ find_running_frame(
* THREAD STATUS FUNCTIONS
* ============================================================================ */

#ifdef MS_WINDOWS
static SYSTEM_PROCESS_INFORMATION *
win32_get_process_info(RemoteUnwinderObject *unwinder)
{
for (;;) {
ULONG needed;
NTSTATUS status = NtQuerySystemInformation(
SystemProcessInformation, unwinder->win_process_buffer,
unwinder->win_process_buffer_size, &needed);
if (status == STATUS_SUCCESS) {
break;
}
if (status != STATUS_INFO_LENGTH_MISMATCH ||
needed <= unwinder->win_process_buffer_size) {
return NULL;
}
void *buffer = PyMem_Realloc(unwinder->win_process_buffer, needed);
if (buffer == NULL) {
return NULL;
}
unwinder->win_process_buffer = buffer;
unwinder->win_process_buffer_size = needed;
}

SYSTEM_PROCESS_INFORMATION *process = unwinder->win_process_buffer;
while ((ULONG_PTR)process->UniqueProcessId != (ULONG_PTR)unwinder->handle.pid) {
if (process->NextEntryOffset == 0) {
return NULL;
}
process = (SYSTEM_PROCESS_INFORMATION *)((char *)process + process->NextEntryOffset);
}
return process;
}

static int
get_snapshot_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid)
{
if (unwinder->win_process_info == NULL) {
unwinder->win_process_info = win32_get_process_info(unwinder);
}
SYSTEM_PROCESS_INFORMATION *process = unwinder->win_process_info;
if (process != NULL) {
SYSTEM_THREAD_INFORMATION *threads = (SYSTEM_THREAD_INFORMATION *)(process + 1);
for (ULONG i = 0; i < process->NumberOfThreads; i++) {
if ((ULONG_PTR)threads[i].ClientId.UniqueThread == tid) {
return threads[i].ThreadState == WIN32_THREAD_RUNNING
? THREAD_STATE_RUNNING : THREAD_STATE_IDLE;
}
}
}
return THREAD_STATE_UNKNOWN;
}
#endif

static int
get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
#if defined(__APPLE__) && TARGET_OS_OSX
Expand Down Expand Up @@ -217,6 +271,9 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
return THREAD_STATE_UNKNOWN;
}
#elif defined(MS_WINDOWS)
if (unwinder->win_use_process_snapshot) {
return get_snapshot_thread_status(unwinder, tid);
}
HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid);
if (thread == NULL) {
return THREAD_STATE_UNKNOWN;
Expand All @@ -227,6 +284,11 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION,
&info, sizeof(info), &size);
CloseHandle(thread);
if (result == STATUS_INVALID_INFO_CLASS) {
// WOW64 on Windows Server 2022 does not support this information class.
unwinder->win_use_process_snapshot = 1;
return get_snapshot_thread_status(unwinder, tid);
}
if (result != STATUS_SUCCESS || size != sizeof(info) ||
(ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid ||
(ULONG_PTR)info.ClientId.UniqueThread != tid)
Expand Down
Loading