soldb-repl 0.4.0

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
//! The command language of the debugger: what a typed line means.
//!
//! One table, [`COMMANDS`], describes every command once — its name, its aliases, its
//! usage, and its help — and both the parser and `help` are built from it, so the two
//! cannot drift apart. Parsing never fails loudly: a line that is not a command becomes
//! [`DebuggerCommand::Unknown`], which the session answers with a message, the way a
//! shell does.

use crate::{BreakpointTarget, DisplayMode, SourceBreakpointTarget};

/// A command as typed, with its arguments parsed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DebuggerCommand {
    Next,
    NextInstruction,
    Step,
    Continue,
    /// Run until the current frame returns to its caller.
    Finish,
    /// Step back to the previous source step.
    ReverseNext,
    /// Step back one EVM instruction.
    ReverseNextInstruction,
    /// Step back into the previous instruction, the mirror of `step`.
    ReverseStep,
    /// Run backward until a breakpoint or the first step.
    ReverseContinue,
    /// Run backward to the step that entered the current frame.
    ReverseFinish,
    Goto(usize),
    /// List every variable in scope at the current step, and the state variables.
    Vars,
    /// Print one variable, or a path into one, at the current step.
    ///
    /// An empty name means the user typed `print` with no argument; the session reports
    /// the usage rather than treating it as an unknown command.
    Print(String),
    Info(DebuggerInfoCommand),
    /// The call structure at the current step.
    Backtrace,
    /// The source around the current step.
    List,
    /// Memory at the current step, optionally one range of it.
    Memory {
        offset: Option<u64>,
        length: Option<u64>,
    },
    /// The calldata of the current call frame.
    Calldata,
    /// The stack at the current step, all of it or the top `limit` words.
    Stack {
        limit: Option<usize>,
    },
    Mode(Option<DisplayMode>),
    /// Set a breakpoint, optionally one that stops only when a condition holds.
    Break(BreakpointTarget, Option<String>),
    Clear(BreakpointTarget),
    /// Remove a breakpoint by number.
    Delete(u32),
    Help(Option<String>),
    /// Open the full-screen view.
    Tui,
    Quit,
    Empty,
    Unknown(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DebuggerInfoCommand {
    Resources { json: bool },
    Breakpoints,
    Storage,
}

/// What a command is for, which is how `help` groups them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandGroup {
    Stepping,
    Reverse,
    Breakpoints,
    Inspection,
    Variables,
    Session,
}

impl CommandGroup {
    /// Every group, in the order `help` lists them.
    pub const ALL: [Self; 6] = [
        Self::Stepping,
        Self::Reverse,
        Self::Breakpoints,
        Self::Inspection,
        Self::Variables,
        Self::Session,
    ];

    #[must_use]
    pub fn title(self) -> &'static str {
        match self {
            Self::Stepping => "Stepping",
            Self::Reverse => "Reverse",
            Self::Breakpoints => "Breakpoints",
            Self::Inspection => "Inspection",
            Self::Variables => "Variables",
            Self::Session => "Session",
        }
    }
}

/// One command's description: what `help` prints, and what the parser answers to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommandSpec {
    pub name: &'static str,
    pub aliases: &'static [&'static str],
    /// The command with its arguments, as typed.
    pub usage: &'static str,
    /// One line saying what it does.
    pub summary: &'static str,
    /// Further lines `help <command>` prints.
    pub details: &'static [&'static str],
    pub group: CommandGroup,
}

impl CommandSpec {
    /// The name followed by its aliases in parentheses, as `help` lists it.
    #[must_use]
    pub fn names(&self) -> String {
        if self.aliases.is_empty() {
            self.name.to_owned()
        } else {
            format!("{} ({})", self.name, self.aliases.join(", "))
        }
    }

    fn answers_to(&self, word: &str) -> bool {
        self.name == word || self.aliases.contains(&word)
    }
}

/// Every command, in the order `help` lists them.
pub const COMMANDS: &[CommandSpec] = &[
    CommandSpec {
        name: "next",
        aliases: &["n"],
        usage: "next",
        summary: "run to the next source line, stepping over calls",
        details: &["Without debug info, moves one instruction."],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "step",
        aliases: &["s"],
        usage: "step",
        summary: "run to the next source line, entering calls",
        details: &["Without debug info, moves one instruction."],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "nexti",
        aliases: &["ni", "stepi", "si"],
        usage: "nexti",
        summary: "execute one EVM instruction",
        details: &[],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "finish",
        aliases: &["fin"],
        usage: "finish",
        summary: "run until the current frame returns",
        details: &[],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "continue",
        aliases: &["c"],
        usage: "continue",
        summary: "run to the next breakpoint, or the end of the trace",
        details: &[],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "goto",
        aliases: &[],
        usage: "goto <step>",
        summary: "jump to a step of the recording",
        details: &[],
        group: CommandGroup::Stepping,
    },
    CommandSpec {
        name: "reverse-next",
        aliases: &["rn", "rnext"],
        usage: "reverse-next",
        summary: "back to the previous source line, stepping over calls",
        details: &[],
        group: CommandGroup::Reverse,
    },
    CommandSpec {
        name: "reverse-step",
        aliases: &["rs", "rstep"],
        usage: "reverse-step",
        summary: "back to the previous source line, entering calls",
        details: &[],
        group: CommandGroup::Reverse,
    },
    CommandSpec {
        name: "reverse-nexti",
        aliases: &["rni", "back", "rnexti", "reverse-stepi", "rsi"],
        usage: "reverse-nexti",
        summary: "back one EVM instruction",
        details: &[],
        group: CommandGroup::Reverse,
    },
    CommandSpec {
        name: "reverse-finish",
        aliases: &["rfin", "rfinish"],
        usage: "reverse-finish",
        summary: "back to the step that entered the current frame",
        details: &[],
        group: CommandGroup::Reverse,
    },
    CommandSpec {
        name: "reverse-continue",
        aliases: &["rc", "rcontinue"],
        usage: "reverse-continue",
        summary: "back to the previous breakpoint, or the start of the trace",
        details: &[],
        group: CommandGroup::Reverse,
    },
    CommandSpec {
        name: "break",
        aliases: &["b"],
        usage: "break <target> [if <condition>]",
        summary: "stop at a place, or there only when a condition holds",
        details: &[
            "Targets:",
            "  <pc>                        a program counter",
            "  <file>:<line>, line <line>  entering a source line",
            "  <function>, <Contract>.<f>  entering a function",
            "  <state variable>            a write to it: counter, balances[0xabc], config.limit",
            "  storage <slot>              an SSTORE to a slot",
            "  revert                      a REVERT, or a failing step",
            "  call [<address>]            a call, to one address or to any",
            "  op <OPCODE>                 every execution of an opcode",
            "A condition compares locals and paths into them (item.tags[1]), state variables,",
            "the frame's arguments, enum literals (Color.Blue), and pc, gas, depth, op, step,",
            "with == != < <= > >=, joined with && and ||:",
            "  break Shop.sol:40 if price > 10 && item.color == Color.Blue",
        ],
        group: CommandGroup::Breakpoints,
    },
    CommandSpec {
        name: "clear",
        aliases: &[],
        usage: "clear <target>",
        summary: "remove the breakpoint set with that target",
        details: &[],
        group: CommandGroup::Breakpoints,
    },
    CommandSpec {
        name: "delete",
        aliases: &["d"],
        usage: "delete <n>",
        summary: "remove breakpoint number n",
        details: &[],
        group: CommandGroup::Breakpoints,
    },
    CommandSpec {
        name: "info",
        aliases: &["i"],
        usage: "info breakpoints|storage|resources [--json]",
        summary: "the breakpoints, the storage known here, or the loaded debug resources",
        details: &[
            "info breakpoints (i b)  every breakpoint with its number",
            "info storage            every storage slot known at this step",
            "info resources          the contracts whose sources are loaded, as text or JSON",
        ],
        group: CommandGroup::Breakpoints,
    },
    CommandSpec {
        name: "backtrace",
        aliases: &["bt", "where"],
        usage: "backtrace",
        summary: "the call frames at the current step, innermost first",
        details: &[
            "Each frame names its function, or the contract or address executing, where it",
            "is, and the step and program counter it sits at. A frame entered at its function's",
            "entry point shows the arguments it was entered with.",
        ],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "list",
        aliases: &["l"],
        usage: "list",
        summary: "the source around the current step",
        details: &[],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "stack",
        aliases: &[],
        usage: "stack [<n>]",
        summary: "the EVM stack at the current step, top first, all of it or the top n words",
        details: &[
            "Each word is shown with its index from the bottom of the stack, which is the",
            "index `vars` reports a variable's slot as.",
        ],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "memory",
        aliases: &["mem"],
        usage: "memory [<offset> [<length>]]",
        summary: "memory at the current step, in 32-byte words",
        details: &[],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "storage",
        aliases: &[],
        usage: "storage",
        summary: "every storage slot known at the current step",
        details: &["The same as `info storage`."],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "calldata",
        aliases: &[],
        usage: "calldata",
        summary: "the calldata of the current frame",
        details: &[],
        group: CommandGroup::Inspection,
    },
    CommandSpec {
        name: "vars",
        aliases: &["locals"],
        usage: "vars",
        summary: "the variables in scope at the current step, and every state variable",
        details: &[
            "Locals come from the artifact's ETHDebug variable locations when it carries any.",
            "For solc's legacy pipeline they are read off the stack through the fixed layout",
            "its code generator keeps, which the first `vars` of a session says: values can",
            "be wrong under the optimizer inside one basic block, and a frame that could not",
            "be placed, or a function the optimizer inlined, shows as unavailable. State",
            "variables are read through the storage layout of `solc --storage-layout`.",
        ],
        group: CommandGroup::Variables,
    },
    CommandSpec {
        name: "print",
        aliases: &["p"],
        usage: "print <variable>|<path>",
        summary: "one variable, or a path into one: item.tags[1], balances[0xabc], stored.owner",
        details: &[
            "A path follows struct members, array elements, mapping entries, and `length`",
            "from a local or a state variable, in any chain.",
        ],
        group: CommandGroup::Variables,
    },
    CommandSpec {
        name: "mode",
        aliases: &[],
        usage: "mode [source|asm]",
        summary: "show or switch what a stop prints: the source line, or the stack",
        details: &[],
        group: CommandGroup::Session,
    },
    CommandSpec {
        name: "tui",
        aliases: &[],
        usage: "tui",
        summary: "open the full-screen view; q returns to this prompt",
        details: &[],
        group: CommandGroup::Session,
    },
    CommandSpec {
        name: "help",
        aliases: &["h", "?"],
        usage: "help [<command>]",
        summary: "this list, or one command's details",
        details: &[],
        group: CommandGroup::Session,
    },
    CommandSpec {
        name: "quit",
        aliases: &["q", "exit"],
        usage: "quit",
        summary: "leave the debugger",
        details: &[],
        group: CommandGroup::Session,
    },
];

/// The command a word names, by name or alias.
#[must_use]
pub fn command_spec(word: &str) -> Option<&'static CommandSpec> {
    let word = word.trim().to_ascii_lowercase();
    COMMANDS.iter().find(|spec| spec.answers_to(&word))
}

impl DebuggerCommand {
    /// Parses one typed line.
    #[must_use]
    pub fn parse(line: &str) -> Self {
        let line = line.trim();
        if line.is_empty() {
            return Self::Empty;
        }
        let mut parts = line.split_whitespace();
        let word = parts.next().unwrap_or_default();
        let rest = parts.collect::<Vec<_>>().join(" ");
        let Some(spec) = command_spec(word) else {
            return Self::Unknown(line.to_owned());
        };
        let unknown = || Self::Unknown(line.to_owned());
        match spec.name {
            "next" => Self::Next,
            "nexti" => Self::NextInstruction,
            "step" => Self::Step,
            "continue" => Self::Continue,
            "finish" => Self::Finish,
            "reverse-next" => Self::ReverseNext,
            "reverse-nexti" => Self::ReverseNextInstruction,
            "reverse-step" => Self::ReverseStep,
            "reverse-continue" => Self::ReverseContinue,
            "reverse-finish" => Self::ReverseFinish,
            "goto" => rest.parse::<usize>().map_or_else(|_| unknown(), Self::Goto),
            "vars" => Self::Vars,
            "print" => Self::Print(rest),
            "backtrace" => Self::Backtrace,
            "list" => Self::List,
            "memory" => parse_memory_command(&rest).unwrap_or_else(unknown),
            "calldata" => Self::Calldata,
            "stack" => {
                if rest.is_empty() {
                    Self::Stack { limit: None }
                } else {
                    rest.parse::<usize>()
                        .map_or_else(|_| unknown(), |limit| Self::Stack { limit: Some(limit) })
                }
            }
            "storage" => Self::Info(DebuggerInfoCommand::Storage),
            "info" => parse_info_command(&rest).map_or_else(unknown, Self::Info),
            "mode" => Self::Mode(
                (!rest.is_empty())
                    .then(|| DisplayMode::parse(&rest))
                    .flatten(),
            ),
            "break" => {
                // `break <target> if <condition>`: the condition is everything after the
                // first ` if `, so a target containing `if` in a name still parses.
                let (target, condition) = split_condition(&rest);
                parse_breakpoint_target(target)
                    .map_or_else(unknown, |target| Self::Break(target, condition))
            }
            "clear" => parse_breakpoint_target(&rest).map_or_else(unknown, Self::Clear),
            "delete" => rest
                .trim()
                .trim_start_matches('#')
                .parse::<u32>()
                .map_or_else(|_| unknown(), Self::Delete),
            "help" => Self::Help((!rest.is_empty()).then_some(rest)),
            "tui" => Self::Tui,
            "quit" => Self::Quit,
            _ => unknown(),
        }
    }
}

fn parse_info_command(input: &str) -> Option<DebuggerInfoCommand> {
    match input.trim() {
        "resources" => Some(DebuggerInfoCommand::Resources { json: false }),
        "resources --json" | "resources json" => {
            Some(DebuggerInfoCommand::Resources { json: true })
        }
        "breakpoints" | "break" | "b" => Some(DebuggerInfoCommand::Breakpoints),
        "storage" => Some(DebuggerInfoCommand::Storage),
        _ => None,
    }
}

fn parse_memory_command(input: &str) -> Option<DebuggerCommand> {
    let mut parts = input.split_whitespace();
    let offset = match parts.next() {
        Some(text) => Some(parse_u64_arg(text)?),
        None => None,
    };
    let length = match parts.next() {
        Some(text) => Some(parse_u64_arg(text)?),
        None => None,
    };
    if parts.next().is_some() {
        return None;
    }
    Some(DebuggerCommand::Memory { offset, length })
}

fn parse_u64_arg(input: &str) -> Option<u64> {
    let input = input.trim();
    if let Some(hex) = input.strip_prefix("0x") {
        u64::from_str_radix(hex, 16).ok()
    } else {
        input.parse::<u64>().ok()
    }
}

fn parse_breakpoint_target(input: &str) -> Option<BreakpointTarget> {
    let input = input.trim();
    if input.is_empty() {
        return None;
    }
    let mut parts = input.splitn(2, char::is_whitespace);
    let head = parts.next().unwrap_or_default();
    let rest = parts.next().map(str::trim).unwrap_or_default();
    match head.to_ascii_lowercase().as_str() {
        "storage" | "slot" => {
            (!rest.is_empty()).then(|| BreakpointTarget::Storage(rest.to_owned()))
        }
        "revert" if rest.is_empty() => Some(BreakpointTarget::Revert),
        "call" => Some(BreakpointTarget::Call(
            (!rest.is_empty()).then(|| rest.to_owned()),
        )),
        "op" | "opcode" => (!rest.is_empty() && !rest.contains(char::is_whitespace))
            .then(|| BreakpointTarget::Opcode(rest.to_owned())),
        _ => {
            if let Some(target) = parse_source_breakpoint_target(input) {
                return Some(BreakpointTarget::SourceLine(target));
            }
            if let Some(pc) = parse_u64_arg(input) {
                return Some(BreakpointTarget::Pc(pc));
            }
            if is_function_name(input) {
                return Some(BreakpointTarget::Function(input.to_owned()));
            }
            // `balances[0xabc…]` and the like: a place in storage, named the way `print`
            // names it.
            is_state_path(input).then(|| BreakpointTarget::State(input.to_owned()))
        }
    }
}

/// Splits `<target> if <condition>` at the first ` if `, which cannot appear inside a
/// target: a file name, a function name, a slot, and an opcode all lack spaces.
fn split_condition(input: &str) -> (&str, Option<String>) {
    let input = input.trim();
    match input.find(" if ") {
        Some(index) => {
            let condition = input[index + 4..].trim();
            (
                input[..index].trim(),
                (!condition.is_empty()).then(|| condition.to_owned()),
            )
        }
        None => (input, None),
    }
}

/// Whether the text looks like a state variable path: a name followed by any number of
/// `[key]` and `.member` steps.
fn is_state_path(input: &str) -> bool {
    let head = input.split(['[', '.']).next().unwrap_or_default();
    !head.is_empty() && is_function_name(head) && input.len() > head.len()
}

fn is_function_name(input: &str) -> bool {
    let mut segments = input.split('.');
    segments.all(|segment| {
        let mut bytes = segment.bytes();
        bytes
            .next()
            .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
            && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
    })
}

fn parse_source_breakpoint_target(input: &str) -> Option<SourceBreakpointTarget> {
    let input = input.trim();
    if let Some(line) = input.strip_prefix("line ") {
        return parse_source_line_number(line)
            .map(|line| SourceBreakpointTarget { file: None, line });
    }

    let (file, line) = input.rsplit_once(':')?;
    let file = file.trim();
    if file.is_empty() {
        return None;
    }
    parse_source_line_number(line).map(|line| SourceBreakpointTarget {
        file: Some(file.to_owned()),
        line,
    })
}

fn parse_source_line_number(input: &str) -> Option<u64> {
    input.trim().parse::<u64>().ok().filter(|line| *line > 0)
}

#[cfg(test)]
mod tests {
    use super::{command_spec, CommandGroup, DebuggerCommand, COMMANDS};

    #[test]
    fn every_command_is_named_once_and_parses_by_each_alias() {
        let mut seen = Vec::new();
        for spec in COMMANDS {
            for word in std::iter::once(&spec.name).chain(spec.aliases) {
                assert!(!seen.contains(word), "`{word}` names two commands");
                seen.push(word);
                assert_eq!(command_spec(word).map(|found| found.name), Some(spec.name));
            }
            assert!(!spec.summary.is_empty());
            assert!(spec.usage.starts_with(spec.name), "{}", spec.usage);
        }
        assert_eq!(
            CommandGroup::ALL.len(),
            6,
            "every group must be listed for help"
        );
        assert_eq!(
            DebuggerCommand::parse("STACK 4"),
            DebuggerCommand::Stack { limit: Some(4) }
        );
        assert_eq!(DebuggerCommand::parse("tui"), DebuggerCommand::Tui);
        assert_eq!(DebuggerCommand::parse("?"), DebuggerCommand::Help(None));
        assert_eq!(
            DebuggerCommand::parse("help break"),
            DebuggerCommand::Help(Some("break".to_owned()))
        );
        assert_eq!(
            DebuggerCommand::parse("frobnicate"),
            DebuggerCommand::Unknown("frobnicate".to_owned())
        );
    }
}