1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::state::{
    Breakpoint,
    DebugEval,
    ProgramState,
};

use fuel_types::{
    ContractId,
    Word,
};

use hashbrown::{
    HashMap,
    HashSet,
};

/// Debugger implementation for the VM.
#[derive(Debug, Default, Clone)]
pub struct Debugger {
    /// Debugger is active and used.
    is_active: bool,
    /// Single-stepping mode triggers a breakpoint after each instruction
    single_stepping: bool,
    breakpoints: HashMap<ContractId, HashSet<Word>>,
    last_state: Option<ProgramState>,
}

impl Debugger {
    /// Returns `true` if the `Debugger` is active and used.
    pub const fn is_active(&self) -> bool {
        self.is_active
    }

    /// Get single-stepping mode
    pub const fn single_stepping(&self) -> bool {
        self.single_stepping
    }

    /// Set single-stepping mode
    pub fn set_single_stepping(&mut self, single_stepping: bool) {
        self.is_active = true;
        self.single_stepping = single_stepping;
    }

    /// Set a new breakpoint in the provided location.
    pub fn clear_breakpoints(&mut self) {
        self.breakpoints.clear();
    }

    /// Set a new breakpoint in the provided location.
    pub fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
        self.is_active = true;
        let contract = *breakpoint.contract();
        let pc = breakpoint.pc();

        self.breakpoints
            .get_mut(&contract)
            .map(|set| set.insert(pc))
            .map(|_| ())
            .unwrap_or_else(|| {
                let mut set = HashSet::new();

                set.insert(pc);

                self.breakpoints.insert(contract, set);
            });
    }

    /// Remove a breakpoint, if existent.
    pub fn remove_breakpoint(&mut self, breakpoint: &Breakpoint) {
        self.is_active = true;
        self.breakpoints
            .get_mut(breakpoint.contract())
            .map(|set| set.remove(&breakpoint.pc()));
    }

    /// Evaluate the current state of the interpreter whether or not a
    /// breakpoint was reached.
    pub fn eval_state(&mut self, contract: Option<&ContractId>, pc: Word) -> DebugEval {
        // Default contract address maps to unset contract target
        let contract = contract.copied().unwrap_or_default();
        let last_state = self.last_state.take();

        let current = Breakpoint::raw(contract, pc);

        if self.single_stepping {
            return match last_state {
                Some(s) if s == current => DebugEval::Continue,
                _ => current.into(),
            }
        }

        self.breakpoints
            .get(&contract)
            .and_then(|set| set.get(&pc))
            .map(|_| match last_state {
                Some(s) if s == current => DebugEval::Continue,
                _ => current.into(),
            })
            .unwrap_or_default()
    }

    /// Overwrite the last known state of the VM.
    pub fn set_last_state(&mut self, state: ProgramState) {
        self.is_active = true;
        self.last_state.replace(state);
    }

    /// Retried the last state of execution; return `None` if the VM was never
    /// executed.
    pub const fn last_state(&self) -> &Option<ProgramState> {
        &self.last_state
    }
}