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

Add debug feature #3

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 39 additions & 6 deletions src/debug/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::interpreter::ProgramState;

use fuel_asm::Word;
use fuel_asm::{Opcode, Word};
use fuel_tx::ContractAddress;

use std::collections::{HashMap, HashSet};
Expand All @@ -12,10 +12,31 @@ pub struct Breakpoint {
}

impl Breakpoint {
pub const fn new(contract: ContractAddress, pc: Word) -> Self {
const fn raw(contract: ContractAddress, pc: Word) -> Self {
Self { contract, pc }
}

/// Create a new contract breakpoint
///
/// The `$pc` is provided in op count and internally is multiplied by the op
/// size. Also, the op count is always relative to `$is` so it should
/// consider only the bytecode of the contract.
pub const fn new(contract: ContractAddress, pc: Word) -> Self {
let pc = pc * (Opcode::BYTES_SIZE as Word);

Self::raw(contract, pc)
}

/// Create a new script breakpoint
///
/// The `$pc` is provided in op count and internally is multiplied by the op
/// size
pub fn script(pc: Word) -> Self {
let contract = Default::default();

Self::new(contract, pc)
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
}

pub const fn contract(&self) -> &ContractAddress {
&self.contract
}
Expand Down Expand Up @@ -50,6 +71,13 @@ impl DebugEval {
_ => false,
}
}

pub const fn breakpoint(&self) -> Option<&Breakpoint> {
match self {
Self::Breakpoint(b) => Some(b),
_ => None,
}
}
}

#[derive(Debug, Default, Clone)]
Expand All @@ -59,7 +87,10 @@ pub struct Debugger {
}

impl Debugger {
pub fn set_breakpoint(&mut self, contract: ContractAddress, pc: Word) {
pub fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
let contract = *breakpoint.contract();
let pc = breakpoint.pc();

self.breakpoints
.get_mut(&contract)
.map(|set| set.insert(pc))
Expand All @@ -73,8 +104,10 @@ impl Debugger {
});
}

pub fn remove_breakpoint(&mut self, contract: &ContractAddress, pc: Word) {
self.breakpoints.get_mut(contract).map(|set| set.remove(&pc));
pub fn remove_breakpoint(&mut self, breakpoint: &Breakpoint) {
self.breakpoints
.get_mut(breakpoint.contract())
.map(|set| set.remove(&breakpoint.pc()));
}

pub fn eval_state(&mut self, contract: ContractAddress, pc: Word) -> DebugEval {
Expand All @@ -85,7 +118,7 @@ impl Debugger {
.map(|set| set.get(&pc))
.flatten()
.map(|_| {
let breakpoint = Breakpoint::new(contract, pc);
let breakpoint = Breakpoint::raw(contract, pc);

match last_state {
Some(s) if s == breakpoint => DebugEval::Continue,
Expand Down
22 changes: 6 additions & 16 deletions src/interpreter/debug.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
use super::{Interpreter, ProgramState};
use crate::consts::*;
use crate::debug::DebugEval;

use fuel_asm::Word;
use fuel_tx::ContractAddress;
use crate::debug::{Breakpoint, DebugEval};

impl<S> Interpreter<S> {
pub fn set_breakpoint(&mut self, contract: Option<ContractAddress>, pc: Word) {
let contract = contract.unwrap_or_default();

self.debugger.set_breakpoint(contract, pc)
pub fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
self.debugger.set_breakpoint(breakpoint)
}

pub fn remove_breakpoint(&mut self, contract: Option<ContractAddress>, pc: Word) {
let contract = contract.unwrap_or_default();

self.debugger.remove_breakpoint(&contract, pc)
pub fn remove_breakpoint(&mut self, breakpoint: &Breakpoint) {
self.debugger.remove_breakpoint(breakpoint)
}

pub fn eval_debugger_state(&mut self) -> DebugEval {
let debugger = &mut self.debugger;

let contract = self.frames.last().map(|f| f.to()).copied();
let pc = match &contract {
Some(_) => self.registers[REG_PC].saturating_sub(self.registers[REG_IS]),
None => self.registers[REG_PC],
};
let pc = self.registers[REG_PC].saturating_sub(self.registers[REG_IS]);

// Default contract address maps to unset contract target
let contract = contract.unwrap_or_default();
Expand Down
13 changes: 10 additions & 3 deletions src/interpreter/executors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,23 @@ where

#[cfg(feature = "debug")]
pub fn resume(&mut self) -> Result<ProgramState, ExecuteError> {
match self
let state = self
.debugger_last_state()
.ok_or(ExecuteError::DebugStateNotInitialized)?
{
.ok_or(ExecuteError::DebugStateNotInitialized)?;

let state = match state {
ProgramState::Return(w) => Ok(ProgramState::Return(w)),

ProgramState::RunProgram(_) => self.run_program(),

ProgramState::VerifyPredicate(_) => unimplemented!(),
}?;

if state.is_debug() {
self.debugger_set_last_state(state.clone());
}

Ok(state)
}

pub fn run_program(&mut self) -> Result<ProgramState, ExecuteError> {
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ pub mod interpreter;
pub mod prelude {
pub use crate::data::{InterpreterStorage, MemoryStorage, Storage};
pub use crate::debug::Debugger;
pub use crate::interpreter::{Call, CallFrame, Contract, ExecuteError, Interpreter, LogEvent, MemoryRange};
pub use crate::interpreter::{
Call, CallFrame, Contract, ExecuteError, Interpreter, LogEvent, MemoryRange, ProgramState,
};
pub use fuel_asm::{Immediate06, Immediate12, Immediate18, Immediate24, Opcode, RegisterId, Word};
pub use fuel_tx::{
bytes::{Deserializable, SerializableVec, SizedBytes},
Address, Color, ContractAddress, Hash, Input, Output, Salt, Transaction, ValidationError, Witness,
};

#[cfg(feature = "debug")]
pub use crate::debug::{Breakpoint, DebugEval};
}
61 changes: 61 additions & 0 deletions tests/interpreter/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::program_to_bytes;
use fuel_vm::consts::*;
use fuel_vm::prelude::*;

#[test]
fn breakpoint_script() {
let storage = MemoryStorage::default();
let mut vm = Interpreter::with_storage(storage);

let gas_price = 10;
let gas_limit = 1_000_000;
let maturity = 100;

let script = vec![
Opcode::ADDI(0x10, REG_ZERO, 8),
Opcode::ADDI(0x11, REG_ZERO, 16),
Opcode::ADDI(0x12, REG_ZERO, 32),
Opcode::ADDI(0x13, REG_ZERO, 64),
Opcode::ADDI(0x14, REG_ZERO, 128),
Opcode::RET(0x10),
];
let script = program_to_bytes(script.as_slice());

let tx = Transaction::script(gas_price, gas_limit, maturity, script, vec![], vec![], vec![], vec![]);

vm.init(tx).expect("Failed to init VM!");

let suite = vec![
(
Breakpoint::script(0),
vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(2),
vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(3),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(5),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)],
),
];

suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b));
let state = vm.run().expect("Failed to execute script!");

suite.into_iter().fold(state, |state, (breakpoint, registers)| {
let debug = state.debug_ref().expect("Expected breakpoint");
let b = debug.breakpoint().expect("State without expected breakpoint");

assert_eq!(&breakpoint, b);
registers.into_iter().for_each(|(r, w)| {
assert_eq!(w, vm.registers()[r]);
});

vm.resume().expect("Failed to resume")
});
}
3 changes: 3 additions & 0 deletions tests/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mod flow;
mod memory;
mod predicate;

#[cfg(feature = "debug")]
mod debug;

pub use super::common;

pub fn program_to_bytes(program: &[Opcode]) -> Vec<u8> {
Expand Down