aya_ebpf/programs/
fentry.rs

1use core::ffi::c_void;
2
3use crate::{args::FromBtfArgument, EbpfContext};
4
5pub struct FEntryContext {
6    ctx: *mut c_void,
7}
8
9impl FEntryContext {
10    pub fn new(ctx: *mut c_void) -> FEntryContext {
11        FEntryContext { ctx }
12    }
13
14    /// Returns the `n`th argument to passed to the probe function, starting from 0.
15    ///
16    /// # Examples
17    ///
18    /// ```no_run
19    /// # #![allow(non_camel_case_types)]
20    /// # #![allow(dead_code)]
21    /// # use aya_ebpf::{cty::c_int, programs::FEntryContext};
22    /// # type pid_t = c_int;
23    /// # struct task_struct {
24    /// #     pid: pid_t,
25    /// # }
26    /// unsafe fn try_fentry_try_to_wake_up(ctx: FEntryContext) -> Result<u32, u32> {
27    ///     let tp: *const task_struct = ctx.arg(0);
28    ///
29    ///     // Do something with tp
30    ///
31    ///     Ok(0)
32    /// }
33    /// ```
34    pub unsafe fn arg<T: FromBtfArgument>(&self, n: usize) -> T {
35        T::from_argument(self.ctx as *const _, n)
36    }
37}
38
39impl EbpfContext for FEntryContext {
40    fn as_ptr(&self) -> *mut c_void {
41        self.ctx
42    }
43}