Skip to main content

linux_perf_data/
auxtrace.rs

1use byteorder::ByteOrder;
2use linux_perf_event_reader::RawData;
3
4/// Auxtrace data record.
5///
6/// This is usually used with Intel PT, which records the pure Intel PT data.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Auxtrace<'a> {
9    /// Size of `aux_data`
10    pub size: u64,
11    /// Offset of this trace in the total trace
12    pub offset: u64,
13    /// Reference ID
14    pub reference: u64,
15    /// Index of this trace in the total trace
16    pub index: u32,
17    /// ID of thread that contributes to this trace
18    pub tid: u32,
19    /// ID of CPU that contributes to this trace
20    pub cpu: u32,
21    /// Pure data. The length of this data is guaranteed to be consistent
22    /// with the `size` field.
23    pub aux_data: RawData<'a>,
24}
25
26impl<'a> Auxtrace<'a> {
27    pub fn parse<T: ByteOrder>(mut data: RawData<'a>) -> Result<Self, std::io::Error> {
28        let size = data.read_u64::<T>()?;
29        let offset = data.read_u64::<T>()?;
30        let reference = data.read_u64::<T>()?;
31        let index = data.read_u32::<T>()?;
32        let tid = data.read_u32::<T>()?;
33        let cpu = data.read_u32::<T>()?;
34        let _reserved = data.read_u32::<T>()?;
35        let aux_data = data;
36        Ok(Self {
37            size,
38            offset,
39            reference,
40            index,
41            tid,
42            cpu,
43            aux_data,
44        })
45    }
46}
47
48#[cfg(test)]
49mod test {
50    use byteorder::LittleEndian;
51    use linux_perf_event_reader::RawData;
52
53    use super::Auxtrace;
54
55    #[test]
56    fn parse() {
57        let data = RawData::Single(&[
58            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59            0x00, 0x00, 0xf9, 0xcd, 0x2a, 0x49, 0x28, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
60            0x7a, 0x24, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61        ]);
62        let auxtrace = Auxtrace::parse::<LittleEndian>(data).unwrap();
63        assert_eq!(auxtrace.size, 0x01);
64        assert_eq!(auxtrace.offset, 0x00);
65        assert_eq!(auxtrace.reference, 0x428492acdf9);
66        assert_eq!(auxtrace.index, 0x8);
67        assert_eq!(auxtrace.tid, 0x247a);
68        assert_eq!(auxtrace.cpu, 8);
69        assert_eq!(auxtrace.aux_data.len(), auxtrace.size as usize);
70    }
71}