• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     virtual ~RdbFaultEvent() = default;
77 
78 protected:
SetBundleName(const std::string & name)79     void SetBundleName(const std::string &name) { bundleName_ = name; };
80 
81 private:
82     std::string bundleName_;
83     std::string faultType_;
84     std::string custLog_;
85     int32_t errorCode_;
86 };
87 
88 class RdbFaultDbFileEvent : public RdbFaultEvent {
89 public:
90     RdbFaultDbFileEvent(const std::string &faultType, int32_t errorCode, const RdbStoreConfig &config,
91         const std::string &custLog = "", bool printDbInfo = false);
92 
93     virtual void Report() const override;
94 
95 private:
96     std::string BuildLogInfo() const;
97     std::string BuildConfigLog() const;
98 
99     const RdbStoreConfig &config_;
100     bool printDbInfo_;
101 };
102 
103 class RdbFaultHiViewReporter {
104 public:
105     using Collector =  int32_t (*)(const RdbStoreConfig &config, std::map<std::string,
106         DistributedRdb::RdbDebugInfo>&, DistributedRdb::RdbDfxInfo&);
107     static RdbCorruptedEvent Create(const RdbStoreConfig &config, int32_t errCode, const std::string &appendix = "",
108         bool needSyncParaFromSrv = true);
109     static bool RegCollector(Collector collector);
110     static void ReportCorrupted(const RdbCorruptedEvent &eventInfo);
111     static void ReportCorruptedOnce(const RdbCorruptedEvent &eventInfo);
112     static void ReportFault(const RdbFaultEvent &faultEvent);
113     static void ReportRestore(const RdbCorruptedEvent &eventInfo, bool repair = true);
114     static bool IsReportCorruptedFault(const std::string &dbPath);
115     static std::string GetBundleName(const std::string &bundleName, const std::string &storeName);
116     static void ReportRAGFault(const std::string &errMsg, const std::string &functionName,
117         const std::string &bundleName, const int faultType, const int errCode);
118 
119 private:
120     static void Update(std::map<std::string, DebugInfo> &localInfos, const std::map<std::string, DebugInfo> &infos);
121     static void CreateCorruptedFlag(const std::string &dbPath);
122     static void DeleteCorruptedFlag(const std::string &dbPath);
123     static bool IsReportFault(const std::string &bundleName, int32_t errCode);
124     static uint8_t *GetFaultCounter(int32_t errCode);
125     static Collector collector_;
126     static RdbFaultCode faultCounters_[];
127     static bool memCorruptReportedFlg_;
128 };
129 } // namespace OHOS::NativeRdb
130 #endif // DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H
131