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 "rule_cluster.h"
17
18 #include <sstream>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include "hiview_logger.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
27 namespace {
28 static constexpr const char* const DEFAULT_RULE_FILE = "/system/etc/hiview/freeze_rules.xml";
29 static constexpr const char* const TAG_FREEZE = "freeze";
30 static constexpr const char* const TAG_RULES = "rules";
31 static constexpr const char* const TAG_RULE = "rule";
32 static constexpr const char* const TAG_LINKS = "links";
33 static constexpr const char* const TAG_EVENT = "event";
34 static constexpr const char* const TAG_RESULT = "result";
35 static constexpr const char* const TAG_RELEVANCE = "relevance";
36 static constexpr const char* const ATTRIBUTE_ID = "id";
37 static constexpr const char* const ATTRIBUTE_WINDOW = "window";
38 static constexpr const char* const ATTRIBUTE_DELAY = "delay";
39 static constexpr const char* const ATTRIBUTE_DOMAIN = "domain";
40 static constexpr const char* const ATTRIBUTE_STRINGID = "stringid";
41 static constexpr const char* const ATTRIBUTE_TYPE = "type";
42 static constexpr const char* const ATTRIBUTE_USER = "user";
43 static constexpr const char* const ATTRIBUTE_WATCHPOINT = "watchpoint";
44 static constexpr const char* const ATTRIBUTE_CODE = "code";
45 static constexpr const char* const ATTRIBUTE_SCOPE = "scope";
46 static constexpr const char* const ATTRIBUTE_SAME_PACKAGE = "samePackage";
47 static constexpr const char* const ATTRIBUTE_ACTION = "action";
48 static constexpr const char* const ATTRIBUTE_APPLICATION = "application";
49 static constexpr const char* const ATTRIBUTE_SYSTEM = "system";
50 static const int MAX_FILE_SIZE = 512 * 1024;
51 }
52
FreezeRuleCluster()53 FreezeRuleCluster::FreezeRuleCluster()
54 {
55 rules_.clear();
56 }
57
~FreezeRuleCluster()58 FreezeRuleCluster::~FreezeRuleCluster()
59 {
60 rules_.clear();
61 }
62
Init()63 bool FreezeRuleCluster::Init()
64 {
65 if (access(DEFAULT_RULE_FILE, R_OK) != 0) {
66 HIVIEW_LOGE("cannot access rule file.");
67 return false;
68 }
69
70 if (CheckFileSize(DEFAULT_RULE_FILE) == false) {
71 HIVIEW_LOGE("bad rule file size.");
72 return false;
73 }
74
75 if (ParseRuleFile(DEFAULT_RULE_FILE) == false) {
76 HIVIEW_LOGE("failed to parse rule file.");
77 return false;
78 }
79
80 if (rules_.size() == 0) {
81 HIVIEW_LOGE("no rule in rule file.");
82 return false;
83 }
84
85 return true;
86 }
87
CheckFileSize(const std::string & path)88 bool FreezeRuleCluster::CheckFileSize(const std::string& path)
89 {
90 struct stat st;
91 if (stat(path.c_str(), &st) != 0) {
92 return false;
93 }
94 if (st.st_size > MAX_FILE_SIZE) {
95 return false;
96 }
97 return true;
98 }
99
ParseRuleFile(const std::string & file)100 bool FreezeRuleCluster::ParseRuleFile(const std::string& file)
101 {
102 xmlDoc* doc = xmlReadFile(file.c_str(), nullptr, 0);
103 if (doc == nullptr) {
104 HIVIEW_LOGE("failed to read rule file.");
105 return false;
106 }
107
108 xmlNode* root = xmlDocGetRootElement(doc);
109 if (root == nullptr) {
110 HIVIEW_LOGE("failed to get root element in rule file.");
111 xmlFreeDoc(doc);
112 doc = nullptr;
113 return false;
114 }
115
116 for (xmlNode* node = root; node; node = node->next) {
117 if (node->type != XML_ELEMENT_NODE) {
118 continue;
119 }
120 if (TAG_FREEZE == std::string((char*)(node->name))) {
121 ParseTagFreeze(node);
122 break;
123 }
124 }
125
126 xmlFreeDoc(doc);
127 doc = nullptr;
128 return true;
129 }
130
ParseTagFreeze(xmlNode * tag)131 void FreezeRuleCluster::ParseTagFreeze(xmlNode* tag)
132 {
133 for (xmlNode* node = tag->children; node; node = node->next) {
134 if (TAG_RULES == std::string((char*)(node->name))) {
135 ParseTagRules(node);
136 }
137 }
138 }
139
ParseTagRules(xmlNode * tag)140 void FreezeRuleCluster::ParseTagRules(xmlNode* tag)
141 {
142 for (xmlNode* node = tag->children; node; node = node->next) {
143 if (TAG_RULE == std::string((char*)(node->name))) {
144 ParseTagRule(node);
145 }
146 }
147 }
148
ParseTagRule(xmlNode * tag)149 void FreezeRuleCluster::ParseTagRule(xmlNode* tag)
150 {
151 std::string domain = GetAttributeValue<std::string>(tag, ATTRIBUTE_DOMAIN);
152 if (domain == "") {
153 HIVIEW_LOGE("null rule attribute:domain.");
154 return;
155 }
156 std::string stringId = GetAttributeValue<std::string>(tag, ATTRIBUTE_STRINGID);
157 if (stringId == "") {
158 HIVIEW_LOGE("null rule attribute:stringid.");
159 return;
160 }
161
162 FreezeRule rule = FreezeRule(domain, stringId);
163
164 for (xmlNode* node = tag->children; node; node = node->next) {
165 if (TAG_LINKS == std::string((char*)(node->name))) {
166 ParseTagLinks(node, rule);
167 }
168 }
169
170 if (rules_.find(domain + stringId) != rules_.end()) {
171 HIVIEW_LOGE("skip duplicated rule, stringid:%{public}s.", stringId.c_str());
172 return;
173 }
174
175 rules_[domain + stringId] = rule;
176 }
177
ParseTagLinks(xmlNode * tag,FreezeRule & rule)178 void FreezeRuleCluster::ParseTagLinks(xmlNode* tag, FreezeRule& rule)
179 {
180 for (xmlNode* node = tag->children; node; node = node->next) {
181 if (TAG_EVENT == std::string((char*)(node->name))) {
182 std::string domain = GetAttributeValue<std::string>(node, ATTRIBUTE_DOMAIN);
183 if (domain == "") {
184 HIVIEW_LOGE("null event attribute:domain.");
185 return;
186 }
187 std::string stringId = GetAttributeValue<std::string>(node, ATTRIBUTE_STRINGID);
188 if (stringId == "") {
189 HIVIEW_LOGE("null event attribute:stringid.");
190 return;
191 }
192
193 long window = GetAttributeValue<long>(node, ATTRIBUTE_WINDOW);
194
195 FreezeResult result = FreezeResult(window, domain, stringId);
196 ParseTagEvent(node, result);
197 rule.AddResult(domain, stringId, result);
198
199 bool principalPoint = false;
200 if (rule.GetDomain() == domain && rule.GetStringId() == stringId) {
201 principalPoint = true;
202 }
203 if (result.GetScope() == "app") {
204 applicationPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
205 } else if (result.GetScope() == "sys") {
206 systemPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
207 } else if (result.GetScope() == "sysWarning") {
208 sysWarningPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
209 }
210 }
211 }
212 }
213
ParseTagEvent(xmlNode * tag,FreezeResult & result)214 void FreezeRuleCluster::ParseTagEvent(xmlNode* tag, FreezeResult& result)
215 {
216 for (xmlNode* node = tag->children; node; node = node->next) {
217 if (TAG_RESULT == std::string((char*)(node->name))) {
218 ParseTagResult(node, result);
219 break;
220 }
221 }
222 }
223
ParseTagResult(xmlNode * tag,FreezeResult & result)224 void FreezeRuleCluster::ParseTagResult(xmlNode* tag, FreezeResult& result)
225 {
226 long delay = GetAttributeValue<long>(tag, ATTRIBUTE_DELAY);
227 unsigned long code = GetAttributeValue<unsigned long>(tag, ATTRIBUTE_CODE);
228 std::string scope = GetAttributeValue<std::string>(tag, ATTRIBUTE_SCOPE);
229 std::string samePackage = GetAttributeValue<std::string>(tag, ATTRIBUTE_SAME_PACKAGE);
230 std::string action = GetAttributeValue<std::string>(tag, ATTRIBUTE_ACTION);
231
232 result.SetDelay(delay);
233 result.SetId(code);
234 result.SetScope(scope);
235 result.SetSamePackage(samePackage);
236 result.SetAction(action);
237 }
238
239 template<typename T>
GetAttributeValue(xmlNode * node,const std::string & name)240 T FreezeRuleCluster::GetAttributeValue(xmlNode* node, const std::string& name)
241 {
242 xmlChar* prop = xmlGetProp(node, (xmlChar*)(name.c_str()));
243 std::string propa = "";
244 if (prop != nullptr) {
245 propa = (char*)prop;
246 }
247 std::istringstream istr(propa);
248 T value;
249 istr >> value;
250 xmlFree(prop);
251 return value;
252 }
253
GetResult(const WatchPoint & watchPoint,std::vector<FreezeResult> & list)254 bool FreezeRuleCluster::GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list)
255 {
256 std::string domain = watchPoint.GetDomain();
257 std::string stringId = watchPoint.GetStringId();
258 if (rules_.find(domain + stringId) == rules_.end()) {
259 return false;
260 }
261 auto map = rules_[domain + stringId].GetMap();
262 for (auto& i : map) {
263 list.push_back(i.second);
264 }
265
266 if (list.empty()) {
267 return false;
268 }
269
270 sort(list.begin(), list.end(), [] (const FreezeResult& frontResult, const FreezeResult& rearResult) {
271 return frontResult.GetWindow() < rearResult.GetWindow();
272 });
273 return true;
274 }
275
GetApplicationPairs() const276 std::map<std::string, std::pair<std::string, bool>> FreezeRuleCluster::GetApplicationPairs() const
277 {
278 return applicationPairs_;
279 }
280
GetSystemPairs() const281 std::map<std::string, std::pair<std::string, bool>> FreezeRuleCluster::GetSystemPairs() const
282 {
283 return systemPairs_;
284 }
285
GetSysWarningPairs() const286 std::map<std::string, std::pair<std::string, bool>> FreezeRuleCluster::GetSysWarningPairs() const
287 {
288 return sysWarningPairs_;
289 }
290
GetDomain() const291 std::string FreezeResult::GetDomain() const
292 {
293 return domain_;
294 }
295
GetStringId() const296 std::string FreezeResult::GetStringId() const
297 {
298 return stringId_;
299 }
300
GetId() const301 unsigned long FreezeResult::GetId() const
302 {
303 return code_;
304 }
305
SetId(unsigned long code)306 void FreezeResult::SetId(unsigned long code)
307 {
308 code_ = code;
309 }
310
GetScope() const311 std::string FreezeResult::GetScope() const
312 {
313 return scope_;
314 }
315
SetScope(const std::string & scope)316 void FreezeResult::SetScope(const std::string& scope)
317 {
318 scope_ = scope;
319 }
320
GetWindow() const321 long FreezeResult::GetWindow() const
322 {
323 return window_;
324 }
325
GetDelay() const326 long FreezeResult::GetDelay() const
327 {
328 return delay_;
329 }
330
SetDelay(long delay)331 void FreezeResult::SetDelay(long delay)
332 {
333 delay_ = delay;
334 }
335
GetSamePackage() const336 std::string FreezeResult::GetSamePackage() const
337 {
338 return samePackage_;
339 }
340
SetSamePackage(const std::string & samePackage)341 void FreezeResult::SetSamePackage(const std::string& samePackage)
342 {
343 samePackage_ = samePackage;
344 }
345
GetAction() const346 std::string FreezeResult::GetAction() const
347 {
348 return action_;
349 }
350
SetAction(const std::string & action)351 void FreezeResult::SetAction(const std::string& action)
352 {
353 action_ = action;
354 }
355
GetDomain() const356 std::string FreezeRule::GetDomain() const
357 {
358 return domain_;
359 }
360
SetDomain(const std::string & domain)361 void FreezeRule::SetDomain(const std::string& domain)
362 {
363 domain_ = domain;
364 }
365
GetStringId() const366 std::string FreezeRule::GetStringId() const
367 {
368 return stringId_;
369 }
370
SetStringId(const std::string & stringId)371 void FreezeRule::SetStringId(const std::string& stringId)
372 {
373 stringId_ = stringId;
374 }
375
GetMap() const376 std::map<std::string, FreezeResult> FreezeRule::GetMap() const
377 {
378 return results_;
379 }
380
AddResult(const std::string & domain,const std::string & stringId,const FreezeResult & result)381 void FreezeRule::AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result)
382 {
383 if (results_.find(domain + stringId) != results_.end()) {
384 HIVIEW_LOGE("skip duplicated event tag, stringid:%{public}s.", stringId.c_str());
385 return;
386 }
387
388 results_[domain + stringId] = result;
389 }
390
GetResult(const std::string & domain,const std::string & stringId,FreezeResult & result)391 bool FreezeRule::GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result)
392 {
393 if (results_.find(domain + stringId) == results_.end()) {
394 HIVIEW_LOGE("failed to find rule result, domain:%{public}s stringid:%{public}s.",
395 domain.c_str(), stringId.c_str());
396 return false;
397 }
398
399 result = results_[domain + stringId]; // take result back
400 return true;
401 }
402 } // namespace HiviewDFX
403 } // namespace OHOS
404