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