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

chore: refactor debugger dump code #9170

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 0 additions & 12 deletions crates/debugger/src/context.rs

This file was deleted.

12 changes: 9 additions & 3 deletions crates/debugger/src/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
//! Debugger implementation.

use crate::{
context::DebuggerContext, tui::TUI, DebugNode, DebuggerBuilder, ExitReason, FileDumper,
};
use crate::{tui::TUI, DebugNode, DebuggerBuilder, ExitReason, FileDumper};
use alloy_primitives::map::AddressHashMap;
use eyre::Result;
use foundry_common::evm::Breakpoints;
use foundry_evm_traces::debug::ContractSources;
use std::path::PathBuf;

pub struct DebuggerContext {
pub debug_arena: Vec<DebugNode>,
pub identified_contracts: AddressHashMap<String>,
/// Source map of contract sources
pub contracts_sources: ContractSources,
pub breakpoints: Breakpoints,
}

pub struct Debugger {
context: DebuggerContext,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The file dumper implementation
//! The debug file dumper implementation.

use crate::{context::DebuggerContext, DebugNode};
use crate::{debugger::DebuggerContext, DebugNode};
use alloy_primitives::Address;
use eyre::Result;
use foundry_common::fs::write_json_file;
Expand All @@ -12,9 +12,11 @@ use foundry_evm_traces::debug::{ArtifactData, ContractSources, SourceData};
use serde::Serialize;
use std::{collections::HashMap, ops::Deref, path::PathBuf};

/// The file dumper
/// 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,
}

Expand All @@ -30,6 +32,13 @@ impl<'a> FileDumper<'a> {
}
}

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

impl DebuggerDump {
fn from(debugger_context: &DebuggerContext) -> Self {
Self {
Expand All @@ -39,12 +48,6 @@ impl DebuggerDump {
}
}

#[derive(Serialize)]
struct DebuggerDump {
contracts: ContractsDump,
debug_arena: Vec<DebugNode>,
}

#[derive(Serialize)]
pub struct SourceElementDump {
offset: u32,
Expand All @@ -54,36 +57,29 @@ pub struct SourceElementDump {
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,
}

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

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

#[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 ContractsDump {
pub fn new(debugger_context: &DebuggerContext) -> Self {
Self {
Expand All @@ -97,6 +93,12 @@ impl ContractsDump {
}
}

#[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 {
Expand Down Expand Up @@ -124,26 +126,27 @@ impl ContractsSourcesDump {
}
}

#[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() }
}
}

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 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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ extern crate tracing;
mod op;

mod builder;
mod context;
mod debugger;
mod file_dumper;
mod tui;

mod node;

pub use node::DebugNode;

pub use builder::DebuggerBuilder;
Expand Down
2 changes: 1 addition & 1 deletion crates/debugger/src/tui/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Debugger context and event handler implementation.

use crate::{context::DebuggerContext, DebugNode, ExitReason};
use crate::{debugger::DebuggerContext, DebugNode, ExitReason};
use alloy_primitives::{hex, Address};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
use foundry_evm_core::buffer::BufferKind;
Expand Down
2 changes: 1 addition & 1 deletion crates/debugger/src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
};

mod context;
use crate::context::DebuggerContext;
use crate::debugger::DebuggerContext;
use context::TUIContext;

mod draw;
Expand Down
Loading