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