• 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 "rule_cluster.h"
17 
18 #include <sstream>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 
22 #include "logger.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("FreezeDetector");
27 
FreezeRuleCluster()28 FreezeRuleCluster::FreezeRuleCluster()
29 {
30     rules_.clear();
31 }
32 
~FreezeRuleCluster()33 FreezeRuleCluster::~FreezeRuleCluster()
34 {
35     rules_.clear();
36 }
37 
Init()38 bool FreezeRuleCluster::Init()
39 {
40     if (access(DEFAULT_RULE_FILE.c_str(), R_OK) != 0) {
41         HIVIEW_LOGE("cannot access rule file.");
42         return false;
43     }
44 
45     if (CheckFileSize(DEFAULT_RULE_FILE) == false) {
46         HIVIEW_LOGE("bad rule file size.");
47         return false;
48     }
49 
50     if (ParseRuleFile(DEFAULT_RULE_FILE) == false) {
51         HIVIEW_LOGE("failed to parse rule file.");
52         return false;
53     }
54 
55     if (rules_.size() == 0) {
56         HIVIEW_LOGE("no rule in rule file.");
57         return false;
58     }
59 
60     return true;
61 }
62 
CheckFileSize(const std::string & path)63 bool FreezeRuleCluster::CheckFileSize(const std::string& path)
64 {
65     struct stat st;
66     if (stat(path.c_str(), &st) != 0) {
67         return false;
68     }
69     if (st.st_size > MAX_FILE_SIZE) {
70         return false;
71     }
72     return true;
73 }
74 
ParseRuleFile(const std::string & file)75 bool FreezeRuleCluster::ParseRuleFile(const std::string& file)
76 {
77     xmlDoc* doc = xmlReadFile(file.c_str(), nullptr, 0);
78     if (doc == nullptr) {
79         HIVIEW_LOGE("failed to read rule file.");
80         return false;
81     }
82 
83     xmlNode* root = xmlDocGetRootElement(doc);
84     if (root == nullptr) {
85         HIVIEW_LOGE("failed to get root element in rule file.");
86         xmlFreeDoc(doc);
87         doc = nullptr;
88         return false;
89     }
90 
91     for (xmlNode* node = root; node; node = node->next) {
92         if (node->type != XML_ELEMENT_NODE) {
93             continue;
94         }
95         if (TAG_FREEZE == std::string((char*)(node->name))) {
96             ParseTagFreeze(node);
97             break;
98         }
99     }
100 
101     xmlFreeDoc(doc);
102     doc = nullptr;
103     return true;
104 }
105 
ParseTagFreeze(xmlNode * tag)106 void FreezeRuleCluster::ParseTagFreeze(xmlNode* tag)
107 {
108     for (xmlNode* node = tag->children; node; node = node->next) {
109         if (TAG_RULES == std::string((char*)(node->name))) {
110             ParseTagRules(node);
111         }
112     }
113 }
114 
ParseTagRules(xmlNode * tag)115 void FreezeRuleCluster::ParseTagRules(xmlNode* tag)
116 {
117     for (xmlNode* node = tag->children; node; node = node->next) {
118         if (TAG_RULE == std::string((char*)(node->name))) {
119             ParseTagRule(node);
120         }
121     }
122 }
123 
ParseTagRule(xmlNode * tag)124 void FreezeRuleCluster::ParseTagRule(xmlNode* tag)
125 {
126     std::string domain = GetAttributeValue<std::string>(tag, ATTRIBUTE_DOMAIN);
127     if (domain == "") {
128         HIVIEW_LOGE("null rule attribute:domain.");
129         return;
130     }
131     std::string stringId = GetAttributeValue<std::string>(tag, ATTRIBUTE_STRINGID);
132     if (stringId == "") {
133         HIVIEW_LOGE("null rule attribute:stringid.");
134         return;
135     }
136 
137     FreezeRule rule = FreezeRule(domain, stringId);
138 
139     for (xmlNode* node = tag->children; node; node = node->next) {
140         if (TAG_LINKS == std::string((char*)(node->name))) {
141             ParseTagLinks(node, rule);
142         }
143     }
144 
145     if (rules_.find(domain + stringId) != rules_.end()) {
146         HIVIEW_LOGE("skip duplicated rule, stringid:%{public}s.", stringId.c_str());
147         return;
148     }
149 
150     rules_[domain + stringId] = rule;
151 }
152 
ParseTagLinks(xmlNode * tag,FreezeRule & rule)153 void FreezeRuleCluster::ParseTagLinks(xmlNode* tag, FreezeRule& rule)
154 {
155     for (xmlNode* node = tag->children; node; node = node->next) {
156         if (TAG_EVENT == std::string((char*)(node->name))) {
157             std::string domain = GetAttributeValue<std::string>(node, ATTRIBUTE_DOMAIN);
158             if (domain == "") {
159                 HIVIEW_LOGE("null event attribute:domain.");
160                 return;
161             }
162             std::string stringId = GetAttributeValue<std::string>(node, ATTRIBUTE_STRINGID);
163             if (stringId == "") {
164                 HIVIEW_LOGE("null event attribute:stringid.");
165                 return;
166             }
167 
168             long window = GetAttributeValue<long>(node, ATTRIBUTE_WINDOW);
169 
170             FreezeResult result = FreezeResult(window, domain, stringId);
171             ParseTagEvent(node, result);
172             rule.AddResult(domain, stringId, result);
173 
174             bool principalPoint = false;
175             if (rule.GetDomain() == domain && rule.GetStringId() == stringId) {
176                 principalPoint = true;
177             }
178             if (result.GetScope() == "app") {
179                 applicationPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
180             } else {
181                 systemPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
182             }
183         }
184     }
185 }
186 
ParseTagEvent(xmlNode * tag,FreezeResult & result)187 void FreezeRuleCluster::ParseTagEvent(xmlNode* tag, FreezeResult& result)
188 {
189     for (xmlNode* node = tag->children; node; node = node->next) {
190         if (TAG_RESULT == std::string((char*)(node->name))) {
191             ParseTagResult(node, result);
192             break;
193         }
194     }
195 }
196 
ParseTagResult(xmlNode * tag,FreezeResult & result)197 void FreezeRuleCluster::ParseTagResult(xmlNode* tag, FreezeResult& result)
198 {
199     unsigned long code = GetAttributeValue<unsigned long>(tag, ATTRIBUTE_CODE);
200     std::string scope = GetAttributeValue<std::string>(tag, ATTRIBUTE_SCOPE);
201     std::string samePackage = GetAttributeValue<std::string>(tag, ATTRIBUTE_SAME_PACKAGE);
202 
203     result.SetId(code);
204     result.SetScope(scope);
205     result.SetSamePackage(samePackage);
206 }
207 
208 template<typename T>
GetAttributeValue(xmlNode * node,const std::string & name)209 T FreezeRuleCluster::GetAttributeValue(xmlNode* node, const std::string& name)
210 {
211     xmlChar* prop = xmlGetProp(node, (xmlChar*)(name.c_str()));
212     std::string propa = "";
213     if (prop != nullptr) {
214         propa = (char*)prop;
215     }
216     std::istringstream istr(propa);
217     T value;
218     istr >> value;
219     xmlFree(prop);
220     return value;
221 }
222 
GetResult(const WatchPoint & watchPoint,std::vector<FreezeResult> & list)223 bool FreezeRuleCluster::GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list)
224 {
225     std::string domain = watchPoint.GetDomain();
226     std::string stringId = watchPoint.GetStringId();
227     if (rules_.find(domain + stringId) == rules_.end()) {
228         return false;
229     }
230     auto map = rules_[domain + stringId].GetMap();
231     for (auto& i : map) {
232         list.push_back(i.second);
233     }
234 
235     if (list.empty()) {
236         return false;
237     }
238     return true;
239 }
240 
AddResult(const std::string & domain,const std::string & stringId,const FreezeResult & result)241 void FreezeRule::AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result)
242 {
243     if (results_.find(domain + stringId) != results_.end()) {
244         HIVIEW_LOGE("skip duplicated event tag, stringid:%{public}s.", stringId.c_str());
245         return;
246     }
247 
248     results_[domain + stringId] = result;
249 }
250 
GetResult(const std::string & domain,const std::string & stringId,FreezeResult & result)251 bool FreezeRule::GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result)
252 {
253     if (results_.find(domain + stringId) == results_.end()) {
254         HIVIEW_LOGE("failed to find rule result, domain:%{public}s stringid:%{public}s.",
255             domain.c_str(), stringId.c_str());
256         return false;
257     }
258 
259     result = results_[domain + stringId]; // take result back
260     return true;
261 }
262 } // namespace HiviewDFX
263 } // namespace OHOS
264