ntoseye 0.12.0

Windows kernel debugger for Linux hosts running Windows under KVM/QEMU
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use nu_ansi_term::Style;
use reedline::{
    Completer, Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, Span,
    Suggestion,
};
use reedline::{Highlighter, StyledText};
use std::str::FromStr;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::atomic::{AtomicBool, Ordering};

use strum::EnumMessage;
use strum::IntoEnumIterator;

use owo_colors::OwoColorize;
use std::borrow::Cow;

use crate::dbg_backend::DebugBackend;
use crate::debugger::{DebuggerContext, DriverObjectInfo, ThreadInfo};
use crate::error::Result;
use crate::expr::Expr;
use crate::gdb::BreakpointManager;
use crate::symbols::{SymbolIndex, SymbolStore};
use crate::types::{Dtb, VirtAddr};

use crate::repl::*;

#[derive(Clone)]
pub struct CustomPrompt;
pub static DEFAULT_MULTILINE_INDICATOR: &str = "     ::: ";
impl Prompt for CustomPrompt {
    fn render_prompt_left(&self) -> Cow<'_, str> {
        Cow::Owned("ntoseye>".bright_black().to_string())
    }

    fn render_prompt_right(&self) -> Cow<'_, str> {
        Cow::Owned("".into())
    }

    fn render_prompt_indicator(&self, _edit_mode: PromptEditMode) -> Cow<'_, str> {
        Cow::Owned(" ".to_string())
    }

    fn render_prompt_multiline_indicator(&self) -> Cow<'_, str> {
        Cow::Borrowed(DEFAULT_MULTILINE_INDICATOR)
    }

    fn render_prompt_history_search_indicator(
        &self,
        history_search: PromptHistorySearch,
    ) -> Cow<'_, str> {
        let prefix = match history_search.status {
            PromptHistorySearchStatus::Passing => "",
            PromptHistorySearchStatus::Failing => "failing ",
        };

        Cow::Owned(format!(
            "({}reverse-search: {}) ",
            prefix, history_search.term
        ))
    }
}

#[derive(Clone, Copy)]
pub enum CompletionStrategy {
    None,
    Symbol,
    Type,
    Process,
    Thread,
    Vcpu,
    Breakpoint,
    Driver,
}

impl CompletionStrategy {
    pub fn from_kebab(s: &str) -> Option<Self> {
        Some(match s {
            "none" | "" => Self::None,
            "symbol" => Self::Symbol,
            "type" => Self::Type,
            "process" => Self::Process,
            "thread" => Self::Thread,
            "vcpu" => Self::Vcpu,
            "breakpoint" => Self::Breakpoint,
            "driver" => Self::Driver,
            _ => return None,
        })
    }
}

pub fn make_suggestions(
    names: Vec<String>,
    description: &str,
    span_start: usize,
    pos: usize,
) -> Vec<Suggestion> {
    names
        .into_iter()
        .map(|name| Suggestion {
            value: name,
            description: Some(description.to_string()),
            style: None,
            extra: None,
            match_indices: None,
            span: Span::new(span_start, pos),
            append_whitespace: false,
        })
        .collect()
}

/// Cached process info for completion (name, PID)
pub type ProcessCache = Vec<(String, u64)>;

/// Cached execution-context IDs for completion
pub type VcpuCache = Vec<String>;

/// Cached Windows thread info for completion
pub type ThreadCache = Vec<ThreadInfo>;

/// Cached breakpoint info for completion (id, enabled, address, symbol)
pub type BreakpointCache = Vec<(u32, bool, VirtAddr, Option<String>)>;

/// Cached driver object info for completion
pub type DriverObjectCache = Vec<DriverObjectInfo>;

/// Cached (name, help, per-arg strategies) for script-registered commands
pub type UserCommandCache = Vec<(String, String, Vec<CompletionStrategy>)>;

/// Completion-facing state shared between the REPL loop (which rewrites the
/// caches as the target's state changes) and the tab completer. Every field is
/// a cheap-to-clone handle, so the loop and the completer each hold a clone.
#[derive(Clone)]
pub struct ReplCaches {
    pub symbols: Arc<RwLock<SymbolIndex>>,
    pub types: Arc<RwLock<SymbolIndex>>,
    pub symbol_store: Arc<SymbolStore>,
    pub dtb: Arc<RwLock<Dtb>>,
    pub processes: Arc<RwLock<ProcessCache>>,
    pub threads: Arc<RwLock<ThreadCache>>,
    pub vcpus: Arc<RwLock<VcpuCache>>,
    pub breakpoints: Arc<RwLock<BreakpointCache>>,
    pub drivers: Arc<RwLock<DriverObjectCache>>,
    pub user_commands: Arc<RwLock<UserCommandCache>>,
}

impl ReplCaches {
    /// Re-enumerate the guest's process list into the completion cache
    pub fn refresh_processes(&self, debugger: &DebuggerContext) -> Result<()> {
        let processes = debugger.guest.enumerate_processes()?;
        *self.processes.write().unwrap() = processes.into_iter().map(|p| (p.name, p.pid)).collect();
        Ok(())
    }

    pub fn refresh_vcpus(&self, client: &mut dyn DebugBackend) {
        if let Ok(vcpus) = client.thread_list() {
            *self.vcpus.write().unwrap() = vcpus;
        }
    }

    /// The thread cache is only populated on demand (threads/thread commands);
    /// a full thread walk is far too expensive to run on every stop, especially
    /// over serial KD. Reloads just drop the now-stale entries.
    pub fn clear_threads(&self) {
        self.threads.write().unwrap().clear();
    }

    /// Snapshot the current breakpoint set into the completion cache
    pub fn refresh_breakpoints(&self, breakpoints: &BreakpointManager) {
        *self.breakpoints.write().unwrap() = breakpoints
            .list()
            .iter()
            .map(|bp| (bp.id, bp.enabled, bp.address, bp.symbol.clone()))
            .collect();
    }

    /// Rebuild the symbol/type/DTB completion caches after the active context
    /// changes (kernel reload, process attach/detach)
    pub fn refresh_symbol_context(&self, debugger: &DebuggerContext) {
        *self.symbols.write().unwrap() = debugger.current_symbol_index();
        *self.types.write().unwrap() = debugger.current_types_index();
        *self.dtb.write().unwrap() = debugger.current_dtb();
    }

    /// Best-effort re-enumeration of driver objects into the completion cache,
    /// leaving the previous entries in place if enumeration fails
    pub fn refresh_drivers(&self, debugger: &DebuggerContext) {
        if let Ok(drivers) = debugger.enumerate_driver_objects() {
            *self.drivers.write().unwrap() = drivers;
        }
    }
}

pub struct MyCompleter {
    pub caches: ReplCaches,
}

#[derive(Clone, Copy)]
struct CompletionInput<'a> {
    raw_prefix: &'a str,
    ident_start: usize,
    prefix: &'a str,
    arg_start: usize,
    span_start: usize,
    pos: usize,
}

impl<'a> CompletionInput<'a> {
    fn new(text_before_cursor: &'a str, pos: usize) -> Self {
        let arg_start = text_before_cursor.rfind(' ').map(|i| i + 1).unwrap_or(0);
        let raw_prefix = &text_before_cursor[arg_start..];

        // Find the start of the identifier being typed by scanning backward for
        // expression boundary characters (operators, parens, dereference).
        let ident_start = raw_prefix
            .rfind(|c: char| !c.is_ascii_alphanumeric() && c != '_')
            .map(|i| i + 1)
            .unwrap_or(0);
        let prefix = &raw_prefix[ident_start..];
        let span_start = arg_start + ident_start;

        Self {
            raw_prefix,
            ident_start,
            prefix,
            arg_start,
            span_start,
            pos,
        }
    }
}

fn completion_arg_count(text_before_cursor: &str) -> usize {
    let mut arg_count = text_before_cursor.split_whitespace().count();
    if text_before_cursor.ends_with(char::is_whitespace) {
        arg_count += 1;
    }
    arg_count
}

impl Completer for MyCompleter {
    fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
        let text_before_cursor = &line[..pos];
        let mut parts = text_before_cursor.split_whitespace();

        let command_str = parts.next().unwrap_or("");
        let is_command_context = !text_before_cursor.contains(' ');

        if is_command_context {
            let mut suggestions: Vec<Suggestion> = ReplCommand::iter()
                .filter_map(|cmd| {
                    let c_str = cmd.to_string();
                    if c_str.starts_with(command_str) {
                        Some(Suggestion {
                            value: c_str,
                            description: cmd.get_message().map(String::from),
                            style: None,
                            extra: None,
                            match_indices: None,
                            span: Span::new(0, pos),
                            append_whitespace: true,
                        })
                    } else {
                        None
                    }
                })
                .collect();

            let user_cmds = self.caches.user_commands.read().unwrap();
            for (name, help, _) in user_cmds.iter() {
                if name.starts_with(command_str) {
                    suggestions.push(Suggestion {
                        value: name.clone(),
                        description: Some(help.clone()),
                        style: None,
                        extra: None,
                        match_indices: None,
                        span: Span::new(0, pos),
                        append_whitespace: true,
                    });
                }
            }
            return suggestions;
        }

        if let Ok(cmd) = ReplCommand::from_str(command_str) {
            let input = CompletionInput::new(text_before_cursor, pos);

            match cmd.completion_type() {
                CompletionStrategy::Type => {
                    // dt has a special third-arg promotion to symbol completion
                    if completion_arg_count(text_before_cursor) > 2 {
                        return self.apply_strategy(CompletionStrategy::Symbol, input);
                    }
                    return self.apply_strategy(CompletionStrategy::Type, input);
                }
                strat => {
                    return self.apply_strategy(strat, input);
                }
            }
        }

        // Fallback: script-registered command with per-arg completion hints
        let user_cmds = self.caches.user_commands.read().unwrap();
        if let Some((_, _, strategies)) = user_cmds.iter().find(|(n, _, _)| n == command_str) {
            let arg_count = completion_arg_count(text_before_cursor);
            let arg_index = arg_count.saturating_sub(2);
            let strat = strategies
                .get(arg_index)
                .copied()
                .unwrap_or(CompletionStrategy::None);

            return self.apply_strategy(strat, CompletionInput::new(text_before_cursor, pos));
        }

        vec![]
    }
}

impl MyCompleter {
    fn apply_strategy(
        &self,
        strategy: CompletionStrategy,
        input: CompletionInput<'_>,
    ) -> Vec<Suggestion> {
        match strategy {
            CompletionStrategy::None => vec![],

            CompletionStrategy::Symbol => {
                if input.ident_start > 0 {
                    let preceding = &input.raw_prefix[..input.ident_start];

                    if preceding.ends_with('+')
                        || preceding.ends_with('-')
                        || preceding.ends_with('[')
                    {
                        return vec![];
                    }

                    if let Some(expr_text) = preceding.strip_suffix("->") {
                        if let Ok(expr) = Expr::parse(expr_text) {
                            let dtb = *self.caches.dtb.read().unwrap();
                            let fields =
                                expr.complete_fields(&self.caches.symbol_store, dtb, input.prefix);
                            if !fields.is_empty() {
                                return make_suggestions(
                                    fields,
                                    "Field",
                                    input.span_start,
                                    input.pos,
                                );
                            }
                        }
                        return vec![];
                    }

                    if preceding.ends_with('(') {
                        let types = self.caches.types.read().unwrap();
                        let mut results = types.search(input.prefix, 512);
                        if !input.prefix.starts_with('_') {
                            results.extend(types.search(&format!("_{}", input.prefix), 512));
                        }
                        if !results.is_empty() {
                            return make_suggestions(results, "Type", input.span_start, input.pos);
                        }
                    }

                    // `module!<prefix>` -> complete symbols within that module
                    if let Some(module_part) = preceding.strip_suffix('!') {
                        let module = module_part
                            .rsplit(|c: char| !c.is_ascii_alphanumeric() && c != '_')
                            .next()
                            .unwrap_or("");
                        if !module.is_empty() {
                            let dtb = *self.caches.dtb.read().unwrap();
                            let results = self.caches.symbol_store.search_symbols_in_module(
                                dtb,
                                module,
                                input.prefix,
                                1024,
                            );
                            return make_suggestions(
                                results,
                                "Symbol",
                                input.span_start,
                                input.pos,
                            );
                        }
                    }
                }

                let symbols = self.caches.symbols.read().unwrap();
                let results = symbols.search(input.prefix, 1024);
                make_suggestions(results, "Symbol", input.span_start, input.pos)
            }

            CompletionStrategy::Type => {
                let types = self.caches.types.read().unwrap();
                let results = types.search(input.prefix, 1024);
                make_suggestions(results, "Structure", input.span_start, input.pos)
            }

            CompletionStrategy::Process => {
                let processes = self.caches.processes.read().unwrap();
                let prefix_lower = input.prefix.to_lowercase();
                processes
                    .iter()
                    .filter(|(name, pid)| {
                        name.to_lowercase().contains(&prefix_lower)
                            || pid.to_string().starts_with(input.prefix)
                    })
                    .map(|(name, pid)| Suggestion {
                        value: pid.to_string(),
                        description: Some(format!("{} (PID {})", name, pid)),
                        style: None,
                        extra: None,
                        match_indices: None,
                        span: Span::new(input.span_start, input.pos),
                        append_whitespace: false,
                    })
                    .collect()
            }

            CompletionStrategy::Thread => {
                let threads = self.caches.threads.read().unwrap();
                let prefix_lower = input.prefix.to_lowercase();
                threads
                    .iter()
                    .filter(|thread| {
                        thread
                            .tid
                            .is_some_and(|tid| tid.to_string().starts_with(input.prefix))
                            || format!("{:#x}", thread.ethread.0).starts_with(input.prefix)
                            || thread
                                .process_name
                                .as_deref()
                                .is_some_and(|name| name.to_lowercase().contains(&prefix_lower))
                    })
                    .map(|thread| {
                        let value = thread
                            .tid
                            .map(|tid| tid.to_string())
                            .unwrap_or_else(|| format!("{:#x}", thread.ethread.0));
                        let process = thread.process_name.as_deref().unwrap_or("unknown");
                        let tid = thread
                            .tid
                            .map(|tid| tid.to_string())
                            .unwrap_or_else(|| "-".to_string());
                        Suggestion {
                            value,
                            description: Some(format!(
                                "{} TID {} ETHREAD {:#x}",
                                process, tid, thread.ethread.0
                            )),
                            style: None,
                            extra: None,
                            match_indices: None,
                            span: Span::new(input.span_start, input.pos),
                            append_whitespace: false,
                        }
                    })
                    .collect()
            }

            CompletionStrategy::Vcpu => {
                let vcpus = self.caches.vcpus.read().unwrap();
                vcpus
                    .iter()
                    .filter(|tid| tid.starts_with(input.prefix))
                    .map(|tid| Suggestion {
                        value: tid.clone(),
                        description: Some("vCPU".to_string()),
                        style: None,
                        extra: None,
                        match_indices: None,
                        span: Span::new(input.span_start, input.pos),
                        append_whitespace: false,
                    })
                    .collect()
            }

            CompletionStrategy::Breakpoint => {
                let breakpoints = self.caches.breakpoints.read().unwrap();
                breakpoints
                    .iter()
                    .filter(|(id, _, _, _)| id.to_string().starts_with(input.prefix))
                    .map(|(id, _, addr, symbol)| {
                        let sym_str = symbol.as_deref().unwrap_or("-");
                        Suggestion {
                            value: id.to_string(),
                            description: Some(format!("{} @ {:#x}", sym_str, addr.0)),
                            style: None,
                            extra: None,
                            match_indices: None,
                            span: Span::new(input.span_start, input.pos),
                            append_whitespace: false,
                        }
                    })
                    .collect()
            }

            CompletionStrategy::Driver => {
                let drivers = self.caches.drivers.read().unwrap();
                let prefix_lower = input.prefix.to_lowercase();
                drivers
                    .iter()
                    .filter(|driver| {
                        driver.name.to_lowercase().contains(&prefix_lower)
                            || format!("{:#x}", driver.object.0).starts_with(input.prefix)
                    })
                    .map(|driver| Suggestion {
                        value: format!("{:#x}", driver.object.0),
                        description: Some(format!(
                            "{} start={:#x}",
                            driver.name, driver.driver_start.0
                        )),
                        style: None,
                        extra: None,
                        match_indices: None,
                        span: Span::new(input.arg_start, input.pos),
                        append_whitespace: false,
                    })
                    .collect()
            }
        }
    }
}

pub struct TrackingHighlighter {
    pub had_content: Arc<AtomicBool>,
}

impl Highlighter for TrackingHighlighter {
    fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
        self.had_content.store(!line.is_empty(), Ordering::Relaxed);

        let mut styled = StyledText::new();
        styled.push((Style::new(), line.to_string()));
        styled
    }
}