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