cmd_report_sample.cpp 25 KB

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