• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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_log_task.h"
16 
17 #include <unistd.h>
18 
19 #include "common_utils.h"
20 #include "hiview_logger.h"
21 #include "parameter_ex.h"
22 #include "securec.h"
23 #include "shell_catcher.h"
24 #include "string_util.h"
25 #include "time_util.h"
26 #include "freeze_common.h"
27 
28 #ifdef STACKTRACE_CATCHER_ENABLE
29 #include "open_stacktrace_catcher.h"
30 #endif // STACKTRACE_CATCHER_ENABLE
31 
32 #ifdef BINDER_CATCHER_ENABLE
33 #include "binder_catcher.h"
34 #include "peer_binder_catcher.h"
35 #endif // BINDER_CATCHER_ENABLE
36 
37 #ifdef DMESG_CATCHER_ENABLE
38 #include "dmesg_catcher.h"
39 #endif // DMESG_CATCHER_ENABLE
40 
41 #ifdef HITRACE_CATCHER_ENABLE
42 #include "log_catcher_utils.h"
43 #endif // HITRACE_CATCHER_ENABLE
44 
45 #ifdef USAGE_CATCHER_ENABLE
46 #include "memory_catcher.h"
47 #include "cpu_core_info_catcher.h"
48 #endif // USAGE_CATCHER_ENABLE
49 
50 #ifdef OTHER_CATCHER_ENABLE
51 #include "ffrt_catcher.h"
52 #endif // OTHER_CATCHER_ENABLE
53 #include "thermal_info_catcher.h"
54 #include "summary_log_info_catcher.h"
55 
56 namespace OHOS {
57 namespace HiviewDFX {
58 namespace {
59     constexpr int BP_CMD_PERF_TYPE_INDEX = 2;
60     constexpr int BP_CMD_LAYER_INDEX = 1;
61     constexpr size_t BP_CMD_SZ = 3;
62     constexpr size_t FAULTTIME_STR_SIZE = 19;
63     constexpr size_t FAULTTIME_ONE_INDEX = 4;
64     constexpr size_t FAULTTIME_TWO_INDEX = 7;
65     constexpr size_t FAULTTIME_THREE_INDEX = 10;
66     constexpr size_t FAULTTIME_FOUR_INDEX = 13;
67     constexpr size_t FAULTTIME_FIVE_INDEX = 16;
68     const char* SYSTEM_STACK[] = { "foundation", "render_service" };
69     constexpr int TRACE_OUT_OF_TIME = 30; // 30s
70     constexpr int DELAY_OUT_OF_TIME = 15; // 15s
71     constexpr int DEFAULT_LOG_SIZE = 3 * 1024 * 1024; // 3M
72     constexpr uint64_t MILLISEC_TO_SEC = 1000;
73 }
74 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-EventLogTask");
EventLogTask(int fd,int jsonFd,std::shared_ptr<SysEvent> event)75 EventLogTask::EventLogTask(int fd, int jsonFd, std::shared_ptr<SysEvent> event)
76     : targetFd_(fd),
77       targetJsonFd_(jsonFd),
78       event_(event),
79       maxLogSize_(DEFAULT_LOG_SIZE),
80       taskLogSize_(0),
81       status_(Status::TASK_RUNNABLE)
82 {
83     int pid = event_->GetEventIntValue("PID");
84     pid_ = pid ? pid : event_->GetPid();
85 #ifdef STACKTRACE_CATCHER_ENABLE
86     captureList_.insert(std::pair<std::string, capture>("s", [this] { this->AppStackCapture(); }));
87     captureList_.insert(std::pair<std::string, capture>("S", [this] { this->SystemStackCapture(); }));
88 #endif // STACKTRACE_CATCHER_ENABLE
89 #ifdef BINDER_CATCHER_ENABLE
90     captureList_.insert(std::pair<std::string, capture>("b", [this] { this->BinderLogCapture(); }));
91 #endif // BINDER_CATCHER_ENABLE
92 #ifdef OTHER_CATCHER_ENABLE
93     captureList_.insert(std::pair<std::string, capture>("ffrt", [this] { this->FfrtCapture(); }));
94 #endif // OTHER_CATCHER_ENABLE
95 #ifdef USAGE_CATCHER_ENABLE
96     captureList_.insert(std::pair<std::string, capture>("cmd:m", [this] { this->MemoryUsageCapture(); }));
97     captureList_.insert(std::pair<std::string, capture>("cmd:c", [this] { this->CpuUsageCapture(); }));
98     captureList_.insert(std::pair<std::string, capture>("cmd:w", [this] { this->WMSUsageCapture(); }));
99     captureList_.insert(std::pair<std::string, capture>("cmd:a", [this] { this->AMSUsageCapture(); }));
100     captureList_.insert(std::pair<std::string, capture>("cmd:p", [this] { this->PMSUsageCapture(); }));
101     captureList_.insert(std::pair<std::string, capture>("cmd:d", [this] { this->DPMSUsageCapture(); }));
102     captureList_.insert(std::pair<std::string, capture>("cmd:rs", [this] { this->RSUsageCapture(); }));
103     captureList_.insert(std::pair<std::string, capture>("cmd:cci", [this] { this->CpuCoreInfoCapture(); }));
104 #endif // USAGE_CATCHER_ENABLE
105 #ifdef OTHER_CATCHER_ENABLE
106     captureList_.insert(std::pair<std::string, capture>("cmd:mmi", [this] { this->MMIUsageCapture(); }));
107     captureList_.insert(std::pair<std::string, capture>("cmd:dms", [this] { this->DMSUsageCapture(); }));
108     captureList_.insert(std::pair<std::string, capture>("cmd:eec", [this] { this->EECStateCapture(); }));
109     captureList_.insert(std::pair<std::string, capture>("cmd:gec", [this] { this->GECStateCapture(); }));
110     captureList_.insert(std::pair<std::string, capture>("cmd:ui", [this] { this->UIStateCapture(); }));
111     captureList_.insert(std::pair<std::string, capture>("cmd:ss", [this] { this->Screenshot(); }));
112 #endif // OTHER_CATCHER_ENABLE
113 #ifdef HILOG_CATCHER_ENABLE
114     captureList_.insert(std::pair<std::string, capture>("T", [this] { this->HilogCapture(); }));
115     captureList_.insert(std::pair<std::string, capture>("T:power", [this] { this->HilogTagCapture(); }));
116     captureList_.insert(std::pair<std::string, capture>("t", [this] { this->LightHilogCapture(); }));
117 #endif // HILOG_CATCHER_ENABLE
118 #ifdef DMESG_CATCHER_ENABLE
119     captureList_.insert(std::pair<std::string, capture>("e",
120         [this] { this->DmesgCapture(false, DmesgCatcher::DMESG); }));
121     captureList_.insert(std::pair<std::string, capture>("k:SysRq",
122         [this] { this->DmesgCapture(false, DmesgCatcher::SYS_RQ); }));
123     captureList_.insert(std::pair<std::string, capture>("k:SysRqFile",
124         [this] { this->DmesgCapture(true, DmesgCatcher::SYS_RQ); }));
125     captureList_.insert(std::pair<std::string, capture>("k:HungTask",
126         [this] { this->DmesgCapture(false, DmesgCatcher::HUNG_TASK); }));
127     captureList_.insert(std::pair<std::string, capture>("k:HungTaskFile",
128         [this] { this->DmesgCapture(true, DmesgCatcher::HUNG_TASK); }));
129 #endif // DMESG_CATCHER_ENABLE
130     AddCapture();
131 }
132 
AddCapture()133 void EventLogTask::AddCapture()
134 {
135 #ifdef HITRACE_CATCHER_ENABLE
136     captureList_.insert(std::pair<std::string, capture>("tr",
137         [this] { this->HitraceCapture(Parameter::IsBetaVersion()); }));
138 #endif // HITRACE_CATCHER_ENABLE
139 #ifdef SCB_CATCHER_ENABLE
140     captureList_.insert(std::pair<std::string, capture>("cmd:scbCS",
141         [this] { this->SCBSessionCapture(); }));
142     captureList_.insert(std::pair<std::string, capture>("cmd:scbVP",
143         [this] { this->SCBViewParamCapture(); }));
144     captureList_.insert(std::pair<std::string, capture>("cmd:scbWMS",
145         [this] { this->SCBWMSCapture(); }));
146     captureList_.insert(std::pair<std::string, capture>("cmd:scbWMSEVT",
147         [this] { this->SCBWMSEVTCapture(); }));
148     captureList_.insert(std::pair<std::string, capture>("cmd:scbWMSV",
149         [this] { this->SCBWMSVCapture(); }));
150 #endif // SCB_CATCHER_ENABLE
151 #ifdef USAGE_CATCHER_ENABLE
152     captureList_.insert(std::pair<std::string, capture>("cmd:dam",
153         [this] { this->DumpAppMapCapture(); }));
154 #endif // USAGE_CATCHER_ENABLE
155 #ifdef HILOG_CATCHER_ENABLE
156     captureList_.insert(std::pair<std::string, capture>("t:input",
157         [this] { this->InputHilogCapture(); }));
158 #endif // HILOG_CATCHER_ENABLE
159 #ifdef STACKTRACE_CATCHER_ENABLE
160     captureList_.insert(std::pair<std::string, capture>("cmd:remoteS",
161         [this] { this->RemoteStackCapture(); }));
162     captureList_.insert(std::pair<std::string, capture>("GpuStack",
163         [this] { this->GetGPUProcessStack(); }));
164     captureList_.insert(std::pair<std::string, capture>("specificStack",
165         [this] { this->GetStackByProcessName(); }));
166 #endif // STACKTRACE_CATCHER_ENABLE
167     captureList_.insert(std::pair<std::string, capture>("hot",
168         [this] { this->GetThermalInfoCapture(); }));
169     captureList_.insert(std::pair<std::string, capture>("rve",
170         [this] { this->SaveRsVulKanError(); }));
171     captureList_.insert(std::pair<std::string, capture>("sli",
172         [this] { this->SaveSummaryLogInfo(); }));
173 }
174 
AddLog(const std::string & cmd)175 void EventLogTask::AddLog(const std::string &cmd)
176 {
177     if (tasks_.size() == 0) {
178         status_ = Status::TASK_RUNNABLE;
179     }
180 
181     if (captureList_.find(cmd) != captureList_.end()) {
182         captureList_[cmd]();
183         return;
184     }
185 #ifdef BINDER_CATCHER_ENABLE
186     PeerBinderCapture(cmd);
187 #endif // BINDER_CATCHER_ENABLE
188     catchedPids_.clear();
189 }
190 
SetFocusWindowId(const std::string & focusWindowId)191 void EventLogTask::SetFocusWindowId(const std::string& focusWindowId)
192 {
193     focusWindowId_ = focusWindowId;
194 }
195 
StartCompose()196 EventLogTask::Status EventLogTask::StartCompose()
197 {
198     // nothing to do, return success
199     if (status_ != Status::TASK_RUNNABLE) {
200         return status_;
201     }
202     status_ = Status::TASK_RUNNING;
203     // nothing to do, return success
204     if (tasks_.size() == 0) {
205         return Status::TASK_SUCCESS;
206     }
207 
208     auto dupedFd = dup(targetFd_);
209     int dupedJsonFd = -1;
210     if (targetJsonFd_ >= 0) {
211         dupedJsonFd = dup(targetJsonFd_);
212     }
213     uint32_t catcherIndex = 0;
214     for (auto& catcher : tasks_) {
215         catcherIndex++;
216         if (dupedFd < 0) {
217             status_ = Status::TASK_FAIL;
218             AddStopReason(targetFd_, catcher, "Fail to dup file descriptor, exit!");
219             return TASK_FAIL;
220         }
221         std::string description = catcher->GetDescription();
222         description.erase(description.find_last_not_of(" \n\r\t") + 1);
223         FreezeCommon::WriteTimeInfoToFd(dupedFd, description + " start time: ");
224         AddSeparator(dupedFd, catcher);
225         int curLogSize = catcher->Catch(dupedFd, dupedJsonFd);
226         if (catcher->name_ == "PeerBinderCatcher") {
227             terminalThreadStack_ = catcher->terminalBinder_.threadStack;
228         }
229         HIVIEW_LOGI("finish catcher: %{public}s, curLogSize: %{public}d", description.c_str(), curLogSize);
230         FreezeCommon::WriteTimeInfoToFd(dupedFd, description + " end time: ", false);
231         if (ShouldStopLogTask(dupedFd, catcherIndex, curLogSize, catcher)) {
232             break;
233         }
234     }
235     close(dupedFd);
236     if (dupedJsonFd >= 0) {
237         close(dupedJsonFd);
238     }
239     if (status_ == Status::TASK_RUNNING) {
240         status_ = Status::TASK_SUCCESS;
241     }
242     return status_;
243 }
244 
ShouldStopLogTask(int fd,uint32_t curTaskIndex,int curLogSize,std::shared_ptr<EventLogCatcher> catcher)245 bool EventLogTask::ShouldStopLogTask(int fd, uint32_t curTaskIndex, int curLogSize,
246     std::shared_ptr<EventLogCatcher> catcher)
247 {
248     if (status_ == Status::TASK_TIMEOUT) {
249         HIVIEW_LOGE("Break Log task, parent has timeout.");
250         return true;
251     }
252 
253     bool encounterErr = (curLogSize < 0);
254     bool hasFinished = (curTaskIndex == tasks_.size());
255     if (!encounterErr) {
256         taskLogSize_ += static_cast<uint32_t>(curLogSize);
257     }
258 
259     if (taskLogSize_ > maxLogSize_ && !hasFinished) {
260         AddStopReason(fd, catcher, "Exceed max log size");
261         status_ = Status::TASK_EXCEED_SIZE;
262         return true;
263     }
264 
265     if (encounterErr) {
266         AddStopReason(fd, catcher, "Log catcher not successful");
267         HIVIEW_LOGE("catcher %{public}s, Log catcher not successful", catcher->GetDescription().c_str());
268     }
269     return false;
270 }
271 
AddStopReason(int fd,std::shared_ptr<EventLogCatcher> catcher,const std::string & reason)272 void EventLogTask::AddStopReason(int fd, std::shared_ptr<EventLogCatcher> catcher, const std::string& reason)
273 {
274     char buf[BUF_SIZE_512] = {0};
275     int ret = -1;
276     if (catcher != nullptr) {
277         catcher->Stop();
278         // sleep 1s for syncing log to the fd, then we could append failure reason ?
279         sleep(1);
280         std::string summary = catcher->GetDescription();
281         ret = snprintf_s(buf, BUF_SIZE_512, BUF_SIZE_512 - 1, "\nTask stopped when running catcher:%s, Reason:%s \n",
282                          summary.c_str(), reason.c_str());
283     } else {
284         ret = snprintf_s(buf, BUF_SIZE_512, BUF_SIZE_512 - 1, "\nTask stopped, Reason:%s \n", reason.c_str());
285     }
286 
287     if (ret > 0) {
288         write(fd, buf, strnlen(buf, BUF_SIZE_512));
289         fsync(fd);
290     }
291 }
292 
AddSeparator(int fd,std::shared_ptr<EventLogCatcher> catcher) const293 void EventLogTask::AddSeparator(int fd, std::shared_ptr<EventLogCatcher> catcher) const
294 {
295     char buf[BUF_SIZE_512] = {0};
296     std::string summary = catcher->GetDescription();
297     if (summary.empty()) {
298         HIVIEW_LOGE("summary.empty() catcher is %{public}s", catcher->GetName().c_str());
299         return;
300     }
301 
302     if (catcher->GetName() == "PeerBinderCatcher" && Parameter::IsOversea()) {
303         summary = "binder info is not saved in oversea version\n";
304     }
305     int ret = snprintf_s(buf, BUF_SIZE_512, BUF_SIZE_512 - 1, "\n%s\n", summary.c_str());
306     if (ret > 0) {
307         write(fd, buf, strnlen(buf, BUF_SIZE_512));
308         fsync(fd);
309     }
310 }
311 
RecordCatchedPids(const std::string & packageName)312 void EventLogTask::RecordCatchedPids(const std::string& packageName)
313 {
314     int pid = CommonUtils::GetPidByName(packageName);
315     if (pid > 0) {
316         catchedPids_.insert(pid);
317     }
318 }
319 
GetTaskStatus() const320 EventLogTask::Status EventLogTask::GetTaskStatus() const
321 {
322     return status_;
323 }
324 
GetLogSize() const325 long EventLogTask::GetLogSize() const
326 {
327     return taskLogSize_;
328 }
329 
330 #ifdef STACKTRACE_CATCHER_ENABLE
AppStackCapture()331 void EventLogTask::AppStackCapture()
332 {
333     auto capture = std::make_shared<OpenStacktraceCatcher>();
334     capture->Initialize(event_->GetEventValue("PACKAGE_NAME"), pid_, 0);
335     tasks_.push_back(capture);
336 }
337 
SystemStackCapture()338 void EventLogTask::SystemStackCapture()
339 {
340     for (auto packageName : SYSTEM_STACK) {
341         auto capture = std::make_shared<OpenStacktraceCatcher>();
342         capture->Initialize(packageName, 0, 0);
343         RecordCatchedPids(packageName);
344         tasks_.push_back(capture);
345     }
346 }
347 
RemoteStackCapture()348 void EventLogTask::RemoteStackCapture()
349 {
350     auto capture = std::make_shared<OpenStacktraceCatcher>();
351     int32_t remotePid = event_->GetEventIntValue("REMOTE_PID");
352     capture->Initialize(event_->GetEventValue("PACKAGE_NAME"), remotePid, 0);
353     tasks_.push_back(capture);
354 }
355 
GetProcessStack(const std::string & processName)356 void EventLogTask::GetProcessStack(const std::string& processName)
357 {
358     auto capture = std::make_shared<OpenStacktraceCatcher>();
359     int pid = CommonUtils::GetPidByName(processName);
360     HIVIEW_LOGI("get pid of %{public}s: %{public}d.", processName.c_str(), pid);
361     if (pid != -1) {
362         capture->Initialize(processName, pid, 0);
363         tasks_.push_back(capture);
364     }
365 }
366 
GetGPUProcessStack()367 void EventLogTask::GetGPUProcessStack()
368 {
369     std::string processName = event_->GetEventValue("PACKAGE_NAME");
370     processName += ":gpu";
371     GetProcessStack(processName);
372 }
373 
GetStackByProcessName()374 void EventLogTask::GetStackByProcessName()
375 {
376     std::string processName = event_->GetEventValue("PROCESS_NAME");
377     if (!processName.empty()) {
378         GetProcessStack(processName);
379     }
380 }
381 #endif // STACKTRACE_CATCHER_ENABLE
382 
383 #ifdef BINDER_CATCHER_ENABLE
BinderLogCapture()384 void EventLogTask::BinderLogCapture()
385 {
386     auto capture = std::make_shared<BinderCatcher>();
387     capture->Initialize("", 0, 0);
388     tasks_.push_back(capture);
389 }
390 
PeerBinderCapture(const std::string & cmd)391 bool EventLogTask::PeerBinderCapture(const std::string &cmd)
392 {
393     auto find = cmd.find("pb");
394     if (find == cmd.npos) {
395         return false;
396     }
397 
398     std::vector<std::string> cmdList;
399     StringUtil::SplitStr(cmd, ":", cmdList, true);
400     if (cmdList.size() != BP_CMD_SZ || cmdList.front() != "pb") {
401         return false;
402     }
403 
404     auto capture = std::make_shared<PeerBinderCatcher>();
405     capture->Initialize(cmdList[BP_CMD_PERF_TYPE_INDEX],
406         StringUtil::StrToInt(cmdList[BP_CMD_LAYER_INDEX]), pid_);
407     capture->Init(event_, "", catchedPids_);
408     tasks_.push_back(capture);
409     return true;
410 }
411 #endif // BINDER_CATCHER_ENABLE
412 
413 #ifdef DMESG_CATCHER_ENABLE
DmesgCapture(bool writeNewFile,int type)414 void EventLogTask::DmesgCapture(bool writeNewFile, int type)
415 {
416     if (type == DmesgCatcher::DMESG && !Parameter::IsBetaVersion()) {
417         HIVIEW_LOGI("the dmesg cmd can only be executed in beta version");
418         return;
419     }
420 
421     auto capture = std::make_shared<DmesgCatcher>();
422     capture->Initialize("", writeNewFile, type);
423     capture->Init(event_);
424     if (!writeNewFile) {
425         tasks_.push_back(capture);
426         return;
427     }
428     int pid = -1;
429 #ifdef KERNELSTACK_CATCHER_ENABLE
430     if (type == DmesgCatcher::SYS_RQ) {
431         std::string processName = event_->GetEventValue("SPECIFICSTACK_NAME");
432         if (!processName.empty()) {
433             pid = CommonUtils::GetPidByName(processName);
434             HIVIEW_LOGI("processName:%{public}s, pid:%{public}d.", processName.c_str(), pid);
435         }
436     }
437 #endif //KERNELSTACK_CATCHER_ENABLE
438     capture->WriteNewFile(pid);
439 }
440 #endif // DMESG_CATCHER_ENABLE
441 
442 #ifdef HILOG_CATCHER_ENABLE
HilogCapture()443 void EventLogTask::HilogCapture()
444 {
445     if (!Parameter::IsBetaVersion()) {
446         HIVIEW_LOGI("the version is not a beta version");
447         return;
448     }
449     auto capture = std::make_shared<ShellCatcher>();
450     capture->Initialize("hilog -x", ShellCatcher::CATCHER_HILOG, 0);
451     tasks_.push_back(capture);
452 }
453 
HilogTagCapture()454 void EventLogTask::HilogTagCapture()
455 {
456     auto capture = std::make_shared<ShellCatcher>();
457     capture->Initialize("hilog -x", ShellCatcher::CATCHER_TAGHILOG, 0);
458     tasks_.push_back(capture);
459 }
460 
LightHilogCapture()461 void EventLogTask::LightHilogCapture()
462 {
463     auto capture = std::make_shared<ShellCatcher>();
464     capture->Initialize("hilog -z 1000 -P", ShellCatcher::CATCHER_LIGHT_HILOG, pid_);
465     tasks_.push_back(capture);
466 }
467 
InputHilogCapture()468 void EventLogTask::InputHilogCapture()
469 {
470     auto capture = std::make_shared<ShellCatcher>();
471     int32_t eventId = event_->GetEventIntValue("INPUT_ID");
472     if (eventId > 0) {
473         std::string cmd = "hilog -T InputKeyFlow -e " +
474             std::to_string(eventId) + " -x";
475         capture->Initialize(cmd, ShellCatcher::CATCHER_INPUT_EVENT_HILOG, eventId);
476     } else {
477         capture->Initialize("hilog -T InputKeyFlow -x", ShellCatcher::CATCHER_INPUT_HILOG,
478             pid_);
479     }
480     tasks_.push_back(capture);
481 }
482 #endif // HILOG_CATCHER_ENABLE
483 
484 #ifdef HITRACE_CATCHER_ENABLE
HitraceCapture(bool isBetaVersion)485 void EventLogTask::HitraceCapture(bool isBetaVersion)
486 {
487     uint64_t hitraceTime = GetFaultTime();
488     uint64_t currentTime = TimeUtil::GetMilliseconds() / MILLISEC_TO_SEC;
489     if (hitraceTime + TRACE_OUT_OF_TIME <= currentTime) {
490         hitraceTime = currentTime - DELAY_OUT_OF_TIME;
491     }
492 
493     bool grayscale = false;
494     std::string bundleName;
495     std::string eventName = event_->eventName_;
496     if (!isBetaVersion && (eventName == "THREAD_BLOCK_6S" || eventName == "LIFECYCLE_TIMEOUT" ||
497         eventName == "APP_INPUT_BLOCK")) {
498         grayscale = true;
499         bundleName = event_->GetEventValue("PACKAGE_NAME");
500         bundleName = bundleName.empty() ? event_->GetEventValue("PROCESS_NAME") : bundleName;
501     }
502     std::pair<std::string, std::vector<std::string>> result =
503         LogCatcherUtils::FreezeDumpTrace(hitraceTime, grayscale, bundleName);
504     event_->SetEventValue("TELEMETRY_ID", result.first);
505     if (!result.second.empty()) {
506         event_->SetEventValue("TRACE_NAME", result.second[0]);
507     } else if (isBetaVersion) {
508         event_->SetEventValue("TRACE_NAME", "dump trace failed in beta!");
509     } else if (!result.first.empty()) {
510         event_->SetEventValue("TRACE_NAME", "dump trace failed with grayscale!");
511     }
512 }
513 #endif // HITRACE_CATCHER_ENABLE
514 
515 #ifdef USAGE_CATCHER_ENABLE
MemoryUsageCapture()516 void EventLogTask::MemoryUsageCapture()
517 {
518     auto capture = std::make_shared<MemoryCatcher>();
519     capture->SetEvent(event_);
520     capture->Initialize("", 0, 0);
521     if (!memoryCatched_) {
522         tasks_.push_back(capture);
523         memoryCatched_ = true;
524     } else {
525         capture->CollectMemInfo();
526     }
527 }
528 
CpuUsageCapture()529 void EventLogTask::CpuUsageCapture()
530 {
531     auto capture = std::make_shared<ShellCatcher>();
532     capture->Initialize("hidumper --cpuusage", ShellCatcher::CATCHER_CPU, pid_);
533     tasks_.push_back(capture);
534 }
535 
WMSUsageCapture()536 void EventLogTask::WMSUsageCapture()
537 {
538     auto capture = std::make_shared<ShellCatcher>();
539     capture->Initialize("hidumper -s WindowManagerService -a -a", ShellCatcher::CATCHER_WMS, pid_);
540     tasks_.push_back(capture);
541 }
542 
AMSUsageCapture()543 void EventLogTask::AMSUsageCapture()
544 {
545     auto capture = std::make_shared<ShellCatcher>();
546     capture->Initialize("hidumper -s AbilityManagerService -a -a", ShellCatcher::CATCHER_AMS, pid_);
547     tasks_.push_back(capture);
548 }
549 
PMSUsageCapture()550 void EventLogTask::PMSUsageCapture()
551 {
552     auto capture = std::make_shared<ShellCatcher>();
553     capture->Initialize("hidumper -s PowerManagerService -a -s", ShellCatcher::CATCHER_PMS, pid_);
554     tasks_.push_back(capture);
555 }
556 
DPMSUsageCapture()557 void EventLogTask::DPMSUsageCapture()
558 {
559     auto capture = std::make_shared<ShellCatcher>();
560     capture->Initialize("hidumper -s DisplayPowerManagerService", ShellCatcher::CATCHER_DPMS, pid_);
561     tasks_.push_back(capture);
562 }
563 
RSUsageCapture()564 void EventLogTask::RSUsageCapture()
565 {
566     if (!Parameter::IsBetaVersion()) {
567         HIVIEW_LOGI("the version is not a beta version");
568         return;
569     }
570     auto capture = std::make_shared<ShellCatcher>();
571     capture->Initialize("hidumper -s RenderService -a allInfo", ShellCatcher::CATCHER_RS, pid_);
572     tasks_.push_back(capture);
573 }
574 
DumpAppMapCapture()575 void EventLogTask::DumpAppMapCapture()
576 {
577     auto capture = std::make_shared<ShellCatcher>();
578     capture->Initialize("hidumper -s 1910 -a DumpAppMap", ShellCatcher::CATCHER_DAM, pid_);
579     tasks_.push_back(capture);
580 }
581 
CpuCoreInfoCapture()582 void EventLogTask::CpuCoreInfoCapture()
583 {
584     auto capture = std::make_shared<CpuCoreInfoCatcher>();
585     capture->Initialize("", 0, pid_);
586     tasks_.push_back(capture);
587 }
588 #endif // USAGE_CATCHER_ENABLE
589 
590 #ifdef SCB_CATCHER_ENABLE
SCBSessionCapture()591 void EventLogTask::SCBSessionCapture()
592 {
593     auto capture = std::make_shared<ShellCatcher>();
594     capture->Initialize("hidumper -s 4606 -a '-b SCBScenePanel getContainerSession'",
595         ShellCatcher::CATCHER_SCBSESSION, pid_);
596     tasks_.push_back(capture);
597 }
598 
SCBViewParamCapture()599 void EventLogTask::SCBViewParamCapture()
600 {
601     auto capture = std::make_shared<ShellCatcher>();
602     capture->Initialize("hidumper -s 4606 -a '-b SCBScenePanel getViewParam'",
603         ShellCatcher::CATCHER_SCBVIEWPARAM, pid_);
604     tasks_.push_back(capture);
605 }
606 
SCBWMSCapture()607 void EventLogTask::SCBWMSCapture()
608 {
609     auto capture = std::make_shared<ShellCatcher>();
610     capture->SetEvent(event_);
611     if (focusWindowId_.empty()) {
612         HIVIEW_LOGE("dump simplify get focus window error");
613         return;
614     }
615     std::string cmd = "hidumper -s WindowManagerService -a -w " + focusWindowId_ + " -simplify";
616     capture->Initialize(cmd, ShellCatcher::CATCHER_SCBWMS, pid_);
617     capture->SetFocusWindowId(focusWindowId_);
618     tasks_.push_back(capture);
619 }
620 
SCBWMSEVTCapture()621 void EventLogTask::SCBWMSEVTCapture()
622 {
623     auto capture = std::make_shared<ShellCatcher>();
624     capture->SetEvent(event_);
625     if (focusWindowId_.empty()) {
626         HIVIEW_LOGE("dump event get focus window error");
627         return;
628     }
629     std::string cmd = "hidumper -s WindowManagerService -a -w " + focusWindowId_ + " -event";
630     capture->Initialize(cmd, ShellCatcher::CATCHER_SCBWMSEVT, pid_);
631     capture->SetFocusWindowId(focusWindowId_);
632     tasks_.push_back(capture);
633 }
634 
SCBWMSVCapture()635 void EventLogTask::SCBWMSVCapture()
636 {
637     auto capture = std::make_shared<ShellCatcher>();
638     capture->Initialize("hidumper -s WindowManagerService -a '-v all -simplify'",
639         ShellCatcher::CATCHER_SCBWMSV, pid_);
640     tasks_.push_back(capture);
641 }
642 #endif // SCB_CATCHER_ENABLE
643 
644 #ifdef OTHER_CATCHER_ENABLE
FfrtCapture()645 void EventLogTask::FfrtCapture()
646 {
647     if (pid_ > 0) {
648         auto capture = std::make_shared<FfrtCatcher>();
649         capture->Initialize("", pid_, 0);
650         tasks_.push_back(capture);
651     }
652 }
653 
MMIUsageCapture()654 void EventLogTask::MMIUsageCapture()
655 {
656     auto capture = std::make_shared<ShellCatcher>();
657     capture->Initialize("hidumper -s MultimodalInput -a -w", ShellCatcher::CATCHER_MMI, pid_);
658     tasks_.push_back(capture);
659 }
660 
DMSUsageCapture()661 void EventLogTask::DMSUsageCapture()
662 {
663     auto capture = std::make_shared<ShellCatcher>();
664     capture->Initialize("hidumper -s DisplayManagerService -a -a", ShellCatcher::CATCHER_DMS, pid_);
665     tasks_.push_back(capture);
666 }
667 
EECStateCapture()668 void EventLogTask::EECStateCapture()
669 {
670     auto capture = std::make_shared<ShellCatcher>();
671     capture->Initialize("hidumper -s 4606 -a '-b EventExclusiveCommander getAllEventExclusiveCaller'",
672         ShellCatcher::CATCHER_EEC, pid_);
673     tasks_.push_back(capture);
674 }
675 
GECStateCapture()676 void EventLogTask::GECStateCapture()
677 {
678     auto capture = std::make_shared<ShellCatcher>();
679     capture->Initialize("hidumper -s 4606 -a '-b SCBGestureManager getAllGestureEnableCaller'",
680         ShellCatcher::CATCHER_GEC, pid_);
681     tasks_.push_back(capture);
682 }
683 
UIStateCapture()684 void EventLogTask::UIStateCapture()
685 {
686     auto capture = std::make_shared<ShellCatcher>();
687     capture->Initialize("hidumper -s 4606 -a '-p 0'", ShellCatcher::CATCHER_UI, pid_);
688     tasks_.push_back(capture);
689 }
690 
Screenshot()691 void EventLogTask::Screenshot()
692 {
693     auto capture = std::make_shared<ShellCatcher>();
694     capture->Initialize("snapshot_display -f x.jpeg", ShellCatcher::CATCHER_SNAPSHOT, pid_);
695     tasks_.push_back(capture);
696 }
697 #endif // OTHER_CATCHER_ENABLE
698 
GetThermalInfoCapture()699 void EventLogTask::GetThermalInfoCapture()
700 {
701     auto capture = std::make_shared<ThermalInfoCatcher>();
702     capture->Initialize("", 0, pid_);
703     capture->SetEvent(event_);
704     tasks_.push_back(capture);
705 }
706 
SaveRsVulKanError()707 void EventLogTask::SaveRsVulKanError()
708 {
709     if (event_->eventName_ != "RS_VULKAN_ERROR") {
710         return;
711     }
712     long pid = event_->GetEventIntValue("PID") ? event_->GetEventIntValue("PID") : event_->GetPid();
713     std::string processName = CommonUtils::GetProcFullNameByPid(pid);
714     StringUtil::FormatProcessName(processName);
715     event_->SetEventValue("PROCESS_NAME", processName);
716     long appNodeId = event_->GetEventIntValue("APPNODEID");
717     std::string appNodeName = event_->GetEventValue("APPNODENAME");
718     long leashWindowId = event_->GetEventIntValue("LEASHWINDOWID");
719     std::string leashWindowName = event_->GetEventValue("LEASHWINDOWNAME");
720     std::string extInfo = event_->GetEventValue("EXT_INFO");
721 
722     std::string saveContent = "PID=" + std::to_string(pid) + "\nPROCESS_NAME=" + processName + "\nAPPNODEID=" +
723         std::to_string(appNodeId) + "\nAPPNODENAME" + appNodeName + "\nLEASHWINDOWID" + std::to_string(leashWindowId)
724         + "\nLEASHWINDOWNAME=" + leashWindowName + "\nEXT_INFO=" + extInfo + "\n";
725     FileUtil::SaveStringToFd(targetFd_, saveContent);
726 }
727 
SaveSummaryLogInfo()728 void EventLogTask::SaveSummaryLogInfo()
729 {
730     auto capture = std::make_shared<SummaryLogInfoCatcher>();
731     capture->Initialize("", 0, 0);
732     capture->SetFaultTime(static_cast<int64_t>(GetFaultTime()));
733     tasks_.push_back(capture);
734 }
735 
GetFaultTime()736 uint64_t EventLogTask::GetFaultTime()
737 {
738     std::lock_guard<ffrt::mutex> lock(faultTimeMutex_);
739     if (faultTime_ != 0) {
740         return faultTime_;
741     }
742 
743     std::string msg = event_->GetEventValue("MSG");
744     std::string faultTimeTag = "Fault time:";
745     size_t startIndex = msg.find(faultTimeTag);
746     if (startIndex != std::string::npos && msg.size() >= (startIndex + faultTimeTag.size() + FAULTTIME_STR_SIZE)) {
747         std::string faultTimeStr = msg.substr(startIndex + faultTimeTag.size(), FAULTTIME_STR_SIZE);
748         if (faultTimeStr[FAULTTIME_ONE_INDEX] == '/' && faultTimeStr[FAULTTIME_TWO_INDEX] == '/' &&
749             faultTimeStr[FAULTTIME_THREE_INDEX] == '-' && faultTimeStr[FAULTTIME_FOUR_INDEX] == ':' &&
750             faultTimeStr[FAULTTIME_FIVE_INDEX] == ':') {
751             faultTime_ = static_cast<uint64_t>(TimeUtil::StrToTimeStamp(faultTimeStr, "%Y/%m/%d-%H:%M:%S"));
752         }
753     }
754     faultTime_ = (faultTime_ == 0) ? event_->happenTime_ / MILLISEC_TO_SEC : faultTime_;
755     return faultTime_;
756 }
757 } // namespace HiviewDFX
758 } // namespace OHOS
759