1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SUBCOMMAND_DUMP_H 16 #define SUBCOMMAND_DUMP_H 17 18 #include "perf_file_reader.h" 19 20 #include <memory> 21 22 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF 23 #include "report_protobuf_file.h" 24 #endif 25 #include "subcommand.h" 26 #include "symbols_file.h" 27 #include "virtual_runtime.h" 28 29 namespace OHOS { 30 namespace Developtools { 31 namespace HiPerf { 32 static const std::string DEFAULT_DUMP_FILENAME = "perf.data"; 33 34 class SubCommandDump : public SubCommand { 35 public: SubCommandDump()36 SubCommandDump() 37 // clang-format off 38 : SubCommand("dump", "Dump content of a perf data file, like perf.data", 39 40 "Usage: hiperf dump [option] <filename>\n" 41 " Dump specific parts of specified file .\n" 42 " --head\n" 43 " Dump header and attrs only.\n" 44 " -d\n" 45 " Dump data section only.\n" 46 " -f\n" 47 " Dump addtional features only.\n" 48 " --sympath <symbols path>\n" 49 " use symbols path to find symbols.\n" 50 " -i <file name>\n" 51 " perf data file to dump, default is perf.data\n" 52 " --elf <elf file name>\n" 53 " dump elf not perf data.\n" 54 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF 55 " --proto <protobuf file name>\n" 56 " dump perf data from protobuf file.\n" 57 #endif 58 " --export <sample index>\n" 59 " also export the user stack data to some split file,\n" 60 " use this command to produce ut data.\n" 61 " named with sample index(0 base):\n" 62 " hiperf_<pid>_<tid>_user_regs_<index>.dump\n" 63 " hiperf_<pid>_<tid>_user_data_<index>.dump\n" 64 " -o <filename>\n" 65 " dump file name. if empty will use stdout print\n" 66 ) 67 // clang-format on 68 { 69 } 70 ~SubCommandDump() override; 71 72 bool OnSubCommand(std::vector<std::string> &args) override; 73 bool ParseOption(std::vector<std::string> &args) override; 74 75 static bool RegisterSubCommandDump(void); 76 77 static void DumpPrintEventAttr(const perf_event_attr &attr, int indent = 0); 78 std::unique_ptr<PerfFileReader> reader_; 79 80 private: 81 static void DumpSampleType(uint64_t sampleType, int indent); 82 bool PrepareDumpOutput(); 83 int exportSampleIndex_ = -1; 84 int currectSampleIndex_ = 0; 85 std::string dumpFileName_; 86 std::string elfFileName_; 87 std::string outputFilename_ = ""; 88 std::string protobufDumpFileName_; 89 int indent_ = 0; 90 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF 91 std::unique_ptr<ReportProtobufFileReader> protobufInputFileReader_ = nullptr; 92 #endif 93 94 std::vector<AttrWithId> attrIds_; 95 96 bool dumpHeader_ = false; 97 bool dumpFeatures_ = false; 98 bool dumpData_ = false; 99 bool dumpAll_ = true; 100 101 std::vector<std::string> dumpSymbolsPaths_; 102 103 bool CheckInputFile(); 104 bool DumpElfFile(); 105 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF 106 bool DumpProtoFile(); 107 #endif 108 void DumpPrintFileHeader(int indent = 0); 109 void DumpAttrPortion(int indent = 0); 110 void DumpDataPortion(int indent = 0); 111 void DumpCallChain(int indent, std::unique_ptr<PerfRecordSample> &sample); 112 void DumpFeaturePortion(int indent = 0); 113 void ExprotUserData(std::unique_ptr<PerfEventRecord> &record); 114 void ExprotUserStack(const PerfRecordSample &recordSample); 115 void PrintHeaderInfo(const int &indent); 116 void PrintSymbolFile(const int &indent, const SymbolFileStruct &symbolFileStruct); 117 void PrintFeatureEventdesc(int indent, const PerfFileSectionEventDesc §ionEventdesc); 118 VirtualRuntime vr_; 119 }; 120 } // namespace HiPerf 121 } // namespace Developtools 122 } // namespace OHOS 123 #endif // SUBCOMMAND_DUMP_H 124