• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 DUMP_IMPLEMENT_H
16 #define DUMP_IMPLEMENT_H
17 #include <map>
18 #include <memory>
19 #include <getopt.h>
20 #include <mutex>
21 #include "if_system_ability_manager.h"
22 #include "common.h"
23 #include "singleton.h"
24 #include "raw_param.h"
25 #include "common/dumper_parameter.h"
26 #include "common/dump_cfg.h"
27 #include "common/dumper_constant.h"
28 #include "executor/hidumper_executor.h"
29 #include "factory/executor_factory.h"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 class DumpImplement : public Singleton<DumpImplement> {
34 public:
35     DumpImplement();
36     ~DumpImplement();
37     DumpImplement(DumpImplement const &) = delete;
38     void operator=(DumpImplement const &) = delete;
39     DumpStatus Main(int argc, char *argv[], const std::shared_ptr<RawParam>& reqCtl);
40     const sptr<ISystemAbilityManager> GetSystemAbilityManager();
41 
42 private:
43     DumpStatus InitHandle(int argc, char *argv[], const std::shared_ptr<RawParam> &reqCtl,
44         std::shared_ptr<DumperParameter>& ptrDumperParameter);
45     DumpStatus CmdParse(int argc, char* argv[], std::shared_ptr<DumperParameter>& dumpParameter);
46     /**
47      * Check client is hidumper.
48      *
49      * @param pid, process id of client.
50      * @return bool, hidumper client return true, not return false.
51      */
52     bool IsHidumperClientProcess(int pid);
53     DumpStatus CmdParseWithParameter(int argc, char* argv[], DumperOpts& opts_);
54     DumpStatus CmdParseWithParameter(std::shared_ptr<DumperParameter>& dumpParameter,
55         int argc, char* argv[], DumperOpts& opts_);
56     DumpStatus SetCmdParameter(int argc, char* argv[], DumperOpts& opts_);
57     DumpStatus SetCmdIntegerParameter(const std::string& str, int& value);
58     void PrintCommonUsage(std::string& str);
59     void CmdHelp();
60     void setExecutorList(std::vector<std::shared_ptr<HidumperExecutor>>& executors,
61         const std::vector<std::shared_ptr<DumpCfg>>& configs, bool isZip);
62     DumpStatus DumpDatas(const std::vector<std::shared_ptr<HidumperExecutor>>& executors,
63         const std::shared_ptr<DumperParameter>& dumpParameter,
64         HidumperExecutor::StringMatrix dumpDatas);
65     void AddGroupTitle(const std::string& groupName, HidumperExecutor::StringMatrix dumpDatas,
66         const std::shared_ptr<DumperParameter>& dumpParameter);
67     void AddExecutorFactoryToMap();
68     /**
69      * Check group name changed.
70      *
71      * @param lastName, last group name.
72      * @param curName, current group name.
73      * @return bool, changed true, not changed false.
74      */
75     bool CheckGroupName(std::string& lastName, const std::string& curName);
76     bool IsShortOptionReqArg(const char* optStr);
77     void SendErrorMessage(const std::string& errorStr);
78     void SendPidErrorMessage(int pid);
79     void SendReleaseAppErrorMessage(const std::string& opt);
80     void SendReleaseVersionErrorMessage(const std::string& opt);
81     bool ParseSubLongCmdOption(int argc, DumperOpts &opts_, const struct option longOptions[],
82         const int &optionIndex, char *argv[]);
83     DumpStatus ParseLongCmdOption(int argc, DumperOpts& opts_, const struct option longOptions[],
84         const int& optionIndex, char* argv[]);
85     DumpStatus ParseShortCmdOption(int c, DumperOpts& opts_, int argc, char* argv[]);
86     void CheckIncorrectCmdOption(const char* optStr, char* argv[]);
87     std::string RemoveCharacterFromStr(const std::string& str, const char character);
88     bool IsSADumperOption(char* argv[]);
89     DumpStatus CheckProcessAlive(const DumperOpts& opts_);
90     void RemoveDuplicateString(DumperOpts& opts_);
91 #ifdef HIDUMPER_HIVIEWDFX_HISYSEVENT_ENABLE
92     void ReportJsheap(const DumperOpts &opts_);
93     void ReportCjheap(const DumperOpts &opts_);
94 #endif
95     bool CheckAppDebugVersion(int pid);
96     bool CheckDumpPermission(DumperOpts &opt);
97     bool SetIpcStatParam(DumperOpts &opt, const std::string& param);
98     DumpStatus ParseCmdOptionForA(DumperOpts &opt, char *argv[]);
99     void ProcessDumpOptions(int clientPid, std::shared_ptr<DumperParameter> &dumpParameter, DumperOpts &opts);
100     DumpStatus SetMemSmapsParam(DumperOpts &opt, int argc, char *argv[]);
101     DumpStatus SetMemJsheapParam(DumperOpts &opt);
102     DumpStatus SetMemCjheapParam(DumperOpts &opt);
103     DumpStatus SetRawParam(DumperOpts &opt);
104     DumpStatus SetMemPruneParam(DumperOpts &opt);
105     DumpStatus SetGCParam(DumperOpts &opt);
106     DumpStatus CheckArgs(int argc, char* argv[]);
107 
108 private:
109     using ExecutorFactoryMap = std::map<int, std::shared_ptr<ExecutorFactory>>;
110     static const int ARG_INDEX_OFFSET_LAST_OPTION = 2;
111     const std::string unrecognizedError_ = "hidumper: option pid missed. ";
112     const std::string invalidError_ = "hidumper: invalid arg: ";
113     const std::string requireError_ = "hidumper: option requires an argument: ";
114     const std::string pidError_ = "hidumper: No such process: %d\n";
115     std::shared_ptr<ExecutorFactoryMap> ptrExecutorFactoryMap_;
116     mutable std::mutex mutexCmdLock_;
117     std::shared_ptr<RawParam> ptrReqCtl_;
118     sptr<ISystemAbilityManager> sam_;
119     std::string GetTime();
120     std::string path_;
121     static const int IPC_STAT_ARG_NUMS = 4;
122     static const int HIVIEW_UID = 1201;
123     static thread_local std::unique_ptr<DumperSysEventParams> dumperSysEventParams_;
124 };
125 } // namespace HiviewDFX
126 } // namespace OHOS
127 #endif // DUMP_IMPLEMENT_H
128