Skip to content

Commit

Permalink
feat(debugger): REPL add breakpoint by sourcecode line (#2)
Browse files Browse the repository at this point in the history
* @smanilov solution

* Simplify solition by using context::find_opcode_for_source_location
  • Loading branch information
anaPerezGhiglia authored Jun 7, 2024
1 parent 6d3e732 commit e837add
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ impl<'a, B: BlackBoxFunctionSolver<FieldElement>> DebugContext<'a, B> {
.filter(|v: &Vec<Location>| !v.is_empty())
}

/// Returns the `FileId` of the file associated with the innermost function on the call stack.
pub(super) fn get_current_file(&mut self) -> Option<FileId> {
self.get_current_source_location()
.and_then(|locations| locations.last().map(|location| location.file))
}

/// Returns the (possible) stack of source locations corresponding to the
/// given opcode location. Due to compiler inlining it's possible for this
/// function to return multiple source locations. An empty vector means that
Expand Down
28 changes: 28 additions & 0 deletions tooling/debugger/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ impl<'a, B: BlackBoxFunctionSolver<FieldElement>> ReplDebugger<'a, B> {
}
}

fn add_breakpoint_at_line(&mut self, line_number: i64) {
let Some(current_file) = self.context.get_current_file() else {
println!("No current file.");
return;
};

let best_location =
self.context.find_opcode_for_source_location(&current_file, line_number);

match best_location {
Some(location) => {
println!("Added breakpoint at line {}", line_number);
self.add_breakpoint_at(location)
}
None => println!("No opcode at line {}", line_number),
}
}

fn delete_breakpoint_at(&mut self, location: OpcodeLocation) {
if self.context.delete_breakpoint(&location) {
println!("Breakpoint at opcode {location} deleted");
Expand Down Expand Up @@ -475,6 +493,16 @@ pub fn run<B: BlackBoxFunctionSolver<FieldElement>>(
}
},
)
.add(
"break",
command! {
"add a breakpoint at a line of the current file",
(line_number: i64) => |line_number| {
ref_context.borrow_mut().add_breakpoint_at_line(line_number);
Ok(CommandStatus::Done)
}
},
)
.add(
"break",
command! {
Expand Down

0 comments on commit e837add

Please sign in to comment.