|
18 | 18 | #include <sys/wait.h> |
19 | 19 | #endif |
20 | 20 |
|
| 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 | + |
21 | 29 | /* Bound traversal of corrupted remote exception chains. */ |
22 | 30 | #define MAX_EXCEPTION_CHAIN_DEPTH (2 << 15) |
23 | 31 |
|
@@ -158,45 +166,22 @@ find_running_frame( |
158 | 166 | * THREAD STATUS FUNCTIONS |
159 | 167 | * ============================================================================ */ |
160 | 168 |
|
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) { |
163 | 171 | #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)) { |
189 | 176 | return THREAD_STATE_UNKNOWN; |
190 | 177 | } |
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; |
195 | 180 | #elif defined(__linux__) |
196 | 181 | char stat_path[256]; |
197 | 182 | char buffer[2048] = ""; |
198 | 183 |
|
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); |
200 | 185 |
|
201 | 186 | int fd = open(stat_path, O_RDONLY); |
202 | 187 | if (fd == -1) { |
@@ -232,46 +217,24 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread |
232 | 217 | return THREAD_STATE_UNKNOWN; |
233 | 218 | } |
234 | 219 | #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) { |
254 | 222 | return THREAD_STATE_UNKNOWN; |
255 | 223 | } |
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; |
271 | 235 | } |
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; |
275 | 238 | #else |
276 | 239 | return THREAD_STATE_UNKNOWN; |
277 | 240 | #endif |
@@ -398,7 +361,8 @@ unwind_stack_for_thread( |
398 | 361 | STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME); |
399 | 362 | } |
400 | 363 |
|
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); |
402 | 366 |
|
403 | 367 | // Calculate thread status using flags (always) |
404 | 368 | int status_flags = 0; |
@@ -490,13 +454,9 @@ unwind_stack_for_thread( |
490 | 454 | status_flags |= THREAD_STATUS_HAS_EXCEPTION; |
491 | 455 | } |
492 | 456 |
|
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 |
497 | 457 | int cpu_status = THREAD_STATE_UNKNOWN; |
498 | 458 | 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); |
500 | 460 | } |
501 | 461 |
|
502 | 462 | if (cpu_status == THREAD_STATE_UNKNOWN) { |
@@ -597,7 +557,7 @@ unwind_stack_for_thread( |
597 | 557 | } |
598 | 558 | } |
599 | 559 | if (thread_id == NULL) { |
600 | | - thread_id = PyLong_FromLongLong(tid); |
| 560 | + thread_id = PyLong_FromUnsignedLong(tid); |
601 | 561 | if (thread_id == NULL) { |
602 | 562 | set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID"); |
603 | 563 | goto error; |
|
0 commit comments