Skip to main content

foundry_evm/inspectors/
custom_printer.rs

1//! Custom print inspector, it has step level information of execution.
2//! It is a great tool if some debugging is needed.
3
4use alloy_primitives::{Address, U256};
5use evm2::{
6    EvmTypesHost, Inspector,
7    interpreter::{Interpreter, Message, MessageResult, opcode::OpCode},
8};
9use foundry_common::sh_println;
10
11/// Custom print [Inspector], it has step level information of execution.
12///
13/// It is a great tool if some debugging is needed.
14#[derive(Clone, Debug, Default)]
15pub struct CustomPrintTracer;
16
17impl<T: EvmTypesHost> Inspector<T> for CustomPrintTracer {
18    fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) {
19        let opcode = interp.opcode();
20        let name = OpCode::new_or_unknown(opcode).as_str();
21        let gas = interp.gas();
22        let gas_remaining = gas.remaining();
23        let refunded = gas.refunded();
24
25        // Depth counts call frames from 1, as in geth struct logs.
26        let _ = sh_println!(
27            "depth:{}, PC:{}, gas:{:#x}({}), OPCODE: {:?}({:?})  refund:{:#x}({}) Stack:{:?}, Data size:{}",
28            interp.message().depth + 1,
29            interp.pc(),
30            gas_remaining,
31            gas_remaining,
32            name,
33            opcode,
34            refunded,
35            refunded,
36            interp.stack().as_slice(),
37            interp.memory().len(),
38        );
39    }
40
41    fn call(
42        &mut self,
43        _interp: &mut Interpreter<'_, '_, T>,
44        message: &mut Message<T>,
45    ) -> Option<MessageResult<T>> {
46        let _ = sh_println!(
47            "SM Address: {:?}, caller:{:?},target:{:?} is_static:{:?}, transfer:{:?}, input_size:{:?}",
48            message.code_address,
49            message.caller,
50            message.destination,
51            message.is_static(),
52            message.value,
53            message.input.len(),
54        );
55        None
56    }
57
58    fn create(
59        &mut self,
60        _interp: &mut Interpreter<'_, '_, T>,
61        message: &mut Message<T>,
62    ) -> Option<MessageResult<T>> {
63        let _ = sh_println!(
64            "CREATE CALL: caller:{:?}, scheme:{:?}, value:{:?}, init_code:{:?}, gas:{:?}",
65            message.caller,
66            message.kind,
67            message.value,
68            message.input,
69            message.gas_limit
70        );
71        None
72    }
73
74    fn selfdestruct(
75        &mut self,
76        contract: &Address,
77        target: &Address,
78        value: &U256,
79        _host: &mut T::Host<'_>,
80    ) {
81        let _ = sh_println!(
82            "SELFDESTRUCT: contract: {:?}, refund target: {:?}, value {:?}",
83            contract,
84            target,
85            value
86        );
87    }
88}