Skip to content

Commit

Permalink
feat: parse strace version
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma-s1n committed Dec 1, 2023
1 parent 5c8ec20 commit d4064c6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ fn main() -> anyhow::Result<()> {
// Get versions
let sd_version = systemd::SystemdVersion::local_system()?;
let kernel_version = systemd::KernelVersion::local_system()?;
log::info!("Detected Systemd version: {sd_version}, Linux kernel version: {kernel_version}");
let strace_version = strace::StraceVersion::local_system()?;
log::info!("Detected versions: Systemd {sd_version}, Linux kernel {kernel_version}, strace {strace_version}");
if strace_version < strace::StraceVersion::new(6, 4) {
log::warn!("Strace version >=6.4 is strongly recommended, if you experience strace output parsing errors, please consider upgrading")
}

// Parse cl args
let args = cl::Args::parse();
Expand Down
44 changes: 44 additions & 0 deletions src/strace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Strace related code
use std::collections::HashMap;
use std::fmt;
use std::io::BufRead;
use std::process::Command;
use std::str;

mod parser;
mod run;
Expand Down Expand Up @@ -82,3 +86,43 @@ impl IntegerExpression {
}

pub type SyscallRetVal = i128; // allows holding both signed and unsigned 64 bit integers

#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub struct StraceVersion {
pub major: u16,
pub minor: u16,
}

impl StraceVersion {
pub fn new(major: u16, minor: u16) -> Self {
Self { major, minor }
}

pub fn local_system() -> anyhow::Result<Self> {
let output = Command::new("strace").arg("--version").output()?;
if !output.status.success() {
anyhow::bail!("strace invocation failed with code {:?}", output.status);
}
let version_line = output
.stdout
.lines()
.next()
.ok_or_else(|| anyhow::anyhow!("Unable to get strace version"))??;
let (major, minor) = version_line
.rsplit_once(' ')
.ok_or_else(|| anyhow::anyhow!("Unable to get strace version"))?
.1
.split_once('.')
.ok_or_else(|| anyhow::anyhow!("Unable to get strace version"))?;
Ok(Self {
major: major.parse()?,
minor: minor.parse()?,
})
}
}

impl fmt::Display for StraceVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}

0 comments on commit d4064c6

Please sign in to comment.