Skip to content

Commit

Permalink
feat: add optional strace log mirror output
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma-s1n committed Jun 3, 2024
1 parent d0c570f commit 76f3c14
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub enum Action {
/// Generate profile data file to be merged with others instead of generating systemd options directly
#[arg(short, long, default_value = None)]
profile_data_path: Option<PathBuf>,
/// Log strace output to this file.
/// Only use for debugging: this will slow down processing, and may generate a huge file.
#[arg(short = 'l', long, default_value = None)]
strace_log_path: Option<PathBuf>,
},
/// Merge profile data from previous runs to generate systemd options
MergeProfileData {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ fn main() -> anyhow::Result<()> {
command,
mode,
profile_data_path,
strace_log_path,
} => {
// Build supported systemd options
let sd_opts = sd_options(&sd_version, &kernel_version, &mode)?;

// Run strace
let cmd = command.iter().map(|a| &**a).collect::<Vec<&str>>();
let st = strace::Strace::run(&cmd)?;
let st = strace::Strace::run(&cmd, strace_log_path)?;

// Start signal handling thread
let mut signals = signal_hook::iterator::Signals::new([
Expand Down
24 changes: 21 additions & 3 deletions src/strace/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Strace output parser
use std::io::BufRead;
use std::{
fs::File,
io::{self, BufRead, BufWriter, Write},
path::Path,
};

use crate::strace::Syscall;

Expand All @@ -16,14 +20,22 @@ use regex::parse_line;

pub struct LogParser {
reader: Box<dyn BufRead>,
log: Option<BufWriter<File>>,
buf: String,
unfinished_syscalls: Vec<Syscall>,
}

impl LogParser {
pub fn new(reader: Box<dyn BufRead>) -> anyhow::Result<Self> {
pub fn new(reader: Box<dyn BufRead>, log_path: Option<&Path>) -> anyhow::Result<Self> {
let log = log_path
.map(|p| -> io::Result<_> {
let file = File::options().create(true).append(true).open(p)?;
Ok(BufWriter::with_capacity(64 * 1024, file))
})
.map_or(Ok(None), |v| v.map(Some))?;
Ok(Self {
reader,
log,
buf: String::new(),
unfinished_syscalls: Vec::new(),
})
Expand Down Expand Up @@ -65,6 +77,12 @@ impl Iterator for LogParser {
continue;
}

if let Some(log) = self.log.as_mut() {
if let Err(e) = writeln!(log, "{line}") {
return Some(Err(e.into()));
}
}

match parse_line(line, &self.unfinished_syscalls) {
Ok(ParseResult::Syscall(sc)) => {
log::trace!("Parsed line: {line:?}");
Expand Down Expand Up @@ -1286,7 +1304,7 @@ mod tests {
.as_bytes()
.to_vec(),
);
let parser = LogParser::new(Box::new(lines)).unwrap();
let parser = LogParser::new(Box::new(lines), None).unwrap();
let syscalls: Vec<Syscall> = parser.into_iter().collect::<Result<_, _>>().unwrap();

assert_eq!(
Expand Down
7 changes: 5 additions & 2 deletions src/strace/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ pub struct Strace {
process: Child,
/// Temp dir for pipe location
pipe_dir: tempfile::TempDir,
/// Strace log mirror path
log_path: Option<PathBuf>,
}

impl Strace {
pub fn run(command: &[&str]) -> anyhow::Result<Self> {
pub fn run(command: &[&str], log_path: Option<PathBuf>) -> anyhow::Result<Self> {
// Create named pipe
let pipe_dir = tempfile::tempdir()?;
let pipe_path = Self::pipe_path(&pipe_dir);
Expand Down Expand Up @@ -57,6 +59,7 @@ impl Strace {
Ok(Self {
process: child,
pipe_dir,
log_path,
})
}

Expand All @@ -67,7 +70,7 @@ impl Strace {
pub fn log_lines(&self) -> anyhow::Result<LogParser> {
let pipe_path = Self::pipe_path(&self.pipe_dir);
let reader = BufReader::new(File::open(pipe_path)?);
LogParser::new(Box::new(reader))
LogParser::new(Box::new(reader), self.log_path.as_deref())
}
}

Expand Down

0 comments on commit 76f3c14

Please sign in to comment.