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 bool isOccurException = false; 40 int pid; 41 int uid; 42 std::string bundleName; 43 std::string processName; 44 }; 45 46 enum TypeAttribute { 47 NORMAL_TIMEOUT = 0, 48 CRITICAL_TIMEOUT = 1, 49 }; 50 51 enum AppFreezeState { 52 APPFREEZE_STATE_IDLE = 0, 53 APPFREEZE_STATE_FREEZE = 1, 54 APPFREEZE_STATE_CANCELING = 2, 55 APPFREEZE_STATE_CANCELED = 3, 56 }; 57 58 struct AppFreezeInfo { 59 int32_t pid = 0; 60 int state = 0; 61 int64_t occurTime = 0; 62 std::string errorName = ""; 63 }; 64 65 struct ParamInfo { 66 bool needKillProcess = true; 67 int typeId = TypeAttribute::NORMAL_TIMEOUT; 68 int32_t pid = 0; 69 std::string eventName; 70 std::string bundleName; 71 std::string msg; 72 }; 73 74 AppfreezeManager(); 75 ~AppfreezeManager(); 76 77 static std::shared_ptr<AppfreezeManager> GetInstance(); 78 static void DestroyInstance(); 79 int AppfreezeHandle(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo); 80 int AppfreezeHandleWithStack(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo); 81 int LifecycleTimeoutHandle(const ParamInfo& info, FreezeUtil::LifecycleFlow flow = FreezeUtil::LifecycleFlow()); 82 std::string WriteToFile(const std::string& fileName, std::string& content); 83 bool IsHandleAppfreeze(const std::string& bundleName); 84 bool IsProcessDebug(int32_t pid, std::string bundleName); 85 bool IsNeedIgnoreFreezeEvent(int32_t pid, const std::string& errorName); 86 void DeleteStack(int pid); 87 bool CancelAppFreezeDetect(int32_t pid, const std::string& bundleName); 88 void RemoveDeathProcess(std::string bundleName); 89 void ResetAppfreezeState(int32_t pid, const std::string& bundleName); 90 bool IsValidFreezeFilter(int32_t pid, const std::string& bundleName); 91 92 private: 93 struct PeerBinderInfo { 94 int32_t clientPid; 95 int32_t clientTid; 96 int32_t serverPid; 97 int32_t serverTid; 98 }; 99 100 struct TerminalBinder { 101 int32_t pid; 102 int32_t tid; 103 }; 104 105 struct ParseBinderParam { 106 int32_t eventPid; 107 int32_t eventTid; 108 int32_t pid; 109 int layer; 110 }; 111 112 struct HitraceInfo { 113 std::string hiTraceChainId; 114 std::string spanId; 115 std::string pspanId; 116 std::string traceFlag; 117 }; 118 119 AppfreezeManager& operator=(const AppfreezeManager&) = delete; 120 AppfreezeManager(const AppfreezeManager&) = delete; 121 std::map<int, std::list<AppfreezeManager::PeerBinderInfo>> BinderParser(std::ifstream& fin, std::string& stack, 122 std::set<int>& asyncPids) const; 123 std::map<int, std::list<AppfreezeManager::PeerBinderInfo>> BinderLineParser(std::ifstream& fin, std::string& stack, 124 std::map<uint32_t, uint32_t>& asyncBinderMap, 125 std::vector<std::pair<uint32_t, uint64_t>>& freeAsyncSpacePairs) const; 126 std::vector<std::string> GetFileToList(std::string line) const; 127 std::string StrSplit(const std::string& str, uint16_t index) const; 128 void ParseBinderPids(const std::map<int, std::list<AppfreezeManager::PeerBinderInfo>>& binderInfos, 129 std::set<int>& pids, AppfreezeManager::ParseBinderParam params, bool getTerminal, 130 AppfreezeManager::TerminalBinder& terminalBinder) const; 131 std::set<int> GetBinderPeerPids(std::string& stack, AppfreezeManager::ParseBinderParam params, 132 std::set<int>& asyncPids, AppfreezeManager::TerminalBinder& terminalBinder) const; 133 void FindStackByPid(std::string& msg, int pid) const; 134 std::string CatchJsonStacktrace(int pid, const std::string& faultType, const std::string& stack) const; 135 std::string CatcherStacktrace(int pid, const std::string& stack) const; 136 FaultData GetFaultNotifyData(const FaultData& faultData, int pid); 137 int AcquireStack(const FaultData& faultData, const AppInfo& appInfo, const std::string& memoryContent); 138 std::string ReportAppfreezeCpuInfo(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo); 139 int NotifyANR(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo, 140 const std::string& binderInfo, const std::string& memoryContent); 141 int64_t GetFreezeCurrentTime(); 142 void SetFreezeState(int32_t pid, int state, const std::string& errorName); 143 int GetFreezeState(int32_t pid); 144 int64_t GetFreezeTime(int32_t pid); 145 void ClearOldInfo(); 146 void CollectFreezeSysMemory(std::string& memoryContent); 147 int MergeNotifyInfo(FaultData& faultNotifyData, const AppfreezeManager::AppInfo& appInfo); 148 std::string ParseDecToHex(uint64_t id); 149 bool GetHitraceId(HitraceInfo& info); 150 void PerfStart(std::string eventName); 151 std::string GetFirstLine(const std::string &path); 152 153 static const inline std::string LOGGER_DEBUG_PROC_PATH = "/proc/transaction_proc"; 154 std::string name_; 155 static ffrt::mutex singletonMutex_; 156 static std::shared_ptr<AppfreezeManager> instance_; 157 static ffrt::mutex freezeMutex_; 158 std::map<int32_t, AppFreezeInfo> appfreezeInfo_; 159 static ffrt::mutex catchStackMutex_; 160 static std::map<int, std::string> catchStackMap_; 161 static ffrt::mutex freezeFilterMutex_; 162 std::map<std::string, AppFreezeInfo> appfreezeFilterMap_; 163 int64_t perfTime = 0; 164 static ffrt::mutex freezeInfoMutex_; 165 static std::string appfreezeInfoPath_; 166 }; 167 } // namespace AppExecFwk 168 } // namespace OHOS 169 #endif // OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H