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
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
use miden_assembly_syntax::diagnostics::{IntoDiagnostic, Report};
use ratatui::{
    crossterm::event::{KeyCode, KeyEvent},
    prelude::*,
};
use tokio::sync::mpsc::UnboundedSender;

use crate::{
    debug::{BreakpointType, ReadMemoryExpr},
    ui::{
        action::Action,
        pages::Page,
        panes::{
            Pane, breakpoints::BreakpointsPane, disasm::DisassemblyPane,
            source_code::SourceCodePane, stack::OperandStackPane, stacktrace::StackTracePane,
        },
        state::{InputMode, State},
        tui::EventResponse,
    },
};

#[derive(Default)]
pub struct Home {
    command_tx: Option<UnboundedSender<Action>>,
    panes: Vec<Box<dyn Pane>>,
    focused_pane_index: usize,
    fullscreen_pane_index: Option<usize>,
}

impl Home {
    pub fn new() -> Result<Self, Report> {
        let focused_border_style = Style::default().fg(Color::LightGreen);

        Ok(Self {
            command_tx: None,
            panes: vec![
                Box::new(SourceCodePane::new(true, focused_border_style)),
                Box::new(DisassemblyPane::new(false, focused_border_style)),
                Box::new(StackTracePane::new(false, focused_border_style)),
                Box::new(OperandStackPane::new(false, focused_border_style)),
                Box::new(BreakpointsPane::new(false, focused_border_style)),
            ],

            focused_pane_index: 0,
            fullscreen_pane_index: None,
        })
    }
}

impl Page for Home {
    fn init(&mut self, state: &State) -> Result<(), Report> {
        for pane in self.panes.iter_mut() {
            pane.init(state)?;
        }
        Ok(())
    }

    fn focus(&mut self) -> Result<(), Report> {
        if let Some(command_tx) = &self.command_tx {
            const ARROW: &str = symbols::scrollbar::HORIZONTAL.end;
            let status_line =
                format!("[l,h {ARROW} pane movement] [: {ARROW} commands] [q {ARROW} quit]");
            command_tx.send(Action::StatusLine(status_line)).into_diagnostic()?;
        }
        Ok(())
    }

    fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), Report> {
        self.command_tx = Some(tx);
        Ok(())
    }

    fn update(&mut self, action: Action, state: &mut State) -> Result<Option<Action>, Report> {
        let mut actions: Vec<Option<Action>> = vec![];
        match action {
            Action::Tick => {}
            Action::FocusNext => {
                let next_index = self.focused_pane_index.saturating_add(1) % self.panes.len();
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::UnFocus, state)?);
                }
                self.focused_pane_index = next_index;
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::Focus, state)?);
                }
            }
            Action::FocusPrev => {
                let prev_index =
                    self.focused_pane_index.saturating_add(self.panes.len() - 1) % self.panes.len();
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::UnFocus, state)?);
                }
                self.focused_pane_index = prev_index;
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::Focus, state)?);
                }
            }
            Action::Update => {
                for pane in self.panes.iter_mut() {
                    actions.push(pane.update(action.clone(), state)?);
                }
            }
            Action::ToggleFullScreen => {
                self.fullscreen_pane_index =
                    self.fullscreen_pane_index.map_or(Some(self.focused_pane_index), |_| None);
            }
            Action::FocusFooter(..) => {
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::UnFocus, state)?);
                }
            }
            Action::FooterResult(cmd, Some(args)) if cmd.eq(":") => {
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    pane.update(Action::Focus, state)?;
                }
                // Dispatch commands of the form: CMD [ARGS..]
                match args.split_once(' ') {
                    Some((cmd, rest)) => match cmd.trim() {
                        "b" | "break" | "breakpoint" => match rest.parse::<BreakpointType>() {
                            Ok(ty) => {
                                state.create_breakpoint(ty);
                                actions.push(Some(Action::TimedStatusLine(
                                    "breakpoint created".to_string(),
                                    1,
                                )));
                            }
                            Err(err) => {
                                actions.push(Some(Action::TimedStatusLine(err, 5)));
                            }
                        },
                        "r" | "read" => match rest.parse::<ReadMemoryExpr>() {
                            Ok(expr) => match state.read_memory(&expr) {
                                Ok(result) => actions.push(Some(Action::StatusLine(result))),
                                Err(err) => actions.push(Some(Action::TimedStatusLine(err, 5))),
                            },
                            Err(err) => actions.push(Some(Action::TimedStatusLine(err, 5))),
                        },
                        "vars" | "variables" | "locals" => {
                            let show_all = rest.trim() == "all";
                            let result = state.format_variables(show_all);
                            actions.push(Some(Action::StatusLine(result)));
                        }
                        _ => {
                            log::debug!("unknown command with arguments: '{cmd} {args}'");
                            actions.push(Some(Action::TimedStatusLine("unknown command".into(), 1)))
                        }
                    },
                    None => match args.trim() {
                        "q" | "quit" => actions.push(Some(Action::Quit)),
                        "r" | "reload" | "restart" => {
                            actions.push(Some(Action::Reload));
                        }
                        "nl" | "next-line" | "nextline" => {
                            if state.stopped && !state.executor().stopped {
                                state.create_breakpoint(BreakpointType::NextLine);
                                state.stopped = false;
                                actions.push(Some(Action::Continue));
                            } else if state.executor().stopped {
                                actions.push(Some(Action::TimedStatusLine(
                                    "program has terminated, cannot continue".to_string(),
                                    3,
                                )));
                            }
                        }
                        "debug" => {
                            actions.push(Some(Action::ShowDebug));
                        }
                        "vars" | "variables" | "locals" => {
                            let result = state.format_variables(false);
                            actions.push(Some(Action::StatusLine(result)));
                        }
                        "p" | "proc" | "where" => {
                            // Show the current procedure name so users can craft
                            // `b in <pattern>` breakpoints.
                            let live = state
                                .executor()
                                .current_asmop
                                .as_ref()
                                .map(|op| op.context_name().to_string());
                            let frame = state
                                .executor()
                                .callstack
                                .current_frame()
                                .and_then(|f| f.procedure(""))
                                .map(|p| p.to_string());
                            let msg = match (live, frame) {
                                (Some(l), Some(f)) if l == f => {
                                    format!("proc: {l}")
                                }
                                (Some(l), Some(f)) => {
                                    format!("proc (live): {l} / (frame): {f}")
                                }
                                (Some(l), None) => format!("proc (live): {l}"),
                                (None, Some(f)) => format!("proc (frame): {f}"),
                                (None, None) => "proc: <unknown>".to_string(),
                            };
                            actions.push(Some(Action::StatusLine(msg)));
                        }
                        invalid => {
                            log::debug!("unknown command: '{invalid}'");
                            actions.push(Some(Action::TimedStatusLine("unknown command".into(), 1)))
                        }
                    },
                }
            }
            Action::FooterResult(_cmd, None) => {
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(Action::Focus, state)?);
                }
            }
            Action::Continue => {
                // DAP client mode: send step commands over the network
                #[cfg(feature = "dap")]
                if state.debug_mode == crate::ui::state::DebugMode::Remote {
                    self.step_remote(state, &mut actions)?;
                    // Send any pending actions
                    if let Some(tx) = &mut self.command_tx {
                        actions.into_iter().flatten().for_each(|action| {
                            tx.send(action).ok();
                        });
                    }
                    return Ok(None);
                }

                // If the program has already terminated, there's nothing to run.
                // Let the user know they can restart with `:r` / `:reload`.
                if state.executor().stopped {
                    actions.push(Some(Action::TimedStatusLine(
                        "program has terminated — use :r to restart".into(),
                        3,
                    )));
                    return Ok(None);
                }

                let start_cycle = state.executor().cycle;
                let start_asmop = state.executor().current_asmop.clone();
                let start_proc = state.current_procedure();
                let start_line_loc = state.current_display_location();
                let mut previous_proc = state.current_procedure();
                let mut pending_called_breakpoints = Vec::new();
                let mut breakpoints = core::mem::take(&mut state.breakpoints);
                state.breakpoints_hit.clear();
                state.stopped = false;
                let stopped = loop {
                    // If stepping the program results in the program terminating succesfully, stop
                    if state.executor().stopped {
                        break true;
                    }

                    let mut consume_most_recent_finish = false;
                    match state.executor_mut().step() {
                        Ok(Some(exited)) if exited.should_break_on_exit() => {
                            consume_most_recent_finish = true;
                        }
                        Ok(_) => (),
                        Err(err) => {
                            // Execution terminated with an error
                            state.set_execution_failed(err);
                            break true;
                        }
                    }

                    if breakpoints.is_empty() {
                        // No breakpoint management needed, keep executing
                        continue;
                    }

                    let (_op, is_op_boundary, proc, loc, line_loc) = {
                        let op = state.executor().current_op;
                        let is_boundary = state
                            .executor()
                            .current_asmop
                            .as_ref()
                            .map(|_info| true)
                            .unwrap_or(false);
                        let loc = state.current_location();
                        let line_loc = state.current_display_location();
                        let proc = state.current_procedure();
                        (op, is_boundary, proc, loc, line_loc)
                    };

                    // Remove all breakpoints triggered at this cycle
                    let current_cycle = 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 {
                                    state.breakpoints_hit.push(bp.clone());
                                } else {
                                    state.breakpoints_hit.push(core::mem::take(bp));
                                }
                                return retained;
                            } else {
                                return true;
                            }
                        }

                        if cycles_stepped > 0
                            && is_op_boundary
                            && matches!(&bp.ty, BreakpointType::Next)
                            && state.executor().current_asmop != start_asmop
                        {
                            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 {
                                state.executor().current_asmop != start_asmop
                            };
                            if reached_next {
                                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 {
                                state.breakpoints_hit.push(bp.clone());
                            } else {
                                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
                                && state.executor().procedure_has_debug_vars(proc)
                                && state.executor().last_debug_var_count == 0
                            {
                                if !pending {
                                    pending_called_breakpoints.push(bp.id);
                                }
                                return true;
                            }

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

                        true
                    });

                    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);
                        break true;
                    }

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

                    previous_proc = proc;
                };

                // Restore the breakpoints state
                state.breakpoints = breakpoints;

                // Ensure that if we yield to the runtime, that we resume executing when
                // resumed, unless we specifically stopped for a breakpoint or other condition
                state.stopped = stopped;

                // Report program termination to the user
                if stopped && state.executor().stopped {
                    if let Some(err) = state.execution_failed() {
                        actions.push(Some(Action::StatusLine(err.to_string())));
                    } else {
                        actions.push(Some(Action::StatusLine(
                            "program terminated successfully".to_string(),
                        )));
                    }
                }

                // Update the UI with latest state
                for pane in self.panes.iter_mut() {
                    actions.push(pane.update(Action::Update, state)?);
                }
            }
            Action::Reload => match state.reload() {
                Ok(_) => {
                    for pane in self.panes.iter_mut() {
                        actions.push(pane.update(Action::Reload, state)?);
                    }
                }
                Err(err) => {
                    actions.push(Some(Action::TimedStatusLine(err.to_string(), 5)));
                }
            },
            _ => {
                if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
                    actions.push(pane.update(action, state)?);
                }
            }
        }

        if let Some(tx) = &mut self.command_tx {
            actions.into_iter().flatten().for_each(|action| {
                tx.send(action).ok();
            });
        }
        Ok(None)
    }

    fn handle_key_events(
        &mut self,
        key: KeyEvent,
        state: &mut State,
    ) -> Result<Option<EventResponse<Action>>, Report> {
        match state.input_mode {
            InputMode::Normal => {
                let response = match key.code {
                    KeyCode::Right | KeyCode::Char('l') | KeyCode::Char('L') => {
                        EventResponse::Stop(Action::FocusNext)
                    }
                    KeyCode::Left | KeyCode::Char('h') | KeyCode::Char('H') => {
                        EventResponse::Stop(Action::FocusPrev)
                    }
                    KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => {
                        EventResponse::Stop(Action::Down)
                    }
                    KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => {
                        EventResponse::Stop(Action::Up)
                    }
                    KeyCode::Char('g') | KeyCode::Char('G') => EventResponse::Stop(Action::Go),
                    KeyCode::Backspace | KeyCode::Char('b') | KeyCode::Char('B') => {
                        EventResponse::Stop(Action::Back)
                    }
                    KeyCode::Char('f') | KeyCode::Char('F') => {
                        EventResponse::Stop(Action::ToggleFullScreen)
                    }
                    KeyCode::Char(c) if ('1'..='9').contains(&c) => {
                        EventResponse::Stop(Action::Tab(c.to_digit(10).unwrap_or(0) - 1))
                    }
                    KeyCode::Char(']') => EventResponse::Stop(Action::TabNext),
                    KeyCode::Char('[') => EventResponse::Stop(Action::TabPrev),
                    KeyCode::Char(':') => {
                        EventResponse::Stop(Action::FocusFooter(":".into(), None))
                    }
                    KeyCode::Char('q') => EventResponse::Stop(Action::Quit),
                    KeyCode::Char('e') => {
                        state.create_breakpoint(BreakpointType::Finish);
                        state.stopped = false;
                        EventResponse::Stop(Action::Continue)
                    }
                    // Only step if we're stopped, and execution has not terminated
                    KeyCode::Char('s') if state.stopped && !state.executor().stopped => {
                        state.create_breakpoint(BreakpointType::Step);
                        state.stopped = false;
                        EventResponse::Stop(Action::Continue)
                    }
                    // Only step-next if we're stopped, and execution has not terminated
                    KeyCode::Char('n') if state.stopped && !state.executor().stopped => {
                        state.create_breakpoint(BreakpointType::Next);
                        state.stopped = false;
                        EventResponse::Stop(Action::Continue)
                    }
                    // Only resume execution if we're stopped, and execution has not terminated
                    KeyCode::Char('c') if state.stopped && !state.executor().stopped => {
                        state.stopped = false;
                        EventResponse::Stop(Action::Continue)
                    }
                    // Do not try to continue if execution has terminated, but warn user
                    KeyCode::Char('c' | 's' | 'n') if state.stopped && state.executor().stopped => {
                        EventResponse::Stop(Action::TimedStatusLine(
                            "program has terminated, cannot continue".to_string(),
                            3,
                        ))
                    }
                    KeyCode::Char('d') => EventResponse::Stop(Action::Delete),
                    _ => {
                        return Ok(None);
                    }
                };
                Ok(Some(response))
            }
            InputMode::Insert => Ok(None),
            InputMode::Command => Ok(None),
        }
    }

    fn draw(&mut self, frame: &mut Frame<'_>, area: Rect, state: &State) -> Result<(), Report> {
        if let Some(fullscreen_pane_index) = self.fullscreen_pane_index {
            self.panes[fullscreen_pane_index].draw(frame, area, state)?;
        } else {
            let outer_layout = Layout::default()
                .direction(Direction::Horizontal)
                .constraints(vec![Constraint::Fill(3), Constraint::Fill(1)])
                .split(area);

            let left_panes = Layout::default()
                .direction(Direction::Vertical)
                .constraints(vec![
                    self.panes[0].height_constraint(),
                    self.panes[1].height_constraint(),
                    self.panes[2].height_constraint(),
                ])
                .split(outer_layout[0]);

            let right_panes = Layout::default()
                .direction(Direction::Vertical)
                .constraints(vec![
                    self.panes[3].height_constraint(),
                    self.panes[4].height_constraint(),
                ])
                .split(outer_layout[1]);
            self.panes[0].draw(frame, left_panes[0], state)?;
            self.panes[1].draw(frame, left_panes[1], state)?;
            self.panes[2].draw(frame, left_panes[2], state)?;
            self.panes[3].draw(frame, right_panes[0], state)?;
            self.panes[4].draw(frame, right_panes[1], state)?;
        }
        Ok(())
    }
}

#[cfg(feature = "dap")]
impl Home {
    /// Handle stepping in DAP remote mode.
    ///
    /// Determines which DAP command to send based on the current breakpoints,
    /// sends it, waits for the response, and updates the TUI state.
    fn step_remote(
        &mut self,
        state: &mut State,
        actions: &mut Vec<Option<Action>>,
    ) -> Result<(), Report> {
        use crate::exec::DapStopReason;
        let result = state.step_remote();

        match result {
            Ok(DapStopReason::Stopped(_)) => state.stopped = true,
            Ok(DapStopReason::Terminated) => {
                state.executor_mut().stopped = true;
                state.stopped = true;
                actions.push(Some(Action::StatusLine("program terminated successfully".into())));
            }
            Ok(DapStopReason::Restarting) => {
                state.stopped = true;
                actions.push(Some(Action::StatusLine(
                    "server signaled Phase 2 restart during step".into(),
                )));
            }
            Err(e) => {
                state.executor_mut().stopped = true;
                state.stopped = true;
                actions.push(Some(Action::StatusLine(format!("DAP error: {e}"))));
            }
        }

        // Update panes with latest state
        for pane in self.panes.iter_mut() {
            actions.push(pane.update(Action::Update, state)?);
        }

        Ok(())
    }
}