• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "tbox.h"
16 
17 #include <unistd.h>
18 
19 #include <regex>
20 #include "calc_fingerprint.h"
21 #include "file_util.h"
22 #include "log_parse.h"
23 #include "string_util.h"
24 #include "time_util.h"
25 
26 using namespace std;
27 namespace OHOS {
28 namespace HiviewDFX {
29 DEFINE_LOG_TAG("Tbox");
30 
31 const string Tbox::ARRAY_STR = "ARRAY :";
32 const string Tbox::CAUSEDBY_HEADER = "Caused by:";
33 const string Tbox::SUPPRESSED_HEADER = "Suppressed:";
34 
Tbox()35 Tbox::Tbox()
36 {
37 }
38 
~Tbox()39 Tbox::~Tbox()
40 {
41 }
42 
CalcFingerPrint(const string & val,size_t mask,int mode)43 string Tbox::CalcFingerPrint(const string& val, size_t mask, int mode)
44 {
45     char hash[HAS_LEN] = {'0'};
46     int err = -1;
47     switch (mode) {
48         case FP_FILE:
49             err = CalcFingerprint::CalcFileSha(val, hash, sizeof(hash));
50             break;
51         case FP_BUFFER:
52             err = CalcFingerprint::CalcBufferSha(val, val.size(), hash, sizeof(hash));
53             break;
54         default:
55             break;
56     }
57 
58     if (err != 0) {
59         // if file not exist, API will return ENOENT
60         HIVIEW_LOGE("ProcessedEvent: calc fingerprint failed, err is %{public}d.", err);
61     }
62     return string(hash);
63 }
64 
GetPartial(const string & src,const string & res,string & des)65 bool Tbox::GetPartial(const string& src, const string& res, string& des)
66 {
67     des = "";
68     regex reNew(res);
69     smatch result;
70     if (regex_search(src, result, reNew)) {
71         for (size_t i = 1; i < result.size(); ++i) {
72             if (!result.str(i).empty()) {
73                 des = string(result.str(i));
74                 break;
75             }
76         }
77         return true;
78     }
79     return false;
80 }
81 
IsCallStack(string & line)82 bool Tbox::IsCallStack(string& line)
83 {
84     if (regex_search(line, regex("^\\s+at (.*)\\(.*")) ||
85         regex_search(line, regex("^\\s*at .*")) ||
86         regex_search(line, regex("^\\s+- (.*)\\(.*")) ||
87         regex_search(line, regex("\\s+#\\d+")) ||
88         regex_search(line, regex("[0-9a-zA-Z_]+\\+0x[0-9a-f]+/0x[0-9a-f]+")) ||
89         regex_search(line, regex("#\\d+")) ||
90         regex_search(line, regex("\\.*"))) {
91         return true;
92     }
93     return false;
94 }
95 
HasCausedBy(const string & line)96 bool Tbox::HasCausedBy(const string& line)
97 {
98     if ((line.find(CAUSEDBY_HEADER) != string::npos) ||
99         (line.find(SUPPRESSED_HEADER) != string::npos)) {
100         return true;
101     }
102     return false;
103 }
104 
105 /*
106  * format1:  com.ohos.launcher:extension
107  * format2:  #06 pc 00000000000bb328  /system/lib/libart.so (__epoll_pwait+8)
108  */
GetStackName(string line)109 string Tbox::GetStackName(string line)
110 {
111     string stackname = UNKNOWN_STR;
112     if (IsCallStack(line)) {
113         stackname = line;
114         string str;
115         if (GetPartial(line, "^\\s+at (.*)\\).*", str) ||
116             GetPartial(line, "^\\s*at (.*)", str) || // for jsCrash
117             GetPartial(line, "#\\d+ pc [0-9a-f]+ (.*\\+\\d+)\\)", str) ||
118             GetPartial(line, "#\\d+ pc [0-9a-f]+ (.*)", str) ||
119             GetPartial(line, "([0-9a-zA-Z_]+\\+0x[0-9a-f]+/0x[0-9a-f]+)", str)) {
120             stackname = str;
121         } else if (GetPartial(line, "^\\s+- (.*)\\(.*", str)) {
122             size_t ret = str.find_last_of("+");
123             if (ret != string::npos) {
124                 str.replace(ret, string::npos, ")\0");
125                 stackname = str;
126             } else {
127                 stackname = UNKNOWN_STR;
128             }
129         }
130         regex re("(.+?)-(.+)==(.+)");
131         stackname = regex_replace(stackname, re, "$1$3");
132     }
133     return stackname;
134 }
135 
FilterTrace(std::map<std::string,std::string> & eventInfo)136 void Tbox::FilterTrace(std::map<std::string, std::string>& eventInfo)
137 {
138     auto iterEndStack = eventInfo.find(PARAMETER_ENDSTACK);
139     if (eventInfo.empty() || iterEndStack == eventInfo.end() || iterEndStack->second.empty()) {
140         return;
141     }
142     std::vector<std::string> trace;
143     LogParse logparse;
144     std::string block = logparse.GetFilterTrace(iterEndStack->second, trace);
145     eventInfo[PARAMETER_ENDSTACK] = block;
146     eventInfo["FINGERPRINT"] = Tbox::CalcFingerPrint(block, 0, FP_BUFFER);
147     std::stack<std::string> stackTop = logparse.GetStackTop(trace, 3);  // 3 : first/second/last frame
148     logparse.SetFrame(stackTop, eventInfo);
149 }
150 
WaitForDoneFile(const std::string & file,unsigned int timeout)151 bool Tbox::WaitForDoneFile(const std::string& file, unsigned int timeout)
152 {
153     uint64_t remainedTime = timeout * NS_PER_SECOND;
154     while (remainedTime > 0) {
155         if (FileUtil::FileExists(file)) {
156             HIVIEW_LOGD("Done file exist: %{public}s", file.c_str());
157             return true;
158         }
159         uint64_t startTime = TimeUtil::GetNanoTime();
160         sleep(1);
161         uint64_t duration = TimeUtil::GetNanoTime() - startTime;
162         remainedTime = (remainedTime > duration) ? (remainedTime - duration) : 0;
163     }
164     return false;
165 }
166 }
167 }
168