-
Notifications
You must be signed in to change notification settings - Fork 262
Added tracing of threads other than the calling thread on Windows #473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb115a7
Added tracing of threads other than the calling thread
Jardynq 7f3474f
Moved SuspendThread and ResumeThread inside of trace_impl on windows
Jardynq f433ffd
Added example for capturing backtrace of non calling thread
Jardynq f7d4e6d
ci: disable fail-fast for GitHub Actions
aminya 68e1927
fix: add a separate trace_thread function
aminya c5163cd
fix: enable trace_thread_unsynchronized only on windows and noop
aminya 897795e
fix: use thread HANDLE in trace_thread
aminya cff4ef4
test: make the other_thread example noop on other platforms
aminya acfa2c0
fix: make CONTEXT_ALL cross-architecture
aminya 9f43721
fix: fix the GetThreadContext return type
aminya 0cd54c5
fix: remove noop trace_thread
aminya 97396dd
test: make other_thread example no-op on no-std
aminya fa964d1
docs: add the safety notes to the doc string
aminya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
use backtrace::{Backtrace, BacktraceFrame}; | ||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
use std::os::windows::prelude::AsRawHandle; | ||
|
||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
fn worker() { | ||
foo(); | ||
} | ||
|
||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
fn foo() { | ||
bar() | ||
} | ||
|
||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
fn bar() { | ||
baz() | ||
} | ||
|
||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
fn baz() { | ||
println!("Hello from thread!"); | ||
// Sleep for simple sync. Can't read thread that has finished running | ||
std::thread::sleep(std::time::Duration::from_millis(1000)); | ||
loop { | ||
print!(""); | ||
} | ||
} | ||
|
||
#[cfg(all(windows, not(target_vendor = "uwp"), feature = "std"))] | ||
fn main() { | ||
let thread = std::thread::spawn(|| { | ||
worker(); | ||
}); | ||
let os_handle = thread.as_raw_handle(); | ||
|
||
// Allow the thread to start | ||
std::thread::sleep(std::time::Duration::from_millis(100)); | ||
|
||
let mut frames = Vec::new(); | ||
unsafe { | ||
backtrace::trace_thread_unsynchronized(os_handle, |frame| { | ||
frames.push(BacktraceFrame::from(frame.clone())); | ||
true | ||
}); | ||
} | ||
|
||
let mut bt = Backtrace::from(frames); | ||
bt.resolve(); | ||
println!("{:?}", bt); | ||
} | ||
|
||
#[cfg(not(all(windows, not(target_vendor = "uwp"), feature = "std")))] | ||
fn main() { | ||
println!("This example is skipped on non-Windows or no-std platforms"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,6 +375,9 @@ ffi! { | |
pub fn GetCurrentProcess() -> HANDLE; | ||
pub fn GetCurrentThread() -> HANDLE; | ||
pub fn RtlCaptureContext(ContextRecord: PCONTEXT) -> (); | ||
pub fn GetThreadContext(ThreadHandle: HANDLE, ContextRecord: PCONTEXT) -> BOOL; | ||
pub fn SuspendThread(ThreadHandle: HANDLE) -> DWORD; | ||
pub fn ResumeThread(ThreadHandle: HANDLE) -> DWORD; | ||
pub fn LoadLibraryA(a: *const i8) -> HMODULE; | ||
pub fn GetProcAddress(h: HMODULE, name: *const i8) -> FARPROC; | ||
pub fn GetModuleHandleA(name: *const i8) -> HMODULE; | ||
|
@@ -518,6 +521,16 @@ ffi! { | |
pub struct ARM64_NT_NEON128 { | ||
pub D: [f64; 2], | ||
} | ||
|
||
pub const CONTEXT_ARM64: DWORD = 0x00400000; | ||
pub const CONTEXT_CONTROL: DWORD = CONTEXT_ARM64 | 0x00000001; | ||
pub const CONTEXT_INTEGER: DWORD = CONTEXT_ARM64 | 0x00000002; | ||
pub const CONTEXT_FLOATING_POINT: DWORD = CONTEXT_ARM64 | 0x00000004; | ||
pub const CONTEXT_DEBUG_REGISTERS: DWORD = CONTEXT_ARM64 | 0x00000008; | ||
pub const CONTEXT_X18: DWORD = CONTEXT_ARM64 | 0x00000010; | ||
pub const CONTEXT_FULL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT; | ||
pub const CONTEXT_ALL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT | ||
| CONTEXT_DEBUG_REGISTERS | CONTEXT_X18; | ||
} | ||
|
||
#[cfg(target_arch = "x86")] | ||
|
@@ -563,6 +576,18 @@ ffi! { | |
pub RegisterArea: [u8; 80], | ||
pub Spare0: DWORD, | ||
} | ||
|
||
pub const CONTEXT_i386: DWORD = 0x00010000; | ||
pub const CONTEXT_i486: DWORD = 0x00010000; | ||
pub const CONTEXT_CONTROL: DWORD = CONTEXT_i386 | 0x00000001; | ||
pub const CONTEXT_INTEGER: DWORD = CONTEXT_i386 | 0x00000002; | ||
pub const CONTEXT_SEGMENTS: DWORD = CONTEXT_i386 | 0x00000004; | ||
pub const CONTEXT_FLOATING_POINT: DWORD = CONTEXT_i386 | 0x00000008; | ||
pub const CONTEXT_DEBUG_REGISTERS: DWORD = CONTEXT_i386 | 0x00000010; | ||
pub const CONTEXT_EXTENDED_REGISTERS: DWORD = CONTEXT_i386 | 0x00000020; | ||
pub const CONTEXT_FULL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS; | ||
pub const CONTEXT_ALL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | ||
| CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | CONTEXT_EXTENDED_REGISTERS; | ||
} | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
|
@@ -630,6 +655,16 @@ ffi! { | |
pub Low: u64, | ||
pub High: i64, | ||
} | ||
|
||
pub const CONTEXT_AMD64: DWORD = 0x00100000; | ||
pub const CONTEXT_CONTROL: DWORD = CONTEXT_AMD64 | 0x00000001; | ||
pub const CONTEXT_INTEGER: DWORD = CONTEXT_AMD64 | 0x00000002; | ||
pub const CONTEXT_SEGMENTS: DWORD = CONTEXT_AMD64 | 0x00000004; | ||
pub const CONTEXT_FLOATING_POINT: DWORD = CONTEXT_AMD64 | 0x00000008; | ||
pub const CONTEXT_DEBUG_REGISTERS: DWORD = CONTEXT_AMD64 | 0x00000010; | ||
Comment on lines
+660
to
+664
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we avoid repeating the same 5 expressions for 3 different architectures? |
||
pub const CONTEXT_FULL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT; | ||
pub const CONTEXT_ALL: DWORD = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | ||
| CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS; | ||
} | ||
|
||
#[repr(C)] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO would have to be addressed.