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 std::string & watchPackage,const FreezeResult & result,EventStore::ResultSet & set,std::map<std::string,WatchPoint> & resultMap)28 void DBHelper::GetResultMap(const std::string& watchPackage, 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 if (result.GetSamePackage() == "true" && watchPackage != packageName) {
39 HIVIEW_LOGE("failed to match the same package: %{public}s and %{public}s",
40 watchPackage.c_str(), packageName.c_str());
41 continue;
42 }
43
44 long pid = record->GetEventIntValue(FreezeCommon::EVENT_PID);
45 pid = pid ? pid : record->GetPid();
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 std::string & watchPackage,const FreezeResult & result)71 void DBHelper::SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list,
72 const std::string& watchPackage, 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 (*eventQuery).Select(selections)
84 .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
85 .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
86
87 EventStore::ResultSet set = eventQuery->Execute();
88 if (set.GetErrCode() != 0) {
89 HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
90 return;
91 }
92
93 std::map<std::string, WatchPoint> resultMap;
94 GetResultMap(watchPackage, result, set, resultMap);
95
96 std::map<std::string, WatchPoint>::iterator it;
97 for (it = resultMap.begin(); it != resultMap.end(); ++it) {
98 list.push_back(it->second);
99 }
100
101 HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
102 }
103 } // namespace HiviewDFX
104 } // namespace OHOS
105