Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow variables and stack trace inspection in the debugger #4184

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
self.vm.write_memory_at(ptr, value);
}

pub fn get_call_stack(&self) -> Vec<usize> {
self.vm.get_call_stack()
}

pub(super) fn solve(&mut self) -> Result<BrilligSolverStatus, OpcodeResolutionError> {
let status = self.vm.process_opcodes();
self.handle_vm_status(status)
Expand Down
10 changes: 10 additions & 0 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@
self.memory.write(ptr, value);
}

/// Returns the VM's current call stack, including the actual program
/// counter in the last position of the returned vector.
pub fn get_call_stack(&self) -> Vec<usize> {
self.call_stack
.iter()
.map(|v| v.to_usize())
.chain(std::iter::once(self.program_counter))
.collect()
}

/// Process a single opcode and modify the program counter.
pub fn process_opcode(&mut self) -> VMStatus {
let opcode = &self.bytecode[self.program_counter];
Expand Down Expand Up @@ -545,7 +555,7 @@
}

#[test]
fn jmpifnot_opcode() {

Check warning on line 558 in acvm-repo/brillig_vm/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (jmpifnot)
let input_registers =
Registers::load(vec![Value::from(1u128), Value::from(2u128), Value::from(0u128)]);

Expand Down
65 changes: 64 additions & 1 deletion tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
}
}

pub(super) fn get_call_stack(&self) -> Vec<OpcodeLocation> {
let ip = self.acvm.instruction_pointer();
ggiraldez marked this conversation as resolved.
Show resolved Hide resolved
if ip >= self.get_opcodes().len() {
vec![]
} else if let Some(ref solver) = self.brillig_solver {
solver
.get_call_stack()
.iter()
.map(|pc| OpcodeLocation::Brillig { acir_index: ip, brillig_index: *pc })
.collect()
} else {
vec![OpcodeLocation::Acir(ip)]
}
}

pub(super) fn is_source_location_in_debug_module(&self, location: &Location) -> bool {
self.debug_artifact
.file_map
Expand Down Expand Up @@ -123,6 +138,21 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
.unwrap_or(vec![])
}

/// Returns the current call stack with expanded source locations. In
/// general, the matching between opcode location and source location is 1
/// to 1, but due to the compiler inlining functions a single opcode
/// location may expand to multiple source locations.
pub(super) fn get_source_call_stack(&self) -> Vec<(OpcodeLocation, Location)> {
self.get_call_stack()
.iter()
.flat_map(|opcode_location| {
self.get_source_location_for_opcode_location(opcode_location)
.into_iter()
.map(|source_location| (*opcode_location, source_location))
})
.collect()
}

fn get_opcodes_sizes(&self) -> Vec<usize> {
self.get_opcodes()
.iter()
Expand Down Expand Up @@ -362,7 +392,8 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
}
}

pub(super) fn next(&mut self) -> DebugCommandResult {
/// Steps debugging execution until the next source location
pub(super) fn next_into(&mut self) -> DebugCommandResult {
let start_location = self.get_current_source_location();
loop {
let result = self.step_into_opcode();
Expand All @@ -376,6 +407,38 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
}
}

/// Steps debugging execution until the next source location at the same (or
/// less) call stack depth (eg. don't dive into function calls)
pub(super) fn next_over(&mut self) -> DebugCommandResult {
let start_call_stack = self.get_source_call_stack();
loop {
let result = self.next_into();
if !matches!(result, DebugCommandResult::Ok) {
return result;
}
let new_call_stack = self.get_source_call_stack();
if new_call_stack.len() <= start_call_stack.len() {
return DebugCommandResult::Ok;
}
}
}

/// Steps debugging execution until the next source location with a smaller
/// call stack depth (eg. returning from the current function)
pub(super) fn next_out(&mut self) -> DebugCommandResult {
let start_call_stack = self.get_source_call_stack();
loop {
let result = self.next_into();
if !matches!(result, DebugCommandResult::Ok) {
return result;
}
let new_call_stack = self.get_source_call_stack();
if new_call_stack.len() < start_call_stack.len() {
return DebugCommandResult::Ok;
}
}
}

pub(super) fn cont(&mut self) -> DebugCommandResult {
loop {
let result = self.step_into_opcode();
Expand Down
Loading
Loading