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 CmdParse(int argc, char* argv[], std::shared_ptr<DumperParameter>& dumpParameter);
44     /**
45      * Check client is hidumper.
46      *
47      * @param pid, process id of client.
48      * @return bool, hidumper client return true, not return false.
49      */
50     bool IsHidumperClientProcess(int pid);
51     DumpStatus CmdParseWithParameter(int argc, char* argv[], DumperOpts& opts_);
52     DumpStatus CmdParseWithParameter(std::shared_ptr<DumperParameter>& dumpParameter,
53         int argc, char* argv[], DumperOpts& opts_);
54     DumpStatus SetCmdParameter(int argc, char* argv[], DumperOpts& opts_);
55     DumpStatus SetCmdIntegerParameter(const std::string& str, int& value);
56     void CmdHelp();
57     void setExecutorList(std::vector<std::shared_ptr<HidumperExecutor>>& executors,
58         const std::vector<std::shared_ptr<DumpCfg>>& configs, bool isZip);
59     DumpStatus DumpDatas(const std::vector<std::shared_ptr<HidumperExecutor>>& executors,
60         const std::shared_ptr<DumperParameter>& dumpParameter,
61         HidumperExecutor::StringMatrix dumpDatas);
62     void AddGroupTitle(const std::string& groupName, HidumperExecutor::StringMatrix dumpDatas,
63         const std::shared_ptr<DumperParameter>& dumpParameter);
64     void AddExecutorFactoryToMap();
65     /**
66      * Check group name changed.
67      *
68      * @param lastName, last group name.
69      * @param curName, current group name.
70      * @return bool, changed true, not changed false.
71      */
72     bool CheckGroupName(std::string& lastName, const std::string& curName);
73     bool IsShortOptionReqArg(const char* optStr);
74     void SendErrorMessage(const std::string& errorStr);
75     void SendPidErrorMessage(int pid);
76     bool ParseSubLongCmdOption(int argc, DumperOpts &opts_, const struct option longOptions[],
77         const int &optionIndex, char *argv[]);
78     DumpStatus ParseLongCmdOption(int argc, DumperOpts& opts_, const struct option longOptions[],
79         const int& optionIndex, char* argv[]);
80     DumpStatus ParseShortCmdOption(int c, DumperOpts& opts_, int argc, char* argv[]);
81     void CheckIncorrectCmdOption(const char* optStr, char* argv[]);
82     std::string RemoveCharacterFromStr(const std::string& str, const char character);
83     bool IsSADumperOption(char* argv[]);
84     DumpStatus CheckProcessAlive(const DumperOpts& opts_);
85     void RemoveDuplicateString(DumperOpts& opts_);
86 #ifdef HIDUMPER_HIVIEWDFX_HISYSEVENT_ENABLE
87     void ReportCmdUsage(const DumperOpts &opts_, const std::string& cmdStr);
88     std::string TransferVectorToString(const std::vector<std::string>& vs);
89 #endif
90     bool CheckAppDebugVersion(int pid);
91     bool CheckDumpPermission(DumperOpts &opt);
92     bool SetIpcStatParam(DumperOpts &opt, const std::string& param);
93     DumpStatus ParseCmdOptionForA(DumperOpts &opt, char *argv[]);
94 
95 private:
96     using ExecutorFactoryMap = std::map<int, std::shared_ptr<ExecutorFactory>>;
97     static const int ARG_INDEX_OFFSET_LAST_OPTION = 2;
98     const std::string unrecognizedError_ = "hidumper: option pid missed. ";
99     const std::string invalidError_ = "hidumper: invalid arg: ";
100     const std::string requireError_ = "hidumper: option requires an argument: ";
101     const std::string pidError_ = "hidumper: No such process: %d\n";
102     std::shared_ptr<ExecutorFactoryMap> ptrExecutorFactoryMap_;
103     mutable std::mutex mutexCmdLock_;
104     std::shared_ptr<RawParam> ptrReqCtl_;
105     sptr<ISystemAbilityManager> sam_;
106     std::string GetTime();
107     std::string path_;
108     static const int IPC_STAT_ARG_NUMS = 4;
109 };
110 } // namespace HiviewDFX
111 } // namespace OHOS
112 #endif // DUMP_IMPLEMENT_H
113