• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/stringprintf.h>
25 
26 #include "OfflineUnwinder.h"
27 #include "dso.h"
28 #include "perf_regs.h"
29 #include "tracing.h"
30 #include "utils.h"
31 
32 namespace simpleperf {
33 
RecordTypeToString(int record_type)34 static std::string RecordTypeToString(int record_type) {
35   static std::unordered_map<int, std::string> record_type_names = {
36       {PERF_RECORD_MMAP, "mmap"},
37       {PERF_RECORD_LOST, "lost"},
38       {PERF_RECORD_COMM, "comm"},
39       {PERF_RECORD_EXIT, "exit"},
40       {PERF_RECORD_THROTTLE, "throttle"},
41       {PERF_RECORD_UNTHROTTLE, "unthrottle"},
42       {PERF_RECORD_FORK, "fork"},
43       {PERF_RECORD_READ, "read"},
44       {PERF_RECORD_SAMPLE, "sample"},
45       {PERF_RECORD_BUILD_ID, "build_id"},
46       {PERF_RECORD_MMAP2, "mmap2"},
47       {PERF_RECORD_AUX, "aux"},
48       {PERF_RECORD_TRACING_DATA, "tracing_data"},
49       {PERF_RECORD_AUXTRACE_INFO, "auxtrace_info"},
50       {PERF_RECORD_AUXTRACE, "auxtrace"},
51       {SIMPLE_PERF_RECORD_KERNEL_SYMBOL, "kernel_symbol"},
52       {SIMPLE_PERF_RECORD_DSO, "dso"},
53       {SIMPLE_PERF_RECORD_SYMBOL, "symbol"},
54       {SIMPLE_PERF_RECORD_EVENT_ID, "event_id"},
55       {SIMPLE_PERF_RECORD_CALLCHAIN, "callchain"},
56       {SIMPLE_PERF_RECORD_UNWINDING_RESULT, "unwinding_result"},
57       {SIMPLE_PERF_RECORD_TRACING_DATA, "tracing_data"},
58   };
59 
60   auto it = record_type_names.find(record_type);
61   if (it != record_type_names.end()) {
62     return it->second;
63   }
64   return android::base::StringPrintf("unknown(%d)", record_type);
65 }
66 
67 template <>
MoveToBinaryFormat(const RecordHeader & data,char * & p)68 void MoveToBinaryFormat(const RecordHeader& data, char*& p) {
69   data.MoveToBinaryFormat(p);
70 }
71 
SampleId()72 SampleId::SampleId() {
73   memset(this, 0, sizeof(SampleId));
74 }
75 
76 // Return sample_id size in binary format.
CreateContent(const perf_event_attr & attr,uint64_t event_id)77 size_t SampleId::CreateContent(const perf_event_attr& attr, uint64_t event_id) {
78   sample_id_all = attr.sample_id_all;
79   sample_type = attr.sample_type;
80   id_data.id = event_id;
81   // Other data are not necessary. TODO: Set missing SampleId data.
82   return Size();
83 }
84 
ReadFromBinaryFormat(const perf_event_attr & attr,const char * p,const char * end)85 void SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
86   sample_id_all = attr.sample_id_all;
87   sample_type = attr.sample_type;
88   if (sample_id_all) {
89     if (sample_type & PERF_SAMPLE_TID) {
90       MoveFromBinaryFormat(tid_data, p);
91     }
92     if (sample_type & PERF_SAMPLE_TIME) {
93       MoveFromBinaryFormat(time_data, p);
94     }
95     if (sample_type & PERF_SAMPLE_ID) {
96       MoveFromBinaryFormat(id_data, p);
97     }
98     if (sample_type & PERF_SAMPLE_STREAM_ID) {
99       MoveFromBinaryFormat(stream_id_data, p);
100     }
101     if (sample_type & PERF_SAMPLE_CPU) {
102       MoveFromBinaryFormat(cpu_data, p);
103     }
104     if (sample_type & PERF_SAMPLE_IDENTIFIER) {
105       MoveFromBinaryFormat(id_data, p);
106     }
107   }
108   CHECK_LE(p, end);
109   if (p < end) {
110     LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
111   }
112 }
113 
WriteToBinaryFormat(char * & p) const114 void SampleId::WriteToBinaryFormat(char*& p) const {
115   if (sample_id_all) {
116     if (sample_type & PERF_SAMPLE_TID) {
117       MoveToBinaryFormat(tid_data, p);
118     }
119     if (sample_type & PERF_SAMPLE_TIME) {
120       MoveToBinaryFormat(time_data, p);
121     }
122     if (sample_type & PERF_SAMPLE_ID) {
123       MoveToBinaryFormat(id_data, p);
124     }
125     if (sample_type & PERF_SAMPLE_STREAM_ID) {
126       MoveToBinaryFormat(stream_id_data, p);
127     }
128     if (sample_type & PERF_SAMPLE_CPU) {
129       MoveToBinaryFormat(cpu_data, p);
130     }
131   }
132 }
133 
Dump(size_t indent) const134 void SampleId::Dump(size_t indent) const {
135   if (sample_id_all) {
136     if (sample_type & PERF_SAMPLE_TID) {
137       PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
138     }
139     if (sample_type & PERF_SAMPLE_TIME) {
140       PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
141     }
142     if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) {
143       PrintIndented(indent, "sample_id: id %" PRId64 "\n", id_data.id);
144     }
145     if (sample_type & PERF_SAMPLE_STREAM_ID) {
146       PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
147     }
148     if (sample_type & PERF_SAMPLE_CPU) {
149       PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
150     }
151   }
152 }
153 
Size() const154 size_t SampleId::Size() const {
155   size_t size = 0;
156   if (sample_id_all) {
157     if (sample_type & PERF_SAMPLE_TID) {
158       size += sizeof(PerfSampleTidType);
159     }
160     if (sample_type & PERF_SAMPLE_TIME) {
161       size += sizeof(PerfSampleTimeType);
162     }
163     if (sample_type & PERF_SAMPLE_ID) {
164       size += sizeof(PerfSampleIdType);
165     }
166     if (sample_type & PERF_SAMPLE_STREAM_ID) {
167       size += sizeof(PerfSampleStreamIdType);
168     }
169     if (sample_type & PERF_SAMPLE_CPU) {
170       size += sizeof(PerfSampleCpuType);
171     }
172     if (sample_type & PERF_SAMPLE_IDENTIFIER) {
173       size += sizeof(PerfSampleIdType);
174     }
175   }
176   return size;
177 }
178 
Record(Record && other)179 Record::Record(Record&& other) noexcept {
180   header = other.header;
181   sample_id = other.sample_id;
182   binary_ = other.binary_;
183   own_binary_ = other.own_binary_;
184   other.binary_ = nullptr;
185   other.own_binary_ = false;
186 }
187 
Dump(size_t indent) const188 void Record::Dump(size_t indent) const {
189   PrintIndented(indent, "record %s: type %u, misc %u, size %u\n",
190                 RecordTypeToString(type()).c_str(), type(), misc(), size());
191   DumpData(indent + 1);
192   sample_id.Dump(indent + 1);
193 }
194 
Timestamp() const195 uint64_t Record::Timestamp() const {
196   return sample_id.time_data.time;
197 }
Cpu() const198 uint32_t Record::Cpu() const {
199   return sample_id.cpu_data.cpu;
200 }
Id() const201 uint64_t Record::Id() const {
202   return sample_id.id_data.id;
203 }
204 
UpdateBinary(char * new_binary)205 void Record::UpdateBinary(char* new_binary) {
206   if (own_binary_) {
207     delete[] binary_;
208   }
209   own_binary_ = true;
210   binary_ = new_binary;
211 }
212 
MmapRecord(const perf_event_attr & attr,char * p)213 MmapRecord::MmapRecord(const perf_event_attr& attr, char* p) : Record(p) {
214   const char* end = p + size();
215   p += header_size();
216   data = reinterpret_cast<const MmapRecordDataType*>(p);
217   p += sizeof(*data);
218   filename = p;
219   p += Align(strlen(filename) + 1, 8);
220   CHECK_LE(p, end);
221   sample_id.ReadFromBinaryFormat(attr, p, end);
222 }
223 
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)224 MmapRecord::MmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
225                        uint64_t addr, uint64_t len, uint64_t pgoff, const std::string& filename,
226                        uint64_t event_id, uint64_t time) {
227   SetTypeAndMisc(PERF_RECORD_MMAP, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
228   sample_id.CreateContent(attr, event_id);
229   sample_id.time_data.time = time;
230   MmapRecordDataType data;
231   data.pid = pid;
232   data.tid = tid;
233   data.addr = addr;
234   data.len = len;
235   data.pgoff = pgoff;
236   SetDataAndFilename(data, filename);
237 }
238 
SetDataAndFilename(const MmapRecordDataType & data,const std::string & filename)239 void MmapRecord::SetDataAndFilename(const MmapRecordDataType& data, const std::string& filename) {
240   SetSize(header_size() + sizeof(data) + Align(filename.size() + 1, 8) + sample_id.Size());
241   char* new_binary = new char[size()];
242   char* p = new_binary;
243   MoveToBinaryFormat(header, p);
244   this->data = reinterpret_cast<MmapRecordDataType*>(p);
245   MoveToBinaryFormat(data, p);
246   this->filename = p;
247   strcpy(p, filename.c_str());
248   p += Align(filename.size() + 1, 8);
249   sample_id.WriteToBinaryFormat(p);
250   UpdateBinary(new_binary);
251 }
252 
DumpData(size_t indent) const253 void MmapRecord::DumpData(size_t indent) const {
254   PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data->pid,
255                 data->tid, data->addr, data->len);
256   PrintIndented(indent, "pgoff 0x%" PRIx64 ", filename %s\n", data->pgoff, filename);
257 }
258 
Mmap2Record(const perf_event_attr & attr,char * p)259 Mmap2Record::Mmap2Record(const perf_event_attr& attr, char* p) : Record(p) {
260   const char* end = p + size();
261   p += header_size();
262   data = reinterpret_cast<const Mmap2RecordDataType*>(p);
263   p += sizeof(*data);
264   filename = p;
265   p += Align(strlen(filename) + 1, 8);
266   CHECK_LE(p, end);
267   sample_id.ReadFromBinaryFormat(attr, p, end);
268 }
269 
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)270 Mmap2Record::Mmap2Record(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
271                          uint64_t addr, uint64_t len, uint64_t pgoff, uint32_t prot,
272                          const std::string& filename, uint64_t event_id, uint64_t time) {
273   SetTypeAndMisc(PERF_RECORD_MMAP2, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
274   sample_id.CreateContent(attr, event_id);
275   sample_id.time_data.time = time;
276   Mmap2RecordDataType data;
277   data.pid = pid;
278   data.tid = tid;
279   data.addr = addr;
280   data.len = len;
281   data.pgoff = pgoff;
282   data.prot = prot;
283   SetDataAndFilename(data, filename);
284 }
285 
SetDataAndFilename(const Mmap2RecordDataType & data,const std::string & filename)286 void Mmap2Record::SetDataAndFilename(const Mmap2RecordDataType& data, const std::string& filename) {
287   SetSize(header_size() + sizeof(data) + Align(filename.size() + 1, 8) + sample_id.Size());
288   char* new_binary = new char[size()];
289   char* p = new_binary;
290   MoveToBinaryFormat(header, p);
291   this->data = reinterpret_cast<Mmap2RecordDataType*>(p);
292   MoveToBinaryFormat(data, p);
293   this->filename = p;
294   strcpy(p, filename.c_str());
295   p += Align(filename.size() + 1, 8);
296   sample_id.WriteToBinaryFormat(p);
297   UpdateBinary(new_binary);
298 }
299 
DumpData(size_t indent) const300 void Mmap2Record::DumpData(size_t indent) const {
301   PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data->pid,
302                 data->tid, data->addr, data->len);
303   PrintIndented(
304       indent, "pgoff 0x%" PRIx64 ", maj %u, min %u, ino %" PRId64 ", ino_generation %" PRIu64 "\n",
305       data->pgoff, data->maj, data->min, data->ino, data->ino_generation);
306   PrintIndented(indent, "prot %u, flags %u, filename %s\n", data->prot, data->flags, filename);
307 }
308 
CommRecord(const perf_event_attr & attr,char * p)309 CommRecord::CommRecord(const perf_event_attr& attr, char* p) : Record(p) {
310   const char* end = p + size();
311   p += header_size();
312   data = reinterpret_cast<const CommRecordDataType*>(p);
313   p += sizeof(*data);
314   comm = p;
315   p += Align(strlen(p) + 1, 8);
316   CHECK_LE(p, end);
317   sample_id.ReadFromBinaryFormat(attr, p, end);
318 }
319 
CommRecord(const perf_event_attr & attr,uint32_t pid,uint32_t tid,const std::string & comm,uint64_t event_id,uint64_t time)320 CommRecord::CommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
321                        const std::string& comm, uint64_t event_id, uint64_t time) {
322   SetTypeAndMisc(PERF_RECORD_COMM, 0);
323   CommRecordDataType data;
324   data.pid = pid;
325   data.tid = tid;
326   size_t sample_id_size = sample_id.CreateContent(attr, event_id);
327   sample_id.time_data.time = time;
328   SetSize(header_size() + sizeof(data) + Align(comm.size() + 1, 8) + sample_id_size);
329   char* new_binary = new char[size()];
330   char* p = new_binary;
331   MoveToBinaryFormat(header, p);
332   this->data = reinterpret_cast<CommRecordDataType*>(p);
333   MoveToBinaryFormat(data, p);
334   this->comm = p;
335   strcpy(p, comm.c_str());
336   p += Align(comm.size() + 1, 8);
337   sample_id.WriteToBinaryFormat(p);
338   UpdateBinary(new_binary);
339 }
340 
SetCommandName(const std::string & name)341 void CommRecord::SetCommandName(const std::string& name) {
342   if (name.compare(comm) == 0) {
343     return;
344   }
345   // The kernel uses a 8-byte aligned space to store command name. Follow it here to allow the same
346   // reading code.
347   size_t old_name_len = Align(strlen(comm) + 1, 8);
348   size_t new_name_len = Align(name.size() + 1, 8);
349   size_t new_size = size() - old_name_len + new_name_len;
350   char* new_binary = new char[new_size];
351   char* p = new_binary;
352   header.size = new_size;
353   MoveToBinaryFormat(header, p);
354   MoveToBinaryFormat(*data, p);
355   data = reinterpret_cast<CommRecordDataType*>(p - sizeof(CommRecordDataType));
356   comm = p;
357   strcpy(p, name.c_str());
358   p += new_name_len;
359   sample_id.WriteToBinaryFormat(p);
360   CHECK_EQ(p, new_binary + new_size);
361   UpdateBinary(new_binary);
362 }
363 
DumpData(size_t indent) const364 void CommRecord::DumpData(size_t indent) const {
365   PrintIndented(indent, "pid %u, tid %u, comm %s\n", data->pid, data->tid, comm);
366 }
367 
ExitOrForkRecord(const perf_event_attr & attr,char * p)368 ExitOrForkRecord::ExitOrForkRecord(const perf_event_attr& attr, char* p) : Record(p) {
369   const char* end = p + size();
370   p += header_size();
371   data = reinterpret_cast<const ExitOrForkRecordDataType*>(p);
372   p += sizeof(*data);
373   CHECK_LE(p, end);
374   sample_id.ReadFromBinaryFormat(attr, p, end);
375 }
376 
DumpData(size_t indent) const377 void ExitOrForkRecord::DumpData(size_t indent) const {
378   PrintIndented(indent, "pid %u, ppid %u, tid %u, ptid %u\n", data->pid, data->ppid, data->tid,
379                 data->ptid);
380 }
381 
ForkRecord(const perf_event_attr & attr,uint32_t pid,uint32_t tid,uint32_t ppid,uint32_t ptid,uint64_t event_id)382 ForkRecord::ForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
383                        uint32_t ptid, uint64_t event_id) {
384   SetTypeAndMisc(PERF_RECORD_FORK, 0);
385   ExitOrForkRecordDataType data;
386   data.pid = pid;
387   data.ppid = ppid;
388   data.tid = tid;
389   data.ptid = ptid;
390   data.time = 0;
391   size_t sample_id_size = sample_id.CreateContent(attr, event_id);
392   SetSize(header_size() + sizeof(data) + sample_id_size);
393   char* new_binary = new char[size()];
394   char* p = new_binary;
395   MoveToBinaryFormat(header, p);
396   this->data = reinterpret_cast<ExitOrForkRecordDataType*>(p);
397   MoveToBinaryFormat(data, p);
398   sample_id.WriteToBinaryFormat(p);
399   UpdateBinary(new_binary);
400 }
401 
LostRecord(const perf_event_attr & attr,char * p)402 LostRecord::LostRecord(const perf_event_attr& attr, char* p) : Record(p) {
403   const char* end = p + size();
404   p += header_size();
405   MoveFromBinaryFormat(id, p);
406   MoveFromBinaryFormat(lost, p);
407   CHECK_LE(p, end);
408   sample_id.ReadFromBinaryFormat(attr, p, end);
409 }
410 
DumpData(size_t indent) const411 void LostRecord::DumpData(size_t indent) const {
412   PrintIndented(indent, "id %" PRIu64 ", lost %" PRIu64 "\n", id, lost);
413 }
414 
SampleRecord(const perf_event_attr & attr,char * p)415 SampleRecord::SampleRecord(const perf_event_attr& attr, char* p) : Record(p) {
416   const char* end = p + size();
417   p += header_size();
418   sample_type = attr.sample_type;
419 
420   // Set a default id value to report correctly even if ID is not recorded.
421   id_data.id = 0;
422   if (sample_type & PERF_SAMPLE_IDENTIFIER) {
423     MoveFromBinaryFormat(id_data, p);
424   }
425   if (sample_type & PERF_SAMPLE_IP) {
426     MoveFromBinaryFormat(ip_data, p);
427   }
428   if (sample_type & PERF_SAMPLE_TID) {
429     MoveFromBinaryFormat(tid_data, p);
430   }
431   if (sample_type & PERF_SAMPLE_TIME) {
432     MoveFromBinaryFormat(time_data, p);
433   }
434   if (sample_type & PERF_SAMPLE_ADDR) {
435     MoveFromBinaryFormat(addr_data, p);
436   }
437   if (sample_type & PERF_SAMPLE_ID) {
438     MoveFromBinaryFormat(id_data, p);
439   }
440   if (sample_type & PERF_SAMPLE_STREAM_ID) {
441     MoveFromBinaryFormat(stream_id_data, p);
442   }
443   if (sample_type & PERF_SAMPLE_CPU) {
444     MoveFromBinaryFormat(cpu_data, p);
445   }
446   if (sample_type & PERF_SAMPLE_PERIOD) {
447     MoveFromBinaryFormat(period_data, p);
448   }
449   if (sample_type & PERF_SAMPLE_CALLCHAIN) {
450     MoveFromBinaryFormat(callchain_data.ip_nr, p);
451     callchain_data.ips = reinterpret_cast<uint64_t*>(p);
452     p += callchain_data.ip_nr * sizeof(uint64_t);
453   }
454   if (sample_type & PERF_SAMPLE_RAW) {
455     MoveFromBinaryFormat(raw_data.size, p);
456     raw_data.data = p;
457     p += raw_data.size;
458   }
459   if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
460     MoveFromBinaryFormat(branch_stack_data.stack_nr, p);
461     branch_stack_data.stack = reinterpret_cast<BranchStackItemType*>(p);
462     p += branch_stack_data.stack_nr * sizeof(BranchStackItemType);
463   }
464   if (sample_type & PERF_SAMPLE_REGS_USER) {
465     MoveFromBinaryFormat(regs_user_data.abi, p);
466     if (regs_user_data.abi == 0) {
467       regs_user_data.reg_mask = 0;
468     } else {
469       regs_user_data.reg_mask = attr.sample_regs_user;
470       size_t bit_nr = __builtin_popcountll(regs_user_data.reg_mask);
471       regs_user_data.reg_nr = bit_nr;
472       regs_user_data.regs = reinterpret_cast<uint64_t*>(p);
473       p += bit_nr * sizeof(uint64_t);
474     }
475   }
476   if (sample_type & PERF_SAMPLE_STACK_USER) {
477     MoveFromBinaryFormat(stack_user_data.size, p);
478     if (stack_user_data.size == 0) {
479       stack_user_data.dyn_size = 0;
480     } else {
481       stack_user_data.data = p;
482       p += stack_user_data.size;
483       MoveFromBinaryFormat(stack_user_data.dyn_size, p);
484     }
485   }
486   // TODO: Add parsing of other PERF_SAMPLE_*.
487   CHECK_LE(p, end);
488   if (p < end) {
489     LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
490   }
491 }
492 
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 std::vector<uint64_t> & ips,const std::vector<char> & stack,uint64_t dyn_stack_size)493 SampleRecord::SampleRecord(const perf_event_attr& attr, uint64_t id, uint64_t ip, uint32_t pid,
494                            uint32_t tid, uint64_t time, uint32_t cpu, uint64_t period,
495                            const std::vector<uint64_t>& ips, const std::vector<char>& stack,
496                            uint64_t dyn_stack_size) {
497   SetTypeAndMisc(PERF_RECORD_SAMPLE, PERF_RECORD_MISC_USER);
498   sample_type = attr.sample_type;
499   CHECK_EQ(0u,
500            sample_type & ~(PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ID |
501                            PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | PERF_SAMPLE_CALLCHAIN |
502                            PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER));
503   ip_data.ip = ip;
504   tid_data.pid = pid;
505   tid_data.tid = tid;
506   time_data.time = time;
507   id_data.id = id;
508   cpu_data.cpu = cpu;
509   cpu_data.res = 0;
510   period_data.period = period;
511   callchain_data.ip_nr = ips.size();
512   raw_data.size = 0;
513   branch_stack_data.stack_nr = 0;
514   regs_user_data.abi = 0;
515   regs_user_data.reg_mask = 0;
516   regs_user_data.reg_nr = 0;
517   stack_user_data.size = stack.size();
518   stack_user_data.dyn_size = dyn_stack_size;
519 
520   uint32_t size = header_size();
521   if (sample_type & PERF_SAMPLE_IP) {
522     size += sizeof(ip_data);
523   }
524   if (sample_type & PERF_SAMPLE_TID) {
525     size += sizeof(tid_data);
526   }
527   if (sample_type & PERF_SAMPLE_TIME) {
528     size += sizeof(time_data);
529   }
530   if (sample_type & PERF_SAMPLE_ID) {
531     size += sizeof(id_data);
532   }
533   if (sample_type & PERF_SAMPLE_CPU) {
534     size += sizeof(cpu_data);
535   }
536   if (sample_type & PERF_SAMPLE_PERIOD) {
537     size += sizeof(period_data);
538   }
539   if (sample_type & PERF_SAMPLE_CALLCHAIN) {
540     size += sizeof(uint64_t) * (ips.size() + 1);
541   }
542   if (sample_type & PERF_SAMPLE_REGS_USER) {
543     size += sizeof(uint64_t);
544   }
545   if (sample_type & PERF_SAMPLE_STACK_USER) {
546     size += sizeof(uint64_t) + (stack.empty() ? 0 : stack.size() + sizeof(uint64_t));
547   }
548 
549   SetSize(size);
550   char* new_binary = new char[size];
551   char* p = new_binary;
552   MoveToBinaryFormat(header, p);
553   if (sample_type & PERF_SAMPLE_IP) {
554     MoveToBinaryFormat(ip_data, p);
555   }
556   if (sample_type & PERF_SAMPLE_TID) {
557     MoveToBinaryFormat(tid_data, p);
558   }
559   if (sample_type & PERF_SAMPLE_TIME) {
560     MoveToBinaryFormat(time_data, p);
561   }
562   if (sample_type & PERF_SAMPLE_ID) {
563     MoveToBinaryFormat(id_data, p);
564   }
565   if (sample_type & PERF_SAMPLE_CPU) {
566     MoveToBinaryFormat(cpu_data, p);
567   }
568   if (sample_type & PERF_SAMPLE_PERIOD) {
569     MoveToBinaryFormat(period_data, p);
570   }
571   if (sample_type & PERF_SAMPLE_CALLCHAIN) {
572     MoveToBinaryFormat(callchain_data.ip_nr, p);
573     callchain_data.ips = reinterpret_cast<uint64_t*>(p);
574     MoveToBinaryFormat(ips.data(), ips.size(), p);
575   }
576   if (sample_type & PERF_SAMPLE_REGS_USER) {
577     MoveToBinaryFormat(regs_user_data.abi, p);
578   }
579   if (sample_type & PERF_SAMPLE_STACK_USER) {
580     MoveToBinaryFormat(stack_user_data.size, p);
581     if (stack_user_data.size > 0) {
582       stack_user_data.data = p;
583       MoveToBinaryFormat(stack.data(), stack_user_data.size, p);
584       MoveToBinaryFormat(stack_user_data.dyn_size, p);
585     }
586   }
587   CHECK_EQ(p, new_binary + size);
588   UpdateBinary(new_binary);
589 }
590 
ReplaceRegAndStackWithCallChain(const std::vector<uint64_t> & ips)591 void SampleRecord::ReplaceRegAndStackWithCallChain(const std::vector<uint64_t>& ips) {
592   uint32_t size_added_in_callchain = sizeof(uint64_t) * (ips.size() + 1);
593   uint32_t size_reduced_in_reg_stack =
594       regs_user_data.reg_nr * sizeof(uint64_t) + stack_user_data.size + sizeof(uint64_t);
595   uint32_t new_size = size() + size_added_in_callchain - size_reduced_in_reg_stack;
596   BuildBinaryWithNewCallChain(new_size, ips);
597 }
598 
ExcludeKernelCallChain()599 bool SampleRecord::ExcludeKernelCallChain() {
600   if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
601     return true;
602   }
603   size_t i;
604   for (i = 0; i < callchain_data.ip_nr; ++i) {
605     if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
606       break;
607     }
608     // Erase kernel callchain.
609     callchain_data.ips[i] = PERF_CONTEXT_USER;
610   }
611   while (++i < callchain_data.ip_nr) {
612     if (callchain_data.ips[i] < PERF_CONTEXT_MAX) {
613       // Change the sample to make it hit the user space ip address.
614       ip_data.ip = callchain_data.ips[i];
615       if (sample_type & PERF_SAMPLE_IP) {
616         *reinterpret_cast<uint64_t*>(binary_ + header_size()) = ip_data.ip;
617       }
618       header.misc = (header.misc & ~PERF_RECORD_MISC_CPUMODE_MASK) | PERF_RECORD_MISC_USER;
619       reinterpret_cast<perf_event_header*>(binary_)->misc = header.misc;
620       return true;
621     }
622   }
623   return false;
624 }
625 
HasUserCallChain() const626 bool SampleRecord::HasUserCallChain() const {
627   if ((sample_type & PERF_SAMPLE_CALLCHAIN) == 0) {
628     return false;
629   }
630   bool in_user_context = !InKernel();
631   for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
632     if (in_user_context && callchain_data.ips[i] < PERF_CONTEXT_MAX) {
633       return true;
634     }
635     if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
636       in_user_context = true;
637     }
638   }
639   return false;
640 }
641 
UpdateUserCallChain(const std::vector<uint64_t> & user_ips)642 void SampleRecord::UpdateUserCallChain(const std::vector<uint64_t>& user_ips) {
643   size_t kernel_ip_count = 0;
644   for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
645     if (callchain_data.ips[i] == PERF_CONTEXT_USER) {
646       break;
647     }
648     kernel_ip_count++;
649   }
650   if (kernel_ip_count + 1 + user_ips.size() <= callchain_data.ip_nr) {
651     // Callchain isn't changed.
652     return;
653   }
654   size_t new_size =
655       size() + (kernel_ip_count + 1 + user_ips.size() - callchain_data.ip_nr) * sizeof(uint64_t);
656   callchain_data.ip_nr = kernel_ip_count;
657   BuildBinaryWithNewCallChain(new_size, user_ips);
658 }
659 
BuildBinaryWithNewCallChain(uint32_t new_size,const std::vector<uint64_t> & ips)660 void SampleRecord::BuildBinaryWithNewCallChain(uint32_t new_size,
661                                                const std::vector<uint64_t>& ips) {
662   size_t callchain_pos = reinterpret_cast<char*>(callchain_data.ips) - binary_ - sizeof(uint64_t);
663   char* new_binary = binary_;
664   if (new_size > size()) {
665     new_binary = new char[new_size];
666     memcpy(new_binary, binary_, callchain_pos);
667   }
668   char* p = new_binary;
669   SetSize(new_size);
670   MoveToBinaryFormat(header, p);
671   p = new_binary + new_size;
672   if (sample_type & PERF_SAMPLE_STACK_USER) {
673     stack_user_data.size = 0;
674     p -= sizeof(uint64_t);
675     memcpy(p, &stack_user_data.size, sizeof(uint64_t));
676   }
677   if (sample_type & PERF_SAMPLE_REGS_USER) {
678     regs_user_data.abi = 0;
679     p -= sizeof(uint64_t);
680     memcpy(p, &regs_user_data.abi, sizeof(uint64_t));
681   }
682   if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
683     p -= branch_stack_data.stack_nr * sizeof(BranchStackItemType);
684     memcpy(p, branch_stack_data.stack, branch_stack_data.stack_nr * sizeof(BranchStackItemType));
685     branch_stack_data.stack = reinterpret_cast<BranchStackItemType*>(p);
686     p -= sizeof(uint64_t);
687     memcpy(p, &branch_stack_data.stack_nr, sizeof(uint64_t));
688   }
689   if (sample_type & PERF_SAMPLE_RAW) {
690     p -= raw_data.size;
691     memcpy(p, raw_data.data, raw_data.size);
692     raw_data.data = p;
693     p -= sizeof(uint32_t);
694     memcpy(p, &raw_data.size, sizeof(uint32_t));
695   }
696   uint64_t* p64 = reinterpret_cast<uint64_t*>(p);
697   p64 -= ips.size();
698   memcpy(p64, ips.data(), ips.size() * sizeof(uint64_t));
699   *--p64 = PERF_CONTEXT_USER;
700   if (callchain_data.ip_nr > 0) {
701     p64 -= callchain_data.ip_nr;
702     memcpy(p64, callchain_data.ips, callchain_data.ip_nr * sizeof(uint64_t));
703   }
704   callchain_data.ips = p64;
705   callchain_data.ip_nr += 1 + ips.size();
706   *--p64 = callchain_data.ip_nr;
707   CHECK_EQ(callchain_pos, static_cast<size_t>(reinterpret_cast<char*>(p64) - new_binary))
708       << "record time " << time_data.time;
709   if (new_binary != binary_) {
710     UpdateBinary(new_binary);
711   }
712 }
713 
DumpData(size_t indent) const714 void SampleRecord::DumpData(size_t indent) const {
715   PrintIndented(indent, "sample_type: 0x%" PRIx64 "\n", sample_type);
716   if (sample_type & PERF_SAMPLE_IP) {
717     PrintIndented(indent, "ip %p\n", reinterpret_cast<void*>(ip_data.ip));
718   }
719   if (sample_type & PERF_SAMPLE_TID) {
720     PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
721   }
722   if (sample_type & PERF_SAMPLE_TIME) {
723     PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
724   }
725   if (sample_type & PERF_SAMPLE_ADDR) {
726     PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
727   }
728   if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) {
729     PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
730   }
731   if (sample_type & PERF_SAMPLE_STREAM_ID) {
732     PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
733   }
734   if (sample_type & PERF_SAMPLE_CPU) {
735     PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
736   }
737   if (sample_type & PERF_SAMPLE_PERIOD) {
738     PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
739   }
740   if (sample_type & PERF_SAMPLE_CALLCHAIN) {
741     PrintIndented(indent, "callchain nr=%" PRIu64 "\n", callchain_data.ip_nr);
742     for (uint64_t i = 0; i < callchain_data.ip_nr; ++i) {
743       PrintIndented(indent + 1, "0x%" PRIx64 "\n", callchain_data.ips[i]);
744     }
745   }
746   if (sample_type & PERF_SAMPLE_RAW) {
747     PrintIndented(indent, "raw size=%zu\n", raw_data.size);
748     const uint32_t* data = reinterpret_cast<const uint32_t*>(raw_data.data);
749     size_t size = raw_data.size / sizeof(uint32_t);
750     for (size_t i = 0; i < size; ++i) {
751       PrintIndented(indent + 1, "0x%08x (%zu)\n", data[i], data[i]);
752     }
753   }
754   if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
755     PrintIndented(indent, "branch_stack nr=%" PRIu64 "\n", branch_stack_data.stack_nr);
756     for (uint64_t i = 0; i < branch_stack_data.stack_nr; ++i) {
757       auto& item = branch_stack_data.stack[i];
758       PrintIndented(indent + 1, "from 0x%" PRIx64 ", to 0x%" PRIx64 ", flags 0x%" PRIx64 "\n",
759                     item.from, item.to, item.flags);
760     }
761   }
762   if (sample_type & PERF_SAMPLE_REGS_USER) {
763     PrintIndented(indent, "user regs: abi=%" PRId64 "\n", regs_user_data.abi);
764     RegSet regs(regs_user_data.abi, regs_user_data.reg_mask, regs_user_data.regs);
765     for (size_t i = 0; i < 64; ++i) {
766       uint64_t value;
767       if (regs.GetRegValue(i, &value)) {
768         PrintIndented(indent + 1, "reg (%s) 0x%016" PRIx64 "\n", GetRegName(i, regs.arch).c_str(),
769                       value);
770       }
771     }
772   }
773   if (sample_type & PERF_SAMPLE_STACK_USER) {
774     PrintIndented(indent, "user stack: size %zu dyn_size %" PRIu64 "\n", stack_user_data.size,
775                   stack_user_data.dyn_size);
776     const uint64_t* p = reinterpret_cast<const uint64_t*>(stack_user_data.data);
777     const uint64_t* end = p + (stack_user_data.size / sizeof(uint64_t));
778     while (p < end) {
779       PrintIndented(indent + 1, "");
780       for (size_t i = 0; i < 4 && p < end; ++i, ++p) {
781         printf(" %016" PRIx64, *p);
782       }
783       printf("\n");
784     }
785     printf("\n");
786   }
787 }
788 
Timestamp() const789 uint64_t SampleRecord::Timestamp() const {
790   return time_data.time;
791 }
Cpu() const792 uint32_t SampleRecord::Cpu() const {
793   return cpu_data.cpu;
794 }
Id() const795 uint64_t SampleRecord::Id() const {
796   return id_data.id;
797 }
798 
AdjustCallChainGeneratedByKernel()799 void SampleRecord::AdjustCallChainGeneratedByKernel() {
800   // The kernel stores return addrs in the callchain, but we want the addrs of call instructions
801   // along the callchain.
802   uint64_t* ips = callchain_data.ips;
803   uint64_t context =
804       header.misc == PERF_RECORD_MISC_KERNEL ? PERF_CONTEXT_KERNEL : PERF_CONTEXT_USER;
805   bool first_frame = true;
806   for (size_t i = 0; i < callchain_data.ip_nr; ++i) {
807     if (ips[i] < PERF_CONTEXT_MAX) {
808       if (first_frame) {
809         first_frame = false;
810       } else {
811         if (ips[i] < 2) {
812           // A wrong ip address, erase it.
813           ips[i] = context;
814         } else {
815           // Here we want to change the return addr to the addr of the previous instruction. We
816           // don't need to find the exact start addr of the previous instruction. A location in
817           // [start_addr_of_call_inst, start_addr_of_next_inst) is enough.
818 #if defined(__arm__) || defined(__aarch64__)
819           // If we are built for arm/aarch64, this may be a callchain of thumb code. For thumb code,
820           // the real instruction addr is (ip & ~1), and ip - 2 can used to hit the address range
821           // of the previous instruction. For non thumb code, any addr in [ip - 4, ip - 1] is fine.
822           ips[i] -= 2;
823 #else
824           ips[i]--;
825 #endif
826         }
827       }
828     } else {
829       context = ips[i];
830     }
831   }
832 }
833 
GetCallChain(size_t * kernel_ip_count) const834 std::vector<uint64_t> SampleRecord::GetCallChain(size_t* kernel_ip_count) const {
835   std::vector<uint64_t> ips;
836   bool in_kernel = InKernel();
837   ips.push_back(ip_data.ip);
838   *kernel_ip_count = in_kernel ? 1 : 0;
839   if ((sample_type & PERF_SAMPLE_CALLCHAIN) == 0) {
840     return ips;
841   }
842   bool first_ip = true;
843   for (uint64_t i = 0; i < callchain_data.ip_nr; ++i) {
844     uint64_t ip = callchain_data.ips[i];
845     if (ip >= PERF_CONTEXT_MAX) {
846       switch (ip) {
847         case PERF_CONTEXT_KERNEL:
848           in_kernel = true;
849           break;
850         case PERF_CONTEXT_USER:
851           in_kernel = false;
852           break;
853         default:
854           LOG(DEBUG) << "Unexpected perf_context in callchain: " << std::hex << ip << std::dec;
855       }
856     } else {
857       if (first_ip) {
858         first_ip = false;
859         // Remove duplication with sample ip.
860         if (ip == ip_data.ip) {
861           continue;
862         }
863       }
864       ips.push_back(ip);
865       if (in_kernel) {
866         ++*kernel_ip_count;
867       }
868     }
869   }
870   return ips;
871 }
872 
AuxRecord(const perf_event_attr & attr,char * p)873 AuxRecord::AuxRecord(const perf_event_attr& attr, char* p) : Record(p) {
874   const char* end = p + size();
875   p += header_size();
876   data = reinterpret_cast<DataType*>(p);
877   p += sizeof(DataType);
878   sample_id.ReadFromBinaryFormat(attr, p, end);
879 }
880 
DumpData(size_t indent) const881 void AuxRecord::DumpData(size_t indent) const {
882   PrintIndented(indent, "aux_offset %" PRIu64 "\n", data->aux_offset);
883   PrintIndented(indent, "aux_size %" PRIu64 "\n", data->aux_size);
884   PrintIndented(indent, "flags 0x%" PRIx64 "\n", data->flags);
885 }
886 
BuildIdRecord(char * p)887 BuildIdRecord::BuildIdRecord(char* p) : Record(p) {
888   const char* end = p + size();
889   p += header_size();
890   MoveFromBinaryFormat(pid, p);
891   build_id = BuildId(p, BUILD_ID_SIZE);
892   p += Align(build_id.Size(), 8);
893   filename = p;
894   p += Align(strlen(filename) + 1, 64);
895   CHECK_EQ(p, end);
896 }
897 
DumpData(size_t indent) const898 void BuildIdRecord::DumpData(size_t indent) const {
899   PrintIndented(indent, "pid %u\n", pid);
900   PrintIndented(indent, "build_id %s\n", build_id.ToString().c_str());
901   PrintIndented(indent, "filename %s\n", filename);
902 }
903 
BuildIdRecord(bool in_kernel,uint32_t pid,const BuildId & build_id,const std::string & filename)904 BuildIdRecord::BuildIdRecord(bool in_kernel, uint32_t pid, const BuildId& build_id,
905                              const std::string& filename) {
906   SetTypeAndMisc(PERF_RECORD_BUILD_ID, in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
907   this->pid = pid;
908   this->build_id = build_id;
909   SetSize(header_size() + sizeof(this->pid) + Align(build_id.Size(), 8) +
910           Align(filename.size() + 1, 64));
911   char* new_binary = new char[size()];
912   char* p = new_binary;
913   MoveToBinaryFormat(header, p);
914   MoveToBinaryFormat(this->pid, p);
915   memcpy(p, build_id.Data(), build_id.Size());
916   p += Align(build_id.Size(), 8);
917   this->filename = p;
918   strcpy(p, filename.c_str());
919   UpdateBinary(new_binary);
920 }
921 
AuxTraceInfoRecord(char * p)922 AuxTraceInfoRecord::AuxTraceInfoRecord(char* p) : Record(p) {
923   const char* end = p + size();
924   p += header_size();
925   data = reinterpret_cast<DataType*>(p);
926   CHECK_EQ(data->aux_type, AUX_TYPE_ETM);
927   CHECK_EQ(data->version, 0);
928   for (uint32_t i = 0; i < data->nr_cpu; ++i) {
929     CHECK_EQ(data->etm4_info[i].magic, MAGIC_ETM4);
930   }
931   p += sizeof(DataType) + data->nr_cpu * sizeof(ETM4Info);
932   CHECK_EQ(p, end);
933 }
934 
AuxTraceInfoRecord(const DataType & data,const std::vector<ETM4Info> & etm4_info)935 AuxTraceInfoRecord::AuxTraceInfoRecord(const DataType& data,
936                                        const std::vector<ETM4Info>& etm4_info) {
937   SetTypeAndMisc(PERF_RECORD_AUXTRACE_INFO, 0);
938   SetSize(header_size() + sizeof(DataType) + sizeof(ETM4Info) * etm4_info.size());
939   char* new_binary = new char[size()];
940   char* p = new_binary;
941   MoveToBinaryFormat(header, p);
942   this->data = reinterpret_cast<DataType*>(p);
943   MoveToBinaryFormat(data, p);
944   for (auto& etm4 : etm4_info) {
945     MoveToBinaryFormat(etm4, p);
946   }
947   UpdateBinary(new_binary);
948 }
949 
DumpData(size_t indent) const950 void AuxTraceInfoRecord::DumpData(size_t indent) const {
951   PrintIndented(indent, "aux_type %u\n", data->aux_type);
952   PrintIndented(indent, "version %" PRIu64 "\n", data->version);
953   PrintIndented(indent, "nr_cpu %u\n", data->nr_cpu);
954   PrintIndented(indent, "pmu_type %u\n", data->pmu_type);
955   PrintIndented(indent, "snapshot %" PRIu64 "\n", data->snapshot);
956   indent++;
957   for (int i = 0; i < data->nr_cpu; i++) {
958     const ETM4Info& e = data->etm4_info[i];
959     PrintIndented(indent, "magic 0x%" PRIx64 "\n", e.magic);
960     PrintIndented(indent, "cpu %" PRIu64 "\n", e.cpu);
961     PrintIndented(indent, "trcconfigr 0x%" PRIx64 "\n", e.trcconfigr);
962     PrintIndented(indent, "trctraceidr 0x%" PRIx64 "\n", e.trctraceidr);
963     PrintIndented(indent, "trcidr0 0x%" PRIx64 "\n", e.trcidr0);
964     PrintIndented(indent, "trcidr1 0x%" PRIx64 "\n", e.trcidr1);
965     PrintIndented(indent, "trcidr2 0x%" PRIx64 "\n", e.trcidr2);
966     PrintIndented(indent, "trcidr8 0x%" PRIx64 "\n", e.trcidr8);
967     PrintIndented(indent, "trcauthstatus 0x%" PRIx64 "\n", e.trcauthstatus);
968   }
969 }
970 
AuxTraceRecord(char * p)971 AuxTraceRecord::AuxTraceRecord(char* p) : Record(p) {
972   const char* end = p + header.size;
973   p += header_size();
974   data = reinterpret_cast<DataType*>(p);
975   p += sizeof(DataType);
976   CHECK_EQ(p, end);
977 }
978 
AuxTraceRecord(uint64_t aux_size,uint64_t offset,uint32_t idx,uint32_t tid,uint32_t cpu)979 AuxTraceRecord::AuxTraceRecord(uint64_t aux_size, uint64_t offset, uint32_t idx, uint32_t tid,
980                                uint32_t cpu) {
981   SetTypeAndMisc(PERF_RECORD_AUXTRACE, 0);
982   SetSize(header_size() + sizeof(DataType));
983   char* new_binary = new char[size()];
984   char* p = new_binary;
985   MoveToBinaryFormat(header, p);
986   data = reinterpret_cast<DataType*>(p);
987   data->aux_size = aux_size;
988   data->offset = offset;
989   data->reserved0 = 0;
990   data->idx = idx;
991   data->tid = tid;
992   data->cpu = cpu;
993   data->reserved1 = 0;
994   UpdateBinary(new_binary);
995 }
996 
DumpData(size_t indent) const997 void AuxTraceRecord::DumpData(size_t indent) const {
998   PrintIndented(indent, "aux_size %" PRIu64 "\n", data->aux_size);
999   PrintIndented(indent, "offset %" PRIu64 "\n", data->offset);
1000   PrintIndented(indent, "idx %u\n", data->idx);
1001   PrintIndented(indent, "tid %u\n", data->tid);
1002   PrintIndented(indent, "cpu %u\n", data->cpu);
1003   PrintIndented(indent, "location.file_offset %" PRIu64 "\n", location.file_offset);
1004 }
1005 
KernelSymbolRecord(char * p)1006 KernelSymbolRecord::KernelSymbolRecord(char* p) : Record(p) {
1007   const char* end = p + size();
1008   p += header_size();
1009   MoveFromBinaryFormat(kallsyms_size, p);
1010   kallsyms = p;
1011   p += Align(kallsyms_size, 8);
1012   CHECK_EQ(p, end);
1013 }
1014 
DumpData(size_t indent) const1015 void KernelSymbolRecord::DumpData(size_t indent) const {
1016   PrintIndented(indent, "kallsyms: %s\n", std::string(kallsyms, kallsyms + kallsyms_size).c_str());
1017 }
1018 
KernelSymbolRecord(const std::string & kallsyms)1019 KernelSymbolRecord::KernelSymbolRecord(const std::string& kallsyms) {
1020   SetTypeAndMisc(SIMPLE_PERF_RECORD_KERNEL_SYMBOL, 0);
1021   kallsyms_size = kallsyms.size();
1022   SetSize(header_size() + 4 + Align(kallsyms.size(), 8));
1023   char* new_binary = new char[size()];
1024   char* p = new_binary;
1025   MoveToBinaryFormat(header, p);
1026   MoveToBinaryFormat(kallsyms_size, p);
1027   this->kallsyms = p;
1028   memcpy(p, kallsyms.data(), kallsyms_size);
1029   UpdateBinary(new_binary);
1030 }
1031 
DsoRecord(char * p)1032 DsoRecord::DsoRecord(char* p) : Record(p) {
1033   const char* end = p + size();
1034   p += header_size();
1035   MoveFromBinaryFormat(dso_type, p);
1036   MoveFromBinaryFormat(dso_id, p);
1037   MoveFromBinaryFormat(min_vaddr, p);
1038   dso_name = p;
1039   p += Align(strlen(dso_name) + 1, 8);
1040   CHECK_EQ(p, end);
1041 }
1042 
DsoRecord(uint64_t dso_type,uint64_t dso_id,const std::string & dso_name,uint64_t min_vaddr)1043 DsoRecord::DsoRecord(uint64_t dso_type, uint64_t dso_id, const std::string& dso_name,
1044                      uint64_t min_vaddr) {
1045   SetTypeAndMisc(SIMPLE_PERF_RECORD_DSO, 0);
1046   this->dso_type = dso_type;
1047   this->dso_id = dso_id;
1048   this->min_vaddr = min_vaddr;
1049   SetSize(header_size() + 3 * sizeof(uint64_t) + Align(dso_name.size() + 1, 8));
1050   char* new_binary = new char[size()];
1051   char* p = new_binary;
1052   MoveToBinaryFormat(header, p);
1053   MoveToBinaryFormat(dso_type, p);
1054   MoveToBinaryFormat(dso_id, p);
1055   MoveToBinaryFormat(min_vaddr, p);
1056   this->dso_name = p;
1057   strcpy(p, dso_name.c_str());
1058   UpdateBinary(new_binary);
1059 }
1060 
DumpData(size_t indent) const1061 void DsoRecord::DumpData(size_t indent) const {
1062   PrintIndented(indent, "dso_type: %s(%" PRIu64 ")\n",
1063                 DsoTypeToString(static_cast<DsoType>(dso_type)), dso_type);
1064   PrintIndented(indent, "dso_id: %" PRIu64 "\n", dso_id);
1065   PrintIndented(indent, "min_vaddr: 0x%" PRIx64 "\n", min_vaddr);
1066   PrintIndented(indent, "dso_name: %s\n", dso_name);
1067 }
1068 
SymbolRecord(char * p)1069 SymbolRecord::SymbolRecord(char* p) : Record(p) {
1070   const char* end = p + size();
1071   p += header_size();
1072   MoveFromBinaryFormat(addr, p);
1073   MoveFromBinaryFormat(len, p);
1074   MoveFromBinaryFormat(dso_id, p);
1075   name = p;
1076   p += Align(strlen(name) + 1, 8);
1077   CHECK_EQ(p, end);
1078 }
1079 
SymbolRecord(uint64_t addr,uint64_t len,const std::string & name,uint64_t dso_id)1080 SymbolRecord::SymbolRecord(uint64_t addr, uint64_t len, const std::string& name, uint64_t dso_id) {
1081   SetTypeAndMisc(SIMPLE_PERF_RECORD_SYMBOL, 0);
1082   this->addr = addr;
1083   this->len = len;
1084   this->dso_id = dso_id;
1085   SetSize(header_size() + 3 * sizeof(uint64_t) + Align(name.size() + 1, 8));
1086   char* new_binary = new char[size()];
1087   char* p = new_binary;
1088   MoveToBinaryFormat(header, p);
1089   MoveToBinaryFormat(addr, p);
1090   MoveToBinaryFormat(len, p);
1091   MoveToBinaryFormat(dso_id, p);
1092   this->name = p;
1093   strcpy(p, name.c_str());
1094   UpdateBinary(new_binary);
1095 }
1096 
DumpData(size_t indent) const1097 void SymbolRecord::DumpData(size_t indent) const {
1098   PrintIndented(indent, "name: %s\n", name);
1099   PrintIndented(indent, "addr: 0x%" PRIx64 "\n", addr);
1100   PrintIndented(indent, "len: 0x%" PRIx64 "\n", len);
1101   PrintIndented(indent, "dso_id: %" PRIu64 "\n", dso_id);
1102 }
1103 
TracingDataRecord(char * p)1104 TracingDataRecord::TracingDataRecord(char* p) : Record(p) {
1105   const char* end = p + size();
1106   p += header_size();
1107   MoveFromBinaryFormat(data_size, p);
1108   data = p;
1109   p += Align(data_size, 64);
1110   CHECK_EQ(p, end);
1111 }
1112 
TracingDataRecord(const std::vector<char> & tracing_data)1113 TracingDataRecord::TracingDataRecord(const std::vector<char>& tracing_data) {
1114   SetTypeAndMisc(SIMPLE_PERF_RECORD_TRACING_DATA, 0);
1115   data_size = tracing_data.size();
1116   SetSize(header_size() + sizeof(uint32_t) + Align(tracing_data.size(), 64));
1117   char* new_binary = new char[size()];
1118   char* p = new_binary;
1119   MoveToBinaryFormat(header, p);
1120   MoveToBinaryFormat(data_size, p);
1121   data = p;
1122   memcpy(p, tracing_data.data(), data_size);
1123   UpdateBinary(new_binary);
1124 }
1125 
DumpData(size_t indent) const1126 void TracingDataRecord::DumpData(size_t indent) const {
1127   Tracing tracing(std::vector<char>(data, data + data_size));
1128   tracing.Dump(indent);
1129 }
1130 
EventIdRecord(char * p)1131 EventIdRecord::EventIdRecord(char* p) : Record(p) {
1132   const char* end = p + size();
1133   p += header_size();
1134   MoveFromBinaryFormat(count, p);
1135   data = reinterpret_cast<const EventIdData*>(p);
1136   p += sizeof(data[0]) * count;
1137   CHECK_EQ(p, end);
1138 }
1139 
EventIdRecord(const std::vector<uint64_t> & data)1140 EventIdRecord::EventIdRecord(const std::vector<uint64_t>& data) {
1141   SetTypeAndMisc(SIMPLE_PERF_RECORD_EVENT_ID, 0);
1142   SetSize(header_size() + sizeof(uint64_t) * (1 + data.size()));
1143   char* new_binary = new char[size()];
1144   char* p = new_binary;
1145   MoveToBinaryFormat(header, p);
1146   count = data.size() / 2;
1147   MoveToBinaryFormat(count, p);
1148   this->data = reinterpret_cast<EventIdData*>(p);
1149   memcpy(p, data.data(), sizeof(uint64_t) * data.size());
1150   UpdateBinary(new_binary);
1151 }
1152 
DumpData(size_t indent) const1153 void EventIdRecord::DumpData(size_t indent) const {
1154   PrintIndented(indent, "count: %" PRIu64 "\n", count);
1155   for (size_t i = 0; i < count; ++i) {
1156     PrintIndented(indent, "attr_id[%" PRIu64 "]: %" PRIu64 "\n", i, data[i].attr_id);
1157     PrintIndented(indent, "event_id[%" PRIu64 "]: %" PRIu64 "\n", i, data[i].event_id);
1158   }
1159 }
1160 
CallChainRecord(char * p)1161 CallChainRecord::CallChainRecord(char* p) : Record(p) {
1162   const char* end = p + size();
1163   p += header_size();
1164   MoveFromBinaryFormat(pid, p);
1165   MoveFromBinaryFormat(tid, p);
1166   MoveFromBinaryFormat(chain_type, p);
1167   MoveFromBinaryFormat(time, p);
1168   MoveFromBinaryFormat(ip_nr, p);
1169   ips = reinterpret_cast<uint64_t*>(p);
1170   p += ip_nr * sizeof(uint64_t);
1171   sps = reinterpret_cast<uint64_t*>(p);
1172   p += ip_nr * sizeof(uint64_t);
1173   CHECK_EQ(p, end);
1174 }
1175 
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)1176 CallChainRecord::CallChainRecord(pid_t pid, pid_t tid, CallChainJoiner::ChainType type,
1177                                  uint64_t time, const std::vector<uint64_t>& ips,
1178                                  const std::vector<uint64_t>& sps) {
1179   CHECK_EQ(ips.size(), sps.size());
1180   SetTypeAndMisc(SIMPLE_PERF_RECORD_CALLCHAIN, 0);
1181   this->pid = pid;
1182   this->tid = tid;
1183   this->chain_type = static_cast<int>(type);
1184   this->time = time;
1185   this->ip_nr = ips.size();
1186   SetSize(header_size() + (4 + ips.size() * 2) * sizeof(uint64_t));
1187   char* new_binary = new char[size()];
1188   char* p = new_binary;
1189   MoveToBinaryFormat(header, p);
1190   MoveToBinaryFormat(this->pid, p);
1191   MoveToBinaryFormat(this->tid, p);
1192   MoveToBinaryFormat(this->chain_type, p);
1193   MoveToBinaryFormat(this->time, p);
1194   MoveToBinaryFormat(this->ip_nr, p);
1195   this->ips = reinterpret_cast<uint64_t*>(p);
1196   MoveToBinaryFormat(ips.data(), ips.size(), p);
1197   this->sps = reinterpret_cast<uint64_t*>(p);
1198   MoveToBinaryFormat(sps.data(), sps.size(), p);
1199   UpdateBinary(new_binary);
1200 }
1201 
DumpData(size_t indent) const1202 void CallChainRecord::DumpData(size_t indent) const {
1203   const char* type_name = "";
1204   switch (chain_type) {
1205     case CallChainJoiner::ORIGINAL_OFFLINE:
1206       type_name = "ORIGINAL_OFFLINE";
1207       break;
1208     case CallChainJoiner::ORIGINAL_REMOTE:
1209       type_name = "ORIGINAL_REMOTE";
1210       break;
1211     case CallChainJoiner::JOINED_OFFLINE:
1212       type_name = "JOINED_OFFLINE";
1213       break;
1214     case CallChainJoiner::JOINED_REMOTE:
1215       type_name = "JOINED_REMOTE";
1216       break;
1217   }
1218   PrintIndented(indent, "pid %u\n", pid);
1219   PrintIndented(indent, "tid %u\n", tid);
1220   PrintIndented(indent, "chain_type %s\n", type_name);
1221   PrintIndented(indent, "time %" PRIu64 "\n", time);
1222   PrintIndented(indent, "ip_nr %" PRIu64 "\n", ip_nr);
1223   for (size_t i = 0; i < ip_nr; ++i) {
1224     PrintIndented(indent + 1, "ip 0x%" PRIx64 ", sp 0x%" PRIx64 "\n", ips[i], sps[i]);
1225   }
1226 }
1227 
UnwindingResultRecord(char * p)1228 UnwindingResultRecord::UnwindingResultRecord(char* p) : Record(p) {
1229   const char* end = p + size();
1230   p += header_size();
1231   MoveFromBinaryFormat(time, p);
1232   MoveFromBinaryFormat(unwinding_result.used_time, p);
1233   MoveFromBinaryFormat(unwinding_result.error_code, p);
1234   MoveFromBinaryFormat(unwinding_result.error_addr, p);
1235   MoveFromBinaryFormat(unwinding_result.stack_start, p);
1236   MoveFromBinaryFormat(unwinding_result.stack_end, p);
1237 
1238   // regs_user_data
1239   MoveFromBinaryFormat(regs_user_data.abi, p);
1240   MoveFromBinaryFormat(regs_user_data.reg_mask, p);
1241   size_t bit_nr = __builtin_popcountll(regs_user_data.reg_mask);
1242   regs_user_data.reg_nr = bit_nr;
1243   regs_user_data.regs = reinterpret_cast<uint64_t*>(p);
1244   p += bit_nr * sizeof(uint64_t);
1245 
1246   // stack_user_data
1247   MoveFromBinaryFormat(stack_user_data.size, p);
1248   if (stack_user_data.size == 0) {
1249     stack_user_data.dyn_size = 0;
1250   } else {
1251     stack_user_data.data = p;
1252     p += stack_user_data.size;
1253     MoveFromBinaryFormat(stack_user_data.dyn_size, p);
1254   }
1255 
1256   // callchain
1257   if (p < end) {
1258     MoveFromBinaryFormat(callchain.length, p);
1259     callchain.ips = reinterpret_cast<uint64_t*>(p);
1260     p += callchain.length * sizeof(uint64_t);
1261     callchain.sps = reinterpret_cast<uint64_t*>(p);
1262     p += callchain.length * sizeof(uint64_t);
1263   }
1264   CHECK_LE(p, end);
1265 }
1266 
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)1267 UnwindingResultRecord::UnwindingResultRecord(uint64_t time, const UnwindingResult& unwinding_result,
1268                                              const PerfSampleRegsUserType& regs_user_data,
1269                                              const PerfSampleStackUserType& stack_user_data,
1270                                              const std::vector<uint64_t>& ips,
1271                                              const std::vector<uint64_t>& sps) {
1272   SetTypeAndMisc(SIMPLE_PERF_RECORD_UNWINDING_RESULT, 0);
1273   uint32_t size = header_size() + 6 * sizeof(uint64_t);
1274   size += (2 + regs_user_data.reg_nr) * sizeof(uint64_t);
1275   size +=
1276       stack_user_data.size == 0 ? sizeof(uint64_t) : (2 * sizeof(uint64_t) + stack_user_data.size);
1277   CHECK_EQ(ips.size(), sps.size());
1278   size += (1 + ips.size() * 2) * sizeof(uint64_t);
1279   SetSize(size);
1280   this->time = time;
1281   this->unwinding_result = unwinding_result;
1282   char* new_binary = new char[size];
1283   char* p = new_binary;
1284   MoveToBinaryFormat(header, p);
1285   MoveToBinaryFormat(this->time, p);
1286   MoveToBinaryFormat(unwinding_result.used_time, p);
1287   MoveToBinaryFormat(unwinding_result.error_code, p);
1288   MoveToBinaryFormat(unwinding_result.error_addr, p);
1289   MoveToBinaryFormat(unwinding_result.stack_start, p);
1290   MoveToBinaryFormat(unwinding_result.stack_end, p);
1291   MoveToBinaryFormat(regs_user_data.abi, p);
1292   MoveToBinaryFormat(regs_user_data.reg_mask, p);
1293   if (regs_user_data.reg_nr > 0) {
1294     MoveToBinaryFormat(regs_user_data.regs, regs_user_data.reg_nr, p);
1295   }
1296   MoveToBinaryFormat(stack_user_data.size, p);
1297   if (stack_user_data.size > 0) {
1298     MoveToBinaryFormat(stack_user_data.data, stack_user_data.size, p);
1299     MoveToBinaryFormat(stack_user_data.dyn_size, p);
1300   }
1301   MoveToBinaryFormat(static_cast<uint64_t>(ips.size()), p);
1302   MoveToBinaryFormat(ips.data(), ips.size(), p);
1303   MoveToBinaryFormat(sps.data(), sps.size(), p);
1304   CHECK_EQ(p, new_binary + size);
1305   UpdateBinary(new_binary);
1306 }
1307 
DumpData(size_t indent) const1308 void UnwindingResultRecord::DumpData(size_t indent) const {
1309   PrintIndented(indent, "time %" PRIu64 "\n", time);
1310   PrintIndented(indent, "used_time %" PRIu64 "\n", unwinding_result.used_time);
1311   PrintIndented(indent, "error_code %" PRIu64 "\n", unwinding_result.error_code);
1312   PrintIndented(indent, "error_addr 0x%" PRIx64 "\n", unwinding_result.error_addr);
1313   PrintIndented(indent, "stack_start 0x%" PRIx64 "\n", unwinding_result.stack_start);
1314   PrintIndented(indent, "stack_end 0x%" PRIx64 "\n", unwinding_result.stack_end);
1315   if (regs_user_data.reg_nr > 0) {
1316     PrintIndented(indent, "user regs: abi=%" PRId64 "\n", regs_user_data.abi);
1317     RegSet regs(regs_user_data.abi, regs_user_data.reg_mask, regs_user_data.regs);
1318     for (size_t i = 0; i < 64; ++i) {
1319       uint64_t value;
1320       if (regs.GetRegValue(i, &value)) {
1321         PrintIndented(indent + 1, "reg (%s) 0x%016" PRIx64 "\n", GetRegName(i, regs.arch).c_str(),
1322                       value);
1323       }
1324     }
1325   }
1326   if (stack_user_data.size > 0) {
1327     PrintIndented(indent, "user stack: size %zu dyn_size %" PRIu64 "\n", stack_user_data.size,
1328                   stack_user_data.dyn_size);
1329     const uint64_t* p = reinterpret_cast<const uint64_t*>(stack_user_data.data);
1330     const uint64_t* end = p + (stack_user_data.size / sizeof(uint64_t));
1331     while (p < end) {
1332       PrintIndented(indent + 1, "");
1333       for (size_t i = 0; i < 4 && p < end; ++i, ++p) {
1334         printf(" %016" PRIx64, *p);
1335       }
1336       printf("\n");
1337     }
1338     printf("\n");
1339   }
1340   if (callchain.length > 0) {
1341     PrintIndented(indent, "callchain length=%" PRIu64 ":\n", callchain.length);
1342     for (uint64_t i = 0; i < callchain.length; i++) {
1343       PrintIndented(indent + 1, "ip_%" PRIu64 ": 0x%" PRIx64 "\n", i + 1, callchain.ips[i]);
1344       PrintIndented(indent + 1, "sp_%" PRIu64 ": 0x%" PRIx64 "\n", i + 1, callchain.sps[i]);
1345     }
1346   }
1347 }
1348 
UnknownRecord(char * p)1349 UnknownRecord::UnknownRecord(char* p) : Record(p) {
1350   p += header_size();
1351   data = p;
1352 }
1353 
DumpData(size_t) const1354 void UnknownRecord::DumpData(size_t) const {}
1355 
ReadRecordFromBuffer(const perf_event_attr & attr,uint32_t type,char * p)1356 std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, uint32_t type, char* p) {
1357   switch (type) {
1358     case PERF_RECORD_MMAP:
1359       return std::unique_ptr<Record>(new MmapRecord(attr, p));
1360     case PERF_RECORD_MMAP2:
1361       return std::unique_ptr<Record>(new Mmap2Record(attr, p));
1362     case PERF_RECORD_COMM:
1363       return std::unique_ptr<Record>(new CommRecord(attr, p));
1364     case PERF_RECORD_EXIT:
1365       return std::unique_ptr<Record>(new ExitRecord(attr, p));
1366     case PERF_RECORD_FORK:
1367       return std::unique_ptr<Record>(new ForkRecord(attr, p));
1368     case PERF_RECORD_LOST:
1369       return std::unique_ptr<Record>(new LostRecord(attr, p));
1370     case PERF_RECORD_SAMPLE:
1371       return std::unique_ptr<Record>(new SampleRecord(attr, p));
1372     case PERF_RECORD_AUX:
1373       return std::unique_ptr<Record>(new AuxRecord(attr, p));
1374     case PERF_RECORD_TRACING_DATA:
1375       return std::unique_ptr<Record>(new TracingDataRecord(p));
1376     case PERF_RECORD_AUXTRACE_INFO:
1377       return std::unique_ptr<Record>(new AuxTraceInfoRecord(p));
1378     case PERF_RECORD_AUXTRACE:
1379       return std::unique_ptr<Record>(new AuxTraceRecord(p));
1380     case SIMPLE_PERF_RECORD_KERNEL_SYMBOL:
1381       return std::unique_ptr<Record>(new KernelSymbolRecord(p));
1382     case SIMPLE_PERF_RECORD_DSO:
1383       return std::unique_ptr<Record>(new DsoRecord(p));
1384     case SIMPLE_PERF_RECORD_SYMBOL:
1385       return std::unique_ptr<Record>(new SymbolRecord(p));
1386     case SIMPLE_PERF_RECORD_EVENT_ID:
1387       return std::unique_ptr<Record>(new EventIdRecord(p));
1388     case SIMPLE_PERF_RECORD_CALLCHAIN:
1389       return std::unique_ptr<Record>(new CallChainRecord(p));
1390     case SIMPLE_PERF_RECORD_UNWINDING_RESULT:
1391       return std::unique_ptr<Record>(new UnwindingResultRecord(p));
1392     case SIMPLE_PERF_RECORD_TRACING_DATA:
1393       return std::unique_ptr<Record>(new TracingDataRecord(p));
1394     default:
1395       return std::unique_ptr<Record>(new UnknownRecord(p));
1396   }
1397 }
1398 
ReadRecordFromOwnedBuffer(const perf_event_attr & attr,uint32_t type,char * p)1399 std::unique_ptr<Record> ReadRecordFromOwnedBuffer(const perf_event_attr& attr, uint32_t type,
1400                                                   char* p) {
1401   std::unique_ptr<Record> record = ReadRecordFromBuffer(attr, type, p);
1402   if (record != nullptr) {
1403     record->OwnBinary();
1404   } else {
1405     delete[] p;
1406   }
1407   return record;
1408 }
1409 
ReadRecordsFromBuffer(const perf_event_attr & attr,char * buf,size_t buf_size)1410 std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr, char* buf,
1411                                                            size_t buf_size) {
1412   std::vector<std::unique_ptr<Record>> result;
1413   char* p = buf;
1414   char* end = buf + buf_size;
1415   while (p < end) {
1416     RecordHeader header(p);
1417     CHECK_LE(p + header.size, end);
1418     CHECK_NE(0u, header.size);
1419     result.push_back(ReadRecordFromBuffer(attr, header.type, p));
1420     p += header.size;
1421   }
1422   return result;
1423 }
1424 
ReadRecordFromBuffer(const perf_event_attr & attr,char * p)1425 std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, char* p) {
1426   auto header = reinterpret_cast<const perf_event_header*>(p);
1427   return ReadRecordFromBuffer(attr, header->type, p);
1428 }
1429 
1430 }  // namespace simpleperf
1431