• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     struct ParamInfo {
51         int typeId = TypeAttribute::NORMAL_TIMEOUT;
52         int32_t pid = 0;
53         std::string eventName;
54         std::string bundleName;
55         std::string msg;
56     };
57 
58     AppfreezeManager();
59     ~AppfreezeManager();
60 
61     static std::shared_ptr<AppfreezeManager> GetInstance();
62     static void DestroyInstance();
63     int AppfreezeHandle(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo);
64     int AppfreezeHandleWithStack(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo);
65     int LifecycleTimeoutHandle(const ParamInfo& info, std::unique_ptr<FreezeUtil::LifecycleFlow> flow = nullptr);
66     bool IsHandleAppfreeze(const std::string& bundleName);
67     bool IsProcessDebug(int32_t pid, std::string processName);
68 
69 private:
70     AppfreezeManager& operator=(const AppfreezeManager&) = delete;
71     AppfreezeManager(const AppfreezeManager&) = delete;
72     uint64_t GetMilliseconds();
73     std::map<int, std::set<int>> BinderParser(std::ifstream& fin, std::string& stack) const;
74     void ParseBinderPids(const std::map<int, std::set<int>>& binderInfo, std::set<int>& pids, int pid, int layer) const;
75     std::set<int> GetBinderPeerPids(std::string& stack, int pid) const;
76     std::string CatchJsonStacktrace(int pid) const;
77     std::string CatcherStacktrace(int pid) const;
78     int AcquireStack(const FaultData& faultData, const AppInfo& appInfo);
79     int NotifyANR(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo, const std::string& binderInfo);
80 
81     static const inline std::string LOGGER_DEBUG_PROC_PATH = "/proc/transaction_proc";
82     std::string name_;
83     static ffrt::mutex singletonMutex_;
84     static std::shared_ptr<AppfreezeManager> instance_;
85 };
86 }  // namespace AppExecFwk
87 }  // namespace OHOS
88 #endif  // OHOS_ABILITY_RUNTIME_APPFREEZE_MAMAGER_H