milestone2

This commit is contained in:
ridethepig 2023-03-05 04:27:22 +00:00
parent 82de943a75
commit ec51b69948
3 changed files with 56 additions and 4 deletions

View File

@ -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");
}
}
}
}
}

View File

@ -1,6 +1,7 @@
pub enum DebuggerCommand {
Quit,
Run(Vec<String>),
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,
}

View File

@ -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<Status, nix::Error> {
let _pid = self.pid();
self.child.kill().expect("Failed to kill subprocess");
self.wait(None)
}
}