From 2affb3567dc7fb42e97da8d5bc7a6b89bdbceb8d Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Sun, 15 Oct 2023 18:46:35 +0200
Subject: Add docs

---
 src/main.rs  | 3 +++
 src/trace.rs | 8 +++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/main.rs b/src/main.rs
index 0694aa5..1859606 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,9 +8,12 @@ use nix::unistd::Pid;
 #[derive(Parser)]
 #[command(author, version, about, long_about = None)]
 struct Args {
+    /// Command to execute from ptrace
     command: String,
 }
 
+/// Create a fork of the program and execute the process in the child. Parent gets the pid
+/// value and trace it.
 fn main() -> anyhow::Result<()> {
     let args = Args::parse();
 
diff --git a/src/trace.rs b/src/trace.rs
index b00b9f9..04b3ecd 100644
--- a/src/trace.rs
+++ b/src/trace.rs
@@ -8,6 +8,7 @@ use nix::{
 };
 use std::{os::unix::process::CommandExt, process::Command};
 
+/// Exec the `command` value tracing it with `ptrace` lib
 pub fn exec(command: &String) -> anyhow::Result<()> {
     let params: Vec<&str> = command.split(' ').collect();
 
@@ -23,10 +24,13 @@ pub fn exec(command: &String) -> anyhow::Result<()> {
     Ok(())
 }
 
+/// Trace a process with `pid` ID
 pub fn trace(pid: Pid) -> anyhow::Result<()> {
+    // Since you have to do 2 syscalls (start and end) you have to alternate the print value,
+    // because it could be equals except for the `rax` register.
     let mut have_to_print = true;
 
-    // First wait if for the parent process
+    // First wait for the parent process
     _ = waitpid(pid, None)?;
 
     loop {
@@ -35,9 +39,11 @@ pub fn trace(pid: Pid) -> anyhow::Result<()> {
         let status = waitpid(pid, None)?;
 
         match status {
+            // Break the loop if the process exists
             WaitStatus::Exited(_pid, _) => {
                 break;
             }
+            // Match the stopped value for a process
             WaitStatus::Stopped(pid, signal) => {
                 match signal {
                     Signal::SIGTRAP => {
-- 
cgit v1.2.3-18-g5258