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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::sync::atomic::Ordering;
use owo_colors::OwoColorize;
use crate::dbg_backend::ContinueDisposition;
use crate::error::Result;
use crate::gdb::breakpoints::Breakpoint;
use crate::session::{StepKind, StopResolution};
use crate::types::VirtAddr;
use crate::ui;
use crate::repl::*;
repl_command! {
continue_vm();
names: ["continue", "g"],
usage: "continue",
summary: "Resume VM execution.",
}
repl_command! {
continue_handled();
names: ["gh"],
usage: "gh",
summary: "Resume and mark the current exception handled.",
}
repl_command! {
continue_not_handled();
names: ["gn"],
usage: "gn",
summary: "Resume and pass the current exception to Windows (KD only).",
}
repl_command! {
interrupt_running_vm();
names: ["break"],
usage: "break",
summary: "Break/pause VM execution.",
run_state: Running,
}
repl_command! {
single_step();
names: ["si", "t"],
usage: "si",
summary: "Single step (step into).",
run_state: Halted,
}
repl_command! {
cmd_p();
names: ["p", "ni"],
usage: "p or ni",
summary: "Step over the current instruction.",
run_state: Halted,
}
repl_command! {
cmd_gu();
names: ["gu", "finish"],
usage: "gu or finish",
summary: "Run until the current function returns.",
run_state: Halted,
}
impl ReplState<'_> {
pub fn interrupt_running_vm(&mut self) -> Result<()> {
match surface_pending_stop(self.ctx, &self.caches, &self.exception_policies) {
Ok(true) => {
if let Err(error) = self.apply_buffered_exception_policy() {
error!("failed to apply exception policy: {error}");
}
return Ok(());
}
Ok(false) => {}
Err(e) => {
error!("error checking running VM: {:?}", e);
return Ok(());
}
}
if let Err(e) = surface_interrupt_stop(self.ctx, &self.caches) {
error!("failed to interrupt: {:?}", e);
}
Ok(())
}
fn continue_vm(&mut self) -> Result<()> {
self.continue_vm_with_disposition(ContinueDisposition::Handled)
}
fn continue_handled(&mut self) -> Result<()> {
self.continue_vm_with_disposition(ContinueDisposition::Handled)
}
fn continue_not_handled(&mut self) -> Result<()> {
self.continue_vm_with_disposition(ContinueDisposition::NotHandled)
}
fn run_exception_policy_command(&mut self, command: Option<&str>) -> Result<()> {
if let Some(command) = command {
self.dispatch_exception_command(command)?;
}
Ok(())
}
/// A pending stop is rendered by the asynchronous stop helper before it can
/// hand control back here. Commands still run, and an explicit continue
/// outcome is applied afterward; command-free auto-continues were already
/// completed by the helper.
fn apply_buffered_exception_policy(&mut self) -> Result<()> {
let Some(event) = self.ctx.last_event.as_ref().map(|last| last.stop.clone()) else {
return Ok(());
};
match self.exception_policies.action_for(&event) {
ExceptionPolicyAction::Surface {
command: Some(command),
} => self.run_exception_policy_command(Some(&command)),
ExceptionPolicyAction::Continue {
notify,
disposition,
command: Some(command),
} => {
self.run_exception_policy_command(Some(&command))?;
if notify {
println!(
"Exception {:#010x}; continuing",
event.exception_code.unwrap_or_default()
);
}
self.ctx
.backend
.continue_execution_with_disposition(disposition)?;
self.ctx.record_continuation_disposition(disposition);
Ok(())
}
ExceptionPolicyAction::Surface { command: None }
| ExceptionPolicyAction::Continue { command: None, .. } => Ok(()),
}
}
fn continue_vm_with_disposition(&mut self, disposition: ContinueDisposition) -> Result<()> {
if self.ctx.backend.is_running() {
match surface_pending_stop(self.ctx, &self.caches, &self.exception_policies) {
Ok(true) => {
if let Err(error) = self.apply_buffered_exception_policy() {
error!("failed to apply exception policy: {error}");
}
}
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_with_disposition(disposition) {
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(event)) => {
let resolution = match self.ctx.classify_stop_event(event) {
Ok(StopResolution::Resumed) => continue,
Ok(resolution) => resolution,
Err(error) => {
error!("failed to classify stop: {error}");
break;
}
};
let target_reloaded =
matches!(&resolution, StopResolution::TargetReloaded { .. });
let modules_changed = refresh_stop_caches_pre(
&mut *self.ctx.backend,
&self.ctx.target,
&mut self.ctx.breakpoints,
&self.caches,
);
refresh_stop_caches_post(
&self.ctx.target,
&self.caches,
target_reloaded,
modules_changed,
);
refresh_windows_thread_context_for_backend_thread(
&mut self.ctx.target,
&self.ctx.current_thread,
);
match resolution {
StopResolution::Resumed => unreachable!("handled above"),
StopResolution::Breakpoint {
breakpoint,
condition_error,
..
} => {
if let Some(error) = condition_error {
error!("breakpoint condition failed: {error}");
}
if let Some(action) = breakpoint.action.as_deref()
&& self.dispatch_breakpoint_action(action)?
{
if let Err(error) = self
.ctx
.resume_with_disposition(ContinueDisposition::Handled)
{
error!("failed to continue after breakpoint action: {error}");
break;
}
continue;
}
if breakpoint.hardware.is_some() {
self.surface_hardware_breakpoint_hit(&breakpoint);
} else {
print_stop_separator();
let cause = (!breakpoint.temporary).then(|| {
format!(
"{} {}",
ui::muted("breakpoint"),
ui::bp_id(breakpoint.id)
)
});
print_break_context_at(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
None,
cause,
);
}
break;
}
StopResolution::Bugcheck { event } => {
print_stop_separator();
print_bugcheck_summary(&self.ctx.target, event.bugcheck.as_ref());
println!();
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(),
);
break;
}
StopResolution::TargetReloaded { event, coherent } => {
print_stop_separator();
print_target_reload_notification_context(
&self.ctx.target,
&self.ctx.current_thread,
&event,
TargetReloadStatus::Reloaded {
loaded_module_list_available: coherent,
},
);
break;
}
StopResolution::Stopped { event, .. } => {
match self.exception_policies.action_for(&event) {
ExceptionPolicyAction::Surface { command } => {
if let Err(error) =
self.run_exception_policy_command(command.as_deref())
{
error!("exception command failed: {error}");
}
}
ExceptionPolicyAction::Continue {
notify,
disposition,
command,
} => {
if let Err(error) =
self.run_exception_policy_command(command.as_deref())
{
error!("exception command failed: {error}");
} else {
if notify {
let code = event.exception_code.unwrap_or_default();
let address =
event.exception_address.or(event.program_counter);
let chance = match event.first_chance {
Some(true) => "first chance",
Some(false) => "second chance",
None => "unknown chance",
};
let location = address
.map(|address| format!(" at {address:#x}"))
.unwrap_or_default();
println!(
"Exception {code:#010x} ({chance}){location}; continuing"
);
}
match self
.ctx
.backend
.continue_execution_with_disposition(disposition)
{
Ok(()) => {
self.ctx
.record_continuation_disposition(disposition);
continue;
}
Err(error) => {
error!(
"failed to continue after exception: {error}"
);
break;
}
}
}
}
}
print_stop_separator();
let cause =
stop_exception_cause(event.exception_code, event.program_counter);
print_break_context_at(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
None,
cause,
);
break;
}
}
}
Ok(None) => {
// timeout
}
Err(e) => {
error!("error waiting for stop: {:?}", e);
break;
}
}
}
Ok(())
}
/// Print a hardware (DR) breakpoint hit, mirroring the software-breakpoint
/// `Hit` rendering. The address the breakpoint watches is unrelated to
/// `rip` for data watches, so the cause child keeps the watched symbol
/// while the BREAK banner shows where execution actually stopped.
fn surface_hardware_breakpoint_hit(&mut self, bp: &Breakpoint) {
print_stop_separator();
let access = bp
.hardware
.map(|hw| format!(" {}{}", hw.access.letter(), hw.len))
.unwrap_or_default();
let cause = format!(
"{} {}{}{}",
ui::muted("hardware breakpoint"),
ui::bp_id(bp.id),
access.bright_black(),
bp.symbol
.as_ref()
.map(|s| format!(" {}", ui::symbol(s)))
.unwrap_or_default()
);
print_break_context_at(
&mut *self.ctx.backend,
&self.ctx.register_map,
&mut self.ctx.target,
&self.ctx.breakpoints,
&self.ctx.current_thread,
None,
Some(cause),
);
}
fn single_step(&mut self) -> Result<()> {
// 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(());
}
print_stop_separator();
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.continue_vm();
}
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.continue_vm();
let _ = self
.ctx
.breakpoints
.remove(&mut *self.ctx.backend, &self.ctx.target, temp_id);
self.caches.refresh_breakpoints(&self.ctx.breakpoints);
result
}
fn cmd_p(&mut self) -> Result<()> {
// 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.single_step(),
Ok(StepKind::RunTo(target)) => self.run_to_temporary_code_breakpoint(target),
Err(e) => {
error!("failed to decode current instruction: {}", e);
Ok(())
}
}
}
fn cmd_gu(&mut self) -> Result<()> {
match self.ctx.step_out_target() {
Ok(target) => self.run_to_temporary_code_breakpoint(target),
Err(e) => {
error!("{}", e);
Ok(())
}
}
}
}