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