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 DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H 17 #define DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H 18 19 #include <fcntl.h> 20 #include <sys/stat.h> 21 #include <unistd.h> 22 23 #include <ctime> 24 #include <string> 25 26 #include "connection.h" 27 #include "rdb_store_config.h" 28 #include "rdb_types.h" 29 #include "rdb_dfx_errno.h" 30 namespace OHOS::NativeRdb { 31 using DebugInfo = OHOS::DistributedRdb::RdbDebugInfo; 32 using DfxInfo = OHOS::DistributedRdb::RdbDfxInfo; 33 struct RdbCorruptedEvent { 34 std::string bundleName; 35 std::string moduleName; 36 std::string storeType; 37 std::string storeName; 38 uint32_t securityLevel; 39 uint32_t pathArea; 40 uint32_t encryptStatus; 41 uint32_t integrityCheck; 42 uint32_t errorCode; 43 int32_t systemErrorNo; 44 std::string appendix; 45 time_t errorOccurTime; 46 std::string path; 47 std::map<std::string, DebugInfo> debugInfos; 48 DfxInfo dfxInfo; 49 }; 50 51 struct RdbFaultCode { 52 int nativeCode; 53 uint8_t faultCounter; 54 }; 55 56 // Fault Type Define 57 static constexpr const char *FT_OPEN = "OPEN_DB"; 58 static constexpr const char *FT_CURD = "CURD_DB"; 59 static constexpr const char *FT_EX_FILE = "EX_FILE"; 60 static constexpr const char *FT_EX_HUKS = "EX_HUKS"; 61 static constexpr const char *FT_CP = "CHECK_POINT"; 62 static constexpr const char *FT_SQLITE = "SQLITE"; 63 64 static constexpr const char *BUNDLE_NAME_COMMON = "common"; 65 66 class RdbFaultEvent { 67 public: 68 RdbFaultEvent(const std::string &faultType, int32_t errorCode, const std::string &bundleName, 69 const std::string &custLog); 70 GetBundleName()71 std::string GetBundleName() const { return bundleName_; }; GetFaultType()72 std::string GetFaultType() const { return faultType_; } GetErrCode()73 int32_t GetErrCode() const { return errorCode_; } GetLogInfo()74 std::string GetLogInfo() const { return custLog_; }; 75 virtual void Report() const; 76 77 protected: SetBundleName(const std::string & name)78 void SetBundleName(const std::string &name) { bundleName_ = name; }; 79 80 private: 81 std::string bundleName_; 82 std::string faultType_; 83 std::string custLog_; 84 int32_t errorCode_; 85 }; 86 87 class RdbFaultDbFileEvent : public RdbFaultEvent { 88 public: 89 RdbFaultDbFileEvent(const std::string &faultType, int32_t errorCode, const RdbStoreConfig &config, 90 const std::string &custLog = "", bool printDbInfo = false); 91 92 virtual void Report() const override; 93 94 private: 95 std::string BuildLogInfo() const; 96 std::string BuildConfigLog() const; 97 98 const RdbStoreConfig &config_; 99 bool printDbInfo_; 100 }; 101 102 class RdbFaultHiViewReporter { 103 public: 104 using Collector = int32_t (*)(const RdbStoreConfig &config, std::map<std::string, 105 DistributedRdb::RdbDebugInfo>&, DistributedRdb::RdbDfxInfo&); 106 static RdbCorruptedEvent Create(const RdbStoreConfig &config, int32_t errCode, const std::string &appendix = "", 107 bool needSyncParaFromSrv = true); 108 static bool RegCollector(Collector collector); 109 static void ReportCorrupted(const RdbCorruptedEvent &eventInfo); 110 static void ReportCorruptedOnce(const RdbCorruptedEvent &eventInfo); 111 static void ReportFault(const RdbFaultEvent &faultEvent); 112 static void ReportRestore(const RdbCorruptedEvent &eventInfo, bool repair = true); 113 static bool IsReportCorruptedFault(const std::string &dbPath); 114 static std::string GetBundleName(const std::string &bundleName, const std::string &storeName); 115 116 private: 117 static void Update(std::map<std::string, DebugInfo> &localInfos, const std::map<std::string, DebugInfo> &infos); 118 static void CreateCorruptedFlag(const std::string &dbPath); 119 static void DeleteCorruptedFlag(const std::string &dbPath); 120 static bool IsReportFault(const std::string &bundleName, int32_t errCode); 121 static uint8_t *GetFaultCounter(int32_t errCode); 122 static Collector collector_; 123 static RdbFaultCode faultCounters_[]; 124 static bool memCorruptReportedFlg_; 125 }; 126 } // namespace OHOS::NativeRdb 127 #endif // DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H 128