wdext 0.1.0

A DbgEng wrapper framework
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// SPDX-FileCopyrightText: 2026 takubokudori
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::{ffi::c_void, slice};

use windows::{Win32::Foundation::E_POINTER, core::GUID};

use crate::{
    WdResult, impl_ttd_view,
    ttd::{cursor::Cursor, raw::bindings::*},
};

impl_ttd_view!(ReplayEngineView, IReplayEngineView);

/// Per-thread position-index tables exposed by the replay engine.
#[derive(Debug, Clone, Copy)]
pub struct ThreadPositionIndices<'a> {
    first: &'a [usize],
    last: &'a [usize],
    lifetime_first: &'a [usize],
    lifetime_last: &'a [usize],
}

impl<'a> ThreadPositionIndices<'a> {
    #[inline]
    pub fn first(&self) -> &'a [usize] { self.first }

    #[inline]
    pub fn last(&self) -> &'a [usize] { self.last }

    #[inline]
    pub fn lifetime_first(&self) -> &'a [usize] { self.lifetime_first }

    #[inline]
    pub fn lifetime_last(&self) -> &'a [usize] { self.lifetime_last }
}

// IReplayEngineView
impl ReplayEngineView {
    /// Reinterprets this engine view using TTD's native interface mechanism.
    ///
    /// # Safety
    ///
    /// `iid` must identify the interface type to which the returned pointer is
    /// cast, and the native engine must outlive every use of that pointer.
    pub unsafe fn unsafe_as_interface(&self, iid: &GUID) -> *mut c_void {
        unsafe { self.0.UnsafeAsInterface(iid) }
    }

    /// Const variant of [`Self::unsafe_as_interface`].
    ///
    /// # Safety
    ///
    /// The same requirements as [`Self::unsafe_as_interface`] apply.
    pub unsafe fn unsafe_as_interface_const(
        &self,
        iid: &GUID,
    ) -> *const c_void {
        unsafe { self.0.UnsafeAsInterfaceConst(iid) }
    }

    #[inline]
    pub fn get_peb_address(&self) -> GuestAddress {
        unsafe { self.0.GetPebAddress() }
    }

    /// Returns a snapshot of the trace system information.
    #[inline]
    pub fn get_system_info(&self) -> SystemInfo {
        unsafe { *self.0.GetSystemInfo() }
    }

    /// Returns a snapshot of the first recorded position.
    #[inline]
    pub fn get_first_position(&self) -> Position {
        unsafe { *self.0.GetFirstPosition() }
    }

    /// Returns a snapshot of the last recorded position.
    #[inline]
    pub fn get_last_position(&self) -> Position {
        unsafe { *self.0.GetLastPosition() }
    }

    /// Returns a snapshot of the trace lifetime.
    #[inline]
    pub fn get_lifetime(&self) -> PositionRange {
        unsafe { *self.0.GetLifetime() }
    }

    #[inline]
    pub fn get_recording_type(&self) -> RecordingType {
        unsafe { self.0.GetRecordingType() }
    }

    /// Returns a snapshot of the requested thread metadata.
    #[inline]
    pub fn get_thread_info(
        &self,
        unique_thread_id: UniqueThreadId,
    ) -> ThreadInfo {
        unsafe { *self.0.GetThreadInfo(unique_thread_id) }
    }

    #[inline]
    pub fn get_thread_count(&self) -> usize {
        unsafe { self.0.GetThreadCount() }
    }

    #[inline]
    pub fn get_thread_list(&self) -> *const ThreadInfo {
        unsafe { self.0.GetThreadList() }
    }

    #[inline]
    pub fn get_thread_first_position_index(&self) -> *const usize {
        unsafe { self.0.GetThreadFirstPositionIndex() }
    }

    #[inline]
    pub fn get_thread_last_position_index(&self) -> *const usize {
        unsafe { self.0.GetThreadLastPositionIndex() }
    }

    #[inline]
    pub fn get_thread_lifetime_first_position_index(&self) -> *const usize {
        unsafe { self.0.GetThreadLifetimeFirstPositionIndex() }
    }

    #[inline]
    pub fn get_thread_lifetime_last_position_index(&self) -> *const usize {
        unsafe { self.0.GetThreadLifetimeLastPositionIndex() }
    }

    #[inline]
    pub fn get_thread_created_event_count(&self) -> usize {
        unsafe { self.0.GetThreadCreatedEventCount() }
    }

    #[inline]
    pub fn get_thread_created_event_list(&self) -> *const ThreadCreatedEvent {
        unsafe { self.0.GetThreadCreatedEventList() }
    }

    #[inline]
    pub fn get_thread_terminated_event_count(&self) -> usize {
        unsafe { self.0.GetThreadTerminatedEventCount() }
    }

    #[inline]
    pub fn get_thread_terminated_event_list(
        &self,
    ) -> *const ThreadTerminatedEvent {
        unsafe { self.0.GetThreadTerminatedEventList() }
    }

    #[inline]
    pub fn get_module_count(&self) -> usize {
        unsafe { self.0.GetModuleCount() }
    }

    #[inline]
    pub fn get_module_list(&self) -> *const Module {
        unsafe { self.0.GetModuleList() }
    }

    #[inline]
    pub fn get_module_instance_count(&self) -> usize {
        unsafe { self.0.GetModuleInstanceCount() }
    }

    #[inline]
    pub fn get_module_instance_list(&self) -> *const ModuleInstance {
        unsafe { self.0.GetModuleInstanceList() }
    }

    #[inline]
    pub fn get_module_instance_unload_index(&self) -> *const usize {
        unsafe { self.0.GetModuleInstanceUnloadIndex() }
    }

    #[inline]
    pub fn get_module_loaded_event_count(&self) -> usize {
        unsafe { self.0.GetModuleLoadedEventCount() }
    }

    #[inline]
    pub fn get_module_loaded_event_list(&self) -> *const ModuleLoadedEvent {
        unsafe { self.0.GetModuleLoadedEventList() }
    }

    #[inline]
    pub fn get_module_unloaded_event_count(&self) -> usize {
        unsafe { self.0.GetModuleUnloadedEventCount() }
    }

    #[inline]
    pub fn get_module_unloaded_event_list(&self) -> *const ModuleUnloadedEvent {
        unsafe { self.0.GetModuleUnloadedEventList() }
    }

    #[inline]
    pub fn get_exception_event_count(&self) -> usize {
        unsafe { self.0.GetExceptionEventCount() }
    }

    #[inline]
    pub fn get_exception_event_list(&self) -> *const ExceptionEvent {
        unsafe { self.0.GetExceptionEventList() }
    }

    /// Returns the first exception at or after `position` without copying it.
    ///
    /// The event itself is stored by TTD and the returned reference is tied to
    /// this replay-engine view. Raw pointers embedded in `ExceptionEvent` are
    /// still non-owning native pointers and retain their native lifetime rules.
    pub fn get_exception_at_or_after_position(
        &self,
        position: Position,
    ) -> Option<&ExceptionEvent> {
        let ptr = unsafe { self.0.GetExceptionAtOrAfterPosition(position) };
        unsafe { ptr.as_ref() }
    }

    #[inline]
    pub fn get_keyframe_count(&self) -> usize {
        unsafe { self.0.GetKeyframeCount() }
    }

    #[inline]
    pub fn get_keyframe_list(&self) -> *const Position {
        unsafe { self.0.GetKeyframeList() }
    }

    #[inline]
    pub fn get_record_client_count(&self) -> usize {
        unsafe { self.0.GetRecordClientCount() }
    }

    #[inline]
    pub fn get_record_client_list(&self) -> *const RecordClient {
        unsafe { self.0.GetRecordClientList() }
    }

    /// Returns a record client without copying it, or `None` if TTD returns null.
    pub fn get_record_client(
        &self,
        id: RecordClientId,
    ) -> Option<&RecordClient> {
        let ptr = unsafe { self.0.GetRecordClient(id) };
        unsafe { ptr.as_ref() }
    }

    #[inline]
    pub fn get_custom_event_count(&self) -> usize {
        unsafe { self.0.GetCustomEventCount() }
    }

    #[inline]
    pub fn get_custom_event_list(&self) -> *const CustomEvent {
        unsafe { self.0.GetCustomEventList() }
    }

    #[inline]
    pub fn get_activity_count(&self) -> usize {
        unsafe { self.0.GetActivityCount() }
    }

    #[inline]
    pub fn get_activity_list(&self) -> *const Activity {
        unsafe { self.0.GetActivityList() }
    }

    #[inline]
    pub fn get_island_count(&self) -> usize {
        unsafe { self.0.GetIslandCount() }
    }

    #[inline]
    pub fn get_island_list(&self) -> *const Island {
        unsafe { self.0.GetIslandList() }
    }

    /// Creates an owning replay cursor.
    pub fn new_cursor(&self) -> WdResult<Cursor> {
        let raw = unsafe { self.0.NewCursor() };

        // SAFETY: NewCursor transfers ownership of the returned ICursor to the
        // caller.
        unsafe { Cursor::from_raw(raw) }.ok_or_else(|| E_POINTER.into())
    }

    /// Builds the replay index.
    ///
    /// # Safety
    ///
    /// If `report_progress` uses `caller_context`, the context pointer must
    /// remain valid for every callback invocation made by TTD. The callback
    /// must obey the SDK callback ABI and must not unwind across FFI.
    pub unsafe fn build_index(
        &self,
        report_progress: IndexBuildProgressCallback,
        caller_context: *const c_void,
        flags: IndexBuildFlags,
    ) -> IndexStatus {
        unsafe { self.0.BuildIndex(report_progress, caller_context, flags) }
    }

    #[inline]
    pub fn get_index_status(&self) -> IndexStatus {
        unsafe { self.0.GetIndexStatus() }
    }

    #[inline]
    pub fn get_index_file_stats(&self) -> IndexFileStats {
        unsafe { self.0.GetIndexFileStats() }
    }

    /// Configures TTD debug/error reporting.
    ///
    /// # Safety
    ///
    /// `error_reporting` must either be null if accepted by the selected mode,
    /// or point to a valid SDK-compatible `ErrorReporting` implementation for
    /// every use TTD makes of it.
    pub unsafe fn register_debug_mode_and_logging(
        &self,
        debug_mode: DebugModeType,
        error_reporting: *mut ErrorReporting,
    ) {
        unsafe {
            self.0
                .RegisterDebugModeAndLogging(debug_mode, error_reporting)
        }
    }

    /// Returns TTD's mutable engine-internals interface.
    ///
    /// # Safety
    ///
    /// `IEngineInternals` is an internal native interface. The caller is
    /// responsible for its ABI and lifetime requirements.
    pub unsafe fn get_internals(&self) -> *mut IEngineInternals {
        unsafe { self.0.GetInternals() }
    }

    /// Returns TTD's const engine-internals interface.
    ///
    /// # Safety
    ///
    /// The returned pointer is non-owning and may only be used while the native
    /// replay engine remains valid.
    pub unsafe fn get_internals_const(&self) -> *const IEngineInternals {
        unsafe { self.0.GetInternalsConst() }
    }
}

// Convenience helpers. Every helper calls one or more canonical wrappers above.
impl ReplayEngineView {
    #[inline]
    fn _ttd_slice<'a, T>(
        &'a self,
        ptr: *const T,
        count: usize,
        api: &'static str,
    ) -> &'a [T] {
        if count == 0 {
            return &[];
        }

        assert!(
            !ptr.is_null(),
            "TTD {api} returned null for a non-empty list"
        );

        // SAFETY: the TTD API contract pairs this pointer with the associated
        // count. The returned slice is tied to &self, and construction of this
        // non-owning ReplayEngineView is itself unsafe unless the native view
        // remains alive for all uses of the wrapper.
        unsafe { slice::from_raw_parts(ptr, count) }
    }

    pub fn _get_thread_list(&self) -> &[ThreadInfo] {
        self._ttd_slice(
            self.get_thread_list(),
            self.get_thread_count(),
            "GetThreadList",
        )
    }

    /// Returns all four per-thread position-index tables.
    pub fn _get_thread_position_indices(&self) -> ThreadPositionIndices<'_> {
        let count = self.get_thread_count();

        ThreadPositionIndices {
            first: self._ttd_slice(
                self.get_thread_first_position_index(),
                count,
                "GetThreadFirstPositionIndex",
            ),
            last: self._ttd_slice(
                self.get_thread_last_position_index(),
                count,
                "GetThreadLastPositionIndex",
            ),
            lifetime_first: self._ttd_slice(
                self.get_thread_lifetime_first_position_index(),
                count,
                "GetThreadLifetimeFirstPositionIndex",
            ),
            lifetime_last: self._ttd_slice(
                self.get_thread_lifetime_last_position_index(),
                count,
                "GetThreadLifetimeLastPositionIndex",
            ),
        }
    }

    pub fn _get_thread_first_position_indices(&self) -> &[usize] {
        self._ttd_slice(
            self.get_thread_first_position_index(),
            self.get_thread_count(),
            "GetThreadFirstPositionIndex",
        )
    }

    pub fn _get_thread_last_position_indices(&self) -> &[usize] {
        self._ttd_slice(
            self.get_thread_last_position_index(),
            self.get_thread_count(),
            "GetThreadLastPositionIndex",
        )
    }

    pub fn _get_thread_lifetime_first_position_indices(&self) -> &[usize] {
        self._ttd_slice(
            self.get_thread_lifetime_first_position_index(),
            self.get_thread_count(),
            "GetThreadLifetimeFirstPositionIndex",
        )
    }

    pub fn _get_thread_lifetime_last_position_indices(&self) -> &[usize] {
        self._ttd_slice(
            self.get_thread_lifetime_last_position_index(),
            self.get_thread_count(),
            "GetThreadLifetimeLastPositionIndex",
        )
    }

    pub fn _get_thread_created_event_list(&self) -> &[ThreadCreatedEvent] {
        self._ttd_slice(
            self.get_thread_created_event_list(),
            self.get_thread_created_event_count(),
            "GetThreadCreatedEventList",
        )
    }

    pub fn _get_thread_terminated_event_list(
        &self,
    ) -> &[ThreadTerminatedEvent] {
        self._ttd_slice(
            self.get_thread_terminated_event_list(),
            self.get_thread_terminated_event_count(),
            "GetThreadTerminatedEventList",
        )
    }

    pub fn _get_module_list(&self) -> &[Module] {
        self._ttd_slice(
            self.get_module_list(),
            self.get_module_count(),
            "GetModuleList",
        )
    }

    pub fn _get_module_instance_list(&self) -> &[ModuleInstance] {
        self._ttd_slice(
            self.get_module_instance_list(),
            self.get_module_instance_count(),
            "GetModuleInstanceList",
        )
    }

    pub fn _get_module_instance_unload_indices(&self) -> &[usize] {
        self._ttd_slice(
            self.get_module_instance_unload_index(),
            self.get_module_instance_count(),
            "GetModuleInstanceUnloadIndex",
        )
    }

    pub fn _get_module_loaded_event_list(&self) -> &[ModuleLoadedEvent] {
        self._ttd_slice(
            self.get_module_loaded_event_list(),
            self.get_module_loaded_event_count(),
            "GetModuleLoadedEventList",
        )
    }

    pub fn _get_module_unloaded_event_list(&self) -> &[ModuleUnloadedEvent] {
        self._ttd_slice(
            self.get_module_unloaded_event_list(),
            self.get_module_unloaded_event_count(),
            "GetModuleUnloadedEventList",
        )
    }

    pub fn _get_exception_event_list(&self) -> &[ExceptionEvent] {
        self._ttd_slice(
            self.get_exception_event_list(),
            self.get_exception_event_count(),
            "GetExceptionEventList",
        )
    }

    pub fn _get_keyframe_list(&self) -> &[Position] {
        self._ttd_slice(
            self.get_keyframe_list(),
            self.get_keyframe_count(),
            "GetKeyframeList",
        )
    }

    pub fn _get_record_client_list(&self) -> &[RecordClient] {
        self._ttd_slice(
            self.get_record_client_list(),
            self.get_record_client_count(),
            "GetRecordClientList",
        )
    }

    pub fn _get_custom_event_list(&self) -> &[CustomEvent] {
        self._ttd_slice(
            self.get_custom_event_list(),
            self.get_custom_event_count(),
            "GetCustomEventList",
        )
    }

    pub fn _get_activity_list(&self) -> &[Activity] {
        self._ttd_slice(
            self.get_activity_list(),
            self.get_activity_count(),
            "GetActivityList",
        )
    }

    pub fn _get_island_list(&self) -> &[Island] {
        self._ttd_slice(
            self.get_island_list(),
            self.get_island_count(),
            "GetIslandList",
        )
    }
}