Skip to content

Commit

Permalink
Move all debugger code into a new crate under tooling/debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 4, 2023
1 parent 5a8f56d commit d37218b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 33 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"tooling/backend_interface",
"tooling/bb_abstraction_leaks",
"tooling/lsp",
"tooling/debugger",
"tooling/nargo",
"tooling/nargo_cli",
"tooling/nargo_toml",
Expand Down Expand Up @@ -52,6 +53,7 @@ nargo = { path = "tooling/nargo" }
nargo_cli = { path = "tooling/nargo_cli" }
nargo_toml = { path = "tooling/nargo_toml" }
noir_lsp = { path = "tooling/lsp" }
noir_debugger = { path = "tooling/debugger" }
noirc_abi = { path = "tooling/noirc_abi" }
bb_abstraction_leaks = { path = "tooling/bb_abstraction_leaks" }
noirc_driver = { path = "compiler/noirc_driver" }
Expand Down
16 changes: 16 additions & 0 deletions tooling/debugger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "noir_debugger"
description = "Debugger for Noir"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
acvm.workspace = true
nargo.workspace = true
noirc_printable_type.workspace = true
thiserror.workspace = true
reedline-repl-rs = "1.0.7"
55 changes: 33 additions & 22 deletions tooling/nargo/src/ops/debug.rs → tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use acvm::BlackBoxFunctionSolver;
use acvm::{acir::circuit::Circuit, acir::native_types::WitnessMap};
use acvm::acir::circuit::OpcodeLocation;

use crate::artifacts::debug::DebugArtifact;
use crate::errors::ExecutionError;
use crate::NargoError;
use nargo::artifacts::debug::DebugArtifact;
use nargo::errors::ExecutionError;
use nargo::NargoError;

use super::foreign_calls::ForeignCallExecutor;
use nargo::ops::ForeignCallExecutor;

use std::rc::Rc;
use std::sync::Mutex;

use thiserror::Error;

use reedline_repl_rs::clap::{
ArgMatches as ReplArgMatches,
Command as ReplCommand,
Expand All @@ -23,6 +25,17 @@ enum SolveResult {
Ok,
}

#[derive(Debug, Error)]
enum DebuggingError {
/// ACIR circuit execution error
#[error(transparent)]
ExecutionError(#[from] nargo::errors::ExecutionError),

/// Oracle handling error
#[error(transparent)]
ForeignCallError(#[from] noirc_printable_type::ForeignCallError),
}

struct ReplContext<'backend, B: BlackBoxFunctionSolver> {
acvm: Option<ACVM<'backend, B>>,
debug_artifact: DebugArtifact,
Expand All @@ -32,7 +45,7 @@ struct ReplContext<'backend, B: BlackBoxFunctionSolver> {
}

impl<'backend, B> ReplContext<'backend, B> where B: BlackBoxFunctionSolver {
fn step_opcode(&mut self) -> Result<SolveResult, NargoError> {
fn step_opcode(&mut self) -> Result<SolveResult, DebuggingError> {
// Assert messages are not a map due to https://github.com/noir-lang/acvm/issues/522
let assert_messages = &self.circuit.assert_messages;
let get_assert_message = |opcode_location| {
Expand All @@ -59,7 +72,7 @@ impl<'backend, B> ReplContext<'backend, B> where B: BlackBoxFunctionSolver {
_ => None,
};

Err(NargoError::ExecutionError(match call_stack {
Err(DebuggingError::ExecutionError(match call_stack {
Some(call_stack) => {
if let Some(assert_message) = get_assert_message(
call_stack.last().expect("Call stacks should not be empty"),
Expand Down Expand Up @@ -109,22 +122,20 @@ impl<'backend, B> ReplContext<'backend, B> where B: BlackBoxFunctionSolver {
}
}

impl From<reedline_repl_rs::Error> for NargoError {
impl From<reedline_repl_rs::Error> for DebuggingError {
fn from(_e: reedline_repl_rs::Error) -> Self {
NargoError::CompilationError
DebuggingError::ExecutionError(ExecutionError::Halted)
}
}


// impl fmt::Display for NargoError {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// match self {
// NargoError::CompilationError => write!(f, "Compilation Error"),
// NargoError::ExecutionError(e) => write!(f, "Execution Error: {}", e),
// NargoError::ForeignCallError(e) => write!(f, "Foreign call Error: {}", e),
// }
// }
// }
impl From<nargo::errors::NargoError> for DebuggingError {
fn from(e: nargo::errors::NargoError) -> Self {
match e {
NargoError::ForeignCallError(e1) => DebuggingError::ForeignCallError(e1),
_ => DebuggingError::ExecutionError(ExecutionError::Halted),
}
}
}

pub fn debug_circuit<B: BlackBoxFunctionSolver>(
blackbox_solver: &B,
Expand Down Expand Up @@ -167,7 +178,7 @@ pub fn debug_circuit<B: BlackBoxFunctionSolver>(
Ok(solved_witness)
}

fn step_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, NargoError> {
fn step_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, DebuggingError> {
let mut c = context.lock().unwrap();
c.show_current_vm_status();
match c.step_opcode()? {
Expand All @@ -176,7 +187,7 @@ fn step_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut
}
}

fn continue_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, NargoError> {
fn continue_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, DebuggingError> {
let mut c = context.lock().unwrap();
c.show_current_vm_status();
println!("(Continuing execution...)");
Expand All @@ -189,7 +200,7 @@ fn continue_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &
Ok(Some("Ok".to_string()))
}

fn quit_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, NargoError> {
fn quit_command<B: BlackBoxFunctionSolver>(_args: ReplArgMatches, context: &mut Rc<Mutex<ReplContext<B>>>) -> Result<Option<String>, DebuggingError> {
context.lock().unwrap().show_current_vm_status();
Err(NargoError::ExecutionError(ExecutionError::Halted))
Err(DebuggingError::ExecutionError(ExecutionError::Halted))
}
3 changes: 1 addition & 2 deletions tooling/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ iter-extended.workspace = true
serde.workspace = true
thiserror.workspace = true
base64.workspace = true
codespan-reporting.workspace = true
reedline-repl-rs = "1.0.7"
codespan-reporting.workspace = true
4 changes: 2 additions & 2 deletions tooling/nargo/src/ops/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ impl MockedCall {
}

#[derive(Debug, Default)]
pub(crate) struct ForeignCallExecutor {
pub struct ForeignCallExecutor {
/// Mocks have unique ids used to identify them in Noir, allowing to update or remove them.
last_mock_id: usize,
/// The registered mocks
mocked_responses: Vec<MockedCall>,
}

impl ForeignCallExecutor {
pub(crate) fn execute(
pub fn execute(
&mut self,
foreign_call: &ForeignCallWaitInfo,
show_output: bool,
Expand Down
3 changes: 1 addition & 2 deletions tooling/nargo/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pub use self::execute::execute_circuit;
pub use self::debug::debug_circuit;
pub use self::optimize::{optimize_contract, optimize_program};
pub use self::test::{run_test, TestStatus};
pub use self::foreign_calls::ForeignCallExecutor;

mod execute;
mod debug;
mod foreign_calls;
mod optimize;
mod test;
2 changes: 1 addition & 1 deletion tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ iter-extended.workspace = true
nargo.workspace = true
nargo_toml.workspace = true
noir_lsp.workspace = true
noir_debugger.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_abi.workspace = true
Expand All @@ -36,7 +37,6 @@ serde.workspace = true
serde_json.workspace = true
prettytable-rs = "0.10"
rayon = "1.7.0"
reedline-repl-rs = "1.0.7"
thiserror.workspace = true
tower.workspace = true
async-lsp = { version = "0.0.5", default-features = false, features = [
Expand Down
4 changes: 3 additions & 1 deletion tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use noirc_abi::InputMap;
use noirc_driver::{CompileOptions, CompiledProgram};
use noirc_frontend::graph::CrateName;

use noir_debugger;

use super::compile_cmd::compile_bin_package;
use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::NargoConfig;
Expand Down Expand Up @@ -106,7 +108,7 @@ pub(crate) fn debug_program(
file_map: compiled_program.file_map.clone(),
};

let solved_witness_err = nargo::ops::debug_circuit(
let solved_witness_err = noir_debugger::debug_circuit(
&blackbox_solver,
compiled_program.circuit.clone(),
debug_artifact,
Expand Down
1 change: 0 additions & 1 deletion tooling/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub(crate) fn start_cli() -> eyre::Result<()> {
NargoCommand::Compile(args) => compile_cmd::run(&backend, args, config),
NargoCommand::Debug(args) => debug_cmd::run(&backend, args, config),
NargoCommand::Execute(args) => execute_cmd::run(&backend, args, config),
NargoCommand::Debug(args) => debug_cmd::run(&backend, args, config),
NargoCommand::Prove(args) => prove_cmd::run(&backend, args, config),
NargoCommand::Verify(args) => verify_cmd::run(&backend, args, config),
NargoCommand::Test(args) => test_cmd::run(&backend, args, config),
Expand Down

0 comments on commit d37218b

Please sign in to comment.