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 int typeId = TypeAttribute::NORMAL_TIMEOUT; 67 int32_t pid = 0; 68 std::string eventName; 69 std::string bundleName; 70 std::string msg; 71 }; 72 73 AppfreezeManager(); 74 ~AppfreezeManager(); 75 76 static std::shared_ptr<AppfreezeManager> GetInstance(); 77 static void DestroyInstance(); 78 int AppfreezeHandle(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo); 79 int AppfreezeHandleWithStack(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo); 80 int LifecycleTimeoutHandle(const ParamInfo& info, FreezeUtil::LifecycleFlow flow = FreezeUtil::LifecycleFlow()); 81 std::string WriteToFile(const std::string& fileName, std::string& content); 82 bool IsHandleAppfreeze(const std::string& bundleName); 83 bool IsProcessDebug(int32_t pid, std::string bundleName); 84 bool IsNeedIgnoreFreezeEvent(int32_t pid, const std::string& errorName); 85 void DeleteStack(int pid); 86 bool CancelAppFreezeDetect(int32_t pid, const std::string& bundleName); 87 void RemoveDeathProcess(std::string bundleName); 88 void ResetAppfreezeState(int32_t pid, const std::string& bundleName); 89 bool IsValidFreezeFilter(int32_t pid, const std::string& bundleName); 90 91 private: 92 struct PeerBinderInfo { 93 int32_t clientPid; 94 int32_t clientTid; 95 int32_t serverPid; 96 int32_t serverTid; 97 }; 98 99 struct TerminalBinder { 100 bool firstLayerInit; 101 int32_t pid; 102 int32_t tid; 103 }; 104 105 AppfreezeManager& operator=(const AppfreezeManager&) = delete; 106 AppfreezeManager(const AppfreezeManager&) = delete; 107 uint64_t GetMilliseconds(); 108 std::map<int, std::list<AppfreezeManager::PeerBinderInfo>> BinderParser(std::ifstream& fin, std::string& stack, 109 std::set<int>& asyncPids) const; 110 std::map<int, std::list<AppfreezeManager::PeerBinderInfo>> BinderLineParser(std::ifstream& fin, std::string& stack, 111 std::map<uint32_t, uint32_t>& asyncBinderMap, 112 std::vector<std::pair<uint32_t, uint64_t>>& freeAsyncSpacePairs) const; 113 std::vector<std::string> GetFileToList(std::string line) const; 114 void ParseBinderPids(const std::map<int, std::list<AppfreezeManager::PeerBinderInfo>>& binderInfos, 115 std::set<int>& pids, int pid, int layer, AppfreezeManager::TerminalBinder& terminalBinder) const; 116 std::set<int> GetBinderPeerPids(std::string& stack, int pid, std::set<int>& asyncPids, 117 AppfreezeManager::TerminalBinder& terminalBinder) const; 118 void FindStackByPid(std::string& ret, int pid) const; 119 std::string CatchJsonStacktrace(int pid, const std::string& faultType, const std::string& stack) const; 120 std::string CatcherStacktrace(int pid, const std::string& stack) const; 121 int AcquireStack(const FaultData& faultData, const AppInfo& appInfo, const std::string& memoryContent); 122 int NotifyANR(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo, 123 const std::string& binderInfo, const std::string& memoryContent); 124 int64_t GetFreezeCurrentTime(); 125 void SetFreezeState(int32_t pid, int state, const std::string& errorName); 126 int GetFreezeState(int32_t pid); 127 int64_t GetFreezeTime(int32_t pid); 128 void ClearOldInfo(); 129 void CollectFreezeSysMemory(std::string& memoryContent); 130 int MergeNotifyInfo(FaultData& faultNotifyData, const AppfreezeManager::AppInfo& appInfo); 131 132 static const inline std::string LOGGER_DEBUG_PROC_PATH = "/proc/transaction_proc"; 133 std::string name_; 134 static ffrt::mutex singletonMutex_; 135 static std::shared_ptr<AppfreezeManager> instance_; 136 static ffrt::mutex freezeMutex_; 137 std::map<int32_t, AppFreezeInfo> appfreezeInfo_; 138 static ffrt::mutex catchStackMutex_; 139 static std::map<int, std::string> catchStackMap_; 140 static ffrt::mutex freezeFilterMutex_; 141 std::map<std::string, AppFreezeInfo> appfreezeFilterMap_; 142 }; 143 } // namespace AppExecFwk 144 } // namespace OHOS 145 #endif // OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H