perf_event: refactor perf_event_open_trace_point

Rewrite it in terms of perf_event_open.
reviewable/pr1393/r8
Tamir Duberstein 6 days ago
parent d4b2dde78a
commit 1944c4aa00
No known key found for this signature in database

@ -9,8 +9,6 @@ use std::{
sync::atomic::{AtomicUsize, Ordering}, sync::atomic::{AtomicUsize, Ordering},
}; };
use libc::pid_t;
use crate::{ use crate::{
programs::{ programs::{
Link, ProgramData, ProgramError, kprobe::KProbeError, perf_attach, Link, ProgramData, ProgramError, kprobe::KProbeError, perf_attach,
@ -111,7 +109,7 @@ pub(crate) fn attach<T: Link + From<PerfLinkInner>>(
// separate argument. // separate argument.
fn_name: &OsStr, fn_name: &OsStr,
offset: u64, offset: u64,
pid: Option<pid_t>, pid: Option<u32>,
cookie: Option<u64>, cookie: Option<u64>,
) -> Result<T::Id, ProgramError> { ) -> Result<T::Id, ProgramError> {
// https://github.com/torvalds/linux/commit/e12f03d7031a977356e3d7b75a68c2185ff8d155 // https://github.com/torvalds/linux/commit/e12f03d7031a977356e3d7b75a68c2185ff8d155
@ -153,7 +151,7 @@ fn create_as_probe(
kind: ProbeKind, kind: ProbeKind,
fn_name: &OsStr, fn_name: &OsStr,
offset: u64, offset: u64,
pid: Option<pid_t>, pid: Option<u32>,
) -> Result<crate::MockableFd, ProgramError> { ) -> Result<crate::MockableFd, ProgramError> {
use ProbeKind::*; use ProbeKind::*;
@ -188,7 +186,7 @@ fn create_as_trace_point(
kind: ProbeKind, kind: ProbeKind,
name: &OsStr, name: &OsStr,
offset: u64, offset: u64,
pid: Option<pid_t>, pid: Option<u32>,
) -> Result<(crate::MockableFd, OsString), ProgramError> { ) -> Result<(crate::MockableFd, OsString), ProgramError> {
use ProbeKind::*; use ProbeKind::*;

@ -115,7 +115,7 @@ pub(crate) fn read_sys_fs_trace_point_id(
tracefs: &Path, tracefs: &Path,
category: &str, category: &str,
name: &Path, name: &Path,
) -> Result<u32, TracePointError> { ) -> Result<u64, TracePointError> {
let filename = tracefs.join("events").join(category).join(name).join("id"); let filename = tracefs.join("events").join(category).join(name).join("id");
let id = match fs::read_to_string(&filename) { let id = match fs::read_to_string(&filename) {

@ -11,7 +11,6 @@ use std::{
}; };
use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}; 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 object::{Object as _, ObjectSection as _, ObjectSymbol as _, Symbol};
use thiserror::Error; use thiserror::Error;
@ -109,7 +108,7 @@ impl UProbe {
&mut self, &mut self,
location: Loc, location: Loc,
target: T, target: T,
pid: Option<pid_t>, pid: Option<u32>,
cookie: Option<u64>, cookie: Option<u64>,
) -> Result<UProbeLinkId, ProgramError> { ) -> Result<UProbeLinkId, ProgramError> {
let proc_map = pid.map(ProcMap::new).transpose()?; let proc_map = pid.map(ProcMap::new).transpose()?;
@ -159,9 +158,10 @@ where
.and_then(|proc_map| { .and_then(|proc_map| {
proc_map proc_map
.find_library_path_by_name(target) .find_library_path_by_name(target)
.map_err(|source| UProbeError::ProcMap { .map_err(|source| {
pid: proc_map.pid, let ProcMap { pid, data: _ } = proc_map;
source, let pid = *pid;
UProbeError::ProcMap { pid, source }
}) })
.transpose() .transpose()
}) })
@ -190,7 +190,7 @@ where
)] )]
fn test_resolve_attach_path() { fn test_resolve_attach_path() {
// Look up the current process's pid. // 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(); 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 // 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}")] #[error("error fetching libs for {pid}")]
ProcMap { ProcMap {
/// The pid. /// The pid.
pid: i32, pid: u32,
/// The [`ProcMapError`] that caused the error. /// The [`ProcMapError`] that caused the error.
#[source] #[source]
source: ProcMapError, source: ProcMapError,
@ -419,12 +419,12 @@ impl<'a> ProcMapEntry<'a> {
/// ///
/// The information here may be used to resolve addresses to paths. /// The information here may be used to resolve addresses to paths.
struct ProcMap<T> { struct ProcMap<T> {
pid: pid_t, pid: u32,
data: T, data: T,
} }
impl ProcMap<Vec<u8>> { impl ProcMap<Vec<u8>> {
fn new(pid: pid_t) -> Result<Self, UProbeError> { fn new(pid: u32) -> Result<Self, UProbeError> {
let filename = PathBuf::from(format!("/proc/{pid}/maps")); let filename = PathBuf::from(format!("/proc/{pid}/maps"));
let data = fs::read(&filename) let data = fs::read(&filename)
.map_err(|io_error| UProbeError::FileError { filename, io_error })?; .map_err(|io_error| UProbeError::FileError { filename, io_error })?;

@ -133,7 +133,7 @@ pub(crate) fn perf_event_open_probe(
ret_bit: Option<u32>, ret_bit: Option<u32>,
name: &OsStr, name: &OsStr,
offset: u64, offset: u64,
pid: Option<pid_t>, pid: Option<u32>,
) -> io::Result<crate::MockableFd> { ) -> io::Result<crate::MockableFd> {
use std::os::unix::ffi::OsStrExt as _; 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_3.config1 = c_name.as_ptr() as u64;
attr.__bindgen_anon_4.config2 = offset; attr.__bindgen_anon_4.config2 = offset;
let cpu = if pid.is_some() { -1 } else { 0 }; let (pid, cpu) = match pid {
let pid = pid.unwrap_or(-1); Some(pid) => (pid as i32, -1),
None => (-1, 0),
};
perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC)
} }
pub(crate) fn perf_event_open_trace_point( pub(crate) fn perf_event_open_trace_point(
id: u32, event_id: u64,
pid: Option<pid_t>, pid: Option<u32>,
) -> io::Result<crate::MockableFd> { ) -> io::Result<crate::MockableFd> {
let mut attr = unsafe { mem::zeroed::<perf_event_attr>() }; let scope = match pid {
Some(pid) => PerfEventScope::OneProcess { pid, cpu: None },
attr.size = mem::size_of::<perf_event_attr>() as u32; None => PerfEventScope::AllProcessesOneCpu { cpu: 0 },
attr.type_ = PERF_TYPE_TRACEPOINT as u32; };
attr.config = u64::from(id); perf_event_open(
PerfEventConfig::TracePoint { event_id },
let cpu = if pid.is_some() { -1 } else { 0 }; scope,
let pid = pid.unwrap_or(-1); SamplePolicy::Period(0),
WakeupPolicy::Events(1),
perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) false,
PERF_FLAG_FD_CLOEXEC,
)
} }
pub(crate) fn perf_event_ioctl( pub(crate) fn perf_event_ioctl(

@ -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
pub aya::programs::uprobe::UProbeError::InvalidTarget::path: std::path::PathBuf pub aya::programs::uprobe::UProbeError::InvalidTarget::path: std::path::PathBuf
pub aya::programs::uprobe::UProbeError::ProcMap 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::ProcMap::source: aya::programs::uprobe::ProcMapError
pub aya::programs::uprobe::UProbeError::SymbolError 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)> 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 pub struct aya::programs::uprobe::UProbe
impl aya::programs::uprobe::UProbe impl aya::programs::uprobe::UProbe
pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType
pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef<std::path::Path>, Loc: core::convert::Into<aya::programs::uprobe::UProbeAttachLocation<'loc>>>(&mut self, location: Loc, target: T, pid: core::option::Option<libc::unix::pid_t>, cookie: core::option::Option<u64>) -> core::result::Result<aya::programs::uprobe::UProbeLinkId, aya::programs::ProgramError> pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef<std::path::Path>, Loc: core::convert::Into<aya::programs::uprobe::UProbeAttachLocation<'loc>>>(&mut self, location: Loc, target: T, pid: core::option::Option<u32>, cookie: core::option::Option<u64>) -> core::result::Result<aya::programs::uprobe::UProbeLinkId, aya::programs::ProgramError>
pub fn aya::programs::uprobe::UProbe::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::uprobe::UProbe::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::uprobe::UProbe::kind(&self) -> aya::programs::ProbeKind 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> 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
pub aya::programs::UProbeError::InvalidTarget::path: std::path::PathBuf pub aya::programs::UProbeError::InvalidTarget::path: std::path::PathBuf
pub aya::programs::UProbeError::ProcMap 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::ProcMap::source: aya::programs::uprobe::ProcMapError
pub aya::programs::UProbeError::SymbolError pub aya::programs::UProbeError::SymbolError
pub aya::programs::UProbeError::SymbolError::error: alloc::boxed::Box<(dyn core::error::Error + core::marker::Send + core::marker::Sync)> 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 pub struct aya::programs::UProbe
impl aya::programs::uprobe::UProbe impl aya::programs::uprobe::UProbe
pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType pub const aya::programs::uprobe::UProbe::PROGRAM_TYPE: aya::programs::ProgramType
pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef<std::path::Path>, Loc: core::convert::Into<aya::programs::uprobe::UProbeAttachLocation<'loc>>>(&mut self, location: Loc, target: T, pid: core::option::Option<libc::unix::pid_t>, cookie: core::option::Option<u64>) -> core::result::Result<aya::programs::uprobe::UProbeLinkId, aya::programs::ProgramError> pub fn aya::programs::uprobe::UProbe::attach<'loc, T: core::convert::AsRef<std::path::Path>, Loc: core::convert::Into<aya::programs::uprobe::UProbeAttachLocation<'loc>>>(&mut self, location: Loc, target: T, pid: core::option::Option<u32>, cookie: core::option::Option<u64>) -> core::result::Result<aya::programs::uprobe::UProbeLinkId, aya::programs::ProgramError>
pub fn aya::programs::uprobe::UProbe::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::uprobe::UProbe::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::uprobe::UProbe::kind(&self) -> aya::programs::ProbeKind 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> pub fn aya::programs::uprobe::UProbe::load(&mut self) -> core::result::Result<(), aya::programs::ProgramError>

Loading…
Cancel
Save