• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "faultlog_event_base.h"
16 
17 #include "securec.h"
18 
19 #include "constants.h"
20 #include "faultlog_util.h"
21 #include "faultlog_processor_base.h"
22 #include "faultlog_events_processor.h"
23 #include "hisysevent.h"
24 #include "hiview_logger.h"
25 #include "log_analyzer.h"
26 #include "string_util.h"
27 #include "time_util.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger");
32 namespace {
33 constexpr uint32_t MAX_TIMESTR_LEN = 256;
34 }
IsDebugSignal(const FaultLogInfo & info)35 static bool IsDebugSignal(const FaultLogInfo& info)
36 {
37     return info.faultLogType == FaultLogType::ADDR_SANITIZER && info.reason.find("DEBUG SIGNAL") != std::string::npos;
38 }
39 
UpdateSysEvent(SysEvent & sysEvent,FaultLogInfo & info)40 void FaultLogEventBase::UpdateSysEvent(SysEvent& sysEvent, FaultLogInfo& info)
41 {
42     sysEvent.SetEventValue(FaultKey::FAULT_TYPE, std::to_string(info.faultLogType));
43     sysEvent.SetEventValue(FaultKey::MODULE_NAME, info.module);
44     sysEvent.SetEventValue(FaultKey::LOG_PATH, info.logPath);
45     // DEBUG SIGNAL does not need to update HAPPEN_TIME
46     if (!IsDebugSignal(info)) {
47         sysEvent.SetEventValue(FaultKey::HAPPEN_TIME, sysEvent.happenTime_);
48     }
49     sysEvent.SetEventValue("tz_", TimeUtil::GetTimeZone());
50     sysEvent.SetEventValue(FaultKey::MODULE_VERSION, info.sectionMap[FaultKey::MODULE_VERSION]);
51     sysEvent.SetEventValue(FaultKey::VERSION_CODE, info.sectionMap[FaultKey::VERSION_CODE]);
52     sysEvent.SetEventValue(FaultKey::PRE_INSTALL, info.sectionMap[FaultKey::PRE_INSTALL]);
53     sysEvent.SetEventValue(FaultKey::FOREGROUND, info.sectionMap[FaultKey::FOREGROUND]);
54 
55     std::map<std::string, std::string> eventInfos;
56     if (AnalysisFaultlog(info, eventInfos)) {
57         auto pName = sysEvent.GetEventValue(FaultKey::P_NAME);
58         if (pName.empty()) {
59             sysEvent.SetEventValue(FaultKey::P_NAME, std::string("/"));
60         }
61         sysEvent.SetEventValue(FaultKey::FIRST_FRAME, eventInfos[FaultKey::FIRST_FRAME].empty() ? "/" :
62                                 StringUtil::EscapeJsonStringValue(eventInfos[FaultKey::FIRST_FRAME]));
63         sysEvent.SetEventValue(FaultKey::SECOND_FRAME, eventInfos[FaultKey::SECOND_FRAME].empty() ? "/" :
64                                 StringUtil::EscapeJsonStringValue(eventInfos[FaultKey::SECOND_FRAME]));
65         sysEvent.SetEventValue(FaultKey::LAST_FRAME, eventInfos[FaultKey::LAST_FRAME].empty() ? "/" :
66                                 StringUtil::EscapeJsonStringValue(eventInfos[FaultKey::LAST_FRAME]));
67     }
68 
69     std::string fingerPrint;
70     if (info.faultLogType == FaultLogType::ADDR_SANITIZER) {
71         fingerPrint = sysEvent.GetEventValue(FaultKey::FINGERPRINT);
72     }
73     if (fingerPrint.empty()) {
74         sysEvent.SetEventValue(FaultKey::FINGERPRINT, eventInfos[FaultKey::FINGERPRINT]);
75     }
76 }
77 
FillCommonFaultLogInfo(SysEvent & sysEvent,FaultLogInfo & info) const78 void FaultLogEventBase::FillCommonFaultLogInfo(SysEvent& sysEvent, FaultLogInfo& info) const
79 {
80     info.time = static_cast<int64_t>(sysEvent.happenTime_);
81     info.id = sysEvent.GetUid();
82     info.pid = sysEvent.GetPid();
83     info.faultLogType = faultType_;
84     info.module =  GetFaultModule(sysEvent);
85     info.reason = sysEvent.GetEventValue(FaultKey::REASON);
86     info.summary = StringUtil::UnescapeJsonStringValue(sysEvent.GetEventValue(FaultKey::SUMMARY));
87     info.sectionMap = sysEvent.GetKeyValuePairs();
88     FillTimestampInfo(sysEvent, info);
89 }
90 
FillFaultLogInfo(SysEvent & sysEvent) const91 FaultLogInfo FaultLogEventBase::FillFaultLogInfo(SysEvent& sysEvent) const
92 {
93     FaultLogInfo info;
94     FillCommonFaultLogInfo(sysEvent, info);
95     FillSpecificFaultLogInfo(sysEvent, info);
96 
97     HIVIEW_LOGI("eventName:%{public}s, time %{public}" PRId64 ", uid %{public}d, pid %{public}d, "
98                 "module: %{public}s, reason: %{public}s",
99                 sysEvent.eventName_.c_str(), info.time, info.id, info.pid,
100                 info.module.c_str(), info.reason.c_str());
101     return info;
102 }
103 
FillTimestampInfo(const SysEvent & sysEvent,FaultLogInfo & info) const104 void FaultLogEventBase::FillTimestampInfo(const SysEvent& sysEvent, FaultLogInfo& info) const
105 {
106     uint64_t secTime = sysEvent.happenTime_ / TimeUtil::SEC_TO_MILLISEC;
107     char strBuff[MAX_TIMESTR_LEN] = {0};
108     if (snprintf_s(strBuff, sizeof(strBuff), sizeof(strBuff) - 1, "%s.%03lu",
109             TimeUtil::TimestampFormatToDate(secTime, "%Y-%m-%d %H:%M:%S").c_str(),
110             sysEvent.happenTime_ % TimeUtil::SEC_TO_MILLISEC) < 0) {
111         HIVIEW_LOGE("fill faultlog info timestamp snprintf fail!");
112         info.sectionMap[FaultKey::TIMESTAMP] = "1970-01-01 00:00:00.000";
113     } else {
114         info.sectionMap[FaultKey::TIMESTAMP] = std::string(strBuff);
115     }
116 }
117 
ProcessFaultLogEvent(std::shared_ptr<Event> & event,const std::shared_ptr<EventLoop> & workLoop,const std::shared_ptr<FaultLogManager> & faultLogManager)118 bool FaultLogEventBase::ProcessFaultLogEvent(std::shared_ptr<Event>& event, const std::shared_ptr<EventLoop>& workLoop,
119     const std::shared_ptr<FaultLogManager>& faultLogManager)
120 {
121     auto sysEvent = std::static_pointer_cast<SysEvent>(event);
122     FaultLogInfo info = FillFaultLogInfo(*sysEvent);
123     FaultLogEventsProcessor faultLogEventsProcessor;
124     faultLogEventsProcessor.AddFaultLog(info, workLoop, faultLogManager);
125     UpdateSysEvent(*sysEvent, info);
126     ReportToAppEvent(sysEvent, info);
127     return true;
128 }
129 } // namespace HiviewDFX
130 } // namespace OHOS
131