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 = std::strtol(record->GetEventValue(EventStore::EventCol::TID).c_str(), nullptr, 0);
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 }
58
59 long uid = record->GetEventIntValue(FreezeCommon::EVENT_UID);
60 uid = uid ? uid : record->GetUid();
61 resultWatchPoint = WatchPoint::Builder()
62 .InitSeq(record->GetSeq()).InitDomain(result.GetDomain()).InitStringId(result.GetStringId())
63 .InitTimestamp(record->happenTime_).InitPid(pid).InitUid(uid).InitTid(tid).InitPackageName(packageName)
64 .InitProcessName(record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME))
65 .InitMsg(StringUtil::ReplaceStr(record->GetEventValue(FreezeCommon::EVENT_MSG), "\\n", "\n")).Build();
66 std::string info = record->GetEventValue(EventStore::EventCol::INFO);
67 std::regex reg("logPath:([^,]+)");
68 std::smatch smatchResult;
69 if (std::regex_search(info, smatchResult, reg)) {
70 resultWatchPoint.SetLogPath(smatchResult[1].str());
71 }
72 }
73 }
74
SelectEventFromDB(unsigned long long start,unsigned long long end,std::vector<WatchPoint> & list,const struct WatchParams & watchParams,const FreezeResult & result)75 void DBHelper::SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list,
76 const struct WatchParams& watchParams, const FreezeResult& result)
77 {
78 if (freezeCommon_ == nullptr) {
79 return;
80 }
81 if (start > end) {
82 return;
83 }
84
85 auto eventQuery = EventStore::SysEventDao::BuildQuery(result.GetDomain(), {result.GetStringId()});
86 std::vector<std::string> selections { EventStore::EventCol::TS };
87 if (eventQuery) {
88 eventQuery->Select(selections)
89 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
90 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
91 } else {
92 HIVIEW_LOGE("event query selections failed.");
93 return;
94 }
95 EventStore::ResultSet set = eventQuery->Execute();
96 if (set.GetErrCode() != 0) {
97 HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
98 return;
99 }
100
101 WatchPoint resultWatchPoint;
102 GetResultWatchPoint(watchParams, result, set, resultWatchPoint);
103 if (resultWatchPoint.GetDomain().empty()) {
104 return;
105 }
106 list.push_back(resultWatchPoint);
107 HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
108 }
109
SelectRecords(unsigned long long start,unsigned long long end,const std::string & domain,const std::vector<std::string> & eventNames)110 std::vector<SysEvent> DBHelper::SelectRecords(unsigned long long start, unsigned long long end,
111 const std::string& domain, const std::vector<std::string>& eventNames)
112 {
113 std::vector<SysEvent> records;
114 if (freezeCommon_ == nullptr || start >= end) {
115 return records;
116 }
117 auto eventQuery = EventStore::SysEventDao::BuildQuery(domain, eventNames);
118 if (!eventQuery) {
119 return records;
120 }
121 eventQuery->Select({ EventStore::EventCol::TS })
122 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
123 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
124 EventStore::ResultSet set = eventQuery->Execute();
125 if (set.GetErrCode() == 0) {
126 while (set.HasNext()) {
127 auto record = set.Next();
128 records.emplace_back(*record);
129 }
130 }
131 return records;
132 }
133 } // namespace HiviewDFX
134 } // namespace OHOS
135