|
| 1 | +//! The file dumper implementation |
| 2 | +
|
| 3 | +use crate::{context::DebuggerContext, DebugNode}; |
| 4 | +use alloy_primitives::Address; |
| 5 | +use eyre::Result; |
| 6 | +use foundry_common::fs::write_json_file; |
| 7 | +use foundry_compilers::{ |
| 8 | + artifacts::sourcemap::{Jump, SourceElement}, |
| 9 | + multi::MultiCompilerLanguage, |
| 10 | +}; |
| 11 | +use foundry_evm_traces::debug::{ArtifactData, ContractSources, SourceData}; |
| 12 | +use serde::Serialize; |
| 13 | +use std::{collections::HashMap, ops::Deref, path::PathBuf}; |
| 14 | + |
| 15 | +/// The file dumper |
| 16 | +pub struct FileDumper<'a> { |
| 17 | + path: &'a PathBuf, |
| 18 | + debugger_context: &'a mut DebuggerContext, |
| 19 | +} |
| 20 | + |
| 21 | +impl<'a> FileDumper<'a> { |
| 22 | + pub fn new(path: &'a PathBuf, debugger_context: &'a mut DebuggerContext) -> Self { |
| 23 | + Self { path, debugger_context } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn run(&mut self) -> Result<()> { |
| 27 | + let data = DebuggerDump::from(self.debugger_context); |
| 28 | + write_json_file(self.path, &data).unwrap(); |
| 29 | + Ok(()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl DebuggerDump { |
| 34 | + fn from(debugger_context: &DebuggerContext) -> Self { |
| 35 | + Self { |
| 36 | + contracts: ContractsDump::new(debugger_context), |
| 37 | + debug_arena: debugger_context.debug_arena.clone(), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Serialize)] |
| 43 | +struct DebuggerDump { |
| 44 | + contracts: ContractsDump, |
| 45 | + debug_arena: Vec<DebugNode>, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Serialize)] |
| 49 | +pub struct SourceElementDump { |
| 50 | + offset: u32, |
| 51 | + length: u32, |
| 52 | + index: i32, |
| 53 | + jump: u32, |
| 54 | + modifier_depth: u32, |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Serialize)] |
| 58 | +struct ContractsDump { |
| 59 | + // Map of call address to contract name |
| 60 | + identified_contracts: HashMap<Address, String>, |
| 61 | + sources: ContractsSourcesDump, |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Serialize)] |
| 65 | +struct ContractsSourcesDump { |
| 66 | + sources_by_id: HashMap<String, HashMap<u32, SourceDataDump>>, |
| 67 | + artifacts_by_name: HashMap<String, Vec<ArtifactDataDump>>, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Serialize)] |
| 71 | +struct SourceDataDump { |
| 72 | + source: String, |
| 73 | + language: MultiCompilerLanguage, |
| 74 | + path: PathBuf, |
| 75 | +} |
| 76 | + |
| 77 | +#[derive(Serialize)] |
| 78 | +struct ArtifactDataDump { |
| 79 | + pub source_map: Option<Vec<SourceElementDump>>, |
| 80 | + pub source_map_runtime: Option<Vec<SourceElementDump>>, |
| 81 | + pub pc_ic_map: Option<HashMap<usize, usize>>, |
| 82 | + pub pc_ic_map_runtime: Option<HashMap<usize, usize>>, |
| 83 | + pub build_id: String, |
| 84 | + pub file_id: u32, |
| 85 | +} |
| 86 | + |
| 87 | +impl ContractsDump { |
| 88 | + pub fn new(debugger_context: &DebuggerContext) -> Self { |
| 89 | + Self { |
| 90 | + identified_contracts: debugger_context |
| 91 | + .identified_contracts |
| 92 | + .iter() |
| 93 | + .map(|(k, v)| (*k, v.clone())) |
| 94 | + .collect(), |
| 95 | + sources: ContractsSourcesDump::new(&debugger_context.contracts_sources), |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl ContractsSourcesDump { |
| 101 | + pub fn new(contracts_sources: &ContractSources) -> Self { |
| 102 | + Self { |
| 103 | + sources_by_id: contracts_sources |
| 104 | + .sources_by_id |
| 105 | + .iter() |
| 106 | + .map(|(name, inner_map)| { |
| 107 | + ( |
| 108 | + name.clone(), |
| 109 | + inner_map |
| 110 | + .iter() |
| 111 | + .map(|(id, source_data)| (*id, SourceDataDump::new(source_data))) |
| 112 | + .collect(), |
| 113 | + ) |
| 114 | + }) |
| 115 | + .collect(), |
| 116 | + artifacts_by_name: contracts_sources |
| 117 | + .artifacts_by_name |
| 118 | + .iter() |
| 119 | + .map(|(name, data)| { |
| 120 | + (name.clone(), data.iter().map(ArtifactDataDump::new).collect()) |
| 121 | + }) |
| 122 | + .collect(), |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl SourceDataDump { |
| 128 | + pub fn new(v: &SourceData) -> Self { |
| 129 | + Self { source: v.source.deref().clone(), language: v.language, path: v.path.clone() } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl SourceElementDump { |
| 134 | + pub fn new(v: &SourceElement) -> Self { |
| 135 | + Self { |
| 136 | + offset: v.offset(), |
| 137 | + length: v.length(), |
| 138 | + index: v.index_i32(), |
| 139 | + jump: match v.jump() { |
| 140 | + Jump::In => 0, |
| 141 | + Jump::Out => 1, |
| 142 | + Jump::Regular => 2, |
| 143 | + }, |
| 144 | + modifier_depth: v.modifier_depth(), |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl ArtifactDataDump { |
| 150 | + pub fn new(v: &ArtifactData) -> Self { |
| 151 | + Self { |
| 152 | + source_map: v |
| 153 | + .source_map |
| 154 | + .clone() |
| 155 | + .map(|source_map| source_map.iter().map(SourceElementDump::new).collect()), |
| 156 | + source_map_runtime: v |
| 157 | + .source_map_runtime |
| 158 | + .clone() |
| 159 | + .map(|source_map| source_map.iter().map(SourceElementDump::new).collect()), |
| 160 | + pc_ic_map: v.pc_ic_map.clone().map(|v| v.inner.iter().map(|(k, v)| (*k, *v)).collect()), |
| 161 | + pc_ic_map_runtime: v |
| 162 | + .pc_ic_map_runtime |
| 163 | + .clone() |
| 164 | + .map(|v| v.inner.iter().map(|(k, v)| (*k, *v)).collect()), |
| 165 | + build_id: v.build_id.clone(), |
| 166 | + file_id: v.file_id, |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments