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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::consts::*;
use crate::types::*;
use byteorder::{ByteOrder, ReadBytesExt};
use std::io;
use std::io::Read;
#[derive(Debug, Clone, Copy)]
pub struct PerfEventHeader {
pub type_: u32,
pub misc: u16,
pub size: u16,
}
impl PerfEventHeader {
pub const STRUCT_SIZE: usize = 4 + 2 + 2;
pub fn parse<R: Read, T: ByteOrder>(mut reader: R) -> Result<Self, std::io::Error> {
let type_ = reader.read_u32::<T>()?;
let misc = reader.read_u16::<T>()?;
let size = reader.read_u16::<T>()?;
Ok(Self { type_, misc, size })
}
}
#[derive(Debug, Clone, Copy)]
pub struct PerfEventAttr {
pub type_: u32,
pub size: u32,
pub config: u64,
pub sampling_period_or_frequency: u64,
pub sample_format: SampleFormat,
pub read_format: ReadFormat,
pub flags: AttrFlags,
pub wakeup_events_or_watermark: u32,
pub bp_type: HwBreakpointType,
pub bp_addr_or_kprobe_func_or_uprobe_func_or_config1: u64,
pub bp_len_or_kprobe_addr_or_probe_offset_or_config2: u64,
pub branch_sample_format: BranchSampleFormat,
pub sample_regs_user: u64,
pub sample_stack_user: u32,
pub clockid: ClockId,
pub sample_regs_intr: u64,
pub aux_watermark: u32,
pub sample_max_stack: u16,
pub aux_sample_size: u32,
pub sig_data: u64,
}
impl PerfEventAttr {
pub fn parse<R: Read, T: ByteOrder>(
mut reader: R,
size: Option<u32>,
) -> Result<Self, std::io::Error> {
let type_ = reader.read_u32::<T>()?;
let self_described_size = reader.read_u32::<T>()?;
let config = reader.read_u64::<T>()?;
let size = size.unwrap_or(self_described_size);
if size < PERF_ATTR_SIZE_VER0 {
return Err(io::ErrorKind::InvalidInput.into());
}
let sampling_period_or_frequency = reader.read_u64::<T>()?;
let sample_type = reader.read_u64::<T>()?;
let read_format = reader.read_u64::<T>()?;
let flags = reader.read_u64::<T>()?;
let wakeup_events_or_watermark = reader.read_u32::<T>()?;
let bp_type = reader.read_u32::<T>()?;
let bp_addr_or_kprobe_func_or_uprobe_func_or_config1 = reader.read_u64::<T>()?;
let bp_len_or_kprobe_addr_or_probe_offset_or_config2 = if size >= PERF_ATTR_SIZE_VER1 {
reader.read_u64::<T>()?
} else {
0
};
let branch_sample_type = if size >= PERF_ATTR_SIZE_VER2 {
reader.read_u64::<T>()?
} else {
0
};
let (sample_regs_user, sample_stack_user, clockid) = if size >= PERF_ATTR_SIZE_VER3 {
let sample_regs_user = reader.read_u64::<T>()?;
let sample_stack_user = reader.read_u32::<T>()?;
let clockid = reader.read_u32::<T>()?;
(sample_regs_user, sample_stack_user, clockid)
} else {
(0, 0, 0)
};
let sample_regs_intr = if size >= PERF_ATTR_SIZE_VER4 {
reader.read_u64::<T>()?
} else {
0
};
let (aux_watermark, sample_max_stack) = if size >= PERF_ATTR_SIZE_VER5 {
let aux_watermark = reader.read_u32::<T>()?;
let sample_max_stack = reader.read_u16::<T>()?;
let __reserved_2 = reader.read_u16::<T>()?;
(aux_watermark, sample_max_stack)
} else {
(0, 0)
};
let aux_sample_size = if size >= PERF_ATTR_SIZE_VER6 {
let aux_sample_size = reader.read_u32::<T>()?;
let __reserved_3 = reader.read_u32::<T>()?;
aux_sample_size
} else {
0
};
let sig_data = if size >= PERF_ATTR_SIZE_VER7 {
reader.read_u64::<T>()?
} else {
0
};
if size > PERF_ATTR_SIZE_VER7 {
let remaining = size - PERF_ATTR_SIZE_VER7;
io::copy(&mut reader.by_ref().take(remaining.into()), &mut io::sink())?;
}
Ok(Self {
type_,
size,
config,
sampling_period_or_frequency,
sample_format: SampleFormat::from_bits_truncate(sample_type),
read_format: ReadFormat::from_bits_truncate(read_format),
flags: AttrFlags::from_bits_truncate(flags),
wakeup_events_or_watermark,
bp_type: HwBreakpointType::from_bits_truncate(bp_type),
bp_addr_or_kprobe_func_or_uprobe_func_or_config1,
bp_len_or_kprobe_addr_or_probe_offset_or_config2,
branch_sample_format: BranchSampleFormat::from_bits_truncate(branch_sample_type),
sample_regs_user,
sample_stack_user,
clockid: ClockId::from_u32(clockid).ok_or(io::ErrorKind::InvalidInput)?,
sample_regs_intr,
aux_watermark,
sample_max_stack,
aux_sample_size,
sig_data,
})
}
}