diff options
-rw-r--r-- | ripple/minitrace/src/main.rs | 42 |
1 files changed, 37 insertions, 5 deletions
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<CString> { + 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 |