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