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 #include <sys/mman.h>
22 #include <unistd.h>
23 #include <algorithm>
24 #include <set>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 
32 #include "dso.h"
33 #include "event_attr.h"
34 #include "perf_event.h"
35 #include "record.h"
36 #include "utils.h"
37 
38 using namespace PerfFileFormat;
39 
CreateInstance(const std::string & filename)40 std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(const std::string& filename) {
41   // Remove old perf.data to avoid file ownership problems.
42   std::string err;
43   if (!android::base::RemoveFileIfExists(filename, &err)) {
44     LOG(ERROR) << "failed to remove file " << filename << ": " << err;
45     return nullptr;
46   }
47   FILE* fp = fopen(filename.c_str(), "web+");
48   if (fp == nullptr) {
49     PLOG(ERROR) << "failed to open record file '" << filename << "'";
50     return nullptr;
51   }
52 
53   return std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
54 }
55 
RecordFileWriter(const std::string & filename,FILE * fp)56 RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
57     : filename_(filename),
58       record_fp_(fp),
59       attr_section_offset_(0),
60       attr_section_size_(0),
61       data_section_offset_(0),
62       data_section_size_(0),
63       feature_section_offset_(0),
64       feature_count_(0) {
65 }
66 
~RecordFileWriter()67 RecordFileWriter::~RecordFileWriter() {
68   if (record_fp_ != nullptr) {
69     fclose(record_fp_);
70     unlink(filename_.c_str());
71   }
72 }
73 
WriteAttrSection(const std::vector<EventAttrWithId> & attr_ids)74 bool RecordFileWriter::WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids) {
75   if (attr_ids.empty()) {
76     return false;
77   }
78 
79   // Skip file header part.
80   if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
81     return false;
82   }
83 
84   // Write id section.
85   uint64_t id_section_offset;
86   if (!GetFilePos(&id_section_offset)) {
87     return false;
88   }
89   for (auto& attr_id : attr_ids) {
90     if (!Write(attr_id.ids.data(), attr_id.ids.size() * sizeof(uint64_t))) {
91       return false;
92     }
93   }
94 
95   // Write attr section.
96   uint64_t attr_section_offset;
97   if (!GetFilePos(&attr_section_offset)) {
98     return false;
99   }
100   for (auto& attr_id : attr_ids) {
101     FileAttr file_attr;
102     file_attr.attr = *attr_id.attr;
103     file_attr.ids.offset = id_section_offset;
104     file_attr.ids.size = attr_id.ids.size() * sizeof(uint64_t);
105     id_section_offset += file_attr.ids.size;
106     if (!Write(&file_attr, sizeof(file_attr))) {
107       return false;
108     }
109   }
110 
111   uint64_t data_section_offset;
112   if (!GetFilePos(&data_section_offset)) {
113     return false;
114   }
115 
116   attr_section_offset_ = attr_section_offset;
117   attr_section_size_ = data_section_offset - attr_section_offset;
118   data_section_offset_ = data_section_offset;
119 
120   // Save event_attr for use when reading records.
121   event_attr_ = *attr_ids[0].attr;
122   return true;
123 }
124 
WriteRecord(const Record & record)125 bool RecordFileWriter::WriteRecord(const Record& record) {
126   // linux-tools-perf only accepts records with size <= 65535 bytes. To make
127   // perf.data generated by simpleperf be able to be parsed by linux-tools-perf,
128   // Split simpleperf custom records which are > 65535 into a bunch of
129   // RECORD_SPLIT records, followed by a RECORD_SPLIT_END record.
130   constexpr uint32_t RECORD_SIZE_LIMIT = 65535;
131   if (record.size() <= RECORD_SIZE_LIMIT) {
132     bool result = WriteData(record.Binary(), record.size());
133     if (result && record.type() == PERF_RECORD_AUXTRACE) {
134       auto auxtrace = static_cast<const AuxTraceRecord*>(&record);
135       result = WriteData(auxtrace->location.addr, auxtrace->data->aux_size);
136     }
137     return result;
138   }
139   CHECK_GT(record.type(), SIMPLE_PERF_RECORD_TYPE_START);
140   const char* p = record.Binary();
141   uint32_t left_bytes = static_cast<uint32_t>(record.size());
142   RecordHeader header;
143   header.type = SIMPLE_PERF_RECORD_SPLIT;
144   char header_buf[Record::header_size()];
145   char* header_p;
146   while (left_bytes > 0) {
147     uint32_t bytes_to_write = std::min(RECORD_SIZE_LIMIT - Record::header_size(), left_bytes);
148     header.size = bytes_to_write + Record::header_size();
149     header_p = header_buf;
150     header.MoveToBinaryFormat(header_p);
151     if (!WriteData(header_buf, Record::header_size())) {
152       return false;
153     }
154     if (!WriteData(p, bytes_to_write)) {
155       return false;
156     }
157     p += bytes_to_write;
158     left_bytes -= bytes_to_write;
159   }
160   header.type = SIMPLE_PERF_RECORD_SPLIT_END;
161   header.size = Record::header_size();
162   header_p = header_buf;
163   header.MoveToBinaryFormat(header_p);
164   return WriteData(header_buf, Record::header_size());
165 }
166 
WriteData(const void * buf,size_t len)167 bool RecordFileWriter::WriteData(const void* buf, size_t len) {
168   if (!Write(buf, len)) {
169     return false;
170   }
171   data_section_size_ += len;
172   return true;
173 }
174 
Write(const void * buf,size_t len)175 bool RecordFileWriter::Write(const void* buf, size_t len) {
176   if (len != 0u && fwrite(buf, len, 1, record_fp_) != 1) {
177     PLOG(ERROR) << "failed to write to record file '" << filename_ << "'";
178     return false;
179   }
180   return true;
181 }
182 
Read(void * buf,size_t len)183 bool RecordFileWriter::Read(void* buf, size_t len) {
184   if (len != 0u && fread(buf, len, 1, record_fp_) != 1) {
185     PLOG(ERROR) << "failed to read record file '" << filename_ << "'";
186     return false;
187   }
188   return true;
189 }
190 
ReadDataSection(const std::function<void (const Record *)> & callback)191 bool RecordFileWriter::ReadDataSection(const std::function<void(const Record*)>& callback) {
192   if (fseek(record_fp_, data_section_offset_, SEEK_SET) == -1) {
193     PLOG(ERROR) << "fseek() failed";
194     return false;
195   }
196   std::vector<char> record_buf(512);
197   uint64_t read_pos = 0;
198   while (read_pos < data_section_size_) {
199     if (!Read(record_buf.data(), Record::header_size())) {
200       return false;
201     }
202     RecordHeader header(record_buf.data());
203     if (record_buf.size() < header.size) {
204       record_buf.resize(header.size);
205     }
206     if (!Read(record_buf.data() + Record::header_size(), header.size - Record::header_size())) {
207       return false;
208     }
209     read_pos += header.size;
210     std::unique_ptr<Record> r = ReadRecordFromBuffer(event_attr_, header.type, record_buf.data());
211     if (r->type() == PERF_RECORD_AUXTRACE) {
212       auto auxtrace = static_cast<AuxTraceRecord*>(r.get());
213       auxtrace->location.file_offset = data_section_offset_ + read_pos;
214       if (fseek(record_fp_, auxtrace->data->aux_size, SEEK_CUR) != 0) {
215         PLOG(ERROR) << "fseek() failed";
216         return false;
217       }
218       read_pos += auxtrace->data->aux_size;
219     }
220     callback(r.get());
221   }
222   return true;
223 }
224 
GetFilePos(uint64_t * file_pos)225 bool RecordFileWriter::GetFilePos(uint64_t* file_pos) {
226   off_t offset = ftello(record_fp_);
227   if (offset == -1) {
228     PLOG(ERROR) << "ftello() failed";
229     return false;
230   }
231   *file_pos = static_cast<uint64_t>(offset);
232   return true;
233 }
234 
BeginWriteFeatures(size_t feature_count)235 bool RecordFileWriter::BeginWriteFeatures(size_t feature_count) {
236   feature_section_offset_ = data_section_offset_ + data_section_size_;
237   feature_count_ = feature_count;
238   uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
239 
240   // Reserve enough space in the record file for the feature header.
241   std::vector<unsigned char> zero_data(feature_header_size);
242   if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
243     PLOG(ERROR) << "fseek() failed";
244     return false;
245   }
246   return Write(zero_data.data(), zero_data.size());
247 }
248 
WriteBuildIdFeature(const std::vector<BuildIdRecord> & build_id_records)249 bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
250   if (!WriteFeatureBegin(FEAT_BUILD_ID)) {
251     return false;
252   }
253   for (auto& record : build_id_records) {
254     if (!Write(record.Binary(), record.size())) {
255       return false;
256     }
257   }
258   return WriteFeatureEnd(FEAT_BUILD_ID);
259 }
260 
WriteStringWithLength(const std::string & s)261 bool RecordFileWriter::WriteStringWithLength(const std::string& s) {
262   uint32_t len = static_cast<uint32_t>(Align(s.size() + 1, 64));
263   if (!Write(&len, sizeof(len))) {
264     return false;
265   }
266   if (!Write(&s[0], s.size() + 1)) {
267     return false;
268   }
269   size_t pad_size = Align(s.size() + 1, 64) - s.size() - 1;
270   if (pad_size > 0u) {
271     char align_buf[pad_size];
272     memset(align_buf, '\0', pad_size);
273     if (!Write(align_buf, pad_size)) {
274       return false;
275     }
276   }
277   return true;
278 }
279 
WriteFeatureString(int feature,const std::string & s)280 bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
281   if (!WriteFeatureBegin(feature)) {
282     return false;
283   }
284   if (!WriteStringWithLength(s)) {
285     return false;
286   }
287   return WriteFeatureEnd(feature);
288 }
289 
WriteCmdlineFeature(const std::vector<std::string> & cmdline)290 bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
291   if (!WriteFeatureBegin(FEAT_CMDLINE)) {
292     return false;
293   }
294   uint32_t arg_count = cmdline.size();
295   if (!Write(&arg_count, sizeof(arg_count))) {
296     return false;
297   }
298   for (auto& arg : cmdline) {
299     if (!WriteStringWithLength(arg)) {
300       return false;
301     }
302   }
303   return WriteFeatureEnd(FEAT_CMDLINE);
304 }
305 
WriteBranchStackFeature()306 bool RecordFileWriter::WriteBranchStackFeature() {
307   if (!WriteFeatureBegin(FEAT_BRANCH_STACK)) {
308     return false;
309   }
310   return WriteFeatureEnd(FEAT_BRANCH_STACK);
311 }
312 
WriteAuxTraceFeature(const std::vector<uint64_t> & auxtrace_offset)313 bool RecordFileWriter::WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset) {
314   std::vector<uint64_t> data;
315   for (auto offset : auxtrace_offset) {
316     data.push_back(offset);
317     data.push_back(AuxTraceRecord::Size());
318   }
319   return WriteFeatureBegin(FEAT_AUXTRACE) && Write(data.data(), data.size() * sizeof(uint64_t)) &&
320          WriteFeatureEnd(FEAT_AUXTRACE);
321 }
322 
WriteFileFeatures(const std::vector<Dso * > & files)323 bool RecordFileWriter::WriteFileFeatures(const std::vector<Dso*>& files) {
324   for (Dso* dso : files) {
325     // Always want to dump dex file offsets for DSO_DEX_FILE type.
326     if (!dso->HasDumpId() && dso->type() != DSO_DEX_FILE) {
327       continue;
328     }
329     uint32_t dso_type = dso->type();
330     uint64_t min_vaddr;
331     uint64_t file_offset_of_min_vaddr;
332     dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
333 
334     // Dumping all symbols in hit files takes too much space, so only dump
335     // needed symbols.
336     const std::vector<Symbol>& symbols = dso->GetSymbols();
337     std::vector<const Symbol*> dump_symbols;
338     for (const auto& sym : symbols) {
339       if (sym.HasDumpId()) {
340         dump_symbols.push_back(&sym);
341       }
342     }
343     std::sort(dump_symbols.begin(), dump_symbols.end(), Symbol::CompareByAddr);
344 
345     const std::vector<uint64_t>* dex_file_offsets = dso->DexFileOffsets();
346     if (!WriteFileFeature(dso->Path(), dso_type, min_vaddr, file_offset_of_min_vaddr,
347                           dump_symbols, dex_file_offsets)) {
348       return false;
349     }
350   }
351   return true;
352 }
353 
WriteFileFeature(const std::string & file_path,uint32_t file_type,uint64_t min_vaddr,uint64_t file_offset_of_min_vaddr,const std::vector<const Symbol * > & symbols,const std::vector<uint64_t> * dex_file_offsets)354 bool RecordFileWriter::WriteFileFeature(const std::string& file_path,
355                                         uint32_t file_type,
356                                         uint64_t min_vaddr,
357                                         uint64_t file_offset_of_min_vaddr,
358                                         const std::vector<const Symbol*>& symbols,
359                                         const std::vector<uint64_t>* dex_file_offsets) {
360   uint32_t size = file_path.size() + 1 + sizeof(uint32_t) * 2 +
361       sizeof(uint64_t) + symbols.size() * (sizeof(uint64_t) + sizeof(uint32_t));
362   for (const auto& symbol : symbols) {
363     size += strlen(symbol->Name()) + 1;
364   }
365   if (dex_file_offsets != nullptr) {
366     size += sizeof(uint32_t) + sizeof(uint64_t) * dex_file_offsets->size();
367   }
368   if (file_type == DSO_ELF_FILE) {
369     size += sizeof(uint64_t);
370   }
371   std::vector<char> buf(sizeof(uint32_t) + size);
372   char* p = buf.data();
373   MoveToBinaryFormat(size, p);
374   MoveToBinaryFormat(file_path.c_str(), file_path.size() + 1, p);
375   MoveToBinaryFormat(file_type, p);
376   MoveToBinaryFormat(min_vaddr, p);
377   uint32_t symbol_count = static_cast<uint32_t>(symbols.size());
378   MoveToBinaryFormat(symbol_count, p);
379   for (const auto& symbol : symbols) {
380     MoveToBinaryFormat(symbol->addr, p);
381     uint32_t len = symbol->len;
382     MoveToBinaryFormat(len, p);
383     MoveToBinaryFormat(symbol->Name(), strlen(symbol->Name()) + 1, p);
384   }
385   if (dex_file_offsets != nullptr) {
386     uint32_t offset_count = dex_file_offsets->size();
387     MoveToBinaryFormat(offset_count, p);
388     MoveToBinaryFormat(dex_file_offsets->data(), offset_count, p);
389   }
390   if (file_type == DSO_ELF_FILE) {
391     MoveToBinaryFormat(file_offset_of_min_vaddr, p);
392   }
393   CHECK_EQ(buf.size(), static_cast<size_t>(p - buf.data()));
394 
395   return WriteFeature(FEAT_FILE, buf);
396 }
397 
WriteMetaInfoFeature(const std::unordered_map<std::string,std::string> & info_map)398 bool RecordFileWriter::WriteMetaInfoFeature(
399     const std::unordered_map<std::string, std::string>& info_map) {
400   uint32_t size = 0u;
401   for (auto& pair : info_map) {
402     size += pair.first.size() + 1;
403     size += pair.second.size() + 1;
404   }
405   std::vector<char> buf(size);
406   char* p = buf.data();
407   for (auto& pair : info_map) {
408     MoveToBinaryFormat(pair.first.c_str(), pair.first.size() + 1, p);
409     MoveToBinaryFormat(pair.second.c_str(), pair.second.size() + 1, p);
410   }
411   return WriteFeature(FEAT_META_INFO, buf);
412 }
413 
WriteFeature(int feature,const std::vector<char> & data)414 bool RecordFileWriter::WriteFeature(int feature, const std::vector<char>& data) {
415   return WriteFeatureBegin(feature) && Write(data.data(), data.size()) && WriteFeatureEnd(feature);
416 }
417 
WriteFeatureBegin(int feature)418 bool RecordFileWriter::WriteFeatureBegin(int feature) {
419   auto it = features_.find(feature);
420   if (it == features_.end()) {
421     CHECK_LT(features_.size(), feature_count_);
422     auto& sec = features_[feature];
423     if (!GetFilePos(&sec.offset)) {
424       return false;
425     }
426     sec.size = 0;
427   }
428   return true;
429 }
430 
WriteFeatureEnd(int feature)431 bool RecordFileWriter::WriteFeatureEnd(int feature) {
432   auto it = features_.find(feature);
433   if (it == features_.end()) {
434     return false;
435   }
436   uint64_t offset;
437   if (!GetFilePos(&offset)) {
438     return false;
439   }
440   it->second.size = offset - it->second.offset;
441   return true;
442 }
443 
EndWriteFeatures()444 bool RecordFileWriter::EndWriteFeatures() {
445   // Used features (features_.size()) should be <= allocated feature space.
446   CHECK_LE(features_.size(), feature_count_);
447   if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
448     PLOG(ERROR) << "fseek() failed";
449     return false;
450   }
451   for (const auto& pair : features_) {
452     if (!Write(&pair.second, sizeof(SectionDesc))) {
453       return false;
454     }
455   }
456   return true;
457 }
458 
WriteFileHeader()459 bool RecordFileWriter::WriteFileHeader() {
460   FileHeader header;
461   memset(&header, 0, sizeof(header));
462   memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
463   header.header_size = sizeof(header);
464   header.attr_size = sizeof(FileAttr);
465   header.attrs.offset = attr_section_offset_;
466   header.attrs.size = attr_section_size_;
467   header.data.offset = data_section_offset_;
468   header.data.size = data_section_size_;
469   for (const auto& pair : features_) {
470     int i = pair.first / 8;
471     int j = pair.first % 8;
472     header.features[i] |= (1 << j);
473   }
474 
475   if (fseek(record_fp_, 0, SEEK_SET) == -1) {
476     return false;
477   }
478   if (!Write(&header, sizeof(header))) {
479     return false;
480   }
481   return true;
482 }
483 
Close()484 bool RecordFileWriter::Close() {
485   CHECK(record_fp_ != nullptr);
486   bool result = true;
487 
488   // Write file header. We gather enough information to write file header only after
489   // writing data section and feature section.
490   if (!WriteFileHeader()) {
491     result = false;
492   }
493 
494   if (fclose(record_fp_) != 0) {
495     PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
496     result = false;
497   }
498   record_fp_ = nullptr;
499   return result;
500 }
501