1 /*
2 * Copyright (c) 2021-2025 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 "faultlogger.h"
16 #include <memory>
17
18 #ifdef UNIT_TEST
19 #include <iostream>
20 #include <cstring>
21 #endif
22 #include <fstream>
23
24 #include "faultlog_dump.h"
25 #include "faultlog_event_factory.h"
26 #include "faultlog_util.h"
27 #include "faultlogger_service_ohos.h"
28 #include "hiview_logger.h"
29 #include "page_history_manager.h"
30 #include "plugin_factory.h"
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 REGISTER(Faultlogger);
35 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger");
IsInterestedPipelineEvent(std::shared_ptr<Event> event)36 bool Faultlogger::IsInterestedPipelineEvent(std::shared_ptr<Event> event)
37 {
38 if (!hasInit_ || event == nullptr) {
39 return false;
40 }
41
42 const int eventCount = 5;
43 std::array<std::string, eventCount> eventNames = {
44 "PROCESS_EXIT",
45 "JS_ERROR",
46 "CJ_ERROR",
47 "RUST_PANIC",
48 "ADDR_SANITIZER"
49 };
50
51 return std::find(eventNames.begin(), eventNames.end(), event->eventName_) != eventNames.end();
52 }
53
OnEvent(std::shared_ptr<Event> & event)54 bool Faultlogger::OnEvent(std::shared_ptr<Event>& event)
55 {
56 if (!hasInit_ || event == nullptr) {
57 return false;
58 }
59 if (event->rawData_ == nullptr) {
60 return false;
61 }
62 FaultLogEventFactory factory;
63 auto faultLogEvent = factory.CreateFaultLogEvent(event->eventName_);
64 if (faultLogEvent) {
65 return faultLogEvent->ProcessFaultLogEvent(event, workLoop_, faultLogManager_);
66 }
67 return true;
68 }
69
OnEventListeningCallback(const Event & event)70 void Faultlogger::OnEventListeningCallback(const Event& event)
71 {
72 if (!hasInit_ || event.rawData_ == nullptr) {
73 return;
74 }
75 PageHistoryManager::GetInstance().HandleEvent(event);
76 }
77
CanProcessEvent(std::shared_ptr<Event> event)78 bool Faultlogger::CanProcessEvent(std::shared_ptr<Event> event)
79 {
80 return true;
81 }
82
ReadyToLoad()83 bool Faultlogger::ReadyToLoad()
84 {
85 return true;
86 }
87
OnLoad()88 void Faultlogger::OnLoad()
89 {
90 auto context = GetHiviewContext();
91 if (context == nullptr) {
92 HIVIEW_LOGE("GetHiviewContext failed.");
93 return;
94 }
95 workLoop_ = context->GetSharedWorkLoop();
96 faultLogManager_ = std::make_shared<FaultLogManager>(workLoop_);
97 faultLogManager_->Init();
98 hasInit_ = true;
99 #ifndef UNITTEST
100 FaultloggerServiceOhos::StartService(std::make_shared<FaultLogManagerService>(workLoop_, faultLogManager_));
101
102 faultLogBootScan_ = std::make_shared<FaultLogBootScan>(workLoop_, faultLogManager_);
103 context->RegisterUnorderedEventListener(faultLogBootScan_);
104 #endif
105 }
106
Dump(int fd,const std::vector<std::string> & cmds)107 void Faultlogger::Dump(int fd, const std::vector<std::string>& cmds)
108 {
109 FaultLogDump faultLogDump(fd, faultLogManager_);
110 faultLogDump.DumpByCommands(cmds);
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS
114