soldb-repl 0.4.1

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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! A debugging session: commands in, [`Output`] out.
//!
//! The session owns the [`DebuggerState`] and answers every command with data, never
//! text, so the terminal REPL, the full-screen view, an editor, and a script all run
//! the same commands and see the same answers. What is said once a session — that
//! locals are inferred, that frame arguments are read off the stack — is remembered
//! here, so it is said once whichever frontend asks first.

use soldb_debugger::{
    state_value, state_variables, ChainStorage, DebugValueStatus, DebugVariable, StateSource,
    StateVariable, StorageWords, INFERRED_LOCALS_WARNING,
};

use crate::command::{command_spec, CommandGroup, COMMANDS};
use crate::response::{
    ArgumentInfo, BreakpointEvent, BreakpointInfo, FrameInfo, Level, ListedLine, LoadedContract,
    MemoryInfo, MemoryWord, Output, ResourceInfo, SlotInfo, StateInfo, Stop, StopLocation,
    StopReason, ValueStatus, VariableInfo,
};
use crate::{
    BreakpointKind, DebuggerCommand, DebuggerInfoCommand, DebuggerState, DisplayMode, StepOutcome,
    VariablesOrigin,
};

/// What `backtrace` says once about arguments read off the stack.
pub const FRAME_ARGUMENTS_WARNING: &str = "frame arguments are read off the stack, not from \
compiler-reported variable locations";

/// The note under [`FRAME_ARGUMENTS_WARNING`].
pub const FRAME_ARGUMENTS_NOTE: &str = "the calling convention was proved from this trace's \
calldata; ETHDebug variable locations will replace this once the compiler emits them";

/// How many lines `list` shows on each side of the current one.
pub const LISTING_RADIUS: u64 = 5;

/// A debugging session over one trace.
pub struct Session {
    state: DebuggerState,
    chain: Option<Box<dyn ChainStorage>>,
    locals_warning_shown: bool,
    arguments_warning_shown: bool,
}

impl std::fmt::Debug for Session {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Session")
            .field("step", &self.state.current_step)
            .field(
                "chain",
                &self.chain.as_ref().map(|chain| chain.label().to_owned()),
            )
            .finish_non_exhaustive()
    }
}

impl Session {
    #[must_use]
    pub fn new(state: DebuggerState) -> Self {
        Self {
            state,
            chain: None,
            locals_warning_shown: false,
            arguments_warning_shown: false,
        }
    }

    /// Reads storage slots the trace never touched from `chain`.
    #[must_use]
    pub fn with_chain(mut self, chain: Option<Box<dyn ChainStorage>>) -> Self {
        self.chain = chain;
        self
    }

    #[must_use]
    pub fn state(&self) -> &DebuggerState {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut DebuggerState {
        &mut self.state
    }

    /// The storage words known at the current step, read from the chain for slots the
    /// trace never touched when a chain is attached.
    #[must_use]
    pub fn storage_words(&self) -> Option<StorageWords<'_>> {
        self.state.storage_words_with_chain(self.chain.as_deref())
    }

    /// What was loaded: the trace's size and the contracts with sources.
    #[must_use]
    pub fn loaded(&self) -> Output {
        let mut contracts = Vec::<LoadedContract>::new();
        for contract in self.state.step_map().map_or(&[][..], |map| map.contracts()) {
            if contract.info.instructions.is_empty() && contract.source_contents.is_empty() {
                continue;
            }
            let entry = LoadedContract {
                name: contract.name.clone(),
                address: contract.address.clone(),
            };
            if !contracts.contains(&entry) {
                contracts.push(entry);
            }
        }
        Output::Loaded {
            steps: self.state.step_count(),
            contracts,
        }
    }

    /// Where the session is now, as the first stop.
    #[must_use]
    pub fn initial_stop(&self) -> Vec<Output> {
        match self.stop(StopReason::Initial) {
            Some(stop) => vec![Output::Stop(stop)],
            None => vec![message(Level::Warning, "No trace loaded.")],
        }
    }

    /// Runs one command and answers it.
    pub fn execute(&mut self, command: DebuggerCommand) -> Vec<Output> {
        match command {
            DebuggerCommand::Empty => Vec::new(),
            DebuggerCommand::Quit => vec![message(Level::Info, "Exiting debugger."), Output::Quit],
            DebuggerCommand::Tui => vec![Output::Tui],
            DebuggerCommand::Help(topic) => vec![Self::help(topic.as_deref())],
            DebuggerCommand::Mode(None) => vec![message(
                Level::Info,
                format!("Mode: {}", self.state.display_mode.as_str()),
            )],
            DebuggerCommand::Info(DebuggerInfoCommand::Resources { json }) => {
                vec![self.resources(json)]
            }
            DebuggerCommand::Info(DebuggerInfoCommand::Breakpoints) => vec![self.breakpoints()],
            DebuggerCommand::Info(DebuggerInfoCommand::Storage) => vec![self.storage()],
            DebuggerCommand::Info(DebuggerInfoCommand::TransientStorage) => {
                vec![self.transient_storage()]
            }
            DebuggerCommand::Vars => vec![self.variables(None)],
            DebuggerCommand::Print(name) => {
                let name = name.trim();
                if name.is_empty() {
                    vec![message(Level::Warning, "Usage: print <variable>")]
                } else {
                    vec![self.variables(Some(name))]
                }
            }
            DebuggerCommand::Backtrace => vec![self.backtrace()],
            DebuggerCommand::List => vec![self.listing(LISTING_RADIUS)],
            DebuggerCommand::Memory { offset, length } => vec![self.memory(offset, length)],
            DebuggerCommand::Calldata => vec![self.calldata()],
            DebuggerCommand::ReturnData => vec![self.return_data()],
            DebuggerCommand::Stack { limit } => vec![self.stack(limit)],
            DebuggerCommand::Unknown(text) => vec![message(
                Level::Warning,
                format!("Unknown command: {text}; type `help` for the list"),
            )],
            command => {
                let mut outputs = match self.state.apply_command(command) {
                    Some(outcome) => self.outcome(outcome),
                    None => Vec::new(),
                };
                if let Some(note) = self.state.take_note() {
                    match outputs.last_mut() {
                        Some(Output::Stop(stop)) => stop.note = Some(note),
                        _ => outputs.push(Output::Message {
                            level: Level::Note,
                            text: note,
                            note: None,
                        }),
                    }
                }
                outputs
            }
        }
    }

    /// What a movement or breakpoint command did.
    fn outcome(&self, outcome: StepOutcome) -> Vec<Output> {
        let stop = |reason: StopReason| -> Vec<Output> {
            match self.stop(reason) {
                Some(stop) => vec![Output::Stop(stop)],
                None => vec![message(Level::Warning, "No trace loaded.")],
            }
        };
        match outcome {
            StepOutcome::NoTrace => vec![message(Level::Warning, "No trace loaded.")],
            StepOutcome::Moved { .. } => stop(StopReason::Moved),
            StepOutcome::BreakpointHit { breakpoint, .. } => stop(StopReason::Breakpoint {
                id: breakpoint.id,
                label: breakpoint.label(),
            }),
            StepOutcome::AtEnd { .. } => stop(StopReason::End),
            StepOutcome::AtStart { .. } => stop(StopReason::Start),
            StepOutcome::InvalidStep {
                requested,
                max_step,
            } => vec![message(
                Level::Warning,
                match max_step {
                    Some(max_step) => format!("Invalid step {requested}; max step is {max_step}"),
                    None => format!("Invalid step {requested}; trace is empty"),
                },
            )],
            StepOutcome::ModeChanged(mode) => {
                vec![message(Level::Info, format!("Mode: {}", mode.as_str()))]
            }
            StepOutcome::BreakpointSet(breakpoint) => vec![Output::Breakpoint {
                event: BreakpointEvent::Set,
                id: breakpoint.id,
                label: breakpoint.label(),
            }],
            StepOutcome::BreakpointCleared(breakpoint) => vec![Output::Breakpoint {
                event: BreakpointEvent::Cleared,
                id: breakpoint.id,
                label: breakpoint.label(),
            }],
            StepOutcome::BreakpointMissing(label) => vec![message(
                Level::Warning,
                format!("No breakpoint set at {label}"),
            )],
            StepOutcome::BreakpointError(text) => vec![message(
                Level::Warning,
                format!("Could not set breakpoint: {text}"),
            )],
        }
    }

    /// The current step as a stop, or `None` without a trace.
    #[must_use]
    pub fn stop(&self, reason: StopReason) -> Option<Stop> {
        let step = self.state.current_step_data()?;
        let location = self.state.location().map(|location| StopLocation {
            text: self
                .state
                .step_map()
                .and_then(|map| map.contracts().get(location.key.contract))
                .and_then(|contract| contract.line_text(location.key.source_id, location.line))
                .map(str::to_owned),
            path: location.path,
            line: location.line,
            function: location.function_name,
            generated: location.generated,
        });
        let address = self
            .state
            .step_map()
            .and_then(|map| map.executing_address(self.state.current_step))
            .map(str::to_owned);
        let stack = (self.state.display_mode == DisplayMode::Assembly).then(|| {
            step.snapshot_ref()
                .stack
                .iter()
                .rev()
                .map(|word| word.to_string())
                .collect()
        });
        Some(Stop {
            reason,
            step: self.state.current_step,
            last_step: self.state.step_count().saturating_sub(1),
            pc: step.pc,
            op: step.op.to_string(),
            gas: step.gas,
            location,
            address,
            has_source: self.state.has_source(),
            stack,
            note: None,
        })
    }

    #[must_use]
    pub fn breakpoints(&self) -> Output {
        Output::Breakpoints {
            breakpoints: self
                .state
                .breakpoints()
                .iter()
                .map(|breakpoint| BreakpointInfo {
                    id: breakpoint.id,
                    label: breakpoint.label(),
                })
                .collect(),
        }
    }

    /// The call structure at the current step, innermost frame first.
    pub fn backtrace(&mut self) -> Output {
        let frames = self.state.frames();
        if frames.is_empty() {
            return message(Level::Warning, "No trace loaded.");
        }
        let show_warning =
            !self.arguments_warning_shown && frames.iter().any(|frame| !frame.arguments.is_empty());
        let (warning, note) = if show_warning {
            self.arguments_warning_shown = true;
            (
                Some(FRAME_ARGUMENTS_WARNING.to_owned()),
                Some(FRAME_ARGUMENTS_NOTE.to_owned()),
            )
        } else {
            (None, None)
        };
        let frames = frames
            .iter()
            .enumerate()
            .map(|(index, frame)| FrameInfo {
                index,
                name: frame
                    .function_name
                    .clone()
                    .or_else(|| frame.contract_name.clone())
                    .or_else(|| frame.address.clone())
                    .unwrap_or_else(|| "<unknown>".to_owned()),
                arguments: frame
                    .arguments
                    .iter()
                    .map(|argument| ArgumentInfo {
                        name: argument.name.clone(),
                        value: argument.value.display.clone(),
                    })
                    .collect(),
                location: frame
                    .location
                    .as_ref()
                    .map(|location| format!("{}:{}", location.path, location.line)),
                address: (frame.external && frame.function_name.is_some())
                    .then(|| frame.address.clone())
                    .flatten(),
                step: frame.step,
                pc: frame.pc,
                kind: frame.kind.clone(),
            })
            .collect();
        Output::Backtrace {
            warning,
            note,
            frames,
        }
    }

    /// `radius` lines of source on each side of the current step's line.
    #[must_use]
    pub fn listing(&self, radius: u64) -> Output {
        let Some(listing) = self.state.source_listing(radius) else {
            return if self.state.has_source() {
                message(Level::Warning, "No source for this step.")
            } else {
                message(
                    Level::Warning,
                    "Cannot list source: no ETHDebug metadata is loaded; start the session with \
                     `--ethdebug-dir <address>:<contract>:<dir>`",
                )
            };
        };
        Output::Listing {
            path: listing.path,
            current_line: listing.current_line,
            lines: listing
                .lines
                .into_iter()
                .map(|(line, text)| ListedLine {
                    line,
                    text,
                    current: line == listing.current_line,
                })
                .collect(),
        }
    }

    /// Memory at the current step in 32-byte words, the whole of it or one range.
    #[must_use]
    pub fn memory(&self, offset: Option<u64>, length: Option<u64>) -> Output {
        let Some(step) = self.state.current_step_data() else {
            return message(Level::Warning, "No trace loaded.");
        };
        let Some(memory) = step.snapshot_ref().memory else {
            let captured = self
                .state
                .trace()
                .is_some_and(|trace| trace.capabilities.memory);
            return if captured {
                message(Level::Info, "Memory is empty at this step.")
            } else {
                message(Level::Warning, "Memory was not captured by this backend.")
            };
        };
        let hex = memory.trim_start_matches("0x");
        if hex.is_empty() {
            return message(Level::Info, "Memory is empty at this step.");
        }
        if !hex.is_ascii() {
            return message(Level::Info, format!("Memory: {hex}"));
        }
        let total = hex.len() / 2;
        let start = usize::try_from(offset.unwrap_or(0)).unwrap_or(usize::MAX);
        if start >= total {
            return message(
                Level::Warning,
                format!("Nothing to show: memory is {total} bytes; offset {start} is past the end"),
            );
        }
        let end = length
            .and_then(|length| usize::try_from(length).ok())
            .and_then(|length| start.checked_add(length))
            .map_or(total, |end| end.min(total));
        let bytes = hex.as_bytes();
        let mut words = Vec::new();
        let mut word_start = start;
        while word_start < end {
            let word_end = word_start.saturating_add(32).min(end);
            words.push(MemoryWord {
                offset: word_start,
                hex: std::str::from_utf8(&bytes[word_start * 2..word_end * 2])
                    .unwrap_or("")
                    .to_owned(),
            });
            word_start = word_end;
        }
        Output::Memory(MemoryInfo {
            total,
            start,
            end,
            words,
        })
    }

    /// Every slot whose value is known at the current step: what the transaction has read
    /// or written so far in this frame's storage.
    #[must_use]
    pub fn storage(&self) -> Output {
        let Some(step) = self.state.current_step_data() else {
            return message(Level::Warning, "No trace loaded.");
        };
        let captured = self
            .state
            .trace()
            .is_some_and(|trace| trace.capabilities.storage);
        if !captured {
            return message(
                Level::Warning,
                "Storage was not captured by this backend; no per-step storage was recorded.",
            );
        }
        let known = self
            .state
            .storage_words()
            .map(|words| words.known())
            .unwrap_or_default();
        // The slots this step itself changed, so a stop at an `SSTORE` shows what moved.
        let changed = step.snapshot_ref().storage_diff;
        let slots = known
            .into_iter()
            .map(|(slot, value)| {
                let slot = soldb_debugger::short_hex(&slot);
                let was = changed
                    .iter()
                    .find(|(candidate, _)| normalize_storage_slot(candidate) == slot)
                    .map(|(_, change)| change.before.clone().unwrap_or_else(|| "0x0".to_owned()));
                SlotInfo {
                    slot,
                    value: soldb_debugger::short_hex(&value),
                    was,
                }
            })
            .collect();
        Output::Storage {
            address: self.state.storage_address().map(str::to_owned),
            slots,
        }
    }

    /// Every transient storage slot whose value is known at the current step.
    #[must_use]
    pub fn transient_storage(&self) -> Output {
        if self.state.current_step_data().is_none() {
            return message(Level::Warning, "No trace loaded.");
        }
        let captured = self
            .state
            .trace()
            .is_some_and(|trace| trace.capabilities.transient_storage);
        if !captured {
            return message(
                Level::Warning,
                "Transient storage is not in this trace; it was recorded without `TLOAD` and `TSTORE` values.",
            );
        }
        let (Some(words), Some(address)) =
            (self.state.transient_words(), self.state.storage_address())
        else {
            return message(
                Level::Warning,
                "Transient storage unavailable: this frame's storage belongs to no known account.",
            );
        };
        let slots = words
            .known()
            .into_iter()
            .map(|(slot, value)| SlotInfo {
                slot: soldb_debugger::short_hex(&slot),
                value: soldb_debugger::short_hex(&value),
                was: None,
            })
            .collect();
        Output::TransientStorage {
            address: Some(address.to_owned()),
            slots,
        }
    }

    /// The return data buffer at the current step.
    #[must_use]
    pub fn return_data(&self) -> Output {
        let Some(step) = self.state.current_step_data() else {
            return message(Level::Warning, "No trace loaded.");
        };
        let captured = self
            .state
            .trace()
            .is_some_and(|trace| trace.capabilities.return_data);
        if !captured {
            return message(
                Level::Warning,
                "Return data is not in this trace; the backend did not record it per step.",
            );
        }
        let data = step.snapshot_ref().return_data.unwrap_or_default();
        Output::ReturnData {
            bytes: data.len() / 2,
            data: format!("0x{data}"),
        }
    }

    #[must_use]
    pub fn calldata(&self) -> Output {
        if self.state.trace().is_none() {
            return message(Level::Warning, "No trace loaded.");
        }
        match self.state.calldata() {
            Some(data) => Output::Calldata {
                bytes: data.trim_start_matches("0x").len() / 2,
                data,
            },
            None => message(
                Level::Warning,
                "Calldata for this frame was not recorded by the backend; only the root frame's \
                 is known.",
            ),
        }
    }

    /// The stack at the current step, all of it or the top `limit` words.
    #[must_use]
    pub fn stack(&self, limit: Option<usize>) -> Output {
        let Some(step) = self.state.current_step_data() else {
            return message(Level::Warning, "No trace loaded.");
        };
        let words = step
            .snapshot_ref()
            .stack
            .iter()
            .map(|word| word.to_string())
            .collect::<Vec<_>>();
        let shown = limit.map_or(words.len(), |limit| limit.min(words.len()));
        Output::Stack { words, shown }
    }

    /// The variables in scope at the current step, or the one named by `filter`, which
    /// may be a path into a local or a state variable.
    pub fn variables(&mut self, filter: Option<&str>) -> Output {
        let Some(step) = self.state.current_step_data() else {
            return message(Level::Warning, "Cannot read variables: no trace is loaded");
        };
        let pc = step.pc;
        // No contract sources loaded at all: the same "load debug info" answer `list`
        // gives, rather than a per-step "no variables here".
        let no_metadata = self.state.step_map().is_none_or(|map| {
            map.contracts()
                .iter()
                .all(|c| c.info.instructions.is_empty())
        });
        if no_metadata {
            return message(
                Level::Warning,
                "Cannot read variables: no ETHDebug metadata is loaded; start the session with \
                 `--ethdebug-dir <address>:<contract>:<dir>`",
            );
        }
        let variables = self.state.variables();
        let warning = match &variables {
            Ok(step_variables)
                if step_variables.origin == VariablesOrigin::Inferred
                    && !self.locals_warning_shown =>
            {
                self.locals_warning_shown = true;
                Some(INFERRED_LOCALS_WARNING.to_owned())
            }
            _ => None,
        };
        let (locals, unavailable) = match variables {
            Ok(step_variables) => (step_variables.variables, None),
            Err(reason) => (Vec::new(), Some(reason)),
        };
        let words = self.storage_words();
        let layout = self.state.storage_layout();
        let chain_label = words.as_ref().and_then(StorageWords::chain_label);
        let transient_words = self.state.transient_words();
        let transient_layout = self.state.transient_storage_layout();

        if let Some(name) = filter {
            if let Some(variable) = locals.iter().find(|variable| variable.name == name) {
                return Output::Variable {
                    warning,
                    variable: local_info(variable),
                };
            }
            // A path through a local: `item.tags[1]`, `stored.owners[0xabc]`, `blob.length`.
            match self.state.local_path(name) {
                Some(Ok(variable)) => {
                    return Output::Variable {
                        warning,
                        variable: local_info(&variable),
                    }
                }
                Some(Err(reason)) => {
                    return message(Level::Warning, format!("Cannot read variable: {reason}"))
                }
                None => {}
            }
            // Names are unique across both spaces, so a transient variable is found by name.
            if let (Some(transient_layout), Some(transient_words)) =
                (transient_layout, transient_words.as_ref())
            {
                if let Ok(variable) = state_value(transient_layout, transient_words, name) {
                    return Output::Variable {
                        warning,
                        variable: transient_info(&variable),
                    };
                }
            }
            let (Some(layout), Some(words)) = (layout, words.as_ref()) else {
                return message(
                    Level::Warning,
                    format!(
                        "No such variable: `{name}` is not in scope at PC {pc}; state variables \
                         need a storage layout, compile with `--storage-layout`"
                    ),
                );
            };
            return match state_value(layout, words, name) {
                Ok(variable) => Output::Variable {
                    warning,
                    variable: state_info(&variable, chain_label),
                },
                Err(error) => Output::Message {
                    level: Level::Warning,
                    text: format!("No such variable: `{name}` is not in scope at PC {pc}; {error}"),
                    note: unavailable.map(|reason| {
                        format!(
                            "locals are unavailable here: {reason}, so a local of that name \
                             cannot be looked up"
                        )
                    }),
                },
            };
        }

        let state = match (layout, words.as_ref()) {
            (Some(layout), _) if layout.variables.is_empty() => StateInfo::None,
            (Some(layout), Some(words)) => StateInfo::Variables {
                variables: state_variables(layout, words)
                    .iter()
                    .map(|variable| state_info(variable, chain_label))
                    .collect(),
            },
            (Some(_), None) => StateInfo::NoStorage,
            (None, _) => StateInfo::NoLayout,
        };
        let transient = match (transient_layout, transient_words.as_ref()) {
            (Some(layout), Some(words)) => state_variables(layout, words)
                .iter()
                .map(transient_info)
                .collect(),
            _ => Vec::new(),
        };
        Output::Variables {
            warning,
            pc,
            locals: locals.iter().map(local_info).collect(),
            unavailable,
            state,
            transient,
        }
    }

    /// The contracts whose debug resources are loaded.
    #[must_use]
    pub fn resources(&self, json: bool) -> Output {
        let contracts = self
            .state
            .step_map()
            .map_or(&[][..], |map| map.contracts())
            .iter()
            .filter(|contract| !contract.info.instructions.is_empty())
            .map(|contract| ResourceInfo {
                name: contract.name.clone(),
                address: contract.address.clone(),
                environment: contract.info.environment.clone(),
                sources: contract.info.sources.values().cloned().collect(),
            })
            .collect();
        Output::Resources { contracts, json }
    }

    /// The command list, or one command's details.
    #[must_use]
    pub fn help(topic: Option<&str>) -> Output {
        let lines = match topic.map(str::trim).filter(|topic| !topic.is_empty()) {
            Some(topic) => match command_spec(topic) {
                Some(spec) => {
                    let mut lines = vec![format!("{}  {}", spec.usage, spec.summary)];
                    if !spec.aliases.is_empty() {
                        lines.push(format!("aliases: {}", spec.aliases.join(", ")));
                    }
                    lines.extend(spec.details.iter().map(|line| (*line).to_owned()));
                    lines
                }
                None => vec![format!("No help for `{topic}`; type `help` for the list")],
            },
            None => {
                let width = COMMANDS
                    .iter()
                    .map(|spec| spec.usage.len())
                    .max()
                    .unwrap_or(0);
                let mut lines = Vec::new();
                for group in CommandGroup::ALL {
                    lines.push(format!("{}:", group.title()));
                    for spec in COMMANDS.iter().filter(|spec| spec.group == group) {
                        lines.push(format!(
                            "  {:<width$}  {}",
                            spec.usage,
                            spec.summary,
                            width = width
                        ));
                    }
                }
                lines.push("Aliases: n s ni fin c | rn rs rni rfin rc | b d i | bt l mem | p | h q; `help <command>` says more.".to_owned());
                lines
            }
        };
        Output::Help { lines }
    }
}

fn message(level: Level, text: impl Into<String>) -> Output {
    Output::Message {
        level,
        text: text.into(),
        note: None,
    }
}

fn status(status: DebugValueStatus) -> ValueStatus {
    match status {
        DebugValueStatus::Decoded => ValueStatus::Decoded,
        DebugValueStatus::Raw => ValueStatus::Raw,
        DebugValueStatus::Unavailable => ValueStatus::Unavailable,
    }
}

/// A local, parameter, or return value with the stack slot it was read from, or without
/// one when it could not be read.
fn local_info(variable: &DebugVariable) -> VariableInfo {
    VariableInfo {
        name: variable.name.clone(),
        ty: variable.ty.clone(),
        value: variable.value.display.clone(),
        raw: variable.value.raw.clone(),
        status: status(variable.value.status),
        place: (variable.value.status != DebugValueStatus::Unavailable)
            .then(|| format!("{}+{}", variable.location.kind, variable.location.offset)),
    }
}

fn state_info(variable: &StateVariable, chain_label: Option<&str>) -> VariableInfo {
    let mut place = if variable.offset == 0 {
        format!("slot {}", variable.slot)
    } else {
        format!("slot {} + {}", variable.slot, variable.offset)
    };
    if variable.source == StateSource::Chain {
        place.push_str(&format!(", from {}", chain_label.unwrap_or("the chain")));
    }
    VariableInfo {
        name: variable.name.clone(),
        ty: variable.ty.clone(),
        value: variable.value.display.clone(),
        raw: variable.value.raw.clone(),
        status: status(variable.value.status),
        place: Some(place),
    }
}

/// A `transient` state variable, its place marked so it is not read as a storage slot.
fn transient_info(variable: &StateVariable) -> VariableInfo {
    let place = if variable.offset == 0 {
        format!("transient slot {}", variable.slot)
    } else {
        format!("transient slot {} + {}", variable.slot, variable.offset)
    };
    VariableInfo {
        place: Some(place),
        ..state_info(variable, None)
    }
}

/// A recorded slot the way `storage` prints it: `0x`-prefixed, no leading zeros.
fn normalize_storage_slot(slot: &str) -> String {
    let digits = slot.trim_start_matches("0x").trim_start_matches('0');
    if digits.is_empty() {
        "0x0".to_owned()
    } else {
        format!("0x{}", digits.to_ascii_lowercase())
    }
}

/// Whether a breakpoint stops on a source line, for a view that marks lines.
#[must_use]
pub fn breakpoint_lines(state: &DebuggerState) -> Vec<(String, u64)> {
    state
        .breakpoints()
        .iter()
        .flat_map(|breakpoint| match &breakpoint.kind {
            BreakpointKind::Line(lines) => lines
                .iter()
                .map(|line| (line.path.clone(), line.key.line))
                .collect(),
            _ => Vec::new(),
        })
        .collect()
}