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