Skip to main content

miden_debug/ui/
mod.rs

1#[cfg(feature = "tui")]
2mod action;
3#[cfg(feature = "tui")]
4mod app;
5#[cfg(feature = "tui")]
6mod duration;
7#[cfg(feature = "tui")]
8mod pages;
9#[cfg(feature = "tui")]
10mod panes;
11pub(crate) mod state;
12#[cfg(feature = "tui")]
13mod syntax_highlighting;
14#[cfg(feature = "tui")]
15mod tui;
16
17#[cfg(feature = "tui")]
18use log::LevelFilter;
19#[cfg(feature = "tui")]
20use miden_assembly_syntax::diagnostics::{IntoDiagnostic, Report};
21
22pub use self::state::{DebugMode, State};
23#[cfg(feature = "tui")]
24use self::{action::Action, app::App};
25#[cfg(feature = "tui")]
26use crate::config::DebuggerConfig;
27
28#[cfg(feature = "tui")]
29#[allow(dead_code)]
30pub fn run(config: Box<DebuggerConfig>, logger: Box<dyn log::Log>) -> Result<(), Report> {
31    run_with_log_level(config, logger, LevelFilter::Trace)
32}
33
34#[cfg(feature = "tui")]
35pub fn run_with_log_level(
36    config: Box<DebuggerConfig>,
37    logger: Box<dyn log::Log>,
38    max_level: LevelFilter,
39) -> Result<(), Report> {
40    let mut builder = tokio::runtime::Builder::new_current_thread();
41    let rt = builder.enable_all().build().into_diagnostic()?;
42    rt.block_on(async move { start_ui(config, logger, max_level).await })
43}
44
45/// Launch the TUI debugger with a pre-built [State].
46///
47/// This is the programmatic entry point used by transaction debugging, where
48/// the caller constructs a [State] with pre-recorded event replay data.
49#[cfg(feature = "tui")]
50pub fn run_with_state(state: State, logger: Box<dyn log::Log>) -> Result<(), Report> {
51    run_with_state_and_log_level(state, logger, LevelFilter::Trace)
52}
53
54/// Launch the TUI debugger with a pre-built [State] and log level filter.
55#[cfg(feature = "tui")]
56pub fn run_with_state_and_log_level(
57    state: State,
58    logger: Box<dyn log::Log>,
59    max_level: LevelFilter,
60) -> Result<(), Report> {
61    let mut builder = tokio::runtime::Builder::new_current_thread();
62    let rt = builder.enable_all().build().into_diagnostic()?;
63    rt.block_on(async move { start_ui_with_state(state, logger, max_level).await })
64}
65
66/// Replay a recorded execution snapshot in the TUI debugger.
67///
68/// Reads a [`ReplaySnapshot`](crate::exec::ReplaySnapshot) written during a recorded DAP session
69/// (e.g. `miden-client exec --start-debug-adapter ... --record <FILE>`) and re-runs the same
70/// program with its captured inputs, forests, and event log fed back through the event-replay
71/// host — so the transaction can be stepped through offline, without the original host.
72#[cfg(feature = "tui")]
73pub fn run_replay_and_log_level(
74    snapshot_path: &std::path::Path,
75    logger: Box<dyn log::Log>,
76    max_level: LevelFilter,
77) -> Result<(), Report> {
78    use std::sync::Arc;
79
80    use miden_assembly::DefaultSourceManager;
81
82    use crate::exec::ReplaySnapshot;
83
84    let snapshot = ReplaySnapshot::read_from_file(snapshot_path)
85        .map_err(|err| Report::msg(format!("{err}")))?;
86    // The snapshot does not carry source files; the debugger falls back to disassembly, exactly
87    // as it does for a raw program with no debug info.
88    let source_manager = Arc::new(DefaultSourceManager::default());
89    let state = State::new_for_transaction(
90        snapshot.package,
91        snapshot.stack_inputs,
92        snapshot.advice_inputs,
93        snapshot.options,
94        source_manager,
95        snapshot.mast_forests,
96        snapshot.event_log,
97    )?;
98    run_with_state_and_log_level(state, logger, max_level)
99}
100
101#[cfg(feature = "tui")]
102#[allow(dead_code)]
103pub async fn start_ui(
104    config: Box<DebuggerConfig>,
105    logger: Box<dyn log::Log>,
106    max_level: LevelFilter,
107) -> Result<(), Report> {
108    use ratatui::crossterm as term;
109
110    crate::logger::DebugLogger::install_with_max_level(logger, max_level).into_diagnostic()?;
111
112    let original_hook = std::panic::take_hook();
113    std::panic::set_hook(Box::new(move |panic_info| {
114        let _ = term::terminal::disable_raw_mode();
115        let _ = term::execute!(std::io::stdout(), term::terminal::LeaveAlternateScreen);
116        original_hook(panic_info);
117    }));
118
119    let mut app = App::new(config).await?;
120    app.run().await?;
121
122    Ok(())
123}
124
125#[cfg(feature = "tui")]
126async fn start_ui_with_state(
127    state: State,
128    logger: Box<dyn log::Log>,
129    max_level: LevelFilter,
130) -> Result<(), Report> {
131    use ratatui::crossterm as term;
132
133    crate::logger::DebugLogger::install_with_max_level(logger, max_level).into_diagnostic()?;
134
135    let original_hook = std::panic::take_hook();
136    std::panic::set_hook(Box::new(move |panic_info| {
137        let _ = term::terminal::disable_raw_mode();
138        let _ = term::execute!(std::io::stdout(), term::terminal::LeaveAlternateScreen);
139        original_hook(panic_info);
140    }));
141
142    let mut app = App::from_state(state).await?;
143    app.run().await?;
144
145    Ok(())
146}