perf_event_data/records/
ksymbol.rs

1use std::borrow::Cow;
2use std::fmt;
3
4use bitflags::bitflags;
5use perf_event_open_sys::bindings;
6
7use crate::prelude::*;
8
9/// KSYMBOL records indicate symbols being registered or unregistered within
10/// the kernel.
11///
12/// This struct corresponds to `PERF_RECORD_KSYMBOL`. See the [manpage] for
13/// more documentation.
14///
15/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
16#[derive(Clone)]
17#[allow(missing_docs)]
18pub struct KSymbol<'a> {
19    pub addr: u64,
20    pub len: u32,
21    pub ksym_type: KSymbolType,
22    pub flags: KSymbolFlags,
23    pub name: Cow<'a, [u8]>,
24}
25
26impl<'a> KSymbol<'a> {
27    /// Convert all borrowed data in this `KSymbol` into owned data.
28    pub fn into_owned(self) -> KSymbol<'static> {
29        KSymbol {
30            name: self.name.into_owned().into(),
31            ..self
32        }
33    }
34}
35
36impl fmt::Debug for KSymbol<'_> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.debug_struct("KSymbol")
39            .field("addr", &crate::util::fmt::HexAddr(self.addr))
40            .field("len", &self.len)
41            .field("ksym_type", &self.ksym_type)
42            .field("flags", &self.flags)
43            .field("name", &crate::util::fmt::ByteStr(&self.name))
44            .finish()
45    }
46}
47
48c_enum! {
49    /// The type of the kernel symbol.
50    #[derive(Copy, Clone, Eq, PartialEq, Hash)]
51    pub enum KSymbolType : u16 {
52        /// The symbol is of an unknown type.
53        UNKNOWN = bindings::PERF_RECORD_KSYMBOL_TYPE_UNKNOWN as _,
54
55        /// The symbol is a BPF function.
56        BPF = bindings::PERF_RECORD_KSYMBOL_TYPE_BPF as _,
57
58        /// The symbol is out-of-line code.
59        ///
60        /// See the [kernel source][src] for examples of when this could occur.
61        ///
62        /// [src]: https://sourcegraph.com/github.com/torvalds/linux@d4d58949a6eac1c45ab022562c8494725e1ac094/-/blob/tools/include/uapi/linux/perf_event.h?L1220
63        OOL = bindings::PERF_RECORD_KSYMBOL_TYPE_OOL as _,
64    }
65}
66
67impl KSymbolType {
68    /// Create a new `KSymbolType`.
69    pub const fn new(value: u16) -> Self {
70        Self(value)
71    }
72}
73
74bitflags! {
75    /// Flags for [`KSymbol`].
76    #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
77    pub struct KSymbolFlags : u16 {
78        /// If set, this means that the symbol is being unregistered.
79        const UNREGISTER = bindings::PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER as _;
80    }
81}
82
83impl<'p> Parse<'p> for KSymbolType {
84    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
85    where
86        E: Endian,
87        B: ParseBuf<'p>,
88    {
89        Ok(Self::new(p.parse()?))
90    }
91}
92
93impl<'p> Parse<'p> for KSymbolFlags {
94    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
95    where
96        E: Endian,
97        B: ParseBuf<'p>,
98    {
99        Ok(Self::from_bits_retain(p.parse()?))
100    }
101}
102
103impl<'p> Parse<'p> for KSymbol<'p> {
104    fn parse<B, E>(p: &mut Parser<B, E>) -> ParseResult<Self>
105    where
106        E: Endian,
107        B: ParseBuf<'p>,
108    {
109        Ok(Self {
110            addr: p.parse()?,
111            len: p.parse()?,
112            ksym_type: p.parse()?,
113            flags: p.parse()?,
114            name: p.parse_rest_trim_nul()?,
115        })
116    }
117}