-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathdebug.rs
More file actions
350 lines (327 loc) · 12.5 KB
/
Copy pathdebug.rs
File metadata and controls
350 lines (327 loc) · 12.5 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Parts of the file are Copyright (c) The Diem Core Contributors
// Parts of the file are Copyright (c) The Move Contributors
// Parts of the file are Copyright (c) Aptos Foundation
// All Aptos Foundation code and content is licensed pursuant to the Innovation-Enabling Source Code License, available at https://github.com/aptos-labs/aptos-core/blob/main/LICENSE
use crate::{
interpreter::InterpreterDebugInterface, source_locator, LoadedFunction, RuntimeEnvironment,
};
use move_vm_types::{instr::Instruction, values::Locals};
use std::{
collections::{BTreeSet, VecDeque},
env,
io::{self, IsTerminal, Write},
str::FromStr,
};
// ── Batch-command queue ───────────────────
thread_local! {
/// Pre-loaded command queue populated from [`MOVE_VM_STEP_COMMANDS_ENV_VAR_NAME`].
/// Commands are comma-separated, e.g. `step,stack,step,continue`.
///
/// When a command is available it is consumed without reading stdin,
/// making the debugger scriptable for non-interactive use.
static COMMAND_QUEUE: RefCell<VecDeque<String>> = {
let queue = env::var(crate::tracing::MOVE_VM_STEP_COMMANDS_ENV_VAR_NAME)
.map(|s| s.split(',').map(|c| c.trim().to_owned()).collect())
.unwrap_or_default();
RefCell::new(queue)
};
}
use std::cell::RefCell;
#[derive(Debug)]
#[allow(unused)]
enum DebugCommand {
PrintStack,
Step(usize),
StepOver(usize),
StepOut,
Continue,
Breakpoint(String),
DeleteBreakpoint(String),
PrintBreakpoints,
}
impl DebugCommand {
pub fn debug_string(&self) -> &str {
match self {
Self::PrintStack => "stack",
Self::Step(_) => "step",
Self::StepOver(_) => "step_over",
Self::StepOut => "step_out",
Self::Continue => "continue",
Self::Breakpoint(_) => "breakpoint ",
Self::DeleteBreakpoint(_) => "delete ",
Self::PrintBreakpoints => "breakpoints",
}
}
pub fn commands() -> Vec<DebugCommand> {
vec![
Self::PrintStack,
Self::Step(0),
Self::StepOver(0),
Self::StepOut,
Self::Continue,
Self::Breakpoint("".to_string()),
Self::DeleteBreakpoint("".to_string()),
Self::PrintBreakpoints,
]
}
}
fn parse_number(s: &str) -> Result<usize, String> {
if s.trim_start().is_empty() {
return Ok(1);
}
let n = s.trim_start().parse::<usize>();
if n.is_err() {
return Err("Step count must be a number".to_string());
}
Ok(n.unwrap())
}
impl FromStr for DebugCommand {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use DebugCommand::*;
let s = s.trim();
if s.starts_with(PrintStack.debug_string()) {
return Ok(PrintStack);
}
if s.starts_with(StepOut.debug_string()) {
return Ok(StepOut);
}
if let Some(n) = s.strip_prefix(StepOver(0).debug_string()) {
return Ok(StepOver(parse_number(n)?));
}
if let Some(n) = s.strip_prefix(Step(0).debug_string()) {
return Ok(Step(parse_number(n)?));
}
if s.starts_with(Continue.debug_string()) {
return Ok(Continue);
}
if let Some(breakpoint) = s.strip_prefix(Breakpoint("".to_owned()).debug_string()) {
return Ok(Breakpoint(breakpoint.to_owned()));
}
if let Some(breakpoint) = s.strip_prefix(DeleteBreakpoint("".to_owned()).debug_string()) {
return Ok(DeleteBreakpoint(breakpoint.to_owned()));
}
if s.starts_with(PrintBreakpoints.debug_string()) {
return Ok(PrintBreakpoints);
}
Err(format!(
"Unrecognized command: {}\nAvailable commands: {}",
s,
Self::commands()
.iter()
.map(|command| command.debug_string())
.collect::<Vec<_>>()
.join(", ")
))
}
}
#[derive(Debug)]
#[allow(unused)]
pub(crate) struct DebugContext {
breakpoints: BTreeSet<String>,
input_checker: InputChecker,
}
#[derive(Debug)]
#[allow(unused)]
enum InputChecker {
StepRemaining(usize),
StepOverRemaining {
stack_depth: usize,
remaining: usize,
},
StepOut {
target_stack_depth: usize,
},
Continue,
}
impl DebugContext {
#[allow(unused)]
pub(crate) fn new() -> Self {
Self {
breakpoints: BTreeSet::new(),
input_checker: InputChecker::StepRemaining(1),
}
}
#[allow(unused)]
pub(crate) fn debug_loop(
&mut self,
function: &LoadedFunction,
locals: &Locals,
pc: u16,
instr: &Instruction,
runtime_environment: &RuntimeEnvironment,
interpreter: &dyn InterpreterDebugInterface,
) {
let instr_string = format!("{:?}", instr);
let function_string = format!("{}::{}", function.name_as_pretty_string(), pc);
let breakpoint_hit = self
.breakpoints
.iter()
.any(|bp| instr_string[..].starts_with(bp.as_str()) || function_string.contains(bp));
let should_take_input = match &mut self.input_checker {
InputChecker::StepRemaining(n) => {
if *n == 1 {
self.input_checker = InputChecker::Continue;
true
} else {
*n -= 1;
false
}
},
InputChecker::StepOverRemaining {
stack_depth,
remaining,
} => {
if *stack_depth >= interpreter.get_stack_frames(usize::MAX).stack_trace().len() {
if *remaining == 1 {
self.input_checker = InputChecker::Continue;
true
} else {
*remaining -= 1;
false
}
} else {
false
}
},
InputChecker::StepOut { target_stack_depth } => {
if *target_stack_depth
== interpreter.get_stack_frames(usize::MAX).stack_trace().len()
{
self.input_checker = InputChecker::Continue;
true
} else {
false
}
},
InputChecker::Continue => false,
};
if should_take_input || breakpoint_hit {
if breakpoint_hit {
let bp_match = self
.breakpoints
.iter()
.find(|bp| instr_string.starts_with(bp.as_str()))
.cloned()
.unwrap_or(function_string.clone());
println!(
"Breakpoint {} hit with instruction {}",
bp_match, instr_string
);
}
// Print function + source location.
print!("function >> {}", function_string);
if let Some(module_id) = function.module_id() {
if let Some(loc) =
source_locator::get_bytecode_source_location(module_id, function.index(), pc)
{
print!(" (at {})", loc);
}
}
println!();
println!("instruction >> {:?}\nprogram counter >> {}", instr, pc);
loop {
// ── Try the pre-loaded command queue first ────────────────────
let queued = COMMAND_QUEUE.with(|q| q.borrow_mut().pop_front());
let input_str = if let Some(cmd) = queued {
println!("> {}", cmd); // echo so output is parseable
cmd
} else {
// ── Fall back to stdin; auto-continue when not a TTY ──────
if !io::stdin().is_terminal() {
self.input_checker = InputChecker::Continue;
break;
}
print!("> ");
std::io::stdout().flush().unwrap();
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) => line,
Err(err) => {
println!("Error reading input: {}", err);
break;
},
}
};
match input_str.parse::<DebugCommand>() {
Err(err) => println!("{}", err),
Ok(command) => match command {
DebugCommand::Step(n) => {
self.input_checker = InputChecker::StepRemaining(n);
break;
},
DebugCommand::StepOver(n) => {
self.input_checker = InputChecker::StepOverRemaining {
stack_depth: interpreter
.get_stack_frames(usize::MAX)
.stack_trace()
.len(),
remaining: n,
};
break;
},
DebugCommand::Continue => {
self.input_checker = InputChecker::Continue;
break;
},
DebugCommand::Breakpoint(breakpoint) => {
self.breakpoints.insert(breakpoint.to_string());
},
DebugCommand::DeleteBreakpoint(breakpoint) => {
self.breakpoints.remove(&breakpoint);
},
DebugCommand::StepOut => {
let stack_depth =
interpreter.get_stack_frames(usize::MAX).stack_trace().len();
if stack_depth == 0 {
println!("No stack frames to step out of");
} else {
self.input_checker = InputChecker::StepOut {
target_stack_depth: stack_depth - 1,
};
break;
}
},
DebugCommand::PrintBreakpoints => self
.breakpoints
.iter()
.enumerate()
.for_each(|(i, bp)| println!("[{}] {}", i, bp)),
DebugCommand::PrintStack => {
let mut s = String::new();
interpreter
.debug_print_stack_trace(&mut s, runtime_environment)
.unwrap();
println!("{}", s);
println!("Current frame: {}\n", function_string);
let code = function.code();
println!(" Code:");
for (i, instr) in code.iter().enumerate() {
if i as u16 == pc {
println!(" > [{}] {:?}", pc, instr);
} else {
println!(" [{}] {:?}", i, instr);
}
}
if function.local_tys().is_empty() {
println!(" Locals:");
println!(" (none)");
} else {
let mut s = String::new();
source_locator::print_locals_enriched(
&mut s,
function,
locals,
runtime_environment,
false,
)
.unwrap();
println!("{}", s);
}
},
},
}
}
}
}
}