From 390bb643fdfcba8c721bf88d7d3811f4ca570cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Tue, 3 Oct 2023 22:39:03 -0400 Subject: [PATCH] Move all debugger code into a new crate under tooling/debugger --- Cargo.lock | 12 ++++ Cargo.toml | 2 + tooling/debugger/Cargo.toml | 16 ++++++ .../src/ops/debug.rs => debugger/src/lib.rs} | 55 +++++++++++-------- tooling/nargo/src/ops/foreign_calls.rs | 4 +- tooling/nargo/src/ops/mod.rs | 3 +- tooling/nargo_cli/Cargo.toml | 2 +- tooling/nargo_cli/src/cli/debug_cmd.rs | 4 +- tooling/nargo_cli/src/cli/mod.rs | 1 - 9 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 tooling/debugger/Cargo.toml rename tooling/{nargo/src/ops/debug.rs => debugger/src/lib.rs} (84%) diff --git a/Cargo.lock b/Cargo.lock index 347e0b0521f..649c25a65c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2323,6 +2323,7 @@ dependencies = [ "iter-extended", "nargo", "nargo_toml", + "noir_debugger", "noir_lsp", "noirc_abi", "noirc_driver", @@ -2372,6 +2373,17 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "noir_debugger" +version = "0.15.0" +dependencies = [ + "acvm", + "nargo", + "noirc_printable_type", + "reedline-repl-rs", + "thiserror", +] + [[package]] name = "noir_lsp" version = "0.16.0" diff --git a/Cargo.toml b/Cargo.toml index dcd2c20d195..c79f4282e9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "tooling/backend_interface", "tooling/bb_abstraction_leaks", "tooling/lsp", + "tooling/debugger", "tooling/nargo", "tooling/nargo_cli", "tooling/nargo_toml", @@ -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" } diff --git a/tooling/debugger/Cargo.toml b/tooling/debugger/Cargo.toml new file mode 100644 index 00000000000..319950b526f --- /dev/null +++ b/tooling/debugger/Cargo.toml @@ -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" diff --git a/tooling/nargo/src/ops/debug.rs b/tooling/debugger/src/lib.rs similarity index 84% rename from tooling/nargo/src/ops/debug.rs rename to tooling/debugger/src/lib.rs index 7630254925e..fdb07ae86bd 100644 --- a/tooling/nargo/src/ops/debug.rs +++ b/tooling/debugger/src/lib.rs @@ -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, @@ -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>, debug_artifact: DebugArtifact, @@ -32,7 +45,7 @@ struct ReplContext<'backend, B: BlackBoxFunctionSolver> { } impl<'backend, B> ReplContext<'backend, B> where B: BlackBoxFunctionSolver { - fn step_opcode(&mut self) -> Result { + fn step_opcode(&mut self) -> Result { // 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| { @@ -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"), @@ -109,22 +122,20 @@ impl<'backend, B> ReplContext<'backend, B> where B: BlackBoxFunctionSolver { } } -impl From for NargoError { +impl From 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 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( blackbox_solver: &B, @@ -167,7 +178,7 @@ pub fn debug_circuit( Ok(solved_witness) } -fn step_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, NargoError> { +fn step_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, DebuggingError> { let mut c = context.lock().unwrap(); c.show_current_vm_status(); match c.step_opcode()? { @@ -176,7 +187,7 @@ fn step_command(_args: ReplArgMatches, context: &mut } } -fn continue_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, NargoError> { +fn continue_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, DebuggingError> { let mut c = context.lock().unwrap(); c.show_current_vm_status(); println!("(Continuing execution...)"); @@ -189,7 +200,7 @@ fn continue_command(_args: ReplArgMatches, context: & Ok(Some("Ok".to_string())) } -fn quit_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, NargoError> { +fn quit_command(_args: ReplArgMatches, context: &mut Rc>>) -> Result, DebuggingError> { context.lock().unwrap().show_current_vm_status(); - Err(NargoError::ExecutionError(ExecutionError::Halted)) + Err(DebuggingError::ExecutionError(ExecutionError::Halted)) } diff --git a/tooling/nargo/src/ops/foreign_calls.rs b/tooling/nargo/src/ops/foreign_calls.rs index e44ab1732c9..4d20a0bd4f0 100644 --- a/tooling/nargo/src/ops/foreign_calls.rs +++ b/tooling/nargo/src/ops/foreign_calls.rs @@ -89,7 +89,7 @@ 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 @@ -97,7 +97,7 @@ pub(crate) struct ForeignCallExecutor { } impl ForeignCallExecutor { - pub(crate) fn execute( + pub fn execute( &mut self, foreign_call: &ForeignCallWaitInfo, show_output: bool, diff --git a/tooling/nargo/src/ops/mod.rs b/tooling/nargo/src/ops/mod.rs index 16933a8217c..ab0aaf6c37d 100644 --- a/tooling/nargo/src/ops/mod.rs +++ b/tooling/nargo/src/ops/mod.rs @@ -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; diff --git a/tooling/nargo_cli/Cargo.toml b/tooling/nargo_cli/Cargo.toml index bd9f14b035c..9bb37f6429c 100644 --- a/tooling/nargo_cli/Cargo.toml +++ b/tooling/nargo_cli/Cargo.toml @@ -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 @@ -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 = [ diff --git a/tooling/nargo_cli/src/cli/debug_cmd.rs b/tooling/nargo_cli/src/cli/debug_cmd.rs index 8740860c172..db1b75428c5 100644 --- a/tooling/nargo_cli/src/cli/debug_cmd.rs +++ b/tooling/nargo_cli/src/cli/debug_cmd.rs @@ -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; @@ -105,7 +107,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, diff --git a/tooling/nargo_cli/src/cli/mod.rs b/tooling/nargo_cli/src/cli/mod.rs index f3a2fe90b67..0324c7e8848 100644 --- a/tooling/nargo_cli/src/cli/mod.rs +++ b/tooling/nargo_cli/src/cli/mod.rs @@ -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),