perf_event_data/records/
exit.rs

1use crate::prelude::*;
2
3/// EXIT records indicate that a process has exited.
4///
5/// This struct corresponds to `PERF_RECORD_EXIT`. See the [manpage] for more
6/// documentation.
7///
8/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
9#[derive(Clone, Debug)]
10#[allow(missing_docs)]
11pub struct Exit {
12    pub pid: u32,
13    pub ppid: u32,
14    pub tid: u32,
15    pub ptid: u32,
16    pub time: u64,
17}
18
19impl<'p> Parse<'p> for Exit {
20    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
21    where
22        E: Endian,
23        B: ParseBuf<'p>,
24    {
25        Ok(Self {
26            pid: p.parse()?,
27            ppid: p.parse()?,
28            tid: p.parse()?,
29            ptid: p.parse()?,
30            time: p.parse()?,
31        })
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::endian::Little;
39
40    #[test]
41    #[cfg_attr(not(target_endian = "little"), ignore)]
42    fn test_parse() {
43        #[rustfmt::skip]
44        let bytes: &[u8] = &[
45            0x10, 0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
46            0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
47            0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
48        ];
49
50        let mut parser: Parser<_, Little> = Parser::new(bytes, ParseConfig::default());
51        let exit: Exit = parser.parse().unwrap();
52
53        assert_eq!(exit.pid, 0x1010);
54        assert_eq!(exit.ppid, 0x0500);
55        assert_eq!(exit.tid, 0x01);
56        assert_eq!(exit.ptid, 0x02);
57        assert_eq!(exit.time, 0x0400000003);
58    }
59}