chore: tracefs review fixes

pull/471/head
banditopazzo 2 years ago
parent c6c4ac7eea
commit 48fdf5a250

@ -207,9 +207,9 @@ pub enum ProgramError {
name: String, name: String,
}, },
/// TraceFS not found. /// An error occurred while working with IO.
#[error("tracefs non found")] #[error(transparent)]
TraceFsNotFound, IOError(#[from] io::Error),
} }
/// A [`Program`] file descriptor. /// A [`Program`] file descriptor.

@ -9,8 +9,8 @@ use std::{
use crate::{ use crate::{
programs::{ programs::{
kprobe::KProbeError, perf_attach, perf_attach::PerfLink, perf_attach_debugfs, kprobe::KProbeError, perf_attach, perf_attach::PerfLink, perf_attach_debugfs,
trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::get_tracefs, Link, trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::find_tracefs_path,
ProgramData, ProgramError, Link, ProgramData, ProgramError,
}, },
sys::{kernel_version, perf_event_open_probe, perf_event_open_trace_point}, sys::{kernel_version, perf_event_open_probe, perf_event_open_trace_point},
}; };
@ -61,7 +61,7 @@ pub(crate) fn attach<T: Link + From<PerfLink>>(
pub(crate) fn detach_debug_fs(kind: ProbeKind, event_alias: &str) -> Result<(), ProgramError> { pub(crate) fn detach_debug_fs(kind: ProbeKind, event_alias: &str) -> Result<(), ProgramError> {
use ProbeKind::*; use ProbeKind::*;
let tracefs = get_tracefs()?; let tracefs = find_tracefs_path()?;
match kind { match kind {
KProbe | KRetProbe => delete_probe_event(tracefs, kind, event_alias) KProbe | KRetProbe => delete_probe_event(tracefs, kind, event_alias)
@ -118,7 +118,7 @@ fn create_as_trace_point(
) -> Result<(i32, String), ProgramError> { ) -> Result<(i32, String), ProgramError> {
use ProbeKind::*; use ProbeKind::*;
let tracefs = get_tracefs()?; let tracefs = find_tracefs_path()?;
let event_alias = match kind { let event_alias = match kind {
KProbe | KRetProbe => create_probe_event(tracefs, kind, name, offset) KProbe | KRetProbe => create_probe_event(tracefs, kind, name, offset)

@ -7,13 +7,12 @@ use crate::{
programs::{ programs::{
define_link_wrapper, load_program, define_link_wrapper, load_program,
perf_attach::{perf_attach, PerfLink, PerfLinkId}, perf_attach::{perf_attach, PerfLink, PerfLinkId},
utils::find_tracefs_path,
ProgramData, ProgramError, ProgramData, ProgramError,
}, },
sys::perf_event_open_trace_point, sys::perf_event_open_trace_point,
}; };
use super::utils::get_tracefs;
/// The type returned when attaching a [`TracePoint`] fails. /// The type returned when attaching a [`TracePoint`] fails.
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum TracePointError { pub enum TracePointError {
@ -79,7 +78,7 @@ impl TracePoint {
/// ///
/// The returned value can be used to detach, see [TracePoint::detach]. /// The returned value can be used to detach, see [TracePoint::detach].
pub fn attach(&mut self, category: &str, name: &str) -> Result<TracePointLinkId, ProgramError> { pub fn attach(&mut self, category: &str, name: &str) -> Result<TracePointLinkId, ProgramError> {
let tracefs = get_tracefs()?; let tracefs = find_tracefs_path()?;
let id = read_sys_fs_trace_point_id(tracefs, category, name)?; let id = read_sys_fs_trace_point_id(tracefs, category, name)?;
let fd = perf_event_open_trace_point(id, None).map_err(|(_code, io_error)| { let fd = perf_event_open_trace_point(id, None).map_err(|(_code, io_error)| {
ProgramError::SyscallError { ProgramError::SyscallError {

@ -1,5 +1,5 @@
//! Common functions shared between multiple eBPF program types. //! Common functions shared between multiple eBPF program types.
use std::{ffi::CStr, os::unix::io::RawFd, path::Path}; use std::{ffi::CStr, io, os::unix::io::RawFd, path::Path};
use crate::{ use crate::{
programs::{FdLink, Link, ProgramData, ProgramError}, programs::{FdLink, Link, ProgramData, ProgramError},
@ -23,24 +23,25 @@ pub(crate) fn attach_raw_tracepoint<T: Link + From<FdLink>>(
program_data.links.insert(FdLink::new(pfd).into()) program_data.links.insert(FdLink::new(pfd).into())
} }
// Get tracefs filesystem /// Find tracefs filesystem path
pub(crate) fn get_tracefs() -> Result<&'static Path, ProgramError> { pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref TRACE_FS: Option<&'static Path> = { static ref TRACE_FS: Option<&'static Path> = {
let mounts = [ let known_mounts = [
Path::new("/sys/kernel/tracing"), Path::new("/sys/kernel/tracing"),
Path::new("/sys/kernel/debug/tracing"), Path::new("/sys/kernel/debug/tracing"),
]; ];
for mount in mounts { for mount in known_mounts {
if mount.exists() { if mount.exists() {
return Some(mount); return Some(mount);
} }
} }
None None
}; };
} }
TRACE_FS.as_deref().ok_or(ProgramError::TraceFsNotFound) TRACE_FS
.as_deref()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "tracefs not found").into())
} }

Loading…
Cancel
Save