From 1ccfdbc17577a5f132ba0af2eb9b754e6e19ddca Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 24 Aug 2023 11:17:52 -0400 Subject: [PATCH] aya: support non-UTF8 probing Fixes #751. --- aya/src/programs/kprobe.rs | 9 +++++++-- aya/src/programs/probe.rs | 16 ++++++++++------ aya/src/programs/uprobe.rs | 3 ++- aya/src/sys/perf_event.rs | 7 +++---- xtask/public-api/aya.txt | 4 ++-- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/aya/src/programs/kprobe.rs b/aya/src/programs/kprobe.rs index ef34a3f4..c915136e 100644 --- a/aya/src/programs/kprobe.rs +++ b/aya/src/programs/kprobe.rs @@ -1,5 +1,6 @@ //! Kernel space probes. use std::{ + ffi::OsStr, io, os::fd::AsFd as _, path::{Path, PathBuf}, @@ -71,8 +72,12 @@ impl KProbe { /// target function. /// /// The returned value can be used to detach from the given function, see [KProbe::detach]. - pub fn attach(&mut self, fn_name: &str, offset: u64) -> Result { - attach(&mut self.data, self.kind, Path::new(fn_name), offset, None) + pub fn attach>( + &mut self, + fn_name: T, + offset: u64, + ) -> Result { + attach(&mut self.data, self.kind, fn_name.as_ref(), offset, None) } /// Detaches the program. diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index ea0d59b5..f8b53bae 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -101,7 +101,13 @@ pub(crate) struct ProbeEvent { pub(crate) fn attach>( program_data: &mut ProgramData, kind: ProbeKind, - fn_name: &Path, + // NB: the meaning of this argument is different for kprobe/kretprobe and uprobe/uretprobe; in + // the kprobe case it is the name of the function to attach to, in the uprobe case it is a path + // to the binary or library. + // + // TODO: consider encoding the type and the argument in the [`ProbeKind`] enum instead of a + // separate argument. + fn_name: &OsStr, offset: u64, pid: Option, ) -> Result { @@ -140,7 +146,7 @@ pub(crate) fn detach_debug_fs(event: ProbeEvent) -> Result<(), ProgramError> { fn create_as_probe( kind: ProbeKind, - fn_name: &Path, + fn_name: &OsStr, offset: u64, pid: Option, ) -> Result { @@ -176,7 +182,7 @@ fn create_as_probe( fn create_as_trace_point( kind: ProbeKind, - name: &Path, + name: &OsStr, offset: u64, pid: Option, ) -> Result<(OwnedFd, OsString), ProgramError> { @@ -204,7 +210,7 @@ fn create_as_trace_point( fn create_probe_event( tracefs: &Path, kind: ProbeKind, - fn_name: &Path, + fn_name: &OsStr, offset: u64, ) -> Result { use std::os::unix::ffi::OsStrExt as _; @@ -216,8 +222,6 @@ fn create_probe_event( KRetProbe | URetProbe => 'r', }; - let fn_name = fn_name.as_os_str(); - let mut event_alias = OsString::new(); write!( &mut event_alias, diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index 00d306ad..d95f6b2c 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -94,7 +94,8 @@ impl UProbe { 0 }; - attach(&mut self.data, self.kind, &path, sym_offset + offset, pid) + let fn_name = path.as_os_str(); + attach(&mut self.data, self.kind, fn_name, sym_offset + offset, pid) } /// Detaches the program. diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index 000fecbc..1659778a 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -1,8 +1,7 @@ use std::{ - ffi::{c_long, CString}, + ffi::{c_long, CString, OsStr}, io, mem, os::fd::{BorrowedFd, FromRawFd as _, OwnedFd}, - path::Path, }; use libc::{c_int, pid_t}; @@ -63,7 +62,7 @@ pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult { pub(crate) fn perf_event_open_probe( ty: u32, ret_bit: Option, - name: &Path, + name: &OsStr, offset: u64, pid: Option, ) -> SysResult { @@ -75,7 +74,7 @@ pub(crate) fn perf_event_open_probe( attr.config = 1 << ret_bit; } - let c_name = CString::new(name.as_os_str().as_bytes()).unwrap(); + let c_name = CString::new(name.as_bytes()).unwrap(); attr.size = mem::size_of::() as u32; attr.type_ = ty; diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 1ef6c0c8..4c06ccf6 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -2896,7 +2896,7 @@ impl core::convert::From for aya::programs::kprobe::KProbeError pub fn aya::programs::kprobe::KProbeError::from(t: T) -> T pub struct aya::programs::kprobe::KProbe impl aya::programs::kprobe::KProbe -pub fn aya::programs::kprobe::KProbe::attach(&mut self, fn_name: &str, offset: u64) -> core::result::Result +pub fn aya::programs::kprobe::KProbe::attach>(&mut self, fn_name: T, offset: u64) -> core::result::Result pub fn aya::programs::kprobe::KProbe::detach(&mut self, link_id: aya::programs::kprobe::KProbeLinkId) -> core::result::Result<(), aya::programs::ProgramError> pub fn aya::programs::kprobe::KProbe::from_pin>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result pub fn aya::programs::kprobe::KProbe::kind(&self) -> aya::programs::ProbeKind @@ -5997,7 +5997,7 @@ impl core::convert::From for aya::programs::fexit::FExit pub fn aya::programs::fexit::FExit::from(t: T) -> T pub struct aya::programs::KProbe impl aya::programs::kprobe::KProbe -pub fn aya::programs::kprobe::KProbe::attach(&mut self, fn_name: &str, offset: u64) -> core::result::Result +pub fn aya::programs::kprobe::KProbe::attach>(&mut self, fn_name: T, offset: u64) -> core::result::Result pub fn aya::programs::kprobe::KProbe::detach(&mut self, link_id: aya::programs::kprobe::KProbeLinkId) -> core::result::Result<(), aya::programs::ProgramError> pub fn aya::programs::kprobe::KProbe::from_pin>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result pub fn aya::programs::kprobe::KProbe::kind(&self) -> aya::programs::ProbeKind