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 OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H
17 #define OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H
18 
19 #include <sys/types.h>
20 
21 #include <fstream>
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "cpp/mutex.h"
29 #include "cpp/condition_variable.h"
30 #include "fault_data.h"
31 #include "freeze_util.h"
32 
33 namespace OHOS {
34 using AbilityRuntime::FreezeUtil;
35 namespace AppExecFwk {
36 class AppfreezeManager : public std::enable_shared_from_this<AppfreezeManager> {
37 public:
38     struct AppInfo {
39         int pid;
40         int uid;
41         std::string bundleName;
42         std::string processName;
43     };
44 
45     enum TypeAttribute {
46         NORMAL_TIMEOUT = 0,
47         CRITICAL_TIMEOUT = 1,
48     };
49 
50     enum AppFreezeState {
51         APPFREEZE_STATE_IDLE = 0,
52         APPFREEZE_STATE_FREEZE = 1,
53         APPFREEZE_STATE_CANCELING = 2,
54         APPFREEZE_STATE_CANCELED = 3,
55     };
56 
57     struct AppFreezeInfo {
58         int32_t pid = 0;
59         int state = 0;
60         int64_t occurTime = 0;
61     };
62 
63     struct ParamInfo {
64         int typeId = TypeAttribute::NORMAL_TIMEOUT;
65         int32_t pid = 0;
66         std::string eventName;
67         std::string bundleName;
68         std::string msg;
69     };
70 
71     AppfreezeManager();
72     ~AppfreezeManager();
73 
74     static std::shared_ptr<AppfreezeManager> GetInstance();
75     static void DestroyInstance();
76     int AppfreezeHandle(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo);
77     int AppfreezeHandleWithStack(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo);
78     int LifecycleTimeoutHandle(const ParamInfo& info, FreezeUtil::LifecycleFlow flow = FreezeUtil::LifecycleFlow());
79     std::string WriteToFile(const std::string& fileName, std::string& content);
80     bool IsHandleAppfreeze(const std::string& bundleName);
81     bool IsProcessDebug(int32_t pid, std::string processName);
82     bool IsNeedIgnoreFreezeEvent(int32_t pid);
83     void DeleteStack(int pid);
84     bool CancelAppFreezeDetect(int32_t pid, const std::string& bundleName);
85     void RemoveDeathProcess(std::string bundleName);
86     void ResetAppfreezeState(int32_t pid, const std::string& bundleName);
87     bool IsValidFreezeFilter(int32_t pid, const std::string& bundleName);
88 
89 private:
90     AppfreezeManager& operator=(const AppfreezeManager&) = delete;
91     AppfreezeManager(const AppfreezeManager&) = delete;
92     uint64_t GetMilliseconds();
93     std::map<int, std::set<int>> BinderParser(std::ifstream& fin, std::string& stack, std::set<int>& asyncPids) const;
94     std::map<int, std::set<int>> BinderLineParser(std::ifstream& fin, std::string& stack,
95         std::map<uint32_t, uint32_t>& asyncBinderMap,
96         std::vector<std::pair<uint32_t, uint64_t>>& freeAsyncSpacePairs) const;
97     std::vector<std::string> GetFileToList(std::string line) const;
98     void ParseBinderPids(const std::map<int, std::set<int>>& binderInfo, std::set<int>& pids, int pid, int layer) const;
99     std::set<int> GetBinderPeerPids(std::string& stack, int pid, std::set<int>& asyncPids) const;
100     void FindStackByPid(std::string& ret, int pid) const;
101     std::string CatchJsonStacktrace(int pid, const std::string& faultType) const;
102     std::string CatcherStacktrace(int pid) const;
103     int AcquireStack(const FaultData& faultData, const AppInfo& appInfo, const std::string& memoryContent);
104     int NotifyANR(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo,
105         const std::string& binderInfo, const std::string& memoryContent);
106     int64_t GetFreezeCurrentTime();
107     void SetFreezeState(int32_t pid, int state);
108     int GetFreezeState(int32_t pid);
109     int64_t GetFreezeTime(int32_t pid);
110     void ClearOldInfo();
111     void CollectFreezeSysMemory(std::string& memoryContent);
112 
113     static const inline std::string LOGGER_DEBUG_PROC_PATH = "/proc/transaction_proc";
114     std::string name_;
115     static ffrt::mutex singletonMutex_;
116     static std::shared_ptr<AppfreezeManager> instance_;
117     static ffrt::mutex freezeMutex_;
118     std::map<int32_t, AppFreezeInfo> appfreezeInfo_;
119     static ffrt::mutex catchStackMutex_;
120     static std::map<int, std::string> catchStackMap_;
121     static ffrt::mutex freezeFilterMutex_;
122     std::map<std::string, AppFreezeInfo> appfreezeFilterMap_;
123 };
124 }  // namespace AppExecFwk
125 }  // namespace OHOS
126 #endif  // OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H