bpf: add support for tracepoint program (#29)

This patch add initial support for tracepoint program type.
Hope you enjoy.

Signed-off-by: Tw <wei.tan@intel.com>
pull/31/head
Tw 3 years ago committed by GitHub
parent d996a88de4
commit 55ba0538f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -293,3 +293,32 @@ impl std::fmt::Display for ProbeKind {
} }
} }
} }
pub struct TracePoint {
item: ItemFn,
name: String,
}
impl TracePoint {
pub fn from_syn(mut args: Args, item: ItemFn) -> Result<TracePoint> {
let name = name_arg(&mut args)?.unwrap_or_else(|| item.sig.ident.to_string());
Ok(TracePoint { item, name })
}
pub fn expand(&self) -> Result<TokenStream> {
let section_name = format!("tp/{}", self.name);
let fn_name = &self.item.sig.ident;
let item = &self.item;
Ok(quote! {
#[no_mangle]
#[link_section = #section_name]
fn #fn_name(ctx: *mut ::core::ffi::c_void) -> u32 {
let _ = #fn_name(::aya_bpf::programs::TracePointContext::new(ctx));
return 0;
#item
}
})
}
}

@ -1,6 +1,6 @@
mod expand; mod expand;
use expand::{Args, Map, Probe, ProbeKind, SchedClassifier, SkMsg, SockOps, Xdp}; use expand::{Args, Map, Probe, ProbeKind, SchedClassifier, SkMsg, SockOps, TracePoint, Xdp};
use proc_macro::TokenStream; use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn, ItemStatic}; use syn::{parse_macro_input, ItemFn, ItemStatic};
@ -101,3 +101,14 @@ fn probe(kind: ProbeKind, attrs: TokenStream, item: TokenStream) -> TokenStream
.unwrap_or_else(|err| err.to_compile_error()) .unwrap_or_else(|err| err.to_compile_error())
.into() .into()
} }
#[proc_macro_attribute]
pub fn tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(attrs as Args);
let item = parse_macro_input!(item as ItemFn);
TracePoint::from_syn(args, item)
.and_then(|u| u.expand())
.unwrap_or_else(|err| err.to_compile_error())
.into()
}

@ -4,7 +4,7 @@ use aya_bpf_cty::{c_long, c_void};
use crate::{ use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH}, bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH},
helpers::{bpf_map_lookup_elem, bpf_map_update_elem, bpf_map_delete_elem}, helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
}; };
#[repr(transparent)] #[repr(transparent)]

@ -2,10 +2,12 @@ pub mod probe;
pub mod sk_msg; pub mod sk_msg;
pub mod sk_skb; pub mod sk_skb;
pub mod sock_ops; pub mod sock_ops;
pub mod tracepoint;
pub mod xdp; pub mod xdp;
pub use probe::ProbeContext; pub use probe::ProbeContext;
pub use sk_msg::SkMsgContext; pub use sk_msg::SkMsgContext;
pub use sk_skb::SkSkbContext; pub use sk_skb::SkSkbContext;
pub use sock_ops::SockOpsContext; pub use sock_ops::SockOpsContext;
pub use tracepoint::TracePointContext;
pub use xdp::XdpContext; pub use xdp::XdpContext;

@ -0,0 +1,22 @@
use crate::{helpers::bpf_probe_read, BpfContext};
use core::ffi::c_void;
pub struct TracePointContext {
ctx: *mut c_void,
}
impl TracePointContext {
pub fn new(ctx: *mut c_void) -> TracePointContext {
TracePointContext { ctx }
}
pub unsafe fn read_at<T>(&self, offset: usize) -> Result<T, i64> {
bpf_probe_read(self.ctx.add(offset) as *const T)
}
}
impl BpfContext for TracePointContext {
fn as_ptr(&self) -> *mut c_void {
self.ctx
}
}
Loading…
Cancel
Save