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 "bbox_detector_plugin.h"
16
17 #include <fstream>
18 #include <securec.h>
19
20 #include "event.h"
21 #include "file_util.h"
22 #include "common_defines.h"
23 #include "logger.h"
24 #include "plugin_factory.h"
25 #include "sys_event_dao.h"
26 #include "smart_parser.h"
27 #include "string_util.h"
28 #include "tbox.h"
29 #include "time_util.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 using namespace std;
34 REGISTER(BBoxDetectorPlugin)
35 DEFINE_LOG_TAG("BBoxDetectorPlugin");
36
37 static std::vector<std::string> EVENT_LIST = { "PANIC", "HWWATCHDOG", "LPM3EXCEPTION", "BOOTLOADER_CRASH", "BOOTFAIL",
38 "TRUSTZONE_REBOOTSYS", "CONNEXCEPTION", "SENSORHUBCRASH", "HIFICRASH",
39 "HARDWARE_FAULT", "MODEMCRASH", "AUDIO_CODEC_CRASH", "TRUSTZONECRASH",
40 "ISPCRASH", "IVPCRASH", "PRESS10S", "GENERAL_SEE_CRASH", "DSSCRASH",
41 "UNKNOWNS", "NPUEXCEPTION", "MODEM_REBOOTSYS", "FDULCRASH", "PRESS6S"};
42
OnLoad()43 void BBoxDetectorPlugin::OnLoad()
44 {
45 SetName("BBoxDetectorPlugin");
46 SetVersion("BBoxDetector1.0");
47 }
48
OnUnload()49 void BBoxDetectorPlugin::OnUnload()
50 {
51 HIVIEW_LOGI("BBoxDetectorPlugin OnUnload");
52 }
53
OnEvent(std::shared_ptr<Event> & event)54 bool BBoxDetectorPlugin::OnEvent(std::shared_ptr<Event> &event)
55 {
56 if (!IsInterestedPipelineEvent(event)) {
57 return false;
58 }
59 auto sysEvent = Event::DownCastTo<SysEvent>(event);
60 HandleBBoxEvent(sysEvent);
61 return true;
62 }
63
IsInterestedPipelineEvent(std::shared_ptr<Event> event)64 bool BBoxDetectorPlugin::IsInterestedPipelineEvent(std::shared_ptr<Event> event)
65 {
66 if (event == nullptr || event->domain_ != "KERNEL_VENDOR") {
67 return false;
68 }
69
70 auto sysEvent = Event::DownCastTo<SysEvent>(event);
71 auto subEventType = sysEvent->GetEventValue("name_");
72 vector<std::string>::iterator it = find(EVENT_LIST.begin(), EVENT_LIST.end(), subEventType);
73 if (it == EVENT_LIST.end()) {
74 HIVIEW_LOGI("subsystem event %{public}s is ignored", subEventType.c_str());
75 return false;
76 }
77
78 return true;
79 }
80
WaitForLogs(const std::string & logDir)81 void BBoxDetectorPlugin::WaitForLogs(const std::string& logDir)
82 {
83 std::string doneFile = logDir + "/DONE";
84 if (!Tbox::WaitForDoneFile(doneFile, 60)) { // 60s
85 HIVIEW_LOGE("can not find file: %{public}s", doneFile.c_str());
86 }
87 }
88
HandleBBoxEvent(std::shared_ptr<SysEvent> & sysEvent)89 void BBoxDetectorPlugin::HandleBBoxEvent(std::shared_ptr<SysEvent> &sysEvent)
90 {
91 std::string event = sysEvent->GetEventValue("REASON");
92 std::string module = sysEvent->GetEventValue("MODULE");
93 std::string timeStr = sysEvent->GetEventValue("SUB_LOG_PATH");
94 std::string LOG_PATH = sysEvent->GetEventValue("LOG_PATH");
95 std::string dynamicPaths = ((!LOG_PATH.empty() && LOG_PATH[LOG_PATH.size() - 1] == '/') ?
96 LOG_PATH : LOG_PATH + '/') + timeStr;
97 auto times = static_cast<int64_t>(TimeUtil::StrToTimeStamp(StringUtil::GetRleftSubstr(timeStr, "-"),
98 "%Y%m%d%H%M%S"));
99 sysEvent->SetEventValue("HAPPEN_TIME", times);
100
101 WaitForLogs(dynamicPaths);
102 auto eventInfos = SmartParser::Analysis(dynamicPaths, logParseConfig_, sysEvent->GetEventValue("name_"));
103 Tbox::FilterTrace(eventInfos);
104
105 sysEvent->SetEventValue("FIRST_FRAME", eventInfos["FIRST_FRAME"].empty() ? "/" :
106 StringUtil::EscapeJsonStringValue(eventInfos["FIRST_FRAME"]));
107 sysEvent->SetEventValue("SECOND_FRAME", eventInfos["SECOND_FRAME"].empty() ? "/" :
108 StringUtil::EscapeJsonStringValue(eventInfos["SECOND_FRAME"]));
109 sysEvent->SetEventValue("LAST_FRAME", eventInfos["LAST_FRAME"].empty() ? "/ " :
110 StringUtil::EscapeJsonStringValue(eventInfos["LAST_FRAME"]));
111 sysEvent->SetEventValue("FINGERPRINT", Tbox::CalcFingerPrint(event + module + eventInfos["FIRST_FRAME"] +
112 eventInfos["SECOND_FRAME"] + eventInfos["LAST_FRAME"], 0, FP_BUFFER));
113 sysEvent->SetEventValue("LOG_PATH", dynamicPaths);
114 if (sysEvent->GetSeq() != 0 && EventStore::SysEventDao::Update(sysEvent, false) != 0) {
115 HIVIEW_LOGE("update failed, event: %{public}s", sysEvent->eventName_.c_str());
116 }
117 }
118 }
119 }
120