From 24500fba44633c3a2569d1ca41a7ce5198acb888 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Thu, 23 Jan 2025 05:04:09 +0000 Subject: [PATCH 1/4] Move dbghelp64.rs to win64.rs Windows 64-bit doesn't use dbghelp for tracing, so the name was confusing. --- src/backtrace/mod.rs | 10 +++++----- src/backtrace/{dbghelp64.rs => win64.rs} | 5 ----- 2 files changed, 5 insertions(+), 10 deletions(-) rename src/backtrace/{dbghelp64.rs => win64.rs} (97%) diff --git a/src/backtrace/mod.rs b/src/backtrace/mod.rs index 75925d97..22cebc95 100644 --- a/src/backtrace/mod.rs +++ b/src/backtrace/mod.rs @@ -189,15 +189,15 @@ cfg_if::cfg_if! { } else if #[cfg(all(windows, not(target_vendor = "uwp")))] { cfg_if::cfg_if! { if #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm64ec"))] { - mod dbghelp64; - use dbghelp64 as dbghelp; + mod win64; + use self::win64::trace as trace_imp; + pub(crate) use self::win64::Frame as FrameImp; } else if #[cfg(any(target_arch = "x86", target_arch = "arm"))] { mod dbghelp32; - use dbghelp32 as dbghelp; + use self::dbghelp32::trace as trace_imp; + pub(crate) use self::dbghelp32::Frame as FrameImp; } } - use self::dbghelp::trace as trace_imp; - pub(crate) use self::dbghelp::Frame as FrameImp; } else { mod noop; use self::noop::trace as trace_imp; diff --git a/src/backtrace/dbghelp64.rs b/src/backtrace/win64.rs similarity index 97% rename from src/backtrace/dbghelp64.rs rename to src/backtrace/win64.rs index fd31388c..a338526c 100644 --- a/src/backtrace/dbghelp64.rs +++ b/src/backtrace/win64.rs @@ -77,11 +77,6 @@ impl MyContext { } } -#[cfg(any( - target_arch = "x86_64", - target_arch = "aarch64", - target_arch = "arm64ec" -))] #[inline(always)] pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { use core::ptr; From c7d957cad1c52f6ef844c8673374e982b1dfdb3d Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Thu, 23 Jan 2025 05:04:48 +0000 Subject: [PATCH 2/4] Move the note about 64-bit tracing from dbghelp32 --- src/backtrace/dbghelp32.rs | 8 -------- src/backtrace/win64.rs | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/backtrace/dbghelp32.rs b/src/backtrace/dbghelp32.rs index cc71edff..fa564804 100644 --- a/src/backtrace/dbghelp32.rs +++ b/src/backtrace/dbghelp32.rs @@ -111,14 +111,6 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { Err(()) => return, // oh well... }; - // On x86_64 and ARM64 we opt to not use the default `Sym*` functions from - // dbghelp for getting the function table and module base. Instead we use - // the `RtlLookupFunctionEntry` function in kernel32 which will account for - // JIT compiler frames as well. These should be equivalent, but using - // `Rtl*` allows us to backtrace through JIT frames. - // - // Note that `RtlLookupFunctionEntry` only works for in-process backtraces, - // but that's all we support anyway, so it all lines up well. let function_table_access = dbghelp.SymFunctionTableAccess64(); let get_module_base = dbghelp.SymGetModuleBase64(); diff --git a/src/backtrace/win64.rs b/src/backtrace/win64.rs index a338526c..ab74dd55 100644 --- a/src/backtrace/win64.rs +++ b/src/backtrace/win64.rs @@ -91,6 +91,10 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { // The base address of the module containing the function will be stored here // when RtlLookupFunctionEntry returns successfully. let mut base = 0; + // We use the `RtlLookupFunctionEntry` function in kernel32 which allows + // us to backtrace through JIT frames. + // Note that `RtlLookupFunctionEntry` only works for in-process backtraces, + // but that's all we support anyway, so it all lines up well. let fn_entry = RtlLookupFunctionEntry(ip, &mut base, ptr::null_mut()); if fn_entry.is_null() { // No function entry could be found - this may indicate a corrupt From fba809da107e69efdb43f693056ae887aacc7ccf Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Thu, 23 Jan 2025 05:05:40 +0000 Subject: [PATCH 3/4] Less bad style --- src/backtrace/dbghelp32.rs | 3 +-- src/backtrace/win64.rs | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/backtrace/dbghelp32.rs b/src/backtrace/dbghelp32.rs index fa564804..9c459306 100644 --- a/src/backtrace/dbghelp32.rs +++ b/src/backtrace/dbghelp32.rs @@ -9,8 +9,6 @@ //! Note that all dbghelp support is loaded dynamically, see `src/dbghelp.rs` //! for more information about that. -#![allow(bad_style)] - use super::super::{dbghelp, windows_sys::*}; use core::ffi::c_void; use core::mem; @@ -119,6 +117,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { // Attempt to use `StackWalkEx` if we can, but fall back to `StackWalk64` // since it's in theory supported on more systems. match (*dbghelp.dbghelp()).StackWalkEx() { + #[allow(non_snake_case)] Some(StackWalkEx) => { let mut inner: STACKFRAME_EX = mem::zeroed(); inner.StackFrameSize = mem::size_of::() as u32; diff --git a/src/backtrace/win64.rs b/src/backtrace/win64.rs index ab74dd55..f35754fd 100644 --- a/src/backtrace/win64.rs +++ b/src/backtrace/win64.rs @@ -6,8 +6,6 @@ //! We still report inlined frames during symbolization by consulting the appropriate //! `dbghelp` functions. -#![allow(bad_style)] - use super::super::windows_sys::*; use core::ffi::c_void; From b1bc2e8cd85b2218de95f5d59cd1f38fe8634321 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Mon, 27 Jan 2025 01:37:13 +0000 Subject: [PATCH 4/4] Rename dbghelp32.rs to win32.rs --- src/backtrace/mod.rs | 6 +++--- src/backtrace/{dbghelp32.rs => win32.rs} | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename src/backtrace/{dbghelp32.rs => win32.rs} (100%) diff --git a/src/backtrace/mod.rs b/src/backtrace/mod.rs index 22cebc95..d0c35645 100644 --- a/src/backtrace/mod.rs +++ b/src/backtrace/mod.rs @@ -193,9 +193,9 @@ cfg_if::cfg_if! { use self::win64::trace as trace_imp; pub(crate) use self::win64::Frame as FrameImp; } else if #[cfg(any(target_arch = "x86", target_arch = "arm"))] { - mod dbghelp32; - use self::dbghelp32::trace as trace_imp; - pub(crate) use self::dbghelp32::Frame as FrameImp; + mod win32; + use self::win32::trace as trace_imp; + pub(crate) use self::win32::Frame as FrameImp; } } } else { diff --git a/src/backtrace/dbghelp32.rs b/src/backtrace/win32.rs similarity index 100% rename from src/backtrace/dbghelp32.rs rename to src/backtrace/win32.rs