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
use std::borrow::Cow;

use bitflags::bitflags;
use perf_event_open_sys::bindings;

use crate::prelude::*;

/// KSYMBOL records indicate symbols being registered or unregistered within
/// the kernel.
///
/// This struct corresponds to `PERF_RECORD_KSYMBOL`. See the [manpage] for
/// more documentation.
///
/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct KSymbol<'a> {
    pub addr: u64,
    pub len: u32,
    pub ksym_type: KSymbolType,
    pub flags: KSymbolFlags,
    pub name: Cow<'a, [u8]>,
}

impl<'a> KSymbol<'a> {
    /// Convert all borrowed data in this `KSymbol` into owned data.
    pub fn into_owned(self) -> KSymbol<'static> {
        KSymbol {
            name: self.name.into_owned().into(),
            ..self
        }
    }
}

c_enum! {
    /// The type of the kernel symbol.
    pub struct KSymbolType : u16 {
        /// The symbol is of an unknown type.
        const UNKNOWN = bindings::PERF_RECORD_KSYMBOL_TYPE_UNKNOWN as _;

        /// The symbol is a BPF function.
        const BPF = bindings::PERF_RECORD_KSYMBOL_TYPE_BPF as _;

        /// The symbol is out-of-line code.
        ///
        /// See the [kernel source][src] for examples of when this could occur.
        ///
        /// [src]: https://sourcegraph.com/github.com/torvalds/linux@d4d58949a6eac1c45ab022562c8494725e1ac094/-/blob/tools/include/uapi/linux/perf_event.h?L1220
        const OOL = bindings::PERF_RECORD_KSYMBOL_TYPE_OOL as _;
    }
}

bitflags! {
    /// Flags for [`KSymbol`].
    #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
    pub struct KSymbolFlags : u16 {
        /// If set, this means that the symbol is being unregistered.
        const UNREGISTER = bindings::PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER as _;
    }
}

impl<'p> Parse<'p> for KSymbolType {
    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        Ok(Self::new(p.parse()?))
    }
}

impl<'p> Parse<'p> for KSymbolFlags {
    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        Ok(Self::from_bits_retain(p.parse()?))
    }
}

impl<'p> Parse<'p> for KSymbol<'p> {
    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
    where
        E: Endian,
        B: ParseBuf<'p>,
    {
        Ok(Self {
            addr: p.parse()?,
            len: p.parse()?,
            ksym_type: p.parse()?,
            flags: p.parse()?,
            name: p.parse_rest_trim_nul()?,
        })
    }
}