• 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 "securec.h"
24 #include "string_util.h"
25 #include "time_util.h"
26 
27 using namespace std;
28 namespace OHOS {
29 namespace HiviewDFX {
30 DEFINE_LOG_TAG("Tbox");
31 
Tbox()32 Tbox::Tbox()
33 {
34 }
35 
~Tbox()36 Tbox::~Tbox()
37 {
38 }
39 
CalcFingerPrint(const string & val,size_t mask,int mode)40 string Tbox::CalcFingerPrint(const string& val, size_t mask, int mode)
41 {
42     char hash[HAS_LEN] = {'0'};
43     int err = -1;
44     switch (mode) {
45         case FP_FILE:
46             err = CalcFingerprint::CalcFileSha(val, hash, sizeof(hash));
47             break;
48         case FP_BUFFER:
49             err = CalcFingerprint::CalcBufferSha(val, val.size(), hash, sizeof(hash));
50             break;
51         default:
52             break;
53     }
54 
55     if (err != 0) {
56         // if file not exist, API will return ENOENT
57         HIVIEW_LOGE("ProcessedEvent: calc fingerprint failed, err is %{public}d.", err);
58     }
59     return string(hash);
60 }
61 
GetPartial(const string & src,const string & res,string & des)62 bool Tbox::GetPartial(const string& src, const string& res, string& des)
63 {
64     des = "";
65     regex reNew(res);
66     smatch result;
67     if (regex_search(src, result, reNew)) {
68         for (size_t i = 1; i < result.size(); ++i) {
69             if (!result.str(i).empty()) {
70                 des = string(result.str(i));
71                 break;
72             }
73         }
74         return true;
75     }
76     return false;
77 }
78 
IsCallStack(const string & line)79 bool Tbox::IsCallStack(const string& line)
80 {
81     if (regex_search(line, regex("^\\s+at (.*)\\(.*")) ||
82         regex_search(line, regex("^\\s*at .*")) ||
83         regex_search(line, regex("^\\s+- (.*)\\(.*")) ||
84         regex_search(line, regex("\\s+#\\d+")) ||
85         regex_search(line, regex("[0-9a-zA-Z_]+\\+0x[0-9a-f]+/0x[0-9a-f]+")) ||
86         regex_search(line, regex("#\\d+")) ||
87         regex_search(line, regex("\\.*"))) {
88         return true;
89     }
90     return false;
91 }
92 
93 /*
94  * format1:  com.ohos.launcher:extension
95  * format2:  #06 pc 00000000000bb328  /system/lib/libart.so (__epoll_pwait+8)
96  */
GetStackName(const string & line)97 string Tbox::GetStackName(const string& line)
98 {
99     string stackname = UNKNOWN_STR;
100     string str;
101     if (GetPartial(line, "^\\s+at (.*)\\).*", str) ||
102         GetPartial(line, "^\\s*at (.*)", str) || // for jsCrash
103         GetPartial(line, "#\\d+ pc [0-9a-f]+ (.*\\+\\d+)\\)", str) ||
104         GetPartial(line, "#\\d+ pc [0-9a-f]+ (.*)", str) ||
105         GetPartial(line, "([0-9a-zA-Z_]+\\+0x[0-9a-f]+/0x[0-9a-f]+)", str)) {
106         stackname = str;
107     } else if (GetPartial(line, "^\\s+- (.*)\\(.*", str)) {
108         size_t ret = str.find_last_of("+");
109         if (ret != string::npos) {
110             str.replace(ret, string::npos, ")\0");
111             stackname = str;
112         } else {
113             stackname = UNKNOWN_STR;
114         }
115     } else if (IsCallStack(line)) {
116         stackname = line;
117     } else {
118         return stackname;
119     }
120     regex re("(.+?)-(.+)==(.+)");
121     stackname = regex_replace(stackname, re, "$1$3");
122     return stackname;
123 }
124 
FilterTrace(std::map<std::string,std::string> & eventInfo,string eventType)125 void Tbox::FilterTrace(std::map<std::string, std::string>& eventInfo, string eventType)
126 {
127     auto iterEndStack = eventInfo.find(PARAMETER_ENDSTACK);
128     if (eventInfo.empty() || iterEndStack == eventInfo.end() || iterEndStack->second.empty()) {
129         return;
130     }
131     std::vector<std::string> trace;
132     LogParse logparse;
133     std::string block = logparse.GetFilterTrace(iterEndStack->second, trace, eventType);
134     eventInfo[PARAMETER_ENDSTACK] = block;
135     std::stack<std::string> stackTop = logparse.GetStackTop(trace, 3);  // 3 : first/second/last frame
136     logparse.SetFrame(stackTop, eventInfo);
137 }
138 
WaitForDoneFile(const std::string & file,unsigned int timeout)139 bool Tbox::WaitForDoneFile(const std::string& file, unsigned int timeout)
140 {
141     uint64_t remainedTime = timeout * NS_PER_SECOND;
142     while (remainedTime > 0) {
143         if (FileUtil::FileExists(file)) {
144             HIVIEW_LOGD("Done file exist: %{public}s", file.c_str());
145             return true;
146         }
147         uint64_t startTime = TimeUtil::GetNanoTime();
148         sleep(1);
149         uint64_t duration = TimeUtil::GetNanoTime() - startTime;
150         remainedTime = (remainedTime > duration) ? (remainedTime - duration) : 0;
151     }
152     return false;
153 }
154 
GetHappenTime(const string & src,const string & regex)155 int64_t Tbox::GetHappenTime(const string& src, const string& regex)
156 {
157     int64_t happenTime = HAPPEN_TIME_DEFAULT;
158     std::regex recordRegex(regex);
159     struct tm tmHappenTime;
160     (void)memset_s(&tmHappenTime, sizeof(struct tm), 0, sizeof(struct tm));
161     std::smatch matches;
162     if (!std::regex_match(src, matches, recordRegex)) {
163         HIVIEW_LOGE("unmatch event:%{public}s, skip", src.c_str());
164         return happenTime;
165     }
166 
167     string timeStr;
168     for (size_t i = 1; i < matches.size(); ++i) {
169         timeStr += matches[i].str();
170     }
171     strptime(timeStr.c_str(), "%Y%m%d%H%M%S", &tmHappenTime);
172     happenTime = mktime(&tmHappenTime);
173     //if the HappenTime is 1970, return as original information
174     if (tmHappenTime.tm_year == 70) { // 70: The number of years since the HappenTime in 1900
175         return happenTime;
176     }
177 
178     time_t now = time(nullptr);
179     struct tm result;
180     gmtime_r(&now, &result);
181     double offset = difftime(now, mktime(&result)); // zone offset with second. Example, 1 zone is 3600 sec
182     if (difftime(now, happenTime) > offset) {
183         happenTime = timegm(&tmHappenTime);
184     }
185     return happenTime;
186 }
187 }
188 }
189