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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::sync::atomic::Ordering;
use owo_colors::OwoColorize;
use crate::error::Result;
use crate::session::{BreakpointStopAction, StepKind};
use crate::types::VirtAddr;
use crate::ui;
use crate::repl::*;
impl ReplState<'_> {
pub fn interrupt_running_vm(&mut self) -> Result<()> {
match surface_pending_stop(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&mut self.ctx.breakpoints,
&self.caches,
&mut self.ctx.current_thread,
&mut self.ctx.reload_module_list_pending,
) {
Ok(true) => return Ok(()),
Ok(false) => {}
Err(e) => {
error!("error checking running VM: {:?}", e);
return Ok(());
}
}
if let Err(e) = surface_interrupt_stop(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&mut self.ctx.breakpoints,
&self.caches,
&mut self.ctx.current_thread,
&mut self.ctx.reload_module_list_pending,
) {
error!("failed to interrupt: {:?}", e);
}
Ok(())
}
pub fn cmd_continue(&mut self, _parts: &[&str]) -> Result<()> {
if self.ctx.backend.is_running() {
match surface_pending_stop(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&mut self.ctx.breakpoints,
&self.caches,
&mut self.ctx.current_thread,
&mut self.ctx.reload_module_list_pending,
) {
Ok(true) => {}
Ok(false) => error!("VM is running"),
Err(e) => error!("error checking running VM: {:?}", e),
}
return Ok(());
}
// Step past a breakpoint at RIP, re-arm breakpoints, continue, and drop
// stale inspection caches; the canonical resume prologue lives in core.
if let Err(e) = self.ctx.resume() {
error!("failed to continue: {:?}", e);
return Ok(());
}
println!(
"{}",
"VM running, waiting for stop (Ctrl+C to pause)...".bright_black()
);
INTERRUPT_REQUESTED.store(false, Ordering::SeqCst);
loop {
let interrupt_requested = INTERRUPT_REQUESTED.swap(false, Ordering::SeqCst);
let stop_result = if interrupt_requested {
println!();
match self.ctx.backend.try_wait_for_stop(REPL_STOP_POLL) {
Ok(Some(event)) => Ok(Some(event)),
Ok(None) => self.ctx.backend.interrupt().map(Some),
Err(e) => Err(e),
}
} else {
self.ctx.backend.try_wait_for_stop(REPL_STOP_POLL)
};
match stop_result {
Ok(Some(mut event)) => {
let reload_status = apply_target_reload_if_needed(
&mut *self.ctx.backend,
&mut self.ctx.target,
&mut self.ctx.breakpoints,
&self.caches,
&mut event,
);
update_reload_module_list_pending(
&mut self.ctx.reload_module_list_pending,
reload_status,
);
if reload_status.pending_rediscovery() {
print_pending_rediscovery_stop_context(
&mut *self.ctx.backend,
&self.ctx.register_map,
&self.ctx.target,
&mut self.ctx.current_thread,
&event,
reload_status,
);
break;
}
let _ = try_complete_pending_module_list_reload(
&mut *self.ctx.backend,
&mut self.ctx.target,
&self.caches,
&mut self.ctx.reload_module_list_pending,
);
if !interrupt_requested
&& !self.ctx.reload_module_list_pending
&& should_resume_assisted_refresh_stop(
&self.ctx.target,
&self.ctx.breakpoints,
&event,
reload_status,
)
{
if let Err(e) = resume_assisted_refresh_stop(&mut *self.ctx.backend) {
error!("failed to resume after assisted refresh break: {:?}", e);
break;
}
continue;
}
// A stray single-step (STATUS_SINGLE_STEP, not at a user
// breakpoint) is a debugger artifact from a managed step-over
// on SMP KD, not a stop to surface; clear TF on its processor
// and resume. Shared classification with continue_until_break.
if stop_is_stray_single_step(&event, &self.ctx.breakpoints) {
set_current_thread_from_stop(
&mut *self.ctx.backend,
&event,
&mut self.ctx.current_thread,
);
let _ = clear_trap_flag(&mut *self.ctx.backend, &self.ctx.register_map);
if let Err(e) = self.ctx.backend.continue_execution() {
error!("failed to resume after stray single-step: {:?}", e);
break;
}
continue;
}
let target_reloaded = reload_status.target_reloaded();
let reload_load_symbols_stop =
is_target_reload_load_symbols_stop(&event, reload_status);
let is_bugcheck = event.is_bugcheck && !target_reloaded;
set_current_thread_from_stop(
&mut *self.ctx.backend,
&event,
&mut self.ctx.current_thread,
);
let modules_changed = refresh_stop_caches_pre(
&mut *self.ctx.backend,
&self.ctx.target,
&self.caches,
);
if is_bugcheck {
print_bugcheck_summary(&self.ctx.target, event.bugcheck.as_ref());
println!();
}
let stop_exception_code = event.exception_code;
let stop_pc = event.program_counter;
let early_non_breakpoint_stop = !target_reloaded
&& !is_bugcheck
&& stop_exception_code.zip(stop_pc).is_some_and(|(_, pc)| {
self.ctx.breakpoints.breakpoint_id_at_address(pc).is_none()
});
if early_non_breakpoint_stop {
print_stop_notice_parts(stop_exception_code, stop_pc);
println!();
}
refresh_stop_caches_post(
&self.ctx.target,
&self.caches,
target_reloaded,
modules_changed,
);
if self.ctx.breakpoints.has_enabled_breakpoints() && !is_bugcheck {
// Done before reading the current thread's regs so
// the BP-hit check below sees the post-rewind RIP
rewind_threads_off_breakpoints(
&mut *self.ctx.backend,
&self.ctx.register_map,
&self.ctx.breakpoints,
&self.ctx.current_thread,
);
}
let hit_result =
if early_non_breakpoint_stop || is_bugcheck || reload_load_symbols_stop {
BreakpointStopAction::NotBreakpoint
} else {
let regs = match self.ctx.backend.read_registers() {
Ok(r) => r,
Err(e) => {
error!("failed to read registers: {:?}", e);
break;
}
};
self.ctx.target.registers =
Some(self.ctx.register_map.to_hashmap(®s));
let rip = self.ctx.register_map.read_u64("rip", ®s).unwrap_or(0);
let cr3 = self.ctx.register_map.read_u64("cr3", ®s).unwrap_or(0);
if cr3 != 0 {
self.ctx.target.set_context_dtb_override(cr3);
self.caches.refresh_symbol_context(&self.ctx.target);
}
refresh_windows_thread_context_for_backend_thread(
&mut self.ctx.target,
&self.ctx.current_thread,
);
// Shared core resolver so the REPL and `continue_until_break`
// can't drift on breakpoint-hit disposition; absorbed cases
// (Resumed) step over and resume internally, so we keep waiting.
match self.ctx.resolve_breakpoint_stop(rip, cr3) {
Ok(BreakpointStopAction::Resumed) => continue,
Ok(action) => action,
Err(e) => {
error!("failed to handle breakpoint stop: {:?}", e);
break;
}
}
};
match hit_result {
BreakpointStopAction::Hit {
id,
symbol,
temporary,
..
} => {
println!();
if !temporary {
println!(
"{} {} {}",
ui::label("breakpoint:"),
ui::bp_id(id),
symbol
.as_ref()
.map(|s| format!("({})", ui::symbol(s)))
.unwrap_or_default()
);
}
print_break_context(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
);
break;
}
// Absorbed inline in the resolver above; defensively keep
// waiting if one ever reaches here.
BreakpointStopAction::Resumed => continue,
BreakpointStopAction::NotBreakpoint => {
if !target_reloaded && !early_non_breakpoint_stop && !is_bugcheck {
print_stop_notice_parts(stop_exception_code, stop_pc);
println!();
}
if reload_load_symbols_stop {
print_target_reload_notification_context(
&self.ctx.target,
&self.ctx.current_thread,
&event,
reload_status,
);
} else if is_bugcheck {
print_break_context_for_bugcheck(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
event.bugcheck.as_ref(),
);
} else {
print_break_context(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
);
}
break;
}
}
}
Ok(None) => {
// timeout
}
Err(e) => {
error!("error waiting for stop: {:?}", e);
break;
}
}
}
Ok(())
}
pub fn cmd_break(&mut self, _parts: &[&str]) -> Result<()> {
if !self.ctx.backend.is_running() {
error!("VM is already paused");
return Ok(());
}
self.interrupt_running_vm()
}
pub fn cmd_si(&mut self, _parts: &[&str]) -> Result<()> {
if self.ctx.backend.is_running() {
error!("VM is running");
return Ok(());
}
// The step itself (over-breakpoint dance, trap-flag clear, breakpoint
// re-arm, thread re-select) is the canonical `Session::step`;
// the REPL only adds the break-context display.
if let Err(e) = self.ctx.step() {
error!("failed to step: {:?}", e);
return Ok(());
}
println!();
print_break_context(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
);
Ok(())
}
fn run_to_temporary_code_breakpoint(&mut self, address: VirtAddr) -> Result<()> {
if self
.ctx
.breakpoints
.enabled_breakpoint_id_for_current_context(&self.ctx.target, address)
.is_some()
{
return self.cmd_continue(&["continue"]);
}
let temp_id = match self.ctx.breakpoints.add_temporary_code(
&mut *self.ctx.backend,
&self.ctx.target,
address,
) {
Ok(id) => id,
Err(e) => {
error!(
"failed to set temporary breakpoint at {}: {}",
ui::addr(address.0),
e
);
return Ok(());
}
};
self.caches.refresh_breakpoints(&self.ctx.breakpoints);
let result = self.cmd_continue(&["continue"]);
let _ = self
.ctx
.breakpoints
.remove(&mut *self.ctx.backend, &self.ctx.target, temp_id);
self.caches.refresh_breakpoints(&self.ctx.breakpoints);
result
}
pub fn cmd_p(&mut self, _parts: &[&str]) -> Result<()> {
if self.ctx.backend.is_running() {
error!("VM is running");
return Ok(());
}
// The step-over decision (is the current insn a call? where does it
// return?) is shared with the SDKs; the REPL only differs in *how* it
// runs to the target, via its rich-display continue loop.
match self.ctx.step_over_target() {
Ok(StepKind::Single) => self.cmd_si(&["si"]),
Ok(StepKind::RunTo(target)) => self.run_to_temporary_code_breakpoint(target),
Err(e) => {
error!("failed to decode current instruction: {}", e);
Ok(())
}
}
}
pub fn cmd_gu(&mut self, _parts: &[&str]) -> Result<()> {
if self.ctx.backend.is_running() {
error!("VM is running");
return Ok(());
}
match self.ctx.step_out_target() {
Ok(target) => self.run_to_temporary_code_breakpoint(target),
Err(e) => {
error!("{}", e);
Ok(())
}
}
}
}
#[cfg(test)]
mod tests {
use crate::session::split_condition_operator;
#[test]
fn condition_operator_splitter_prefers_two_char_operators() {
assert_eq!(
split_condition_operator("$rax >= 0x10"),
Some(("$rax", ">=", "0x10"))
);
assert_eq!(
split_condition_operator("$rax == $rbx"),
Some(("$rax", "==", "$rbx"))
);
assert_eq!(split_condition_operator("$rax"), None);
}
}