Skip to content

Commit f433ffd

Browse files
Jardynqaminya
authored andcommitted
Added example for capturing backtrace of non calling thread
1 parent 7f3474f commit f433ffd

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/other_thread.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Windows only currently
2+
use std::os::windows::prelude::AsRawHandle;
3+
use backtrace::{Backtrace, BacktraceFrame};
4+
5+
6+
fn worker() {
7+
foo();
8+
}
9+
fn foo() {
10+
bar()
11+
}
12+
fn bar() {
13+
baz()
14+
}
15+
fn baz() {
16+
println!("Hello from thread!");
17+
// Sleep for simple sync. Can't read thread that has finished running
18+
//std::thread::sleep(std::time::Duration::from_millis(1000));
19+
loop {
20+
print!("");
21+
};
22+
}
23+
24+
25+
fn main() {
26+
let thread = std::thread::spawn(|| {
27+
worker();
28+
});
29+
let os_handle = thread.as_raw_handle();
30+
31+
// Allow the thread to start
32+
std::thread::sleep(std::time::Duration::from_millis(100));
33+
34+
let mut frames = Vec::new();
35+
unsafe {
36+
backtrace::trace_thread_unsynchronized(os_handle,|frame| {
37+
frames.push(BacktraceFrame::from(frame.clone()));
38+
true
39+
});
40+
}
41+
42+
let mut bt = Backtrace::from(frames);
43+
bt.resolve();
44+
println!("{:?}", bt);
45+
}

0 commit comments

Comments
 (0)