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
// REPL Debugger Module
//
// Task: REPL-008-001 - Step execution (next line)
// Test Approach: RED → GREEN → REFACTOR → PROPERTY → MUTATION
//
// Quality targets:
// - Unit tests: 5+ scenarios
// - Property tests: Step never skips lines
// - Mutation score: ≥90%
// - Complexity: <10 per function
use crate::repl::{purify_bash, BreakpointManager};
use std::collections::HashMap;
/// A stack frame in the call stack
///
/// Represents a function call or execution context with its name
/// and the line number where it was called from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StackFrame {
/// Name of the function or context
pub name: String,
/// Line number where this frame was called (1-indexed)
pub line: usize,
}
impl StackFrame {
/// Create a new stack frame
pub fn new(name: impl Into<String>, line: usize) -> Self {
Self {
name: name.into(),
line,
}
}
}
/// Line comparison result for purification-aware debugging
///
/// Compares original bash line with its purified version,
/// enabling the debugger to show what transformations were applied.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineComparison {
/// Original bash line
pub original: String,
/// Purified version of the line
pub purified: String,
/// Whether the lines differ
pub differs: bool,
}
/// Debug session for step-by-step execution of bash scripts
///
/// Tracks the current execution state including:
/// - Script lines
/// - Current line number
/// - Breakpoints
/// - Variables
#[derive(Debug, Clone)]
pub struct DebugSession {
/// Lines of the script being debugged
lines: Vec<String>,
/// Current line number (0-indexed)
current_line: usize,
/// Breakpoint manager
breakpoints: BreakpointManager,
/// Session variables
variables: HashMap<String, String>,
/// Call stack for tracking function calls
call_stack: Vec<StackFrame>,
/// Purified version of the script lines (if purification succeeded)
purified_lines: Option<Vec<String>>,
/// Whether execution is complete
finished: bool,
}
impl DebugSession {
/// Create a new debug session from script content
pub fn new(script: &str) -> Self {
let lines: Vec<String> = script.lines().map(|l| l.to_string()).collect();
// Initialize call stack with main frame
let call_stack = vec![StackFrame::new("<main>", 0)];
// Attempt to purify the script for comparison
let purified_lines = purify_bash(script)
.ok()
.map(|purified| purified.lines().map(|l| l.to_string()).collect());
Self {
lines,
current_line: 0,
breakpoints: BreakpointManager::new(),
variables: HashMap::new(),
call_stack,
purified_lines,
finished: false,
}
}
/// Get the current line number (1-indexed for user display)
pub fn current_line(&self) -> usize {
self.current_line + 1
}
/// Get the current line content
pub fn current_line_content(&self) -> Option<&str> {
self.lines.get(self.current_line).map(|s| s.as_str())
}
/// Check if execution is finished
pub fn is_finished(&self) -> bool {
self.finished
}
/// Get total number of lines
pub fn total_lines(&self) -> usize {
self.lines.len()
}
/// Execute one line (step)
///
/// Executes the current line and advances to the next line.
/// Returns the output from executing the line.
///
/// # Returns
/// - `Some(output)` if a line was executed
/// - `None` if execution is finished
pub fn step(&mut self) -> Option<String> {
// Check if finished
if self.finished || self.current_line >= self.lines.len() {
self.finished = true;
return None;
}
// Get current line
let line = self.lines.get(self.current_line)?.clone();
// Execute the line (simplified - just echo for now)
let output = format!("Executed: {}", line);
// Advance to next line
self.current_line += 1;
// Check if finished
if self.current_line >= self.lines.len() {
self.finished = true;
}
Some(output)
}
/// Set a breakpoint at the specified line (1-indexed)
pub fn set_breakpoint(&mut self, line: usize) -> bool {
if line == 0 || line > self.lines.len() {
return false;
}
self.breakpoints.set_breakpoint(line)
}
/// Check if current line has a breakpoint
pub fn at_breakpoint(&self) -> bool {
self.breakpoints.is_breakpoint_hit(self.current_line())
}
/// Get the current call depth (number of stack frames)
///
/// Returns the size of the call stack. Depth 1 = main frame only.
pub fn call_depth(&self) -> usize {
self.call_stack.len()
}
/// Execute to next line at same call depth (skip over function calls)
///
/// Similar to step(), but if a function call is encountered, it executes
/// the entire function and stops at the next line at the same depth.
///
/// For now, this is simplified to just call step() since we don't have
/// full function call tracking yet. Future enhancement will track actual
/// function boundaries.
///
/// # Returns
/// - `Some(output)` if a line was executed
/// - `None` if execution is finished
pub fn step_over(&mut self) -> Option<String> {
// Simplified implementation: For now, just call step()
// Future: Track call depth and skip over function calls
self.step()
}
/// Continue execution until a breakpoint is hit or execution finishes
///
/// Executes lines repeatedly using step() until:
/// - A breakpoint is encountered (returns BreakpointHit with line number)
/// - Execution completes (returns Finished)
///
/// # Returns
/// - `ContinueResult::BreakpointHit(line)` if stopped at breakpoint
/// - `ContinueResult::Finished` if execution completed
pub fn continue_execution(&mut self) -> ContinueResult {
loop {
// Check if at breakpoint before executing
if self.at_breakpoint() {
return ContinueResult::BreakpointHit(self.current_line());
}
// Execute one step
match self.step() {
Some(_output) => {
// Line executed, check if we hit a breakpoint
if self.at_breakpoint() {
return ContinueResult::BreakpointHit(self.current_line());
}
}
None => {
// Execution finished
return ContinueResult::Finished;
}
}
}
}
/// Execute until the current function returns
///
/// Continues execution until we exit the current stack frame.
/// If already at the top level (main), this behaves like continue to end.
///
/// Note: This is a simplified implementation that works with manual
/// call stack tracking (push_frame/pop_frame). Future versions will
/// automatically track function boundaries.
///
/// # Returns
///
/// - `ContinueResult::BreakpointHit(line)` if stopped at breakpoint
/// - `ContinueResult::Finished` if execution completed or returned from function
///
/// # Examples
///
/// ```
/// # use bashrs::repl::debugger::{DebugSession, ContinueResult};
/// let script = "echo line1\necho line2\necho line3";
/// let mut session = DebugSession::new(script);
///
/// // Simulate entering a function
/// session.push_frame("test_function", 1);
/// assert_eq!(session.call_depth(), 2);
///
/// // Finish will exit the function
/// let result = session.finish();
/// assert!(matches!(result, ContinueResult::Finished));
/// ```
pub fn finish(&mut self) -> ContinueResult {
// Get current call stack depth
let initial_depth = self.call_depth();
// If already finished, return immediately
if self.is_finished() {
return ContinueResult::Finished;
}
// If at top level (<main> only), behave like continue_execution()
if initial_depth <= 1 {
return self.continue_execution();
}
// Execute until we return from current function
loop {
// Check if at breakpoint before executing
if self.at_breakpoint() {
return ContinueResult::BreakpointHit(self.current_line());
}
// Check if we've returned from the function (depth decreased)
if self.call_depth() < initial_depth {
return ContinueResult::Finished;
}
// Execute one step
match self.step() {
Some(_output) => {
// Line executed, check conditions again
if self.at_breakpoint() {
return ContinueResult::BreakpointHit(self.current_line());
}
if self.call_depth() < initial_depth {
return ContinueResult::Finished;
}
}
None => {
// Execution finished
return ContinueResult::Finished;
}
}
}
}
// === Variable Inspection Methods (REPL-009-001) ===
}
include!("debugger_set_variable.rs");