• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 class SubCommandDump : public SubCommand {
34 public:
SubCommandDump()35     SubCommandDump()
36         // clang-format off
37         : SubCommand("dump", "Dump content of a perf data file, like perf.data",
38 
39         "Usage: hiperf dump [option] <filename>\n"
40         "       Dump specific parts of specified file .\n"
41         "   --head\n"
42         "       Dump header and attrs only.\n"
43         "   -d\n"
44         "       Dump data section only.\n"
45         "   -f\n"
46         "       Dump addtional features only.\n"
47         "   --sympath <symbols path>\n"
48         "       use symbols path to find symbols.\n"
49         "   -i <file name>\n"
50         "       perf data file to dump, default is perf.data\n"
51         "   --elf <elf file name>\n"
52         "       dump elf not perf data.\n"
53 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
54         "   --proto <protobuf file name>\n"
55         "       dump perf data from protobuf file.\n"
56 #endif
57         "   --export <sample index>\n"
58         "       also export the user stack data to some split file,\n"
59         "       use this command to produce ut data.\n"
60         "       named with sample index(0 base):\n"
61         "           hiperf_<pid>_<tid>_user_regs_<index>.dump\n"
62         "           hiperf_<pid>_<tid>_user_data_<index>.dump\n"
63         "   -o <filename>\n"
64         "       dump file name. if empty will use stdout print\n"
65         )
66     // clang-format on
67     {
68     }
69     ~SubCommandDump() override;
70 
71     HiperfError OnSubCommand(std::vector<std::string>& args) override;
72     bool ParseOption(std::vector<std::string> &args) override;
73 
74     static bool RegisterSubCommandDump(void);
75 
76     static void DumpPrintEventAttr(const perf_event_attr &attr, int indent = 0);
77     std::unique_ptr<PerfFileReader> reader_;
78 
79     static SubCommand& GetInstance();
80 
81 private:
82     static void DumpSampleType(uint64_t sampleType, int indent);
83     bool PrepareDumpOutput();
84     int exportSampleIndex_ = -1;
85     int currectSampleIndex_ = 0;
86     std::string dumpFileName_;
87     std::string elfFileName_;
88     std::string outputFilename_ = "";
89     std::string protobufDumpFileName_;
90     int indent_ = 0;
91 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
92     std::unique_ptr<ReportProtobufFileReader> protobufInputFileReader_ = nullptr;
93 #endif
94 
95     std::vector<AttrWithId> attrIds_;
96 
97     bool dumpHeader_ = false;
98     bool dumpFeatures_ = false;
99     bool dumpData_ = false;
100     bool dumpAll_ = true;
101 
102     std::vector<std::string> dumpSymbolsPaths_;
103 
104     bool CheckInputFile();
105     bool DumpElfFile();
106 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
107     bool DumpProtoFile();
108 #endif
109     void DumpPrintFileHeader(int indent = 0);
110     void DumpAttrPortion(int indent = 0);
111     void DumpDataPortion(int indent = 0);
112     void DumpCallChain(int indent, const PerfRecordSample& sample);
113     void DumpFeaturePortion(int indent = 0);
114     void DumpUniqueStackTableNode(int indent, const PerfFileSectionUniStackTable &uniStackTable);
115     void ExportUserData(PerfEventRecord& record);
116     void ExportUserStack(const PerfRecordSample &recordSample);
117     void PrintHeaderInfo(const int &indent);
118     void PrintSymbolFile(const int &indent, const SymbolFileStruct &symbolFileStruct);
119     void PrintFeatureEventdesc(int indent, const PerfFileSectionEventDesc &sectionEventdesc);
120     void DumpSpeReport();
121     VirtualRuntime vr_;
122 
123     void SetHM();
124 };
125 } // namespace HiPerf
126 } // namespace Developtools
127 } // namespace OHOS
128 #endif // SUBCOMMAND_DUMP_H
129