• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[FaultKey::LAST_FRAME] = eventInfos["TIME_OUT"];
35     }
36 
37     if (info.reason.compare("SERVICE_BLOCK") == 0 && !eventInfos["QUEUE_NAME"].empty()) {
38         eventInfos[FaultKey::LAST_FRAME] = eventInfos["QUEUE_NAME"];
39     }
40 
41     auto eventType = GetFaultNameByType(info.faultLogType, false);
42     fingerRawString = info.module + StringUtil::GetLeftSubstr(info.reason, "@") +
43         eventInfos[FaultKey::FIRST_FRAME] + eventInfos[FaultKey::SECOND_FRAME] + eventInfos[FaultKey::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 == "CJ_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     std::string binderStack = "";
58     if (eventInfos.count(TERMINAL_THREAD_STACK) > 0 && !eventInfos[TERMINAL_THREAD_STACK].empty()) {
59         binderStack = eventInfos[TERMINAL_THREAD_STACK];
60     }
61 
62     eventInfos = SmartParser::Analysis(logPath, SMART_PARSER_PATH, eventType);
63     if (needDelete) {
64         FileUtil::RemoveFile(logPath);
65     }
66     if (eventInfos.empty()) {
67         eventInfos.insert(std::make_pair(FaultKey::FINGERPRINT, Tbox::CalcFingerPrint(info.module + info.reason +
68             info.summary, 0, FP_BUFFER)));
69         return false;
70     }
71     if (!binderStack.empty()) {
72         // Update ENDSTACK, ENDSTACK is used in Tbox::FilterTrace to extract F1/F2/F3
73         eventInfos[PARAMETER_ENDSTACK] = binderStack;
74     }
75     Tbox::FilterTrace(eventInfos, eventType);
76     std::string fingerRawString;
77     GetFingerRawString(fingerRawString, info, eventInfos);
78     eventInfos[FaultKey::FINGERPRINT] = Tbox::CalcFingerPrint(fingerRawString, 0, FP_BUFFER);
79 
80     if ((eventType == "APP_FREEZE" || eventType == "SYS_FREEZE") && eventInfos[FaultKey::FIRST_FRAME].empty()) {
81         if (!eventInfos["TRACER_PID"].empty()) {
82             int32_t pid = 0;
83             if (sscanf_s(eventInfos["TRACER_PID"].c_str(), "%d", &pid) == 1 && pid > 0) {
84                 eventInfos[FaultKey::LAST_FRAME] +=
85                     ("(Tracer Process Name:" + CommonUtils::GetProcNameByPid(pid) + ")");
86             }
87         }
88         if (!eventInfos["NORMAL_STACK_REASON"].empty()) {
89             eventInfos[FaultKey::LAST_FRAME] += ("(" + eventInfos["NORMAL_STACK_REASON"] + ")");
90         }
91     }
92     return true;
93 }
94 } // namespace HiviewDFX
95 } // namespace OHOS
96