1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "record.h"
18
19 #include <inttypes.h>
20 #include <algorithm>
21 #include <unordered_map>
22
23 #include <android-base/logging.h>
24 #include <android-base/macros.h>
25 #include <android-base/stringprintf.h>
26
27 #include "OfflineUnwinder.h"
28 #include "dso.h"
29 #include "perf_regs.h"
30 #include "tracing.h"
31 #include "utils.h"
32
33 namespace simpleperf {
34
35 #define CHECK_SIZE(p, end, size) \
36 do { \
37 if (UNLIKELY((end) - (p) < (size))) { \
38 return false; \
39 } \
40 } while (0)
41
42 #define CHECK_SIZE_U64(p, end, u64_count) \
43 do { \
44 if (UNLIKELY(((end) - (p)) / sizeof(uint64_t) < (u64_count))) { \
45 return false; \
46 } \
47 } while (0)
48
RecordTypeToString(int record_type)49 static std::string RecordTypeToString(int record_type) {
50 static std::unordered_map<int, std::string> record_type_names = {
51 {PERF_RECORD_MMAP, "mmap"},
52 {PERF_RECORD_LOST, "lost"},
53 {PERF_RECORD_COMM, "comm"},
54 {PERF_RECORD_EXIT, "exit"},
55 {PERF_RECORD_THROTTLE, "throttle"},
56 {PERF_RECORD_UNTHROTTLE, "unthrottle"},
57 {PERF_RECORD_FORK, "fork"},
58 {PERF_RECORD_READ, "read"},
59 {PERF_RECORD_SAMPLE, "sample"},
60 {PERF_RECORD_BUILD_ID, "build_id"},
61 {PERF_RECORD_MMAP2, "mmap2"},
62 {PERF_RECORD_AUX, "aux"},
63 {PERF_RECORD_SWITCH, "switch"},
64 {PERF_RECORD_SWITCH_CPU_WIDE, "switch_cpu_wide"},
65 {PERF_RECORD_TRACING_DATA, "tracing_data"},
66 {PERF_RECORD_AUXTRACE_INFO, "auxtrace_info"},
67 {PERF_RECORD_AUXTRACE, "auxtrace"},
68 {SIMPLE_PERF_RECORD_KERNEL_SYMBOL, "kernel_symbol"},
69 {SIMPLE_PERF_RECORD_DSO, "dso"},
70 {SIMPLE_PERF_RECORD_SYMBOL, "symbol"},
71 {SIMPLE_PERF_RECORD_EVENT_ID, "event_id"},
72 {SIMPLE_PERF_RECORD_CALLCHAIN, "callchain"},
73 {SIMPLE_PERF_RECORD_UNWINDING_RESULT, "unwinding_result"},
74 {SIMPLE_PERF_RECORD_TRACING_DATA, "tracing_data"},
75 };
76
77 auto it = record_type_names.find(record_type);
78 if (it != record_type_names.end()) {
79 return it->second;
80 }
81 return android::base::StringPrintf("unknown(%d)", record_type);
82 }
83
84 template <>
MoveToBinaryFormat(const RecordHeader & data,char * & p)85 void MoveToBinaryFormat(const RecordHeader& data, char*& p) {
86 data.MoveToBinaryFormat(p);
87 }
88
SampleId()89 SampleId::SampleId() {
90 memset(this, 0, sizeof(SampleId));
91 }
92
93 // Return sample_id size in binary format.
CreateContent(const perf_event_attr & attr,uint64_t event_id)94 size_t SampleId::CreateContent(const perf_event_attr& attr, uint64_t event_id) {
95 sample_id_all = attr.sample_id_all;
96 sample_type = attr.sample_type;
97 id_data.id = event_id;
98 // Other data are not necessary. TODO: Set missing SampleId data.
99 return Size();
100 }
101
ReadFromBinaryFormat(const perf_event_attr & attr,const char * p,const char * end)102 bool SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
103 sample_id_all = attr.sample_id_all;
104 sample_type = attr.sample_type;
105 if (sample_id_all) {
106 const uint64_t sample_id_mask = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ID |
107 PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU |
108 PERF_SAMPLE_IDENTIFIER;
109 CHECK_SIZE_U64(p, end, __builtin_popcountll(sample_type & sample_id_mask));
110 if (sample_type & PERF_SAMPLE_TID) {
111 MoveFromBinaryFormat(tid_data, p);
112 }
113 if (sample_type & PERF_SAMPLE_TIME) {
114 MoveFromBinaryFormat(time_data, p);
115 }
116 if (sample_type & PERF_SAMPLE_ID) {
117 MoveFromBinaryFormat(id_data, p);
118 }
119 if (sample_type & PERF_SAMPLE_STREAM_ID) {
120 MoveFromBinaryFormat(stream_id_data, p);
121 }
122 if (sample_type & PERF_SAMPLE_CPU) {
123 MoveFromBinaryFormat(cpu_data, p);
124 }
125 if (sample_type & PERF_SAMPLE_IDENTIFIER) {
126 MoveFromBinaryFormat(id_data, p);
127 }
128 }
129 if (UNLIKELY(p < end)) {
130 LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
131 }
132 return true;
133 }
134
WriteToBinaryFormat(char * & p) const135 void SampleId::WriteToBinaryFormat(char*& p) const {
136 if (sample_id_all) {
137 if (sample_type & PERF_SAMPLE_TID) {
138 MoveToBinaryFormat(tid_data, p);
139 }
140 if (sample_type & PERF_SAMPLE_TIME) {
141 MoveToBinaryFormat(time_data, p);
142 }
143 if (sample_type & PERF_SAMPLE_ID) {
144 MoveToBinaryFormat(id_data, p);
145 }
146 if (sample_type & PERF_SAMPLE_STREAM_ID) {
147 MoveToBinaryFormat(stream_id_data, p);
148 }
149 if (sample_type & PERF_SAMPLE_CPU) {
150 MoveToBinaryFormat(cpu_data, p);
151 }
152 }
153 }
154
Dump(size_t indent) const155 void SampleId::Dump(size_t indent) const {
156 if (sample_id_all) {
157 if (sample_type & PERF_SAMPLE_TID) {
158 PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
159 }
160 if (sample_type & PERF_SAMPLE_TIME) {
161 PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
162 }
163 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) {
164 PrintIndented(indent, "sample_id: id %" PRId64 "\n", id_data.id);
165 }
166 if (sample_type & PERF_SAMPLE_STREAM_ID) {
167 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
168 }
169 if (sample_type & PERF_SAMPLE_CPU) {
170 PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
171 }
172 }
173 }
174
Size() const175 size_t SampleId::Size() const {
176 size_t size = 0;
177 if (sample_id_all) {
178 if (sample_type & PERF_SAMPLE_TID) {
179 size += sizeof(PerfSampleTidType);
180 }
181 if (sample_type & PERF_SAMPLE_TIME) {
182 size += sizeof(PerfSampleTimeType);
183 }
184 if (sample_type & PERF_SAMPLE_ID) {
185 size += sizeof(PerfSampleIdType);
186 }
187 if (sample_type & PERF_SAMPLE_STREAM_ID) {
188 size += sizeof(PerfSampleStreamIdType);
189 }
190 if (sample_type & PERF_SAMPLE_CPU) {
191 size += sizeof(PerfSampleCpuType);
192 }
193 if (sample_type & PERF_SAMPLE_IDENTIFIER) {
194 size += sizeof(PerfSampleIdType);
195 }
196 }
197 return size;
198 }
199
Record(Record && other)200 Record::Record(Record&& other) noexcept {
201 header = other.header;
202 sample_id = other.sample_id;
203 binary_ = other.binary_;
204 own_binary_ = other.own_binary_;
205 other.binary_ = nullptr;
206 other.own_binary_ = false;
207 }
208
ParseHeader(char * & p,char * & end)209 bool Record::ParseHeader(char*& p, char*& end) {
210 binary_ = p;
211 CHECK(end != nullptr);
212 CHECK_SIZE(p, end, sizeof(perf_event_header));
213 if (!header.Parse(p)) {
214 return false;
215 }
216 CHECK_SIZE(p, end, header.size);
217 end = p + header.size;
218 p += sizeof(perf_event_header);
219 return true;
220 }
221
Dump(size_t indent) const222 void Record::Dump(size_t indent) const {
223 PrintIndented(indent, "record %s: type %u, misc 0x%x, size %u\n",
224 RecordTypeToString(type()).c_str(), type(), misc(), size());
225 DumpData(indent + 1);
226 sample_id.Dump(indent + 1);
227 }
228
Timestamp() const229 uint64_t Record::Timestamp() const {
230 return sample_id.time_data.time;
231 }
Cpu() const232 uint32_t Record::Cpu() const {
233 return sample_id.cpu_data.cpu;
234 }
Id() const235 uint64_t Record::Id() const {
236 return sample_id.id_data.id;
237 }
238
UpdateBinary(char * new_binary)239 void Record::UpdateBinary(char* new_binary) {
240 if (own_binary_) {
241 delete[] binary_;
242 }
243 own_binary_ = true;
244 binary_ = new_binary;
245 }
246
Parse(const perf_event_attr & attr,char * p,char * end)247 bool MmapRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
248 if (!ParseHeader(p, end)) {
249 return false;
250 }
251 data = reinterpret_cast<const MmapRecordDataType*>(p);
252 CHECK_SIZE(p, end, sizeof(*data));
253 p += sizeof(*data);
254 size_t size = Align(SafeStrlen(p, end) + 1, 8);
255 CHECK_SIZE(p, end, size);
256 filename = p;
257 p += size;
258 return sample_id.ReadFromBinaryFormat(attr, p, end);
259 }
260
MmapRecord(const perf_event_attr & attr,bool in_kernel,uint32_t pid,uint32_t tid,uint64_t addr,uint64_t len,uint64_t pgoff,const std::string & filename,uint64_t event_id,uint64_t time)261 MmapRecord::MmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
262 uint64_t addr, uint64_t len, uint64_t pgoff, const std::string& filename,
263 uint64_t event_id, uint64_t time) {
264 SetTypeAndMisc(PERF_RECORD_MMAP, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
265 sample_id.CreateContent(attr, event_id);
266 sample_id.time_data.time = time;
267 MmapRecordDataType data;
268 data.pid = pid;
269 data.tid = tid;
270 data.addr = addr;
271 data.len = len;
272 data.pgoff = pgoff;
273 SetDataAndFilename(data, filename);
274 }
275
SetDataAndFilename(const MmapRecordDataType & data,const std::string & filename)276 void MmapRecord::SetDataAndFilename(const MmapRecordDataType& data, const std::string& filename) {
277 SetSize(header_size() + sizeof(data) + Align(filename.size() + 1, 8) + sample_id.Size());
278 char* new_binary = new char[size()];
279 char* p = new_binary;
280 MoveToBinaryFormat(header, p);
281 this->data = reinterpret_cast<MmapRecordDataType*>(p);
282 MoveToBinaryFormat(data, p);
283 this->filename = p;
284 strcpy(p, filename.c_str());
285 p += Align(filename.size() + 1, 8);
286 sample_id.WriteToBinaryFormat(p);
287 UpdateBinary(new_binary);
288 }
289
DumpData(size_t indent) const290 void MmapRecord::DumpData(size_t indent) const {
291 PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data->pid,
292 data->tid, data->addr, data->len);
293 PrintIndented(indent, "pgoff 0x%" PRIx64 ", filename %s\n", data->pgoff, filename);
294 }
295
Parse(const perf_event_attr & attr,char * p,char * end)296 bool Mmap2Record::Parse(const perf_event_attr& attr, char* p, char* end) {
297 if (!ParseHeader(p, end)) {
298 return false;
299 }
300 data = reinterpret_cast<const Mmap2RecordDataType*>(p);
301 CHECK_SIZE(p, end, sizeof(*data));
302 p += sizeof(*data);
303 size_t size = Align(SafeStrlen(p, end) + 1, 8);
304 CHECK_SIZE(p, end, size);
305 filename = p;
306 p += size;
307 return sample_id.ReadFromBinaryFormat(attr, p, end);
308 }
309
Mmap2Record(const perf_event_attr & attr,bool in_kernel,uint32_t pid,uint32_t tid,uint64_t addr,uint64_t len,uint64_t pgoff,uint32_t prot,const std::string & filename,uint64_t event_id,uint64_t time)310 Mmap2Record::Mmap2Record(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
311 uint64_t addr, uint64_t len, uint64_t pgoff, uint32_t prot,
312 const std::string& filename, uint64_t event_id, uint64_t time) {
313 SetTypeAndMisc(PERF_RECORD_MMAP2, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
314 sample_id.CreateContent(attr, event_id);
315 sample_id.time_data.time = time;
316 Mmap2RecordDataType data;
317 data.pid = pid;
318 data.tid = tid;
319 data.addr = addr;
320 data.len = len;
321 data.pgoff = pgoff;
322 data.prot = prot;
323 SetDataAndFilename(data, filename);
324 }
325
SetDataAndFilename(const Mmap2RecordDataType & data,const std::string & filename)326 void Mmap2Record::SetDataAndFilename(const Mmap2RecordDataType& data, const std::string& filename) {
327 SetSize(header_size() + sizeof(data) + Align(filename.size() + 1, 8) + sample_id.Size());
328 char* new_binary = new char[size()];
329 char* p = new_binary;
330 MoveToBinaryFormat(header, p);
331 this->data = reinterpret_cast<Mmap2RecordDataType*>(p);
332 MoveToBinaryFormat(data, p);
333 this->filename = p;
334 strcpy(p, filename.c_str());
335 p += Align(filename.size() + 1, 8);
336 sample_id.WriteToBinaryFormat(p);
337 UpdateBinary(new_binary);
338 }
339
DumpData(size_t indent) const340 void Mmap2Record::DumpData(size_t indent) const {
341 PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data->pid,
342 data->tid, data->addr, data->len);
343 PrintIndented(
344 indent, "pgoff 0x%" PRIx64 ", maj %u, min %u, ino %" PRId64 ", ino_generation %" PRIu64 "\n",
345 data->pgoff, data->maj, data->min, data->ino, data->ino_generation);
346 PrintIndented(indent, "prot %u, flags %u, filename %s\n", data->prot, data->flags, filename);
347 }
348
Parse(const perf_event_attr & attr,char * p,char * end)349 bool CommRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
350 if (!ParseHeader(p, end)) {
351 return false;
352 }
353 data = reinterpret_cast<const CommRecordDataType*>(p);
354 CHECK_SIZE(p, end, sizeof(*data));
355 p += sizeof(*data);
356 size_t size = Align(SafeStrlen(p, end) + 1, 8);
357 CHECK_SIZE(p, end, size);
358 comm = p;
359 p += size;
360 return sample_id.ReadFromBinaryFormat(attr, p, end);
361 }
362
CommRecord(const perf_event_attr & attr,uint32_t pid,uint32_t tid,const std::string & comm,uint64_t event_id,uint64_t time)363 CommRecord::CommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
364 const std::string& comm, uint64_t event_id, uint64_t time) {
365 SetTypeAndMisc(PERF_RECORD_COMM, 0);
366 CommRecordDataType data;
367 data.pid = pid;
368 data.tid = tid;
369 size_t sample_id_size = sample_id.CreateContent(attr, event_id);
370 sample_id.time_data.time = time;
371 SetSize(header_size() + sizeof(data) + Align(comm.size() + 1, 8) + sample_id_size);
372 char* new_binary = new char[size()];
373 char* p = new_binary;
374 MoveToBinaryFormat(header, p);
375 this->data = reinterpret_cast<CommRecordDataType*>(p);
376 MoveToBinaryFormat(data, p);
377 this->comm = p;
378 strcpy(p, comm.c_str());
379 p += Align(comm.size() + 1, 8);
380 sample_id.WriteToBinaryFormat(p);
381 UpdateBinary(new_binary);
382 }
383
SetCommandName(const std::string & name)384 void CommRecord::SetCommandName(const std::string& name) {
385 if (name.compare(comm) == 0) {
386 return;
387 }
388 // The kernel uses a 8-byte aligned space to store command name. Follow it here to allow the same
389 // reading code.
390 size_t old_name_len = Align(strlen(comm) + 1, 8);
391 size_t new_name_len = Align(name.size() + 1, 8);
392 size_t new_size = size() - old_name_len + new_name_len;
393 char* new_binary = new char[new_size];
394 char* p = new_binary;
395 header.size = new_size;
396 MoveToBinaryFormat(header, p);
397 MoveToBinaryFormat(*data, p);
398 data = reinterpret_cast<CommRecordDataType*>(p - sizeof(CommRecordDataType));
399 comm = p;
400 strcpy(p, name.c_str());
401 p += new_name_len;
402 sample_id.WriteToBinaryFormat(p);
403 CHECK_EQ(p, new_binary + new_size);
404 UpdateBinary(new_binary);
405 }
406
DumpData(size_t indent) const407 void CommRecord::DumpData(size_t indent) const {
408 PrintIndented(indent, "pid %u, tid %u, comm %s\n", data->pid, data->tid, comm);
409 }
410
Parse(const perf_event_attr & attr,char * p,char * end)411 bool ExitOrForkRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
412 if (!ParseHeader(p, end)) {
413 return false;
414 }
415 data = reinterpret_cast<const ExitOrForkRecordDataType*>(p);
416 CHECK_SIZE(p, end, sizeof(*data));
417 p += sizeof(*data);
418 return sample_id.ReadFromBinaryFormat(attr, p, end);
419 }
420
DumpData(size_t indent) const421 void ExitOrForkRecord::DumpData(size_t indent) const {
422 PrintIndented(indent, "pid %u, ppid %u, tid %u, ptid %u\n", data->pid, data->ppid, data->tid,
423 data->ptid);
424 }
425
ForkRecord(const perf_event_attr & attr,uint32_t pid,uint32_t tid,uint32_t ppid,uint32_t ptid,uint64_t event_id)426 ForkRecord::ForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
427 uint32_t ptid, uint64_t event_id) {
428 SetTypeAndMisc(PERF_RECORD_FORK, 0);
429 ExitOrForkRecordDataType data;
430 data.pid = pid;
431 data.ppid = ppid;
432 data.tid = tid;
433 data.ptid = ptid;
434 data.time = 0;
435 size_t sample_id_size = sample_id.CreateContent(attr, event_id);
436 SetSize(header_size() + sizeof(data) + sample_id_size);
437 char* new_binary = new char[size()];
438 char* p = new_binary;
439 MoveToBinaryFormat(header, p);
440 this->data = reinterpret_cast<ExitOrForkRecordDataType*>(p);
441 MoveToBinaryFormat(data, p);
442 sample_id.WriteToBinaryFormat(p);
443 UpdateBinary(new_binary);
444 }
445
Parse(const perf_event_attr & attr,char * p,char * end)446 bool LostRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
447 if (!ParseHeader(p, end)) {
448 return false;
449 }
450 CHECK_SIZE_U64(p, end, 2);
451 MoveFromBinaryFormat(id, p);
452 MoveFromBinaryFormat(lost, p);
453 return sample_id.ReadFromBinaryFormat(attr, p, end);
454 }
455
DumpData(size_t indent) const456 void LostRecord::DumpData(size_t indent) const {
457 PrintIndented(indent, "id %" PRIu64 ", lost %" PRIu64 "\n", id, lost);
458 }
459
Parse(const perf_event_attr & attr,char * p,char * end)460 bool SampleRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
461 if (!ParseHeader(p, end)) {
462 return false;
463 }
464 sample_type = attr.sample_type;
465 read_format = attr.read_format;
466 const uint64_t sample_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | PERF_SAMPLE_TID |
467 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | PERF_SAMPLE_ID |
468 PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD;
469 CHECK_SIZE_U64(p, end, __builtin_popcountll(sample_type & sample_mask));
470
471 // Set a default id value to report correctly even if ID is not recorded.
472 id_data.id = 0;
473 if (sample_type & PERF_SAMPLE_IDENTIFIER) {
474 MoveFromBinaryFormat(id_data, p);
475 }
476 if (sample_type & PERF_SAMPLE_IP) {
477 MoveFromBinaryFormat(ip_data, p);
478 }
479 if (sample_type & PERF_SAMPLE_TID) {
480 MoveFromBinaryFormat(tid_data, p);
481 }
482 if (sample_type & PERF_SAMPLE_TIME) {
483 MoveFromBinaryFormat(time_data, p);
484 }
485 if (sample_type & PERF_SAMPLE_ADDR) {
486 MoveFromBinaryFormat(addr_data, p);
487 }
488 if (sample_type & PERF_SAMPLE_ID) {
489 MoveFromBinaryFormat(id_data, p);
490 }
491 if (sample_type & PERF_SAMPLE_STREAM_ID) {
492 MoveFromBinaryFormat(stream_id_data, p);
493 }
494 if (sample_type & PERF_SAMPLE_CPU) {
495 MoveFromBinaryFormat(cpu_data, p);
496 }
497 if (sample_type & PERF_SAMPLE_PERIOD) {
498 MoveFromBinaryFormat(period_data, p);
499 }
500 if (sample_type & PERF_SAMPLE_READ) {
501 uint64_t nr = 1;
502 if (read_format & PERF_FORMAT_GROUP) {
503 CHECK_SIZE_U64(p, end, 1);
504 MoveFromBinaryFormat(nr, p);
505 }
506 uint64_t u64_count = (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) ? 1 : 0;
507 u64_count += (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) ? 1 : 0;
508 if (__builtin_add_overflow(u64_count, nr, &u64_count)) {
509 return false;
510 }
511 if (read_format & PERF_FORMAT_ID) {
512 if (__builtin_add_overflow(u64_count, nr, &u64_count)) {
513 return false;
514 }
515 }
516 CHECK_SIZE_U64(p, end, u64_count);
517 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
518 MoveFromBinaryFormat(read_data.time_enabled, p);
519 }
520 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
521 MoveFromBinaryFormat(read_data.time_running, p);
522 }
523 read_data.counts.resize(nr);
524 if (read_format & PERF_FORMAT_ID) {
525 read_data.ids.resize(nr);
526 }
527 for (uint64_t i = 0; i < nr; i++) {
528 MoveFromBinaryFormat(read_data.counts[i], p);
529 if (read_format & PERF_FORMAT_ID) {
530 MoveFromBinaryFormat(read_data.ids[i], p);
531 }
532 }
533 }
534 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
535 CHECK_SIZE_U64(p, end, 1);
536 MoveFromBinaryFormat(callchain_data.ip_nr, p);
537 CHECK_SIZE_U64(p, end, callchain_data.ip_nr);
538 callchain_data.ips = reinterpret_cast<uint64_t*>(p);
539 p += callchain_data.ip_nr * sizeof(uint64_t);
540 }
541 if (sample_type & PERF_SAMPLE_RAW) {
542 CHECK_SIZE(p, end, sizeof(uint32_t));
543 MoveFromBinaryFormat(raw_data.size, p);
544 CHECK_SIZE(p, end, raw_data.size);
545 raw_data.data = p;
546 p += raw_data.size;
547 }
548 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
549 CHECK_SIZE_U64(p, end, 1);
550 MoveFromBinaryFormat(branch_stack_data.stack_nr, p);
551 CHECK_SIZE(p, end, branch_stack_data.stack_nr * sizeof(BranchStackItemType));
552 branch_stack_data.stack = reinterpret_cast<BranchStackItemType*>(p);
553 p += branch_stack_data.stack_nr * sizeof(BranchStackItemType);
554 }
555 if (sample_type & PERF_SAMPLE_REGS_USER) {
556 CHECK_SIZE_U64(p, end, 1);
557 MoveFromBinaryFormat(regs_user_data.abi, p);
558 if (regs_user_data.abi == 0) {
559 regs_user_data.reg_mask = 0;
560 regs_user_data.reg_nr = 0;
561 regs_user_data.regs = nullptr;
562 } else {
563 regs_user_data.reg_mask = attr.sample_regs_user;
564 size_t bit_nr = __builtin_popcountll(regs_user_data.reg_mask);
565 CHECK_SIZE_U64(p, end, bit_nr);
566 regs_user_data.reg_nr = bit_nr;
567 regs_user_data.regs = reinterpret_cast<uint64_t*>(p);
568 p += bit_nr * sizeof(uint64_t);
569 }
570 }
571 if (sample_type & PERF_SAMPLE_STACK_USER) {
572 CHECK_SIZE_U64(p, end, 1);
573 MoveFromBinaryFormat(stack_user_data.size, p);
574 if (stack_user_data.size == 0) {
575 stack_user_data.dyn_size = 0;
576 } else {
577 CHECK_SIZE(p, end, stack_user_data.size + sizeof(uint64_t));
578 stack_user_data.data = p;
579 p += stack_user_data.size;
580 MoveFromBinaryFormat(stack_user_data.dyn_size, p);
581 }
582 }
583 // TODO: Add parsing of other PERF_SAMPLE_*.
584 if (UNLIKELY(p < end)) {
585 LOG(DEBUG) << "Sample (" << time_data.time << ") has " << end - p << " bytes left";
586 }
587 return true;
588 }
589
SampleRecord(const perf_event_attr & attr,uint64_t id,uint64_t ip,uint32_t pid,uint32_t tid,uint64_t time,uint32_t cpu,uint64_t period,const PerfSampleReadType & read_data,const std::vector<uint64_t> & ips,const std::vector<char> & stack,uint64_t dyn_stack_size)590 SampleRecord::SampleRecord(const perf_event_attr& attr, uint64_t id, uint64_t ip, uint32_t pid,
591 uint32_t tid, uint64_t time, uint32_t cpu, uint64_t period,
592 const PerfSampleReadType& read_data, const std::vector<uint64_t>& ips,
593 const std::vector<char>& stack, uint64_t dyn_stack_size) {
594 SetTypeAndMisc(PERF_RECORD_SAMPLE, PERF_RECORD_MISC_USER);
595 sample_type = attr.sample_type;
596 read_format = attr.read_format;
597 CHECK_EQ(0u,
598 sample_type & ~(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ID |
599 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | PERF_SAMPLE_READ |
600 PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER));
601 ip_data.ip = ip;
602 tid_data.pid = pid;
603 tid_data.tid = tid;
604 time_data.time = time;
605 id_data.id = id;
606 cpu_data.cpu = cpu;
607 cpu_data.res = 0;
608 period_data.period = period;
609 this->read_data = read_data;
610 callchain_data.ip_nr = ips.size();
611 raw_data.size = 0;
612 branch_stack_data.stack_nr = 0;
613 regs_user_data.abi = 0;
614 regs_user_data.reg_mask = 0;
615 regs_user_data.reg_nr = 0;
616 stack_user_data.size = stack.size();
617 stack_user_data.dyn_size = dyn_stack_size;
618
619 uint32_t size = header_size();
620 if (sample_type & PERF_SAMPLE_IP) {
621 size += sizeof(ip_data);
622 }
623 if (sample_type & PERF_SAMPLE_TID) {
624 size += sizeof(tid_data);
625 }
626 if (sample_type & PERF_SAMPLE_TIME) {
627 size += sizeof(time_data);
628 }
629 if (sample_type & PERF_SAMPLE_ID) {
630 size += sizeof(id_data);
631 }
632 if (sample_type & PERF_SAMPLE_CPU) {
633 size += sizeof(cpu_data);
634 }
635 if (sample_type & PERF_SAMPLE_PERIOD) {
636 size += sizeof(period_data);
637 }
638 if (sample_type & PERF_SAMPLE_READ) {
639 size_t u64_count = (read_format & PERF_FORMAT_GROUP) ? 1 : 0;
640 u64_count += (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) ? 1 : 0;
641 u64_count += (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) ? 1 : 0;
642 u64_count += read_data.counts.size() + read_data.ids.size();
643 size += sizeof(uint64_t) * u64_count;
644 }
645 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
646 size += sizeof(uint64_t) * (ips.size() + 1);
647 }
648 if (sample_type & PERF_SAMPLE_REGS_USER) {
649 size += sizeof(uint64_t);
650 }
651 if (sample_type & PERF_SAMPLE_STACK_USER) {
652 size += sizeof(uint64_t) + (stack.empty() ? 0 : stack.size() + sizeof(uint64_t));
653 }
654
655 SetSize(size);
656 char* new_binary = new char[size];
657 char* p = new_binary;
658 MoveToBinaryFormat(header, p);
659 if (sample_type & PERF_SAMPLE_IP) {
660 MoveToBinaryFormat(ip_data, p);
661 }
662 if (sample_type & PERF_SAMPLE_TID) {
663 MoveToBinaryFormat(tid_data, p);
664 }
665 if (sample_type & PERF_SAMPLE_TIME) {
666 MoveToBinaryFormat(time_data, p);
667 }
668 if (sample_type & PERF_SAMPLE_ID) {
669 MoveToBinaryFormat(id_data, p);
670 }
671 if (sample_type & PERF_SAMPLE_CPU) {
672 MoveToBinaryFormat(cpu_data, p);
673 }
674 if (sample_type & PERF_SAMPLE_PERIOD) {
675 MoveToBinaryFormat(period_data, p);
676 }
677 if (sample_type & PERF_SAMPLE_READ) {
678 if (read_format & PERF_FORMAT_GROUP) {
679 uint64_t nr = read_data.counts.size();
680 MoveToBinaryFormat(nr, p);
681 }
682 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
683 MoveToBinaryFormat(read_data.time_enabled, p);
684 }
685 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
686 MoveToBinaryFormat(read_data.time_running, p);
687 }
688 for (size_t i = 0; i < read_data.counts.size(); i++) {
689 MoveToBinaryFormat(read_data.counts[i], p);
690 if (read_format & PERF_FORMAT_ID) {
691 MoveToBinaryFormat(read_data.ids[i], p);
692 }
693 }
694 }
695 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
696 MoveToBinaryFormat(callchain_data.ip_nr, p);
697 callchain_data.ips = reinterpret_cast<uint64_t*>(p);
698 MoveToBinaryFormat(ips.data(), ips.size(), p);
699 }
700 if (sample_type & PERF_SAMPLE_REGS_USER) {
701 MoveToBinaryFormat(regs_user_data.abi, p);
702 }
703 if (sample_type & PERF_SAMPLE_STACK_USER) {
704 MoveToBinaryFormat(stack_user_data.size, p);
705 if (stack_user_data.size > 0) {
706 stack_user_data.data = p;
707 MoveToBinaryFormat(stack.data(), stack_user_data.size, p);
708 MoveToBinaryFormat(stack_user_data.dyn_size, p);
709 }
710 }
711 CHECK_EQ(p, new_binary + size);
712 UpdateBinary(new_binary);
713 }
714
ReplaceRegAndStackWithCallChain(const std::vector<uint64_t> & ips)715 void SampleRecord::ReplaceRegAndStackWithCallChain(const std::vector<uint64_t>& ips) {
716 uint32_t add_size_in_callchain = ips.empty() ? 0 : sizeof(uint64_t) * (ips.size() + 1);
717 uint32_t reduce_size_in_reg = (regs_user_data.reg_nr + 1) * sizeof(uint64_t);
718 uint32_t reduce_size_in_stack =
719 stack_user_data.size == 0 ? sizeof(uint64_t) : (stack_user_data.size + 2 * sizeof(uint64_t));
720 uint32_t reduce_size = reduce_size_in_reg + reduce_size_in_stack;
721
722 uint32_t new_size = size() + add_size_in_callchain;
723 CHECK_GT(new_size, reduce_size);
724 new_size -= reduce_size;
725 sample_type &= ~(PERF_SAMPLE_STACK_USER | PERF_SAMPLE_REGS_USER);
726 BuildBinaryWithNewCallChain(new_size, ips);
727 }
728
ExcludeKernelCallChain()729 bool SampleRecord::ExcludeKernelCallChain() {
730 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
731 return true;
732 }
733 size_t i;
734 for (i = 0; i < callchain_data.ip_nr; ++i) {
735 if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
736 break;
737 }
738 // Erase kernel callchain.
739 callchain_data.ips[i] = PERF_CONTEXT_USER;
740 }
741 while (++i < callchain_data.ip_nr) {
742 if (callchain_data.ips[i] < PERF_CONTEXT_MAX) {
743 // Change the sample to make it hit the user space ip address.
744 ip_data.ip = callchain_data.ips[i];
745 if (sample_type & PERF_SAMPLE_IP) {
746 *reinterpret_cast<uint64_t*>(binary_ + header_size()) = ip_data.ip;
747 }
748 header.misc = (header.misc & ~PERF_RECORD_MISC_CPUMODE_MASK) | PERF_RECORD_MISC_USER;
749 reinterpret_cast<perf_event_header*>(binary_)->misc = header.misc;
750 return true;
751 }
752 }
753 return false;
754 }
755
HasUserCallChain() const756 bool SampleRecord::HasUserCallChain() const {
757 if ((sample_type & PERF_SAMPLE_CALLCHAIN) == 0) {
758 return false;
759 }
760 bool in_user_context = !InKernel();
761 for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
762 if (in_user_context && callchain_data.ips[i] < PERF_CONTEXT_MAX) {
763 return true;
764 }
765 if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
766 in_user_context = true;
767 }
768 }
769 return false;
770 }
771
UpdateUserCallChain(const std::vector<uint64_t> & user_ips)772 void SampleRecord::UpdateUserCallChain(const std::vector<uint64_t>& user_ips) {
773 size_t kernel_ip_count = 0;
774 for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
775 if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
776 break;
777 }
778 kernel_ip_count++;
779 }
780 if (kernel_ip_count + 1 + user_ips.size() <= callchain_data.ip_nr) {
781 // Callchain isn't changed.
782 return;
783 }
784 size_t new_size =
785 size() + (kernel_ip_count + 1 + user_ips.size() - callchain_data.ip_nr) * sizeof(uint64_t);
786 callchain_data.ip_nr = kernel_ip_count;
787 BuildBinaryWithNewCallChain(new_size, user_ips);
788 }
789
BuildBinaryWithNewCallChain(uint32_t new_size,const std::vector<uint64_t> & ips)790 void SampleRecord::BuildBinaryWithNewCallChain(uint32_t new_size,
791 const std::vector<uint64_t>& ips) {
792 size_t callchain_pos = reinterpret_cast<char*>(callchain_data.ips) - binary_ - sizeof(uint64_t);
793 char* new_binary = binary_;
794 if (new_size > size()) {
795 new_binary = new char[new_size];
796 memcpy(new_binary, binary_, callchain_pos);
797 }
798 char* p = new_binary;
799 SetSize(new_size);
800 MoveToBinaryFormat(header, p);
801 p = new_binary + new_size;
802 if (sample_type & PERF_SAMPLE_STACK_USER) {
803 stack_user_data.size = 0;
804 p -= sizeof(uint64_t);
805 memcpy(p, &stack_user_data.size, sizeof(uint64_t));
806 }
807 if (sample_type & PERF_SAMPLE_REGS_USER) {
808 regs_user_data.abi = 0;
809 p -= sizeof(uint64_t);
810 memcpy(p, ®s_user_data.abi, sizeof(uint64_t));
811 }
812 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
813 p -= branch_stack_data.stack_nr * sizeof(BranchStackItemType);
814 memcpy(p, branch_stack_data.stack, branch_stack_data.stack_nr * sizeof(BranchStackItemType));
815 branch_stack_data.stack = reinterpret_cast<BranchStackItemType*>(p);
816 p -= sizeof(uint64_t);
817 memcpy(p, &branch_stack_data.stack_nr, sizeof(uint64_t));
818 }
819 if (sample_type & PERF_SAMPLE_RAW) {
820 p -= raw_data.size;
821 memcpy(p, raw_data.data, raw_data.size);
822 raw_data.data = p;
823 p -= sizeof(uint32_t);
824 memcpy(p, &raw_data.size, sizeof(uint32_t));
825 }
826 uint64_t* p64 = reinterpret_cast<uint64_t*>(p);
827 if (!ips.empty()) {
828 p64 -= ips.size();
829 memcpy(p64, ips.data(), ips.size() * sizeof(uint64_t));
830 *--p64 = PERF_CONTEXT_USER;
831 }
832 p64 -= callchain_data.ip_nr;
833 if (p64 != callchain_data.ips) {
834 memcpy(p64, callchain_data.ips, callchain_data.ip_nr * sizeof(uint64_t));
835 callchain_data.ips = p64;
836 }
837 p64--;
838 if (!ips.empty()) {
839 callchain_data.ip_nr += 1 + ips.size();
840 *p64 = callchain_data.ip_nr;
841 }
842 CHECK_EQ(callchain_pos, static_cast<size_t>(reinterpret_cast<char*>(p64) - new_binary))
843 << "record time " << time_data.time;
844 if (new_binary != binary_) {
845 UpdateBinary(new_binary);
846 }
847 }
848
DumpData(size_t indent) const849 void SampleRecord::DumpData(size_t indent) const {
850 PrintIndented(indent, "sample_type: 0x%" PRIx64 "\n", sample_type);
851 if (sample_type & PERF_SAMPLE_IP) {
852 PrintIndented(indent, "ip %p\n", reinterpret_cast<void*>(ip_data.ip));
853 }
854 if (sample_type & PERF_SAMPLE_TID) {
855 PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
856 }
857 if (sample_type & PERF_SAMPLE_TIME) {
858 PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
859 }
860 if (sample_type & PERF_SAMPLE_ADDR) {
861 PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
862 }
863 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) {
864 PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
865 }
866 if (sample_type & PERF_SAMPLE_STREAM_ID) {
867 PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
868 }
869 if (sample_type & PERF_SAMPLE_CPU) {
870 PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
871 }
872 if (sample_type & PERF_SAMPLE_PERIOD) {
873 PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
874 }
875 if (sample_type & PERF_SAMPLE_READ) {
876 PrintIndented(indent, "read nr=%zu\n", read_data.counts.size());
877 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
878 PrintIndented(indent + 1, "time_enabled %" PRIu64 "\n", read_data.time_enabled);
879 }
880 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
881 PrintIndented(indent + 1, "time_running %" PRIu64 "\n", read_data.time_running);
882 }
883 for (size_t i = 0; i < read_data.counts.size(); i++) {
884 PrintIndented(indent + 1, "count[%zu] %" PRIu64 "\n", i, read_data.counts[i]);
885 if (read_format & PERF_FORMAT_ID) {
886 PrintIndented(indent + 1, "id[%zu] %" PRIu64 "\n", i, read_data.ids[i]);
887 }
888 }
889 }
890 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
891 PrintIndented(indent, "callchain nr=%" PRIu64 "\n", callchain_data.ip_nr);
892 for (uint64_t i = 0; i < callchain_data.ip_nr; ++i) {
893 PrintIndented(indent + 1, "0x%" PRIx64 "\n", callchain_data.ips[i]);
894 }
895 }
896 if (sample_type & PERF_SAMPLE_RAW) {
897 PrintIndented(indent, "raw size=%zu\n", raw_data.size);
898 const uint32_t* data = reinterpret_cast<const uint32_t*>(raw_data.data);
899 size_t size = raw_data.size / sizeof(uint32_t);
900 for (size_t i = 0; i < size; ++i) {
901 PrintIndented(indent + 1, "0x%08x (%zu)\n", data[i], data[i]);
902 }
903 }
904 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
905 PrintIndented(indent, "branch_stack nr=%" PRIu64 "\n", branch_stack_data.stack_nr);
906 for (uint64_t i = 0; i < branch_stack_data.stack_nr; ++i) {
907 auto& item = branch_stack_data.stack[i];
908 PrintIndented(indent + 1, "from 0x%" PRIx64 ", to 0x%" PRIx64 ", flags 0x%" PRIx64 "\n",
909 item.from, item.to, item.flags);
910 }
911 }
912 if (sample_type & PERF_SAMPLE_REGS_USER) {
913 PrintIndented(indent, "user regs: abi=%" PRId64 "\n", regs_user_data.abi);
914 RegSet regs(regs_user_data.abi, regs_user_data.reg_mask, regs_user_data.regs);
915 for (size_t i = 0; i < 64; ++i) {
916 uint64_t value;
917 if (regs.GetRegValue(i, &value)) {
918 PrintIndented(indent + 1, "reg (%s) 0x%016" PRIx64 "\n", GetRegName(i, regs.arch).c_str(),
919 value);
920 }
921 }
922 }
923 if (sample_type & PERF_SAMPLE_STACK_USER) {
924 PrintIndented(indent, "user stack: size %zu dyn_size %" PRIu64 "\n", stack_user_data.size,
925 stack_user_data.dyn_size);
926 const uint64_t* p = reinterpret_cast<const uint64_t*>(stack_user_data.data);
927 const uint64_t* end = p + (stack_user_data.size / sizeof(uint64_t));
928 while (p < end) {
929 PrintIndented(indent + 1, "");
930 for (size_t i = 0; i < 4 && p < end; ++i, ++p) {
931 printf(" %016" PRIx64, *p);
932 }
933 printf("\n");
934 }
935 printf("\n");
936 }
937 }
938
Timestamp() const939 uint64_t SampleRecord::Timestamp() const {
940 return time_data.time;
941 }
Cpu() const942 uint32_t SampleRecord::Cpu() const {
943 return cpu_data.cpu;
944 }
Id() const945 uint64_t SampleRecord::Id() const {
946 return id_data.id;
947 }
948
AdjustCallChainGeneratedByKernel()949 void SampleRecord::AdjustCallChainGeneratedByKernel() {
950 // The kernel stores return addrs in the callchain, but we want the addrs of call instructions
951 // along the callchain.
952 uint64_t* ips = callchain_data.ips;
953 uint64_t context =
954 header.misc == PERF_RECORD_MISC_KERNEL ? PERF_CONTEXT_KERNEL : PERF_CONTEXT_USER;
955 bool first_frame = true;
956 for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
957 if (ips[i] < PERF_CONTEXT_MAX) {
958 if (first_frame) {
959 first_frame = false;
960 } else {
961 if (ips[i] < 2) {
962 // A wrong ip address, erase it.
963 ips[i] = context;
964 } else {
965 // Here we want to change the return addr to the addr of the previous instruction. We
966 // don't need to find the exact start addr of the previous instruction. A location in
967 // [start_addr_of_call_inst, start_addr_of_next_inst) is enough.
968 #if defined(__arm__) || defined(__aarch64__)
969 // If we are built for arm/aarch64, this may be a callchain of thumb code. For thumb code,
970 // the real instruction addr is (ip & ~1), and ip - 2 can used to hit the address range
971 // of the previous instruction. For non thumb code, any addr in [ip - 4, ip - 1] is fine.
972 ips[i] -= 2;
973 #else
974 ips[i]--;
975 #endif
976 }
977 }
978 } else {
979 context = ips[i];
980 }
981 }
982 }
983
GetCallChain(size_t * kernel_ip_count) const984 std::vector<uint64_t> SampleRecord::GetCallChain(size_t* kernel_ip_count) const {
985 std::vector<uint64_t> ips;
986 bool in_kernel = InKernel();
987 ips.push_back(ip_data.ip);
988 *kernel_ip_count = in_kernel ? 1 : 0;
989 if ((sample_type & PERF_SAMPLE_CALLCHAIN) == 0) {
990 return ips;
991 }
992 bool first_ip = true;
993 for (uint64_t i = 0; i < callchain_data.ip_nr; ++i) {
994 uint64_t ip = callchain_data.ips[i];
995 if (ip >= PERF_CONTEXT_MAX) {
996 switch (ip) {
997 case PERF_CONTEXT_KERNEL:
998 in_kernel = true;
999 break;
1000 case PERF_CONTEXT_USER:
1001 in_kernel = false;
1002 break;
1003 default:
1004 LOG(DEBUG) << "Unexpected perf_context in callchain: " << std::hex << ip << std::dec;
1005 }
1006 } else {
1007 if (first_ip) {
1008 first_ip = false;
1009 // Remove duplication with sample ip.
1010 if (ip == ip_data.ip) {
1011 continue;
1012 }
1013 }
1014 ips.push_back(ip);
1015 if (in_kernel) {
1016 ++*kernel_ip_count;
1017 }
1018 }
1019 }
1020 return ips;
1021 }
1022
Parse(const perf_event_attr & attr,char * p,char * end)1023 bool AuxRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
1024 if (!ParseHeader(p, end)) {
1025 return false;
1026 }
1027 data = reinterpret_cast<DataType*>(p);
1028 CHECK_SIZE(p, end, sizeof(*data));
1029 p += sizeof(*data);
1030 return sample_id.ReadFromBinaryFormat(attr, p, end);
1031 }
1032
DumpData(size_t indent) const1033 void AuxRecord::DumpData(size_t indent) const {
1034 PrintIndented(indent, "aux_offset %" PRIu64 "\n", data->aux_offset);
1035 PrintIndented(indent, "aux_size %" PRIu64 "\n", data->aux_size);
1036 PrintIndented(indent, "flags 0x%" PRIx64 "\n", data->flags);
1037 }
1038
Parse(const perf_event_attr & attr,char * p,char * end)1039 bool SwitchRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
1040 if (!ParseHeader(p, end)) {
1041 return false;
1042 }
1043 return sample_id.ReadFromBinaryFormat(attr, p, end);
1044 }
1045
Parse(const perf_event_attr & attr,char * p,char * end)1046 bool SwitchCpuWideRecord::Parse(const perf_event_attr& attr, char* p, char* end) {
1047 if (!ParseHeader(p, end)) {
1048 return false;
1049 }
1050 CHECK_SIZE(p, end, sizeof(tid_data));
1051 MoveFromBinaryFormat(tid_data, p);
1052 return sample_id.ReadFromBinaryFormat(attr, p, end);
1053 }
1054
DumpData(size_t indent) const1055 void SwitchCpuWideRecord::DumpData(size_t indent) const {
1056 if (header.misc & PERF_RECORD_MISC_SWITCH_OUT) {
1057 PrintIndented(indent, "next_pid %u, next_tid %u\n", tid_data.pid, tid_data.tid);
1058 } else {
1059 PrintIndented(indent, "prev_pid %u, prev_tid %u\n", tid_data.pid, tid_data.tid);
1060 }
1061 }
1062
Parse(const perf_event_attr &,char * p,char * end)1063 bool BuildIdRecord::Parse(const perf_event_attr&, char* p, char* end) {
1064 if (!ParseHeader(p, end)) {
1065 return false;
1066 }
1067 size_t size = Align(BUILD_ID_SIZE, 8);
1068 CHECK_SIZE(p, end, sizeof(uint32_t) + size);
1069 MoveFromBinaryFormat(pid, p);
1070 build_id = BuildId(p, BUILD_ID_SIZE);
1071 p += size;
1072 size = Align(SafeStrlen(p, end) + 1, 64);
1073 CHECK_SIZE(p, end, size);
1074 filename = p;
1075 p += size;
1076 return p == end;
1077 }
1078
DumpData(size_t indent) const1079 void BuildIdRecord::DumpData(size_t indent) const {
1080 PrintIndented(indent, "pid %u\n", pid);
1081 PrintIndented(indent, "build_id %s\n", build_id.ToString().c_str());
1082 PrintIndented(indent, "filename %s\n", filename);
1083 }
1084
BuildIdRecord(bool in_kernel,uint32_t pid,const BuildId & build_id,const std::string & filename)1085 BuildIdRecord::BuildIdRecord(bool in_kernel, uint32_t pid, const BuildId& build_id,
1086 const std::string& filename) {
1087 SetTypeAndMisc(PERF_RECORD_BUILD_ID, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
1088 this->pid = pid;
1089 this->build_id = build_id;
1090 SetSize(header_size() + sizeof(this->pid) + Align(build_id.Size(), 8) +
1091 Align(filename.size() + 1, 64));
1092 char* new_binary = new char[size()];
1093 char* p = new_binary;
1094 MoveToBinaryFormat(header, p);
1095 MoveToBinaryFormat(this->pid, p);
1096 memcpy(p, build_id.Data(), build_id.Size());
1097 p += Align(build_id.Size(), 8);
1098 this->filename = p;
1099 strcpy(p, filename.c_str());
1100 UpdateBinary(new_binary);
1101 }
1102
Parse(const perf_event_attr &,char * p,char * end)1103 bool AuxTraceInfoRecord::Parse(const perf_event_attr&, char* p, char* end) {
1104 if (!ParseHeader(p, end)) {
1105 return false;
1106 }
1107 data = reinterpret_cast<DataType*>(p);
1108 CHECK_SIZE(p, end, sizeof(*data));
1109 p += sizeof(*data);
1110 if (data->aux_type != AUX_TYPE_ETM || data->version != 1) {
1111 return false;
1112 }
1113 for (uint32_t i = 0; i < data->nr_cpu; ++i) {
1114 CHECK_SIZE(p, end, sizeof(uint64_t));
1115 uint64_t magic = *reinterpret_cast<uint64_t*>(p);
1116 if (magic == MAGIC_ETM4) {
1117 CHECK_SIZE(p, end, sizeof(ETM4Info));
1118 p += sizeof(ETM4Info);
1119 } else if (magic == MAGIC_ETE) {
1120 CHECK_SIZE(p, end, sizeof(ETEInfo));
1121 p += sizeof(ETEInfo);
1122 } else {
1123 return false;
1124 }
1125 }
1126 return p == end;
1127 }
1128
AuxTraceInfoRecord(const DataType & data,const std::vector<ETEInfo> & ete_info)1129 AuxTraceInfoRecord::AuxTraceInfoRecord(const DataType& data, const std::vector<ETEInfo>& ete_info) {
1130 SetTypeAndMisc(PERF_RECORD_AUXTRACE_INFO, 0);
1131
1132 uint32_t size = header_size() + sizeof(DataType);
1133 for (auto& ete : ete_info) {
1134 size += (ete.trcdevarch == 0) ? sizeof(ETM4Info) : sizeof(ETEInfo);
1135 }
1136 SetSize(size);
1137 char* new_binary = new char[size];
1138 char* p = new_binary;
1139 MoveToBinaryFormat(header, p);
1140 this->data = reinterpret_cast<DataType*>(p);
1141 MoveToBinaryFormat(data, p);
1142 for (auto& ete : ete_info) {
1143 if (ete.trcdevarch == 0) {
1144 ETM4Info etm4;
1145 static_assert(sizeof(ETM4Info) + sizeof(uint64_t) == sizeof(ETEInfo));
1146 memcpy(&etm4, &ete, sizeof(ETM4Info));
1147 MoveToBinaryFormat(etm4, p);
1148 } else {
1149 MoveToBinaryFormat(ete, p);
1150 }
1151 }
1152 UpdateBinary(new_binary);
1153 }
1154
DumpData(size_t indent) const1155 void AuxTraceInfoRecord::DumpData(size_t indent) const {
1156 PrintIndented(indent, "aux_type %u\n", data->aux_type);
1157 PrintIndented(indent, "version %" PRIu64 "\n", data->version);
1158 PrintIndented(indent, "nr_cpu %u\n", data->nr_cpu);
1159 PrintIndented(indent, "pmu_type %u\n", data->pmu_type);
1160 PrintIndented(indent, "snapshot %" PRIu64 "\n", data->snapshot);
1161 indent++;
1162 uint64_t* info = data->info;
1163
1164 for (int i = 0; i < data->nr_cpu; i++) {
1165 if (info[0] == MAGIC_ETM4) {
1166 ETM4Info& e = *reinterpret_cast<ETM4Info*>(info);
1167 PrintIndented(indent, "magic 0x%" PRIx64 "\n", e.magic);
1168 PrintIndented(indent, "cpu %" PRIu64 "\n", e.cpu);
1169 PrintIndented(indent, "nrtrcparams %" PRIu64 "\n", e.nrtrcparams);
1170 PrintIndented(indent, "trcconfigr 0x%" PRIx64 "\n", e.trcconfigr);
1171 PrintIndented(indent, "trctraceidr 0x%" PRIx64 "\n", e.trctraceidr);
1172 PrintIndented(indent, "trcidr0 0x%" PRIx64 "\n", e.trcidr0);
1173 PrintIndented(indent, "trcidr1 0x%" PRIx64 "\n", e.trcidr1);
1174 PrintIndented(indent, "trcidr2 0x%" PRIx64 "\n", e.trcidr2);
1175 PrintIndented(indent, "trcidr8 0x%" PRIx64 "\n", e.trcidr8);
1176 PrintIndented(indent, "trcauthstatus 0x%" PRIx64 "\n", e.trcauthstatus);
1177 info = reinterpret_cast<uint64_t*>(&e + 1);
1178 } else {
1179 CHECK_EQ(info[0], MAGIC_ETE);
1180 ETEInfo& e = *reinterpret_cast<ETEInfo*>(info);
1181 PrintIndented(indent, "magic 0x%" PRIx64 "\n", e.magic);
1182 PrintIndented(indent, "cpu %" PRIu64 "\n", e.cpu);
1183 PrintIndented(indent, "nrtrcparams %" PRIu64 "\n", e.nrtrcparams);
1184 PrintIndented(indent, "trcconfigr 0x%" PRIx64 "\n", e.trcconfigr);
1185 PrintIndented(indent, "trctraceidr 0x%" PRIx64 "\n", e.trctraceidr);
1186 PrintIndented(indent, "trcidr0 0x%" PRIx64 "\n", e.trcidr0);
1187 PrintIndented(indent, "trcidr1 0x%" PRIx64 "\n", e.trcidr1);
1188 PrintIndented(indent, "trcidr2 0x%" PRIx64 "\n", e.trcidr2);
1189 PrintIndented(indent, "trcidr8 0x%" PRIx64 "\n", e.trcidr8);
1190 PrintIndented(indent, "trcauthstatus 0x%" PRIx64 "\n", e.trcauthstatus);
1191 PrintIndented(indent, "trcdevarch 0x%" PRIx64 "\n", e.trcdevarch);
1192 info = reinterpret_cast<uint64_t*>(&e + 1);
1193 }
1194 }
1195 }
1196
Parse(const perf_event_attr &,char * p,char * end)1197 bool AuxTraceRecord::Parse(const perf_event_attr&, char* p, char* end) {
1198 if (!ParseHeader(p, end)) {
1199 return false;
1200 }
1201 data = reinterpret_cast<DataType*>(p);
1202 CHECK_SIZE(p, end, sizeof(*data));
1203 p += sizeof(*data);
1204 return p == end;
1205 }
1206
AuxTraceRecord(uint64_t aux_size,uint64_t offset,uint32_t idx,uint32_t tid,uint32_t cpu)1207 AuxTraceRecord::AuxTraceRecord(uint64_t aux_size, uint64_t offset, uint32_t idx, uint32_t tid,
1208 uint32_t cpu) {
1209 SetTypeAndMisc(PERF_RECORD_AUXTRACE, 0);
1210 SetSize(header_size() + sizeof(DataType));
1211 char* new_binary = new char[size()];
1212 char* p = new_binary;
1213 MoveToBinaryFormat(header, p);
1214 data = reinterpret_cast<DataType*>(p);
1215 data->aux_size = aux_size;
1216 data->offset = offset;
1217 data->reserved0 = 0;
1218 data->idx = idx;
1219 data->tid = tid;
1220 data->cpu = cpu;
1221 data->reserved1 = 0;
1222 UpdateBinary(new_binary);
1223 }
1224
DumpData(size_t indent) const1225 void AuxTraceRecord::DumpData(size_t indent) const {
1226 PrintIndented(indent, "aux_size %" PRIu64 "\n", data->aux_size);
1227 PrintIndented(indent, "offset %" PRIu64 "\n", data->offset);
1228 PrintIndented(indent, "idx %u\n", data->idx);
1229 PrintIndented(indent, "tid %u\n", data->tid);
1230 PrintIndented(indent, "cpu %u\n", data->cpu);
1231 PrintIndented(indent, "location.file_offset %" PRIu64 "\n", location.file_offset);
1232 }
1233
Parse(const perf_event_attr &,char * p,char * end)1234 bool KernelSymbolRecord::Parse(const perf_event_attr&, char* p, char* end) {
1235 if (!ParseHeader(p, end)) {
1236 return false;
1237 }
1238 CHECK_SIZE(p, end, sizeof(uint32_t));
1239 MoveFromBinaryFormat(kallsyms_size, p);
1240 size_t size = Align(kallsyms_size, 8);
1241 CHECK_SIZE(p, end, size);
1242 kallsyms = p;
1243 p += size;
1244 return p == end;
1245 }
1246
DumpData(size_t indent) const1247 void KernelSymbolRecord::DumpData(size_t indent) const {
1248 PrintIndented(indent, "kallsyms: %s\n", std::string(kallsyms, kallsyms + kallsyms_size).c_str());
1249 }
1250
KernelSymbolRecord(const std::string & kallsyms)1251 KernelSymbolRecord::KernelSymbolRecord(const std::string& kallsyms) {
1252 SetTypeAndMisc(SIMPLE_PERF_RECORD_KERNEL_SYMBOL, 0);
1253 kallsyms_size = kallsyms.size();
1254 SetSize(header_size() + 4 + Align(kallsyms.size(), 8));
1255 char* new_binary = new char[size()];
1256 char* p = new_binary;
1257 MoveToBinaryFormat(header, p);
1258 MoveToBinaryFormat(kallsyms_size, p);
1259 this->kallsyms = p;
1260 memcpy(p, kallsyms.data(), kallsyms_size);
1261 UpdateBinary(new_binary);
1262 }
1263
Parse(const perf_event_attr &,char * p,char * end)1264 bool DsoRecord::Parse(const perf_event_attr&, char* p, char* end) {
1265 if (!ParseHeader(p, end)) {
1266 return false;
1267 }
1268 CHECK_SIZE_U64(p, end, 3);
1269 MoveFromBinaryFormat(dso_type, p);
1270 MoveFromBinaryFormat(dso_id, p);
1271 MoveFromBinaryFormat(min_vaddr, p);
1272 size_t size = Align(SafeStrlen(p, end) + 1, 8);
1273 dso_name = p;
1274 p += size;
1275 return p == end;
1276 }
1277
DsoRecord(uint64_t dso_type,uint64_t dso_id,const std::string & dso_name,uint64_t min_vaddr)1278 DsoRecord::DsoRecord(uint64_t dso_type, uint64_t dso_id, const std::string& dso_name,
1279 uint64_t min_vaddr) {
1280 SetTypeAndMisc(SIMPLE_PERF_RECORD_DSO, 0);
1281 this->dso_type = dso_type;
1282 this->dso_id = dso_id;
1283 this->min_vaddr = min_vaddr;
1284 SetSize(header_size() + 3 * sizeof(uint64_t) + Align(dso_name.size() + 1, 8));
1285 char* new_binary = new char[size()];
1286 char* p = new_binary;
1287 MoveToBinaryFormat(header, p);
1288 MoveToBinaryFormat(dso_type, p);
1289 MoveToBinaryFormat(dso_id, p);
1290 MoveToBinaryFormat(min_vaddr, p);
1291 this->dso_name = p;
1292 strcpy(p, dso_name.c_str());
1293 UpdateBinary(new_binary);
1294 }
1295
DumpData(size_t indent) const1296 void DsoRecord::DumpData(size_t indent) const {
1297 PrintIndented(indent, "dso_type: %s(%" PRIu64 ")\n",
1298 DsoTypeToString(static_cast<DsoType>(dso_type)), dso_type);
1299 PrintIndented(indent, "dso_id: %" PRIu64 "\n", dso_id);
1300 PrintIndented(indent, "min_vaddr: 0x%" PRIx64 "\n", min_vaddr);
1301 PrintIndented(indent, "dso_name: %s\n", dso_name);
1302 }
1303
Parse(const perf_event_attr &,char * p,char * end)1304 bool SymbolRecord::Parse(const perf_event_attr&, char* p, char* end) {
1305 if (!ParseHeader(p, end)) {
1306 return false;
1307 }
1308 CHECK_SIZE_U64(p, end, 3);
1309 MoveFromBinaryFormat(addr, p);
1310 MoveFromBinaryFormat(len, p);
1311 MoveFromBinaryFormat(dso_id, p);
1312 size_t size = Align(SafeStrlen(p, end) + 1, 8);
1313 name = p;
1314 p += size;
1315 return p == end;
1316 }
1317
SymbolRecord(uint64_t addr,uint64_t len,const std::string & name,uint64_t dso_id)1318 SymbolRecord::SymbolRecord(uint64_t addr, uint64_t len, const std::string& name, uint64_t dso_id) {
1319 SetTypeAndMisc(SIMPLE_PERF_RECORD_SYMBOL, 0);
1320 this->addr = addr;
1321 this->len = len;
1322 this->dso_id = dso_id;
1323 SetSize(header_size() + 3 * sizeof(uint64_t) + Align(name.size() + 1, 8));
1324 char* new_binary = new char[size()];
1325 char* p = new_binary;
1326 MoveToBinaryFormat(header, p);
1327 MoveToBinaryFormat(addr, p);
1328 MoveToBinaryFormat(len, p);
1329 MoveToBinaryFormat(dso_id, p);
1330 this->name = p;
1331 strcpy(p, name.c_str());
1332 UpdateBinary(new_binary);
1333 }
1334
DumpData(size_t indent) const1335 void SymbolRecord::DumpData(size_t indent) const {
1336 PrintIndented(indent, "name: %s\n", name);
1337 PrintIndented(indent, "addr: 0x%" PRIx64 "\n", addr);
1338 PrintIndented(indent, "len: 0x%" PRIx64 "\n", len);
1339 PrintIndented(indent, "dso_id: %" PRIu64 "\n", dso_id);
1340 }
1341
Parse(const perf_event_attr &,char * p,char * end)1342 bool TracingDataRecord::Parse(const perf_event_attr&, char* p, char* end) {
1343 if (!ParseHeader(p, end)) {
1344 return false;
1345 }
1346 CHECK_SIZE(p, end, sizeof(uint32_t));
1347 MoveFromBinaryFormat(data_size, p);
1348 size_t size = Align(data_size, 64);
1349 CHECK_SIZE(p, end, size);
1350 data = p;
1351 p += size;
1352 return p == end;
1353 }
1354
TracingDataRecord(const std::vector<char> & tracing_data)1355 TracingDataRecord::TracingDataRecord(const std::vector<char>& tracing_data) {
1356 SetTypeAndMisc(SIMPLE_PERF_RECORD_TRACING_DATA, 0);
1357 data_size = tracing_data.size();
1358 SetSize(header_size() + sizeof(uint32_t) + Align(tracing_data.size(), 64));
1359 char* new_binary = new char[size()];
1360 char* p = new_binary;
1361 MoveToBinaryFormat(header, p);
1362 MoveToBinaryFormat(data_size, p);
1363 data = p;
1364 memcpy(p, tracing_data.data(), data_size);
1365 UpdateBinary(new_binary);
1366 }
1367
DumpData(size_t indent) const1368 void TracingDataRecord::DumpData(size_t indent) const {
1369 auto tracing = Tracing::Create(std::vector<char>(data, data + data_size));
1370 if (tracing) {
1371 tracing->Dump(indent);
1372 }
1373 }
1374
Parse(const perf_event_attr &,char * p,char * end)1375 bool EventIdRecord::Parse(const perf_event_attr&, char* p, char* end) {
1376 if (!ParseHeader(p, end)) {
1377 return false;
1378 }
1379 CHECK_SIZE_U64(p, end, 1);
1380 MoveFromBinaryFormat(count, p);
1381 data = reinterpret_cast<const EventIdData*>(p);
1382 CHECK_SIZE(p, end, sizeof(data[0]) * count);
1383 p += sizeof(data[0]) * count;
1384 return p == end;
1385 }
1386
EventIdRecord(const std::vector<uint64_t> & data)1387 EventIdRecord::EventIdRecord(const std::vector<uint64_t>& data) {
1388 SetTypeAndMisc(SIMPLE_PERF_RECORD_EVENT_ID, 0);
1389 SetSize(header_size() + sizeof(uint64_t) * (1 + data.size()));
1390 char* new_binary = new char[size()];
1391 char* p = new_binary;
1392 MoveToBinaryFormat(header, p);
1393 count = data.size() / 2;
1394 MoveToBinaryFormat(count, p);
1395 this->data = reinterpret_cast<EventIdData*>(p);
1396 memcpy(p, data.data(), sizeof(uint64_t) * data.size());
1397 UpdateBinary(new_binary);
1398 }
1399
DumpData(size_t indent) const1400 void EventIdRecord::DumpData(size_t indent) const {
1401 PrintIndented(indent, "count: %" PRIu64 "\n", count);
1402 for (size_t i = 0; i < count; ++i) {
1403 PrintIndented(indent, "attr_id[%" PRIu64 "]: %" PRIu64 "\n", i, data[i].attr_id);
1404 PrintIndented(indent, "event_id[%" PRIu64 "]: %" PRIu64 "\n", i, data[i].event_id);
1405 }
1406 }
1407
Parse(const perf_event_attr &,char * p,char * end)1408 bool CallChainRecord::Parse(const perf_event_attr&, char* p, char* end) {
1409 if (!ParseHeader(p, end)) {
1410 return false;
1411 }
1412 CHECK_SIZE_U64(p, end, 4);
1413 MoveFromBinaryFormat(pid, p);
1414 MoveFromBinaryFormat(tid, p);
1415 MoveFromBinaryFormat(chain_type, p);
1416 MoveFromBinaryFormat(time, p);
1417 MoveFromBinaryFormat(ip_nr, p);
1418 CHECK_SIZE_U64(p, end, ip_nr * 2);
1419 ips = reinterpret_cast<uint64_t*>(p);
1420 p += ip_nr * sizeof(uint64_t);
1421 sps = reinterpret_cast<uint64_t*>(p);
1422 p += ip_nr * sizeof(uint64_t);
1423 return p == end;
1424 }
1425
CallChainRecord(pid_t pid,pid_t tid,CallChainJoiner::ChainType type,uint64_t time,const std::vector<uint64_t> & ips,const std::vector<uint64_t> & sps)1426 CallChainRecord::CallChainRecord(pid_t pid, pid_t tid, CallChainJoiner::ChainType type,
1427 uint64_t time, const std::vector<uint64_t>& ips,
1428 const std::vector<uint64_t>& sps) {
1429 CHECK_EQ(ips.size(), sps.size());
1430 SetTypeAndMisc(SIMPLE_PERF_RECORD_CALLCHAIN, 0);
1431 this->pid = pid;
1432 this->tid = tid;
1433 this->chain_type = static_cast<int>(type);
1434 this->time = time;
1435 this->ip_nr = ips.size();
1436 SetSize(header_size() + (4 + ips.size() * 2) * sizeof(uint64_t));
1437 char* new_binary = new char[size()];
1438 char* p = new_binary;
1439 MoveToBinaryFormat(header, p);
1440 MoveToBinaryFormat(this->pid, p);
1441 MoveToBinaryFormat(this->tid, p);
1442 MoveToBinaryFormat(this->chain_type, p);
1443 MoveToBinaryFormat(this->time, p);
1444 MoveToBinaryFormat(this->ip_nr, p);
1445 this->ips = reinterpret_cast<uint64_t*>(p);
1446 MoveToBinaryFormat(ips.data(), ips.size(), p);
1447 this->sps = reinterpret_cast<uint64_t*>(p);
1448 MoveToBinaryFormat(sps.data(), sps.size(), p);
1449 UpdateBinary(new_binary);
1450 }
1451
DumpData(size_t indent) const1452 void CallChainRecord::DumpData(size_t indent) const {
1453 const char* type_name = "";
1454 switch (chain_type) {
1455 case CallChainJoiner::ORIGINAL_OFFLINE:
1456 type_name = "ORIGINAL_OFFLINE";
1457 break;
1458 case CallChainJoiner::ORIGINAL_REMOTE:
1459 type_name = "ORIGINAL_REMOTE";
1460 break;
1461 case CallChainJoiner::JOINED_OFFLINE:
1462 type_name = "JOINED_OFFLINE";
1463 break;
1464 case CallChainJoiner::JOINED_REMOTE:
1465 type_name = "JOINED_REMOTE";
1466 break;
1467 }
1468 PrintIndented(indent, "pid %u\n", pid);
1469 PrintIndented(indent, "tid %u\n", tid);
1470 PrintIndented(indent, "chain_type %s\n", type_name);
1471 PrintIndented(indent, "time %" PRIu64 "\n", time);
1472 PrintIndented(indent, "ip_nr %" PRIu64 "\n", ip_nr);
1473 for (size_t i = 0; i < ip_nr; ++i) {
1474 PrintIndented(indent + 1, "ip 0x%" PRIx64 ", sp 0x%" PRIx64 "\n", ips[i], sps[i]);
1475 }
1476 }
1477
Parse(const perf_event_attr &,char * p,char * end)1478 bool UnwindingResultRecord::Parse(const perf_event_attr&, char* p, char* end) {
1479 if (!ParseHeader(p, end)) {
1480 return false;
1481 }
1482 CHECK_SIZE_U64(p, end, 8);
1483 MoveFromBinaryFormat(time, p);
1484 MoveFromBinaryFormat(unwinding_result.used_time, p);
1485 MoveFromBinaryFormat(unwinding_result.error_code, p);
1486 MoveFromBinaryFormat(unwinding_result.error_addr, p);
1487 MoveFromBinaryFormat(unwinding_result.stack_start, p);
1488 MoveFromBinaryFormat(unwinding_result.stack_end, p);
1489
1490 // regs_user_data
1491 MoveFromBinaryFormat(regs_user_data.abi, p);
1492 MoveFromBinaryFormat(regs_user_data.reg_mask, p);
1493 size_t bit_nr = __builtin_popcountll(regs_user_data.reg_mask);
1494 CHECK_SIZE_U64(p, end, bit_nr);
1495 regs_user_data.reg_nr = bit_nr;
1496 regs_user_data.regs = reinterpret_cast<uint64_t*>(p);
1497 p += bit_nr * sizeof(uint64_t);
1498
1499 // stack_user_data
1500 CHECK_SIZE_U64(p, end, 1);
1501 MoveFromBinaryFormat(stack_user_data.size, p);
1502 if (stack_user_data.size == 0) {
1503 stack_user_data.dyn_size = 0;
1504 } else {
1505 CHECK_SIZE(p, end, stack_user_data.size + sizeof(uint64_t));
1506 stack_user_data.data = p;
1507 p += stack_user_data.size;
1508 MoveFromBinaryFormat(stack_user_data.dyn_size, p);
1509 }
1510
1511 // callchain
1512 if (p < end) {
1513 CHECK_SIZE_U64(p, end, 1);
1514 MoveFromBinaryFormat(callchain.length, p);
1515 CHECK_SIZE_U64(p, end, callchain.length * 2);
1516 callchain.ips = reinterpret_cast<uint64_t*>(p);
1517 p += callchain.length * sizeof(uint64_t);
1518 callchain.sps = reinterpret_cast<uint64_t*>(p);
1519 p += callchain.length * sizeof(uint64_t);
1520 }
1521 return true;
1522 }
1523
UnwindingResultRecord(uint64_t time,const UnwindingResult & unwinding_result,const PerfSampleRegsUserType & regs_user_data,const PerfSampleStackUserType & stack_user_data,const std::vector<uint64_t> & ips,const std::vector<uint64_t> & sps)1524 UnwindingResultRecord::UnwindingResultRecord(uint64_t time, const UnwindingResult& unwinding_result,
1525 const PerfSampleRegsUserType& regs_user_data,
1526 const PerfSampleStackUserType& stack_user_data,
1527 const std::vector<uint64_t>& ips,
1528 const std::vector<uint64_t>& sps) {
1529 SetTypeAndMisc(SIMPLE_PERF_RECORD_UNWINDING_RESULT, 0);
1530 uint32_t size = header_size() + 6 * sizeof(uint64_t);
1531 size += (2 + regs_user_data.reg_nr) * sizeof(uint64_t);
1532 size +=
1533 stack_user_data.size == 0 ? sizeof(uint64_t) : (2 * sizeof(uint64_t) + stack_user_data.size);
1534 CHECK_EQ(ips.size(), sps.size());
1535 size += (1 + ips.size() * 2) * sizeof(uint64_t);
1536 SetSize(size);
1537 this->time = time;
1538 this->unwinding_result = unwinding_result;
1539 char* new_binary = new char[size];
1540 char* p = new_binary;
1541 MoveToBinaryFormat(header, p);
1542 MoveToBinaryFormat(this->time, p);
1543 MoveToBinaryFormat(unwinding_result.used_time, p);
1544 MoveToBinaryFormat(unwinding_result.error_code, p);
1545 MoveToBinaryFormat(unwinding_result.error_addr, p);
1546 MoveToBinaryFormat(unwinding_result.stack_start, p);
1547 MoveToBinaryFormat(unwinding_result.stack_end, p);
1548 MoveToBinaryFormat(regs_user_data.abi, p);
1549 MoveToBinaryFormat(regs_user_data.reg_mask, p);
1550 if (regs_user_data.reg_nr > 0) {
1551 MoveToBinaryFormat(regs_user_data.regs, regs_user_data.reg_nr, p);
1552 }
1553 MoveToBinaryFormat(stack_user_data.size, p);
1554 if (stack_user_data.size > 0) {
1555 MoveToBinaryFormat(stack_user_data.data, stack_user_data.size, p);
1556 MoveToBinaryFormat(stack_user_data.dyn_size, p);
1557 }
1558 MoveToBinaryFormat(static_cast<uint64_t>(ips.size()), p);
1559 MoveToBinaryFormat(ips.data(), ips.size(), p);
1560 MoveToBinaryFormat(sps.data(), sps.size(), p);
1561 CHECK_EQ(p, new_binary + size);
1562 UpdateBinary(new_binary);
1563 }
1564
DumpData(size_t indent) const1565 void UnwindingResultRecord::DumpData(size_t indent) const {
1566 PrintIndented(indent, "time %" PRIu64 "\n", time);
1567 PrintIndented(indent, "used_time %" PRIu64 "\n", unwinding_result.used_time);
1568 PrintIndented(indent, "error_code %" PRIu64 "\n", unwinding_result.error_code);
1569 PrintIndented(indent, "error_addr 0x%" PRIx64 "\n", unwinding_result.error_addr);
1570 PrintIndented(indent, "stack_start 0x%" PRIx64 "\n", unwinding_result.stack_start);
1571 PrintIndented(indent, "stack_end 0x%" PRIx64 "\n", unwinding_result.stack_end);
1572 if (regs_user_data.reg_nr > 0) {
1573 PrintIndented(indent, "user regs: abi=%" PRId64 "\n", regs_user_data.abi);
1574 RegSet regs(regs_user_data.abi, regs_user_data.reg_mask, regs_user_data.regs);
1575 for (size_t i = 0; i < 64; ++i) {
1576 uint64_t value;
1577 if (regs.GetRegValue(i, &value)) {
1578 PrintIndented(indent + 1, "reg (%s) 0x%016" PRIx64 "\n", GetRegName(i, regs.arch).c_str(),
1579 value);
1580 }
1581 }
1582 }
1583 if (stack_user_data.size > 0) {
1584 PrintIndented(indent, "user stack: size %zu dyn_size %" PRIu64 "\n", stack_user_data.size,
1585 stack_user_data.dyn_size);
1586 const uint64_t* p = reinterpret_cast<const uint64_t*>(stack_user_data.data);
1587 const uint64_t* end = p + (stack_user_data.size / sizeof(uint64_t));
1588 while (p < end) {
1589 PrintIndented(indent + 1, "");
1590 for (size_t i = 0; i < 4 && p < end; ++i, ++p) {
1591 printf(" %016" PRIx64, *p);
1592 }
1593 printf("\n");
1594 }
1595 printf("\n");
1596 }
1597 if (callchain.length > 0) {
1598 PrintIndented(indent, "callchain length=%" PRIu64 ":\n", callchain.length);
1599 for (uint64_t i = 0; i < callchain.length; i++) {
1600 PrintIndented(indent + 1, "ip_%" PRIu64 ": 0x%" PRIx64 "\n", i + 1, callchain.ips[i]);
1601 PrintIndented(indent + 1, "sp_%" PRIu64 ": 0x%" PRIx64 "\n", i + 1, callchain.sps[i]);
1602 }
1603 }
1604 }
1605
Parse(const perf_event_attr &,char * p,char * end)1606 bool UnknownRecord::Parse(const perf_event_attr&, char* p, char* end) {
1607 if (!ParseHeader(p, end)) {
1608 return false;
1609 }
1610 data = p;
1611 return true;
1612 }
1613
DumpData(size_t) const1614 void UnknownRecord::DumpData(size_t) const {}
1615
ReadRecordFromBuffer(const perf_event_attr & attr,uint32_t type,char * p,char * end)1616 std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, uint32_t type, char* p,
1617 char* end) {
1618 std::unique_ptr<Record> r;
1619 switch (type) {
1620 case PERF_RECORD_MMAP:
1621 r.reset(new MmapRecord);
1622 break;
1623 case PERF_RECORD_MMAP2:
1624 r.reset(new Mmap2Record);
1625 break;
1626 case PERF_RECORD_COMM:
1627 r.reset(new CommRecord);
1628 break;
1629 case PERF_RECORD_EXIT:
1630 r.reset(new ExitRecord);
1631 break;
1632 case PERF_RECORD_FORK:
1633 r.reset(new ForkRecord);
1634 break;
1635 case PERF_RECORD_LOST:
1636 r.reset(new LostRecord);
1637 break;
1638 case PERF_RECORD_SAMPLE:
1639 r.reset(new SampleRecord);
1640 break;
1641 case PERF_RECORD_AUX:
1642 r.reset(new AuxRecord);
1643 break;
1644 case PERF_RECORD_SWITCH:
1645 r.reset(new SwitchRecord);
1646 break;
1647 case PERF_RECORD_SWITCH_CPU_WIDE:
1648 r.reset(new SwitchCpuWideRecord);
1649 break;
1650 case PERF_RECORD_TRACING_DATA:
1651 r.reset(new TracingDataRecord);
1652 break;
1653 case PERF_RECORD_AUXTRACE_INFO:
1654 r.reset(new AuxTraceInfoRecord);
1655 break;
1656 case PERF_RECORD_AUXTRACE:
1657 r.reset(new AuxTraceRecord);
1658 break;
1659 case SIMPLE_PERF_RECORD_KERNEL_SYMBOL:
1660 r.reset(new KernelSymbolRecord);
1661 break;
1662 case SIMPLE_PERF_RECORD_DSO:
1663 r.reset(new DsoRecord);
1664 break;
1665 case SIMPLE_PERF_RECORD_SYMBOL:
1666 r.reset(new SymbolRecord);
1667 break;
1668 case SIMPLE_PERF_RECORD_EVENT_ID:
1669 r.reset(new EventIdRecord);
1670 break;
1671 case SIMPLE_PERF_RECORD_CALLCHAIN:
1672 r.reset(new CallChainRecord);
1673 break;
1674 case SIMPLE_PERF_RECORD_UNWINDING_RESULT:
1675 r.reset(new UnwindingResultRecord);
1676 break;
1677 case SIMPLE_PERF_RECORD_TRACING_DATA:
1678 r.reset(new TracingDataRecord);
1679 break;
1680 default:
1681 r.reset(new UnknownRecord);
1682 break;
1683 }
1684 if (UNLIKELY(!r->Parse(attr, p, end))) {
1685 LOG(ERROR) << "failed to parse record " << RecordTypeToString(type);
1686 return nullptr;
1687 }
1688 return r;
1689 }
1690
ReadRecordsFromBuffer(const perf_event_attr & attr,char * buf,size_t buf_size)1691 std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr, char* buf,
1692 size_t buf_size) {
1693 std::vector<std::unique_ptr<Record>> result;
1694 char* p = buf;
1695 char* end = buf + buf_size;
1696 while (p < end) {
1697 std::unique_ptr<Record> r = ReadRecordFromBuffer(attr, p, end);
1698 if (!r) {
1699 return {};
1700 }
1701 p += r->size();
1702 result.emplace_back(std::move(r));
1703 }
1704 return result;
1705 }
1706
ReadRecordFromBuffer(const perf_event_attr & attr,char * p,char * end)1707 std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, char* p, char* end) {
1708 auto header = reinterpret_cast<const perf_event_header*>(p);
1709 return ReadRecordFromBuffer(attr, header->type, p, end);
1710 }
1711
1712 } // namespace simpleperf
1713