@@ -97,19 +97,23 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool, thread: *mut c_vo
97
97
if thread == GetCurrentThread ( ) || thread. is_null ( ) {
98
98
RtlCaptureContext ( & mut context. 0 ) ;
99
99
} else {
100
- const CONTEXT_i386 : u32 = 0x10000 ;
101
- const CONTEXT_CONTROL : u32 = CONTEXT_i386 | 0x01 ; // SS:SP, CS:IP, FLAGS, B;
102
- const CONTEXT_INTEGER : u32 = CONTEXT_i386 | 0x02 ; // AX, BX, CX, DX, SI, D;
103
- const CONTEXT_SEGMENTS : u32 = CONTEXT_i386 | 0x04 ; // DS, ES, FS, G;
104
- const CONTEXT_FLOATING_POINT : u32 = CONTEXT_i386 | 0x08 ; // 387 stat;
105
- const CONTEXT_DEBUG_REGISTERS : u32 = CONTEXT_i386 | 0x10 ; // DB 0-3,6,;
106
- const CONTEXT_EXTENDED_REGISTERS : u32 = CONTEXT_i386 | 0x20 ; // cpu specific extension;
107
- const CONTEXT_ALL : u32 = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | CONTEXT_EXTENDED_REGISTERS ;
108
-
109
- context. 0 . ContextFlags = CONTEXT_ALL ; // TODO: Narrow down flags
100
+ // The suspending and resuming of threads is very risky.
101
+ // It can end in a deadlock if the current thread tries to access
102
+ // an object thats been locked by the suspended thread.
103
+ // That's why we only do as little work as possible while
104
+ // the thread is suspended, and resume it quickly after.
105
+
106
+ // There is most definitely more pitfalls i haven't thought
107
+ // of or encountered. This is windows after all.
108
+
109
+ context. 0 . ContextFlags = CONTEXT_ALL ; // TODO: Narrow down required flags
110
+ if SuspendThread ( thread) as i32 == -1 {
111
+ ResumeThread ( thread) ;
112
+ return ;
113
+ }
110
114
let status = GetThreadContext ( thread, & mut context. 0 ) ;
111
- if status == 0 {
112
- return // GetThreadContext failed
115
+ if ResumeThread ( thread ) as i32 == - 1 || status == 0 {
116
+ return ;
113
117
}
114
118
}
115
119
0 commit comments