1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci#include "log_analyzer.h"
16020a203aSopenharmony_ci
17020a203aSopenharmony_ci#include <securec.h>
18020a203aSopenharmony_ci
19020a203aSopenharmony_ci#include "common_utils.h"
20020a203aSopenharmony_ci#include "constants.h"
21020a203aSopenharmony_ci#include "faultlog_util.h"
22020a203aSopenharmony_ci#include "file_util.h"
23020a203aSopenharmony_ci#include "smart_parser.h"
24020a203aSopenharmony_ci#include "string_util.h"
25020a203aSopenharmony_ci#include "tbox.h"
26020a203aSopenharmony_ci
27020a203aSopenharmony_cinamespace OHOS {
28020a203aSopenharmony_cinamespace HiviewDFX {
29020a203aSopenharmony_cistatic void GetFingerRawString(std::string& fingerRawString, const FaultLogInfo& info,
30020a203aSopenharmony_ci                               std::map<std::string, std::string>& eventInfos)
31020a203aSopenharmony_ci{
32020a203aSopenharmony_ci    if ((info.reason.compare("APP_HICOLLIE") == 0 || info.reason.compare("SERVICE_TIMEOUT_WARNING") == 0 ||
33020a203aSopenharmony_ci        info.reason.compare("SERVICE_TIMEOUT") == 0) && !eventInfos["TIME_OUT"].empty()) {
34020a203aSopenharmony_ci        eventInfos["LAST_FRAME"] = eventInfos["TIME_OUT"];
35020a203aSopenharmony_ci    }
36020a203aSopenharmony_ci
37020a203aSopenharmony_ci    if (info.reason.compare("SERVICE_BLOCK") == 0 && !eventInfos["QUEUE_NAME"].empty()) {
38020a203aSopenharmony_ci        eventInfos["LAST_FRAME"] = eventInfos["QUEUE_NAME"];
39020a203aSopenharmony_ci    }
40020a203aSopenharmony_ci
41020a203aSopenharmony_ci    auto eventType = GetFaultNameByType(info.faultLogType, false);
42020a203aSopenharmony_ci    fingerRawString = info.module + StringUtil::GetLeftSubstr(info.reason, "@") +
43020a203aSopenharmony_ci        eventInfos["FIRST_FRAME"] + eventInfos["SECOND_FRAME"] + eventInfos["LAST_FRAME"] +
44020a203aSopenharmony_ci        ((eventType == "JS_ERROR") ? eventInfos["SUBREASON"] : "");
45020a203aSopenharmony_ci}
46020a203aSopenharmony_ci
47020a203aSopenharmony_cibool AnalysisFaultlog(const FaultLogInfo& info, std::map<std::string, std::string>& eventInfos)
48020a203aSopenharmony_ci{
49020a203aSopenharmony_ci    bool needDelete = false;
50020a203aSopenharmony_ci    std::string logPath = info.logPath;
51020a203aSopenharmony_ci    auto eventType = GetFaultNameByType(info.faultLogType, false);
52020a203aSopenharmony_ci    if ((eventType == "JS_ERROR" || eventType == "CPP_CRASH") && !info.summary.empty()) {
53020a203aSopenharmony_ci        logPath = std::string(FaultLogger::FAULTLOG_BASE_FOLDER) + eventType + std::to_string(info.time);
54020a203aSopenharmony_ci        FileUtil::SaveStringToFile(logPath, info.summary);
55020a203aSopenharmony_ci        needDelete = true;
56020a203aSopenharmony_ci    }
57020a203aSopenharmony_ci
58020a203aSopenharmony_ci    eventInfos = SmartParser::Analysis(logPath, SMART_PARSER_PATH, eventType);
59020a203aSopenharmony_ci    if (needDelete) {
60020a203aSopenharmony_ci        FileUtil::RemoveFile(logPath);
61020a203aSopenharmony_ci    }
62020a203aSopenharmony_ci    if (eventInfos.empty()) {
63020a203aSopenharmony_ci        eventInfos.insert(std::make_pair("fingerPrint", Tbox::CalcFingerPrint(info.module + info.reason +
64020a203aSopenharmony_ci            info.summary, 0, FP_BUFFER)));
65020a203aSopenharmony_ci        return false;
66020a203aSopenharmony_ci    }
67020a203aSopenharmony_ci    Tbox::FilterTrace(eventInfos, eventType);
68020a203aSopenharmony_ci    std::string fingerRawString;
69020a203aSopenharmony_ci    GetFingerRawString(fingerRawString, info, eventInfos);
70020a203aSopenharmony_ci    eventInfos["fingerPrint"] = Tbox::CalcFingerPrint(fingerRawString, 0, FP_BUFFER);
71020a203aSopenharmony_ci
72020a203aSopenharmony_ci    if (eventType == "APP_FREEZE" && eventInfos["LAST_FRAME"].empty()) {
73020a203aSopenharmony_ci        if (!eventInfos["TRACER_PID"].empty()) {
74020a203aSopenharmony_ci            int32_t pid = 0;
75020a203aSopenharmony_ci            if (sscanf_s(eventInfos["TRACER_PID"].c_str(), "%d", &pid) == 1 && pid > 0) {
76020a203aSopenharmony_ci                eventInfos["LAST_FRAME"] += ("(Tracer Process Name:" + CommonUtils::GetProcNameByPid(pid) + ")");
77020a203aSopenharmony_ci            }
78020a203aSopenharmony_ci        }
79020a203aSopenharmony_ci        if (!eventInfos["DUMPCATCH_RESULT"].empty()) {
80020a203aSopenharmony_ci            eventInfos["LAST_FRAME"] += ("(" + eventInfos["DUMPCATCH_RESULT"] + ")");
81020a203aSopenharmony_ci        }
82020a203aSopenharmony_ci    }
83020a203aSopenharmony_ci    return true;
84020a203aSopenharmony_ci}
85020a203aSopenharmony_ci} // namespace HiviewDFX
86020a203aSopenharmony_ci} // namespace OHOS
87