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 namespace {
28 static constexpr const char* const EVENT_MSG = "MSG";
29 }
30 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
GetResultMap(const struct WatchParams& watchParams, const FreezeResult& result, EventStore::ResultSet& set, std::map<std::string, WatchPoint>& resultMap)31 void DBHelper::GetResultMap(const struct WatchParams& watchParams, const FreezeResult& result,
32 EventStore::ResultSet& set, std::map<std::string, WatchPoint>& resultMap)
33 {
34 while (set.HasNext()) {
35 auto record = set.Next();
36 std::string key = record->domain_ + "-" + record->eventName_;
37
38 std::string packageName = record->GetEventValue(FreezeCommon::EVENT_PACKAGE_NAME);
39 packageName = packageName.empty() ?
40 record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME) : packageName;
41 long pid = record->GetEventIntValue(FreezeCommon::EVENT_PID);
42 pid = pid ? pid : record->GetPid();
43 if (result.GetSamePackage() == "true" && (watchParams.packageName != packageName || watchParams.pid != pid)) {
44 HIVIEW_LOGE("failed to match the same package: %{public}s and %{public}s, the same pid: %{public}lu and "
45 "%{public}lu", watchParams.packageName.c_str(), packageName.c_str(), watchParams.pid, pid);
46 continue;
47 }
48
49 long uid = record->GetEventIntValue(FreezeCommon::EVENT_UID);
50 uid = uid ? uid : record->GetUid();
51 long tid = std::strtol(record->GetEventValue(EventStore::EventCol::TID).c_str(), nullptr, 0);
52
53 WatchPoint watchPoint = WatchPoint::Builder()
54 .InitSeq(record->GetSeq()).InitDomain(result.GetDomain()).InitStringId(result.GetStringId())
55 .InitTimestamp(record->happenTime_).InitPid(pid).InitUid(uid).InitTid(tid).InitPackageName(packageName)
56 .InitProcessName(record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME))
57 .InitMsg(StringUtil::ReplaceStr(record->GetEventValue(EVENT_MSG), "\\n", "\n")).Build();
58
59 std::string info = record->GetEventValue(EventStore::EventCol::INFO);
60 std::regex reg("logPath:([^,]+)");
61 std::smatch smatchResult;
62 if (std::regex_search(info, smatchResult, reg)) {
63 watchPoint.SetLogPath(smatchResult[1].str());
64 }
65
66 if (resultMap.find(key) != resultMap.end() && watchPoint.GetTimestamp() > resultMap[key].GetTimestamp()) {
67 resultMap[key] = watchPoint;
68 } else {
69 resultMap.insert(std::pair<std::string, WatchPoint>(key, watchPoint));
70 }
71 }
72 }
73
SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list, const struct WatchParams& watchParams, const FreezeResult& result)74 void DBHelper::SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list,
75 const struct WatchParams& watchParams, const FreezeResult& result)
76 {
77 if (freezeCommon_ == nullptr) {
78 return;
79 }
80 if (start > end) {
81 return;
82 }
83
84 auto eventQuery = EventStore::SysEventDao::BuildQuery(result.GetDomain(), {result.GetStringId()});
85 std::vector<std::string> selections { EventStore::EventCol::TS };
86 if (eventQuery) {
87 eventQuery->Select(selections)
88 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
89 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
90 } else {
91 HIVIEW_LOGE("event query selections failed.");
92 return;
93 }
94 EventStore::ResultSet set = eventQuery->Execute();
95 if (set.GetErrCode() != 0) {
96 HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
97 return;
98 }
99
100 std::map<std::string, WatchPoint> resultMap;
101 GetResultMap(watchParams, result, set, resultMap);
102
103 std::map<std::string, WatchPoint>::iterator it;
104 for (it = resultMap.begin(); it != resultMap.end(); ++it) {
105 list.push_back(it->second);
106 }
107 sort(list.begin(), list.end(), [] (const WatchPoint& frontWatchPoint, const WatchPoint& rearWatchPoint) {
108 return frontWatchPoint.GetTimestamp() < rearWatchPoint.GetTimestamp();
109 });
110
111 HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
112 }
113 } // namespace HiviewDFX
114 } // namespace OHOS
115