diff --git a/proj-1/deet/src/debugger.rs b/proj-1/deet/src/debugger.rs index 36f1b9c..9622477 100644 --- a/proj-1/deet/src/debugger.rs +++ b/proj-1/deet/src/debugger.rs @@ -28,10 +28,25 @@ impl Debugger { } } + fn _try_kill(&mut self) { + if self.inferior.is_some() { + let kill_result = self.inferior.as_mut().unwrap().kill(); + if let Ok(_) = kill_result { + println!( + "Killing running inferior (pid {})", + self.inferior.as_ref().unwrap().pid() + ); + } else { + eprintln!("Error killing running inferior"); + } + } + } + pub fn run(&mut self) { loop { match self.get_next_command() { DebuggerCommand::Run(args) => { + self._try_kill(); if let Some(inferior) = Inferior::new(&self.target, &args) { // Create the inferior self.inferior = Some(inferior); @@ -41,10 +56,13 @@ impl Debugger { if let Ok(res) = res { match res { Status::Exited(exit_status) => { - println!("Child exited (statuss {})", exit_status); - }, + println!("Child exited (status {})", exit_status); + self.inferior = None + } Status::Signaled(_) => (), - Status::Stopped(_, _) => (), + Status::Stopped(_signal, _) => { + println!("Child stopped (signal {})", _signal); + } } } else { eprintln!("Error running(continue) subprocess"); @@ -54,8 +72,32 @@ impl Debugger { } } DebuggerCommand::Quit => { + self._try_kill(); return; } + DebuggerCommand::Continue => { + let instance = self.inferior.as_mut(); + if instance.is_none() { + eprintln!("No inferior process running"); + continue; + } + let instance = instance.unwrap(); + let res = instance.cont(); + if let Ok(res) = res { + match res { + Status::Exited(exit_status) => { + println!("Child exited (status {})", exit_status); + self.inferior = None + } + Status::Signaled(_) => (), + Status::Stopped(_signal, _) => { + println!("Child stopped (signal {})", _signal); + } + } + } else { + eprintln!("Error running(continue) subprocess"); + } + } } } } diff --git a/proj-1/deet/src/debugger_command.rs b/proj-1/deet/src/debugger_command.rs index 4b111fc..01a7494 100644 --- a/proj-1/deet/src/debugger_command.rs +++ b/proj-1/deet/src/debugger_command.rs @@ -1,6 +1,7 @@ pub enum DebuggerCommand { Quit, Run(Vec), + Continue, } impl DebuggerCommand { @@ -13,6 +14,9 @@ impl DebuggerCommand { args.iter().map(|s| s.to_string()).collect(), )) } + "c" | "cont" | "continue" => { + Some(DebuggerCommand::Continue) + } // Default case: _ => None, } diff --git a/proj-1/deet/src/inferior.rs b/proj-1/deet/src/inferior.rs index 25421c8..3bceee4 100644 --- a/proj-1/deet/src/inferior.rs +++ b/proj-1/deet/src/inferior.rs @@ -6,7 +6,7 @@ use nix::unistd::Pid; use std::os::unix::process::CommandExt; use std::process::Child; use std::process::Command; - +#[derive(Debug)] pub enum Status { /// Indicates inferior stopped. Contains the signal that stopped the process, as well as the /// current instruction pointer that it is stopped at. @@ -87,4 +87,10 @@ impl Inferior { ptrace::cont(self.pid(), None)?; self.wait(None) } + + pub fn kill(&mut self) -> Result { + let _pid = self.pid(); + self.child.kill().expect("Failed to kill subprocess"); + self.wait(None) + } }