• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "logger.h"
21 #include "string_util.h"
22 #include "sys_event_dao.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("FreezeDetector");
SelectEventFromDB(unsigned long long start,unsigned long long end,std::vector<WatchPoint> & list,const std::string & watchPackage,const FreezeResult & result)27 void DBHelper::SelectEventFromDB(unsigned long long start, unsigned long long end, std::vector<WatchPoint>& list,
28     const std::string& watchPackage, const FreezeResult& result)
29 {
30     if (freezeCommon_ == nullptr) {
31         return;
32     }
33     if (start > end) {
34         return;
35     }
36 
37     auto eventQuery = EventStore::SysEventDao::BuildQuery(result.GetDomain(), {result.GetStringId()});
38     std::vector<std::string> selections { EventStore::EventCol::TS };
39     (*eventQuery).Select(selections)
40         .Where(EventStore::EventCol::TS, EventStore::Op::GE, static_cast<int64_t>(start))
41         .And(EventStore::EventCol::TS, EventStore::Op::LE, static_cast<int64_t>(end));
42 
43     EventStore::ResultSet set = eventQuery->Execute();
44     if (set.GetErrCode() != 0) {
45         HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
46         return;
47     }
48 
49     while (set.HasNext()) {
50         auto record = set.Next();
51 
52         std::string packageName = record->GetEventValue(FreezeCommon::EVENT_PACKAGE_NAME);
53         packageName = packageName.empty() ?
54             record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME) : packageName;
55         if (result.GetSamePackage() == "true" && watchPackage != packageName) {
56             HIVIEW_LOGE("failed to match the same package: %{public}s and  %{public}s",
57                 watchPackage.c_str(), packageName.c_str());
58             continue;
59         }
60 
61         long pid = record->GetEventIntValue(FreezeCommon::EVENT_PID);
62         pid = pid ? pid : record->GetPid();
63         long uid = record->GetEventIntValue(FreezeCommon::EVENT_UID);
64         uid = uid ? uid : record->GetUid();
65         long tid = std::strtoul(record->GetEventValue(EventStore::EventCol::TID).c_str(), nullptr, 0);
66 
67         WatchPoint watchPoint = WatchPoint::Builder()
68             .InitSeq(record->GetSeq())
69             .InitDomain(result.GetDomain())
70             .InitStringId(result.GetStringId())
71             .InitTimestamp(record->happenTime_)
72             .InitPid(pid)
73             .InitUid(uid)
74             .InitTid(tid)
75             .InitPackageName(packageName)
76             .InitProcessName(record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME))
77             .InitMsg(StringUtil::ReplaceStr(record->GetEventValue(FreezeCommon::EVENT_MSG), "\\n", "\n"))
78             .Build();
79 
80         std::string info = record->GetEventValue(EventStore::EventCol::INFO);
81         std::regex reg("logPath:([^,]+)");
82         std::smatch smatchResult;
83         if (std::regex_search(info, smatchResult, reg)) {
84             watchPoint.SetLogPath(smatchResult[1].str());
85         }
86 
87         list.push_back(watchPoint);
88     }
89 
90     HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
91 }
92 } // namespace HiviewDFX
93 } // namespace OHOS
94