bpf: Add PtRegs wrapper

This adds a portable wrapper around pt_regs and user_pt_regs.
It makes writing Raw Tracepoint or KProbe programs easier when the
arguments are one of these types while also ensuring code is portable
across architectures

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/258/head
Dave Tucker 3 years ago
parent 2fca4aee4e
commit 67623fda64

@ -56,6 +56,26 @@ unsafe_impl_from_btf_argument!(i64);
unsafe_impl_from_btf_argument!(usize);
unsafe_impl_from_btf_argument!(isize);
pub struct PtRegs {
regs: *mut pt_regs,
}
/// PtRegs is a portable wrapper around
/// pt_regs and user_pt_regs
impl PtRegs {
pub fn new(ctx: *mut c_void) -> Self {
PtRegs {
regs: ctx as *mut _,
}
}
/// Returns the `n`th argument to passed to the probe function, starting from 0.
pub fn arg<T: FromPtRegs>(&self, n: usize) -> Option<T> {
T::from_argument( unsafe { &*self.regs } , n)
}
}
/// A trait that indicates a valid type for an argument which can be coerced from
/// a pt_regs context.
///

@ -5,6 +5,7 @@
pub use aya_bpf_bindings::bindings;
mod args;
pub use args::PtRegs;
pub mod helpers;
pub mod maps;
pub mod programs;

Loading…
Cancel
Save