1 /*
2 * Copyright (c) 2021 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 #include "db_helper.h"
17
18 #include <regex>
19 #include <map>
20
21 #include "hiview_logger.h"
22 #include "string_util.h"
23 #include "sys_event_dao.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
GetResultWatchPoint(const struct WatchParams & watchParams,const FreezeResult & result,EventStore::ResultSet & set,WatchPoint & resultWatchPoint)28 void DBHelper::GetResultWatchPoint(const struct WatchParams& watchParams, const FreezeResult& result,
29 EventStore::ResultSet& set, WatchPoint& resultWatchPoint)
30 {
31 unsigned long long timestamp = watchParams.timestamp;
32 unsigned long long frontInterval = UINT64_MAX;
33 unsigned long long rearInterval = UINT64_MAX;
34 while (set.HasNext()) {
35 auto record = set.Next();
36 std::string packageName = record->GetEventValue(FreezeCommon::EVENT_PACKAGE_NAME);
37 packageName = packageName.empty() ?
38 record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME) : packageName;
39 long pid = record->GetEventIntValue(FreezeCommon::EVENT_PID);
40 pid = pid ? pid : record->GetPid();
41 long tid = record->GetEventIntValue(FreezeCommon::EVENT_TID);
42 if (result.GetSamePackage() == "true" && (watchParams.packageName != packageName || watchParams.pid != pid ||
43 (watchParams.tid > 0 && tid > 0 && watchParams.tid != tid))) {
44 HIVIEW_LOGE("failed to match query result, watchPoint = [%{public}s, %{public}ld, %{public}ld], "
45 "record = [%{public}s, %{public}ld, %{public}ld]",
46 watchParams.packageName.c_str(), watchParams.pid, watchParams.tid, packageName.c_str(), pid, tid);
47 continue;
48 }
49
50 if (record->happenTime_ < timestamp && timestamp - record->happenTime_ < frontInterval) {
51 frontInterval = timestamp - record->happenTime_;
52 } else if (frontInterval == UINT64_MAX) {
53 if (record->happenTime_ - timestamp >= rearInterval) {
54 continue;
55 }
56 rearInterval = record->happenTime_ - timestamp;
57 } else {
58 continue;
59 }
60
61 long uid = record->GetEventIntValue(FreezeCommon::EVENT_UID);
62 uid = uid ? uid : record->GetUid();
63 resultWatchPoint = WatchPoint::Builder()
64 .InitSeq(record->GetSeq()).InitDomain(result.GetDomain()).InitStringId(result.GetStringId())
65 .InitTimestamp(record->happenTime_).InitPid(pid).InitUid(uid).InitTid(tid).InitPackageName(packageName)
66 .InitProcessName(record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME))
67 .InitMsg(StringUtil::ReplaceStr(record->GetEventValue(FreezeCommon::EVENT_MSG), "\\n", "\n")).Build();
68 std::string info = record->GetEventValue(EventStore::EventCol::INFO);
69 std::regex reg("logPath:([^,]+)");
70 std::smatch smatchResult;
71 if (std::regex_search(info, smatchResult, reg)) {
72 resultWatchPoint.SetLogPath(smatchResult[1].str());
73 }
74 }
75 }
76
SelectEventFromDB(unsigned long long start,unsigned long long end,std::vector<WatchPoint> & list,const struct WatchParams & watchParams,const FreezeResult & result)77 void DBHelper::SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list,
78 const struct WatchParams& watchParams, const FreezeResult& result)
79 {
80 if (freezeCommon_ == nullptr) {
81 return;
82 }
83 if (start > end) {
84 return;
85 }
86
87 auto eventQuery = EventStore::SysEventDao::BuildQuery(result.GetDomain(), {result.GetStringId()});
88 std::vector<std::string> selections { EventStore::EventCol::TS };
89 if (eventQuery) {
90 eventQuery->Select(selections)
91 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
92 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
93 } else {
94 HIVIEW_LOGE("event query selections failed.");
95 return;
96 }
97 EventStore::ResultSet set = eventQuery->Execute();
98 if (set.GetErrCode() != 0) {
99 HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
100 return;
101 }
102
103 WatchPoint resultWatchPoint;
104 GetResultWatchPoint(watchParams, result, set, resultWatchPoint);
105 if (resultWatchPoint.GetDomain().empty()) {
106 return;
107 }
108 list.push_back(resultWatchPoint);
109 HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
110 }
111
SelectRecords(unsigned long long start,unsigned long long end,const std::string & domain,const std::vector<std::string> & eventNames)112 std::vector<SysEvent> DBHelper::SelectRecords(unsigned long long start, unsigned long long end,
113 const std::string& domain, const std::vector<std::string>& eventNames)
114 {
115 std::vector<SysEvent> records;
116 if (freezeCommon_ == nullptr || start >= end) {
117 return records;
118 }
119 auto eventQuery = EventStore::SysEventDao::BuildQuery(domain, eventNames);
120 if (!eventQuery) {
121 return records;
122 }
123 eventQuery->Select({ EventStore::EventCol::TS })
124 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
125 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
126 EventStore::ResultSet set = eventQuery->Execute();
127 if (set.GetErrCode() == 0) {
128 while (set.HasNext()) {
129 auto record = set.Next();
130 records.emplace_back(*record);
131 }
132 }
133 return records;
134 }
135 } // namespace HiviewDFX
136 } // namespace OHOS
137