From 0bfaadf339bd263c9458e827819e39b10bd925ed Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Sun, 15 Oct 2023 17:45:49 +0200
Subject: Init

---
 src/trace.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 src/trace.rs

(limited to 'src/trace.rs')

diff --git a/src/trace.rs b/src/trace.rs
new file mode 100644
index 0000000..0031fcd
--- /dev/null
+++ b/src/trace.rs
@@ -0,0 +1,49 @@
+use nix::{
+    sys::{
+        ptrace,
+        signal::Signal,
+        wait::{waitpid, WaitStatus},
+    },
+    unistd::Pid,
+};
+use std::{os::unix::process::CommandExt, process::Command};
+
+pub fn exec(command: &mut Command) -> anyhow::Result<Pid> {
+    unsafe {
+        command.pre_exec(|| ptrace::traceme().map_err(|e| e.into()));
+    }
+    let child = command.spawn()?;
+    Ok(Pid::from_raw(child.id() as i32))
+}
+
+pub fn trace(pid: Pid) -> anyhow::Result<()> {
+    let mut have_to_print = true;
+    loop {
+        have_to_print ^= true;
+        ptrace::syscall(pid, None)?;
+        let status = waitpid(pid, None)?;
+
+        match status {
+            WaitStatus::Exited(_pid, _) => {
+                break;
+            }
+            WaitStatus::Stopped(pid, signal) => {
+                match signal {
+                    Signal::SIGTRAP => {
+                        let regs = ptrace::getregs(pid)?;
+                        if have_to_print {
+                            println!(
+                                "{}({:x}, {:x}, {:x}, ...) = {:x}",
+                                regs.orig_rax, regs.rdi, regs.rsi, regs.rdx, regs.rax,
+                            );
+                        }
+                    }
+                    _ => {}
+                };
+            }
+            _ => {}
+        };
+    }
+
+    Ok(())
+}
-- 
cgit v1.2.3-18-g5258