Skip to content

Commit 68e1927

Browse files
committed
fix: add a separate trace_thread function
1 parent f7d4e6d commit 68e1927

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

examples/other_thread.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Windows only currently
2-
use std::os::windows::prelude::AsRawHandle;
32
use backtrace::{Backtrace, BacktraceFrame};
4-
3+
use std::os::windows::prelude::AsRawHandle;
54

65
fn worker() {
76
foo();
@@ -18,22 +17,21 @@ fn baz() {
1817
//std::thread::sleep(std::time::Duration::from_millis(1000));
1918
loop {
2019
print!("");
21-
};
20+
}
2221
}
2322

24-
2523
fn main() {
2624
let thread = std::thread::spawn(|| {
2725
worker();
2826
});
2927
let os_handle = thread.as_raw_handle();
30-
28+
3129
// Allow the thread to start
3230
std::thread::sleep(std::time::Duration::from_millis(100));
3331

3432
let mut frames = Vec::new();
3533
unsafe {
36-
backtrace::trace_thread_unsynchronized(os_handle,|frame| {
34+
backtrace::trace_thread_unsynchronized(os_handle, |frame| {
3735
frames.push(BacktraceFrame::from(frame.clone()));
3836
true
3937
});

src/backtrace/dbghelp.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,14 @@ impl Frame {
8888
#[repr(C, align(16))] // required by `CONTEXT`, is a FIXME in winapi right now
8989
struct MyContext(CONTEXT);
9090

91-
//#[inline(always)]
92-
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool, thread: *mut c_void) {
91+
#[inline(always)]
92+
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
93+
let thread = GetCurrentThread();
94+
trace_thread(cb, thread)
95+
}
96+
97+
#[inline(always)]
98+
pub unsafe fn trace_thread(cb: &mut dyn FnMut(&super::Frame) -> bool, thread: *mut c_void) {
9399
// Allocate necessary structures for doing the stack walk
94100
let process = GetCurrentProcess();
95101

@@ -112,12 +118,11 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool, thread: *mut c_vo
112118
return;
113119
}
114120
let status = GetThreadContext(thread, &mut context.0);
115-
if ResumeThread(thread) as i32 == -1 || status == 0{
121+
if ResumeThread(thread) as i32 == -1 || status == 0 {
116122
return;
117123
}
118124
}
119125

120-
121126
// Ensure this process's symbols are initialized
122127
let dbghelp = match dbghelp::init() {
123128
Ok(dbghelp) => dbghelp,

src/backtrace/mod.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use core::ffi::c_void;
22
use core::fmt;
33

4+
use self::dbghelp::trace_thread;
5+
46
/// Inspects the current call-stack, passing all active frames into the closure
57
/// provided to calculate a stack trace.
68
///
@@ -63,12 +65,25 @@ pub fn trace<F: FnMut(&Frame) -> bool>(cb: F) {
6365
///
6466
/// See information on `trace` for caveats on `cb` panicking.
6567
pub unsafe fn trace_unsynchronized<F: FnMut(&Frame) -> bool>(mut cb: F) {
66-
trace_imp(&mut cb, 0 as _)
68+
trace_imp(&mut cb)
6769
}
6870

69-
/// TODO docs
70-
pub unsafe fn trace_thread_unsynchronized<F: FnMut(&Frame) -> bool>(thread: *mut c_void, mut cb: F) {
71-
trace_imp(&mut cb, thread)
71+
/// Similar to [trace_unsynchronized], but additionally, it supports tracing a thread other than the current thread.
72+
///
73+
/// The function gets the traced thread's handle as its first argument.
74+
///
75+
/// This function does not have synchronization guarantees but is available
76+
/// when the `std` feature of this crate isn't compiled in. See the `trace`
77+
/// function for more documentation and examples.
78+
///
79+
/// # Panics
80+
///
81+
/// See information on `trace` for caveats on `cb` panicking.
82+
pub unsafe fn trace_thread_unsynchronized<F: FnMut(&Frame) -> bool>(
83+
thread: *mut c_void,
84+
mut cb: F,
85+
) {
86+
trace_thread(&mut cb, thread)
7287
}
7388

7489
/// A trait representing one frame of a backtrace, yielded to the `trace`

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extern crate std;
110110
#[allow(unused_extern_crates)]
111111
extern crate alloc;
112112

113-
pub use self::backtrace::{trace_unsynchronized, trace_thread_unsynchronized, Frame};
113+
pub use self::backtrace::{trace_thread_unsynchronized, trace_unsynchronized, Frame};
114114
mod backtrace;
115115

116116
pub use self::symbolize::resolve_frame_unsynchronized;

0 commit comments

Comments
 (0)