diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index a8a0d1ab..e921fbdb 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -9,8 +9,6 @@ use std::{ sync::atomic::{AtomicUsize, Ordering}, }; -use libc::pid_t; - use crate::{ programs::{ Link, ProgramData, ProgramError, kprobe::KProbeError, perf_attach, @@ -111,7 +109,7 @@ pub(crate) fn attach>( // separate argument. fn_name: &OsStr, offset: u64, - pid: Option, + pid: Option, cookie: Option, ) -> Result { // https://github.com/torvalds/linux/commit/e12f03d7031a977356e3d7b75a68c2185ff8d155 @@ -153,7 +151,7 @@ fn create_as_probe( kind: ProbeKind, fn_name: &OsStr, offset: u64, - pid: Option, + pid: Option, ) -> Result { use ProbeKind::*; @@ -188,7 +186,7 @@ fn create_as_trace_point( kind: ProbeKind, name: &OsStr, offset: u64, - pid: Option, + pid: Option, ) -> Result<(crate::MockableFd, OsString), ProgramError> { use ProbeKind::*; diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index 7edbae3e..4fbc83b9 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -115,7 +115,7 @@ pub(crate) fn read_sys_fs_trace_point_id( tracefs: &Path, category: &str, name: &Path, -) -> Result { +) -> Result { let filename = tracefs.join("events").join(category).join(name).join("id"); let id = match fs::read_to_string(&filename) { diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index d762f8a8..16899c35 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -11,7 +11,6 @@ use std::{ }; use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}; -use libc::pid_t; use object::{Object as _, ObjectSection as _, ObjectSymbol as _, Symbol}; use thiserror::Error; @@ -109,7 +108,7 @@ impl UProbe { &mut self, location: Loc, target: T, - pid: Option, + pid: Option, cookie: Option, ) -> Result { let proc_map = pid.map(ProcMap::new).transpose()?; @@ -159,9 +158,10 @@ where .and_then(|proc_map| { proc_map .find_library_path_by_name(target) - .map_err(|source| UProbeError::ProcMap { - pid: proc_map.pid, - source, + .map_err(|source| { + let ProcMap { pid, data: _ } = proc_map; + let pid = *pid; + UProbeError::ProcMap { pid, source } }) .transpose() }) @@ -190,7 +190,7 @@ where )] fn test_resolve_attach_path() { // Look up the current process's pid. - let pid = std::process::id().try_into().unwrap(); + let pid = std::process::id(); let proc_map = ProcMap::new(pid).unwrap(); // Now let's resolve the path to libc. It should exist in the current process's memory map and @@ -287,7 +287,7 @@ pub enum UProbeError { #[error("error fetching libs for {pid}")] ProcMap { /// The pid. - pid: i32, + pid: u32, /// The [`ProcMapError`] that caused the error. #[source] source: ProcMapError, @@ -419,12 +419,12 @@ impl<'a> ProcMapEntry<'a> { /// /// The information here may be used to resolve addresses to paths. struct ProcMap { - pid: pid_t, + pid: u32, data: T, } impl ProcMap> { - fn new(pid: pid_t) -> Result { + fn new(pid: u32) -> Result { let filename = PathBuf::from(format!("/proc/{pid}/maps")); let data = fs::read(&filename) .map_err(|io_error| UProbeError::FileError { filename, io_error })?; diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index 50fcabcb..c5592c83 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -133,7 +133,7 @@ pub(crate) fn perf_event_open_probe( ret_bit: Option, name: &OsStr, offset: u64, - pid: Option, + pid: Option, ) -> io::Result { use std::os::unix::ffi::OsStrExt as _; @@ -150,26 +150,30 @@ pub(crate) fn perf_event_open_probe( attr.__bindgen_anon_3.config1 = c_name.as_ptr() as u64; attr.__bindgen_anon_4.config2 = offset; - let cpu = if pid.is_some() { -1 } else { 0 }; - let pid = pid.unwrap_or(-1); + let (pid, cpu) = match pid { + Some(pid) => (pid as i32, -1), + None => (-1, 0), + }; perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) } pub(crate) fn perf_event_open_trace_point( - id: u32, - pid: Option, + event_id: u64, + pid: Option, ) -> io::Result { - let mut attr = unsafe { mem::zeroed::() }; - - attr.size = mem::size_of::() as u32; - attr.type_ = PERF_TYPE_TRACEPOINT as u32; - attr.config = u64::from(id); - - let cpu = if pid.is_some() { -1 } else { 0 }; - let pid = pid.unwrap_or(-1); - - perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) + let scope = match pid { + Some(pid) => PerfEventScope::OneProcess { pid, cpu: None }, + None => PerfEventScope::AllProcessesOneCpu { cpu: 0 }, + }; + perf_event_open( + PerfEventConfig::TracePoint { event_id }, + scope, + SamplePolicy::Period(0), + WakeupPolicy::Events(1), + false, + PERF_FLAG_FD_CLOEXEC, + ) } pub(crate) fn perf_event_ioctl( diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index da53c8d3..fce3d730 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -7374,7 +7374,7 @@ pub aya::programs::uprobe::UProbeError::InvalidLdSoCache::io_error: &'static std pub aya::programs::uprobe::UProbeError::InvalidTarget pub aya::programs::uprobe::UProbeError::InvalidTarget::path: std::path::PathBuf pub aya::programs::uprobe::UProbeError::ProcMap -pub aya::programs::uprobe::UProbeError::ProcMap::pid: i32 +pub aya::programs::uprobe::UProbeError::ProcMap::pid: u32 pub aya::programs::uprobe::UProbeError::ProcMap::source: aya::programs::uprobe::ProcMapError pub aya::programs::uprobe::UProbeError::SymbolError pub aya::programs::uprobe::UProbeError::SymbolError::error: alloc::boxed::Box<(dyn core::error::Error + core::marker::Send + core::marker::Sync)> @@ -7414,7 +7414,7 @@ pub fn aya::programs::uprobe::UProbeError::from(t: T) -> T pub struct aya::programs::uprobe::UProbe impl aya::programs::uprobe::UProbe pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType -pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef, Loc: core::convert::Into>>(&mut self, location: Loc, target: T, pid: core::option::Option, cookie: core::option::Option) -> core::result::Result +pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef, Loc: core::convert::Into>>(&mut self, location: Loc, target: T, pid: core::option::Option, cookie: core::option::Option) -> core::result::Result pub fn aya::programs::uprobe::UProbe::from_pin>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result pub fn aya::programs::uprobe::UProbe::kind(&self) -> aya::programs::ProbeKind pub fn aya::programs::uprobe::UProbe::load(&mut self) -> core::result::Result<(), aya::programs::ProgramError> @@ -8627,7 +8627,7 @@ pub aya::programs::UProbeError::InvalidLdSoCache::io_error: &'static std::io::er pub aya::programs::UProbeError::InvalidTarget pub aya::programs::UProbeError::InvalidTarget::path: std::path::PathBuf pub aya::programs::UProbeError::ProcMap -pub aya::programs::UProbeError::ProcMap::pid: i32 +pub aya::programs::UProbeError::ProcMap::pid: u32 pub aya::programs::UProbeError::ProcMap::source: aya::programs::uprobe::ProcMapError pub aya::programs::UProbeError::SymbolError pub aya::programs::UProbeError::SymbolError::error: alloc::boxed::Box<(dyn core::error::Error + core::marker::Send + core::marker::Sync)> @@ -10149,7 +10149,7 @@ pub fn aya::programs::trace_point::TracePoint::from(t: T) -> T pub struct aya::programs::UProbe impl aya::programs::uprobe::UProbe pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType -pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef, Loc: core::convert::Into>>(&mut self, location: Loc, target: T, pid: core::option::Option, cookie: core::option::Option) -> core::result::Result +pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef, Loc: core::convert::Into>>(&mut self, location: Loc, target: T, pid: core::option::Option, cookie: core::option::Option) -> core::result::Result pub fn aya::programs::uprobe::UProbe::from_pin>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result pub fn aya::programs::uprobe::UProbe::kind(&self) -> aya::programs::ProbeKind pub fn aya::programs::uprobe::UProbe::load(&mut self) -> core::result::Result<(), aya::programs::ProgramError>