hooking 0.8.0

cross platform detour / hooking library in rust
Documentation
//! `#[mini_hook]`: like `#[hook]`, but creates a `MiniHook`, which has no
//! trampoline. The target jumps straight to the annotated function, which
//! can still call the original through the generated `original_function`.
//!
//! `std::process::id` calls libc's `getpid`, so Rust code sees the hook too.

#[cfg(target_os = "linux")]
mod example {
    use hooking::mini_hook;

    /// Reports the real pid plus a million.
    ///
    /// `#[mini_hook]` copies these types into a generated module, so they
    /// are written as full paths.
    #[mini_hook(method = "getpid")]
    extern "C" fn offset_getpid() -> std::ffi::c_int {
        let original = unsafe { offset_getpid::original_function().expect("the hook was created") };
        original() + 1_000_000
    }

    pub fn main() {
        println!("before hooking: pid {}", std::process::id());

        unsafe { offset_getpid::enable_hook().unwrap() };
        println!("hooked: pid {}", std::process::id());

        unsafe { offset_getpid::disable_hook().unwrap() };
        println!("unhooked: pid {}", std::process::id());
    }
}

#[cfg(target_os = "linux")]
fn main() {
    example::main();
}

#[cfg(not(target_os = "linux"))]
fn main() {
    println!("this example needs linux");
}