aya_ebpf/programs/
retprobe.rs

1use core::ffi::c_void;
2
3#[cfg(any(
4    bpf_target_arch = "x86_64",
5    bpf_target_arch = "arm",
6    bpf_target_arch = "powerpc64"
7))]
8use crate::bindings::pt_regs;
9// aarch64 uses user_pt_regs instead of pt_regs
10#[cfg(any(bpf_target_arch = "aarch64", bpf_target_arch = "s390x"))]
11use crate::bindings::user_pt_regs as pt_regs;
12// riscv64 uses user_regs_struct instead of pt_regs
13#[cfg(bpf_target_arch = "riscv64")]
14use crate::bindings::user_regs_struct as pt_regs;
15use crate::{args::FromPtRegs, EbpfContext};
16
17pub struct RetProbeContext {
18    pub regs: *mut pt_regs,
19}
20
21impl RetProbeContext {
22    pub fn new(ctx: *mut c_void) -> RetProbeContext {
23        RetProbeContext {
24            regs: ctx as *mut pt_regs,
25        }
26    }
27
28    /// Returns the return value of the probed function.
29    ///
30    /// # Examples
31    ///
32    /// ```no_run
33    /// # #![allow(dead_code)]
34    /// # use aya_ebpf::{programs::RetProbeContext, cty::c_int};
35    /// unsafe fn try_kretprobe_try_to_wake_up(ctx: RetProbeContext) -> Result<u32, u32> {
36    ///     let retval: c_int = ctx.ret().ok_or(1u32)?;
37    ///
38    ///     // Do something with retval
39    ///
40    ///     Ok(0)
41    /// }
42    /// ```
43    pub fn ret<T: FromPtRegs>(&self) -> Option<T> {
44        T::from_retval(unsafe { &*self.regs })
45    }
46}
47
48impl EbpfContext for RetProbeContext {
49    fn as_ptr(&self) -> *mut c_void {
50        self.regs as *mut c_void
51    }
52}