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