From d1a2b962994470051e22eec1c5410d81f9f3e44a Mon Sep 17 00:00:00 2001 From: Griffin Berlstein Date: Thu, 18 Jul 2024 14:37:38 -0400 Subject: [PATCH] [Cider 2 | Debugger] Preserve debugger info and command history on restart (#2213) A small patch that makes the new `restart` command preserve the watchpoints, breakpoints, and command history of the debugger rather than fully resetting it. --- interp/src/debugger/commands/core.rs | 2 +- interp/src/debugger/debugger_core.rs | 38 ++++++++++++++++--- .../src/debugger/debugging_context/context.rs | 6 +++ interp/src/debugger/mod.rs | 7 ++-- interp/src/main.rs | 9 +++-- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/interp/src/debugger/commands/core.rs b/interp/src/debugger/commands/core.rs index 3cbbc60d3e..6765ca8a23 100644 --- a/interp/src/debugger/commands/core.rs +++ b/interp/src/debugger/commands/core.rs @@ -422,7 +422,7 @@ lazy_static! { CIBuilder::new().invocation("explain") .description("Show examples of commands which take arguments").build(), CIBuilder::new().invocation("restart") - .description("Restart the debugger from the beginning of the execution. This will reset all breakpoints, watchpoints, and the command history").build(), + .description("Restart the debugger from the beginning of the execution. Command history, breakpoints, watchpoints, etc. are preserved").build(), // exit/quit CIBuilder::new().invocation("exit") .invocation("quit") diff --git a/interp/src/debugger/debugger_core.rs b/interp/src/debugger/debugger_core.rs index 6645f93503..3c06257966 100644 --- a/interp/src/debugger/debugger_core.rs +++ b/interp/src/debugger/debugger_core.rs @@ -55,8 +55,14 @@ impl ProgramStatus { } } +/// An opaque wrapper type for internal debugging information +pub struct DebuggerInfo { + ctx: DebuggingContext, + input_stream: Input, +} + pub enum DebuggerReturnStatus { - Restart, + Restart(Box), Exit, } @@ -177,8 +183,20 @@ impl + Clone> Debugger { // so on and so forth - pub fn main_loop(mut self) -> InterpreterResult { - let mut input_stream = Input::new()?; + pub fn main_loop( + mut self, + info: Option, + ) -> InterpreterResult { + let (input_stream, dbg_ctx) = info + .map(|x| (Some(x.input_stream), Some(x.ctx))) + .unwrap_or_else(|| (None, None)); + + if let Some(dbg_ctx) = dbg_ctx { + self.debugging_context = dbg_ctx; + } + + let mut input_stream = + input_stream.map(Ok).unwrap_or_else(Input::new)?; println!( "==== {}: The {}alyx {}nterpreter and {}bugge{} ====", @@ -275,7 +293,12 @@ impl + Clone> Debugger { } Command::Restart => { - return Ok(DebuggerReturnStatus::Restart); + return Ok(DebuggerReturnStatus::Restart(Box::new( + DebuggerInfo { + ctx: self.debugging_context, + input_stream, + }, + ))); } } } @@ -322,7 +345,12 @@ impl + Clone> Debugger { print!("{}", Command::get_explain_string().blue().bold()) } Command::Restart => { - return Ok(DebuggerReturnStatus::Restart); + return Ok(DebuggerReturnStatus::Restart(Box::new( + DebuggerInfo { + ctx: self.debugging_context, + input_stream, + }, + ))); } _ => { println!( diff --git a/interp/src/debugger/debugging_context/context.rs b/interp/src/debugger/debugging_context/context.rs index fd8d0bd5e5..b1857b2fd9 100644 --- a/interp/src/debugger/debugging_context/context.rs +++ b/interp/src/debugger/debugging_context/context.rs @@ -739,3 +739,9 @@ impl DebuggingContext { } } } + +impl Default for DebuggingContext { + fn default() -> Self { + Self::new() + } +} diff --git a/interp/src/debugger/mod.rs b/interp/src/debugger/mod.rs index b424d39fd8..cf36eecfa2 100644 --- a/interp/src/debugger/mod.rs +++ b/interp/src/debugger/mod.rs @@ -5,9 +5,8 @@ mod io_utils; mod macros; pub mod source; -pub use debugger_core::Debugger; -pub use debugger_core::DebuggerReturnStatus; -pub use debugger_core::OwnedDebugger; -pub use debugger_core::ProgramStatus; +pub use debugger_core::{ + Debugger, DebuggerInfo, DebuggerReturnStatus, OwnedDebugger, ProgramStatus, +}; pub(crate) use macros::unwrap_error_message; diff --git a/interp/src/main.rs b/interp/src/main.rs index 888f1455fd..ab989341a0 100644 --- a/interp/src/main.rs +++ b/interp/src/main.rs @@ -3,7 +3,7 @@ use argh::FromArgs; use calyx_utils::OutputFile; use interp::{ configuration, - debugger::{Debugger, DebuggerReturnStatus}, + debugger::{Debugger, DebuggerInfo, DebuggerReturnStatus}, errors::InterpreterResult, flatten::structures::environment::Simulator, }; @@ -128,13 +128,16 @@ fn main() -> InterpreterResult<()> { Ok(()) } Command::Debug(_) => { + let mut info: Option = None; loop { let debugger = Debugger::new(&i_ctx, &opts.data_file)?; - let result = debugger.main_loop()?; + let result = debugger.main_loop(info)?; match result { DebuggerReturnStatus::Exit => break, - DebuggerReturnStatus::Restart => continue, + DebuggerReturnStatus::Restart(new_info) => { + info = Some(*new_info); + } } } Ok(())