Skip to content

Port to windows-sys #494

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- run: cargo test
- run: cargo test --features "serialize-rustc"
- run: cargo test --features "serialize-serde"
- run: cargo test --features "verify-winapi"
- run: cargo test --features "verify-windows-sys"
- run: cargo test --features "cpp_demangle"
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features "std"
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
shell: bash
- run: rustup target add aarch64-pc-windows-msvc
- run: cargo test --no-run --target aarch64-pc-windows-msvc
- run: cargo test --no-run --target aarch64-pc-windows-msvc --features verify-winapi
- run: cargo test --no-run --target aarch64-pc-windows-msvc --features verify-windows-sys

ios:
name: iOS
Expand Down
27 changes: 13 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,18 @@ version = "0.29.0"
default-features = false
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive']

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", optional = true }
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.42.0"
features = [
"Win32_Foundation",
"Win32_Security",
"Win32_System_Diagnostics_Debug",
"Win32_System_Kernel",
"Win32_System_LibraryLoader",
"Win32_System_SystemInformation",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
]

[build-dependencies]
# Only needed for Android, but cannot be target dependent
Expand Down Expand Up @@ -87,18 +97,7 @@ kernel32 = []
libbacktrace = []
libunwind = []
unix-backtrace = []
verify-winapi = [
'winapi/dbghelp',
'winapi/handleapi',
'winapi/libloaderapi',
'winapi/memoryapi',
'winapi/minwindef',
'winapi/processthreadsapi',
'winapi/synchapi',
'winapi/tlhelp32',
'winapi/winbase',
'winapi/winnt',
]
verify-windows-sys = []

[[example]]
name = "backtrace"
Expand Down
35 changes: 20 additions & 15 deletions src/backtrace/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

#![allow(bad_style)]

use super::super::{dbghelp, windows::*};
use windows_sys::{
Win32::Foundation::*, Win32::System::Diagnostics::Debug::*,
Win32::System::SystemInformation::*, Win32::System::Threading::*,
};

use super::super::dbghelp;
use core::ffi::c_void;
use core::mem;

Expand Down Expand Up @@ -115,12 +120,12 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
if #[cfg(target_pointer_width = "64")] {
use core::ptr;

unsafe extern "system" fn function_table_access(_process: HANDLE, addr: DWORD64) -> PVOID {
unsafe extern "system" fn function_table_access(_process: HANDLE, addr: u64) -> *mut c_void {
let mut base = 0;
RtlLookupFunctionEntry(addr, &mut base, ptr::null_mut()).cast()
}

unsafe extern "system" fn get_module_base(_process: HANDLE, addr: DWORD64) -> DWORD64 {
unsafe extern "system" fn get_module_base(_process: HANDLE, addr: u64) -> u64 {
let mut base = 0;
RtlLookupFunctionEntry(addr, &mut base, ptr::null_mut());
base
Expand All @@ -138,7 +143,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
match (*dbghelp.dbghelp()).StackWalkEx() {
Some(StackWalkEx) => {
let mut inner: STACKFRAME_EX = mem::zeroed();
inner.StackFrameSize = mem::size_of::<STACKFRAME_EX>() as DWORD;
inner.StackFrameSize = mem::size_of::<STACKFRAME_EX>() as u32;
let mut frame = super::Frame {
inner: Frame {
stack_frame: StackFrame::New(inner),
Expand All @@ -152,7 +157,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
};

while StackWalkEx(
image as DWORD,
image as u32,
process,
thread,
frame_ptr,
Expand All @@ -162,7 +167,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
Some(get_module_base),
None,
0,
) == TRUE
) == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to still see a name for meaningful "magic numbers" like this one, simply because I am going to ask everyone else to also name magic numbers and sentinel values for their OS.

{
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;

Expand All @@ -185,7 +190,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
};

while dbghelp.StackWalk64()(
image as DWORD,
image as _,
process,
thread,
frame_ptr,
Expand All @@ -194,7 +199,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
Some(function_table_access),
Some(get_module_base),
None,
) == TRUE
) == 1
{
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;

Expand All @@ -207,19 +212,19 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
}

#[cfg(target_arch = "x86_64")]
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
frame.addr_pc_mut().Offset = ctx.Rip as u64;
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> u16 {
frame.addr_pc_mut().Offset = ctx.Rip;
frame.addr_pc_mut().Mode = AddrModeFlat;
frame.addr_stack_mut().Offset = ctx.Rsp as u64;
frame.addr_stack_mut().Offset = ctx.Rsp;
frame.addr_stack_mut().Mode = AddrModeFlat;
frame.addr_frame_mut().Offset = ctx.Rbp as u64;
frame.addr_frame_mut().Offset = ctx.Rbp;
frame.addr_frame_mut().Mode = AddrModeFlat;

IMAGE_FILE_MACHINE_AMD64
}

#[cfg(target_arch = "x86")]
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> u16 {
frame.addr_pc_mut().Offset = ctx.Eip as u64;
frame.addr_pc_mut().Mode = AddrModeFlat;
frame.addr_stack_mut().Offset = ctx.Esp as u64;
Expand All @@ -231,7 +236,7 @@ fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
}

#[cfg(target_arch = "aarch64")]
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> u16 {
frame.addr_pc_mut().Offset = ctx.Pc as u64;
frame.addr_pc_mut().Mode = AddrModeFlat;
frame.addr_stack_mut().Offset = ctx.Sp as u64;
Expand All @@ -244,7 +249,7 @@ fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
}

#[cfg(target_arch = "arm")]
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> WORD {
fn init_frame(frame: &mut Frame, ctx: &CONTEXT) -> u16 {
frame.addr_pc_mut().Offset = ctx.Pc as u64;
frame.addr_pc_mut().Mode = AddrModeFlat;
frame.addr_stack_mut().Offset = ctx.Sp as u64;
Expand Down
Loading