1 /* 2 * Copyright (c) 2021-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 RELIABILITY_XCOLLIE_UTILS_H 17 #define RELIABILITY_XCOLLIE_UTILS_H 18 19 #include <chrono> 20 #include <string> 21 #include <sys/ioctl.h> 22 #include <fstream> 23 #include <map> 24 #include <vector> 25 26 #include "hilog/log.h" 27 28 #undef LOG_DOMAIN 29 #define LOG_DOMAIN 0xD002D06 30 31 #undef LOG_TAG 32 #define LOG_TAG "XCollie" 33 34 namespace OHOS { 35 namespace HiviewDFX { 36 constexpr uint64_t MIN_APP_UID = 20000; 37 constexpr uint64_t TO_MILLISECOND_MULTPLE = 1000; 38 constexpr int64_t SEC_TO_MICROSEC = 1000000; 39 constexpr const char* const WATCHDOG_DIR = "/data/storage/el2/log/watchdog/"; 40 constexpr const char* const FREEZE_DIR = "/data/storage/el2/log/watchdog/freeze/"; 41 42 #define XCOLLIE_LOGF(...) HILOG_FATAL(LOG_CORE, ##__VA_ARGS__) 43 #define XCOLLIE_LOGE(...) HILOG_ERROR(LOG_CORE, ##__VA_ARGS__) 44 #define XCOLLIE_LOGW(...) HILOG_WARN(LOG_CORE, ##__VA_ARGS__) 45 #define XCOLLIE_LOGI(...) HILOG_INFO(LOG_CORE, ##__VA_ARGS__) 46 #define XCOLLIE_LOGD(...) HILOG_DEBUG(LOG_CORE, ##__VA_ARGS__) 47 #define MAGIC_NUM 0x9517 48 #define HTRANSIO 0xAB 49 #define LOGGER_GET_STACK _IO(HTRANSIO, 9) 50 51 constexpr OHOS::HiviewDFX::HiLogLabel KLOG_LABEL = { 52 LOG_KMSG, 53 LOG_DOMAIN, 54 LOG_TAG 55 }; 56 #define XCOLLIE_KLOGI(...) \ 57 do { \ 58 (void)OHOS::HiviewDFX::HiLog::Info(KLOG_LABEL, __VA_ARGS__); \ 59 HILOG_INFO(LOG_CORE, __VA_ARGS__); \ 60 } while (0) 61 62 #define XCOLLIE_KLOGE(...) \ 63 do { \ 64 (void)OHOS::HiviewDFX::HiLog::Error(KLOG_LABEL, __VA_ARGS__); \ 65 HILOG_ERROR(LOG_CORE, __VA_ARGS__); \ 66 } while (0) 67 68 struct FileInfo { 69 std::string filePath; 70 time_t mtime; 71 }; 72 #ifdef SUSPEND_CHECK_ENABLE 73 std::pair<double, double> GetSuspendTime(const char* path, uint64_t &now); 74 #endif 75 76 uint64_t GetCurrentTickMillseconds(); 77 78 79 uint64_t GetCurrentBootMillseconds(); 80 81 void CalculateTimes(uint64_t& bootTimeStart, uint64_t& monoTimeStart); 82 83 uint64_t GetNumsDiffAbs(const uint64_t& numOne, const uint64_t& numTwo); 84 85 bool IsFileNameFormat(char c); 86 87 std::string GetSelfProcName(); 88 89 std::string GetFirstLine(const std::string& path); 90 91 std::string GetProcessNameFromProcCmdline(int32_t pid = 0); 92 93 std::string GetLimitedSizeName(std::string name); 94 95 bool IsProcessDebug(int32_t pid); 96 97 void DelayBeforeExit(unsigned int leftTime); 98 99 std::string TrimStr(const std::string& str, const char cTrim = ' '); 100 101 void SplitStr(const std::string& str, const std::string& sep, 102 std::vector<std::string>& strs, bool canEmpty = false, bool needTrim = true); 103 104 int ParsePeerBinderPid(std::ifstream& fin, int32_t pid); 105 106 bool KillProcessByPid(int32_t pid); 107 108 bool IsDeveloperOpen(); 109 110 bool IsBetaVersion(); 111 112 std::string GetFormatDate(); 113 114 std::string FormatTime(const std::string &format); 115 116 bool CreateDir(const std::string& dirPath); 117 118 void GetFilesByDir(std::vector<FileInfo> &fileList, const std::string& dir); 119 120 int ClearOldFiles(const std::vector<FileInfo> &fileList); 121 122 bool ClearFreezeFileIfNeed(uint64_t stackSize); 123 124 bool WriteStackToFd(int32_t pid, std::string& path, const std::string& stack, 125 const std::string& eventName, bool& isOverLimit); 126 127 int64_t GetTimeStamp(); 128 129 void* FunctionOpen(void* funcHandler, const char* funcName); 130 131 int32_t GetUidByPid(const int32_t pid); 132 133 int64_t GetAppStartTime(int32_t pid, int64_t tid); 134 135 std::map<std::string, int> GetReportTimesMap(); 136 137 void UpdateReportTimes(const std::string& bundleName, int32_t& times, int32_t& checkInterval); 138 139 bool SaveStringToFile(const std::string& path, const std::string& content); 140 141 bool IsNum(const std::string& str); 142 143 bool GetKeyValueByStr(const std::string& tokens, std::string& key, std::string& value, 144 char flag); 145 } // end of HiviewDFX 146 } // end of OHOS 147 #endif 148