1 /*
2  * Copyright (c) 2023 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 
16 #ifndef SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_DUMPER_H
17 #define SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_DUMPER_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "schedule/system_ability_state_scheduler.h"
23 
24 namespace OHOS {
25 enum {
26     IPC_STAT_PREFIX_INDEX = 0,
27     IPC_STAT_PROCESS_INDEX = 1,
28     IPC_STAT_CMD_INDEX = 2
29 };
30 
31 constexpr int32_t IPC_STAT_CMD_LEN = 3;
32 
33 class SystemAbilityManagerDumper {
34 public:
35     static bool Dump(std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler,
36         const std::vector<std::string>& args, std::string& result);
37     static bool IpcDumpCmdParser(int32_t& cmd, const std::vector<std::string>& args);
38     static bool IpcDumpIsAllProcess(const std::string& processName);
39     static bool IpcDumpIsSamgr(const std::string& processName);
40     static bool StartSamgrIpcStatistics(std::string& result);
41     static bool StopSamgrIpcStatistics(std::string& result);
42     static bool GetSamgrIpcStatistics(std::string& result);
43     static bool FfrtDumpParser(std::vector<int32_t>& processIds, const std::string& processIdsStr);
44     static int32_t FfrtDumpProc(std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler,
45         int32_t fd, const std::vector<std::string>& args);
46     static bool GetFfrtDumpInfoProc(std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler,
47         const std::vector<std::string>& args, std::string& result);
48 
49 private:
50     SystemAbilityManagerDumper() = default;
51     ~SystemAbilityManagerDumper() = default;
52     static bool CanDump();
53     static void ShowHelp(std::string& result);
54     static void ShowAllSystemAbilityInfo(std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler,
55         std::string& result);
56     static void ShowSystemAbilityInfo(int32_t said,
57         std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler, std::string& result);
58     static void ShowProcessInfo(const std::string& processName,
59         std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler, std::string& result);
60     static void ShowAllSystemAbilityInfoInState(const std::string& state,
61         std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler, std::string& result);
62     static void IllegalInput(std::string& result);
63     static void DumpFfrtInfoByProcName(int32_t processId, const std::u16string processName, std::string& result);
64     static int32_t SaveDumpResultToFd(int32_t fd, const std::string& result);
65     static void GetSAMgrFfrtInfo(std::string& result);
66 };
67 } // namespace OHOS
68 #endif // SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_DUMPER_H