1fae6e9adSlinfeng use crate::include::bindings::linux_bpf::{ 2fae6e9adSlinfeng perf_event_attr, perf_event_header, perf_event_sample_format, perf_sw_ids, perf_type_id, 3fae6e9adSlinfeng }; 4fae6e9adSlinfeng use crate::syscall::user_access::check_and_clone_cstr; 5fae6e9adSlinfeng use alloc::string::String; 6fae6e9adSlinfeng use num_traits::FromPrimitive; 7fae6e9adSlinfeng use system_error::SystemError; 8fae6e9adSlinfeng 9fae6e9adSlinfeng bitflags! { 10fae6e9adSlinfeng pub struct PerfEventOpenFlags: u32 { 11fae6e9adSlinfeng const PERF_FLAG_FD_NO_GROUP = 1; 12fae6e9adSlinfeng const PERF_FLAG_FD_OUTPUT = 2; 13fae6e9adSlinfeng const PERF_FLAG_PID_CGROUP = 4; 14fae6e9adSlinfeng const PERF_FLAG_FD_CLOEXEC = 8; 15fae6e9adSlinfeng } 16fae6e9adSlinfeng } 17fae6e9adSlinfeng 18fae6e9adSlinfeng /// The `PerfEventIoc` enum is used to define the ioctl commands for perf events. 19fae6e9adSlinfeng /// 20fae6e9adSlinfeng /// See https://elixir.bootlin.com/linux/v6.1/source/include/uapi/linux/perf_event.h#L544 21fae6e9adSlinfeng #[repr(u32)] 22fae6e9adSlinfeng #[derive(Debug, Copy, Clone, FromPrimitive)] 23fae6e9adSlinfeng pub enum PerfEventIoc { 24fae6e9adSlinfeng /// Equivalent to [crate::include::bindings::linux_bpf::AYA_PERF_EVENT_IOC_ENABLE]. 25fae6e9adSlinfeng Enable = 9216, 26fae6e9adSlinfeng /// Equivalent to [crate::include::bindings::linux_bpf::AYA_PERF_EVENT_IOC_DISABLE]. 27fae6e9adSlinfeng Disable = 9217, 28fae6e9adSlinfeng /// Equivalent to [crate::include::bindings::linux_bpf::AYA_PERF_EVENT_IOC_SET_BPF]. 29fae6e9adSlinfeng SetBpf = 1074013192, 30fae6e9adSlinfeng } 31fae6e9adSlinfeng 32fae6e9adSlinfeng #[derive(Debug, Clone)] 33fae6e9adSlinfeng #[allow(unused)] 34fae6e9adSlinfeng /// `perf_event_open` syscall arguments. 35fae6e9adSlinfeng pub struct PerfProbeArgs { 36fae6e9adSlinfeng pub config: perf_sw_ids, 37fae6e9adSlinfeng pub name: String, 38fae6e9adSlinfeng pub offset: u64, 39fae6e9adSlinfeng pub size: u32, 40fae6e9adSlinfeng pub type_: perf_type_id, 41fae6e9adSlinfeng pub pid: i32, 42fae6e9adSlinfeng pub cpu: i32, 43fae6e9adSlinfeng pub group_fd: i32, 44fae6e9adSlinfeng pub flags: PerfEventOpenFlags, 45fae6e9adSlinfeng pub sample_type: Option<perf_event_sample_format>, 46fae6e9adSlinfeng } 47fae6e9adSlinfeng 48fae6e9adSlinfeng impl PerfProbeArgs { try_from( attr: &perf_event_attr, pid: i32, cpu: i32, group_fd: i32, flags: u32, ) -> Result<Self, SystemError>49fae6e9adSlinfeng pub fn try_from( 50fae6e9adSlinfeng attr: &perf_event_attr, 51fae6e9adSlinfeng pid: i32, 52fae6e9adSlinfeng cpu: i32, 53fae6e9adSlinfeng group_fd: i32, 54fae6e9adSlinfeng flags: u32, 55fae6e9adSlinfeng ) -> Result<Self, SystemError> { 56fae6e9adSlinfeng let ty = perf_type_id::from_u32(attr.type_).ok_or(SystemError::EINVAL)?; 57fae6e9adSlinfeng let config = perf_sw_ids::from_u32(attr.config as u32).ok_or(SystemError::EINVAL)?; 58fae6e9adSlinfeng let name = if ty == perf_type_id::PERF_TYPE_MAX { 59fae6e9adSlinfeng let name_ptr = unsafe { attr.__bindgen_anon_3.config1 } as *const u8; 60fae6e9adSlinfeng let name = check_and_clone_cstr(name_ptr, None)?; 61fae6e9adSlinfeng name.into_string().map_err(|_| SystemError::EINVAL)? 62fae6e9adSlinfeng } else { 63fae6e9adSlinfeng String::new() 64fae6e9adSlinfeng }; 65fae6e9adSlinfeng let sample_ty = perf_event_sample_format::from_u32(attr.sample_type as u32); 66fae6e9adSlinfeng let args = PerfProbeArgs { 67fae6e9adSlinfeng config, 68fae6e9adSlinfeng name, 69fae6e9adSlinfeng offset: unsafe { attr.__bindgen_anon_4.config2 }, 70fae6e9adSlinfeng size: attr.size, 71fae6e9adSlinfeng type_: ty, 72fae6e9adSlinfeng pid, 73fae6e9adSlinfeng cpu, 74fae6e9adSlinfeng group_fd, 75fae6e9adSlinfeng flags: PerfEventOpenFlags::from_bits_truncate(flags), 76fae6e9adSlinfeng sample_type: sample_ty, 77fae6e9adSlinfeng }; 78fae6e9adSlinfeng Ok(args) 79fae6e9adSlinfeng } 80fae6e9adSlinfeng } 81fae6e9adSlinfeng 82fae6e9adSlinfeng /// The event type in our particular use case will be `PERF_RECORD_SAMPLE` or `PERF_RECORD_LOST`. 83fae6e9adSlinfeng /// `PERF_RECORD_SAMPLE` indicating that there is an actual sample after this header. 84fae6e9adSlinfeng /// And `PERF_RECORD_LOST` indicating that there is a record lost header following the perf event header. 85fae6e9adSlinfeng #[repr(C)] 86fae6e9adSlinfeng #[derive(Debug)] 87fae6e9adSlinfeng pub struct LostSamples { 88fae6e9adSlinfeng pub header: perf_event_header, 89fae6e9adSlinfeng pub id: u64, 90fae6e9adSlinfeng pub count: u64, 91fae6e9adSlinfeng } 92fae6e9adSlinfeng 93fae6e9adSlinfeng impl LostSamples { as_bytes(&self) -> &[u8]94fae6e9adSlinfeng pub fn as_bytes(&self) -> &[u8] { 95fae6e9adSlinfeng unsafe { core::slice::from_raw_parts(self as *const Self as *const u8, size_of::<Self>()) } 96fae6e9adSlinfeng } 97fae6e9adSlinfeng } 98fae6e9adSlinfeng 99fae6e9adSlinfeng #[repr(C)] 100fae6e9adSlinfeng #[derive(Debug)] 101fae6e9adSlinfeng pub struct SampleHeader { 102fae6e9adSlinfeng pub header: perf_event_header, 103fae6e9adSlinfeng pub size: u32, 104fae6e9adSlinfeng } 105fae6e9adSlinfeng 106fae6e9adSlinfeng impl SampleHeader { as_bytes(&self) -> &[u8]107fae6e9adSlinfeng pub fn as_bytes(&self) -> &[u8] { 108fae6e9adSlinfeng unsafe { core::slice::from_raw_parts(self as *const Self as *const u8, size_of::<Self>()) } 109fae6e9adSlinfeng } 110fae6e9adSlinfeng } 111fae6e9adSlinfeng 112fae6e9adSlinfeng #[repr(C)] 113fae6e9adSlinfeng #[derive(Debug)] 114fae6e9adSlinfeng pub struct PerfSample<'a> { 115fae6e9adSlinfeng pub s_hdr: SampleHeader, 116fae6e9adSlinfeng pub value: &'a [u8], 117fae6e9adSlinfeng } 118fae6e9adSlinfeng 119*7c28051eSlinfeng impl PerfSample<'_> { calculate_size(value_size: usize) -> usize120fae6e9adSlinfeng pub fn calculate_size(value_size: usize) -> usize { 121fae6e9adSlinfeng size_of::<SampleHeader>() + value_size 122fae6e9adSlinfeng } 123fae6e9adSlinfeng } 124