soldb 0.1.0

An ETHDebug-first, LLDB-style debugger for Solidity and the EVM
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Gas-profile command loading and presentation.

use std::fs;
use std::io::Cursor;
use std::path::{Path, PathBuf};

use clap::Args;
use soldb_core::{SoldbError, SoldbResult, TransactionTrace};
use soldb_ethdebug::SourceMapEnvironment;
use soldb_profiler::{profile_transaction, ProfileProgram, ProfileReport, ProgramEnvironment};

use crate::{
    bold, dim, error_color, function_color, info, number_color, opcode_color, print_json,
    print_json_command_error, resolve_contract_specs, separator, success, TraceBackendArg,
    TraceSourceIndex,
};

#[derive(Debug, Args)]
pub(crate) struct ProfileArgs {
    #[arg(required_unless_present = "trace_file", conflicts_with = "trace_file")]
    tx_hash: Option<String>,
    #[arg(
        long,
        value_name = "PATH",
        required_unless_present = "tx_hash",
        conflicts_with = "tx_hash"
    )]
    trace_file: Option<PathBuf>,
    #[arg(long, value_enum, default_value_t = TraceBackendArg::Replay)]
    backend: TraceBackendArg,
    #[arg(long = "ethdebug-dir", short = 'e')]
    ethdebug_dir: Vec<String>,
    #[arg(long, short = 'c')]
    contracts: Option<String>,
    #[arg(long, short = 'r', default_value = "http://localhost:8545")]
    rpc: String,
    #[arg(long, default_value_t = 20)]
    top: usize,
    #[arg(long)]
    json: bool,
    #[arg(long, value_name = "PATH")]
    folded: Option<PathBuf>,
    #[arg(long, value_name = "PATH")]
    flamegraph: Option<PathBuf>,
}

pub(crate) fn command(args: &ProfileArgs) -> SoldbResult<()> {
    match command_inner(args) {
        Err(error) if args.json => {
            print_json_command_error("ProfileError", &error.to_string(), None)?;
            Err(SoldbError::AlreadyReported)
        }
        result => result,
    }
}

fn command_inner(args: &ProfileArgs) -> SoldbResult<()> {
    let trace = load_trace(args)?;
    let programs = load_programs(args, &trace)?;
    let report = profile_transaction(&trace, &programs)?;

    if let Some(path) = &args.folded {
        write_folded(&report, path)?;
    }
    if let Some(path) = &args.flamegraph {
        write_flamegraph(&report, path)?;
    }

    if args.json {
        print_json(&report)?;
    } else {
        print_report(&report, args.top);
        if let Some(path) = &args.folded {
            println!("{} {}", info("Folded stacks:"), path.display());
        }
        if let Some(path) = &args.flamegraph {
            println!("{} {}", info("Flame graph:"), path.display());
        }
    }
    Ok(())
}

fn load_trace(args: &ProfileArgs) -> SoldbResult<TransactionTrace> {
    if let Some(path) = &args.trace_file {
        let input = fs::read_to_string(path).map_err(|error| {
            SoldbError::Message(format!(
                "failed to read trace file `{}`: {error}",
                path.display()
            ))
        })?;
        return serde_json::from_str(&input).map_err(|error| {
            SoldbError::Message(format!(
                "invalid transaction trace in `{}`: {error}",
                path.display()
            ))
        });
    }

    let tx_hash = args.tx_hash.as_deref().ok_or_else(|| {
        SoldbError::Message("profile requires a transaction hash or `--trace-file`".to_owned())
    })?;
    soldb_rpc::trace_transaction_with_resolved_backend(&args.rpc, tx_hash, args.backend.into())
        .map(|resolved| resolved.trace)
}

fn load_programs(args: &ProfileArgs, trace: &TransactionTrace) -> SoldbResult<Vec<ProfileProgram>> {
    let specs = resolve_contract_specs(&args.ethdebug_dir, args.contracts.as_deref())?;
    let single_spec = specs.len() == 1;
    let mut programs = Vec::new();

    for spec in specs {
        if !single_spec && spec.address.is_none() {
            return Err(SoldbError::Message(format!(
                "multi-contract profile requires an address for `{}`; use \
                 `<address>:<contract>:<dir>` or a contract mapping file",
                spec.name
            )));
        }
        let mut loaded = false;
        if let Some(index) =
            TraceSourceIndex::load_environment(&spec, SourceMapEnvironment::Runtime)?
        {
            let address = spec
                .address
                .clone()
                .or_else(|| single_spec.then(|| trace.to_addr.clone()).flatten());
            programs.push(ProfileProgram::new(
                address,
                index.info,
                index.source_contents,
            )?);
            loaded = true;
        }
        if let Some(index) =
            TraceSourceIndex::load_environment(&spec, SourceMapEnvironment::Creation)?
        {
            let address = spec.address.clone().or_else(|| {
                single_spec
                    .then(|| trace.contract_address.clone())
                    .flatten()
            });
            programs.push(ProfileProgram::new(
                address,
                index.info,
                index.source_contents,
            )?);
            loaded = true;
        }
        if !loaded {
            return Err(SoldbError::Message(format!(
                "no ETHDebug or legacy source-map program for `{}` found in `{}`",
                spec.name,
                spec.debug_dir.display()
            )));
        }
    }

    Ok(programs)
}

fn write_folded(report: &ProfileReport, path: &Path) -> SoldbResult<()> {
    fs::write(path, report.folded_text()).map_err(|error| {
        SoldbError::Message(format!(
            "failed to write folded stacks to `{}`: {error}",
            path.display()
        ))
    })
}

fn write_flamegraph(report: &ProfileReport, path: &Path) -> SoldbResult<()> {
    let folded = report.folded_text();
    if folded.is_empty() {
        return Err(SoldbError::Message(
            "cannot render a flame graph without nonzero gas samples".to_owned(),
        ));
    }

    let output = fs::File::create(path).map_err(|error| {
        SoldbError::Message(format!(
            "failed to create flame graph `{}`: {error}",
            path.display()
        ))
    })?;
    let mut options = inferno::flamegraph::Options::default();
    options.title = "EVM gas profile".to_owned();
    options.count_name = "gas".to_owned();
    options.name_type = "stack".to_owned();
    options.deterministic = true;
    inferno::flamegraph::from_reader(&mut options, Cursor::new(folded), output).map_err(|error| {
        SoldbError::Message(format!(
            "failed to render flame graph `{}`: {error}",
            path.display()
        ))
    })
}

fn print_report(report: &ProfileReport, top: usize) {
    println!("{}", bold(info("EVM Gas Profile")));
    if let Some(hash) = &report.transaction.hash {
        println!("{} {hash}", info("Transaction:"));
    }
    if let Some(backend) = &report.transaction.backend {
        println!("{} {backend}", info("Backend:"));
    }
    println!("{} {}", info("Status:"), profile_status(report));
    println!(
        "{} {}",
        info("Transaction gas used:"),
        number_color(report.transaction.gas_used)
    );
    println!(
        "{} {} across {} steps",
        info("Traced instruction gas:"),
        number_color(report.totals.step_gas),
        number_color(report.transaction.steps)
    );
    println!(
        "{} {} ({})",
        info("Program-attributed gas:"),
        number_color(report.totals.program_gas),
        percent(report.totals.program_gas, report.totals.step_gas)
    );
    println!(
        "{} {} ({})",
        info("Source-attributed gas:"),
        number_color(report.totals.source_gas),
        percent(report.totals.source_gas, report.totals.step_gas)
    );
    println!(
        "{} {} ({})",
        info("Known program without source:"),
        number_color(report.totals.sourceless_gas),
        percent(report.totals.sourceless_gas, report.totals.step_gas)
    );
    println!(
        "{} {} ({})",
        info("Unmapped execution gas:"),
        number_color(report.totals.unmapped_gas),
        percent(report.totals.unmapped_gas, report.totals.step_gas)
    );

    print_contracts(report, top);
    print_functions(report, top);
    print_source_lines(report, top);
    print_opcodes(report, top);
    print_hotspots(report, top);
}

fn profile_status(report: &ProfileReport) -> String {
    if report.transaction.success {
        success("SUCCESS")
    } else {
        error_color("REVERTED")
    }
}

fn print_contracts(report: &ProfileReport, top: usize) {
    println!();
    println!("{}", bold("Contracts"));
    println!("{}", separator(72));
    for row in report.contracts.iter().take(top) {
        let environment = match row.environment {
            ProgramEnvironment::Create => "create",
            ProgramEnvironment::Call => "call",
        };
        println!(
            "{:>12} {:>7} {:>8}  {} ({environment})",
            number_color(row.gas),
            percent(row.gas, report.totals.step_gas),
            number_color(row.hits),
            function_color(&row.contract)
        );
    }
    if report.contracts.is_empty() {
        println!("{}", dim("No execution steps matched a debug program"));
    }
}

fn print_functions(report: &ProfileReport, top: usize) {
    println!();
    println!("{}", bold("Functions"));
    println!("{}", separator(72));
    for row in report.functions.iter().take(top) {
        let location = match (&row.source, row.line) {
            (Some(source), Some(line)) => format!(" {source}:{line}"),
            _ => String::new(),
        };
        println!(
            "{:>12} {:>7} {:>8}  {}::{}{}",
            number_color(row.gas),
            percent(row.gas, report.totals.step_gas),
            number_color(row.hits),
            function_color(&row.contract),
            function_color(&row.function),
            dim(location)
        );
    }
    if report.functions.is_empty() {
        println!("{}", dim("No function-level samples"));
    }
}

fn print_source_lines(report: &ProfileReport, top: usize) {
    println!();
    println!("{}", bold("Hot source lines"));
    println!("{}", separator(72));
    for row in report.source_lines.iter().take(top) {
        let text = row
            .text
            .as_deref()
            .map(|text| truncate_text(text, 56))
            .unwrap_or_default();
        println!(
            "{:>12} {:>7} {:>8}  {}:{}  {}",
            number_color(row.gas),
            percent(row.gas, report.totals.step_gas),
            number_color(row.hits),
            row.source,
            row.line,
            dim(text)
        );
    }
    if report.source_lines.is_empty() {
        println!("{}", dim("No source-line samples"));
    }
}

fn print_opcodes(report: &ProfileReport, top: usize) {
    println!();
    println!("{}", bold("Opcodes"));
    println!("{}", separator(72));
    for row in report.opcodes.iter().take(top) {
        println!(
            "{:>12} {:>7} {:>8}  {}",
            number_color(row.gas),
            percent(row.gas, report.totals.step_gas),
            number_color(row.hits),
            opcode_color(&row.opcode)
        );
    }
}

fn print_hotspots(report: &ProfileReport, top: usize) {
    println!();
    println!("{}", bold("Hot instructions"));
    println!("{}", separator(72));
    for row in report.hotspots.iter().take(top) {
        let location = row.source.as_ref().map_or_else(
            || "[no source]".to_owned(),
            |source| {
                source.line.map_or_else(
                    || format!("{}@{}", source.path, source.offset),
                    |line| format!("{}:{line}", source.path),
                )
            },
        );
        println!(
            "{:>12} {:>7} {:>8}  {} PC {} {}",
            number_color(row.gas),
            percent(row.gas, report.totals.step_gas),
            number_color(row.hits),
            opcode_color(&row.opcode),
            number_color(row.pc),
            dim(location)
        );
    }
    if report.hotspots.is_empty() {
        println!("{}", dim("No instructions matched a debug program"));
    }
}

fn percent(value: u64, total: u64) -> String {
    if total == 0 {
        return "0.00%".to_owned();
    }
    format!("{:.2}%", value as f64 * 100.0 / total as f64)
}

fn truncate_text(text: &str, max_chars: usize) -> String {
    let mut chars = text.chars();
    let prefix = chars.by_ref().take(max_chars).collect::<String>();
    if chars.next().is_some() {
        format!("{prefix}…")
    } else {
        prefix
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    use serde_json::json;
    use soldb_core::{TraceStep, TransactionTrace};

    use super::{load_programs, percent, truncate_text, ProfileArgs};
    use crate::TraceBackendArg;

    fn temp_dir(label: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time")
            .as_nanos();
        let dir = std::env::temp_dir().join(format!("soldb-profile-{label}-{unique}"));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    #[test]
    fn formats_percentages_and_unicode_text() {
        assert_eq!(percent(1, 4), "25.00%");
        assert_eq!(percent(0, 0), "0.00%");
        assert_eq!(truncate_text("aébc", 2), "aé…");
        assert_eq!(truncate_text("short", 8), "short");
    }

    #[test]
    fn profiles_programs_loaded_from_legacy_source_maps() {
        let dir = temp_dir("legacy-source-map");
        let source = "contract Counter {}\n";
        fs::write(dir.join("Counter.sol"), source).expect("write source");
        fs::write(
            dir.join("combined.json"),
            json!({
                "sourceList": ["Counter.sol"],
                "contracts": {
                    "Counter.sol:Counter": {
                        "bin-runtime": "00",
                        "srcmap-runtime": format!("0:{}:0", source.len())
                    }
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");
        let args = ProfileArgs {
            tx_hash: None,
            trace_file: None,
            backend: TraceBackendArg::Replay,
            ethdebug_dir: vec![format!("0x2:Counter:{}", dir.display())],
            contracts: None,
            rpc: String::new(),
            top: 20,
            json: false,
            folded: None,
            flamegraph: None,
        };
        let trace = TransactionTrace {
            tx_hash: Some("0xabc".to_owned()),
            from_addr: "0x1".to_owned(),
            to_addr: Some("0x2".to_owned()),
            value: "0x0".to_owned(),
            input_data: "0x".to_owned(),
            gas_used: 3,
            output: "0x".to_owned(),
            success: true,
            error: None,
            debug_trace_available: true,
            contract_address: None,
            backend: Some("replay".to_owned()),
            capabilities: Default::default(),
            artifacts: Default::default(),
            steps: vec![TraceStep {
                pc: 0,
                op: "STOP".to_owned(),
                gas: 3,
                gas_cost: 3,
                depth: 0,
                stack: Vec::new(),
                memory: None,
                storage: None,
                error: None,
                snapshot: Default::default(),
            }],
        };

        let programs = load_programs(&args, &trace).expect("load legacy profile program");
        let report = soldb_profiler::profile_transaction(&trace, &programs).expect("profile");

        assert_eq!(programs.len(), 1);
        assert_eq!(report.totals.source_gas, 3);
        assert_eq!(report.source_lines[0].source, "Counter.sol");
        assert_eq!(report.source_lines[0].line, 1);
    }
}