• 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(bool all,unsigned long long start,unsigned long long end,std::list<WatchPoint> & list)27 void DBHelper::SelectEventFromDB(
28     bool all, unsigned long long start, unsigned long long end, std::list<WatchPoint>& list)
29 {
30     if (freezeCommon_ == nullptr) {
31         return;
32     }
33     if (start > end) {
34         return;
35     }
36 
37     auto eventQuery = EventStore::SysEventDao::BuildQuery(EventStore::StoreType::FAULT);
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     if (all == false) { // with or without resolved events
43         (*eventQuery).And(EventStore::Cond(EventStore::EventCol::INFO, EventStore::Op::NSW, "isResolved"));
44         //    .Or(EventStore::Cond(EventStore::EventCol::INFO, EventStore::Op::NU)));
45     }
46 
47     EventStore::ResultSet set = eventQuery->Execute();
48     if (set.GetErrCode() != 0) {
49         HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
50         return;
51     }
52 
53     while (set.HasNext()) {
54         auto record = set.Next();
55 
56         std::string domain = record->GetEventValue(EventStore::EventCol::DOMAIN);
57         std::string stringId = record->GetEventValue(EventStore::EventCol::NAME);
58         if (freezeCommon_->IsFreezeEvent(domain, stringId) == false) {
59             continue;
60         }
61 
62         long pid = record->GetEventIntValue(FreezeCommon::EVENT_PID);
63         pid = pid ? pid : record->GetPid();
64         long uid = record->GetEventIntValue(FreezeCommon::EVENT_UID);
65         uid = uid ? uid : record->GetUid();
66         long tid = std::strtoul(record->GetEventValue(EventStore::EventCol::TID).c_str(), nullptr, 0);
67 
68         WatchPoint watchPoint = WatchPoint::Builder()
69             .InitSeq(record->GetSeq())
70             .InitDomain(domain)
71             .InitStringId(stringId)
72             .InitTimestamp(record->happenTime_)
73             .InitPid(pid)
74             .InitUid(uid)
75             .InitTid(tid)
76             .InitPackageName(record->GetEventValue(FreezeCommon::EVENT_PACKAGE_NAME))
77             .InitProcessName(record->GetEventValue(FreezeCommon::EVENT_PROCESS_NAME))
78             .InitMsg(StringUtil::ReplaceStr(record->GetEventValue(FreezeCommon::EVENT_MSG), "\\n", "\n"))
79             .Build();
80 
81         std::string info = record->GetEventValue(EventStore::EventCol::INFO);
82         std::regex reg("logPath:([^,]+)");
83         std::smatch result;
84         if (std::regex_search(info, result, reg)) {
85             watchPoint.SetLogPath(result[1].str());
86         }
87 
88         list.push_back(watchPoint);
89     }
90 
91     list.sort();
92     HIVIEW_LOGI("select event from db, size =%{public}zu.", list.size());
93 }
94 
UpdateEventIntoDB(const WatchPoint & watchPoint,int id)95 void DBHelper::UpdateEventIntoDB(const WatchPoint& watchPoint, int id)
96 {
97     auto eventQuery = EventStore::SysEventDao::BuildQuery(EventStore::StoreType::FAULT);
98     std::vector<std::string> selections { EventStore::EventCol::TS };
99     EventStore::ResultSet set = (*eventQuery).Select(selections)
100         .Where(EventStore::EventCol::TS, EventStore::Op::EQ, static_cast<int64_t>(watchPoint.GetTimestamp()))
101         .Execute();
102     if (set.GetErrCode() != 0) {
103         HIVIEW_LOGE("failed to select event from db, error:%{public}d.", set.GetErrCode());
104         return;
105     }
106 
107     if (set.HasNext()) {
108         auto record = set.Next();
109 
110         std::string info = "isResolved,eventId:" + std::to_string(id);
111         std::string logPath = watchPoint.GetLogPath();
112         if (logPath != "" && logPath != "nolog") {
113             info += ",logPath:" + logPath;
114         }
115         record->SetEventValue(EventStore::EventCol::INFO, info);
116 
117         std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>(*record);
118         if (EventStore::SysEventDao::Update(sysEvent, false) != 0) {
119             HIVIEW_LOGE("failed to update info into db, stringId:%{public}s.", watchPoint.GetStringId().c_str());
120         }
121     }
122 }
123 } // namespace HiviewDFX
124 } // namespace OHOS
125