Skip to main content

vihaco_stdlib/debugger/
runner.rs

1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4use super::{DebugAction, Debugger, DebuggerState, PausedBreakpoint};
5use crate::execution_state::runner::{execute_instruction, run};
6use crate::{ExecutionState, RunState};
7use std::ops::ControlFlow;
8use vihaco_module::module::LocalModule;
9use vihaco_runtime::{Dispatch, Field, HasProgram};
10
11/// Continue or single-step execution through the selected debugger.
12pub fn resume_with_breaks<F, M, I, V, Ty, Info>(
13    module: &LocalModule<I, V, Ty, Info>,
14    machine: &mut M,
15    action: DebugAction,
16) -> eyre::Result<()>
17where
18    M: HasProgram<Program = ExecutionState<DebuggerState>>
19        + Field<F, Child = Debugger<u32, I>>
20        + Dispatch<LocalModule<I, V, Ty, Info>, Instruction = I>,
21    I: Clone,
22{
23    run(
24        machine,
25        |machine| {
26            let paused = <M as Field<F>>::get_mut(machine).paused.take();
27            match *machine.program().run_state() {
28                RunState::Halted => Ok(None),
29                RunState::Other(DebuggerState::Breakpoint { resume_pc }) => {
30                    let position = machine.program().pc();
31                    let paused = paused.unwrap_or_else(|| {
32                        if <M as Field<F>>::get(machine).contains(&position) {
33                            PausedBreakpoint::Gutter { key: position }
34                        } else {
35                            PausedBreakpoint::Explicit
36                        }
37                    });
38                    match paused {
39                        PausedBreakpoint::Explicit => {
40                            machine.program_mut().resume_at(resume_pc);
41                            Ok(None)
42                        }
43                        PausedBreakpoint::Gutter { key } => {
44                            machine.program_mut().resume_at(position);
45                            Ok(Some((key, resume_pc)))
46                        }
47                    }
48                }
49                RunState::Runnable => Ok(None),
50            }
51        },
52        |machine, original_at| {
53            let position = machine.program().pc();
54            let displaced = original_at
55                .and_then(|(key, _)| <M as Field<F>>::get(machine).displaced(&key))
56                .cloned();
57            let result = execute_instruction(module, machine, displaced.as_ref());
58            if matches!(
59                machine.program().run_state(),
60                RunState::Other(DebuggerState::Breakpoint { .. })
61            ) {
62                let debugger = <M as Field<F>>::get_mut(machine);
63                let paused = if original_at.is_none() && debugger.contains(&position) {
64                    PausedBreakpoint::Gutter { key: position }
65                } else {
66                    PausedBreakpoint::Explicit
67                };
68                debugger.paused = Some(paused);
69            } else if result.is_err()
70                && matches!(machine.program().run_state(), RunState::Runnable)
71                && let Some((key, resume_pc)) = *original_at
72            {
73                <M as Field<F>>::get_mut(machine).paused = Some(PausedBreakpoint::Gutter { key });
74                machine
75                    .program_mut()
76                    .suspend(DebuggerState::Breakpoint { resume_pc });
77            }
78            result?;
79            *original_at = None;
80            Ok(match action {
81                DebugAction::Step => ControlFlow::Break(()),
82                DebugAction::Continue => ControlFlow::Continue(()),
83            })
84        },
85    )
86}