100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#ifndef DUMP_IMPLEMENT_H
1600600bfbSopenharmony_ci#define DUMP_IMPLEMENT_H
1700600bfbSopenharmony_ci#include <map>
1800600bfbSopenharmony_ci#include <memory>
1900600bfbSopenharmony_ci#include <getopt.h>
2000600bfbSopenharmony_ci#include <mutex>
2100600bfbSopenharmony_ci#include "if_system_ability_manager.h"
2200600bfbSopenharmony_ci#include "common.h"
2300600bfbSopenharmony_ci#include "singleton.h"
2400600bfbSopenharmony_ci#include "raw_param.h"
2500600bfbSopenharmony_ci#include "common/dumper_parameter.h"
2600600bfbSopenharmony_ci#include "common/dump_cfg.h"
2700600bfbSopenharmony_ci#include "common/dumper_constant.h"
2800600bfbSopenharmony_ci#include "executor/hidumper_executor.h"
2900600bfbSopenharmony_ci#include "factory/executor_factory.h"
3000600bfbSopenharmony_ci
3100600bfbSopenharmony_cinamespace OHOS {
3200600bfbSopenharmony_cinamespace HiviewDFX {
3300600bfbSopenharmony_ciclass DumpImplement : public Singleton<DumpImplement> {
3400600bfbSopenharmony_cipublic:
3500600bfbSopenharmony_ci    DumpImplement();
3600600bfbSopenharmony_ci    ~DumpImplement();
3700600bfbSopenharmony_ci    DumpImplement(DumpImplement const &) = delete;
3800600bfbSopenharmony_ci    void operator=(DumpImplement const &) = delete;
3900600bfbSopenharmony_ci    DumpStatus Main(int argc, char *argv[], const std::shared_ptr<RawParam>& reqCtl);
4000600bfbSopenharmony_ci    const sptr<ISystemAbilityManager> GetSystemAbilityManager();
4100600bfbSopenharmony_ci
4200600bfbSopenharmony_ciprivate:
4300600bfbSopenharmony_ci    DumpStatus CmdParse(int argc, char* argv[], std::shared_ptr<DumperParameter>& dumpParameter);
4400600bfbSopenharmony_ci    /**
4500600bfbSopenharmony_ci     * Check client is hidumper.
4600600bfbSopenharmony_ci     *
4700600bfbSopenharmony_ci     * @param pid, process id of client.
4800600bfbSopenharmony_ci     * @return bool, hidumper client return true, not return false.
4900600bfbSopenharmony_ci     */
5000600bfbSopenharmony_ci    bool IsHidumperClientProcess(int pid);
5100600bfbSopenharmony_ci    DumpStatus CmdParseWithParameter(int argc, char* argv[], DumperOpts& opts_);
5200600bfbSopenharmony_ci    DumpStatus CmdParseWithParameter(std::shared_ptr<DumperParameter>& dumpParameter,
5300600bfbSopenharmony_ci        int argc, char* argv[], DumperOpts& opts_);
5400600bfbSopenharmony_ci    DumpStatus SetCmdParameter(int argc, char* argv[], DumperOpts& opts_);
5500600bfbSopenharmony_ci    DumpStatus SetCmdIntegerParameter(const std::string& str, int& value);
5600600bfbSopenharmony_ci    void CmdHelp();
5700600bfbSopenharmony_ci    void setExecutorList(std::vector<std::shared_ptr<HidumperExecutor>>& executors,
5800600bfbSopenharmony_ci        const std::vector<std::shared_ptr<DumpCfg>>& configs, bool isZip);
5900600bfbSopenharmony_ci    DumpStatus DumpDatas(const std::vector<std::shared_ptr<HidumperExecutor>>& executors,
6000600bfbSopenharmony_ci        const std::shared_ptr<DumperParameter>& dumpParameter,
6100600bfbSopenharmony_ci        HidumperExecutor::StringMatrix dumpDatas);
6200600bfbSopenharmony_ci    void AddGroupTitle(const std::string& groupName, HidumperExecutor::StringMatrix dumpDatas,
6300600bfbSopenharmony_ci        const std::shared_ptr<DumperParameter>& dumpParameter);
6400600bfbSopenharmony_ci    void AddExecutorFactoryToMap();
6500600bfbSopenharmony_ci    /**
6600600bfbSopenharmony_ci     * Check group name changed.
6700600bfbSopenharmony_ci     *
6800600bfbSopenharmony_ci     * @param lastName, last group name.
6900600bfbSopenharmony_ci     * @param curName, current group name.
7000600bfbSopenharmony_ci     * @return bool, changed true, not changed false.
7100600bfbSopenharmony_ci     */
7200600bfbSopenharmony_ci    bool CheckGroupName(std::string& lastName, const std::string& curName);
7300600bfbSopenharmony_ci    bool IsShortOptionReqArg(const char* optStr);
7400600bfbSopenharmony_ci    void SendErrorMessage(const std::string& errorStr);
7500600bfbSopenharmony_ci    void SendPidErrorMessage(int pid);
7600600bfbSopenharmony_ci    bool ParseSubLongCmdOption(int argc, DumperOpts &opts_, const struct option longOptions[],
7700600bfbSopenharmony_ci        const int &optionIndex, char *argv[]);
7800600bfbSopenharmony_ci    DumpStatus ParseLongCmdOption(int argc, DumperOpts& opts_, const struct option longOptions[],
7900600bfbSopenharmony_ci        const int& optionIndex, char* argv[]);
8000600bfbSopenharmony_ci    DumpStatus ParseShortCmdOption(int c, DumperOpts& opts_, int argc, char* argv[]);
8100600bfbSopenharmony_ci    void CheckIncorrectCmdOption(const char* optStr, char* argv[]);
8200600bfbSopenharmony_ci    std::string RemoveCharacterFromStr(const std::string& str, const char character);
8300600bfbSopenharmony_ci    bool IsSADumperOption(char* argv[]);
8400600bfbSopenharmony_ci    DumpStatus CheckProcessAlive(const DumperOpts& opts_);
8500600bfbSopenharmony_ci    void RemoveDuplicateString(DumperOpts& opts_);
8600600bfbSopenharmony_ci#ifdef HIDUMPER_HIVIEWDFX_HISYSEVENT_ENABLE
8700600bfbSopenharmony_ci    void ReportCmdUsage(const DumperOpts &opts_, const std::string& cmdStr);
8800600bfbSopenharmony_ci    std::string TransferVectorToString(const std::vector<std::string>& vs);
8900600bfbSopenharmony_ci#endif
9000600bfbSopenharmony_ci    bool CheckAppDebugVersion(int pid);
9100600bfbSopenharmony_ci    bool CheckDumpPermission(DumperOpts &opt);
9200600bfbSopenharmony_ci    bool SetIpcStatParam(DumperOpts &opt, const std::string& param);
9300600bfbSopenharmony_ci    DumpStatus ParseCmdOptionForA(DumperOpts &opt, char *argv[]);
9400600bfbSopenharmony_ci    void ProcessDumpOptions(int clientPid, std::shared_ptr<DumperParameter> &dumpParameter, DumperOpts &opts);
9500600bfbSopenharmony_ci
9600600bfbSopenharmony_ciprivate:
9700600bfbSopenharmony_ci    using ExecutorFactoryMap = std::map<int, std::shared_ptr<ExecutorFactory>>;
9800600bfbSopenharmony_ci    static const int ARG_INDEX_OFFSET_LAST_OPTION = 2;
9900600bfbSopenharmony_ci    const std::string unrecognizedError_ = "hidumper: option pid missed. ";
10000600bfbSopenharmony_ci    const std::string invalidError_ = "hidumper: invalid arg: ";
10100600bfbSopenharmony_ci    const std::string requireError_ = "hidumper: option requires an argument: ";
10200600bfbSopenharmony_ci    const std::string pidError_ = "hidumper: No such process: %d\n";
10300600bfbSopenharmony_ci    std::shared_ptr<ExecutorFactoryMap> ptrExecutorFactoryMap_;
10400600bfbSopenharmony_ci    mutable std::mutex mutexCmdLock_;
10500600bfbSopenharmony_ci    std::shared_ptr<RawParam> ptrReqCtl_;
10600600bfbSopenharmony_ci    sptr<ISystemAbilityManager> sam_;
10700600bfbSopenharmony_ci    std::string GetTime();
10800600bfbSopenharmony_ci    std::string path_;
10900600bfbSopenharmony_ci    static const int IPC_STAT_ARG_NUMS = 4;
11000600bfbSopenharmony_ci};
11100600bfbSopenharmony_ci} // namespace HiviewDFX
11200600bfbSopenharmony_ci} // namespace OHOS
11300600bfbSopenharmony_ci#endif // DUMP_IMPLEMENT_H
114