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: 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 = "Binder catcher stacktrace, ";
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 size_t firstLineIndex = line.find("\n");
648 std::string firstLine = (firstLineIndex != std::string::npos) ? line.substr(0, firstLineIndex) : tags;
649 stack = "";
650 kernelStack = "";
651 GetNoJsonStack(stack, line, kernelStack, false);
652 binderPeerStack += kernelStack;
653 if (line != "[]") {
654 stack = firstLine + "\n" + stack;
655 }
656 oss << stack << std::endl;
657 }
658 binderInfo = oss.str();
659 }
660
WriteFreezeJsonInfo(int fd,int jsonFd,std::shared_ptr<SysEvent> event)661 bool EventLogger::WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event)
662 {
663 std::string msg = StringUtil::ReplaceStr(event->GetEventValue("MSG"), "\\n", "\n");
664 std::string stack;
665 std::string binderInfo = event -> GetEventValue("BINDER_INFO");
666 if (FreezeJsonUtil::IsAppFreeze(event -> eventName_)) {
667 std::string kernelStack = "";
668 GetAppFreezeStack(jsonFd, event, stack, msg, kernelStack);
669 if (!binderInfo.empty() && jsonFd >= 0) {
670 HIVIEW_LOGI("Current binderInfo is? binderInfo:%{public}s", binderInfo.c_str());
671 if (FileUtil::FileExists(binderInfo)) {
672 binderInfo = GetAppFreezeFile(binderInfo);
673 }
674 std::string binderInfoJsonStr;
675 ParsePeerBinder(binderInfo, binderInfoJsonStr);
676 FreezeJsonUtil::WriteKeyValue(jsonFd, "peer_binder", binderInfoJsonStr);
677 ParsePeerStack(binderInfo, kernelStack);
678 }
679 WriteKernelStackToFile(event, fd, kernelStack);
680 } else {
681 stack = event->GetEventValue("STACK");
682 HIVIEW_LOGI("Current stack is? stack:%{public}s", stack.c_str());
683 if (FileUtil::FileExists(stack)) {
684 stack = GetAppFreezeFile(stack);
685 std::string tempStack = "";
686 std::string kernelStack = "";
687 GetNoJsonStack(tempStack, stack, kernelStack, false);
688 WriteKernelStackToFile(event, fd, kernelStack);
689 stack = tempStack;
690 }
691 GetFailedDumpStackMsg(stack, event);
692 }
693
694 std::ostringstream oss;
695 oss << "MSG = " << msg << std::endl;
696 if (!stack.empty()) {
697 oss << StringUtil::UnescapeJsonStringValue(stack) << std::endl;
698 }
699 if (!binderInfo.empty()) {
700 oss << StringUtil::UnescapeJsonStringValue(binderInfo) << std::endl;
701 }
702 FileUtil::SaveStringToFd(fd, oss.str());
703 WriteCallStack(event, fd);
704 return true;
705 }
706
GetFailedDumpStackMsg(std::string & stack,std::shared_ptr<SysEvent> event)707 void EventLogger::GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event)
708 {
709 std::string failedStackStart = " Failed to dump stacktrace for ";
710 if (dbHelper_ != nullptr && stack.size() >= failedStackStart.size() &&
711 !stack.compare(0, failedStackStart.size(), failedStackStart) &&
712 stack.find("syscall SIGDUMP error") != std::string::npos) {
713 long pid = event->GetEventIntValue("PID") ? event->GetEventIntValue("PID") : event->GetPid();
714 std::string packageName = event->GetEventValue("PACKAGE_NAME").empty() ?
715 event->GetEventValue("PROCESS_NAME") : event->GetEventValue("PACKAGE_NAME");
716
717 std::vector<WatchPoint> list;
718 FreezeResult freezeResult(0, "FRAMEWORK", "PROCESS_KILL");
719 freezeResult.SetSamePackage("true");
720 DBHelper::WatchParams params = {pid, packageName};
721 dbHelper_->SelectEventFromDB(event->happenTime_ - QUERY_PROCESS_KILL_INTERVAL, event->happenTime_, list,
722 params, freezeResult);
723 std::string appendStack = "";
724 std::for_each(list.begin(), list.end(), [&appendStack] (const WatchPoint& watchPoint) {
725 appendStack += "\n" + watchPoint.GetMsg();
726 });
727 stack += appendStack.empty() ? "\ncan not get process kill reason" : "\nprocess may be killed by : "
728 + appendStack;
729 }
730 }
731
JudgmentRateLimiting(std::shared_ptr<SysEvent> event)732 bool EventLogger::JudgmentRateLimiting(std::shared_ptr<SysEvent> event)
733 {
734 int32_t interval = event->GetIntValue("eventLog_interval");
735 if (interval == 0) {
736 return true;
737 }
738
739 int64_t pid = event->GetEventIntValue("PID");
740 pid = pid ? pid : event->GetPid();
741 std::string eventName = event->eventName_;
742 std::string eventPid = std::to_string(pid);
743
744 intervalMutex_.lock();
745 std::time_t now = std::time(0);
746 for (auto it = eventTagTime_.begin(); it != eventTagTime_.end();) {
747 if (it->first.find(eventName) != it->first.npos) {
748 if ((now - it->second) >= interval) {
749 it = eventTagTime_.erase(it);
750 continue;
751 }
752 }
753 ++it;
754 }
755
756 std::string tagTimeName = eventName + eventPid;
757 auto it = eventTagTime_.find(tagTimeName);
758 if (it != eventTagTime_.end()) {
759 if ((now - it->second) < interval) {
760 HIVIEW_LOGE("event: id:0x%{public}d, eventName:%{public}s pid:%{public}s. \
761 interval:%{public}" PRId32 " There's not enough interval",
762 event->eventId_, eventName.c_str(), eventPid.c_str(), interval);
763 intervalMutex_.unlock();
764 return false;
765 }
766 }
767 eventTagTime_[tagTimeName] = now;
768 HIVIEW_LOGD("event: id:0x%{public}d, eventName:%{public}s pid:%{public}s. \
769 interval:%{public}" PRId32 " normal interval",
770 event->eventId_, eventName.c_str(), eventPid.c_str(), interval);
771 intervalMutex_.unlock();
772 return true;
773 }
774
UpdateDB(std::shared_ptr<SysEvent> event,std::string logFile)775 bool EventLogger::UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile)
776 {
777 if (logFile == "nolog") {
778 HIVIEW_LOGI("set info_ with nolog into db.");
779 event->SetEventValue(EventStore::EventCol::INFO, "nolog", false);
780 } else {
781 auto logPath = R"~(logPath:)~" + LOGGER_EVENT_LOG_PATH + "/" + logFile;
782 event->SetEventValue(EventStore::EventCol::INFO, logPath, true);
783 }
784 return true;
785 }
786
IsHandleAppfreeze(std::shared_ptr<SysEvent> event)787 bool EventLogger::IsHandleAppfreeze(std::shared_ptr<SysEvent> event)
788 {
789 std::string bundleName = event->GetEventValue("PACKAGE_NAME");
790 if (bundleName.empty()) {
791 bundleName = event->GetEventValue("MODULE_NAME");
792 }
793 if (bundleName.empty()) {
794 return true;
795 }
796
797 const int buffSize = 128;
798 char paramOutBuff[buffSize] = {0};
799 GetParameter("hiviewdfx.appfreeze.filter_bundle_name", "", paramOutBuff, buffSize - 1);
800
801 std::string str(paramOutBuff);
802 if (str.find(bundleName) != std::string::npos) {
803 HIVIEW_LOGW("appfreeze filtration %{public}s.", bundleName.c_str());
804 return false;
805 }
806 return true;
807 }
808
809 #ifdef WINDOW_MANAGER_ENABLE
ReportUserPanicWarning(std::shared_ptr<SysEvent> event,long pid)810 void EventLogger::ReportUserPanicWarning(std::shared_ptr<SysEvent> event, long pid)
811 {
812 if (event->eventName_ == "FREQUENT_CLICK_WARNING") {
813 if (event->happenTime_ - EventFocusListener::lastChangedTime_ <= CLICK_FREEZE_TIME_LIMIT) {
814 return;
815 }
816 } else {
817 backTimes_.push_back(event->happenTime_);
818 if (backTimes_.size() < BACK_FREEZE_COUNT_LIMIT) {
819 return;
820 }
821 if ((event->happenTime_ - backTimes_[0] <= BACK_FREEZE_TIME_LIMIT) &&
822 (event->happenTime_ - EventFocusListener::lastChangedTime_ > BACK_FREEZE_TIME_LIMIT)) {
823 backTimes_.clear();
824 } else {
825 backTimes_.erase(backTimes_.begin(), backTimes_.end() - (BACK_FREEZE_COUNT_LIMIT - 1));
826 return;
827 }
828 }
829
830 auto userPanicEvent = std::make_shared<SysEvent>("EventLogger", nullptr, "");
831
832 std::string processName = (event->eventName_ == "FREQUENT_CLICK_WARNING") ? event->GetEventValue("PROCESS_NAME") :
833 event->GetEventValue("PNAMEID");
834 std::string msg = (event->eventName_ == "FREQUENT_CLICK_WARNING") ? "frequent click" : "gesture navigation back";
835
836 userPanicEvent->domain_ = "FRAMEWORK";
837 userPanicEvent->eventName_ = "USER_PANIC_WARNING";
838 userPanicEvent->happenTime_ = TimeUtil::GetMilliseconds();
839 userPanicEvent->messageType_ = Event::MessageType::SYS_EVENT;
840 userPanicEvent->SetEventValue(EventStore::EventCol::DOMAIN, "FRAMEWORK");
841 userPanicEvent->SetEventValue(EventStore::EventCol::NAME, "USER_PANIC_WARNING");
842 userPanicEvent->SetEventValue(EventStore::EventCol::TYPE, 1);
843 userPanicEvent->SetEventValue(EventStore::EventCol::TS, TimeUtil::GetMilliseconds());
844 userPanicEvent->SetEventValue(EventStore::EventCol::TZ, TimeUtil::GetTimeZone());
845 userPanicEvent->SetEventValue("PID", pid);
846 userPanicEvent->SetEventValue("UID", 0);
847 userPanicEvent->SetEventValue("PACKAGE_NAME", processName);
848 userPanicEvent->SetEventValue("PROCESS_NAME", processName);
849 userPanicEvent->SetEventValue("MSG", msg);
850 userPanicEvent->SetPrivacy(USER_PANIC_WARNING_PRIVACY);
851 userPanicEvent->SetLevel("CRITICAL");
852 userPanicEvent->SetTag("STABILITY");
853
854 auto context = GetHiviewContext();
855 if (context != nullptr) {
856 auto seq = context->GetPipelineSequenceByName("EventloggerPipeline");
857 userPanicEvent->SetPipelineInfo("EventloggerPipeline", seq);
858 userPanicEvent->OnContinue();
859 }
860 }
861 #endif
862
CheckProcessRepeatFreeze(const std::string & eventName,long pid)863 bool EventLogger::CheckProcessRepeatFreeze(const std::string& eventName, long pid)
864 {
865 if (eventName == "THREAD_BLOCK_6S" || eventName == "APP_INPUT_BLOCK") {
866 long lastPid = lastPid_;
867 std::string lastEventName = lastEventName_;
868 lastPid_ = pid;
869 lastEventName_ = eventName;
870 if (lastPid == pid) {
871 HIVIEW_LOGI("eventName=%{public}s, pid=%{public}ld has happened", lastEventName.c_str(), pid);
872 return true;
873 }
874 }
875 return false;
876 }
877
CheckEventOnContinue(std::shared_ptr<SysEvent> event)878 void EventLogger::CheckEventOnContinue(std::shared_ptr<SysEvent> event)
879 {
880 event->ResetPendingStatus();
881 event->OnContinue();
882 }
883
OnLoad()884 void EventLogger::OnLoad()
885 {
886 HIVIEW_LOGI("EventLogger OnLoad.");
887 SetName("EventLogger");
888 SetVersion("1.0");
889 logStore_->SetMaxSize(MAX_FOLDER_SIZE);
890 logStore_->SetMinKeepingFileNumber(MAX_FILE_NUM);
891 LogStoreEx::LogFileComparator comparator = [this](const LogFile &lhs, const LogFile &rhs) {
892 return rhs < lhs;
893 };
894 logStore_->SetLogFileComparator(comparator);
895 logStore_->Init();
896 threadLoop_ = GetWorkLoop();
897
898 EventLoggerConfig logConfig;
899 eventLoggerConfig_ = logConfig.GetConfig();
900
901 activeKeyEvent_ = std::make_unique<ActiveKeyEvent>();
902 activeKeyEvent_ ->Init(logStore_);
903 FreezeCommon freezeCommon;
904 if (!freezeCommon.Init()) {
905 HIVIEW_LOGE("FreezeCommon filed.");
906 return;
907 }
908
909 std::set<std::string> freezeeventNames = freezeCommon.GetPrincipalStringIds();
910 std::unordered_set<std::string> eventNames;
911 for (auto& i : freezeeventNames) {
912 eventNames.insert(i);
913 }
914 auto context = GetHiviewContext();
915 if (context != nullptr) {
916 auto plugin = context->GetPluginByName("FreezeDetectorPlugin");
917 if (plugin == nullptr) {
918 HIVIEW_LOGE("freeze_detecotr plugin is null.");
919 return;
920 }
921 HIVIEW_LOGI("plugin: %{public}s.", plugin->GetName().c_str());
922 context->AddDispatchInfo(plugin, {}, eventNames, {}, {});
923
924 auto ptr = std::static_pointer_cast<EventLogger>(shared_from_this());
925 context->RegisterUnorderedEventListener(ptr);
926 AddListenerInfo(Event::MessageType::PLUGIN_MAINTENANCE);
927 }
928
929 GetCmdlineContent();
930 GetRebootReasonConfig();
931
932 freezeCommon_ = std::make_shared<FreezeCommon>();
933 if (freezeCommon_->Init() && freezeCommon_ != nullptr && freezeCommon_->GetFreezeRuleCluster() != nullptr) {
934 dbHelper_ = std::make_unique<DBHelper>(freezeCommon_);
935 }
936 }
937
OnUnload()938 void EventLogger::OnUnload()
939 {
940 HIVIEW_LOGD("called");
941 #ifdef WINDOW_MANAGER_ENABLE
942 EventFocusListener::UnRegisterFocusListener();
943 #endif
944 }
945
GetListenerName()946 std::string EventLogger::GetListenerName()
947 {
948 return "EventLogger";
949 }
950
OnUnorderedEvent(const Event & msg)951 void EventLogger::OnUnorderedEvent(const Event& msg)
952 {
953 if (CanProcessRebootEvent(msg)) {
954 auto task = [this] { this->ProcessRebootEvent(); };
955 threadLoop_->AddEvent(nullptr, nullptr, task);
956 }
957 }
958
CanProcessRebootEvent(const Event & event)959 bool EventLogger::CanProcessRebootEvent(const Event& event)
960 {
961 return (event.messageType_ == Event::MessageType::PLUGIN_MAINTENANCE) &&
962 (event.eventId_ == Event::EventId::PLUGIN_LOADED);
963 }
964
ProcessRebootEvent()965 void EventLogger::ProcessRebootEvent()
966 {
967 if (GetRebootReason() != LONG_PRESS) {
968 return;
969 }
970
971 auto event = std::make_shared<SysEvent>("EventLogger", nullptr, "");
972
973 if (event == nullptr) {
974 HIVIEW_LOGW("event is null.");
975 return;
976 }
977
978 event->domain_ = DOMAIN_LONGPRESS;
979 event->eventName_ = STRINGID_LONGPRESS;
980 event->happenTime_ = TimeUtil::GetMilliseconds();
981 event->messageType_ = Event::MessageType::SYS_EVENT;
982 event->SetEventValue(EventStore::EventCol::DOMAIN, DOMAIN_LONGPRESS);
983 event->SetEventValue(EventStore::EventCol::NAME, STRINGID_LONGPRESS);
984 event->SetEventValue(EventStore::EventCol::TYPE, 1);
985 event->SetEventValue(EventStore::EventCol::TS, TimeUtil::GetMilliseconds());
986 event->SetEventValue(EventStore::EventCol::TZ, TimeUtil::GetTimeZone());
987 event->SetEventValue("PID", 0);
988 event->SetEventValue("UID", 0);
989 event->SetEventValue("PACKAGE_NAME", STRINGID_LONGPRESS);
990 event->SetEventValue("PROCESS_NAME", STRINGID_LONGPRESS);
991 event->SetEventValue("MSG", STRINGID_LONGPRESS);
992 event->SetPrivacy(LONGPRESS_PRIVACY);
993 event->SetLevel(LONGPRESS_LEVEL);
994
995 auto context = GetHiviewContext();
996 if (context != nullptr) {
997 auto seq = context->GetPipelineSequenceByName("EventloggerPipeline");
998 event->SetPipelineInfo("EventloggerPipeline", seq);
999 event->OnContinue();
1000 }
1001 }
1002
GetRebootReason() const1003 std::string EventLogger::GetRebootReason() const
1004 {
1005 std::string reboot = "";
1006 std::string reset = "";
1007 if (GetMatchString(cmdlineContent_, reboot, REBOOT_REASON + PATTERN_WITHOUT_SPACE) &&
1008 GetMatchString(cmdlineContent_, reset, NORMAL_RESET_TYPE + PATTERN_WITHOUT_SPACE)) {
1009 if (std::any_of(rebootReasons_.begin(), rebootReasons_.end(), [&reboot, &reset](auto& reason) {
1010 return (reason == reboot || reason == reset);
1011 })) {
1012 HIVIEW_LOGI("get reboot reason: LONG_PRESS.");
1013 return LONG_PRESS;
1014 }
1015 }
1016 return "";
1017 }
1018
GetCmdlineContent()1019 void EventLogger::GetCmdlineContent()
1020 {
1021 if (FileUtil::LoadStringFromFile(cmdlinePath_, cmdlineContent_) == false) {
1022 HIVIEW_LOGE("failed to read cmdline:%{public}s.", cmdlinePath_.c_str());
1023 }
1024 }
1025
GetRebootReasonConfig()1026 void EventLogger::GetRebootReasonConfig()
1027 {
1028 rebootReasons_.clear();
1029 if (rebootReasons_.size() == 0) {
1030 rebootReasons_.push_back(AP_S_PRESS6S);
1031 }
1032 }
1033
GetMatchString(const std::string & src,std::string & dst,const std::string & pattern) const1034 bool EventLogger::GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const
1035 {
1036 std::regex reg(pattern);
1037 std::smatch result;
1038 if (std::regex_search(src, result, reg)) {
1039 dst = StringUtil::TrimStr(result[1], '\n');
1040 return true;
1041 }
1042 return false;
1043 }
1044 } // namespace HiviewDFX
1045 } // namespace OHOS
1046