Skip to content

Commit

Permalink
refactor to avoid exceeding # of function args
Browse files Browse the repository at this point in the history
  • Loading branch information
anaPerezGhiglia committed Feb 26, 2025
1 parent ed33a14 commit a233ab3
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 129 deletions.
15 changes: 6 additions & 9 deletions tooling/debugger/src/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::path::PathBuf;

use acvm::acir::circuit::brillig::BrilligBytecode;
use acvm::acir::circuit::Circuit;
use acvm::acir::native_types::WitnessMap;
use acvm::{BlackBoxFunctionSolver, FieldElement};
use nargo::PrintOutput;
Expand Down Expand Up @@ -63,16 +61,15 @@ impl<'a, R: Read, W: Write, B: BlackBoxFunctionSolver<FieldElement>> DapSession<
pub fn new(
server: Server<R, W>,
solver: &'a B,
circuits: &'a [Circuit<FieldElement>],
program: &'a CompiledProgram,
debug_artifact: &'a DebugArtifact,
initial_witness: WitnessMap<FieldElement>,
unconstrained_functions: &'a [BrilligBytecode<FieldElement>],
root_path: Option<PathBuf>,
package_name: String,
) -> Self {
let context = DebugContext::new(
solver,
circuits,
&program.program.functions,
debug_artifact,
initial_witness,
Box::new(DefaultDebugForeignCallExecutor::from_artifact(
Expand All @@ -82,7 +79,7 @@ impl<'a, R: Read, W: Write, B: BlackBoxFunctionSolver<FieldElement>> DapSession<
root_path,
package_name,
)),
unconstrained_functions,
&program.program.unconstrained_functions,
);
Self {
server,
Expand Down Expand Up @@ -620,14 +617,14 @@ pub fn run_session<R: Read, W: Write, B: BlackBoxFunctionSolver<FieldElement>>(
root_path: Option<PathBuf>,
package_name: String,
) -> Result<(), ServerError> {
let debug_artifact = DebugArtifact { debug_symbols: program.debug, file_map: program.file_map };
let debug_artifact =
DebugArtifact { debug_symbols: program.debug.clone(), file_map: program.file_map.clone() };
let mut session = DapSession::new(
server,
solver,
&program.program.functions,
&program,
&debug_artifact,
initial_witness,
&program.program.unconstrained_functions,
root_path,
package_name,
);
Expand Down
234 changes: 123 additions & 111 deletions tooling/debugger/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,125 +65,137 @@ pub(super) enum DebugCommandAPI {
Cont,
}

pub(super) fn start_debugger<'a>(
command_rx: Receiver<DebugCommandAPI>,
result_tx: Sender<DebugCommandAPIResult>,
circuits: Vec<Circuit<FieldElement>>,
debug_artifact: &'a DebugArtifact,
initial_witness: WitnessMap<FieldElement>,
foreign_call_executor: Box<dyn DebugForeignCallExecutor + 'a>,
unconstrained_functions: Vec<BrilligBytecode<FieldElement>>,
pedantic_solving: bool,
) {
let blackbox_solver = Bn254BlackBoxSolver(pedantic_solving);
let mut context = DebugContext::new(
&blackbox_solver,
&circuits,
debug_artifact,
initial_witness.clone(),
foreign_call_executor,
&unconstrained_functions,
);
pub struct Debugger<'a> {
pub circuits: Vec<Circuit<FieldElement>>,
pub debug_artifact: &'a DebugArtifact,
pub initial_witness: WitnessMap<FieldElement>,
pub unconstrained_functions: Vec<BrilligBytecode<FieldElement>>,
pub pedantic_solving: bool,
}

impl<'a> Debugger<'a> {
pub(super) fn start_debugging(
&self,
command_rx: Receiver<DebugCommandAPI>,
result_tx: Sender<DebugCommandAPIResult>,
foreign_call_executor: Box<dyn DebugForeignCallExecutor + 'a>,
) {
let blackbox_solver = Bn254BlackBoxSolver(self.pedantic_solving);
let mut context = DebugContext::new(
&blackbox_solver,
&self.circuits,
self.debug_artifact,
self.initial_witness.clone(),
foreign_call_executor,
&self.unconstrained_functions,
);

println!("Debugger ready for receiving messages..");
loop {
// recv blocks until it receives message
if let Ok(received) = command_rx.recv() {
let result = match received {
DebugCommandAPI::GetCurrentDebugLocation => {
DebugCommandAPIResult::DebugLocation(context.get_current_debug_location())
}
DebugCommandAPI::GetOpcodes => {
DebugCommandAPIResult::Opcodes(context.get_opcodes().to_owned())
}
DebugCommandAPI::GetOpcodesOfCircuit(circuit_id) => DebugCommandAPIResult::Opcodes(
context.get_opcodes_of_circuit(circuit_id).to_owned(),
),
DebugCommandAPI::GetSourceLocationForDebugLocation(debug_location) => {
DebugCommandAPIResult::Locations(
context.get_source_location_for_debug_location(&debug_location),
)
}
DebugCommandAPI::GetCallStack => {
DebugCommandAPIResult::DebugLocations(context.get_call_stack())
}
DebugCommandAPI::IsBreakpointSet(debug_location) => {
DebugCommandAPIResult::Bool(context.is_breakpoint_set(&debug_location))
}
DebugCommandAPI::IsValidDebugLocation(debug_location) => {
DebugCommandAPIResult::Bool(context.is_valid_debug_location(&debug_location))
}
DebugCommandAPI::AddBreakpoint(debug_location) => {
DebugCommandAPIResult::Bool(context.add_breakpoint(debug_location))
}
DebugCommandAPI::DeleteBreakpoint(debug_location) => {
DebugCommandAPIResult::Bool(context.delete_breakpoint(&debug_location))
}
DebugCommandAPI::Restart => {
context.restart();
DebugCommandAPIResult::Unit(())
}
DebugCommandAPI::GetWitnessMap => {
DebugCommandAPIResult::WitnessMap(context.get_witness_map().clone())
}
DebugCommandAPI::IsExecutingBrillig => {
DebugCommandAPIResult::Bool(context.is_executing_brillig())
}
DebugCommandAPI::GetBrilligMemory => DebugCommandAPIResult::MemoryValue(
context.get_brillig_memory().map(|values| values.to_vec()),
),
DebugCommandAPI::WriteBrilligMemory(ptr, value, bit_size) => {
context.write_brillig_memory(ptr, value, bit_size);
DebugCommandAPIResult::Unit(())
}
DebugCommandAPI::OverwriteWitness(witness, value) => {
DebugCommandAPIResult::Field(context.overwrite_witness(witness, value))
}
println!("Debugger ready for receiving messages..");
loop {
// recv blocks until it receives message
if let Ok(received) = command_rx.recv() {
let result = match received {
DebugCommandAPI::GetCurrentDebugLocation => {
DebugCommandAPIResult::DebugLocation(context.get_current_debug_location())
}
DebugCommandAPI::GetOpcodes => {
DebugCommandAPIResult::Opcodes(context.get_opcodes().to_owned())
}
DebugCommandAPI::GetOpcodesOfCircuit(circuit_id) => {
DebugCommandAPIResult::Opcodes(
context.get_opcodes_of_circuit(circuit_id).to_owned(),
)
}
DebugCommandAPI::GetSourceLocationForDebugLocation(debug_location) => {
DebugCommandAPIResult::Locations(
context.get_source_location_for_debug_location(&debug_location),
)
}
DebugCommandAPI::GetCallStack => {
DebugCommandAPIResult::DebugLocations(context.get_call_stack())
}
DebugCommandAPI::IsBreakpointSet(debug_location) => {
DebugCommandAPIResult::Bool(context.is_breakpoint_set(&debug_location))
}
DebugCommandAPI::IsValidDebugLocation(debug_location) => {
DebugCommandAPIResult::Bool(
context.is_valid_debug_location(&debug_location),
)
}
DebugCommandAPI::AddBreakpoint(debug_location) => {
DebugCommandAPIResult::Bool(context.add_breakpoint(debug_location))
}
DebugCommandAPI::DeleteBreakpoint(debug_location) => {
DebugCommandAPIResult::Bool(context.delete_breakpoint(&debug_location))
}
DebugCommandAPI::Restart => {
context.restart();
DebugCommandAPIResult::Unit(())
}
DebugCommandAPI::GetWitnessMap => {
DebugCommandAPIResult::WitnessMap(context.get_witness_map().clone())
}
DebugCommandAPI::IsExecutingBrillig => {
DebugCommandAPIResult::Bool(context.is_executing_brillig())
}
DebugCommandAPI::GetBrilligMemory => DebugCommandAPIResult::MemoryValue(
context.get_brillig_memory().map(|values| values.to_vec()),
),
DebugCommandAPI::WriteBrilligMemory(ptr, value, bit_size) => {
context.write_brillig_memory(ptr, value, bit_size);
DebugCommandAPIResult::Unit(())
}
DebugCommandAPI::OverwriteWitness(witness, value) => {
DebugCommandAPIResult::Field(context.overwrite_witness(witness, value))
}

DebugCommandAPI::GetVariables => DebugCommandAPIResult::Variables(
context.get_variables().iter().map(DebugStackFrame::from).collect(),
),
DebugCommandAPI::IsSolved => DebugCommandAPIResult::Bool(context.is_solved()),
DebugCommandAPI::StepAcirOpcode => {
DebugCommandAPIResult::DebugCommandResult(context.step_acir_opcode())
}
DebugCommandAPI::StepIntoOpcode => {
DebugCommandAPIResult::DebugCommandResult(context.step_into_opcode())
}
DebugCommandAPI::NextInto => {
DebugCommandAPIResult::DebugCommandResult(context.next_into())
}
DebugCommandAPI::NextOver => {
DebugCommandAPIResult::DebugCommandResult(context.next_over())
}
DebugCommandAPI::NextOut => {
DebugCommandAPIResult::DebugCommandResult(context.next_out())
}
DebugCommandAPI::Cont => DebugCommandAPIResult::DebugCommandResult(context.cont()),
DebugCommandAPI::FindOpcodeAtCurrentFileLine(line) => {
DebugCommandAPIResult::DebugLocation(
context.find_opcode_at_current_file_line(line),
)
}
DebugCommandAPI::Finalize => {
let witness_stack = context.finalize();
let _ = result_tx.send(DebugCommandAPIResult::WitnessStack(witness_stack));
// We need to stop the 'event loop' since `finalize` consumes the context
DebugCommandAPI::GetVariables => DebugCommandAPIResult::Variables(
context.get_variables().iter().map(DebugStackFrame::from).collect(),
),
DebugCommandAPI::IsSolved => DebugCommandAPIResult::Bool(context.is_solved()),
DebugCommandAPI::StepAcirOpcode => {
DebugCommandAPIResult::DebugCommandResult(context.step_acir_opcode())
}
DebugCommandAPI::StepIntoOpcode => {
DebugCommandAPIResult::DebugCommandResult(context.step_into_opcode())
}
DebugCommandAPI::NextInto => {
DebugCommandAPIResult::DebugCommandResult(context.next_into())
}
DebugCommandAPI::NextOver => {
DebugCommandAPIResult::DebugCommandResult(context.next_over())
}
DebugCommandAPI::NextOut => {
DebugCommandAPIResult::DebugCommandResult(context.next_out())
}
DebugCommandAPI::Cont => {
DebugCommandAPIResult::DebugCommandResult(context.cont())
}
DebugCommandAPI::FindOpcodeAtCurrentFileLine(line) => {
DebugCommandAPIResult::DebugLocation(
context.find_opcode_at_current_file_line(line),
)
}
DebugCommandAPI::Finalize => {
let witness_stack = context.finalize();
let _ = result_tx.send(DebugCommandAPIResult::WitnessStack(witness_stack));
// We need to stop the 'event loop' since `finalize` consumes the context
drop(result_tx);
drop(command_rx);
break;
}
};
let Ok(()) = result_tx.send(result) else {
drop(result_tx);
drop(command_rx);
break;
}
};
let Ok(()) = result_tx.send(result) else {
};
} else {
println!("Upstream channel closed. Terminating debugger");
drop(result_tx);
drop(command_rx);
break;
};
} else {
println!("Upstream channel closed. Terminating debugger");
drop(result_tx);
drop(command_rx);
break;
}
}
}
}
16 changes: 7 additions & 9 deletions tooling/debugger/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::context::{DebugCommandResult, DebugLocation, DebugStackFrame};
use crate::debug::{start_debugger, DebugCommandAPI, DebugCommandAPIResult};
use crate::debug::{DebugCommandAPI, DebugCommandAPIResult, Debugger};

use acvm::acir::brillig::BitSize;
use acvm::acir::circuit::brillig::{BrilligBytecode, BrilligFunctionId};
Expand Down Expand Up @@ -578,16 +578,14 @@ pub fn run(
let (command_tx, command_rx) = mpsc::channel::<DebugCommandAPI>();
let (result_tx, result_rx) = mpsc::channel::<DebugCommandAPIResult>();
thread::spawn(move || {
start_debugger(
command_rx,
result_tx,
debugger_circuits,
&debugger_artifact,
let debugger = Debugger {
circuits: debugger_circuits,
debug_artifact: &debugger_artifact,
initial_witness,
foreign_call_executor,
debugger_unconstrained_functions,
unconstrained_functions: debugger_unconstrained_functions,
pedantic_solving,
);
};
debugger.start_debugging(command_rx, result_tx, foreign_call_executor);
});

let context = RefCell::new(ReplDebugger::new(
Expand Down

0 comments on commit a233ab3

Please sign in to comment.