Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/debugger/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use {
};

pub trait DebuggerInterface {
fn step(&mut self) -> Value;
fn next(&mut self) -> Value;
fn finish(&mut self) -> Value;
fn r#continue(&mut self) -> Value;
fn set_breakpoint(&mut self, file: String, line: usize) -> Value;
fn remove_breakpoint(&mut self, file: String, line: usize) -> Value;
Expand Down Expand Up @@ -59,7 +61,9 @@ pub fn run_adapter_loop<T: DebuggerInterface>(debugger: &mut T) {
Ok(cmd) => {
response.request_id = cmd.request_id.clone();
let result = match cmd.command.as_str() {
"step" => debugger.step(),
"next" => debugger.next(),
"finish" => debugger.finish(),
"continue" => debugger.r#continue(),
"setBreakpoint" => {
if let Some(args) = cmd.args {
Expand Down
145 changes: 95 additions & 50 deletions crates/debugger/src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ pub struct StackFrame<'a> {

#[derive(Debug)]
pub enum DebugMode {
Step,
Next,
Finish,
Continue,
}

#[derive(Debug)]
pub enum DebugEvent {
Stopped(u64, Option<usize>),
Breakpoint(u64, Option<usize>),
Next(u64, Option<usize>),
Exit(u64),
Error(String),
}
Expand Down Expand Up @@ -163,57 +165,24 @@ impl<H: SyscallHandler> Debugger<H> {

pub fn run(&mut self) -> DebuggerResult<DebugEvent> {
match self.debug_mode {
DebugMode::Step => self.execute_step(),
DebugMode::Next => {
let current_pc = self.get_pc();

if self.at_breakpoint {
match self.vm.step() {
Ok(()) => {
self.at_breakpoint = false;
self.last_breakpoint_pc = None;

if self.vm.halted {
let exit_code = self.vm.exit_code.unwrap_or(0);
return Ok(DebugEvent::Exit(exit_code));
}

let new_pc = self.get_pc();
if self.breakpoints.contains(&new_pc) {
self.at_breakpoint = true;
self.last_breakpoint_pc = Some(new_pc);
let line_number = self.get_line_for_pc(new_pc);
return Ok(DebugEvent::Breakpoint(new_pc, line_number));
}
let line_number = self.get_line_for_pc(new_pc);
return Ok(DebugEvent::Next(new_pc, line_number));
}
Err(e) => return Ok(DebugEvent::Error(format!("{e}"))),
// If call depth increases after step, run until it returns to previous depth.
let call_depth = self.vm.call_stack.len();
match self.execute_step()? {
DebugEvent::Stopped(_, _) if self.vm.call_stack.len() > call_depth => {
self.run_until_call_depth(call_depth)
}
other => Ok(other),
}

if self.breakpoints.contains(&current_pc)
&& self.last_breakpoint_pc != Some(current_pc)
{
self.at_breakpoint = true;
self.last_breakpoint_pc = Some(current_pc);
let line_number = self.get_line_for_pc(current_pc);
return Ok(DebugEvent::Breakpoint(current_pc, line_number));
}
DebugMode::Finish => {
// If call depth > 0, run until it decreases by one level.
let call_depth = self.vm.call_stack.len();
if call_depth == 0 {
return Ok(DebugEvent::Error("not inside a function call".to_string()));
}

let event = match self.vm.step() {
Ok(()) => {
if self.vm.halted {
let exit_code = self.vm.exit_code.unwrap_or(0);
DebugEvent::Exit(exit_code)
} else {
let new_pc = self.get_pc();
let line_number = self.get_line_for_pc(new_pc);
DebugEvent::Next(new_pc, line_number)
}
}
Err(e) => DebugEvent::Error(format!("{e}")),
};
Ok(event)
self.run_until_call_depth(call_depth - 1)
}
DebugMode::Continue => loop {
let current_pc = self.get_pc();
Expand Down Expand Up @@ -256,6 +225,66 @@ impl<H: SyscallHandler> Debugger<H> {
}
}

// Execute a single step.
fn execute_step(&mut self) -> DebuggerResult<DebugEvent> {
let current_pc = self.get_pc();

if self.at_breakpoint {
match self.vm.step() {
Ok(()) => {
self.at_breakpoint = false;
self.last_breakpoint_pc = None;

if self.vm.halted {
return Ok(DebugEvent::Exit(self.vm.exit_code.unwrap_or(0)));
}

let new_pc = self.get_pc();
if self.breakpoints.contains(&new_pc) {
self.at_breakpoint = true;
self.last_breakpoint_pc = Some(new_pc);
return Ok(DebugEvent::Breakpoint(new_pc, self.get_line_for_pc(new_pc)));
}
return Ok(DebugEvent::Stopped(new_pc, self.get_line_for_pc(new_pc)));
}
Err(e) => return Ok(DebugEvent::Error(format!("{e}"))),
}
}

if self.breakpoints.contains(&current_pc) && self.last_breakpoint_pc != Some(current_pc) {
self.at_breakpoint = true;
self.last_breakpoint_pc = Some(current_pc);
return Ok(DebugEvent::Breakpoint(
current_pc,
self.get_line_for_pc(current_pc),
));
}

match self.vm.step() {
Ok(()) => {
if self.vm.halted {
Ok(DebugEvent::Exit(self.vm.exit_code.unwrap_or(0)))
} else {
let new_pc = self.get_pc();
Ok(DebugEvent::Stopped(new_pc, self.get_line_for_pc(new_pc)))
}
}
Err(e) => Ok(DebugEvent::Error(format!("{e}"))),
}
}

// Step through instructions until the current call depth reaches the target depth.
fn run_until_call_depth(&mut self, target_depth: usize) -> DebuggerResult<DebugEvent> {
while self.vm.call_stack.len() > target_depth {
match self.execute_step()? {
DebugEvent::Stopped(_, _) => {}
other => return Ok(other),
}
}
let new_pc = self.get_pc();
Ok(DebugEvent::Stopped(new_pc, self.get_line_for_pc(new_pc)))
}

pub fn get_pc(&self) -> u64 {
self.instruction_offsets
.get(self.vm.pc)
Expand Down Expand Up @@ -382,11 +411,21 @@ impl<H: SyscallHandler> Debugger<H> {
}

impl<H: SyscallHandler> DebuggerInterface for Debugger<H> {
fn step(&mut self) -> Value {
self.set_debug_mode(DebugMode::Step);
self.run_to_json()
}

fn next(&mut self) -> Value {
self.set_debug_mode(DebugMode::Next);
self.run_to_json()
}

fn finish(&mut self) -> Value {
self.set_debug_mode(DebugMode::Finish);
self.run_to_json()
}

fn r#continue(&mut self) -> Value {
self.set_debug_mode(DebugMode::Continue);
self.run_to_json()
Expand Down Expand Up @@ -539,10 +578,16 @@ impl<H: SyscallHandler> DebuggerInterface for Debugger<H> {
}

fn run_to_json(&mut self) -> Value {
let mode_type = match &self.debug_mode {
DebugMode::Step => "step",
DebugMode::Next => "next",
DebugMode::Finish => "finish",
DebugMode::Continue => "continue",
};
match self.run() {
Ok(event) => match event {
DebugEvent::Next(pc, line) => json!({
"type": "next",
DebugEvent::Stopped(pc, line) => json!({
"type": mode_type,
"pc": pc,
"line": line
}),
Expand Down
8 changes: 6 additions & 2 deletions crates/debugger/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ impl Repl {
}
let cmd = input.trim();
match cmd {
"step" | "s" => self.run_and_display(DebugMode::Step),
"next" | "n" => self.run_and_display(DebugMode::Next),
"finish" | "f" => self.run_and_display(DebugMode::Finish),
"continue" | "c" => self.run_and_display(DebugMode::Continue),
cmd if cmd.starts_with("break ") || cmd.starts_with("b ") => {
if let Some(arg) = cmd.split_whitespace().nth(1) {
Expand Down Expand Up @@ -151,7 +153,9 @@ impl Repl {
}
"help" => {
println!("Commands:");
println!(" next (n) - Execute one instruction");
println!(" step (s) - Step into");
println!(" next (n) - Step over");
println!(" finish (f) - Step out");
println!(" continue (c) - Continue execution");
println!(" break (b) <line> - Set breakpoint at line number");
println!(" delete (d) <line> - Remove breakpoint at line");
Expand All @@ -173,7 +177,7 @@ impl Repl {
self.session.debugger.set_debug_mode(mode);
match self.session.debugger.run() {
Ok(event) => match event {
DebugEvent::Next(_pc, line) => {
DebugEvent::Stopped(_pc, line) => {
if let Some(line_num) = line {
let asm = self
.session
Expand Down