1 /* 2 * Copyright (c) 2024 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 HIDEBUG_UTIL_H_ 17 #define HIDEBUG_UTIL_H_ 18 19 #include <atomic> 20 #include <cstdint> 21 #include <functional> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 #include <vector> 26 27 #include "hidebug_native_type.h" 28 namespace OHOS { 29 namespace HiviewDFX { 30 31 int64_t GetElapsedNanoSecondsSinceBoot(); 32 33 int64_t GetRealNanoSecondsTimestamp(); 34 35 template<typename T> 36 class CachedValue { 37 public: 38 CachedValue() = default; GetOrUpdateCachedValue(int64_t effectiveTime,std::function<int (T &)> getValue)39 std::pair<int, T> GetOrUpdateCachedValue(int64_t effectiveTime, std::function<int(T &)> getValue) 40 { 41 std::pair<int, T> ret; 42 int64_t currentTime = GetElapsedNanoSecondsSinceBoot(); 43 if (currentTime > updateTime_.load(std::memory_order_relaxed) + effectiveTime) { 44 std::unique_lock<std::mutex> lock(updateValueMutex_); 45 if (currentTime > updateTime_.load(std::memory_order_acquire) + effectiveTime) { 46 ret.first = getValue(ret.second); 47 if (ret.first == NATIVE_SUCCESS) { 48 std::unique_lock<std::mutex> lockCacheValue(cachedValueMutex_); 49 cachedValue_ = ret.second; 50 updateTime_.store(currentTime, std::memory_order_release); 51 } 52 return ret; 53 } 54 } 55 std::unique_lock<std::mutex> lockCacheValue(cachedValueMutex_); 56 ret.second = cachedValue_; 57 ret.first = NATIVE_SUCCESS; 58 return ret; 59 } 60 61 private: 62 T cachedValue_; 63 std::mutex cachedValueMutex_; 64 std::mutex updateValueMutex_; 65 std::atomic<int64_t> updateTime_ = INT64_MIN; 66 }; 67 68 enum class DirectoryType { 69 CACHE = 0, 70 FILE = 1, 71 }; 72 73 std::string GetProcessDir(DirectoryType type); 74 75 std::vector<std::string> SplitStr(const std::string& origin, char delimiter, 76 const std::function<bool(std::string&)>& filter = nullptr); 77 78 bool GetXAttr(const std::string& fileName, const std::string& key, std::string& value, size_t maxLength); 79 80 bool SetXAttr(const std::string& fileName, const std::string& key, const std::string& value); 81 82 bool CreateFile(const std::string &path); 83 84 bool CreateDirectory(const std::string &path, unsigned mode); 85 86 bool IsLegalPath(const std::string& path); 87 88 uint64_t GetFileSize(const std::string& path); 89 90 class SmartFile { 91 public: 92 static std::unique_ptr<SmartFile> OpenFile(const std::string& path, const std::string& mode); 93 ~SmartFile(); 94 SmartFile(SmartFile&& other) = delete; 95 SmartFile& operator=(SmartFile&& other) = delete; 96 SmartFile(const SmartFile&) = delete; 97 SmartFile& operator=(const SmartFile&) = delete; 98 bool Write(const void *__restrict dataPtr, size_t itemSize, size_t dataNum); 99 size_t Read(void *__restrict dataPtr, size_t itemSize, size_t dataNum); 100 private: SmartFile(FILE * file)101 explicit SmartFile(FILE *file) : file_(file) {} 102 FILE *file_; 103 }; 104 105 bool IsBetaVersion(); 106 107 bool IsDebuggableHap(); 108 109 bool IsDeveloperOptionsEnabled(); 110 111 bool CheckVersionType(const std::string& type, const std::string& key); 112 113 bool CreateResourceLimitDir(); 114 } 115 } 116 #endif // HIDEBUG_UTIL_H_ 117