1 /*
2 * Copyright (c) 2022-2023 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 "log_analyzer.h"
16
17 #include <securec.h>
18
19 #include "common_utils.h"
20 #include "constants.h"
21 #include "faultlog_util.h"
22 #include "file_util.h"
23 #include "smart_parser.h"
24 #include "string_util.h"
25 #include "tbox.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
GetFingerRawString(std::string & fingerRawString,const FaultLogInfo & info,std::map<std::string,std::string> & eventInfos)29 static void GetFingerRawString(std::string& fingerRawString, const FaultLogInfo& info,
30 std::map<std::string, std::string>& eventInfos)
31 {
32 if ((info.reason.compare("APP_HICOLLIE") == 0 || info.reason.compare("SERVICE_TIMEOUT_WARNING") == 0 ||
33 info.reason.compare("SERVICE_TIMEOUT") == 0) && !eventInfos["TIME_OUT"].empty()) {
34 eventInfos["LAST_FRAME"] = eventInfos["TIME_OUT"];
35 }
36
37 if (info.reason.compare("SERVICE_BLOCK") == 0 && !eventInfos["QUEUE_NAME"].empty()) {
38 eventInfos["LAST_FRAME"] = eventInfos["QUEUE_NAME"];
39 }
40
41 auto eventType = GetFaultNameByType(info.faultLogType, false);
42 fingerRawString = info.module + StringUtil::GetLeftSubstr(info.reason, "@") +
43 eventInfos["FIRST_FRAME"] + eventInfos["SECOND_FRAME"] + eventInfos["LAST_FRAME"] +
44 ((eventType == "JS_ERROR") ? eventInfos["SUBREASON"] : "");
45 }
46
AnalysisFaultlog(const FaultLogInfo & info,std::map<std::string,std::string> & eventInfos)47 bool AnalysisFaultlog(const FaultLogInfo& info, std::map<std::string, std::string>& eventInfos)
48 {
49 bool needDelete = false;
50 std::string logPath = info.logPath;
51 auto eventType = GetFaultNameByType(info.faultLogType, false);
52 if ((eventType == "JS_ERROR" || eventType == "CPP_CRASH") && !info.summary.empty()) {
53 logPath = std::string(FaultLogger::FAULTLOG_BASE_FOLDER) + eventType + std::to_string(info.time);
54 FileUtil::SaveStringToFile(logPath, info.summary);
55 needDelete = true;
56 }
57
58 eventInfos = SmartParser::Analysis(logPath, SMART_PARSER_PATH, eventType);
59 if (needDelete) {
60 FileUtil::RemoveFile(logPath);
61 }
62 if (eventInfos.empty()) {
63 eventInfos.insert(std::make_pair("FINGERPRINT", Tbox::CalcFingerPrint(info.module + info.reason +
64 info.summary, 0, FP_BUFFER)));
65 return false;
66 }
67 Tbox::FilterTrace(eventInfos, eventType);
68 std::string fingerRawString;
69 GetFingerRawString(fingerRawString, info, eventInfos);
70 eventInfos["FINGERPRINT"] = Tbox::CalcFingerPrint(fingerRawString, 0, FP_BUFFER);
71
72 if (eventType == "APP_FREEZE" && eventInfos["LAST_FRAME"].empty()) {
73 if (!eventInfos["TRACER_PID"].empty()) {
74 int32_t pid = 0;
75 if (sscanf_s(eventInfos["TRACER_PID"].c_str(), "%d", &pid) == 1 && pid > 0) {
76 eventInfos["LAST_FRAME"] += ("(Tracer Process Name:" + CommonUtils::GetProcNameByPid(pid) + ")");
77 }
78 }
79 if (!eventInfos["DUMPCATCH_RESULT"].empty()) {
80 eventInfos["LAST_FRAME"] += ("(" + eventInfos["DUMPCATCH_RESULT"] + ")");
81 }
82 }
83 return true;
84 }
85 } // namespace HiviewDFX
86 } // namespace OHOS
87