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
29namespace OHOS {
30namespace Developtools {
31namespace HiPerf {
32
33class SubCommandDump : public SubCommand {
34public:
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    bool 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
79private:
80    static void DumpSampleType(uint64_t sampleType, int indent);
81    bool PrepareDumpOutput();
82    int exportSampleIndex_ = -1;
83    int currectSampleIndex_ = 0;
84    std::string dumpFileName_;
85    std::string elfFileName_;
86    std::string outputFilename_ = "";
87    std::string protobufDumpFileName_;
88    int indent_ = 0;
89#if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
90    std::unique_ptr<ReportProtobufFileReader> protobufInputFileReader_ = nullptr;
91#endif
92
93    std::vector<AttrWithId> attrIds_;
94
95    bool dumpHeader_ = false;
96    bool dumpFeatures_ = false;
97    bool dumpData_ = false;
98    bool dumpAll_ = true;
99
100    std::vector<std::string> dumpSymbolsPaths_;
101
102    bool CheckInputFile();
103    bool DumpElfFile();
104#if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
105    bool DumpProtoFile();
106#endif
107    void DumpPrintFileHeader(int indent = 0);
108    void DumpAttrPortion(int indent = 0);
109    void DumpDataPortion(int indent = 0);
110    void DumpCallChain(int indent, std::unique_ptr<PerfRecordSample> &sample);
111    void DumpFeaturePortion(int indent = 0);
112    void DumpUniqueStackTableNode(int indent, const PerfFileSectionUniStackTable &uniStackTable);
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 &sectionEventdesc);
118    void DumpSpeReport();
119    VirtualRuntime vr_;
120
121    void SetHM();
122};
123} // namespace HiPerf
124} // namespace Developtools
125} // namespace OHOS
126#endif // SUBCOMMAND_DUMP_H
127