Skip to content

Commit

Permalink
aya: support non-UTF8 probing
Browse files Browse the repository at this point in the history
Fixes #751.
  • Loading branch information
tamird committed Aug 24, 2023
1 parent 8ffd9bb commit 1ccfdbc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
9 changes: 7 additions & 2 deletions aya/src/programs/kprobe.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Kernel space probes.
use std::{
ffi::OsStr,
io,
os::fd::AsFd as _,
path::{Path, PathBuf},
Expand Down Expand Up @@ -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<KProbeLinkId, ProgramError> {
attach(&mut self.data, self.kind, Path::new(fn_name), offset, None)
pub fn attach<T: AsRef<OsStr>>(
&mut self,
fn_name: T,
offset: u64,
) -> Result<KProbeLinkId, ProgramError> {
attach(&mut self.data, self.kind, fn_name.as_ref(), offset, None)
}

/// Detaches the program.
Expand Down
16 changes: 10 additions & 6 deletions aya/src/programs/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ pub(crate) struct ProbeEvent {
pub(crate) fn attach<T: Link + From<PerfLinkInner>>(
program_data: &mut ProgramData<T>,
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<pid_t>,
) -> Result<T::Id, ProgramError> {
Expand Down Expand Up @@ -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<pid_t>,
) -> Result<OwnedFd, ProgramError> {
Expand Down Expand Up @@ -176,7 +182,7 @@ fn create_as_probe(

fn create_as_trace_point(
kind: ProbeKind,
name: &Path,
name: &OsStr,
offset: u64,
pid: Option<pid_t>,
) -> Result<(OwnedFd, OsString), ProgramError> {
Expand Down Expand Up @@ -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<OsString, (PathBuf, io::Error)> {
use std::os::unix::ffi::OsStrExt as _;
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion aya/src/programs/uprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions aya/src/sys/perf_event.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -63,7 +62,7 @@ pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult<OwnedFd> {
pub(crate) fn perf_event_open_probe(
ty: u32,
ret_bit: Option<u32>,
name: &Path,
name: &OsStr,
offset: u64,
pid: Option<pid_t>,
) -> SysResult<OwnedFd> {
Expand All @@ -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::<perf_event_attr>() as u32;
attr.type_ = ty;
Expand Down
4 changes: 2 additions & 2 deletions xtask/public-api/aya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2896,7 +2896,7 @@ impl<T> core::convert::From<T> 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<aya::programs::kprobe::KProbeLinkId, aya::programs::ProgramError>
pub fn aya::programs::kprobe::KProbe::attach<T: core::convert::AsRef<std::ffi::os_str::OsStr>>(&mut self, fn_name: T, offset: u64) -> core::result::Result<aya::programs::kprobe::KProbeLinkId, aya::programs::ProgramError>
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<P: core::convert::AsRef<std::path::Path>>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result<Self, aya::programs::ProgramError>
pub fn aya::programs::kprobe::KProbe::kind(&self) -> aya::programs::ProbeKind
Expand Down Expand Up @@ -5997,7 +5997,7 @@ impl<T> core::convert::From<T> 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<aya::programs::kprobe::KProbeLinkId, aya::programs::ProgramError>
pub fn aya::programs::kprobe::KProbe::attach<T: core::convert::AsRef<std::ffi::os_str::OsStr>>(&mut self, fn_name: T, offset: u64) -> core::result::Result<aya::programs::kprobe::KProbeLinkId, aya::programs::ProgramError>
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<P: core::convert::AsRef<std::path::Path>>(path: P, kind: aya::programs::ProbeKind) -> core::result::Result<Self, aya::programs::ProgramError>
pub fn aya::programs::kprobe::KProbe::kind(&self) -> aya::programs::ProbeKind
Expand Down

0 comments on commit 1ccfdbc

Please sign in to comment.