xref: /aosp_15_r20/system/extras/simpleperf/record_file_reader.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "record_file.h"
18 
19 #include <fcntl.h>
20 #include <string.h>
21 
22 #include <set>
23 #include <string_view>
24 #include <vector>
25 
26 #include <android-base/logging.h>
27 #include <android-base/scopeguard.h>
28 
29 #include "event_attr.h"
30 #include "record.h"
31 #include "system/extras/simpleperf/record_file.pb.h"
32 #include "utils.h"
33 
34 namespace simpleperf {
35 
36 using namespace PerfFileFormat;
37 
38 namespace PerfFileFormat {
39 
40 static const std::map<int, std::string> feature_name_map = {
41     {FEAT_TRACING_DATA, "tracing_data"},
42     {FEAT_BUILD_ID, "build_id"},
43     {FEAT_HOSTNAME, "hostname"},
44     {FEAT_OSRELEASE, "osrelease"},
45     {FEAT_VERSION, "version"},
46     {FEAT_ARCH, "arch"},
47     {FEAT_NRCPUS, "nrcpus"},
48     {FEAT_CPUDESC, "cpudesc"},
49     {FEAT_CPUID, "cpuid"},
50     {FEAT_TOTAL_MEM, "total_mem"},
51     {FEAT_CMDLINE, "cmdline"},
52     {FEAT_EVENT_DESC, "event_desc"},
53     {FEAT_CPU_TOPOLOGY, "cpu_topology"},
54     {FEAT_NUMA_TOPOLOGY, "numa_topology"},
55     {FEAT_BRANCH_STACK, "branch_stack"},
56     {FEAT_PMU_MAPPINGS, "pmu_mappings"},
57     {FEAT_GROUP_DESC, "group_desc"},
58     {FEAT_AUXTRACE, "auxtrace"},
59     {FEAT_FILE, "file"},
60     {FEAT_META_INFO, "meta_info"},
61     {FEAT_DEBUG_UNWIND, "debug_unwind"},
62     {FEAT_DEBUG_UNWIND_FILE, "debug_unwind_file"},
63     {FEAT_FILE2, "file2"},
64     {FEAT_ETM_BRANCH_LIST, "etm_branch_list"},
65     {FEAT_INIT_MAP, "init_map"},
66 };
67 
GetFeatureName(int feature_id)68 std::string GetFeatureName(int feature_id) {
69   auto it = feature_name_map.find(feature_id);
70   return it == feature_name_map.end() ? "" : it->second;
71 }
72 
GetFeatureId(const std::string & feature_name)73 int GetFeatureId(const std::string& feature_name) {
74   for (auto& pair : feature_name_map) {
75     if (pair.second == feature_name) {
76       return pair.first;
77     }
78   }
79   return -1;
80 }
81 
82 }  // namespace PerfFileFormat
83 
CreateInstance(const std::string & filename)84 std::unique_ptr<RecordFileReader> RecordFileReader::CreateInstance(const std::string& filename) {
85   std::string mode = std::string("rb") + CLOSE_ON_EXEC_MODE;
86   FILE* fp = fopen(filename.c_str(), mode.c_str());
87   if (fp == nullptr) {
88     PLOG(ERROR) << "failed to open record file '" << filename << "'";
89     return nullptr;
90   }
91   auto reader = std::unique_ptr<RecordFileReader>(new RecordFileReader(filename, fp));
92   if (!reader->ReadHeader() || !reader->ReadAttrSection() ||
93       !reader->ReadFeatureSectionDescriptors() || !reader->ReadMetaInfoFeature()) {
94     return nullptr;
95   }
96   reader->UseRecordingEnvironment();
97   return reader;
98 }
99 
RecordFileReader(const std::string & filename,FILE * fp)100 RecordFileReader::RecordFileReader(const std::string& filename, FILE* fp)
101     : filename_(filename),
102       record_fp_(fp),
103       event_id_pos_in_sample_records_(0),
104       event_id_reverse_pos_in_non_sample_records_(0) {
105   file_size_ = GetFileSize(filename_);
106 }
107 
~RecordFileReader()108 RecordFileReader::~RecordFileReader() {
109   if (record_fp_ != nullptr) {
110     Close();
111   }
112 }
113 
Close()114 bool RecordFileReader::Close() {
115   bool result = true;
116   if (fclose(record_fp_) != 0) {
117     PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
118     result = false;
119   }
120   record_fp_ = nullptr;
121   return result;
122 }
123 
ReadHeader()124 bool RecordFileReader::ReadHeader() {
125   if (!Read(&header_, sizeof(header_))) {
126     return false;
127   }
128   if (memcmp(header_.magic, PERF_MAGIC, sizeof(header_.magic)) != 0) {
129     LOG(ERROR) << filename_ << " is not a valid profiling record file.";
130     return false;
131   }
132   if (header_.attr_size == 0 || !CheckSectionDesc(header_.attrs, sizeof(header_)) ||
133       !CheckSectionDesc(header_.data, sizeof(header_))) {
134     LOG(ERROR) << "invalid header in " << filename_;
135     return false;
136   }
137   return true;
138 }
139 
CheckSectionDesc(const SectionDesc & desc,uint64_t min_offset,uint64_t alignment)140 bool RecordFileReader::CheckSectionDesc(const SectionDesc& desc, uint64_t min_offset,
141                                         uint64_t alignment) {
142   uint64_t desc_end;
143   if (desc.offset < min_offset || __builtin_add_overflow(desc.offset, desc.size, &desc_end) ||
144       desc_end > file_size_) {
145     return false;
146   }
147   if (desc.size % alignment != 0) {
148     return false;
149   }
150   return true;
151 }
152 
ReadAttrSection()153 bool RecordFileReader::ReadAttrSection() {
154   size_t attr_count = header_.attrs.size / header_.attr_size;
155   if (header_.attr_size != sizeof(FileAttr)) {
156     if (header_.attr_size <= sizeof(SectionDesc)) {
157       LOG(ERROR) << "invalid attr section in " << filename_;
158       return false;
159     }
160     LOG(DEBUG) << "attr size (" << header_.attr_size << ") in " << filename_
161                << " doesn't match expected size (" << sizeof(FileAttr) << ")";
162   }
163   if (attr_count == 0) {
164     LOG(ERROR) << "no attr in file " << filename_;
165     return false;
166   }
167   if (fseek(record_fp_, header_.attrs.offset, SEEK_SET) != 0) {
168     PLOG(ERROR) << "fseek() failed";
169     return false;
170   }
171   event_attrs_.resize(attr_count);
172   std::vector<SectionDesc> id_sections(attr_count);
173   size_t attr_size_in_file = header_.attr_size - sizeof(SectionDesc);
174   for (size_t i = 0; i < attr_count; ++i) {
175     std::vector<char> buf(header_.attr_size);
176     if (!Read(buf.data(), buf.size())) {
177       return false;
178     }
179     // The struct perf_event_attr is defined in a Linux header file. It can be extended in newer
180     // kernel versions with more fields and a bigger size. To disable these extensions, set their
181     // values to zero. So to copy perf_event_attr from file to memory safely, ensure the copy
182     // doesn't overflow the file or memory, and set the values of any extra fields in memory to
183     // zero.
184     if (attr_size_in_file >= sizeof(perf_event_attr)) {
185       memcpy(&event_attrs_[i].attr, &buf[0], sizeof(perf_event_attr));
186     } else {
187       memset(&event_attrs_[i].attr, 0, sizeof(perf_event_attr));
188       memcpy(&event_attrs_[i].attr, &buf[0], attr_size_in_file);
189     }
190     memcpy(&id_sections[i], &buf[attr_size_in_file], sizeof(SectionDesc));
191     if (!CheckSectionDesc(id_sections[i], 0, sizeof(uint64_t))) {
192       LOG(ERROR) << "invalid attr section in " << filename_;
193       return false;
194     }
195   }
196   if (event_attrs_.size() > 1) {
197     if (!GetCommonEventIdPositionsForAttrs(event_attrs_, &event_id_pos_in_sample_records_,
198                                            &event_id_reverse_pos_in_non_sample_records_)) {
199       return false;
200     }
201   }
202   for (size_t i = 0; i < attr_count; ++i) {
203     if (!ReadIdSection(id_sections[i], &event_attrs_[i].ids)) {
204       return false;
205     }
206     for (auto id : event_attrs_[i].ids) {
207       event_id_to_attr_map_[id] = i;
208     }
209   }
210   return true;
211 }
212 
ReadFeatureSectionDescriptors()213 bool RecordFileReader::ReadFeatureSectionDescriptors() {
214   std::vector<int> features;
215   for (size_t i = 0; i < sizeof(header_.features); ++i) {
216     for (size_t j = 0; j < 8; ++j) {
217       if (header_.features[i] & (1 << j)) {
218         features.push_back(i * 8 + j);
219       }
220     }
221   }
222   uint64_t feature_section_offset = header_.data.offset + header_.data.size;
223   if (fseek(record_fp_, feature_section_offset, SEEK_SET) != 0) {
224     PLOG(ERROR) << "fseek() failed";
225     return false;
226   }
227   uint64_t min_section_data_pos = feature_section_offset + sizeof(SectionDesc) * features.size();
228   for (const auto& id : features) {
229     SectionDesc desc;
230     if (!Read(&desc, sizeof(desc))) {
231       return false;
232     }
233     if (!CheckSectionDesc(desc, min_section_data_pos)) {
234       LOG(ERROR) << "invalid feature section descriptor in " << filename_;
235       return false;
236     }
237     feature_section_descriptors_.emplace(id, desc);
238   }
239   return true;
240 }
241 
ReadIdSection(const SectionDesc & section,std::vector<uint64_t> * ids)242 bool RecordFileReader::ReadIdSection(const SectionDesc& section, std::vector<uint64_t>* ids) {
243   size_t id_count = section.size / sizeof(uint64_t);
244   if (fseek(record_fp_, section.offset, SEEK_SET) != 0) {
245     PLOG(ERROR) << "fseek() failed";
246     return false;
247   }
248   ids->resize(id_count);
249   if (!Read(ids->data(), section.size)) {
250     return false;
251   }
252   return true;
253 }
254 
UseRecordingEnvironment()255 void RecordFileReader::UseRecordingEnvironment() {
256   std::string arch = ReadFeatureString(FEAT_ARCH);
257   if (!arch.empty()) {
258     scoped_arch_.reset(new ScopedCurrentArch(GetArchType(arch)));
259   }
260   auto& meta_info = GetMetaInfoFeature();
261   if (auto it = meta_info.find("event_type_info"); it != meta_info.end()) {
262     if (EventTypeManager::Instance().GetScopedFinder() == nullptr) {
263       scoped_event_types_.reset(new ScopedEventTypes(it->second));
264     }
265   }
266 }
267 
ReadDataSection(const std::function<bool (std::unique_ptr<Record>)> & callback)268 bool RecordFileReader::ReadDataSection(
269     const std::function<bool(std::unique_ptr<Record>)>& callback) {
270   std::unique_ptr<Record> record;
271   while (ReadRecord(record)) {
272     if (record == nullptr) {
273       return true;
274     }
275     if (!callback(std::move(record))) {
276       return false;
277     }
278   }
279   return false;
280 }
281 
ReadRecord(std::unique_ptr<Record> & record)282 bool RecordFileReader::ReadRecord(std::unique_ptr<Record>& record) {
283   if (read_record_pos_.end == 0) {
284     if (fseek(record_fp_, header_.data.offset, SEEK_SET) != 0) {
285       PLOG(ERROR) << "fseek() failed";
286       return false;
287     }
288     read_record_pos_.end = header_.data.size;
289   }
290   record = nullptr;
291   if (read_record_pos_.pos < read_record_pos_.end ||
292       (decompressor_ && decompressor_->HasOutputData())) {
293     record = ReadRecord(read_record_pos_);
294     if (record == nullptr) {
295       return false;
296     }
297     if (record->type() == SIMPLE_PERF_RECORD_EVENT_ID) {
298       ProcessEventIdRecord(*static_cast<EventIdRecord*>(record.get()));
299     }
300   }
301   return true;
302 }
303 
ReadRecord(ReadPos & pos)304 std::unique_ptr<Record> RecordFileReader::ReadRecord(ReadPos& pos) {
305   std::unique_ptr<char[]> p = ReadRecordWithDecompression(pos);
306   if (!p) {
307     return nullptr;
308   }
309   RecordHeader header;
310   if (!header.Parse(p.get())) {
311     return nullptr;
312   }
313 
314   if (header.type == SIMPLE_PERF_RECORD_SPLIT) {
315     // Read until meeting a RECORD_SPLIT_END record.
316     std::vector<char> buf;
317     while (header.type == SIMPLE_PERF_RECORD_SPLIT) {
318       buf.insert(buf.end(), p.get() + Record::header_size(), p.get() + header.size);
319       p = ReadRecordWithDecompression(pos);
320       if (!p || !header.Parse(p.get())) {
321         return nullptr;
322       }
323     }
324     if (header.type != SIMPLE_PERF_RECORD_SPLIT_END) {
325       LOG(ERROR) << "SPLIT records are not followed by a SPLIT_END record.";
326       return nullptr;
327     }
328     if (buf.size() < Record::header_size() || !header.Parse(buf.data()) ||
329         header.size != buf.size()) {
330       LOG(ERROR) << "invalid record merged from SPLIT records";
331       return nullptr;
332     }
333     p.reset(new char[buf.size()]);
334     memcpy(p.get(), buf.data(), buf.size());
335   }
336 
337   const perf_event_attr* attr = &event_attrs_[0].attr;
338   if (event_attrs_.size() > 1 && header.type < PERF_RECORD_USER_DEFINED_TYPE_START) {
339     bool has_event_id = false;
340     uint64_t event_id;
341     if (header.type == PERF_RECORD_SAMPLE) {
342       if (header.size > event_id_pos_in_sample_records_ + sizeof(uint64_t)) {
343         has_event_id = true;
344         event_id = *reinterpret_cast<uint64_t*>(p.get() + event_id_pos_in_sample_records_);
345       }
346     } else {
347       if (header.size > event_id_reverse_pos_in_non_sample_records_) {
348         has_event_id = true;
349         event_id = *reinterpret_cast<uint64_t*>(p.get() + header.size -
350                                                 event_id_reverse_pos_in_non_sample_records_);
351       }
352     }
353     if (has_event_id) {
354       auto it = event_id_to_attr_map_.find(event_id);
355       if (it != event_id_to_attr_map_.end()) {
356         attr = &event_attrs_[it->second].attr;
357       }
358     }
359   }
360   auto r = ReadRecordFromBuffer(*attr, header.type, p.get(), p.get() + header.size);
361   if (!r) {
362     return nullptr;
363   }
364   p.release();
365   r->OwnBinary();
366   if (r->type() == PERF_RECORD_AUXTRACE) {
367     auto auxtrace = static_cast<AuxTraceRecord*>(r.get());
368     auxtrace->location.file_offset = header_.data.offset + read_record_pos_.pos;
369     read_record_pos_.pos += auxtrace->data->aux_size;
370     if (fseek(record_fp_, auxtrace->data->aux_size, SEEK_CUR) != 0) {
371       PLOG(ERROR) << "fseek() failed";
372       return nullptr;
373     }
374   }
375   return r;
376 }
377 
ReadRecordWithDecompression(ReadPos & pos)378 std::unique_ptr<char[]> RecordFileReader::ReadRecordWithDecompression(ReadPos& pos) {
379   while (true) {
380     if (decompressor_) {
381       std::string_view output = decompressor_->GetOutputData();
382       if (output.size() >= sizeof(perf_event_header)) {
383         auto header = reinterpret_cast<const perf_event_header*>(output.data());
384         if (header->size <= output.size()) {
385           std::unique_ptr<char[]> p(new char[header->size]);
386           memcpy(p.get(), output.data(), header->size);
387           decompressor_->ConsumeOutputData(header->size);
388           return p;
389         }
390       }
391     }
392     if (pos.pos == pos.end) {
393       break;
394     }
395     perf_event_header header;
396     if (!Read(&header, sizeof(header))) {
397       return nullptr;
398     }
399     pos.pos += header.size;
400     if (header.type == PERF_RECORD_COMPRESSED) {
401       if (!decompressor_) {
402         decompressor_ = CreateZstdDecompressor();
403         if (!decompressor_) {
404           return nullptr;
405         }
406       }
407       std::vector<char> buf(header.size - sizeof(header));
408       if (!Read(buf.data(), buf.size())) {
409         return nullptr;
410       }
411       if (!decompressor_->AddInputData(buf.data(), buf.size())) {
412         return nullptr;
413       }
414     } else {
415       std::unique_ptr<char[]> p(new char[header.size]);
416       memcpy(p.get(), &header, sizeof(header));
417       if (!Read(p.get() + sizeof(header), header.size - sizeof(header))) {
418         return nullptr;
419       }
420       return p;
421     }
422   }
423   return nullptr;
424 }
425 
Read(void * buf,size_t len)426 bool RecordFileReader::Read(void* buf, size_t len) {
427   if (len != 0 && fread(buf, len, 1, record_fp_) != 1) {
428     PLOG(ERROR) << "failed to read file " << filename_;
429     return false;
430   }
431   return true;
432 }
433 
ReadAtOffset(uint64_t offset,void * buf,size_t len)434 bool RecordFileReader::ReadAtOffset(uint64_t offset, void* buf, size_t len) {
435   if (fseek(record_fp_, offset, SEEK_SET) != 0) {
436     PLOG(ERROR) << "failed to seek to " << offset;
437     return false;
438   }
439   return Read(buf, len);
440 }
441 
ProcessEventIdRecord(const EventIdRecord & r)442 void RecordFileReader::ProcessEventIdRecord(const EventIdRecord& r) {
443   for (size_t i = 0; i < r.count; ++i) {
444     const auto& data = r.data[i];
445     event_attrs_[data.attr_id].ids.push_back(data.event_id);
446     event_id_to_attr_map_[data.event_id] = data.attr_id;
447   }
448 }
449 
GetAttrIndexOfRecord(const Record * record)450 size_t RecordFileReader::GetAttrIndexOfRecord(const Record* record) {
451   auto it = event_id_to_attr_map_.find(record->Id());
452   if (it != event_id_to_attr_map_.end()) {
453     return it->second;
454   }
455   return 0;
456 }
457 
GetAttrIndexByEventId(uint64_t event_id)458 std::optional<size_t> RecordFileReader::GetAttrIndexByEventId(uint64_t event_id) {
459   auto it = event_id_to_attr_map_.find(event_id);
460   if (it != event_id_to_attr_map_.end()) {
461     return it->second;
462   }
463   return std::nullopt;
464 }
465 
ReadFeatureSection(int feature,std::vector<char> * data)466 bool RecordFileReader::ReadFeatureSection(int feature, std::vector<char>* data) {
467   const std::map<int, SectionDesc>& section_map = FeatureSectionDescriptors();
468   auto it = section_map.find(feature);
469   if (it == section_map.end()) {
470     return false;
471   }
472   SectionDesc section = it->second;
473   data->resize(section.size);
474   if (section.size == 0) {
475     return true;
476   }
477   if (!ReadAtOffset(section.offset, data->data(), data->size())) {
478     return false;
479   }
480   return true;
481 }
482 
ReadFeatureSection(int feature,std::string * data)483 bool RecordFileReader::ReadFeatureSection(int feature, std::string* data) {
484   const std::map<int, SectionDesc>& section_map = FeatureSectionDescriptors();
485   auto it = section_map.find(feature);
486   if (it == section_map.end()) {
487     return false;
488   }
489   SectionDesc section = it->second;
490   data->resize(section.size);
491   if (section.size == 0) {
492     return true;
493   }
494   if (!ReadAtOffset(section.offset, data->data(), data->size())) {
495     return false;
496   }
497   return true;
498 }
499 
ReadCmdlineFeature()500 std::vector<std::string> RecordFileReader::ReadCmdlineFeature() {
501   std::vector<char> buf;
502   if (!ReadFeatureSection(FEAT_CMDLINE, &buf)) {
503     return {};
504   }
505   BinaryReader reader(buf.data(), buf.size());
506   std::vector<std::string> cmdline;
507 
508   uint32_t arg_count = 0;
509   reader.Read(arg_count);
510   for (size_t i = 0; i < arg_count && !reader.error; ++i) {
511     uint32_t aligned_len;
512     reader.Read(aligned_len);
513     cmdline.emplace_back(reader.ReadString());
514     uint32_t len = cmdline.back().size() + 1;
515     if (aligned_len != Align(len, 64)) {
516       reader.error = true;
517       break;
518     }
519     reader.Move(aligned_len - len);
520   }
521   return reader.error ? std::vector<std::string>() : cmdline;
522 }
523 
ReadBuildIdFeature()524 std::vector<BuildIdRecord> RecordFileReader::ReadBuildIdFeature() {
525   std::vector<char> buf;
526   if (!ReadFeatureSection(FEAT_BUILD_ID, &buf)) {
527     return {};
528   }
529   const char* p = buf.data();
530   const char* end = buf.data() + buf.size();
531   std::vector<BuildIdRecord> result;
532   while (p + sizeof(perf_event_header) < end) {
533     auto header = reinterpret_cast<const perf_event_header*>(p);
534     if ((header->size <= sizeof(perf_event_header)) || (header->size > end - p)) {
535       return {};
536     }
537     std::unique_ptr<char[]> binary(new char[header->size]);
538     memcpy(binary.get(), p, header->size);
539     p += header->size;
540     BuildIdRecord record;
541     if (!record.Parse(event_attrs_[0].attr, binary.get(), binary.get() + header->size)) {
542       return {};
543     }
544     binary.release();
545     record.OwnBinary();
546     // Set type explicitly as the perf.data produced by perf doesn't set it.
547     record.SetTypeAndMisc(PERF_RECORD_BUILD_ID, record.misc());
548     result.push_back(std::move(record));
549   }
550   return result;
551 }
552 
ReadFeatureString(int feature)553 std::string RecordFileReader::ReadFeatureString(int feature) {
554   std::vector<char> buf;
555   if (!ReadFeatureSection(feature, &buf)) {
556     return std::string();
557   }
558   BinaryReader reader(buf.data(), buf.size());
559   uint32_t len = 0;
560   reader.Read(len);
561   std::string s = reader.ReadString();
562   return reader.error ? "" : s;
563 }
564 
ReadAuxTraceFeature()565 std::vector<uint64_t> RecordFileReader::ReadAuxTraceFeature() {
566   std::vector<char> buf;
567   if (!ReadFeatureSection(FEAT_AUXTRACE, &buf)) {
568     return {};
569   }
570   BinaryReader reader(buf.data(), buf.size());
571   if (reader.LeftSize() % sizeof(uint64_t) != 0) {
572     return {};
573   }
574   if (reader.LeftSize() / sizeof(uint64_t) % 2 == 1) {
575     // Recording files generated by linux perf contain an extra uint64 field. Skip it here.
576     reader.Move(sizeof(uint64_t));
577   }
578 
579   std::vector<uint64_t> auxtrace_offset;
580   while (!reader.error && reader.LeftSize() > 0u) {
581     uint64_t offset;
582     uint64_t size;
583     reader.Read(offset);
584     reader.Read(size);
585     auxtrace_offset.push_back(offset);
586     if (size != AuxTraceRecord::Size()) {
587       reader.error = true;
588     }
589   }
590   return reader.error ? std::vector<uint64_t>() : auxtrace_offset;
591 }
592 
ReadFileFeature(uint64_t & read_pos,FileFeature & file,bool & error)593 bool RecordFileReader::ReadFileFeature(uint64_t& read_pos, FileFeature& file, bool& error) {
594   file.Clear();
595   error = false;
596 
597   bool use_v1 = false;
598   PerfFileFormat::SectionDesc desc;
599   if (auto it = feature_section_descriptors_.find(FEAT_FILE);
600       it != feature_section_descriptors_.end()) {
601     use_v1 = true;
602     desc = it->second;
603   } else if (auto it = feature_section_descriptors_.find(FEAT_FILE2);
604              it != feature_section_descriptors_.end()) {
605     desc = it->second;
606   } else {
607     return false;
608   }
609 
610   if (read_pos >= desc.size) {
611     return false;
612   }
613   if (read_pos == 0) {
614     if (fseek(record_fp_, desc.offset, SEEK_SET) != 0) {
615       PLOG(ERROR) << "fseek() failed";
616       error = true;
617       return false;
618     }
619   }
620 
621   bool result = false;
622   if (use_v1) {
623     result = ReadFileV1Feature(read_pos, desc.size - read_pos, file);
624   } else {
625     result = ReadFileV2Feature(read_pos, desc.size - read_pos, file);
626   }
627   if (!result) {
628     LOG(ERROR) << "failed to read file feature section";
629     error = true;
630   }
631   return result;
632 }
633 
ReadFileV1Feature(uint64_t & read_pos,uint64_t max_size,FileFeature & file)634 bool RecordFileReader::ReadFileV1Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file) {
635   uint32_t size = 0;
636   if (max_size < 4 || !Read(&size, 4) || max_size - 4 < size) {
637     return false;
638   }
639   read_pos += 4;
640   std::vector<char> buf(size);
641   if (!Read(buf.data(), size)) {
642     return false;
643   }
644   read_pos += size;
645   BinaryReader reader(buf.data(), buf.size());
646   file.path = reader.ReadString();
647   uint32_t file_type = 0;
648   reader.Read(file_type);
649   if (file_type > DSO_UNKNOWN_FILE) {
650     LOG(ERROR) << "unknown file type for " << file.path
651                << " in file feature section: " << file_type;
652     return false;
653   }
654   file.type = static_cast<DsoType>(file_type);
655   reader.Read(file.min_vaddr);
656   uint32_t symbol_count = 0;
657   reader.Read(symbol_count);
658   if (symbol_count > size) {
659     return false;
660   }
661   file.symbols.reserve(symbol_count);
662   while (symbol_count-- > 0) {
663     uint64_t start_vaddr = 0;
664     uint32_t len = 0;
665     reader.Read(start_vaddr);
666     reader.Read(len);
667     std::string name = reader.ReadString();
668     file.symbols.emplace_back(name, start_vaddr, len);
669   }
670   if (file.type == DSO_DEX_FILE) {
671     uint32_t offset_count = 0;
672     reader.Read(offset_count);
673     if (offset_count > size) {
674       return false;
675     }
676     file.dex_file_offsets.resize(offset_count);
677     reader.Read(file.dex_file_offsets.data(), offset_count);
678   }
679   file.file_offset_of_min_vaddr = std::numeric_limits<uint64_t>::max();
680   if ((file.type == DSO_ELF_FILE || file.type == DSO_KERNEL_MODULE) && !reader.error &&
681       reader.LeftSize() > 0) {
682     reader.Read(file.file_offset_of_min_vaddr);
683   }
684   return !reader.error && reader.LeftSize() == 0;
685 }
686 
ReadFileV2Feature(uint64_t & read_pos,uint64_t max_size,FileFeature & file)687 bool RecordFileReader::ReadFileV2Feature(uint64_t& read_pos, uint64_t max_size, FileFeature& file) {
688   uint32_t size;
689   if (max_size < 4 || !Read(&size, 4) || max_size - 4 < size) {
690     return false;
691   }
692   read_pos += 4;
693   std::string s(size, '\0');
694   if (!Read(s.data(), size)) {
695     return false;
696   }
697   read_pos += size;
698   proto::FileFeature proto_file;
699   if (!proto_file.ParseFromString(s)) {
700     return false;
701   }
702   file.path = proto_file.path();
703   file.type = static_cast<DsoType>(proto_file.type());
704   file.min_vaddr = proto_file.min_vaddr();
705   file.symbols.reserve(proto_file.symbol_size());
706   for (size_t i = 0; i < proto_file.symbol_size(); i++) {
707     const auto& proto_symbol = proto_file.symbol(i);
708     file.symbols.emplace_back(proto_symbol.name(), proto_symbol.vaddr(), proto_symbol.len());
709   }
710   if (file.type == DSO_DEX_FILE) {
711     if (!proto_file.has_dex_file()) {
712       return false;
713     }
714     const auto& dex_file_offsets = proto_file.dex_file().dex_file_offset();
715     file.dex_file_offsets.insert(file.dex_file_offsets.end(), dex_file_offsets.begin(),
716                                  dex_file_offsets.end());
717   } else if (file.type == DSO_ELF_FILE) {
718     if (!proto_file.has_elf_file()) {
719       return false;
720     }
721     file.file_offset_of_min_vaddr = proto_file.elf_file().file_offset_of_min_vaddr();
722   } else if (file.type == DSO_KERNEL_MODULE) {
723     if (!proto_file.has_kernel_module()) {
724       return false;
725     }
726     file.file_offset_of_min_vaddr = proto_file.kernel_module().memory_offset_of_min_vaddr();
727   }
728   return true;
729 }
730 
ReadMetaInfoFeature()731 bool RecordFileReader::ReadMetaInfoFeature() {
732   if (feature_section_descriptors_.count(FEAT_META_INFO)) {
733     std::vector<char> buf;
734     if (!ReadFeatureSection(FEAT_META_INFO, &buf)) {
735       return false;
736     }
737     std::string_view s(buf.data(), buf.size());
738     size_t key_start = 0;
739     while (key_start < s.size()) {
740       // Parse a C-string for key.
741       size_t key_end = s.find('\0', key_start);
742       if (key_end == key_start || key_end == s.npos) {
743         LOG(ERROR) << "invalid meta info in " << filename_;
744         return false;
745       }
746       // Parse a C-string for value.
747       size_t value_start = key_end + 1;
748       size_t value_end = s.find('\0', value_start);
749       if (value_end == value_start || value_end == s.npos) {
750         LOG(ERROR) << "invalid meta info in " << filename_;
751         return false;
752       }
753       meta_info_[&s[key_start]] = &s[value_start];
754       key_start = value_end + 1;
755     }
756   }
757   return true;
758 }
759 
GetClockId()760 std::string RecordFileReader::GetClockId() {
761   if (auto it = meta_info_.find("clockid"); it != meta_info_.end()) {
762     return it->second;
763   }
764   return "perf";
765 }
766 
ReadDebugUnwindFeature()767 std::optional<DebugUnwindFeature> RecordFileReader::ReadDebugUnwindFeature() {
768   if (feature_section_descriptors_.count(FEAT_DEBUG_UNWIND)) {
769     std::string s;
770     if (!ReadFeatureSection(FEAT_DEBUG_UNWIND, &s)) {
771       return std::nullopt;
772     }
773     proto::DebugUnwindFeature proto_debug_unwind;
774     proto_debug_unwind.ParseFromString(s);
775     DebugUnwindFeature debug_unwind(proto_debug_unwind.file_size());
776     for (size_t i = 0; i < proto_debug_unwind.file_size(); i++) {
777       debug_unwind[i].path = proto_debug_unwind.file(i).path();
778       debug_unwind[i].size = proto_debug_unwind.file(i).size();
779     }
780     return debug_unwind;
781   }
782   return std::nullopt;
783 }
784 
ReadInitMapFeature(const std::function<bool (std::unique_ptr<Record>)> & callback)785 bool RecordFileReader::ReadInitMapFeature(
786     const std::function<bool(std::unique_ptr<Record>)>& callback) {
787   auto it = feature_section_descriptors_.find(FEAT_INIT_MAP);
788   if (it == feature_section_descriptors_.end()) {
789     return false;
790   }
791   if (fseek(record_fp_, it->second.offset, SEEK_SET) != 0) {
792     PLOG(ERROR) << "fseek() failed";
793     return false;
794   }
795   ReadPos pos = {0, it->second.size};
796   while (pos.pos < pos.end || (decompressor_ && decompressor_->HasOutputData())) {
797     auto r = ReadRecord(pos);
798     if (!r) {
799       return false;
800     }
801     if (!callback(std::move(r))) {
802       return false;
803     }
804   }
805   return true;
806 }
807 
LoadBuildIdAndFileFeatures(ThreadTree & thread_tree)808 bool RecordFileReader::LoadBuildIdAndFileFeatures(ThreadTree& thread_tree) {
809   std::vector<BuildIdRecord> records = ReadBuildIdFeature();
810   std::vector<std::pair<std::string, BuildId>> build_ids;
811   for (auto& r : records) {
812     build_ids.push_back(std::make_pair(r.filename, r.build_id));
813   }
814   Dso::SetBuildIds(build_ids);
815 
816   FileFeature file_feature;
817   uint64_t read_pos = 0;
818   bool error = false;
819   while (ReadFileFeature(read_pos, file_feature, error)) {
820     if (!thread_tree.AddDsoInfo(file_feature)) {
821       return false;
822     }
823   }
824   return !error;
825 }
826 
ReadAuxData(uint32_t cpu,uint64_t aux_offset,size_t size,std::vector<uint8_t> & buf,bool & error)827 bool RecordFileReader::ReadAuxData(uint32_t cpu, uint64_t aux_offset, size_t size,
828                                    std::vector<uint8_t>& buf, bool& error) {
829   error = false;
830   long saved_pos = ftell(record_fp_);
831   if (saved_pos == -1) {
832     PLOG(ERROR) << "ftell() failed";
833     error = true;
834     return false;
835   }
836   android::base::ScopeGuard guard([&]() { fseek(record_fp_, saved_pos, SEEK_SET); });
837 
838   OverflowResult aux_end = SafeAdd(aux_offset, size);
839   if (aux_end.overflow) {
840     LOG(ERROR) << "aux_end overflow";
841     error = true;
842     return false;
843   }
844   if (aux_data_location_.empty() && !BuildAuxDataLocation()) {
845     error = true;
846     return false;
847   }
848   AuxDataLocation* location = nullptr;
849   auto it = aux_data_location_.find(cpu);
850   if (it != aux_data_location_.end()) {
851     auto comp = [](uint64_t aux_offset, const AuxDataLocation& location) {
852       return aux_offset < location.aux_offset;
853     };
854     auto location_it = std::upper_bound(it->second.begin(), it->second.end(), aux_offset, comp);
855     if (location_it != it->second.begin()) {
856       --location_it;
857       location = &*location_it;
858     }
859   }
860   if (location == nullptr) {
861     // ETM data can be dropped when recording if the userspace buffer is full. This isn't an error.
862     LOG(INFO) << "aux data is missing: cpu " << cpu << ", aux_offset " << aux_offset << ", size "
863               << size << ". Probably the data is lost when recording.";
864     return false;
865   }
866   if (decompressor_) {
867     return ReadAuxDataFromDecompressor(cpu, aux_offset, size, buf, *location, error);
868   }
869   if (buf.size() < size) {
870     buf.resize(size);
871   }
872   if (!ReadAtOffset(aux_offset - location->aux_offset + location->file_offset, buf.data(), size)) {
873     error = true;
874     return false;
875   }
876   return true;
877 }
878 
BuildAuxDataLocation()879 bool RecordFileReader::BuildAuxDataLocation() {
880   std::vector<uint64_t> auxtrace_offset = ReadAuxTraceFeature();
881   std::unique_ptr<char[]> buf(new char[AuxTraceRecord::Size()]);
882   for (auto offset : auxtrace_offset) {
883     if (!ReadAtOffset(offset, buf.get(), AuxTraceRecord::Size())) {
884       return false;
885     }
886     AuxTraceRecord auxtrace;
887     if (!auxtrace.Parse(event_attrs_[0].attr, buf.get(), buf.get() + AuxTraceRecord::Size())) {
888       return false;
889     }
890     AuxDataLocation location(auxtrace.data->offset, auxtrace.data->aux_size,
891                              offset + auxtrace.size());
892     OverflowResult aux_end = SafeAdd(location.aux_offset, location.aux_size);
893     OverflowResult file_end = SafeAdd(location.file_offset, location.aux_size);
894     if (aux_end.overflow || file_end.overflow || file_end.value > file_size_) {
895       LOG(ERROR) << "invalid auxtrace feature section";
896       return false;
897     }
898     auto location_it = aux_data_location_.find(auxtrace.data->cpu);
899     if (location_it != aux_data_location_.end()) {
900       const AuxDataLocation& prev_location = location_it->second.back();
901       // The AuxTraceRecords should be sorted by aux_offset for each cpu.
902       if (prev_location.aux_offset > location.aux_offset) {
903         LOG(ERROR) << "invalid auxtrace feature section";
904         return false;
905       }
906       location_it->second.emplace_back(location);
907     } else {
908       aux_data_location_[auxtrace.data->cpu].emplace_back(location);
909     }
910   }
911   return true;
912 }
913 
ReadAuxDataFromDecompressor(uint32_t cpu,uint64_t aux_offset,size_t size,std::vector<uint8_t> & buf,const AuxDataLocation & location,bool & error)914 bool RecordFileReader::ReadAuxDataFromDecompressor(uint32_t cpu, uint64_t aux_offset, size_t size,
915                                                    std::vector<uint8_t>& buf,
916                                                    const AuxDataLocation& location, bool& error) {
917   if (!auxdata_decompressor_) {
918     auxdata_decompressor_.reset(new AuxDataDecompressor);
919     auxdata_decompressor_->decompressor = CreateZstdDecompressor();
920     if (!auxdata_decompressor_->decompressor) {
921       error = true;
922       return false;
923     }
924   }
925   if (auxdata_decompressor_->cpu != cpu || auxdata_decompressor_->location != location) {
926     auxdata_decompressor_->cpu = cpu;
927     auxdata_decompressor_->location = location;
928     Decompressor& decompressor = *auxdata_decompressor_->decompressor;
929     // Read and decompress new aux data.
930     std::string_view output = decompressor.GetOutputData();
931     if (!output.empty()) {
932       decompressor.ConsumeOutputData(output.size());
933     }
934     std::vector<char> input(location.aux_size);
935     if (!ReadAtOffset(location.file_offset, input.data(), input.size()) ||
936         !decompressor.AddInputData(input.data(), input.size())) {
937       error = true;
938       return false;
939     }
940   }
941   std::string_view data = auxdata_decompressor_->decompressor->GetOutputData();
942   if (location.aux_offset + data.size() < aux_offset + size) {
943     // ETM data can be dropped when recording if the userspace buffer is full. This isn't an
944     // error.
945     LOG(INFO) << "aux data is missing: cpu " << cpu << ", aux_offset " << aux_offset << ", size "
946               << size << ". Probably the data is lost when recording.";
947     return false;
948   }
949   if (buf.size() < size) {
950     buf.resize(size);
951   }
952   memcpy(buf.data(), &data[aux_offset - location.aux_offset], size);
953   return true;
954 }
955 
DataSection()956 std::vector<std::unique_ptr<Record>> RecordFileReader::DataSection() {
957   std::vector<std::unique_ptr<Record>> records;
958   ReadDataSection([&](std::unique_ptr<Record> record) {
959     records.push_back(std::move(record));
960     return true;
961   });
962   return records;
963 }
964 
IsPerfDataFile(const std::string & filename)965 bool IsPerfDataFile(const std::string& filename) {
966   auto fd = FileHelper::OpenReadOnly(filename);
967   if (fd.ok()) {
968     PerfFileFormat::FileHeader header;
969     return android::base::ReadFully(fd, &header, sizeof(header)) &&
970            memcmp(header.magic, PERF_MAGIC, sizeof(header.magic)) == 0;
971   }
972   return false;
973 }
974 
975 }  // namespace simpleperf
976