Refine debugger.

This commit is contained in:
Stephen Chung
2022-01-25 12:24:30 +08:00
parent fc87dec128
commit 40aaab60c3
8 changed files with 334 additions and 158 deletions

View File

@@ -42,9 +42,9 @@ pub enum BreakPoint {
#[cfg(not(feature = "no_position"))]
AtPosition { source: Identifier, pos: Position },
/// Break at a particular function call.
AtFunctionName { fn_name: Identifier },
AtFunctionName { name: Identifier },
/// Break at a particular function call with a particular number of arguments.
AtFunctionCall { fn_name: Identifier, args: usize },
AtFunctionCall { name: Identifier, args: usize },
}
impl fmt::Display for BreakPoint {
@@ -57,8 +57,11 @@ impl fmt::Display for BreakPoint {
write!(f, "@ {:?}", pos)
}
}
Self::AtFunctionName { fn_name } => write!(f, "{} (...)", fn_name),
Self::AtFunctionCall { fn_name, args } => write!(
Self::AtFunctionName { name: fn_name } => write!(f, "{} (...)", fn_name),
Self::AtFunctionCall {
name: fn_name,
args,
} => write!(
f,
"{} ({})",
fn_name,
@@ -71,6 +74,7 @@ impl fmt::Display for BreakPoint {
}
}
/// A function call.
#[derive(Debug, Clone, Hash)]
pub struct CallStackFrame {
pub fn_name: Identifier,
@@ -104,7 +108,7 @@ impl fmt::Display for CallStackFrame {
/// A type providing debugging facilities.
#[derive(Debug, Clone, Hash)]
pub struct Debugger {
active: bool,
status: DebuggerCommand,
break_points: Vec<BreakPoint>,
call_stack: Vec<CallStackFrame>,
}
@@ -113,7 +117,7 @@ impl Debugger {
/// Create a new [`Debugger`].
pub const fn new() -> Self {
Self {
active: false,
status: DebuggerCommand::Continue,
break_points: Vec::new(),
call_stack: Vec::new(),
}
@@ -123,54 +127,74 @@ impl Debugger {
pub fn call_stack_len(&self) -> usize {
self.call_stack.len()
}
/// Get the current call stack.
#[inline(always)]
pub fn call_stack(&self) -> &[CallStackFrame] {
&self.call_stack
}
/// Rewind the function call stack to a particular depth.
#[inline(always)]
pub fn rewind_call_stack(&mut self, len: usize) {
pub(crate) fn rewind_call_stack(&mut self, len: usize) {
self.call_stack.truncate(len);
}
/// Add a new frame to the function call stack.
#[inline(always)]
pub fn push_call_stack_frame(
pub(crate) fn push_call_stack_frame(
&mut self,
fn_name: impl Into<Identifier>,
args: StaticVec<Dynamic>,
source: impl Into<Identifier>,
pos: Position,
) {
let fp = CallStackFrame {
self.call_stack.push(CallStackFrame {
fn_name: fn_name.into(),
args,
source: source.into(),
pos,
};
println!("{}", fp);
self.call_stack.push(fp);
});
}
/// Is this [`Debugger`] currently active?
/// Get the current status of this [`Debugger`].
#[inline(always)]
#[must_use]
pub fn is_active(&self) -> bool {
self.active
pub fn status(&self) -> DebuggerCommand {
self.status
}
/// Activate or deactivate this [`Debugger`].
/// Set the status of this [`Debugger`].
#[inline(always)]
pub fn set_status(&mut self, status: DebuggerCommand) {
self.status = status;
}
/// Set the status of this [`Debugger`].
#[inline(always)]
pub fn reset_status(&mut self, status: Option<DebuggerCommand>) {
if let Some(cmd) = status {
self.status = cmd;
}
}
/// Activate: set the status of this [`Debugger`] to [`DebuggerCommand::StepInto`].
/// Deactivate: set the status of this [`Debugger`] to [`DebuggerCommand::Continue`].
#[inline(always)]
pub fn activate(&mut self, active: bool) {
self.active = active;
if active {
self.set_status(DebuggerCommand::StepInto);
} else {
self.set_status(DebuggerCommand::Continue);
}
}
/// Does a particular [`AST` Node][ASTNode] trigger a break-point?
pub fn is_break_point(&self, src: &str, node: ASTNode) -> bool {
self.iter_break_points().any(|bp| match bp {
self.break_points().iter().any(|bp| match bp {
#[cfg(not(feature = "no_position"))]
BreakPoint::AtPosition { source, pos } => node.position() == *pos && src == source,
BreakPoint::AtFunctionName { fn_name } => match node {
BreakPoint::AtFunctionName { name } => match node {
ASTNode::Expr(Expr::FnCall(x, _)) | ASTNode::Stmt(Stmt::FnCall(x, _)) => {
x.name == *fn_name
x.name == *name
}
_ => false,
},
BreakPoint::AtFunctionCall { fn_name, args } => match node {
BreakPoint::AtFunctionCall { name, args } => match node {
ASTNode::Expr(Expr::FnCall(x, _)) | ASTNode::Stmt(Stmt::FnCall(x, _)) => {
x.args.len() == *args && x.name == *fn_name
x.args.len() == *args && x.name == *name
}
_ => false,
},
@@ -179,7 +203,7 @@ impl Debugger {
/// Get a slice of all [`BreakPoint`]'s.
#[inline(always)]
#[must_use]
pub fn break_points(&mut self) -> &[BreakPoint] {
pub fn break_points(&self) -> &[BreakPoint] {
&self.break_points
}
/// Get the underlying [`Vec`] holding all [`BreakPoint`]'s.
@@ -188,66 +212,98 @@ impl Debugger {
pub fn break_points_mut(&mut self) -> &mut Vec<BreakPoint> {
&mut self.break_points
}
/// Get an iterator over all [`BreakPoint`]'s.
#[inline(always)]
#[must_use]
pub fn iter_break_points(&self) -> impl Iterator<Item = &BreakPoint> {
self.break_points.iter()
}
/// Get a mutable iterator over all [`BreakPoint`]'s.
#[inline(always)]
#[must_use]
pub fn iter_break_points_mut(&mut self) -> impl Iterator<Item = &mut BreakPoint> {
self.break_points.iter_mut()
}
}
impl Engine {
pub(crate) fn run_debugger(
/// Run the debugger callback.
#[inline(always)]
pub(crate) fn run_debugger<'a>(
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: ASTNode,
node: impl Into<ASTNode<'a>>,
level: usize,
) -> bool {
) {
if let Some(cmd) =
self.run_debugger_with_reset(scope, global, state, lib, this_ptr, node, level)
{
global.debugger.set_status(cmd);
}
}
/// Run the debugger callback.
///
/// Returns `true` if the debugger needs to be reactivated at the end of the block, statement or
/// function call.
///
/// # Note
///
/// When the debugger callback return [`DebuggerCommand::StepOver`], the debugger if temporarily
/// disabled and `true` is returned.
///
/// It is up to the [`Engine`] to reactivate the debugger.
#[inline]
#[must_use]
pub(crate) fn run_debugger_with_reset<'a>(
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: impl Into<ASTNode<'a>>,
level: usize,
) -> Option<DebuggerCommand> {
if let Some(ref on_debugger) = self.debugger {
if global.debugger.active || global.debugger.is_break_point(&global.source, node) {
let source = global.source.clone();
let source = if source.is_empty() {
None
} else {
Some(source.as_str())
};
let mut context = crate::EvalContext {
engine: self,
scope,
global,
state,
lib,
this_ptr,
level,
};
let node = node.into();
match on_debugger(&mut context, node, source, node.position()) {
DebuggerCommand::Continue => {
global.debugger.activate(false);
return false;
}
DebuggerCommand::StepInto => {
global.debugger.activate(true);
return true;
}
DebuggerCommand::StepOver => {
global.debugger.activate(false);
return true;
}
let stop = match global.debugger.status {
DebuggerCommand::Continue => false,
DebuggerCommand::StepOver => matches!(node, ASTNode::Stmt(_)),
DebuggerCommand::StepInto => true,
};
if !stop && !global.debugger.is_break_point(&global.source, node) {
return None;
}
let source = global.source.clone();
let source = if source.is_empty() {
None
} else {
Some(source.as_str())
};
let mut context = crate::EvalContext {
engine: self,
scope,
global,
state,
lib,
this_ptr,
level,
};
let command = on_debugger(&mut context, node, source, node.position());
match command {
DebuggerCommand::Continue => {
global.debugger.set_status(DebuggerCommand::Continue);
None
}
DebuggerCommand::StepInto => {
global.debugger.set_status(DebuggerCommand::StepInto);
None
}
DebuggerCommand::StepOver => {
global.debugger.set_status(DebuggerCommand::Continue);
Some(DebuggerCommand::StepOver)
}
}
} else {
None
}
false
}
}