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
OnLoad()37 void BBoxDetectorPlugin::OnLoad()
38 {
39 SetName("BBoxDetectorPlugin");
40 SetVersion("BBoxDetector1.0");
41 }
42
OnUnload()43 void BBoxDetectorPlugin::OnUnload()
44 {
45 HIVIEW_LOGI("BBoxDetectorPlugin OnUnload");
46 }
47
OnEvent(std::shared_ptr<Event> & event)48 bool BBoxDetectorPlugin::OnEvent(std::shared_ptr<Event> &event)
49 {
50 if (event == nullptr || event->domain_ != "KERNEL_VENDOR") {
51 return false;
52 }
53 auto sysEvent = Event::DownCastTo<SysEvent>(event);
54 HandleBBoxEvent(sysEvent);
55 return true;
56 }
57
WaitForLogs(const std::string & logDir)58 void BBoxDetectorPlugin::WaitForLogs(const std::string& logDir)
59 {
60 std::string doneFile = logDir + "/DONE";
61 if (!Tbox::WaitForDoneFile(doneFile, 60)) { // 60s
62 HIVIEW_LOGE("can not find file: %{public}s", doneFile.c_str());
63 }
64 }
65
HandleBBoxEvent(std::shared_ptr<SysEvent> & sysEvent)66 void BBoxDetectorPlugin::HandleBBoxEvent(std::shared_ptr<SysEvent> &sysEvent)
67 {
68 std::string event = sysEvent->GetEventValue("REASON");
69 std::string module = sysEvent->GetEventValue("MODULE");
70 std::string timeStr = sysEvent->GetEventValue("SUB_LOG_PATH");
71 std::string LOG_PATH = sysEvent->GetEventValue("LOG_PATH");
72 std::string dynamicPaths = ((!LOG_PATH.empty() && LOG_PATH[LOG_PATH.size() - 1] == '/') ?
73 LOG_PATH : LOG_PATH + '/') + timeStr;
74 auto times = static_cast<int64_t>(TimeUtil::StrToTimeStamp(StringUtil::GetRleftSubstr(timeStr, "-"),
75 "%Y%m%d%H%M%S"));
76 sysEvent->SetEventValue("HAPPEN_TIME", times);
77
78 WaitForLogs(dynamicPaths);
79 auto eventInfos = SmartParser::Analysis(dynamicPaths, logParseConfig_, sysEvent->GetEventValue("name_"));
80 Tbox::FilterTrace(eventInfos);
81
82 sysEvent->SetEventValue("FIRST_FRAME", eventInfos["FIRST_FRAME"].empty() ? "/" :
83 StringUtil::EscapeJsonStringValue(eventInfos["FIRST_FRAME"]));
84 sysEvent->SetEventValue("SECOND_FRAME", eventInfos["SECOND_FRAME"].empty() ? "/" :
85 StringUtil::EscapeJsonStringValue(eventInfos["SECOND_FRAME"]));
86 sysEvent->SetEventValue("LAST_FRAME", eventInfos["LAST_FRAME"].empty() ? "/ " :
87 StringUtil::EscapeJsonStringValue(eventInfos["LAST_FRAME"]));
88 sysEvent->SetEventValue("FINGERPRINT", Tbox::CalcFingerPrint(event + module + eventInfos["FIRST_FRAME"] +
89 eventInfos["SECOND_FRAME"] + eventInfos["LAST_FRAME"], 0, FP_BUFFER));
90 sysEvent->SetEventValue("LOG_PATH", dynamicPaths);
91 }
92 }
93 }
94