soldb-repl 0.4.3

Interactive debugger state machine (breakpoints, stepping, display modes) for SolDB
Documentation
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Text for a terminal, from [`Output`].
//!
//! One line says where the debugger is: the source position, then the step, program
//! counter, opcode, and gas in parentheses, so a person reads the location first and a
//! script finds the numbers in one place. Everything is plain text unless colors are
//! asked for, and nothing is printed twice.

use std::fmt::Write as _;

use crate::response::{
    BreakpointEvent, Level, Output, StateInfo, Stop, StopReason, ValueStatus, VariableInfo,
};

/// Renders [`Output`] as lines of text.
#[derive(Debug, Clone, Copy, Default)]
pub struct Renderer {
    /// Whether to color the text with ANSI escapes.
    pub color: bool,
}

impl Renderer {
    #[must_use]
    pub const fn new(color: bool) -> Self {
        Self { color }
    }

    /// The text for one item, each line ended by a newline; empty for items that print
    /// nothing.
    #[must_use]
    pub fn render(&self, output: &Output) -> String {
        let mut text = String::new();
        for line in self.lines(output) {
            text.push_str(&line);
            text.push('\n');
        }
        text
    }

    /// The lines for one item.
    #[must_use]
    pub fn lines(&self, output: &Output) -> Vec<String> {
        match output {
            Output::Loaded { steps, contracts } => {
                let mut lines = vec![format!("Loaded trace with {} steps.", self.number(steps))];
                if contracts.is_empty() {
                    lines.push(self.dim(
                        "No sources matched the executed contracts; stepping is by instruction.",
                    ));
                } else {
                    let listed = contracts
                        .iter()
                        .map(|contract| match &contract.address {
                            Some(address) => {
                                format!("{} ({})", self.name(&contract.name), self.dim(address))
                            }
                            None => self.name(&contract.name),
                        })
                        .collect::<Vec<_>>();
                    lines.push(format!(
                        "{} {}",
                        self.info("Sources loaded for:"),
                        listed.join(", ")
                    ));
                }
                lines
            }
            Output::Stop(stop) => self.stop_lines(stop),
            Output::Message { level, text, note } => {
                let mut lines = vec![match level {
                    Level::Info => self.info_message(text),
                    Level::Warning => self.warning(text),
                    Level::Note => format!("{} {text}", self.warning("Note:")),
                }];
                if let Some(note) = note {
                    lines.push(format!("{} {note}", self.dim("note:")));
                }
                lines
            }
            Output::Breakpoint { event, id, label } => vec![match event {
                BreakpointEvent::Set => format!(
                    "{} {label}",
                    self.success(format!("Breakpoint #{id} set at"))
                ),
                BreakpointEvent::Cleared => format!(
                    "{} {label}",
                    self.info(format!("Breakpoint #{id} cleared at"))
                ),
            }],
            Output::Breakpoints { breakpoints } => {
                if breakpoints.is_empty() {
                    vec![self.dim("No breakpoints set.")]
                } else {
                    breakpoints
                        .iter()
                        .map(|breakpoint| {
                            format!("#{} {}", self.number(breakpoint.id), breakpoint.label)
                        })
                        .collect()
                }
            }
            Output::Backtrace {
                warning,
                note,
                frames,
            } => {
                let mut lines = Vec::new();
                if let Some(warning) = warning {
                    lines.push(self.warning(format!("warning: {warning}")));
                }
                if let Some(note) = note {
                    lines.push(format!("{} {note}", self.dim("note:")));
                }
                for frame in frames {
                    let mut line = format!("#{:<2} ", frame.index);
                    if let Some(kind) = &frame.kind {
                        let _ = write!(line, "[{kind}] ");
                    }
                    line.push_str(&self.name(&frame.name));
                    if !frame.arguments.is_empty() {
                        let arguments = frame
                            .arguments
                            .iter()
                            .map(|argument| format!("{} = {}", argument.name, argument.value))
                            .collect::<Vec<_>>()
                            .join(", ");
                        let _ = write!(line, "({arguments})");
                    }
                    if let Some(location) = &frame.location {
                        let _ = write!(line, " at {location}");
                    }
                    if let Some(address) = &frame.address {
                        let _ = write!(line, " ({})", self.address(address));
                    }
                    line.push_str(&self.dim(format!("  step {}, PC {}", frame.step, frame.pc)));
                    lines.push(line);
                }
                lines
            }
            Output::Listing {
                path,
                current_line,
                lines,
            } => {
                let mut out = vec![format!("{}:{}", self.info(path), self.number(current_line))];
                for line in lines {
                    if line.current {
                        out.push(format!(
                            "{} {}",
                            self.bold(format!("=> {:>5} |", line.line)),
                            self.bold(&line.text)
                        ));
                    } else {
                        out.push(format!(
                            "{} {}",
                            self.dim(format!("   {:>5} |", line.line)),
                            line.text
                        ));
                    }
                }
                out
            }
            Output::Stack { words, shown } => {
                if words.is_empty() {
                    return vec![self.dim("Stack: empty")];
                }
                let mut lines = vec![format!(
                    "{} {} words, top first",
                    self.info("Stack:"),
                    self.number(words.len())
                )];
                for (index, word) in words.iter().enumerate().rev().take(*shown) {
                    lines.push(format!("  [{index}] {}", self.number(word)));
                }
                if *shown < words.len() {
                    lines.push(self.dim(format!("  ... {} more below", words.len() - shown)));
                }
                lines
            }
            Output::Memory(memory) => {
                let range = if memory.start > 0 || memory.end < memory.total {
                    format!(", showing bytes {}..{}", memory.start, memory.end)
                } else {
                    String::new()
                };
                let mut lines = vec![format!(
                    "{} {} bytes{range}",
                    self.info("Memory:"),
                    self.number(memory.total)
                )];
                for word in &memory.words {
                    lines.push(format!(
                        "{} {}",
                        self.dim(format!("0x{:04x}:", word.offset)),
                        word.hex
                    ));
                }
                lines
            }
            Output::Storage { address, slots } => {
                if slots.is_empty() {
                    return vec![self.dim("Storage: no slots read or written yet.")];
                }
                let mut lines = vec![match address {
                    Some(address) => {
                        format!(
                            "{} {}",
                            self.info("Storage:"),
                            self.dim(format!("of {address}"))
                        )
                    }
                    None => self.info("Storage:"),
                }];
                for slot in slots {
                    let was = slot
                        .was
                        .as_ref()
                        .map(|was| self.dim(format!("  (was {was})")))
                        .unwrap_or_default();
                    lines.push(format!("  {} = {}{was}", slot.slot, slot.value));
                }
                lines
            }
            Output::TransientStorage { address, slots } => {
                if slots.is_empty() {
                    return vec![self.dim(
                        "Transient storage: nothing read or written yet; every slot is zero.",
                    )];
                }
                let mut lines = vec![match address {
                    Some(address) => format!(
                        "{} {}",
                        self.info("Transient storage:"),
                        self.dim(format!("of {address}"))
                    ),
                    None => self.info("Transient storage:"),
                }];
                for slot in slots {
                    lines.push(format!("  {} = {}", slot.slot, slot.value));
                }
                lines
            }
            Output::Calldata { bytes, data } => vec![
                format!("{} {} bytes", self.info("Calldata:"), self.number(bytes)),
                data.clone(),
            ],
            Output::ReturnData { bytes: 0, .. } => vec![self.dim("Return data: empty.")],
            Output::ReturnData { bytes, data } => vec![
                format!("{} {} bytes", self.info("Return data:"), self.number(bytes)),
                data.clone(),
            ],
            Output::Variables {
                warning,
                pc,
                locals,
                unavailable,
                state,
                transient,
            } => {
                let mut lines = Vec::new();
                if let Some(warning) = warning {
                    lines.push(self.warning(format!("warning: {warning}")));
                }
                if locals.is_empty() {
                    // The difference matters: locals that cannot be read here at all is
                    // not the same as none being live at this program counter.
                    lines.push(match unavailable {
                        Some(reason) => format!(
                            "{} locals are unavailable here: {reason}",
                            self.dim("Variables:")
                        ),
                        None => format!(
                            "{} no variables in scope at PC {}",
                            self.dim("Variables:"),
                            self.number(pc)
                        ),
                    });
                }
                for variable in locals {
                    lines.push(self.variable(variable));
                }
                match state {
                    StateInfo::Variables { variables } => {
                        lines.push(self.dim("State:"));
                        for variable in variables {
                            lines.push(self.variable(variable));
                        }
                    }
                    // Transient variables are state variables too; they follow under `State:`.
                    StateInfo::None if !transient.is_empty() => lines.push(self.dim("State:")),
                    StateInfo::None => {
                        lines.push(self.dim("State: this contract declares no state variables"));
                    }
                    StateInfo::NoLayout => lines.push(self.dim(
                        "State: no storage layout loaded; compile with `--storage-layout` to \
                         read state variables",
                    )),
                    StateInfo::NoStorage => {
                        lines.push(self.dim("State: no storage was recorded for this step"));
                    }
                }
                for variable in transient {
                    lines.push(self.variable(variable));
                }
                lines
            }
            Output::Variable { warning, variable } => {
                let mut lines = Vec::new();
                if let Some(warning) = warning {
                    lines.push(self.warning(format!("warning: {warning}")));
                }
                lines.push(self.variable(variable));
                lines
            }
            Output::Resources { contracts, json } => {
                if *json {
                    let document = serde_json::json!({ "contracts": contracts });
                    return serde_json::to_string_pretty(&document)
                        .unwrap_or_default()
                        .lines()
                        .map(str::to_owned)
                        .collect();
                }
                if contracts.is_empty() {
                    return vec![self.warning("No contract sources are loaded.")];
                }
                let mut lines = Vec::new();
                for contract in contracts {
                    lines.push(format!(
                        "{} {} {}",
                        self.name(&contract.name),
                        self.dim(contract.address.as_deref().unwrap_or("<any address>")),
                        self.dim(format!("[{}]", contract.environment))
                    ));
                    for path in &contract.sources {
                        lines.push(format!("  {path}"));
                    }
                }
                lines
            }
            Output::Help { lines } => lines.clone(),
            Output::Tui | Output::Quit => Vec::new(),
        }
    }

    /// The stop: the reason when there is one, then the location line and the source
    /// line, or the stack in assembly mode.
    fn stop_lines(&self, stop: &Stop) -> Vec<String> {
        let mut lines = Vec::new();
        match &stop.reason {
            StopReason::Initial | StopReason::Moved => {}
            StopReason::Breakpoint { id, label } => lines.push(format!(
                "{} {}, {label}",
                self.success(format!("Breakpoint #{id} hit at step")),
                self.number(stop.step)
            )),
            StopReason::End => lines.push(format!(
                "{} {}",
                self.info("End of trace at step"),
                self.number(stop.step)
            )),
            StopReason::Start => lines.push(format!(
                "{} {}",
                self.info("Start of trace at step"),
                self.number(stop.step)
            )),
        }
        let status = format!(
            "step {}/{}, pc {}, {}, gas {}",
            self.number(stop.step),
            self.number(stop.last_step),
            self.number(stop.pc),
            self.opcode(&stop.op),
            self.number(stop.gas)
        );
        match &stop.location {
            Some(location) => {
                let function = location
                    .function
                    .as_deref()
                    .map(|name| format!(" in {}", self.name(name)))
                    .unwrap_or_default();
                let generated = if location.generated {
                    self.dim("  (compiler-generated code for this line)")
                } else {
                    String::new()
                };
                lines.push(format!(
                    "{}:{}{function}{generated}  {}",
                    self.info(&location.path),
                    self.number(location.line),
                    self.dim(format!("({status})"))
                ));
                if stop.stack.is_none() {
                    if let Some(text) = &location.text {
                        lines.push(format!(
                            "{} {text}",
                            self.dim(format!("{:>5} |", location.line))
                        ));
                    }
                }
            }
            None if stop.has_source => {
                let address = stop
                    .address
                    .as_deref()
                    .map(|address| format!(" in {}", self.address(address)))
                    .unwrap_or_default();
                lines.push(format!(
                    "{}  {}",
                    self.dim(format!("no source for this step{address}")),
                    self.dim(format!("({status})"))
                ));
            }
            None => lines.push(status),
        }
        if let Some(stack) = &stop.stack {
            lines.push(format!(
                "{} {}",
                self.info("Stack:"),
                self.stack_summary(stack)
            ));
        }
        if let Some(note) = &stop.note {
            lines.push(format!("{} {note}", self.warning("Note:")));
        }
        lines
    }

    /// The top of the stack on one line, the words numbered from the bottom.
    fn stack_summary(&self, top_first: &[String]) -> String {
        if top_first.is_empty() {
            return self.dim("empty");
        }
        let total = top_first.len();
        let mut parts = top_first
            .iter()
            .take(4)
            .enumerate()
            .map(|(offset, word)| {
                format!(
                    "{} {}",
                    self.dim(format!("[{}]", total - 1 - offset)),
                    self.number(shorten_hex(word))
                )
            })
            .collect::<Vec<_>>();
        if total > 4 {
            parts.push(self.dim(format!("... +{} more", total - 4)));
        }
        parts.join(" ")
    }

    fn variable(&self, variable: &VariableInfo) -> String {
        let value = match variable.status {
            ValueStatus::Unavailable => self.warning(&variable.value),
            _ => self.success(&variable.value),
        };
        let place = variable
            .place
            .as_ref()
            .map(|place| self.dim(format!(" [{place}]")))
            .unwrap_or_default();
        format!(
            "{} {} = {value}{place}",
            self.info(&variable.ty),
            self.bold(&variable.name)
        )
    }

    fn info_message(&self, text: &str) -> String {
        // `Label: rest` colors the label; a bare sentence is printed as it is.
        match text.split_once(": ") {
            Some((label, rest)) if !label.contains(' ') || label.starts_with("Mode") => {
                format!("{} {rest}", self.info(format!("{label}:")))
            }
            _ => self.info(text),
        }
    }

    fn paint(&self, text: impl std::fmt::Display, code: &str) -> String {
        if self.color {
            format!("\x1b[{code}m{text}\x1b[0m")
        } else {
            text.to_string()
        }
    }

    fn bold(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "1")
    }

    fn dim(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "2")
    }

    fn info(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "96")
    }

    fn success(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "92")
    }

    fn warning(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "93")
    }

    fn number(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "93")
    }

    fn opcode(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "94")
    }

    fn name(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "95")
    }

    fn address(&self, text: impl std::fmt::Display) -> String {
        self.paint(text, "95")
    }
}

/// A long hex word as its first digits: `0x1234...`.
#[must_use]
pub fn shorten_hex(value: &str) -> String {
    let Some(digits) = value.strip_prefix("0x") else {
        return value.to_owned();
    };
    if value.len() <= 10 {
        return value.to_owned();
    }
    let head = digits.chars().take(4).collect::<String>();
    format!("0x{head}...")
}

#[cfg(test)]
mod tests {
    use super::{shorten_hex, Renderer};
    use crate::response::{
        Level, Output, SlotInfo, StateInfo, Stop, StopLocation, StopReason, ValueStatus,
        VariableInfo,
    };

    #[test]
    fn transient_storage_lists_its_slots_or_says_every_one_is_zero() {
        let renderer = Renderer::new(false);
        assert_eq!(
            renderer.render(&Output::TransientStorage {
                address: Some("0xabc".to_owned()),
                slots: vec![SlotInfo {
                    slot: "0x0".to_owned(),
                    value: "0x2".to_owned(),
                    was: None,
                }],
            }),
            "Transient storage: of 0xabc\n  0x0 = 0x2\n"
        );
        assert_eq!(
            renderer.render(&Output::TransientStorage {
                address: None,
                slots: Vec::new(),
            }),
            "Transient storage: nothing read or written yet; every slot is zero.\n"
        );
    }

    #[test]
    fn return_data_is_its_size_and_hex_or_says_it_is_empty() {
        let renderer = Renderer::new(false);
        assert_eq!(
            renderer.render(&Output::ReturnData {
                bytes: 1,
                data: "0x2a".to_owned()
            }),
            "Return data: 1 bytes\n0x2a\n"
        );
        assert_eq!(
            renderer.render(&Output::ReturnData {
                bytes: 0,
                data: "0x".to_owned()
            }),
            "Return data: empty.\n"
        );
    }

    #[test]
    fn transient_variables_follow_the_state_and_keep_its_reason() {
        let renderer = Renderer::new(false);
        let variables = |state| Output::Variables {
            warning: None,
            pc: 7,
            locals: vec![VariableInfo {
                name: "a".to_owned(),
                ty: "uint256".to_owned(),
                value: "1".to_owned(),
                raw: None,
                status: ValueStatus::Decoded,
                place: Some("stack+1".to_owned()),
            }],
            unavailable: None,
            state,
            transient: vec![VariableInfo {
                name: "locked".to_owned(),
                ty: "bool".to_owned(),
                value: "true".to_owned(),
                raw: None,
                status: ValueStatus::Decoded,
                place: Some("transient slot 0x0".to_owned()),
            }],
        };
        // A layout with no storage variables does not say the contract has none.
        assert_eq!(
            renderer.render(&variables(StateInfo::None)),
            "uint256 a = 1 [stack+1]\nState:\nbool locked = true [transient slot 0x0]\n"
        );
        // A missing storage layout is still reported.
        assert!(renderer
            .render(&variables(StateInfo::NoLayout))
            .contains("no storage layout loaded"));
    }

    #[test]
    fn a_stop_is_one_location_line_and_the_source_line() {
        let renderer = Renderer::new(false);
        let stop = Stop {
            reason: StopReason::Breakpoint {
                id: 1,
                label: "Counter.sol:7".to_owned(),
            },
            step: 12,
            last_step: 99,
            pc: 34,
            op: "PUSH1".to_owned(),
            gas: 5000,
            location: Some(StopLocation {
                path: "Counter.sol".to_owned(),
                line: 7,
                function: Some("increment".to_owned()),
                generated: false,
                text: Some("        count += 1;".to_owned()),
            }),
            address: None,
            has_source: true,
            stack: None,
            note: Some("something".to_owned()),
        };
        assert_eq!(
            renderer.render(&Output::Stop(stop)),
            "Breakpoint #1 hit at step 12, Counter.sol:7\n\
             Counter.sol:7 in increment  (step 12/99, pc 34, PUSH1, gas 5000)\n    \
             7 |         count += 1;\n\
             Note: something\n"
        );
        let bare = Stop {
            reason: StopReason::Moved,
            step: 0,
            last_step: 3,
            pc: 0,
            op: "PUSH1".to_owned(),
            gas: 1,
            location: None,
            address: Some("0xabc".to_owned()),
            has_source: false,
            stack: Some(vec!["0x2".to_owned(), "0x1".to_owned()]),
            note: None,
        };
        assert_eq!(
            renderer.render(&Output::Stop(bare)),
            "step 0/3, pc 0, PUSH1, gas 1\nStack: [1] 0x2 [0] 0x1\n"
        );
    }

    #[test]
    fn messages_and_hex_are_plain_without_color() {
        let renderer = Renderer::new(false);
        assert_eq!(
            renderer.render(&Output::Message {
                level: Level::Warning,
                text: "Unknown command: x".to_owned(),
                note: Some("try help".to_owned()),
            }),
            "Unknown command: x\nnote: try help\n"
        );
        assert_eq!(shorten_hex("0x1234"), "0x1234");
        assert_eq!(shorten_hex("0x123456789abcdef0"), "0x1234...");
        assert_eq!(shorten_hex("plain"), "plain");
        assert!(Renderer::new(true)
            .render(&Output::Help {
                lines: vec!["x".to_owned()]
            })
            .starts_with('x'));
    }
}