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 a single stepping example #728

Merged
merged 1 commit into from
May 3, 2024
Merged
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
96 changes: 96 additions & 0 deletions fuel-vm/examples/single_step.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! This example shows how you can run the VM in single-stepping mode,
//! allowing for e.g. visualization of the execution state at each step.

use fuel_asm::{
op,
RawInstruction,
RegId,
};
use fuel_tx::{
ConsensusParameters,
TransactionBuilder,
};
use fuel_vm::{
interpreter::{
Interpreter,
NotSupportedEcal,
},
prelude::*,
};

fn get_next_instruction<S, Tx>(
vm: &Interpreter<S, Tx, NotSupportedEcal>,
) -> Option<Instruction> {
let pc = vm.registers()[RegId::PC];
let instruction = RawInstruction::from_be_bytes(vm.memory().read_bytes(pc).ok()?);
Instruction::try_from(instruction).ok()
}

fn main() {
let mut vm = Interpreter::<_, _, NotSupportedEcal>::with_memory_storage();
vm.set_single_stepping(true);

let script_data: Vec<u8> = file!().bytes().collect();
let script = vec![
op::movi(0x21, 5), // How many times to loop
op::addi(0x20, 0x20, 1), // Increment loop counter
op::jneb(0x20, 0x21, RegId::ZERO, 0), // Jump back to increment
op::ret(RegId::ONE),
]
.into_iter()
.collect();

let consensus_params = ConsensusParameters::standard();
let tx = TransactionBuilder::script(script, script_data)
.script_gas_limit(1_000_000)
.maturity(Default::default())
.add_random_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.expect("failed to generate a checked tx")
.into_ready(
0,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.expect("Failed to finalize tx");

let mut t = *vm.transact(tx).expect("panicked").state();

loop {
match t {
ProgramState::Return(r) => {
println!("done: returned {r:?}");
break;
}
ProgramState::ReturnData(r) => {
println!("done: returned data {r:?}");
break;
}
ProgramState::Revert(r) => {
println!("done: reverted {r:?}");
break;
}
ProgramState::RunProgram(d) => {
match d {
DebugEval::Breakpoint(bp) => {
println!(
"at {:>4} reg[0x20] = {:4}, next instruction: {}",
bp.pc(),
&vm.registers()[0x20],
get_next_instruction(&vm)
.map(|i| format!("{i:?}"))
.unwrap_or_else(|| "???".to_owned()),
);
}
DebugEval::Continue => {}
}
t = vm.resume().expect("panicked");
}
ProgramState::VerifyPredicate(d) => {
println!("paused on debugger {d:?} (in predicate)");
t = vm.resume().expect("panicked");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I feel like we should be using unwrap in all these places that call expect. expect is meant to explain why it's expected to be Ok. In all these cases we just say that it's failing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example, so I'm using expect to highlight that in real-world usage you're supposed to handle the errors.

}
}
}
}
Loading