Skip to main content

kbpf_basic/
raw_tracepoint.rs

1//! Arguments for attaching BPF raw tracepoint programs.
2use alloc::string::String;
3
4use crate::{BpfError, KernelAuxiliaryOps, Result, linux_bpf::*};
5
6/// Arguments for attaching a BPF raw tracepoint program.
7#[derive(Debug)]
8pub struct BpfRawTracePointArg {
9    /// Name of the raw tracepoint.
10    pub name: String,
11    /// File descriptor of the BPF program.
12    pub prog_fd: u32,
13}
14
15impl BpfRawTracePointArg {
16    /// Try to create a `BpfRawTracePointArg` from a `bpf_attr` structure.
17    pub fn try_from_bpf_attr<F: KernelAuxiliaryOps>(attr: &bpf_attr) -> Result<Self> {
18        let (name_ptr, prog_fd) = unsafe {
19            let name_ptr = attr.raw_tracepoint.name as *const u8;
20
21            let prog_fd = attr.raw_tracepoint.prog_fd;
22            (name_ptr, prog_fd)
23        };
24        let name = F::string_from_user_cstr(name_ptr)?;
25        Ok(BpfRawTracePointArg { name, prog_fd })
26    }
27}