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

fix(debugger): crash when stepping through locations spanning multiple lines #3920

Merged
merged 11 commits into from
Jan 2, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion tooling/debugger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ codespan-reporting.workspace = true
dap.workspace = true
easy-repl = "0.2.1"
owo-colors = "3"
serde_json.workspace = true
serde_json.workspace = true

[dev_dependencies]
tempfile.workspace = true
1 change: 1 addition & 0 deletions tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod context;
mod dap;
mod repl;
mod source_code_printer;

use std::io::{Read, Write};

Expand Down
83 changes: 2 additions & 81 deletions tooling/debugger/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
use easy_repl::{command, CommandStatus, Repl};
use std::cell::RefCell;

use codespan_reporting::files::Files;
use noirc_errors::Location;

use owo_colors::OwoColorize;

use std::ops::Range;
use crate::source_code_printer::print_source_code_location;

pub struct ReplDebugger<'a, B: BlackBoxFunctionSolver> {
context: DebugContext<'a, B>,
Expand Down Expand Up @@ -70,73 +65,7 @@
);
}
}
self.show_source_code_location(&location);
}
}
}

fn print_location_path(&self, loc: Location) {
let line_number = self.debug_artifact.location_line_number(loc).unwrap();
let column_number = self.debug_artifact.location_column_number(loc).unwrap();

println!(
"At {}:{line_number}:{column_number}",
self.debug_artifact.name(loc.file).unwrap()
);
}

fn show_source_code_location(&self, location: &OpcodeLocation) {
let locations = self.debug_artifact.debug_symbols[0].opcode_location(location);
let Some(locations) = locations else { return };
for loc in locations {
self.print_location_path(loc);

let loc_line_index = self.debug_artifact.location_line_index(loc).unwrap();

// How many lines before or after the location's line we
// print
let context_lines = 5;

let first_line_to_print =
if loc_line_index < context_lines { 0 } else { loc_line_index - context_lines };

let last_line_index = self.debug_artifact.last_line_index(loc).unwrap();
let last_line_to_print = std::cmp::min(loc_line_index + context_lines, last_line_index);

let source = self.debug_artifact.location_source_code(loc).unwrap();
for (current_line_index, line) in source.lines().enumerate() {
let current_line_number = current_line_index + 1;

if current_line_index < first_line_to_print {
// Ignore lines before range starts
continue;
} else if current_line_index == first_line_to_print && current_line_index > 0 {
// Denote that there's more lines before but we're not showing them
print_line_of_ellipsis(current_line_index);
}

if current_line_index > last_line_to_print {
// Denote that there's more lines after but we're not showing them,
// and stop printing
print_line_of_ellipsis(current_line_number);
break;
}

if current_line_index == loc_line_index {
// Highlight current location
let Range { start: loc_start, end: loc_end } =
self.debug_artifact.location_in_line(loc).unwrap();
println!(
"{:>3} {:2} {}{}{}",
current_line_number,
"->",
&line[0..loc_start].to_string().dimmed(),
&line[loc_start..loc_end],
&line[loc_end..].to_string().dimmed()
);
} else {
print_dimmed_line(current_line_number, line);
}
print_source_code_location(self.debug_artifact, &location);
}
}
}
Expand Down Expand Up @@ -384,14 +313,6 @@
}
}

fn print_line_of_ellipsis(line_number: usize) {
println!("{}", format!("{:>3} {}", line_number, "...").dimmed());
}

fn print_dimmed_line(line_number: usize, line: &str) {
println!("{}", format!("{:>3} {:2} {}", line_number, "", line).dimmed());
}

pub fn run<B: BlackBoxFunctionSolver>(
blackbox_solver: &B,
circuit: &Circuit,
Expand Down Expand Up @@ -526,7 +447,7 @@
},
)
.add(
"regset",

Check warning on line 450 in tooling/debugger/src/repl.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (regset)
command! {
"update a Brillig register with the given value",
(index: usize, value: String) => |index, value| {
Expand Down
Loading
Loading