• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 HIVIEWDFX_HICHECKER_H
17 #define HIVIEWDFX_HICHECKER_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "caution.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace Rule {
27 const uint64_t RULE_CAUTION_PRINT_LOG = 1ULL << 63;
28 const uint64_t RULE_CAUTION_TRIGGER_CRASH = 1ULL << 62;
29 const uint64_t RULE_THREAD_CHECK_SLOW_PROCESS = 1ULL;
30 const uint64_t RULE_CHECK_SLOW_EVENT = 1ULL << 32;
31 const uint64_t RULE_CHECK_ABILITY_CONNECTION_LEAK = 1ULL << 33;
32 const uint64_t ALL_RULES = RULE_CAUTION_PRINT_LOG | RULE_CAUTION_TRIGGER_CRASH
33             | RULE_THREAD_CHECK_SLOW_PROCESS | RULE_CHECK_SLOW_EVENT | RULE_CHECK_ABILITY_CONNECTION_LEAK;
34 const uint64_t ALL_PROCESS_RULES = RULE_CHECK_SLOW_EVENT | RULE_CHECK_ABILITY_CONNECTION_LEAK;
35 const uint64_t ALL_THREAD_RULES = RULE_THREAD_CHECK_SLOW_PROCESS;
36 const uint64_t ALL_CAUTION_RULES = RULE_CAUTION_PRINT_LOG | RULE_CAUTION_TRIGGER_CRASH;
37 };
38 
39 class CautionDetail {
40 public:
CautionDetail(const Caution & caution,uint64_t rules)41     CautionDetail(const Caution& caution, uint64_t rules) : caution_(caution), rules_(rules) {}
42     CautionDetail(const CautionDetail&) = delete;
43     CautionDetail& operator = (CautionDetail&) = delete;
CautionEnable(uint64_t rule)44     bool CautionEnable(uint64_t rule) const
45     {
46         return rule == (rules_ & rule);
47     }
48 
49     const Caution caution_;
50     uint64_t rules_;
51 };
52 
53 class HiChecker {
54 public:
55     HiChecker() = delete;
56     HiChecker(const HiChecker&) = delete;
57     HiChecker& operator = (HiChecker&) = delete;
58     static void NotifySlowProcess(const std::string& tag);
59     static void NotifySlowEvent(const std::string& tag);
60     static void NotifyAbilityConnectionLeak(const Caution& caution);
61     static bool NeedCheckSlowEvent();
62 
63     static void AddRule(uint64_t rule);
64     static void RemoveRule(uint64_t rule);
65     static uint64_t GetRule();
66     static bool Contains(uint64_t rule);
67 private:
68     static void HandleCaution(const Caution& caution);
69     static void OnThreadCautionFound(CautionDetail& cautionDetail);
70     static void OnProcessCautionFound(CautionDetail& cautionDetail);
71     static void PrintLog(const CautionDetail& cautionDetail);
72     static void TriggerCrash(const CautionDetail& cautionDetail);
73     static bool HasCautionRule(uint64_t rules);
74     static void DumpStackTrace(std::string& msg);
75     static bool CheckRule(uint64_t rule);
76 
77     static std::mutex mutexLock_;
78     static volatile bool checkMode_;
79     static volatile uint64_t processRules_;
80     static thread_local uint64_t threadLocalRules_;
81 };
82 } // HiviewDFX
83 } // OHOS
84 #endif // HIVIEWDFX_HICHECKER_H