1 /*
2  * Copyright (C) 2016 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 <inttypes.h>
18 
19 #include <limits>
20 #include <memory>
21 
22 #include <android-base/strings.h>
23 
24 #include "system/extras/simpleperf/report_sample.pb.h"
25 
26 #include <google/protobuf/io/coded_stream.h>
27 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
28 
29 #include "command.h"
30 #include "event_attr.h"
31 #include "event_type.h"
32 #include "record_file.h"
33 #include "thread_tree.h"
34 #include "utils.h"
35 
36 using namespace simpleperf;
37 namespace proto = simpleperf_report_proto;
38 
39 namespace {
40 
41 static const char PROT_FILE_MAGIC[] = "SIMPLEPERF";
42 static const uint16_t PROT_FILE_VERSION = 1u;
43 
44 class ProtobufFileWriter : public google::protobuf::io::CopyingOutputStream {
45  public:
ProtobufFileWriter(FILE * out_fp)46   explicit ProtobufFileWriter(FILE* out_fp) : out_fp_(out_fp) {}
47 
Write(const void * buffer,int size)48   bool Write(const void* buffer, int size) override {
49     return fwrite(buffer, size, 1, out_fp_) == 1;
50   }
51 
52  private:
53   FILE* out_fp_;
54 };
55 
56 class ProtobufFileReader : public google::protobuf::io::CopyingInputStream {
57  public:
ProtobufFileReader(FILE * in_fp)58   explicit ProtobufFileReader(FILE* in_fp) : in_fp_(in_fp) {}
59 
Read(void * buffer,int size)60   int Read(void* buffer, int size) override {
61     return fread(buffer, 1, size, in_fp_);
62   }
63 
64  private:
65   FILE* in_fp_;
66 };
67 
68 struct CallEntry {
69   Dso* dso;
70   const Symbol* symbol;
71   uint64_t vaddr_in_file;
72 };
73 
74 class ReportSampleCommand : public Command {
75  public:
ReportSampleCommand()76   ReportSampleCommand()
77       : Command(
78             "report-sample", "report raw sample information in perf.data",
79             // clang-format off
80 "Usage: simpleperf report-sample [options]\n"
81 "--dump-protobuf-report  <file>\n"
82 "           Dump report file generated by\n"
83 "           `simpleperf report-sample --protobuf -o <file>`.\n"
84 "-i <file>  Specify path of record file, default is perf.data.\n"
85 "-o report_file_name  Set report file name. Default report file name is\n"
86 "                     report_sample.trace if --protobuf is used, otherwise\n"
87 "                     the report is written to stdout.\n"
88 "--protobuf  Use protobuf format in report_sample.proto to output samples.\n"
89 "            Need to set a report_file_name when using this option.\n"
90 "--show-callchain  Print callchain samples.\n"
91 "--remove-unknown-kernel-symbols  Remove kernel callchains when kernel symbols\n"
92 "                                 are not available in perf.data.\n"
93 "--show-art-frames  Show frames of internal methods in the ART Java interpreter.\n"
94 "--symdir <dir>     Look for files with symbols in a directory recursively.\n"
95             // clang-format on
96             ),
97         record_filename_("perf.data"),
98         show_callchain_(false),
99         use_protobuf_(false),
100         report_fp_(nullptr),
101         coded_os_(nullptr),
102         sample_count_(0),
103         lost_count_(0),
104         trace_offcpu_(false),
105         remove_unknown_kernel_symbols_(false),
106         kernel_symbols_available_(false),
107         show_art_frames_(false) {}
108 
109   bool Run(const std::vector<std::string>& args) override;
110 
111  private:
112   bool ParseOptions(const std::vector<std::string>& args);
113   bool DumpProtobufReport(const std::string& filename);
114   bool OpenRecordFile();
115   bool PrintMetaInfo();
116   bool ProcessRecord(std::unique_ptr<Record> record);
117   void UpdateThreadName(uint32_t pid, uint32_t tid);
118   bool ProcessSampleRecord(const SampleRecord& r);
119   bool PrintSampleRecordInProtobuf(const SampleRecord& record,
120                                    const std::vector<CallEntry>& entries);
121   bool GetCallEntry(const ThreadEntry* thread, bool in_kernel, uint64_t ip, bool omit_unknown_dso,
122                     CallEntry* entry);
123   bool WriteRecordInProtobuf(proto::Record& proto_record);
124   bool PrintLostSituationInProtobuf();
125   bool PrintFileInfoInProtobuf();
126   bool PrintThreadInfoInProtobuf();
127   bool PrintSampleRecord(const SampleRecord& record, const std::vector<CallEntry>& entries);
128   void PrintLostSituation();
129 
130   std::string record_filename_;
131   std::unique_ptr<RecordFileReader> record_file_reader_;
132   std::string dump_protobuf_report_file_;
133   bool show_callchain_;
134   bool use_protobuf_;
135   ThreadTree thread_tree_;
136   std::string report_filename_;
137   FILE* report_fp_;
138   google::protobuf::io::CodedOutputStream* coded_os_;
139   size_t sample_count_;
140   size_t lost_count_;
141   bool trace_offcpu_;
142   std::vector<std::string> event_types_;
143   bool remove_unknown_kernel_symbols_;
144   bool kernel_symbols_available_;
145   bool show_art_frames_;
146   // map from <pid, tid> to thread name
147   std::map<uint64_t, const char*> thread_names_;
148 };
149 
Run(const std::vector<std::string> & args)150 bool ReportSampleCommand::Run(const std::vector<std::string>& args) {
151   // 1. Parse options.
152   if (!ParseOptions(args)) {
153     return false;
154   }
155   // 2. Prepare report fp.
156   report_fp_ = stdout;
157   std::unique_ptr<FILE, decltype(&fclose)> fp(nullptr, fclose);
158   if (!report_filename_.empty()) {
159     const char* open_mode = use_protobuf_ ? "wb" : "w";
160     fp.reset(fopen(report_filename_.c_str(), open_mode));
161     if (fp == nullptr) {
162       PLOG(ERROR) << "failed to open " << report_filename_;
163       return false;
164     }
165     report_fp_ = fp.get();
166   }
167 
168   // 3. Dump protobuf report.
169   if (!dump_protobuf_report_file_.empty()) {
170     return DumpProtobufReport(dump_protobuf_report_file_);
171   }
172 
173   // 4. Open record file.
174   if (!OpenRecordFile()) {
175     return false;
176   }
177   if (use_protobuf_) {
178     GOOGLE_PROTOBUF_VERIFY_VERSION;
179   } else {
180     thread_tree_.ShowMarkForUnknownSymbol();
181     thread_tree_.ShowIpForUnknownSymbol();
182   }
183 
184   // 5. Prepare protobuf output stream.
185   std::unique_ptr<ProtobufFileWriter> protobuf_writer;
186   std::unique_ptr<google::protobuf::io::CopyingOutputStreamAdaptor> protobuf_os;
187   std::unique_ptr<google::protobuf::io::CodedOutputStream> protobuf_coded_os;
188   if (use_protobuf_) {
189     if (fprintf(report_fp_, "%s", PROT_FILE_MAGIC) != 10 ||
190         fwrite(&PROT_FILE_VERSION, sizeof(uint16_t), 1, report_fp_) != 1u) {
191       PLOG(ERROR) << "Failed to write magic/version";
192       return false;
193     }
194     protobuf_writer.reset(new ProtobufFileWriter(report_fp_));
195     protobuf_os.reset(new google::protobuf::io::CopyingOutputStreamAdaptor(
196         protobuf_writer.get()));
197     protobuf_coded_os.reset(
198         new google::protobuf::io::CodedOutputStream(protobuf_os.get()));
199     coded_os_ = protobuf_coded_os.get();
200   }
201 
202   // 6. Read record file, and print samples online.
203   if (!PrintMetaInfo()) {
204     return false;
205   }
206   if (!record_file_reader_->ReadDataSection(
207           [this](std::unique_ptr<Record> record) {
208             return ProcessRecord(std::move(record));
209           })) {
210     return false;
211   }
212 
213   if (use_protobuf_) {
214     if (!PrintLostSituationInProtobuf()) {
215       return false;
216     }
217     if (!PrintFileInfoInProtobuf()) {
218       return false;
219     }
220     if (!PrintThreadInfoInProtobuf()) {
221       return false;
222     }
223     coded_os_->WriteLittleEndian32(0);
224     if (coded_os_->HadError()) {
225       LOG(ERROR) << "print protobuf report failed";
226       return false;
227     }
228     protobuf_coded_os.reset(nullptr);
229   } else {
230     PrintLostSituation();
231     fflush(report_fp_);
232   }
233   if (ferror(report_fp_) != 0) {
234     PLOG(ERROR) << "print report failed";
235     return false;
236   }
237   return true;
238 }
239 
ParseOptions(const std::vector<std::string> & args)240 bool ReportSampleCommand::ParseOptions(const std::vector<std::string>& args) {
241   for (size_t i = 0; i < args.size(); ++i) {
242     if (args[i] == "--dump-protobuf-report") {
243       if (!NextArgumentOrError(args, &i)) {
244         return false;
245       }
246       dump_protobuf_report_file_ = args[i];
247     } else if (args[i] == "-i") {
248       if (!NextArgumentOrError(args, &i)) {
249         return false;
250       }
251       record_filename_ = args[i];
252     } else if (args[i] == "-o") {
253       if (!NextArgumentOrError(args, &i)) {
254         return false;
255       }
256       report_filename_ = args[i];
257     } else if (args[i] == "--protobuf") {
258       use_protobuf_ = true;
259     } else if (args[i] == "--show-callchain") {
260       show_callchain_ = true;
261     } else if (args[i] == "--remove-unknown-kernel-symbols") {
262       remove_unknown_kernel_symbols_ = true;
263     } else if (args[i] == "--show-art-frames") {
264       show_art_frames_ = true;
265     } else if (args[i] == "--symdir") {
266       if (!NextArgumentOrError(args, &i)) {
267         return false;
268       }
269       if (!Dso::AddSymbolDir(args[i])) {
270         return false;
271       }
272     } else {
273       ReportUnknownOption(args, i);
274       return false;
275     }
276   }
277 
278   if (use_protobuf_ && report_filename_.empty()) {
279     report_filename_ = "report_sample.trace";
280   }
281   return true;
282 }
283 
DumpProtobufReport(const std::string & filename)284 bool ReportSampleCommand::DumpProtobufReport(const std::string& filename) {
285   GOOGLE_PROTOBUF_VERIFY_VERSION;
286   std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "rb"),
287                                               fclose);
288   if (fp == nullptr) {
289     PLOG(ERROR) << "failed to open " << filename;
290     return false;
291   }
292   char magic[11] = {};
293   if (fread(magic, 10, 1, fp.get()) != 1u || memcmp(magic, PROT_FILE_MAGIC, 10) != 0) {
294     PLOG(ERROR) << filename << " isn't a file generated by report-sample command.";
295     return false;
296   }
297   FprintIndented(report_fp_, 0, "magic: %s\n", magic);
298   uint16_t version;
299   if (fread(&version, sizeof(uint16_t), 1, fp.get()) != 1u || version != PROT_FILE_VERSION) {
300     PLOG(ERROR) << filename << " doesn't have the expected version.";
301     return false;
302   }
303   FprintIndented(report_fp_, 0, "version: %u\n", version);
304 
305   ProtobufFileReader protobuf_reader(fp.get());
306   google::protobuf::io::CopyingInputStreamAdaptor adaptor(&protobuf_reader);
307   google::protobuf::io::CodedInputStream coded_is(&adaptor);
308   // map from file_id to max_symbol_id requested on the file.
309   std::unordered_map<uint32_t, int32_t> max_symbol_id_map;
310   // files[file_id] is the number of symbols in the file.
311   std::vector<uint32_t> files;
312   uint32_t max_message_size = 64 * (1 << 20);
313   uint32_t warning_message_size = 512 * (1 << 20);
314   coded_is.SetTotalBytesLimit(max_message_size, warning_message_size);
315   while (true) {
316     uint32_t size;
317     if (!coded_is.ReadLittleEndian32(&size)) {
318       PLOG(ERROR) << "failed to read " << filename;
319       return false;
320     }
321     if (size == 0) {
322       break;
323     }
324     // Handle files having large symbol table.
325     if (size > max_message_size) {
326       max_message_size = size;
327       coded_is.SetTotalBytesLimit(max_message_size, warning_message_size);
328     }
329     auto limit = coded_is.PushLimit(size);
330     proto::Record proto_record;
331     if (!proto_record.ParseFromCodedStream(&coded_is)) {
332       PLOG(ERROR) << "failed to read " << filename;
333       return false;
334     }
335     coded_is.PopLimit(limit);
336     if (proto_record.has_sample()) {
337       auto& sample = proto_record.sample();
338       static size_t sample_count = 0;
339       FprintIndented(report_fp_, 0, "sample %zu:\n", ++sample_count);
340       FprintIndented(report_fp_, 1, "event_type_id: %zu\n", sample.event_type_id());
341       FprintIndented(report_fp_, 1, "time: %" PRIu64 "\n", sample.time());
342       FprintIndented(report_fp_, 1, "event_count: %" PRIu64 "\n", sample.event_count());
343       FprintIndented(report_fp_, 1, "thread_id: %d\n", sample.thread_id());
344       FprintIndented(report_fp_, 1, "callchain:\n");
345       for (int i = 0; i < sample.callchain_size(); ++i) {
346         const proto::Sample_CallChainEntry& callchain = sample.callchain(i);
347         FprintIndented(report_fp_, 2, "vaddr_in_file: %" PRIx64 "\n",
348                        callchain.vaddr_in_file());
349         FprintIndented(report_fp_, 2, "file_id: %u\n", callchain.file_id());
350         int32_t symbol_id = callchain.symbol_id();
351         FprintIndented(report_fp_, 2, "symbol_id: %d\n", symbol_id);
352         if (symbol_id < -1) {
353           LOG(ERROR) << "unexpected symbol_id " << symbol_id;
354           return false;
355         }
356         if (symbol_id != -1) {
357           max_symbol_id_map[callchain.file_id()] =
358               std::max(max_symbol_id_map[callchain.file_id()], symbol_id);
359         }
360       }
361     } else if (proto_record.has_lost()) {
362       auto& lost = proto_record.lost();
363       FprintIndented(report_fp_, 0, "lost_situation:\n");
364       FprintIndented(report_fp_, 1, "sample_count: %" PRIu64 "\n",
365                      lost.sample_count());
366       FprintIndented(report_fp_, 1, "lost_count: %" PRIu64 "\n",
367                      lost.lost_count());
368     } else if (proto_record.has_file()) {
369       auto& file = proto_record.file();
370       FprintIndented(report_fp_, 0, "file:\n");
371       FprintIndented(report_fp_, 1, "id: %u\n", file.id());
372       FprintIndented(report_fp_, 1, "path: %s\n", file.path().c_str());
373       for (int i = 0; i < file.symbol_size(); ++i) {
374         FprintIndented(report_fp_, 1, "symbol: %s\n", file.symbol(i).c_str());
375       }
376       for (int i = 0; i < file.mangled_symbol_size(); ++i) {
377         FprintIndented(report_fp_, 1, "mangled_symbol: %s\n", file.mangled_symbol(i).c_str());
378       }
379       if (file.id() != files.size()) {
380         LOG(ERROR) << "file id doesn't increase orderly, expected "
381                    << files.size() << ", really " << file.id();
382         return false;
383       }
384       files.push_back(file.symbol_size());
385     } else if (proto_record.has_thread()) {
386       auto& thread = proto_record.thread();
387       FprintIndented(report_fp_, 0, "thread:\n");
388       FprintIndented(report_fp_, 1, "thread_id: %u\n", thread.thread_id());
389       FprintIndented(report_fp_, 1, "process_id: %u\n", thread.process_id());
390       FprintIndented(report_fp_, 1, "thread_name: %s\n", thread.thread_name().c_str());
391     } else if (proto_record.has_meta_info()) {
392       auto& meta_info = proto_record.meta_info();
393       FprintIndented(report_fp_, 0, "meta_info:\n");
394       for (int i = 0; i < meta_info.event_type_size(); ++i) {
395         FprintIndented(report_fp_, 1, "event_type: %s\n", meta_info.event_type(i).c_str());
396       }
397       if (meta_info.has_app_package_name()) {
398         FprintIndented(report_fp_, 0, "app_package_name: %s\n",
399                        meta_info.app_package_name().c_str());
400       }
401     } else {
402       LOG(ERROR) << "unexpected record type ";
403       return false;
404     }
405   }
406   for (auto pair : max_symbol_id_map) {
407     if (pair.first >= files.size()) {
408       LOG(ERROR) << "file_id(" << pair.first << ") >= file count ("
409                  << files.size() << ")";
410       return false;
411     }
412     if (static_cast<uint32_t>(pair.second) >= files[pair.first]) {
413       LOG(ERROR) << "symbol_id(" << pair.second << ") >= symbol count ("
414                  << files[pair.first] << ") in file_id( " << pair.first << ")";
415       return false;
416     }
417   }
418   return true;
419 }
420 
OpenRecordFile()421 bool ReportSampleCommand::OpenRecordFile() {
422   record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
423   if (record_file_reader_ == nullptr) {
424     return false;
425   }
426   record_file_reader_->LoadBuildIdAndFileFeatures(thread_tree_);
427   auto& meta_info = record_file_reader_->GetMetaInfoFeature();
428   if (auto it = meta_info.find("trace_offcpu"); it != meta_info.end()) {
429     trace_offcpu_ = it->second == "true";
430   }
431   if (auto it = meta_info.find("kernel_symbols_available"); it != meta_info.end()) {
432     kernel_symbols_available_ = it->second == "true";
433   }
434   for (EventAttrWithId& attr : record_file_reader_->AttrSection()) {
435     event_types_.push_back(GetEventNameByAttr(*attr.attr));
436   }
437   return true;
438 }
439 
PrintMetaInfo()440 bool ReportSampleCommand::PrintMetaInfo() {
441   auto& meta_info = record_file_reader_->GetMetaInfoFeature();
442   auto it = meta_info.find("app_package_name");
443   std::string app_package_name = it != meta_info.end() ? it->second : "";
444   if (use_protobuf_) {
445     proto::Record proto_record;
446     proto::MetaInfo* meta_info = proto_record.mutable_meta_info();
447     for (auto& event_type : event_types_) {
448       *(meta_info->add_event_type()) = event_type;
449     }
450     if (!app_package_name.empty()) {
451       meta_info->set_app_package_name(app_package_name);
452     }
453     return WriteRecordInProtobuf(proto_record);
454   }
455   FprintIndented(report_fp_, 0, "meta_info:\n");
456   FprintIndented(report_fp_, 1, "trace_offcpu: %s\n", trace_offcpu_ ? "true" : "false");
457   for (auto& event_type : event_types_) {
458     FprintIndented(report_fp_, 1, "event_type: %s\n", event_type.c_str());
459   }
460   if (!app_package_name.empty()) {
461     FprintIndented(report_fp_, 1, "app_package_name: %s\n", app_package_name.c_str());
462   }
463   return true;
464 }
465 
ProcessRecord(std::unique_ptr<Record> record)466 bool ReportSampleCommand::ProcessRecord(std::unique_ptr<Record> record) {
467   thread_tree_.Update(*record);
468   if (record->type() == PERF_RECORD_SAMPLE) {
469     return ProcessSampleRecord(*static_cast<SampleRecord*>(record.get()));
470   }
471   if (record->type() == PERF_RECORD_LOST) {
472     lost_count_ += static_cast<const LostRecord*>(record.get())->lost;
473   }
474   return true;
475 }
476 
ProcessSampleRecord(const SampleRecord & r)477 bool ReportSampleCommand::ProcessSampleRecord(const SampleRecord& r) {
478   size_t kernel_ip_count;
479   std::vector<uint64_t> ips = r.GetCallChain(&kernel_ip_count);
480   if (kernel_ip_count > 0u && remove_unknown_kernel_symbols_ && !kernel_symbols_available_) {
481     ips.erase(ips.begin(), ips.begin() + kernel_ip_count);
482     kernel_ip_count = 0;
483   }
484   if (ips.empty()) {
485     return true;
486   }
487   if (!show_callchain_) {
488     ips.resize(1);
489     kernel_ip_count = std::min(kernel_ip_count, static_cast<size_t>(1u));
490   }
491   sample_count_++;
492   std::vector<CallEntry> entries;
493   bool near_java_method = false;
494   auto is_entry_for_interpreter = [](const CallEntry& entry) {
495     return android::base::EndsWith(entry.dso->Path(), "/libart.so");
496   };
497   const ThreadEntry* thread = thread_tree_.FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
498   for (size_t i = 0; i < ips.size(); ++i) {
499     bool omit_unknown_dso = i > 0u;
500     CallEntry entry;
501     if (!GetCallEntry(thread, i < kernel_ip_count, ips[i], omit_unknown_dso, &entry)) {
502       break;
503     }
504     if (!show_art_frames_) {
505       // Remove interpreter frames both before and after the Java frame.
506       if (entry.dso->IsForJavaMethod()) {
507         near_java_method = true;
508         while (!entries.empty() && is_entry_for_interpreter(entries.back())) {
509           entries.pop_back();
510         }
511       } else if (is_entry_for_interpreter(entry)) {
512         if (near_java_method) {
513           continue;
514         }
515       } else {
516         near_java_method = false;
517       }
518     }
519     entries.push_back(entry);
520   }
521   if (use_protobuf_) {
522     uint64_t key = (static_cast<uint64_t>(r.tid_data.pid) << 32) | r.tid_data.tid;
523     thread_names_[key] = thread->comm;
524     return PrintSampleRecordInProtobuf(r, entries);
525   }
526   return PrintSampleRecord(r, entries);
527 }
528 
PrintSampleRecordInProtobuf(const SampleRecord & r,const std::vector<CallEntry> & entries)529 bool ReportSampleCommand::PrintSampleRecordInProtobuf(const SampleRecord& r,
530                                                       const std::vector<CallEntry>& entries) {
531   proto::Record proto_record;
532   proto::Sample* sample = proto_record.mutable_sample();
533   sample->set_time(r.time_data.time);
534   sample->set_event_count(r.period_data.period);
535   sample->set_thread_id(r.tid_data.tid);
536   sample->set_event_type_id(record_file_reader_->GetAttrIndexOfRecord(&r));
537 
538   for (const CallEntry& node : entries) {
539     proto::Sample_CallChainEntry* callchain = sample->add_callchain();
540     uint32_t file_id;
541     if (!node.dso->GetDumpId(&file_id)) {
542       file_id = node.dso->CreateDumpId();
543     }
544     int32_t symbol_id = -1;
545     if (node.symbol != thread_tree_.UnknownSymbol()) {
546       if (!node.symbol->GetDumpId(reinterpret_cast<uint32_t*>(&symbol_id))) {
547         symbol_id = node.dso->CreateSymbolDumpId(node.symbol);
548       }
549     }
550     callchain->set_vaddr_in_file(node.vaddr_in_file);
551     callchain->set_file_id(file_id);
552     callchain->set_symbol_id(symbol_id);
553 
554     // Android studio wants a clear call chain end to notify whether a call chain is complete.
555     // For the main thread, the call chain ends at __libc_init in libc.so. For other threads,
556     // the call chain ends at __start_thread in libc.so.
557     // The call chain of the main thread can go beyond __libc_init, to _start (<= android O) or
558     // _start_main (> android O).
559     if (node.dso->FileName() == "libc.so" &&
560         (strcmp(node.symbol->Name(), "__libc_init") == 0 ||
561             strcmp(node.symbol->Name(), "__start_thread") == 0)) {
562       break;
563     }
564   }
565   return WriteRecordInProtobuf(proto_record);
566 }
567 
WriteRecordInProtobuf(proto::Record & proto_record)568 bool ReportSampleCommand::WriteRecordInProtobuf(proto::Record& proto_record) {
569   coded_os_->WriteLittleEndian32(proto_record.ByteSize());
570   if (!proto_record.SerializeToCodedStream(coded_os_)) {
571     LOG(ERROR) << "failed to write record to protobuf";
572     return false;
573   }
574   return true;
575 }
576 
GetCallEntry(const ThreadEntry * thread,bool in_kernel,uint64_t ip,bool omit_unknown_dso,CallEntry * entry)577 bool ReportSampleCommand::GetCallEntry(const ThreadEntry* thread,
578                                        bool in_kernel, uint64_t ip,
579                                        bool omit_unknown_dso,
580                                        CallEntry* entry) {
581   const MapEntry* map = thread_tree_.FindMap(thread, ip, in_kernel);
582   if (omit_unknown_dso && thread_tree_.IsUnknownDso(map->dso)) {
583     return false;
584   }
585   entry->symbol = thread_tree_.FindSymbol(map, ip, &(entry->vaddr_in_file), &(entry->dso));
586   // If we can't find symbol, use the dso shown in the map.
587   if (entry->symbol == thread_tree_.UnknownSymbol()) {
588     entry->dso = map->dso;
589   }
590   return true;
591 }
592 
PrintLostSituationInProtobuf()593 bool ReportSampleCommand::PrintLostSituationInProtobuf() {
594   proto::Record proto_record;
595   proto::LostSituation* lost = proto_record.mutable_lost();
596   lost->set_sample_count(sample_count_);
597   lost->set_lost_count(lost_count_);
598   return WriteRecordInProtobuf(proto_record);
599 }
600 
CompareDsoByDumpId(Dso * d1,Dso * d2)601 static bool CompareDsoByDumpId(Dso* d1, Dso* d2) {
602   uint32_t id1 = UINT_MAX;
603   d1->GetDumpId(&id1);
604   uint32_t id2 = UINT_MAX;
605   d2->GetDumpId(&id2);
606   return id1 < id2;
607 }
608 
PrintFileInfoInProtobuf()609 bool ReportSampleCommand::PrintFileInfoInProtobuf() {
610   std::vector<Dso*> dsos = thread_tree_.GetAllDsos();
611   std::sort(dsos.begin(), dsos.end(), CompareDsoByDumpId);
612   for (Dso* dso : dsos) {
613     uint32_t file_id;
614     if (!dso->GetDumpId(&file_id)) {
615       continue;
616     }
617     proto::Record proto_record;
618     proto::File* file = proto_record.mutable_file();
619     file->set_id(file_id);
620     file->set_path(std::string{dso->GetReportPath()});
621     const std::vector<Symbol>& symbols = dso->GetSymbols();
622     std::vector<const Symbol*> dump_symbols;
623     for (const auto& sym : symbols) {
624       if (sym.HasDumpId()) {
625         dump_symbols.push_back(&sym);
626       }
627     }
628     std::sort(dump_symbols.begin(), dump_symbols.end(),
629               Symbol::CompareByDumpId);
630 
631     for (const auto& sym : dump_symbols) {
632       std::string* symbol = file->add_symbol();
633       *symbol = sym->DemangledName();
634       std::string* mangled_symbol = file->add_mangled_symbol();
635       *mangled_symbol = sym->Name();
636     }
637     if (!WriteRecordInProtobuf(proto_record)) {
638       return false;
639     }
640   }
641   return true;
642 }
643 
PrintThreadInfoInProtobuf()644 bool ReportSampleCommand::PrintThreadInfoInProtobuf() {
645   for (const auto& p : thread_names_) {
646     uint32_t pid = p.first >> 32;
647     uint32_t tid = p.first & std::numeric_limits<uint32_t>::max();
648     proto::Record proto_record;
649     proto::Thread* proto_thread = proto_record.mutable_thread();
650     proto_thread->set_thread_id(tid);
651     proto_thread->set_process_id(pid);
652     proto_thread->set_thread_name(p.second);
653     if (!WriteRecordInProtobuf(proto_record)) {
654       return false;
655     }
656   }
657   return true;
658 }
659 
PrintSampleRecord(const SampleRecord & r,const std::vector<CallEntry> & entries)660 bool ReportSampleCommand::PrintSampleRecord(const SampleRecord& r,
661                                             const std::vector<CallEntry>& entries) {
662   FprintIndented(report_fp_, 0, "sample:\n");
663   FprintIndented(report_fp_, 1, "event_type: %s\n",
664                  event_types_[record_file_reader_->GetAttrIndexOfRecord(&r)].data());
665   FprintIndented(report_fp_, 1, "time: %" PRIu64 "\n", r.time_data.time);
666   FprintIndented(report_fp_, 1, "event_count: %" PRIu64 "\n", r.period_data.period);
667   FprintIndented(report_fp_, 1, "thread_id: %d\n", r.tid_data.tid);
668   const char* thread_name = thread_tree_.FindThreadOrNew(r.tid_data.pid, r.tid_data.tid)->comm;
669   FprintIndented(report_fp_, 1, "thread_name: %s\n", thread_name);
670   CHECK(!entries.empty());
671   FprintIndented(report_fp_, 1, "vaddr_in_file: %" PRIx64 "\n", entries[0].vaddr_in_file);
672   FprintIndented(report_fp_, 1, "file: %s\n", entries[0].dso->GetReportPath().data());
673   FprintIndented(report_fp_, 1, "symbol: %s\n", entries[0].symbol->DemangledName());
674 
675   if (entries.size() > 1u) {
676     FprintIndented(report_fp_, 1, "callchain:\n");
677     for (size_t i = 1u; i < entries.size(); ++i) {
678       FprintIndented(report_fp_, 2, "vaddr_in_file: %" PRIx64 "\n", entries[i].vaddr_in_file);
679       FprintIndented(report_fp_, 2, "file: %s\n", entries[i].dso->GetReportPath().data());
680       FprintIndented(report_fp_, 2, "symbol: %s\n", entries[i].symbol->DemangledName());
681     }
682   }
683   return true;
684 }
685 
PrintLostSituation()686 void ReportSampleCommand::PrintLostSituation() {
687   FprintIndented(report_fp_, 0, "lost_situation:\n");
688   FprintIndented(report_fp_, 1, "sample_count: %" PRIu64 "\n", sample_count_);
689   FprintIndented(report_fp_, 1, "lost_count: %" PRIu64 "\n", lost_count_);
690 }
691 
692 }  // namespace
693 
694 namespace simpleperf {
695 
RegisterReportSampleCommand()696 void RegisterReportSampleCommand() {
697   RegisterCommand("report-sample", [] {
698     return std::unique_ptr<Command>(new ReportSampleCommand());
699   });
700 }
701 
702 }  // namespace simpleperf
703