@ -6,6 +6,7 @@ pub use crate::generated::{
use crate ::{
use crate ::{
generated ::{
generated ::{
bpf_link_type ,
bpf_prog_type ::BPF_PROG_TYPE_PERF_EVENT ,
bpf_prog_type ::BPF_PROG_TYPE_PERF_EVENT ,
perf_type_id ::{
perf_type_id ::{
PERF_TYPE_BREAKPOINT , PERF_TYPE_HARDWARE , PERF_TYPE_HW_CACHE , PERF_TYPE_RAW ,
PERF_TYPE_BREAKPOINT , PERF_TYPE_HARDWARE , PERF_TYPE_HW_CACHE , PERF_TYPE_RAW ,
@ -16,9 +17,9 @@ use crate::{
links ::define_link_wrapper ,
links ::define_link_wrapper ,
load_program , perf_attach ,
load_program , perf_attach ,
perf_attach ::{ PerfLinkIdInner , PerfLinkInner } ,
perf_attach ::{ PerfLinkIdInner , PerfLinkInner } ,
ProgramData, ProgramError ,
FdLink, LinkError , ProgramData, ProgramError ,
} ,
} ,
sys ::perf_event_open ,
sys ::{ bpf_link_get_info_by_fd , perf_event_open } ,
} ;
} ;
/// The type of perf event
/// The type of perf event
@ -189,6 +190,35 @@ impl PerfEvent {
}
}
}
}
impl TryFrom < PerfEventLink > for FdLink {
type Error = LinkError ;
fn try_from ( value : PerfEventLink ) -> Result < Self , Self ::Error > {
if let PerfLinkInner ::FdLink ( fd ) = value . into_inner ( ) {
Ok ( fd )
} else {
Err ( LinkError ::InvalidLink )
}
}
}
impl TryFrom < FdLink > for PerfEventLink {
type Error = LinkError ;
fn try_from ( fd_link : FdLink ) -> Result < Self , Self ::Error > {
let info =
bpf_link_get_info_by_fd ( fd_link . fd ) . map_err ( | io_error | LinkError ::SyscallError {
call : "BPF_OBJ_GET_INFO_BY_FD" ,
code : 0 ,
io_error ,
} ) ? ;
if info . type_ = = ( bpf_link_type ::BPF_LINK_TYPE_PERF_EVENT as u32 ) {
return Ok ( PerfEventLink ::new ( PerfLinkInner ::FdLink ( fd_link ) ) ) ;
}
Err ( LinkError ::InvalidLink )
}
}
define_link_wrapper ! (
define_link_wrapper ! (
/// The link used by [PerfEvent] programs.
/// The link used by [PerfEvent] programs.
PerfEventLink ,
PerfEventLink ,