fxt/
thread.rs

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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{trace_header, ParseError, ParseResult, THREAD_RECORD_TYPE};
use nom::combinator::all_consuming;
use nom::number::complete::le_u64;
use std::num::NonZeroU8;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ProcessKoid(pub u64);

impl From<u64> for ProcessKoid {
    fn from(n: u64) -> Self {
        Self(n)
    }
}

impl PartialEq<u64> for ProcessKoid {
    fn eq(&self, other: &u64) -> bool {
        self.0.eq(other)
    }
}

impl std::fmt::Display for ProcessKoid {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum ProcessRef {
    Index(NonZeroU8),
    Inline(ProcessKoid),
}

impl ProcessRef {
    pub(crate) fn parse<'a>(thread_ref: u8, buf: &'a [u8]) -> ParseResult<'a, Self> {
        Ok(if let Some(index) = NonZeroU8::new(thread_ref) {
            (buf, Self::Index(index))
        } else {
            let (buf, koid) = le_u64(buf)?;
            (buf, Self::Inline(ProcessKoid(koid)))
        })
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ThreadKoid(pub u64);

impl From<u64> for ThreadKoid {
    fn from(n: u64) -> Self {
        Self(n)
    }
}

impl PartialEq<u64> for ThreadKoid {
    fn eq(&self, other: &u64) -> bool {
        self.0.eq(other)
    }
}

impl std::fmt::Display for ThreadKoid {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum ThreadRef {
    Index(NonZeroU8),
    Inline(ThreadKoid),
}

impl ThreadRef {
    pub(crate) fn parse<'a>(thread_ref: u8, buf: &'a [u8]) -> ParseResult<'a, Self> {
        Ok(if let Some(index) = NonZeroU8::new(thread_ref) {
            (buf, Self::Index(index))
        } else {
            let (buf, koid) = le_u64(buf)?;
            (buf, Self::Inline(ThreadKoid(koid)))
        })
    }
}

#[derive(Debug, PartialEq)]
pub(crate) struct ThreadRecord {
    pub index: NonZeroU8,
    pub process_koid: ProcessKoid,
    pub thread_koid: ThreadKoid,
}

impl ThreadRecord {
    pub(super) fn parse(buf: &[u8]) -> ParseResult<'_, Self> {
        let (buf, header) = ThreadHeader::parse(buf)?;
        let (rem, payload) = header.take_payload(buf)?;
        let (payload, process_koid) = nom::combinator::map(le_u64, ProcessKoid)(payload)?;
        let (empty, thread_koid) =
            all_consuming(nom::combinator::map(le_u64, ThreadKoid))(payload)?;
        assert!(empty.is_empty(), "all_consuming must not return any remaining buffer");
        let index =
            NonZeroU8::new(header.thread_index()).ok_or(nom::Err::Error(ParseError::InvalidRef))?;
        Ok((rem, Self { index, process_koid, thread_koid }))
    }
}

trace_header! {
    ThreadHeader (THREAD_RECORD_TYPE) {
        u8, thread_index: 16, 23;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::RawTraceRecord;

    #[test]
    fn process_ref_index() {
        let (trailing, parsed) = ProcessRef::parse(11, &[1, 1, 1, 1]).unwrap();
        assert_eq!(parsed, ProcessRef::Index(NonZeroU8::new(11).unwrap()));
        assert_eq!(trailing, [1, 1, 1, 1],);
    }

    #[test]
    fn process_ref_inline() {
        let mut buf = 52u64.to_le_bytes().to_vec(); // value
        buf.extend([1, 1, 1, 1]); // trailing

        let (trailing, parsed) = ProcessRef::parse(0, &buf).unwrap();
        assert_eq!(parsed, ProcessRef::Inline(ProcessKoid(52)));
        assert_eq!(trailing, [1, 1, 1, 1]);
    }

    #[test]
    fn thread_ref_index() {
        let (trailing, parsed) = ThreadRef::parse(14, &[1, 1, 1, 1]).unwrap();
        assert_eq!(parsed, ThreadRef::Index(NonZeroU8::new(14).unwrap()));
        assert_eq!(trailing, [1, 1, 1, 1]);
    }

    #[test]
    fn thread_ref_inline() {
        let mut buf = 54u64.to_le_bytes().to_vec(); // value
        buf.extend([1, 1, 1, 1]); // trailing

        let (trailing, parsed) = ThreadRef::parse(0, &buf).unwrap();
        assert_eq!(parsed, ThreadRef::Inline(ThreadKoid(54)));
        assert_eq!(trailing, [1, 1, 1, 1]);
    }

    #[test]
    fn thread_record() {
        let mut header = ThreadHeader::empty();
        header.set_thread_index(10);
        header.set_size_words(3); // header, process koid, thread koid

        let mut buf = header.0.to_le_bytes().to_vec(); // header
        buf.extend(52u64.to_le_bytes()); // process
        buf.extend(54u64.to_le_bytes()); // thread
        buf.extend([1, 1, 1, 1]); // trailing

        let (trailing, parsed) = RawTraceRecord::parse(&buf).unwrap();
        assert_eq!(
            parsed.parsed,
            RawTraceRecord::Thread(ThreadRecord {
                index: NonZeroU8::new(10).unwrap(),
                process_koid: ProcessKoid(52),
                thread_koid: ThreadKoid(54)
            })
        );
        assert_eq!(trailing, [1, 1, 1, 1]);
    }
}