From 0c1fa0017ab4eb5f1a547e42347bb376868b60df Mon Sep 17 00:00:00 2001 From: edef Date: Tue, 8 Feb 2022 01:46:16 +0000 Subject: ripple/minitrace: log openat paths Co-authored-by: V Change-Id: Idcb3c29c4761158be788511f5f4bdb3003edf909 --- ripple/minitrace/src/main.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'ripple/minitrace/src') diff --git a/ripple/minitrace/src/main.rs b/ripple/minitrace/src/main.rs index 5fa35da..6b7c044 100644 --- a/ripple/minitrace/src/main.rs +++ b/ripple/minitrace/src/main.rs @@ -14,7 +14,15 @@ use { }, unistd::Pid, }, - std::{convert::TryInto, env, os::unix::process::CommandExt, process::Command}, + std::{ + convert::TryInto, + env, + ffi::CString, + fs::File, + io::{self, BufRead, Seek, SeekFrom}, + os::unix::process::CommandExt, + process::Command, + }, }; // TODO(edef): consider implementing this in terms of TID? @@ -40,6 +48,7 @@ impl Tid { #[derive(Debug)] struct Process { tgid: Tgid, + mem: File, } impl Process { @@ -66,7 +75,25 @@ impl Process { status => bail!("unexpected child state: {:?}", status), } - Ok(Process { tgid }) + Ok(Process { + tgid, + mem: File::open(format!("/proc/{}/mem", tgid.0)) + .context("Couldn't open child memory")?, + }) + } + + fn read_mem_cstr(&self, ptr: u64) -> Result { + let mut mem = io::BufReader::new(&self.mem); + mem.seek(SeekFrom::Start(ptr))?; + let mut buf = vec![]; + mem.read_until(0, &mut buf)?; + + // TODO(V): replace with CString::from_vec_with_nul when we update to Rust 1.58 + if buf.pop() != Some(0) { + bail!("Couldn't find null terminator"); + } + + Ok(CString::new(buf).expect("logic error")) } } @@ -139,7 +166,7 @@ fn main() -> Result<()> { syscall_state = Some(EntryExit::Entry(entry)); - if !check_syscall(entry) { + if !check_syscall(&process, entry) { ptrace::kill(event_tid.as_pid())?; panic!("unsupported syscall {:?}", entry); } @@ -169,7 +196,7 @@ fn main() -> Result<()> { Ok(()) } -fn check_syscall(entry: SyscallEntry) -> bool { +fn check_syscall(process: &Process, entry: SyscallEntry) -> bool { match entry.number { // read 0 => {} @@ -236,12 +263,17 @@ fn check_syscall(entry: SyscallEntry) -> bool { // openat 257 => { - let [dirfd, _pathname, _flags, _mode, _, _] = entry.args; + let [dirfd, pathname, _flags, _mode, _, _] = entry.args; const AT_FDCWD: i32 = -100; if dirfd.try_into() == Ok(AT_FDCWD) { return false; } + + println!( + "openat(AT_FDCWD, {:?}, ..)", + process.read_mem_cstr(pathname).unwrap() + ); } // newfstatat -- cgit 1.4.1