Skip to content

Commit

Permalink
feat: implement next debug command to step through source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 16, 2023
1 parent 1d41d39 commit eb1a668
Showing 1 changed file with 67 additions and 21 deletions.
88 changes: 67 additions & 21 deletions tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,27 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
brillig_index, ip, opcodes[ip]
),
}
Self::show_source_code_location(&location, &self.debug_artifact);
self.show_source_code_location(&location);
}
}
}

fn show_source_code_location(location: &OpcodeLocation, debug_artifact: &DebugArtifact) {
let locations = debug_artifact.debug_symbols[0].opcode_location(location);
fn show_source_code_location(&self, location: &OpcodeLocation) {
let locations = self.source_location(location.into());
if let Some(locations) = locations {
for loc in locations {
let source = Files::source(debug_artifact, loc.file).unwrap();
let source = Files::source(&self.debug_artifact, loc.file).unwrap();
let start = loc.span.start() as usize;
let end = loc.span.end() as usize;
let line_index = Files::line_index(debug_artifact, loc.file, start).unwrap();
let line_number = Files::line_number(debug_artifact, loc.file, line_index).unwrap();
let line_index = Files::line_index(&self.debug_artifact, loc.file, start).unwrap();
let line_number =
Files::line_number(&self.debug_artifact, loc.file, line_index).unwrap();
let column_number =
Files::column_number(debug_artifact, loc.file, line_index, start).unwrap();
Files::column_number(&self.debug_artifact, loc.file, line_index, start)
.unwrap();
println!(
"At {}.nr:{line_number}:{column_number}",
Files::name(debug_artifact, loc.file).unwrap()
Files::name(&self.debug_artifact, loc.file).unwrap()
);
println!("\n{}\n", &source[start..end]);
}
Expand All @@ -164,22 +166,25 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
}
}

fn get_current_debug_locations(&self) -> Option<Vec<Location>> {
if let Some(location) = self.acvm.location() {
self.debug_artifact.debug_symbols[0].opcode_location(&location)
} else {
None
fn source_location(&self, opcode_location: &OpcodeLocation) -> Option<Vec<Location>> {
self.debug_artifact.debug_symbols[0].opcode_location(opcode_location)
}

fn get_current_source_location(&self) -> Option<Vec<Location>> {
match &self.acvm.location() {
None => None,
Some(ref location) => self.source_location(location),
}
}

fn display_sources(&self) {
let locations = self.get_current_debug_locations();
let location = self.get_current_source_location();

for (file_id, _) in &self.debug_artifact.file_map {
for file_id in self.debug_artifact.file_map.keys() {
let filename = Files::name(&self.debug_artifact, *file_id).unwrap();
let source = Files::source(&self.debug_artifact, *file_id).unwrap();
let marker_indices =
get_indices_for_locations(&self.debug_artifact, *file_id, &locations);
get_indices_for_locations(&self.debug_artifact, *file_id, &location);
println!("File {filename}.nr:");
print_with_line_numbers(source, marker_indices);
}
Expand Down Expand Up @@ -209,17 +214,42 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
self.handle_acvm_status(solver_status)
}

fn breakpoint_reached(&self) -> bool {
if let Some(location) = self.acvm.location() {
if self.breakpoints.contains(&location) {
println!("Stopped at breakpoint in opcode {}", location);
return true;
}
}
false
}

fn next(&mut self) -> Result<SolveResult, NargoError> {
let start_location = self.get_current_source_location();
loop {
match self.step_into_opcode()? {
SolveResult::Done => break,
SolveResult::Ok => {}
}
if self.breakpoint_reached() {
return Ok(SolveResult::Ok);
}
let new_location = self.get_current_source_location();
if matches!(new_location, Some(..)) && new_location != start_location {
return Ok(SolveResult::Ok);
}
}
Ok(SolveResult::Done)
}

fn cont(&mut self) -> Result<SolveResult, NargoError> {
loop {
match self.step_acir_opcode()? {
SolveResult::Done => break,
SolveResult::Ok => {}
}
if let Some(location) = self.acvm.location() {
if self.breakpoints.contains(&location) {
println!("Stopped at breakpoint in opcode {}", location);
return Ok(SolveResult::Ok);
}
if self.breakpoint_reached() {
return Ok(SolveResult::Ok);
}
}
Ok(SolveResult::Done)
Expand Down Expand Up @@ -305,6 +335,22 @@ pub fn debug_circuit<B: BlackBoxFunctionSolver>(
}
},
)
.add(
"next",
command! {
"step until a new source location is reached",
() => || {
if ref_context.borrow().finished() {
println!("Execution finished");
Ok(CommandStatus::Done)
} else {
let result = ref_context.borrow_mut().next().into_critical()?;
ref_context.borrow().show_current_vm_status();
handle_result(result)
}
}
},
)
.add(
"continue",
command! {
Expand Down

0 comments on commit eb1a668

Please sign in to comment.