1 /*
2  * Copyright (C) 2021 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 "event_logger.h"
16 
17 #include "securec.h"
18 
19 #include <cinttypes>
20 #include <list>
21 #include <map>
22 #include <regex>
23 #include <sstream>
24 #include <unistd.h>
25 #include <vector>
26 #include <iostream>
27 #include <filesystem>
28 #include <string_ex.h>
29 
30 #include "parameter.h"
31 
32 #include "common_utils.h"
33 #include "dfx_json_formatter.h"
34 #include "event_source.h"
35 #include "file_util.h"
36 #include "freeze_json_util.h"
37 #include "log_catcher_utils.h"
38 #include "parameter_ex.h"
39 #include "plugin_factory.h"
40 #include "string_util.h"
41 #include "sys_event.h"
42 #include "sys_event_dao.h"
43 #include "time_util.h"
44 #ifdef WINDOW_MANAGER_ENABLE
45 #include "event_focus_listener.h"
46 #include "window_manager_lite.h"
47 #include "wm_common.h"
48 #endif
49 
50 #include "event_log_task.h"
51 #include "event_logger_config.h"
52 
53 namespace OHOS {
54 namespace HiviewDFX {
55 static constexpr const char *const ASHMEM_PATH = "/proc/ashmem_process_info";
56 static constexpr const char *const DMAHEAP_PATH = "/proc/dmaheap_process_info";
57 static constexpr const char *const GPUMEM_PATH = "/proc/gpumem_process_info";
58 static constexpr const char *const ASHMEM = "AshmemUsed";
59 static constexpr const char *const DMAHEAP = "DmaHeapTotalUsed";
60 static constexpr const char *const GPUMEM = "GpuTotalUsed";
61 static constexpr int OVER_MEM_SIZE = 2 * 1024 * 1024;
62 static constexpr int DECIMEL = 10;
63 
64 REGISTER(EventLogger);
65 DEFINE_LOG_LABEL(0xD002D01, "EventLogger");
IsInterestedPipelineEvent(std::shared_ptr<Event> event)66 bool EventLogger::IsInterestedPipelineEvent(std::shared_ptr<Event> event)
67 {
68     if (event == nullptr) {
69         return false;
70     }
71     if (event->eventId_ > EVENT_MAX_ID) {
72         return false;
73     }
74 
75     auto sysEvent = Event::DownCastTo<SysEvent>(event);
76     if (eventLoggerConfig_.find(sysEvent->eventName_) == eventLoggerConfig_.end()) {
77         return false;
78     }
79     HIVIEW_LOGD("event time:%{public}" PRIu64 " jsonExtraInfo is %{public}s", TimeUtil::GetMilliseconds(),
80         sysEvent->AsJsonStr().c_str());
81 
82     EventLoggerConfig::EventLoggerConfigData& configOut = eventLoggerConfig_[sysEvent->eventName_];
83     sysEvent->eventName_ = configOut.name;
84     sysEvent->SetValue("eventLog_action", configOut.action);
85     sysEvent->SetValue("eventLog_interval", configOut.interval);
86     return true;
87 }
88 
GetEventPid(std::shared_ptr<SysEvent> & sysEvent)89 long EventLogger::GetEventPid(std::shared_ptr<SysEvent> &sysEvent)
90 {
91     long pid = sysEvent->GetEventIntValue("PID");
92     if (pid > 0) {
93         return pid;
94     }
95     pid = CommonUtils::GetPidByName(sysEvent->GetEventValue("PACKAGE_NAME"));
96     if (pid > 0) {
97         sysEvent->SetEventValue("PID", pid);
98         return pid;
99     }
100     pid = sysEvent->GetPid();
101     sysEvent->SetEventValue("PID", pid);
102     return pid;
103 }
104 
OnEvent(std::shared_ptr<Event> & onEvent)105 bool EventLogger::OnEvent(std::shared_ptr<Event> &onEvent)
106 {
107     if (onEvent == nullptr) {
108         HIVIEW_LOGE("event == nullptr");
109         return false;
110     }
111 #ifdef WINDOW_MANAGER_ENABLE
112     EventFocusListener::RegisterFocusListener();
113 #endif
114     std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(onEvent);
115 
116     long pid = GetEventPid(sysEvent);
117     std::string eventName = sysEvent->eventName_;
118     if (eventName == "GESTURE_NAVIGATION_BACK" || eventName == "FREQUENT_CLICK_WARNING") {
119 #ifdef WINDOW_MANAGER_ENABLE
120         if (EventFocusListener::registerState_ == EventFocusListener::REGISTERED) {
121             ReportUserPanicWarning(sysEvent, pid);
122         }
123 #endif
124         return true;
125     }
126     if (!IsHandleAppfreeze(sysEvent)) {
127         return true;
128     }
129 
130     std::string domain = sysEvent->domain_;
131     HIVIEW_LOGI("domain=%{public}s, eventName=%{public}s, pid=%{public}ld", domain.c_str(), eventName.c_str(), pid);
132 
133     if (CheckProcessRepeatFreeze(eventName, pid)) {
134         return true;
135     }
136     if (sysEvent->GetValue("eventLog_action").empty()) {
137         HIVIEW_LOGI("eventName=%{public}s, pid=%{public}ld, eventLog_action is empty.", eventName.c_str(), pid);
138         UpdateDB(sysEvent, "nolog");
139         return true;
140     }
141 
142     sysEvent->OnPending();
143 
144     bool isFfrt = std::find(DUMP_FFRT.begin(), DUMP_FFRT.end(), eventName) != DUMP_FFRT.end();
145     auto task = [this, sysEvent, isFfrt] {
146         HIVIEW_LOGI("time:%{public}" PRIu64 " jsonExtraInfo is %{public}s", TimeUtil::GetMilliseconds(),
147             sysEvent->AsJsonStr().c_str());
148         if (!JudgmentRateLimiting(sysEvent)) {
149             return;
150         }
151         if (isFfrt) {
152             this->StartFfrtDump(sysEvent);
153         }
154         this->StartLogCollect(sysEvent);
155     };
156     HIVIEW_LOGI("before submit event task to ffrt, eventName=%{public}s, pid=%{public}ld", eventName.c_str(), pid);
157     ffrt::submit(task, {}, {}, ffrt::task_attr().name("eventlogger"));
158     HIVIEW_LOGD("after submit event task to ffrt, eventName=%{public}s, pid=%{public}ld", eventName.c_str(), pid);
159     return true;
160 }
161 
GetFile(std::shared_ptr<SysEvent> event,std::string & logFile,bool isFfrt)162 int EventLogger::GetFile(std::shared_ptr<SysEvent> event, std::string& logFile, bool isFfrt)
163 {
164     uint64_t logTime = event->happenTime_ / TimeUtil::SEC_TO_MILLISEC;
165     std::string formatTime = TimeUtil::TimestampFormatToDate(logTime, "%Y%m%d%H%M%S");
166     int32_t pid = static_cast<int32_t>(event->GetEventIntValue("PID"));
167     pid = pid ? pid : event->GetPid();
168     if (!isFfrt) {
169         std::string idStr = event->eventName_.empty() ? std::to_string(event->eventId_) : event->eventName_;
170         logFile = idStr + "-" + std::to_string(pid) + "-" + formatTime + ".log";
171     } else {
172         logFile = "ffrt_" + std::to_string(pid) + "_" + formatTime;
173     }
174 
175     if (FileUtil::FileExists(LOGGER_EVENT_LOG_PATH + "/" + logFile)) {
176         HIVIEW_LOGW("filename: %{public}s is existed, direct use.", logFile.c_str());
177         if (!isFfrt) {
178             UpdateDB(event, logFile);
179         }
180         return -1;
181     }
182     return logStore_->CreateLogFile(logFile);
183 }
184 
StartFfrtDump(std::shared_ptr<SysEvent> event)185 void EventLogger::StartFfrtDump(std::shared_ptr<SysEvent> event)
186 {
187     LogCatcherUtils::FFRT_TYPE type = LogCatcherUtils::TOP;
188     long pid = event->GetEventIntValue("PID") ? event->GetEventIntValue("PID") : event->GetPid();
189 #ifdef WINDOW_MANAGER_ENABLE
190     std::vector<Rosen::MainWindowInfo> windowInfos;
191 #endif
192     if (event->eventName_ == "GET_DISPLAY_SNAPSHOT" || event->eventName_ == "CREATE_VIRTUAL_SCREEN") {
193 #ifdef WINDOW_MANAGER_ENABLE
194         Rosen::WindowManagerLite::GetInstance().GetMainWindowInfos(TOP_WINDOW_NUM, windowInfos);
195         if (windowInfos.size() == 0) {
196             return;
197         }
198 #else
199         return;
200 #endif
201     } else {
202         type = LogCatcherUtils::GetFfrtDumpType(pid);
203     }
204 
205     std::string ffrtFile;
206     int ffrtFd = GetFile(event, ffrtFile, true);
207     if (ffrtFd < 0) {
208         HIVIEW_LOGE("create ffrt log file %{public}s failed, %{public}d", ffrtFile.c_str(), ffrtFd);
209         return;
210     }
211 
212     int count = (type == LogCatcherUtils::TOP) ? LogCatcherUtils::WAIT_CHILD_PROCESS_COUNT * DUMP_TIME_RATIO :
213         LogCatcherUtils::WAIT_CHILD_PROCESS_COUNT;
214     if (type == LogCatcherUtils::TOP) {
215 #ifdef WINDOW_MANAGER_ENABLE
216         FileUtil::SaveStringToFd(ffrtFd, "dump topWindowInfos, process infos:\n");
217         std::string cmdAms = "--ffrt ";
218         std::string cmdSam = "--ffrt ";
219         int size = static_cast<int>(windowInfos.size());
220         for (int i = 0; i < size ; i++) {
221             auto info = windowInfos[i];
222             FileUtil::SaveStringToFd(ffrtFd, "    " + std::to_string(info.pid_) + ":" + info.bundleName_ + "\n");
223             cmdAms += std::to_string(info.pid_) + (i < size -1 ? "," : "");
224             cmdSam += std::to_string(info.pid_) + (i < size -1 ? "|" : "");
225         }
226         LogCatcherUtils::ReadShellToFile(ffrtFd, "ApplicationManagerService", cmdAms, count);
227         if (count > LogCatcherUtils::WAIT_CHILD_PROCESS_COUNT / DUMP_TIME_RATIO) {
228             LogCatcherUtils::ReadShellToFile(ffrtFd, "SystemAbilityManager", cmdSam, count);
229         }
230 #endif
231     } else {
232         FileUtil::SaveStringToFd(ffrtFd, "ffrt dump info:\n");
233         std::string serviceName = (type == LogCatcherUtils::APP) ? "ApplicationManagerService" : "SystemAbilityManager";
234         LogCatcherUtils::ReadShellToFile(ffrtFd, serviceName, "--ffrt " + std::to_string(pid), count);
235     }
236     close(ffrtFd);
237 }
238 
GetStringFromFile(const std::string path)239 std::string EventLogger::GetStringFromFile(const std::string path)
240 {
241     std::string content;
242     FileUtil::LoadStringFromFile(path, content);
243     return content;
244 }
245 
GetNumFromString(const std::string & mem)246 int EventLogger::GetNumFromString(const std::string &mem)
247 {
248     int num = 0;
249     for (const char &c : mem) {
250         if (isdigit(c)) {
251             num += num * DECIMEL + (c - '0');
252         }
253         if (num > INT_MAX) {
254             return INT_MAX;
255         }
256     }
257     return num;
258 }
259 
CheckString(int fd,const std::string & mem,std::string & data,const std::string key,const std::string path)260 void EventLogger::CheckString(
261     int fd, const std::string &mem, std::string &data, const std::string key, const std::string path)
262 {
263     if (mem.find(key) != std::string::npos) {
264         int memsize = GetNumFromString(mem);
265         if (memsize > OVER_MEM_SIZE) {
266             data += GetStringFromFile(path);
267         }
268     }
269 }
270 
CollectMemInfo(int fd,std::shared_ptr<SysEvent> event)271 void EventLogger::CollectMemInfo(int fd, std::shared_ptr<SysEvent> event)
272 {
273     std::string content = event->GetEventValue("FREEZE_MEMORY");
274     std::string data = "";
275     if (!content.empty()) {
276         std::vector<std::string> vec;
277         OHOS::SplitStr(content, "\\n", vec);
278         FreezeCommon::WriteStartInfoToFd(fd, "start collect meminfo: ");
279         FileUtil::SaveStringToFd(fd, "\nMemoryCatcher --\n");
280         for (const std::string& mem : vec) {
281             FileUtil::SaveStringToFd(fd, mem + "\n");
282             CheckString(fd, mem, data, ASHMEM, ASHMEM_PATH);
283             CheckString(fd, mem, data, DMAHEAP, DMAHEAP_PATH);
284             CheckString(fd, mem, data, GPUMEM, GPUMEM_PATH);
285         }
286         FreezeCommon::WriteEndInfoToFd(fd, "\nend collect meminfo: ");
287     }
288     if (!data.empty()) {
289         FileUtil::SaveStringToFd(fd, data);
290     } else {
291         FileUtil::SaveStringToFd(fd, "don't collect ashmem dmaheap gpumem");
292     }
293 }
294 
SaveDbToFile(const std::shared_ptr<SysEvent> & event)295 void EventLogger::SaveDbToFile(const std::shared_ptr<SysEvent>& event)
296 {
297     std::string historyFile = LOGGER_EVENT_LOG_PATH + "/" + "history.log";
298     mode_t mode = 0644;
299     if (FileUtil::CreateFile(historyFile, mode) != 0 && !FileUtil::FileExists(historyFile)) {
300         HIVIEW_LOGE("failed to create file=%{public}s, errno=%{public}d", historyFile.c_str(), errno);
301         return;
302     }
303     std::vector<std::string> lines;
304     FileUtil::LoadLinesFromFile(historyFile, lines);
305     bool truncated = false;
306     if (lines.size() > HISTORY_EVENT_LIMIT) {
307         truncated = true;
308     }
309     auto time = TimeUtil::TimestampFormatToDate(event->happenTime_ / TimeUtil::SEC_TO_MILLISEC,
310         "%Y%m%d%H%M%S");
311     long pid = event->GetEventIntValue("PID") ? event->GetEventIntValue("PID") : event->GetPid();
312     long uid = event->GetEventIntValue("UID") ? event->GetEventIntValue("UID") : event->GetUid();
313     std::string str = "time[" + time + "], domain[" + event->domain_ + "], wpName[" +
314         event->eventName_ + "], pid: " + std::to_string(pid) + ", uid: " + std::to_string(uid) + "\n";
315     FileUtil::SaveStringToFile(historyFile, str, truncated);
316 }
317 
StartLogCollect(std::shared_ptr<SysEvent> event)318 void EventLogger::StartLogCollect(std::shared_ptr<SysEvent> event)
319 {
320     std::string logFile;
321     int fd = GetFile(event, logFile, false);
322     if (fd < 0) {
323         HIVIEW_LOGE("create log file %{public}s failed, %{public}d", logFile.c_str(), fd);
324         return;
325     }
326 
327     int jsonFd = -1;
328     if (FreezeJsonUtil::IsAppFreeze(event->eventName_)) {
329         std::string jsonFilePath = FreezeJsonUtil::GetFilePath(event->GetEventIntValue("PID"),
330             event->GetEventIntValue("UID"), event->happenTime_);
331         jsonFd = FreezeJsonUtil::GetFd(jsonFilePath);
332     }
333 
334     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, jsonFd, event);
335     std::string cmdStr = event->GetValue("eventLog_action");
336     std::vector<std::string> cmdList;
337     StringUtil::SplitStr(cmdStr, ",", cmdList);
338     for (const std::string& cmd : cmdList) {
339         logTask->AddLog(cmd);
340     }
341 
342     const uint32_t placeholder = 3;
343     auto start = TimeUtil::GetMilliseconds();
344     uint64_t startTime = start / TimeUtil::SEC_TO_MILLISEC;
345     std::ostringstream startTimeStr;
346     startTimeStr << "start time: " << TimeUtil::TimestampFormatToDate(startTime, "%Y/%m/%d-%H:%M:%S");
347     startTimeStr << ":" << std::setw(placeholder) << std::setfill('0') <<
348         std::to_string(start % TimeUtil::SEC_TO_MILLISEC);
349     startTimeStr << std::endl;
350     FileUtil::SaveStringToFd(fd, startTimeStr.str());
351     WriteCommonHead(fd, event);
352     WriteFreezeJsonInfo(fd, jsonFd, event);
353     CollectMemInfo(fd, event);
354     auto ret = logTask->StartCompose();
355     if (ret != EventLogTask::TASK_SUCCESS) {
356         HIVIEW_LOGE("capture fail %{public}d", ret);
357     }
358     auto end = TimeUtil::GetMilliseconds();
359     std::string totalTime = "\n\nCatcher log total time is " + std::to_string(end - start) + "ms\n";
360     FileUtil::SaveStringToFd(fd, totalTime);
361     close(fd);
362     if (jsonFd >= 0) {
363         close(jsonFd);
364     }
365     UpdateDB(event, logFile);
366     SaveDbToFile(event);
367 
368     constexpr int waitTime = 1;
369     auto CheckFinishFun = [this, event] { this->CheckEventOnContinue(event); };
370     threadLoop_->AddTimerEvent(nullptr, nullptr, CheckFinishFun, waitTime, false);
371     HIVIEW_LOGI("Collect on finish, name: %{public}s", logFile.c_str());
372 }
373 
ParseMsgForMessageAndEventHandler(const std::string & msg,std::string & message,std::string & eventHandlerStr)374 bool ParseMsgForMessageAndEventHandler(const std::string& msg, std::string& message, std::string& eventHandlerStr)
375 {
376     std::vector<std::string> lines;
377     StringUtil::SplitStr(msg, "\n", lines, false, true);
378     bool isGetMessage = false;
379     std::string messageStartFlag = "Fault time:";
380     std::string messageEndFlag = "mainHandler dump is:";
381     std::string eventFlag = "Event {";
382     bool isGetEvent = false;
383     std::regex eventStartFlag(".*((Immediate)|(High)|(Low)) priority event queue information:.*");
384     std::regex eventEndFlag(".*Total size of ((Immediate)|(High)|(Low)) events :.*");
385     std::list<std::string> eventHandlerList;
386     for (auto line = lines.begin(); line != lines.end(); line++) {
387         if ((*line).find(messageStartFlag) != std::string::npos) {
388             isGetMessage = true;
389             continue;
390         }
391         if (isGetMessage) {
392             if ((*line).find(messageEndFlag) != std::string::npos) {
393                 isGetMessage = false;
394                 HIVIEW_LOGD("Get FreezeJson message jsonStr: %{public}s", message.c_str());
395                 continue;
396             }
397             message += StringUtil::TrimStr(*line);
398             continue;
399         }
400         if (regex_match(*line, eventStartFlag)) {
401             isGetEvent = true;
402             continue;
403         }
404         if (isGetEvent) {
405             if (regex_match(*line, eventEndFlag)) {
406                 isGetEvent = false;
407                 continue;
408             }
409             std::string::size_type pos = (*line).find(eventFlag);
410             if (pos == std::string::npos) {
411                 continue;
412             }
413             std::string handlerStr = StringUtil::TrimStr(*line).substr(pos);
414             HIVIEW_LOGD("Get EventHandler str: %{public}s.", handlerStr.c_str());
415             eventHandlerList.push_back(handlerStr);
416         }
417     }
418     eventHandlerStr = FreezeJsonUtil::GetStrByList(eventHandlerList);
419     return true;
420 }
421 
ParsePeerBinder(const std::string & binderInfo,std::string & binderInfoJsonStr)422 void ParsePeerBinder(const std::string& binderInfo, std::string& binderInfoJsonStr)
423 {
424     std::vector<std::string> lines;
425     StringUtil::SplitStr(binderInfo, "\\n", lines, false, true);
426     std::list<std::string> infoList;
427     std::map<std::string, std::string> processNameMap;
428 
429     for (auto lineIt = lines.begin(); lineIt != lines.end(); lineIt++) {
430         std::string line = *lineIt;
431         if (line.empty() || line.find("async") != std::string::npos) {
432             continue;
433         }
434 
435         if (line.find("context") != line.npos) {
436             break;
437         }
438 
439         std::istringstream lineStream(line);
440         std::vector<std::string> strList;
441         std::string tmpstr;
442         while (lineStream >> tmpstr) {
443             strList.push_back(tmpstr);
444         }
445         if (strList.size() < 7) { // less than 7: valid array size
446             continue;
447         }
448         // 2: binder peer id
449         std::string pidStr = strList[2].substr(0, strList[2].find(":"));
450         if (pidStr == "") {
451             continue;
452         }
453         if (processNameMap.find(pidStr) == processNameMap.end()) {
454             std::string filePath = "/proc/" + pidStr + "/cmdline";
455             std::string realPath;
456             if (!FileUtil::PathToRealPath(filePath, realPath)) {
457                 continue;
458             }
459             std::ifstream cmdLineFile(realPath);
460             std::string processName;
461             if (cmdLineFile) {
462                 std::getline(cmdLineFile, processName);
463                 cmdLineFile.close();
464                 StringUtil::FormatProcessName(processName);
465                 processNameMap[pidStr] = processName;
466             } else {
467                 HIVIEW_LOGE("Fail to open /proc/%{public}s/cmdline", pidStr.c_str());
468             }
469         }
470         std::string lineStr = line + "    " + pidStr + FreezeJsonUtil::WrapByParenthesis(processNameMap[pidStr]);
471         infoList.push_back(lineStr);
472     }
473     binderInfoJsonStr = FreezeJsonUtil::GetStrByList(infoList);
474 }
475 
WriteCommonHead(int fd,std::shared_ptr<SysEvent> event)476 bool EventLogger::WriteCommonHead(int fd, std::shared_ptr<SysEvent> event)
477 {
478     std::ostringstream headerStream;
479 
480     headerStream << "DOMAIN = " << event->domain_ << std::endl;
481     headerStream << "EVENTNAME = " << event->eventName_ << std::endl;
482     uint64_t logTime = event->happenTime_ / TimeUtil::SEC_TO_MILLISEC;
483     uint64_t logTimeMs = event->happenTime_ % TimeUtil::SEC_TO_MILLISEC;
484     std::string happenTime = TimeUtil::TimestampFormatToDate(logTime, "%Y/%m/%d-%H:%M:%S");
485     headerStream << "TIMESTAMP = " << happenTime << ":" << logTimeMs << std::endl;
486     long pid = event->GetEventIntValue("PID");
487     pid = pid ? pid : event->GetPid();
488     headerStream << "PID = " << pid << std::endl;
489     long uid = event->GetEventIntValue("UID");
490     uid = uid ? uid : event->GetUid();
491     headerStream << "UID = " << uid << std::endl;
492     if (event->GetEventIntValue("TID")) {
493         headerStream << "TID = " << event->GetEventIntValue("TID") << std::endl;
494     } else {
495         headerStream << "TID = " << pid << std::endl;
496     }
497     if (event->GetEventValue("MODULE_NAME") != "") {
498         headerStream << "MODULE_NAME = " << event->GetEventValue("MODULE_NAME") << std::endl;
499     } else {
500         headerStream << "PACKAGE_NAME = " << event->GetEventValue("PACKAGE_NAME") << std::endl;
501     }
502     headerStream << "PROCESS_NAME = " << event->GetEventValue("PROCESS_NAME") << std::endl;
503     headerStream << "eventLog_action = " << event->GetValue("eventLog_action") << std::endl;
504     headerStream << "eventLog_interval = " << event->GetValue("eventLog_interval") << std::endl;
505 
506     FileUtil::SaveStringToFd(fd, headerStream.str());
507     return true;
508 }
509 
WriteCallStack(std::shared_ptr<SysEvent> event,int fd)510 void EventLogger::WriteCallStack(std::shared_ptr<SysEvent> event, int fd)
511 {
512     if (event->domain_.compare("FORM_MANAGER") == 0 && event->eventName_.compare("FORM_BLOCK_CALLSTACK") == 0) {
513         std::ostringstream stackOss;
514         std::string stackMsg = StringUtil::ReplaceStr(event->GetEventValue("EVENT_KEY_FORM_BLOCK_CALLSTACK"),
515         "\\n", "\n");
516         stackOss << "CallStack = " << stackMsg << std::endl;
517         FileUtil::SaveStringToFd(fd, stackOss.str());
518 
519         std::ostringstream appNameOss;
520         std::string appMsg = StringUtil::ReplaceStr(event->GetEventValue("EVENT_KEY_FORM_BLOCK_APPNAME"),
521         "\\n", "\n");
522         appNameOss << "AppName = " << appMsg << std::endl;
523         FileUtil::SaveStringToFd(fd, appNameOss.str());
524     }
525 }
526 
GetAppFreezeFile(std::string & stackPath)527 std::string EventLogger::GetAppFreezeFile(std::string& stackPath)
528 {
529     std::string result = "";
530     if (!FileUtil::FileExists(stackPath)) {
531         result = "";
532         HIVIEW_LOGE("File is not exist");
533         return result;
534     }
535     FileUtil::LoadStringFromFile(stackPath, result);
536     bool isRemove = FileUtil::RemoveFile(stackPath.c_str());
537     HIVIEW_LOGI("Remove file? isRemove:%{public}d", isRemove);
538     return result;
539 }
540 
IsKernelStack(const std::string & stack)541 bool EventLogger::IsKernelStack(const std::string& stack)
542 {
543     return (!stack.empty() && stack.find("Stack backtrace") != std::string::npos);
544 }
545 
GetNoJsonStack(std::string & stack,std::string & contentStack,std::string & kernelStack,bool isFormat)546 void EventLogger::GetNoJsonStack(std::string& stack, std::string& contentStack,
547     std::string& kernelStack, bool isFormat)
548 {
549     if (!IsKernelStack(contentStack)) {
550         stack = contentStack;
551         contentStack = "[]";
552     } else if (DfxJsonFormatter::FormatKernelStack(contentStack, stack, isFormat)) {
553         kernelStack = contentStack;
554         contentStack = stack;
555         stack = "";
556         if (!isFormat || !DfxJsonFormatter::FormatJsonStack(contentStack, stack)) {
557             stack = contentStack;
558         }
559     } else {
560         kernelStack = contentStack;
561         stack = "Failed to format kernel stack\n";
562         contentStack = "[]";
563     }
564 }
565 
GetAppFreezeStack(int jsonFd,std::shared_ptr<SysEvent> event,std::string & stack,const std::string & msg,std::string & kernelStack)566 void EventLogger::GetAppFreezeStack(int jsonFd, std::shared_ptr<SysEvent> event,
567     std::string& stack, const std::string& msg, std::string& kernelStack)
568 {
569     std::string message;
570     std::string eventHandlerStr;
571     ParseMsgForMessageAndEventHandler(msg, message, eventHandlerStr);
572     std::string appRunningUniqueId = event->GetEventValue("APP_RUNNING_UNIQUE_ID");
573 
574     std::string jsonStack = event->GetEventValue("STACK");
575     HIVIEW_LOGI("Current jsonStack is? jsonStack:%{public}s", jsonStack.c_str());
576     if (FileUtil::FileExists(jsonStack)) {
577         jsonStack = GetAppFreezeFile(jsonStack);
578     }
579 
580     if (!jsonStack.empty() && jsonStack[0] == '[') { // json stack info should start with '['
581         jsonStack = StringUtil::UnescapeJsonStringValue(jsonStack);
582         if (!DfxJsonFormatter::FormatJsonStack(jsonStack, stack)) {
583             stack = jsonStack;
584         }
585     } else {
586         GetNoJsonStack(stack, jsonStack, kernelStack, true);
587     }
588 
589     GetFailedDumpStackMsg(stack, event);
590 
591     if (jsonFd >= 0) {
592         HIVIEW_LOGI("success to open FreezeJsonFile! jsonFd: %{public}d", jsonFd);
593         FreezeJsonUtil::WriteKeyValue(jsonFd, "message", message);
594         FreezeJsonUtil::WriteKeyValue(jsonFd, "event_handler", eventHandlerStr);
595         FreezeJsonUtil::WriteKeyValue(jsonFd, "appRunningUniqueId", appRunningUniqueId);
596         FreezeJsonUtil::WriteKeyValue(jsonFd, "stack", jsonStack);
597     } else {
598         HIVIEW_LOGE("fail to open FreezeJsonFile! jsonFd: %{public}d", jsonFd);
599     }
600 }
601 
WriteKernelStackToFile(std::shared_ptr<SysEvent> event,int originFd,const std::string & kernelStack)602 void EventLogger::WriteKernelStackToFile(std::shared_ptr<SysEvent> event, int originFd,
603     const std::string& kernelStack)
604 {
605     if (kernelStack.empty()) {
606         return;
607     }
608     uint64_t logTime = event->happenTime_ / TimeUtil::SEC_TO_MILLISEC;
609     std::string formatTime = TimeUtil::TimestampFormatToDate(logTime, "%Y%m%d%H%M%S");
610     int32_t pid = static_cast<int32_t>(event->GetEventIntValue("PID"));
611     pid = pid ? pid : event->GetPid();
612     std::string idStr = event->eventName_.empty() ? std::to_string(event->eventId_) : event->eventName_;
613     std::string logFile = idStr + "-" + std::to_string(pid) + "-" + formatTime + "-KernelStack-" +
614         std::to_string(originFd) + ".log";
615     std::string path = LOGGER_EVENT_LOG_PATH + "/" + logFile;
616     if (FileUtil::FileExists(path)) {
617         HIVIEW_LOGI("Filename: %{public}s is existed.", logFile.c_str());
618         return;
619     }
620     int kernelFd = logStore_->CreateLogFile(logFile);
621     if (kernelFd >= 0) {
622         FileUtil::SaveStringToFd(kernelFd, kernelStack);
623         close(kernelFd);
624         HIVIEW_LOGD("Success WriteKernelStackToFile: %{public}s.", path.c_str());
625     }
626 }
627 
ParsePeerStack(std::string & binderInfo,std::string & binderPeerStack)628 void EventLogger::ParsePeerStack(std::string& binderInfo, std::string& binderPeerStack)
629 {
630     if (binderInfo.empty() || !IsKernelStack(binderInfo)) {
631         return;
632     }
633     std::string tags = "PeerBinder catcher stacktrace for pid ";
634     auto index = binderInfo.find(tags);
635     if (index == std::string::npos) {
636         return;
637     }
638     std::ostringstream oss;
639     oss << binderInfo.substr(0, index);
640     std::string bodys = binderInfo.substr(index, binderInfo.size());
641     std::vector<std::string> lines;
642     StringUtil::SplitStr(bodys, tags, lines, false, true);
643     std::string stack;
644     std::string kernelStack;
645     for (auto lineIt = lines.begin(); lineIt != lines.end(); lineIt++) {
646         std::string line = tags + *lineIt;
647         stack = "";
648         kernelStack = "";
649         GetNoJsonStack(stack, line, kernelStack, false);
650         binderPeerStack += kernelStack;
651         oss << stack << std::endl;
652     }
653     binderInfo = oss.str();
654 }
655 
WriteFreezeJsonInfo(int fd,int jsonFd,std::shared_ptr<SysEvent> event)656 bool EventLogger::WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event)
657 {
658     std::string msg = StringUtil::ReplaceStr(event->GetEventValue("MSG"), "\\n", "\n");
659     std::string stack;
660     std::string binderInfo = event -> GetEventValue("BINDER_INFO");
661     if (FreezeJsonUtil::IsAppFreeze(event -> eventName_)) {
662         std::string kernelStack = "";
663         GetAppFreezeStack(jsonFd, event, stack, msg, kernelStack);
664         if (!binderInfo.empty() && jsonFd >= 0) {
665             HIVIEW_LOGI("Current binderInfo is? binderInfo:%{public}s", binderInfo.c_str());
666             if (FileUtil::FileExists(binderInfo)) {
667                 binderInfo = GetAppFreezeFile(binderInfo);
668             }
669             std::string binderInfoJsonStr;
670             ParsePeerBinder(binderInfo, binderInfoJsonStr);
671             FreezeJsonUtil::WriteKeyValue(jsonFd, "peer_binder", binderInfoJsonStr);
672             ParsePeerStack(binderInfo, kernelStack);
673         }
674         WriteKernelStackToFile(event, fd, kernelStack);
675     } else {
676         stack = event->GetEventValue("STACK");
677         HIVIEW_LOGI("Current stack is? stack:%{public}s", stack.c_str());
678         if (FileUtil::FileExists(stack)) {
679             stack = GetAppFreezeFile(stack);
680             std::string tempStack = "";
681             std::string kernelStack = "";
682             GetNoJsonStack(tempStack, stack, kernelStack, false);
683             WriteKernelStackToFile(event, fd, kernelStack);
684             stack = tempStack;
685         }
686         GetFailedDumpStackMsg(stack, event);
687     }
688 
689     std::ostringstream oss;
690     oss << "MSG = " << msg << std::endl;
691     if (!stack.empty()) {
692         oss << StringUtil::UnescapeJsonStringValue(stack) << std::endl;
693     }
694     if (!binderInfo.empty()) {
695         oss << StringUtil::UnescapeJsonStringValue(binderInfo) << std::endl;
696     }
697     FileUtil::SaveStringToFd(fd, oss.str());
698     WriteCallStack(event, fd);
699     return true;
700 }
701 
GetFailedDumpStackMsg(std::string & stack,std::shared_ptr<SysEvent> event)702 void EventLogger::GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event)
703 {
704     std::string failedStackStart = " Failed to dump stacktrace for ";
705     if (dbHelper_ != nullptr && stack.size() >= failedStackStart.size() &&
706         !stack.compare(0, failedStackStart.size(), failedStackStart) &&
707         stack.find("syscall SIGDUMP error") != std::string::npos) {
708         long pid = event->GetEventIntValue("PID") ? event->GetEventIntValue("PID") : event->GetPid();
709         std::string packageName = event->GetEventValue("PACKAGE_NAME").empty() ?
710             event->GetEventValue("PROCESS_NAME") : event->GetEventValue("PACKAGE_NAME");
711 
712         std::vector<WatchPoint> list;
713         FreezeResult freezeResult(0, "FRAMEWORK", "PROCESS_KILL");
714         freezeResult.SetSamePackage("true");
715         DBHelper::WatchParams params = {pid, packageName};
716         dbHelper_->SelectEventFromDB(event->happenTime_ - QUERY_PROCESS_KILL_INTERVAL, event->happenTime_, list,
717             params, freezeResult);
718         std::string appendStack = "";
719         std::for_each(list.begin(), list.end(), [&appendStack] (const WatchPoint& watchPoint) {
720             appendStack += "\n" + watchPoint.GetMsg();
721         });
722         stack += appendStack.empty() ? "\ncan not get process kill reason" : "\nprocess may be killed by : "
723             + appendStack;
724     }
725 }
726 
JudgmentRateLimiting(std::shared_ptr<SysEvent> event)727 bool EventLogger::JudgmentRateLimiting(std::shared_ptr<SysEvent> event)
728 {
729     int32_t interval = event->GetIntValue("eventLog_interval");
730     if (interval == 0) {
731         return true;
732     }
733 
734     int64_t pid = event->GetEventIntValue("PID");
735     pid = pid ? pid : event->GetPid();
736     std::string eventName = event->eventName_;
737     std::string eventPid = std::to_string(pid);
738 
739     intervalMutex_.lock();
740     std::time_t now = std::time(0);
741     for (auto it = eventTagTime_.begin(); it != eventTagTime_.end();) {
742         if (it->first.find(eventName) != it->first.npos) {
743             if ((now - it->second) >= interval) {
744                 it = eventTagTime_.erase(it);
745                 continue;
746             }
747         }
748         ++it;
749     }
750 
751     std::string tagTimeName = eventName + eventPid;
752     auto it = eventTagTime_.find(tagTimeName);
753     if (it != eventTagTime_.end()) {
754         if ((now - it->second) < interval) {
755             HIVIEW_LOGE("event: id:0x%{public}d, eventName:%{public}s pid:%{public}s. \
756                 interval:%{public}" PRId32 " There's not enough interval",
757                 event->eventId_, eventName.c_str(), eventPid.c_str(), interval);
758             intervalMutex_.unlock();
759             return false;
760         }
761     }
762     eventTagTime_[tagTimeName] = now;
763     HIVIEW_LOGD("event: id:0x%{public}d, eventName:%{public}s pid:%{public}s. \
764         interval:%{public}" PRId32 " normal interval",
765         event->eventId_, eventName.c_str(), eventPid.c_str(), interval);
766     intervalMutex_.unlock();
767     return true;
768 }
769 
UpdateDB(std::shared_ptr<SysEvent> event,std::string logFile)770 bool EventLogger::UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile)
771 {
772     if (logFile == "nolog") {
773         HIVIEW_LOGI("set info_ with nolog into db.");
774         event->SetEventValue(EventStore::EventCol::INFO, "nolog", false);
775     } else {
776         auto logPath = R"~(logPath:)~" + LOGGER_EVENT_LOG_PATH  + "/" + logFile;
777         event->SetEventValue(EventStore::EventCol::INFO, logPath, true);
778     }
779     return true;
780 }
781 
IsHandleAppfreeze(std::shared_ptr<SysEvent> event)782 bool EventLogger::IsHandleAppfreeze(std::shared_ptr<SysEvent> event)
783 {
784     std::string bundleName = event->GetEventValue("PACKAGE_NAME");
785     if (bundleName.empty()) {
786         bundleName = event->GetEventValue("MODULE_NAME");
787     }
788     if (bundleName.empty()) {
789         return true;
790     }
791 
792     const int buffSize = 128;
793     char paramOutBuff[buffSize] = {0};
794     GetParameter("hiviewdfx.appfreeze.filter_bundle_name", "", paramOutBuff, buffSize - 1);
795 
796     std::string str(paramOutBuff);
797     if (str.find(bundleName) != std::string::npos) {
798         HIVIEW_LOGW("appfreeze filtration %{public}s.", bundleName.c_str());
799         return false;
800     }
801     return true;
802 }
803 
804 #ifdef WINDOW_MANAGER_ENABLE
ReportUserPanicWarning(std::shared_ptr<SysEvent> event,long pid)805 void EventLogger::ReportUserPanicWarning(std::shared_ptr<SysEvent> event, long pid)
806 {
807     if (event->eventName_ == "FREQUENT_CLICK_WARNING") {
808         if (event->happenTime_ - EventFocusListener::lastChangedTime_ <= CLICK_FREEZE_TIME_LIMIT) {
809             return;
810         }
811     } else {
812         backTimes_.push_back(event->happenTime_);
813         if (backTimes_.size() < BACK_FREEZE_COUNT_LIMIT) {
814             return;
815         }
816         if ((event->happenTime_ - backTimes_[0] <= BACK_FREEZE_TIME_LIMIT) &&
817             (event->happenTime_ - EventFocusListener::lastChangedTime_ > BACK_FREEZE_TIME_LIMIT)) {
818             backTimes_.clear();
819         } else {
820             backTimes_.erase(backTimes_.begin(), backTimes_.end() - (BACK_FREEZE_COUNT_LIMIT - 1));
821             return;
822         }
823     }
824 
825     auto userPanicEvent = std::make_shared<SysEvent>("EventLogger", nullptr, "");
826 
827     std::string processName = (event->eventName_ == "FREQUENT_CLICK_WARNING") ? event->GetEventValue("PROCESS_NAME") :
828         event->GetEventValue("PNAMEID");
829     std::string msg = (event->eventName_ == "FREQUENT_CLICK_WARNING") ? "frequent click" : "gesture navigation back";
830 
831     userPanicEvent->domain_ = "FRAMEWORK";
832     userPanicEvent->eventName_ = "USER_PANIC_WARNING";
833     userPanicEvent->happenTime_ = TimeUtil::GetMilliseconds();
834     userPanicEvent->messageType_ = Event::MessageType::SYS_EVENT;
835     userPanicEvent->SetEventValue(EventStore::EventCol::DOMAIN, "FRAMEWORK");
836     userPanicEvent->SetEventValue(EventStore::EventCol::NAME, "USER_PANIC_WARNING");
837     userPanicEvent->SetEventValue(EventStore::EventCol::TYPE, 1);
838     userPanicEvent->SetEventValue(EventStore::EventCol::TS, TimeUtil::GetMilliseconds());
839     userPanicEvent->SetEventValue(EventStore::EventCol::TZ, TimeUtil::GetTimeZone());
840     userPanicEvent->SetEventValue("PID", pid);
841     userPanicEvent->SetEventValue("UID", 0);
842     userPanicEvent->SetEventValue("PACKAGE_NAME", processName);
843     userPanicEvent->SetEventValue("PROCESS_NAME", processName);
844     userPanicEvent->SetEventValue("MSG", msg);
845     userPanicEvent->SetPrivacy(USER_PANIC_WARNING_PRIVACY);
846     userPanicEvent->SetLevel("CRITICAL");
847     userPanicEvent->SetTag("STABILITY");
848 
849     auto context = GetHiviewContext();
850     if (context != nullptr) {
851         auto seq = context->GetPipelineSequenceByName("EventloggerPipeline");
852         userPanicEvent->SetPipelineInfo("EventloggerPipeline", seq);
853         userPanicEvent->OnContinue();
854     }
855 }
856 #endif
857 
CheckProcessRepeatFreeze(const std::string & eventName,long pid)858 bool EventLogger::CheckProcessRepeatFreeze(const std::string& eventName, long pid)
859 {
860     if (eventName == "THREAD_BLOCK_6S" || eventName == "APP_INPUT_BLOCK") {
861         long lastPid = lastPid_;
862         std::string lastEventName = lastEventName_;
863         lastPid_ = pid;
864         lastEventName_ = eventName;
865         if (lastPid == pid) {
866             HIVIEW_LOGI("eventName=%{public}s, pid=%{public}ld has happened", lastEventName.c_str(), pid);
867             return true;
868         }
869     }
870     return false;
871 }
872 
CheckEventOnContinue(std::shared_ptr<SysEvent> event)873 void EventLogger::CheckEventOnContinue(std::shared_ptr<SysEvent> event)
874 {
875     event->ResetPendingStatus();
876     event->OnContinue();
877 }
878 
OnLoad()879 void EventLogger::OnLoad()
880 {
881     HIVIEW_LOGI("EventLogger OnLoad.");
882     SetName("EventLogger");
883     SetVersion("1.0");
884     logStore_->SetMaxSize(MAX_FOLDER_SIZE);
885     logStore_->SetMinKeepingFileNumber(MAX_FILE_NUM);
886     LogStoreEx::LogFileComparator comparator = [this](const LogFile &lhs, const LogFile &rhs) {
887         return rhs < lhs;
888     };
889     logStore_->SetLogFileComparator(comparator);
890     logStore_->Init();
891     threadLoop_ = GetWorkLoop();
892 
893     EventLoggerConfig logConfig;
894     eventLoggerConfig_ = logConfig.GetConfig();
895 
896     activeKeyEvent_ = std::make_unique<ActiveKeyEvent>();
897     activeKeyEvent_ ->Init(logStore_);
898     FreezeCommon freezeCommon;
899     if (!freezeCommon.Init()) {
900         HIVIEW_LOGE("FreezeCommon filed.");
901         return;
902     }
903 
904     std::set<std::string> freezeeventNames = freezeCommon.GetPrincipalStringIds();
905     std::unordered_set<std::string> eventNames;
906     for (auto& i : freezeeventNames) {
907         eventNames.insert(i);
908     }
909     auto context = GetHiviewContext();
910     if (context != nullptr) {
911         auto plugin = context->GetPluginByName("FreezeDetectorPlugin");
912         if (plugin == nullptr) {
913             HIVIEW_LOGE("freeze_detecotr plugin is null.");
914             return;
915         }
916         HIVIEW_LOGI("plugin: %{public}s.", plugin->GetName().c_str());
917         context->AddDispatchInfo(plugin, {}, eventNames, {}, {});
918 
919         auto ptr = std::static_pointer_cast<EventLogger>(shared_from_this());
920         context->RegisterUnorderedEventListener(ptr);
921         AddListenerInfo(Event::MessageType::PLUGIN_MAINTENANCE);
922     }
923 
924     GetCmdlineContent();
925     GetRebootReasonConfig();
926 
927     freezeCommon_ = std::make_shared<FreezeCommon>();
928     if (freezeCommon_->Init() && freezeCommon_ != nullptr && freezeCommon_->GetFreezeRuleCluster() != nullptr) {
929         dbHelper_ = std::make_unique<DBHelper>(freezeCommon_);
930     }
931 }
932 
OnUnload()933 void EventLogger::OnUnload()
934 {
935     HIVIEW_LOGD("called");
936 #ifdef WINDOW_MANAGER_ENABLE
937     EventFocusListener::UnRegisterFocusListener();
938 #endif
939 }
940 
GetListenerName()941 std::string EventLogger::GetListenerName()
942 {
943     return "EventLogger";
944 }
945 
OnUnorderedEvent(const Event & msg)946 void EventLogger::OnUnorderedEvent(const Event& msg)
947 {
948     if (CanProcessRebootEvent(msg)) {
949         auto task = [this] { this->ProcessRebootEvent(); };
950         threadLoop_->AddEvent(nullptr, nullptr, task);
951     }
952 }
953 
CanProcessRebootEvent(const Event & event)954 bool EventLogger::CanProcessRebootEvent(const Event& event)
955 {
956     return (event.messageType_ == Event::MessageType::PLUGIN_MAINTENANCE) &&
957         (event.eventId_ == Event::EventId::PLUGIN_LOADED);
958 }
959 
ProcessRebootEvent()960 void EventLogger::ProcessRebootEvent()
961 {
962     if (GetRebootReason() != LONG_PRESS) {
963         return;
964     }
965 
966     auto event = std::make_shared<SysEvent>("EventLogger", nullptr, "");
967 
968     if (event == nullptr) {
969         HIVIEW_LOGW("event is null.");
970         return;
971     }
972 
973     event->domain_ = DOMAIN_LONGPRESS;
974     event->eventName_ = STRINGID_LONGPRESS;
975     event->happenTime_ = TimeUtil::GetMilliseconds();
976     event->messageType_ = Event::MessageType::SYS_EVENT;
977     event->SetEventValue(EventStore::EventCol::DOMAIN, DOMAIN_LONGPRESS);
978     event->SetEventValue(EventStore::EventCol::NAME, STRINGID_LONGPRESS);
979     event->SetEventValue(EventStore::EventCol::TYPE, 1);
980     event->SetEventValue(EventStore::EventCol::TS, TimeUtil::GetMilliseconds());
981     event->SetEventValue(EventStore::EventCol::TZ, TimeUtil::GetTimeZone());
982     event->SetEventValue("PID", 0);
983     event->SetEventValue("UID", 0);
984     event->SetEventValue("PACKAGE_NAME", STRINGID_LONGPRESS);
985     event->SetEventValue("PROCESS_NAME", STRINGID_LONGPRESS);
986     event->SetEventValue("MSG", STRINGID_LONGPRESS);
987     event->SetPrivacy(LONGPRESS_PRIVACY);
988     event->SetLevel(LONGPRESS_LEVEL);
989 
990     auto context = GetHiviewContext();
991     if (context != nullptr) {
992         auto seq = context->GetPipelineSequenceByName("EventloggerPipeline");
993         event->SetPipelineInfo("EventloggerPipeline", seq);
994         event->OnContinue();
995     }
996 }
997 
GetRebootReason() const998 std::string EventLogger::GetRebootReason() const
999 {
1000     std::string reboot = "";
1001     std::string reset = "";
1002     if (GetMatchString(cmdlineContent_, reboot, REBOOT_REASON + PATTERN_WITHOUT_SPACE) &&
1003         GetMatchString(cmdlineContent_, reset, NORMAL_RESET_TYPE + PATTERN_WITHOUT_SPACE)) {
1004             if (std::any_of(rebootReasons_.begin(), rebootReasons_.end(), [&reboot, &reset](auto& reason) {
1005                 return (reason == reboot || reason == reset);
1006             })) {
1007                 HIVIEW_LOGI("get reboot reason: LONG_PRESS.");
1008                 return LONG_PRESS;
1009             }
1010         }
1011     return "";
1012 }
1013 
GetCmdlineContent()1014 void EventLogger::GetCmdlineContent()
1015 {
1016     if (FileUtil::LoadStringFromFile(cmdlinePath_, cmdlineContent_) == false) {
1017         HIVIEW_LOGE("failed to read cmdline:%{public}s.", cmdlinePath_.c_str());
1018     }
1019 }
1020 
GetRebootReasonConfig()1021 void EventLogger::GetRebootReasonConfig()
1022 {
1023     rebootReasons_.clear();
1024     if (rebootReasons_.size() == 0) {
1025         rebootReasons_.push_back(AP_S_PRESS6S);
1026     }
1027 }
1028 
GetMatchString(const std::string & src,std::string & dst,const std::string & pattern) const1029 bool EventLogger::GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const
1030 {
1031     std::regex reg(pattern);
1032     std::smatch result;
1033     if (std::regex_search(src, result, reg)) {
1034         dst = StringUtil::TrimStr(result[1], '\n');
1035         return true;
1036     }
1037     return false;
1038 }
1039 } // namespace HiviewDFX
1040 } // namespace OHOS
1041