Skip to content

Commit d4e85a1

Browse files
GetCurrentProcess only once in win32 backtrace
1 parent 0f130c5 commit d4e85a1

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/backtrace/win32.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//!
99
//! Note that all dbghelp support is loaded dynamically, see `src/dbghelp.rs`
1010
//! for more information about that.
11+
#![deny(unsafe_op_in_unsafe_fn)]
1112

1213
use super::super::{dbghelp, windows_sys::*};
1314
use core::ffi::c_void;
@@ -97,11 +98,11 @@ struct MyContext(CONTEXT);
9798
#[inline(always)]
9899
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
99100
// Allocate necessary structures for doing the stack walk
100-
let process = GetCurrentProcess();
101-
let thread = GetCurrentThread();
101+
let process = unsafe { GetCurrentProcess() };
102+
let thread = unsafe { GetCurrentThread() };
102103

103-
let mut context = mem::zeroed::<MyContext>();
104-
RtlCaptureContext(&mut context.0);
104+
let mut context = unsafe { mem::zeroed::<MyContext>() };
105+
unsafe { RtlCaptureContext(&mut context.0) };
105106

106107
// Ensure this process's symbols are initialized
107108
let dbghelp = match dbghelp::init() {
@@ -112,11 +113,9 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
112113
let function_table_access = dbghelp.SymFunctionTableAccess64();
113114
let get_module_base = dbghelp.SymGetModuleBase64();
114115

115-
let process_handle = GetCurrentProcess();
116-
117116
// Attempt to use `StackWalkEx` if we can, but fall back to `StackWalk64`
118117
// since it's in theory supported on more systems.
119-
match (*dbghelp.dbghelp()).StackWalkEx() {
118+
match unsafe { (*dbghelp.dbghelp()).StackWalkEx() } {
120119
#[allow(non_snake_case)]
121120
Some(StackWalkEx) => {
122121
let mut inner: STACKFRAME_EX = mem::zeroed();
@@ -133,20 +132,22 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
133132
_ => unreachable!(),
134133
};
135134

136-
while StackWalkEx(
137-
image as u32,
138-
process,
139-
thread,
140-
frame_ptr,
141-
&mut context.0 as *mut CONTEXT as *mut _,
142-
None,
143-
Some(function_table_access),
144-
Some(get_module_base),
145-
None,
146-
0,
147-
) == TRUE
148-
{
149-
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;
135+
while unsafe {
136+
StackWalkEx(
137+
image as u32,
138+
process,
139+
thread,
140+
frame_ptr,
141+
&mut context.0 as *mut CONTEXT as *mut _,
142+
None,
143+
Some(function_table_access),
144+
Some(get_module_base),
145+
None,
146+
0,
147+
) == TRUE
148+
} {
149+
frame.inner.base_address =
150+
unsafe { get_module_base(process, frame.ip() as _) as _ };
150151

151152
if !cb(&frame) {
152153
break;
@@ -166,19 +167,21 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
166167
_ => unreachable!(),
167168
};
168169

169-
while dbghelp.StackWalk64()(
170-
image as u32,
171-
process,
172-
thread,
173-
frame_ptr,
174-
&mut context.0 as *mut CONTEXT as *mut _,
175-
None,
176-
Some(function_table_access),
177-
Some(get_module_base),
178-
None,
179-
) == TRUE
180-
{
181-
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;
170+
while unsafe {
171+
dbghelp.StackWalk64()(
172+
image as u32,
173+
process,
174+
thread,
175+
frame_ptr,
176+
&mut context.0 as *mut CONTEXT as *mut _,
177+
None,
178+
Some(function_table_access),
179+
Some(get_module_base),
180+
None,
181+
) == TRUE
182+
} {
183+
frame.inner.base_address =
184+
unsafe { get_module_base(process, frame.ip() as _) as _ };
182185

183186
if !cb(&frame) {
184187
break;

0 commit comments

Comments
 (0)