1/*
2 * Copyright (C) 2021 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 HIDUMPER_SERVICES_CONFIG_UTILS_H
16#define HIDUMPER_SERVICES_CONFIG_UTILS_H
17#include <string>
18#include <vector>
19#include "dump_common_utils.h"
20#include "common/dump_cfg.h"
21#include "common/dumper_parameter.h"
22#include "util/config_data.h"
23namespace OHOS {
24namespace HiviewDFX {
25class ConfigUtils : public ConfigData {
26public:
27    explicit ConfigUtils(const std::shared_ptr<DumperParameter> &param);
28    ~ConfigUtils();
29public:
30    // Used for get dumper configs
31    static DumpStatus GetDumperConfigs(const std::shared_ptr<DumperParameter> &param);
32    // Used for get section names
33    static DumpStatus GetSectionNames(const std::string &name, std::vector<std::string> &nameList);
34    // Used for get dump level
35    static int GetDumpLevelByPid(int uid, const DumpCommonUtils::PidInfo &pidInfo);
36#ifdef DUMP_TEST_MODE // for mock test
37public:
38#else // for mock test
39private:
40#endif // for mock test
41    DumpStatus GetDumperConfigs();
42    bool MergePidInfos(std::vector<DumpCommonUtils::PidInfo> &outInfos, int pid);
43    bool HandleDumpLog(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
44    bool HandleDumpList(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
45    bool HandleDumpService(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
46    bool HandleDumpAbility(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
47    bool HandleDumpSystem(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
48    bool HandleDumpCpuFreq(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
49    bool HandleDumpCpuUsage(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
50    bool HandleDumpMem(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
51    bool HandleDumpJsHeapMem(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
52    bool HandleDumpMemShowMaps(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
53    void HandleDumpMemCommon(std::shared_ptr<DumperParameter> dumpParam,
54        std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
55    bool HandleDumpStorage(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
56    bool HandleDumpNet(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
57    bool HandleDumpProcesses(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
58    bool HandleDumpFaultLog(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
59    bool HandleDumpAppendix(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
60    bool HandleDumpIpcStat(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs);
61    bool CopySmaps();
62    DumpStatus GetConfig(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
63        std::shared_ptr<OptionArgs> args);
64    DumpStatus GetDumper(int index, std::vector<std::shared_ptr<DumpCfg>> &result,
65        std::shared_ptr<OptionArgs> args, int level = DumperConstant::NONE);
66    DumpStatus GetDumper(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
67        std::shared_ptr<OptionArgs> args, int level = DumperConstant::NONE);
68    DumpStatus GetGroup(int index, std::vector<std::shared_ptr<DumpCfg>> &result,
69        std::shared_ptr<OptionArgs> args, int level = DumperConstant::NONE, int nest = 0);
70    DumpStatus GetGroup(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
71        std::shared_ptr<OptionArgs> args, int level = DumperConstant::NONE, int nest = 0);
72    // Used for get section name from group name
73    static std::string GetSectionName(const std::string &name);
74private:
75    DumpStatus GetGroupSimple(const GroupCfg& groupCfg, std::vector<std::shared_ptr<DumpCfg>> &result,
76        std::shared_ptr<OptionArgs> args, int level = DumperConstant::NONE, int nest = 0);
77    static DumpStatus GetGroupNames(const std::string &name, std::vector<std::string> &nameList);
78    static void ConvertTreeToList(std::vector<std::shared_ptr<DumpCfg>> &tree,
79        std::vector<std::shared_ptr<DumpCfg>> &list, int nest = 0);
80    static void SetSection(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs, const std::string &section, int nest = 0);
81    bool MergeDebugPidInfos(std::vector<DumpCommonUtils::PidInfo> &pidInfos, int pid);
82private:
83    const std::shared_ptr<DumperParameter> dumperParam_;
84    std::vector<DumpCommonUtils::PidInfo> pidInfos_;
85    std::vector<DumpCommonUtils::CpuInfo> cpuInfos_;
86    std::vector<DumpCommonUtils::PidInfo> currentPidInfos_;
87    DumpCommonUtils::PidInfo currentPidInfo_;
88    bool isDumpSystemSystem {false};
89};
90} // namespace HiviewDFX
91} // namespace OHOS
92#endif // HIDUMPER_SERVICES_CONFIG_UTILS_H
93