foundry_debugger/
file_dumper.rs

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! The debug file dumper implementation.

use crate::{debugger::DebuggerContext, DebugNode};
use alloy_primitives::Address;
use eyre::Result;
use foundry_common::fs::write_json_file;
use foundry_compilers::{
    artifacts::sourcemap::{Jump, SourceElement},
    multi::MultiCompilerLanguage,
};
use foundry_evm_traces::debug::{ArtifactData, ContractSources, SourceData};
use serde::Serialize;
use std::{collections::HashMap, ops::Deref, path::PathBuf};

/// Generates and writes debugger dump in a json file.
pub struct FileDumper<'a> {
    /// Path to json file to write dump into.
    path: &'a PathBuf,
    /// Debugger context to generate dump for.
    debugger_context: &'a mut DebuggerContext,
}

impl<'a> FileDumper<'a> {
    pub fn new(path: &'a PathBuf, debugger_context: &'a mut DebuggerContext) -> Self {
        Self { path, debugger_context }
    }

    pub fn run(&mut self) -> Result<()> {
        let data = DebuggerDump::from(self.debugger_context);
        write_json_file(self.path, &data).unwrap();
        Ok(())
    }
}

/// Holds info of debugger dump.
#[derive(Serialize)]
struct DebuggerDump {
    contracts: ContractsDump,
    debug_arena: Vec<DebugNode>,
}

impl DebuggerDump {
    fn from(debugger_context: &DebuggerContext) -> Self {
        Self {
            contracts: ContractsDump::new(debugger_context),
            debug_arena: debugger_context.debug_arena.clone(),
        }
    }
}

#[derive(Serialize)]
pub struct SourceElementDump {
    offset: u32,
    length: u32,
    index: i32,
    jump: u32,
    modifier_depth: u32,
}

impl SourceElementDump {
    pub fn new(v: &SourceElement) -> Self {
        Self {
            offset: v.offset(),
            length: v.length(),
            index: v.index_i32(),
            jump: match v.jump() {
                Jump::In => 0,
                Jump::Out => 1,
                Jump::Regular => 2,
            },
            modifier_depth: v.modifier_depth(),
        }
    }
}

#[derive(Serialize)]
struct ContractsDump {
    // Map of call address to contract name
    identified_contracts: HashMap<Address, String>,
    sources: ContractsSourcesDump,
}

impl ContractsDump {
    pub fn new(debugger_context: &DebuggerContext) -> Self {
        Self {
            identified_contracts: debugger_context
                .identified_contracts
                .iter()
                .map(|(k, v)| (*k, v.clone()))
                .collect(),
            sources: ContractsSourcesDump::new(&debugger_context.contracts_sources),
        }
    }
}

#[derive(Serialize)]
struct ContractsSourcesDump {
    sources_by_id: HashMap<String, HashMap<u32, SourceDataDump>>,
    artifacts_by_name: HashMap<String, Vec<ArtifactDataDump>>,
}

impl ContractsSourcesDump {
    pub fn new(contracts_sources: &ContractSources) -> Self {
        Self {
            sources_by_id: contracts_sources
                .sources_by_id
                .iter()
                .map(|(name, inner_map)| {
                    (
                        name.clone(),
                        inner_map
                            .iter()
                            .map(|(id, source_data)| (*id, SourceDataDump::new(source_data)))
                            .collect(),
                    )
                })
                .collect(),
            artifacts_by_name: contracts_sources
                .artifacts_by_name
                .iter()
                .map(|(name, data)| {
                    (name.clone(), data.iter().map(ArtifactDataDump::new).collect())
                })
                .collect(),
        }
    }
}

#[derive(Serialize)]
struct SourceDataDump {
    source: String,
    language: MultiCompilerLanguage,
    path: PathBuf,
}

impl SourceDataDump {
    pub fn new(v: &SourceData) -> Self {
        Self { source: v.source.deref().clone(), language: v.language, path: v.path.clone() }
    }
}

#[derive(Serialize)]
struct ArtifactDataDump {
    pub source_map: Option<Vec<SourceElementDump>>,
    pub source_map_runtime: Option<Vec<SourceElementDump>>,
    pub pc_ic_map: Option<HashMap<usize, usize>>,
    pub pc_ic_map_runtime: Option<HashMap<usize, usize>>,
    pub build_id: String,
    pub file_id: u32,
}

impl ArtifactDataDump {
    pub fn new(v: &ArtifactData) -> Self {
        Self {
            source_map: v
                .source_map
                .clone()
                .map(|source_map| source_map.iter().map(SourceElementDump::new).collect()),
            source_map_runtime: v
                .source_map_runtime
                .clone()
                .map(|source_map| source_map.iter().map(SourceElementDump::new).collect()),
            pc_ic_map: v.pc_ic_map.clone().map(|v| v.inner.iter().map(|(k, v)| (*k, *v)).collect()),
            pc_ic_map_runtime: v
                .pc_ic_map_runtime
                .clone()
                .map(|v| v.inner.iter().map(|(k, v)| (*k, *v)).collect()),
            build_id: v.build_id.clone(),
            file_id: v.file_id,
        }
    }
}