make dump_registers method public (#367)

* make dump_registers method public

* be smart about getting the ucontext

* more docu
This commit is contained in:
Dominik Maier 2021-11-08 02:53:53 +01:00 committed by GitHub
parent a80126681e
commit 8ec8be1ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -1,12 +1,16 @@
//! Implements a mini-bsod generator //! Implements a mini-bsod generator
//! It dumps all important registers and prints a stacktrace.
//! You may use the [`crate::bolts::os::unix_signals::ucontext`]
//! function to get a [`ucontext_t`].
use libc::siginfo_t; use libc::siginfo_t;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use crate::bolts::os::unix_signals::{ucontext_t, Signal}; use crate::bolts::os::unix_signals::{ucontext_t, Signal};
/// Write the contens of all important registers
#[cfg(all(target_os = "linux", target_arch = "x86_64"))] #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
fn dump_registers<W: Write>( pub fn dump_registers<W: Write>(
writer: &mut BufWriter<W>, writer: &mut BufWriter<W>,
ucontext: &ucontext_t, ucontext: &ucontext_t,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
@ -39,11 +43,12 @@ fn dump_registers<W: Write>(
Ok(()) Ok(())
} }
/// Write the contens of all important registers
#[cfg(all( #[cfg(all(
any(target_os = "linux", target_os = "android"), any(target_os = "linux", target_os = "android"),
target_arch = "aarch64" target_arch = "aarch64"
))] ))]
fn dump_registers<W: Write>( pub fn dump_registers<W: Write>(
writer: &mut BufWriter<W>, writer: &mut BufWriter<W>,
ucontext: &ucontext_t, ucontext: &ucontext_t,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
@ -62,8 +67,9 @@ fn dump_registers<W: Write>(
Ok(()) Ok(())
} }
/// Write the contens of all important registers
#[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))]
fn dump_registers<W: Write>( pub fn dump_registers<W: Write>(
writer: &mut BufWriter<W>, writer: &mut BufWriter<W>,
ucontext: &ucontext_t, ucontext: &ucontext_t,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
@ -85,9 +91,10 @@ fn dump_registers<W: Write>(
Ok(()) Ok(())
} }
/// Write the contens of all important registers
#[allow(clippy::unnecessary_wraps, clippy::similar_names)] #[allow(clippy::unnecessary_wraps, clippy::similar_names)]
#[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))]
fn dump_registers<W: Write>( pub fn dump_registers<W: Write>(
writer: &mut BufWriter<W>, writer: &mut BufWriter<W>,
ucontext: &ucontext_t, ucontext: &ucontext_t,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {

View File

@ -3,9 +3,7 @@ use alloc::vec::Vec;
use core::{ use core::{
cell::UnsafeCell, cell::UnsafeCell,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
mem, mem, ptr,
mem::MaybeUninit,
ptr,
ptr::write_volatile, ptr::write_volatile,
sync::atomic::{compiler_fence, Ordering}, sync::atomic::{compiler_fence, Ordering},
}; };
@ -244,11 +242,15 @@ pub unsafe fn setup_signal_handler<T: 'static + Handler>(handler: &mut T) -> Res
/// Function to get the current [`ucontext_t`] for this process. /// Function to get the current [`ucontext_t`] for this process.
/// This calls the libc `getcontext` function under the hood. /// This calls the libc `getcontext` function under the hood.
/// It can be useful, for example for `dump_regs`.
/// Note that calling this method may, of course, alter the state.
/// We wrap it here, as it seems to be (currently) /// We wrap it here, as it seems to be (currently)
/// not available on `MacOS` in the `libc` crate. /// not available on `MacOS` in the `libc` crate.
#[cfg(unix)] #[cfg(unix)]
#[allow(clippy::inline_always)] // we assume that inlining will destroy less state
#[inline(always)]
pub fn ucontext() -> Result<ucontext_t, Error> { pub fn ucontext() -> Result<ucontext_t, Error> {
let mut ucontext = unsafe { MaybeUninit::zeroed().assume_init() }; let mut ucontext = unsafe { mem::zeroed() };
if unsafe { getcontext(&mut ucontext) } == 0 { if unsafe { getcontext(&mut ucontext) } == 0 {
Ok(ucontext) Ok(ucontext)
} else { } else {