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