one_collect 0.1.34811

Cross-platform library for capturing machine-level traces.
Documentation
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use std::collections::HashMap;
use std::collections::hash_map::Drain;
use std::collections::hash_map::Entry::{Vacant, Occupied};

use super::*;
use crate::{Writable, ReadOnly};
use crate::etw::*;

struct PartialThreadCallstacks {
    partials: HashMap<u64, PartialCallstack>,
}

impl PartialThreadCallstacks {
    fn new() -> Self {
        Self {
            partials: HashMap::default(),
        }
    }

    fn flush(&mut self) -> Drain<'_, u64, PartialCallstack> {
        self.partials.drain()
    }

    fn add_frames<'a>(
        &'a mut self,
        time: u64,
        frames: &'a [u64],
        buffer: &'a mut Vec<u64>) -> Option<&'a [u64]> {
        let user_stack = PartialCallstack::frames_end_in_userspace(frames);

        match self.partials.entry(time) {
            Vacant(entry) => {
                if user_stack {
                    /* Return immediately, since user mode only stack */
                    return Some(frames);
                }

                /* Kernel stack, save for userspace */
                let mut partial = PartialCallstack::default();

                partial.add_frames(frames);

                entry.insert(partial);

                None
            },
            Occupied(mut entry) => {
                if user_stack {
                    let partial = entry.remove();

                    buffer.clear();
                    buffer.extend_from_slice(partial.frames());
                    buffer.extend_from_slice(frames);

                    Some(&buffer[..])
                } else {
                    entry.get_mut().add_frames(frames);

                    None
                }
            }
        }
    }
}

pub struct CompletedCallstack<'a> {
    time: u64,
    pid: u32,
    tid: u32,
    frames: &'a [u64],
}

impl<'a> CompletedCallstack<'a> {
    fn new(
        time: u64,
        pid: u32,
        tid: u32,
        frames: &'a [u64]) -> Self {
        Self {
            time,
            pid,
            tid,
            frames,
        }
    }

    pub fn time(&self) -> u64 { self.time }

    pub fn pid(&self) -> u32 { self.pid }

    pub fn tid(&self) -> u32 { self.tid }

    pub fn frames(&'a self) -> &'a [u64] { self.frames }

    fn notify(
        &self,
        callbacks: &mut Vec<Box<dyn FnMut(&CompletedCallstack)>>) {
        for callback in callbacks {
            callback(self);
        }
    }
}

#[derive(Default)]
struct PartialCallstackLookup {
    stacks: HashMap<u64, PartialThreadCallstacks>,
    buffer: Vec<u64>,
    flushed_callbacks: Vec<Box<dyn FnMut()>>,
    frames_callbacks: Vec<Box<dyn FnMut(&CompletedCallstack)>>,
}

impl PartialCallstackLookup {
    fn add_flushed_callback(
        &mut self,
        callback: impl FnMut() + 'static) {
        self.flushed_callbacks.push(Box::new(callback));
    }

    fn add_frames_callback(
        &mut self,
        callback: impl FnMut(&CompletedCallstack) + 'static) {
        self.frames_callbacks.push(Box::new(callback));
    }

    fn flush(&mut self) {
        for (key, mut stacks) in self.stacks.drain() {
            let pid = (key >> 32 & 0xFFFF) as u32;
            let tid = (key & 0xFFFF) as u32;

            for (time, stack) in stacks.flush() {
                let full = CompletedCallstack::new(
                    time,
                    pid,
                    tid,
                    stack.frames());

                full.notify(&mut self.frames_callbacks);
            }
        }

        for callback in &mut self.flushed_callbacks {
            callback();
        }
    }

    fn add_frames(
        &mut self,
        time: u64,
        pid: u32,
        tid: u32,
        frames: &[u64]) {
        if frames.is_empty() {
            return;
        }

        let key = (pid as u64) << 32 | tid as u64;

        let stacks = self.stacks
            .entry(key)
            .or_insert_with(|| PartialThreadCallstacks::new());

        if let Some(frames) = stacks.add_frames(
            time,
            frames,
            &mut self.buffer) {
            let full = CompletedCallstack::new(
                time,
                pid,
                tid,
                frames);

            full.notify(&mut self.frames_callbacks);
        }
    }
}

pub struct CallstackReader {
    ancillary: ReadOnly<AncillaryData>,
    lookup: Writable<PartialCallstackLookup>,
    match_id: Writable<u64>,
}

impl Clone for CallstackReader {
    fn clone(&self) -> Self {
        Self {
            ancillary: self.ancillary.clone(),
            lookup: self.lookup.clone(),
            match_id: Writable::new(0),
        }
    }
}

impl CallstackReader {
    pub fn match_id(&self) -> u64 { *self.match_id.borrow() }

    pub fn add_flushed_callback(
        &self,
        callback: impl FnMut() + 'static) {
        self.lookup.borrow_mut().add_flushed_callback(callback);
    }

    pub fn add_async_frames_callback(
        &self,
        callback: impl FnMut(&CompletedCallstack) + 'static) {
        self.lookup.borrow_mut().add_frames_callback(callback);
    }

    pub fn read_frames(
        &self,
        _full_data: &[u8],
        frames: &mut Vec<u64>) {
        self.ancillary.borrow().callstack(
            frames,
            &mut self.match_id.borrow_mut());
    }
}

pub struct CallstackHelper {
    ancillary: Writable<ReadOnly<AncillaryData>>,
    lookup: Writable<PartialCallstackLookup>,
}

impl CallstackHelper {
    pub fn new() -> Self {
        /* Start with an empty ancillary data */
        let empty = Writable::new(AncillaryData::default());

        Self {
            ancillary: Writable::new(empty.read_only()),
            lookup: Writable::new(PartialCallstackLookup::default()),
        }
    }

    pub fn with_external_lookup(self) -> Self {
        /* NOP on Windows */
        self
    }

    pub fn has_unwinder(&self) -> bool { false }

    pub fn to_reader(self) -> CallstackReader {
        CallstackReader {
            ancillary: self.ancillary.borrow().clone(),
            lookup: self.lookup.clone(),
            match_id: Writable::new(0),
        }
    }
}

impl CallstackHelp for EtwSession {
    fn with_callstack_help(
        mut self,
        helper: &CallstackHelper) -> Self {
        let ancillary = self.ancillary_data();

        /* Set the ancillary data from the target session */
        *helper.ancillary.borrow_mut() = ancillary.clone();

        let helper_lookup = helper.lookup.clone();

        self.add_built_callback(move |session| {
            /*
             * Session is about to be parsed, check if
             * we should be handling kernel stacks.
             */
            if !session.needs_kernel_callstacks() {
                return Ok(());
            }

            /* Hookup async callstack event */
            let callstack = session.callstack_event();
            let fmt = callstack.format();
            let time = fmt.get_field_ref_unchecked("EventTimeStamp");
            let pid = fmt.get_field_ref_unchecked("StackProcess");
            let tid = fmt.get_field_ref_unchecked("StackThread");
            let frames = fmt.get_field_ref_unchecked("StackFrames");
            let frame_offset = fmt.get_field_unchecked(frames).offset;

            let lookup = helper_lookup.clone();
            let mut frame_buffer = Vec::new();

            /* Callstacks just add to our lookup as they arrive */
            callstack.add_callback(move |data| {
                let fmt = data.format();
                let data = data.event_data();

                let time = fmt.get_u64(time, data)?;
                let pid = fmt.get_u32(pid, data)?;
                let tid = fmt.get_u32(tid, data)?;

                /* Read frames from remaining data */
                let frame_data = &data[frame_offset..];

                frame_buffer.clear();

                for frame in frame_data.chunks_exact(8) {
                    let frame = unsafe { *(frame.as_ptr() as *const u64) };

                    frame_buffer.push(frame);
                }

                /* Add frames to lookup */
                lookup.borrow_mut().add_frames(
                    time,
                    pid,
                    tid,
                    &frame_buffer);

                Ok(())
            });

            /* Ensure we flush stacks upon being stopped */
            let lookup = helper_lookup.clone();

            session.add_stopped_callback(
                move |_context| {
                    lookup.borrow_mut().flush();
                });

            Ok(())
        });

        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    fn build_partial_lookup(
        time: u64,
        pid: u32,
        tid: u32,
        frames: Vec<u64>,
        outcome: Writable<bool>) -> PartialCallstackLookup {
        let mut stacks = PartialCallstackLookup::default();

        stacks.add_frames_callback(move |completed| {
            *outcome.borrow_mut() =
                completed.time != time ||
                completed.pid != pid ||
                completed.pid != tid ||
                completed.frames != &frames;
        });

        stacks
    }

    #[test]
    fn partial_callstacks() {
        let outcome = Writable::new(false);

        let mut stacks = build_partial_lookup(
            0xf00d,
            0,
            1,
            vec!(KERNEL_START, 0x1, 0x2, 0x3, 0x4),
            outcome.clone());

        /* Frames ending in userspace should give full callstack */
        stacks.add_frames(
            0xf00d,
            0,
            1,
            &vec!(KERNEL_START, 0x1, 0x2, 0x3, 0x4));

        assert_eq!(true, *outcome.borrow());
        *outcome.borrow_mut() = false;

        /* Partial frames should only complete on user */
        stacks.add_frames(
            0xf00d,
            0,
            1,
            &vec!(KERNEL_START));
        assert_eq!(false, *outcome.borrow());

        stacks.add_frames(
            0xf00d,
            0,
            1,
            &vec!(0x1, 0x2, 0x3, 0x4));
        assert_eq!(true, *outcome.borrow());
        *outcome.borrow_mut() = false;

        /* Flush should work for kernel only stacks */
        let mut stacks = build_partial_lookup(
            0xf00d,
            0,
            1,
            vec!(KERNEL_START),
            outcome.clone());

        stacks.add_frames(
            0xf00d,
            0,
            1,
            &vec!(KERNEL_START));
        assert_eq!(false, *outcome.borrow());

        stacks.flush();

        assert_eq!(true, *outcome.borrow());
    }

    #[test]
    #[ignore]
    fn it_works() {
        let helper = CallstackHelper::new();

        let mut session = EtwSession::new()
            .with_callstack_help(&helper);

        let ancillary = session.ancillary_data();

        let profile_count = Writable::new(0);
        let count = profile_count.clone();
        let event = session.profile_cpu_event(Some(PROPERTY_STACK_TRACE));
        let profile_times = Writable::new(HashSet::new());
        let times = profile_times.clone();

        event.add_callback(
            move |_data| {
                times.borrow_mut().insert(ancillary.borrow().time());
                *count.borrow_mut() += 1;
                Ok(())
            });

        let stack_reader = helper.to_reader();

        let stack_count = Writable::new(0);
        let count = stack_count.clone();
        let stack_times = Writable::new(HashSet::new());
        let times = stack_times.clone();

        stack_reader.add_async_frames_callback(
            move |stack| {
                times.borrow_mut().insert(stack.time());
                *count.borrow_mut() += 1;
            });

        let flushed_count = Writable::new(0);
        let count = flushed_count.clone();

        stack_reader.add_flushed_callback(
            move || {
                *count.borrow_mut() += 1;
            });

        let duration = std::time::Duration::from_secs(1);

        session.parse_for_duration(
            "one_collect_callstacks_test",
            duration).unwrap();

        let flushed_count = *flushed_count.borrow();
        let profile_count = *profile_count.borrow();
        let stack_count = *stack_count.borrow();

        println!("Counts:");
        println!("Profile: {}", profile_count);
        println!("Stacks: {}", stack_count);

        let mut first = u64::MAX;
        let mut last = u64::MIN;

        for time in profile_times.borrow().iter() {
            let time = *time;

            if time < first {
                first = time;
            }

            if time > last {
                last = time;
            }
        }

        for time in profile_times.borrow().iter() {
            if !stack_times.borrow().contains(time) {
                println!("Missed {}", time);
            }
        }

        println!("Range: {} - {}", first, last);

        assert!(flushed_count != 0);
        assert!(profile_count != 0);
        assert!(stack_count != 0);
    }
}