miden-debug 0.7.0

An interactive debugger for Miden VM programs
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
use std::rc::Rc;

use miden_assembly_syntax::diagnostics::Report;
use rustyline::{DefaultEditor, error::ReadlineError};

use super::commands::ReplCommand;
use crate::{
    config::DebuggerConfig,
    debug::{Breakpoint, BreakpointType},
    ui::state::State,
};

/// Interactive REPL session for the debugger.
pub struct ReplSession {
    state: State,
    editor: DefaultEditor,
}

impl ReplSession {
    /// Create a new REPL session from the given config.
    pub fn new(config: Box<DebuggerConfig>) -> Result<Self, Report> {
        let state = State::new(config)?;
        let editor = DefaultEditor::new()
            .map_err(|e| Report::msg(format!("failed to create editor: {e}")))?;

        Ok(Self { state, editor })
    }

    /// Run the main REPL loop.
    pub fn run(&mut self) -> Result<(), Report> {
        self.print_welcome();
        self.print_location();

        loop {
            let prompt = self.make_prompt();
            match self.editor.readline(&prompt) {
                Ok(line) => {
                    let line = line.trim();
                    if line.is_empty() {
                        continue;
                    }

                    // Add to history
                    let _ = self.editor.add_history_entry(line);

                    // Parse and execute command
                    match line.parse::<ReplCommand>() {
                        Ok(cmd) => {
                            if matches!(cmd, ReplCommand::Quit) {
                                println!("\x1b[36mGoodbye!\x1b[0m");
                                break;
                            }
                            if let Err(e) = self.execute_command(cmd) {
                                eprintln!("\x1b[31mError:\x1b[0m {e}");
                            }
                        }
                        Err(e) => {
                            eprintln!("\x1b[31mError:\x1b[0m {e}");
                        }
                    }
                }
                Err(ReadlineError::Interrupted) => {
                    println!("^C");
                    continue;
                }
                Err(ReadlineError::Eof) => {
                    println!("\x1b[36mGoodbye!\x1b[0m");
                    break;
                }
                Err(e) => {
                    eprintln!("\x1b[31mError reading line:\x1b[0m {e}");
                    break;
                }
            }
        }

        Ok(())
    }

    fn print_welcome(&self) {
        println!("\x1b[1;36mMiden Debugger REPL\x1b[0m");
        println!("Type \x1b[33mhelp\x1b[0m for available commands.");
        println!();
    }

    fn make_prompt(&self) -> String {
        let cycle = self.state.executor().cycle;

        if self.state.executor().stopped {
            if self.state.execution_failed().is_some() {
                format!("\x1b[36m[\x1b[0mcycle {cycle} \x1b[1;31mERR\x1b[0m\x1b[36m]\x1b[0m > ")
            } else {
                format!("\x1b[36m[\x1b[0mcycle {cycle} \x1b[1;32mEND\x1b[0m\x1b[36m]\x1b[0m > ")
            }
        } else if self.state.stopped {
            format!("\x1b[36m[\x1b[0mcycle {cycle} \x1b[1;33mSTOP\x1b[0m\x1b[36m]\x1b[0m > ")
        } else {
            format!("\x1b[36m[\x1b[0mcycle {cycle}\x1b[36m]\x1b[0m > ")
        }
    }

    fn print_location(&self) {
        let proc_name = self.state.current_procedure().unwrap_or_else(|| Rc::from("<unknown>"));
        if let Some(resolved) = self.state.current_display_location() {
            println!("at {} in {}", resolved, proc_name);
        } else if self.state.executor().callstack.current_frame().is_some() {
            println!("in {}", proc_name);
        }
    }

    fn execute_command(&mut self, cmd: ReplCommand) -> Result<(), String> {
        match cmd {
            ReplCommand::Step => self.cmd_step(1),
            ReplCommand::StepN(n) => self.cmd_step(n),
            ReplCommand::Next => self.cmd_next(),
            ReplCommand::NextLine => self.cmd_next_line(),
            ReplCommand::Continue => self.cmd_continue(),
            ReplCommand::Finish => self.cmd_finish(),
            ReplCommand::Break(bp_type) => self.cmd_break(bp_type),
            ReplCommand::Breakpoints => self.cmd_breakpoints(),
            ReplCommand::Delete(id) => self.cmd_delete(id),
            ReplCommand::Stack => self.cmd_stack(),
            ReplCommand::Memory(expr) => self.cmd_memory(&expr),
            ReplCommand::Locals => self.cmd_locals(),
            ReplCommand::Vars(show_all) => self.cmd_vars(show_all),
            ReplCommand::Where => self.cmd_where(),
            ReplCommand::List => self.cmd_list(),
            ReplCommand::Backtrace => self.cmd_backtrace(),
            ReplCommand::Reload => self.cmd_reload(),
            ReplCommand::Help => self.cmd_help(),
            ReplCommand::Quit => unreachable!("quit handled in run loop"),
        }
    }

    fn cmd_step(&mut self, n: usize) -> Result<(), String> {
        if self.state.executor().stopped {
            return Err("program has terminated, cannot step".into());
        }

        for _ in 0..n {
            if self.state.executor().stopped {
                break;
            }
            match self.state.executor_mut().step() {
                Ok(_) => {}
                Err(err) => {
                    let msg = format!("execution error: {err}");
                    self.state.set_execution_failed(err);
                    return Err(msg);
                }
            }
        }

        self.print_location();
        Ok(())
    }

    fn cmd_next(&mut self) -> Result<(), String> {
        if self.state.executor().stopped {
            return Err("program has terminated, cannot continue".into());
        }

        self.state.create_breakpoint(BreakpointType::Next);
        self.state.stopped = false;
        self.run_until_stopped();
        self.print_location();
        Ok(())
    }

    fn cmd_next_line(&mut self) -> Result<(), String> {
        if self.state.executor().stopped {
            return Err("program has terminated, cannot continue".into());
        }

        self.state.create_breakpoint(BreakpointType::NextLine);
        self.state.stopped = false;
        self.run_until_stopped();
        self.print_location();
        Ok(())
    }

    fn cmd_continue(&mut self) -> Result<(), String> {
        if self.state.executor().stopped {
            return Err("program has terminated, cannot continue".into());
        }

        self.state.stopped = false;
        self.run_until_stopped();

        if self.state.executor().stopped {
            if let Some(err) = self.state.execution_failed() {
                println!("Program terminated with error: {}", err);
            } else {
                println!("Program terminated successfully");
            }
        } else {
            self.print_location();
        }

        Ok(())
    }

    fn cmd_finish(&mut self) -> Result<(), String> {
        if self.state.executor().stopped {
            return Err("program has terminated, cannot continue".into());
        }

        self.state.create_breakpoint(BreakpointType::Finish);
        self.state.stopped = false;
        self.run_until_stopped();
        self.print_location();
        Ok(())
    }

    fn run_until_stopped(&mut self) {
        let start_cycle = self.state.executor().cycle;
        let start_asmop = self.state.executor().current_asmop.clone();
        let start_proc = self.state.current_procedure();
        let start_line_loc = self.state.current_display_location();
        let mut previous_proc = self.state.current_procedure();
        let mut pending_called_breakpoints = Vec::new();
        let mut breakpoints: Vec<Breakpoint> = core::mem::take(&mut self.state.breakpoints);
        self.state.breakpoints_hit.clear();

        loop {
            // Check if program has terminated
            if self.state.executor().stopped {
                self.state.stopped = true;
                break;
            }

            let mut consume_most_recent_finish = false;
            match self.state.executor_mut().step() {
                Ok(Some(ref exited)) if exited.should_break_on_exit() => {
                    consume_most_recent_finish = true;
                }
                Ok(_) => {}
                Err(err) => {
                    self.state.set_execution_failed(err);
                    self.state.stopped = true;
                    break;
                }
            }

            if breakpoints.is_empty() {
                continue;
            }

            // Get current execution state for breakpoint checking
            let is_op_boundary = self.state.executor().current_asmop.is_some();
            let loc = self.state.current_location();
            let line_loc = self.state.current_display_location();
            let proc = self.state.current_procedure();

            // Check breakpoints
            let current_cycle = self.state.executor().cycle;
            let cycles_stepped = current_cycle - start_cycle;

            breakpoints.retain_mut(|bp| {
                if let Some(n) = bp.cycles_to_skip(current_cycle) {
                    if cycles_stepped >= n {
                        let retained = !bp.is_one_shot();
                        if retained {
                            self.state.breakpoints_hit.push(bp.clone());
                        } else {
                            self.state.breakpoints_hit.push(core::mem::take(bp));
                        }
                        return retained;
                    }
                    return true;
                }

                if cycles_stepped > 0
                    && is_op_boundary
                    && matches!(&bp.ty, BreakpointType::Next)
                    && self.state.executor().current_asmop != start_asmop
                {
                    self.state.breakpoints_hit.push(core::mem::take(bp));
                    return false;
                }

                if cycles_stepped > 0
                    && is_op_boundary
                    && matches!(&bp.ty, BreakpointType::NextLine)
                {
                    let has_source_context = start_line_loc.is_some() || line_loc.is_some();
                    let reached_next = if has_source_context {
                        State::is_next_source_line(
                            start_proc.as_deref(),
                            start_line_loc.as_ref(),
                            proc.as_deref(),
                            line_loc.as_ref(),
                        )
                    } else {
                        self.state.executor().current_asmop != start_asmop
                    };
                    if reached_next {
                        self.state.breakpoints_hit.push(core::mem::take(bp));
                        return false;
                    }
                }

                if let Some(loc) = loc.as_ref()
                    && bp.should_break_at(loc)
                {
                    let retained = !bp.is_one_shot();
                    if retained {
                        self.state.breakpoints_hit.push(bp.clone());
                    } else {
                        self.state.breakpoints_hit.push(core::mem::take(bp));
                    }
                    return retained;
                }

                if matches!(&bp.ty, BreakpointType::Called(_))
                    && let Some(proc) = proc.as_deref()
                {
                    let matched = bp.should_break_in(proc);
                    if !matched {
                        pending_called_breakpoints.retain(|id| *id != bp.id);
                        return true;
                    }

                    let was_matched = previous_proc
                        .as_deref()
                        .is_some_and(|previous| bp.should_break_in(previous));
                    let matched_at_start =
                        start_proc.as_deref().is_some_and(|start| bp.should_break_in(start));
                    let pending = pending_called_breakpoints.contains(&bp.id);
                    let entered_matching_proc = !was_matched && !matched_at_start;

                    if entered_matching_proc
                        && self.state.executor().procedure_has_debug_vars(proc)
                        && self.state.executor().last_debug_var_count == 0
                    {
                        if !pending {
                            pending_called_breakpoints.push(bp.id);
                        }
                        return true;
                    }

                    if entered_matching_proc
                        || (pending && self.state.executor().last_debug_var_count > 0)
                    {
                        pending_called_breakpoints.retain(|id| *id != bp.id);
                        let retained = !bp.is_one_shot();
                        if retained {
                            self.state.breakpoints_hit.push(bp.clone());
                        } else {
                            self.state.breakpoints_hit.push(core::mem::take(bp));
                        }
                        return retained;
                    }
                }

                true
            });

            // Handle Finish breakpoint
            if consume_most_recent_finish
                && let Some(id) = breakpoints.iter().rev().find_map(|bp| {
                    if matches!(bp.ty, BreakpointType::Finish) {
                        Some(bp.id)
                    } else {
                        None
                    }
                })
            {
                breakpoints.retain(|bp| bp.id != id);
                self.state.stopped = true;
                break;
            }

            if !self.state.breakpoints_hit.is_empty() {
                self.state.stopped = true;
                break;
            }

            previous_proc = proc;
        }

        // Restore breakpoints
        self.state.breakpoints = breakpoints;
    }

    fn cmd_break(&mut self, bp_type: BreakpointType) -> Result<(), String> {
        self.state.create_breakpoint(bp_type.clone());
        let id = self.state.breakpoints.last().map(|bp| bp.id).unwrap_or(0);
        println!("Breakpoint {} set: {}", id, format_bp_type(&bp_type));
        Ok(())
    }

    fn cmd_breakpoints(&mut self) -> Result<(), String> {
        if self.state.breakpoints.is_empty() {
            println!("No breakpoints set");
            return Ok(());
        }

        println!("Breakpoints:");
        for bp in &self.state.breakpoints {
            if !bp.is_internal() {
                println!("  [{}] {}", bp.id, format_bp_type(&bp.ty));
            }
        }
        Ok(())
    }

    fn cmd_delete(&mut self, id: Option<u8>) -> Result<(), String> {
        match id {
            Some(id) => {
                let count_before = self.state.breakpoints.len();
                self.state.breakpoints.retain(|bp| bp.id != id);
                if self.state.breakpoints.len() < count_before {
                    println!("Deleted breakpoint {}", id);
                } else {
                    return Err(format!("no breakpoint with id {}", id));
                }
            }
            None => {
                // Delete only user-created (non-internal) breakpoints
                self.state.breakpoints.retain(|bp| bp.is_internal());
                println!("Deleted all breakpoints");
            }
        }
        Ok(())
    }

    fn cmd_stack(&mut self) -> Result<(), String> {
        let stack = &self.state.executor().current_stack;

        if stack.is_empty() {
            println!("Stack is empty");
            return Ok(());
        }

        println!("Operand Stack ({} elements):", stack.len());
        for (i, elem) in stack.iter().enumerate() {
            let val = elem.as_canonical_u64();
            let marker = if i == 0 { ">" } else { " " };
            println!("  {} [{}] {} (0x{:x})", marker, i, val, val);
        }
        Ok(())
    }

    fn cmd_memory(&mut self, expr: &crate::debug::ReadMemoryExpr) -> Result<(), String> {
        let result = self.state.read_memory(expr)?;
        println!("{}", result);
        Ok(())
    }

    fn cmd_locals(&mut self) -> Result<(), String> {
        let output = self.state.format_variables(false);
        println!("{}", output);
        Ok(())
    }

    fn cmd_vars(&mut self, show_all: bool) -> Result<(), String> {
        let output = self.state.format_variables(show_all);
        println!("{}", output);
        Ok(())
    }

    fn cmd_where(&mut self) -> Result<(), String> {
        if self.state.executor().callstack.current_frame().is_some() {
            let proc_name = self.state.current_procedure().unwrap_or_else(|| Rc::from("<unknown>"));

            if let Some(resolved) = self.state.current_display_location() {
                println!(
                    "{}:{}:{} in {}",
                    resolved.source_file.uri().as_str(),
                    resolved.line,
                    resolved.col,
                    proc_name
                );
            } else {
                println!("in {} (no source location available)", proc_name);
            }
        } else {
            println!("No current frame");
        }
        Ok(())
    }

    fn cmd_list(&mut self) -> Result<(), String> {
        if let Some(frame) = self.state.executor().callstack.current_frame() {
            let recent = frame.recent();
            if recent.is_empty() {
                println!("No recent instructions");
                return Ok(());
            }

            println!("Recent instructions:");
            for (i, op) in recent.iter().enumerate() {
                let marker = if i == recent.len() - 1 { ">" } else { " " };
                println!("  {} {}", marker, op.display());
            }
        } else {
            println!("No current frame");
        }
        Ok(())
    }

    fn cmd_backtrace(&mut self) -> Result<(), String> {
        let frames = self.state.executor().callstack.frames();
        if frames.is_empty() {
            println!("No call stack");
            return Ok(());
        }

        println!("Backtrace ({} frames):", frames.len());
        for (i, frame) in frames.iter().rev().enumerate() {
            let proc_name = frame.procedure("").unwrap_or_else(|| Rc::from("<unknown>"));
            let loc_str = frame
                .last_resolved(&*self.state.source_manager)
                .map(|r| format!(" at {}", r))
                .unwrap_or_default();

            println!("  #{} {}{}", i, proc_name, loc_str);
        }
        Ok(())
    }

    fn cmd_reload(&mut self) -> Result<(), String> {
        self.state.reload().map_err(|e| format!("reload failed: {e}"))?;
        println!("Program reloaded");
        self.print_location();
        Ok(())
    }

    fn cmd_help(&mut self) -> Result<(), String> {
        println!("{}", ReplCommand::help_text());
        Ok(())
    }
}

fn format_bp_type(ty: &BreakpointType) -> String {
    match ty {
        BreakpointType::Step => "next cycle".into(),
        BreakpointType::StepN(n) => format!("after {} cycles", n),
        BreakpointType::StepTo(c) => format!("at cycle {}", c),
        BreakpointType::Next => "next instruction".into(),
        BreakpointType::NextLine => "next source line".into(),
        BreakpointType::Finish => "function return".into(),
        BreakpointType::File(pat) => pat.as_str().to_string(),
        BreakpointType::Line { pattern, line } => format!("{}:{}", pattern.as_str(), line),
        BreakpointType::Opcode(op) => format!("opcode {:?}", op),
        BreakpointType::Called(pat) => format!("call {}", pat.as_str()),
    }
}