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 void AddExecutorFactoryToMap(); 64 /** 65 * Check group name changed. 66 * 67 * @param lastName, last group name. 68 * @param curName, current group name. 69 * @return bool, changed true, not changed false. 70 */ 71 bool CheckGroupName(std::string& lastName, const std::string& curName); 72 bool IsShortOptionReqArg(const char* optStr); 73 void SendErrorMessage(const std::string& errorStr); 74 void SendPidErrorMessage(int pid); 75 DumpStatus ParseLongCmdOption(DumperOpts& opts_, const struct option longOptions[], 76 const int& optionIndex, char* argv[]); 77 DumpStatus ParseShortCmdOption(int c, DumperOpts& opts_, int argc, char* argv[]); 78 void CheckIncorrectCmdOption(const char* optStr, char* argv[]); 79 std::string RemoveCharacterFromStr(const std::string& str, const char character); 80 bool IsLongOption(const std::string& str, const struct option longOptions[], int size); 81 bool IsSADumperOption(char* argv[]); 82 DumpStatus CheckProcessAlive(const DumperOpts& opts_); 83 void RemoveDuplicateString(DumperOpts& opts_); 84 85 private: 86 using ExecutorFactoryMap = std::map<int, std::shared_ptr<ExecutorFactory>>; 87 static const int ARG_INDEX_OFFSET_LAST_OPTION = 2; 88 const std::string unrecognizedError_ = "hidumper: unrecognized option: "; 89 const std::string invalidError_ = "hidumper: invalid arg: "; 90 const std::string requireError_ = "hidumper: option requires an argument: "; 91 const std::string pidError_ = "hidumper: No such process: %d\n"; 92 std::shared_ptr<ExecutorFactoryMap> ptrExecutorFactoryMap_; 93 mutable std::mutex mutexCmdLock_; 94 std::shared_ptr<RawParam> ptrReqCtl_; 95 sptr<ISystemAbilityManager> sam_; 96 std::string GetTime(); 97 std::string path_; 98 }; 99 } // namespace HiviewDFX 100 } // namespace OHOS 101 #endif // DUMP_IMPLEMENT_H 102