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 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 " --elf <elf file name>\n" 51 " dump elf not perf data.\n" 52 #if HAVE_PROTOBUF 53 " --proto <protobuf file name>\n" 54 " dump perf data from protobuf file.\n" 55 #endif 56 " --export <sample index>\n" 57 " also export the user stack data to some split file,\n" 58 " use this command to produce ut data." 59 " named with sample index(0 base):\n" 60 " hiperf_<pid>_<tid>_user_regs_<index>.dump\n" 61 " hiperf_<pid>_<tid>_user_data_<index>.dump\n" 62 " <file name>\n" 63 " perf data file to dump, default is perf.data\n\n" 64 ) 65 // clang-format on 66 { 67 } 68 ~SubCommandDump() override; 69 70 bool OnSubCommand(std::vector<std::string> &args) override; 71 bool ParseOption(std::vector<std::string> &args) override; 72 73 static bool RegisterSubCommandDump(void); 74 75 static void DumpPrintEventAttr(const perf_event_attr &attr, int indent = 0); 76 std::unique_ptr<PerfFileReader> reader_; 77 78 private: 79 static void DumpSampleType(uint64_t sampleType, int indent); 80 int exportSampleIndex_ = -1; 81 int currectSampleIndex_ = 0; 82 std::string dumpFileName_; 83 std::string elfFileName_; 84 std::string protobufDumpFileName_; 85 int indent_ = 0; 86 #if HAVE_PROTOBUF 87 std::unique_ptr<ReportProtobufFileReader> protobufInputFileReader_ = nullptr; 88 #endif 89 90 std::vector<AttrWithId> attrIds_; 91 92 bool dumpHeader_ = false; 93 bool dumpFeatures_ = false; 94 bool dumpData_ = false; 95 bool dumpAll_ = true; 96 97 std::vector<std::string> dumpSymbolsPaths_; 98 99 bool CheckInputFile(); 100 bool DumpElfFile(); 101 #if HAVE_PROTOBUF 102 bool DumpProtoFile(); 103 #endif 104 void DumpPrintFileHeader(int indent = 0); 105 void DumpAttrPortion(int indent = 0); 106 void DumpDataPortion(int indent = 0); 107 void DumpCallChain(int indent, std::unique_ptr<PerfRecordSample> &sample); 108 void DumpFeaturePortion(int indent = 0); 109 void ExprotUserData(std::unique_ptr<PerfEventRecord> &record); 110 void ExprotUserStack(const PerfRecordSample &recordSample); 111 void PrintHeaderInfo(const int &indent); 112 void PrintSymbolFile(const int &indent, const SymbolFileStruct &symbolFileStruct); 113 void PrintFeatureEventdesc(int indent, const PerfFileSectionEventDesc §ionEventdesc); 114 VirtualRuntime vr_; 115 }; 116 } // namespace HiPerf 117 } // namespace Developtools 118 } // namespace OHOS 119 #endif