chore: tracefs review fixes

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

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

@ -9,8 +9,8 @@ use std::{
use crate::{
programs::{
kprobe::KProbeError, perf_attach, perf_attach::PerfLink, perf_attach_debugfs,
trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::get_tracefs, Link,
ProgramData, ProgramError,
trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::find_tracefs_path,
Link, ProgramData, ProgramError,
},
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> {
use ProbeKind::*;
let tracefs = get_tracefs()?;
let tracefs = find_tracefs_path()?;
match kind {
KProbe | KRetProbe => delete_probe_event(tracefs, kind, event_alias)
@ -118,7 +118,7 @@ fn create_as_trace_point(
) -> Result<(i32, String), ProgramError> {
use ProbeKind::*;
let tracefs = get_tracefs()?;
let tracefs = find_tracefs_path()?;
let event_alias = match kind {
KProbe | KRetProbe => create_probe_event(tracefs, kind, name, offset)

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

@ -1,5 +1,5 @@
//! 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::{
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())
}
// Get tracefs filesystem
pub(crate) fn get_tracefs() -> Result<&'static Path, ProgramError> {
/// Find tracefs filesystem path
pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
lazy_static::lazy_static! {
static ref TRACE_FS: Option<&'static Path> = {
let mounts = [
let known_mounts = [
Path::new("/sys/kernel/tracing"),
Path::new("/sys/kernel/debug/tracing"),
];
for mount in mounts {
for mount in known_mounts {
if mount.exists() {
return Some(mount);
}
}
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