diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-10-15 18:46:35 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-10-15 18:46:35 +0200 |
commit | 2affb3567dc7fb42e97da8d5bc7a6b89bdbceb8d (patch) | |
tree | 040392627a943d77b970bca0d52af762551125d2 | |
parent | 36fc4e67d4b8c23d726234f34113ae518d4a12e7 (diff) |
Add docs
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/trace.rs | 8 |
2 files changed, 10 insertions, 1 deletions
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 => { |