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 #ifndef EVENT_LOGGER_EVENT_LOG_TASK_H
16 #define EVENT_LOGGER_EVENT_LOG_TASK_H
17 
18 #include <functional>
19 #include <memory>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include "event.h"
25 #include "singleton.h"
26 #include "sys_event.h"
27 
28 #include "event_log_catcher.h"
29 namespace OHOS {
30 namespace HiviewDFX {
31 class EventLogTask {
32     static constexpr int DEFAULT_LOG_SIZE = 1024 * 1024; // 1M
33 public:
34     enum Status {
35         TASK_RUNNABLE = 0,
36         TASK_RUNNING = 1,
37         TASK_SUCCESS = 2,
38         TASK_TIMEOUT = 3,
39         TASK_EXCEED_SIZE = 4,
40         TASK_SUB_TASK_FAIL = 5,
41         TASK_FAIL = 6,
42         TASK_DESTROY = 7,
43     };
44 
45     EventLogTask(int fd, int jsonFd, std::shared_ptr<SysEvent> event);
~EventLogTask()46     virtual ~EventLogTask() {};
47     void AddLog(const std::string &cmd);
48     EventLogTask::Status StartCompose();
49     EventLogTask::Status GetTaskStatus() const;
50     long GetLogSize() const;
51 private:
52     static constexpr uint32_t MAX_DUMP_TRACE_LIMIT = 15;
53 
54     using capture = std::function<void()>;
55 
56     int targetFd_;
57     int targetJsonFd_;
58     std::shared_ptr<SysEvent> event_;
59     std::vector<std::shared_ptr<EventLogCatcher>> tasks_;
60     uint32_t maxLogSize_;
61     uint32_t taskLogSize_;
62     volatile Status status_;
63     std::map<std::string, capture> captureList_;
64     int pid_;
65     std::set<int> catchedPids_;
66 
67     bool ShouldStopLogTask(int fd, uint32_t curTaskIndex, int curLogSize, std::shared_ptr<EventLogCatcher> catcher);
68     void AddStopReason(int fd, std::shared_ptr<EventLogCatcher> catcher, const std::string& reason);
69     void AddSeparator(int fd, std::shared_ptr<EventLogCatcher> catcher) const;
70     void RecordCatchedPids(const std::string& packageName);
71 
72     void AppStackCapture();
73     void SystemStackCapture();
74     void BinderLogCapture();
75     void MemoryUsageCapture();
76     bool PeerBinderCapture(const std::string &cmd);
77     void CpuUsageCapture();
78     void WMSUsageCapture();
79     void AMSUsageCapture();
80     void PMSUsageCapture();
81     void DPMSUsageCapture();
82     void RSUsageCapture();
83     void MMIUsageCapture();
84     void DMSUsageCapture();
85     void EECStateCapture();
86     void GECStateCapture();
87     void UIStateCapture();
88     void Screenshot();
89     void HilogCapture();
90     void LightHilogCapture();
91     void DmesgCapture();
92     void SysrqCapture(bool isWriteNewFile);
93     void HitraceCapture();
94     void SCBSessionCapture();
95     void SCBViewParamCapture();
96     void SCBWMSCapture();
97     void SCBWMSEVTCapture();
98     void DumpAppMapCapture();
99     void InputHilogCapture();
100     void RemoteStackCapture();
101     void GetThermalInfo(int fd);
102 };
103 } // namespace HiviewDFX
104 } // namespace OHOS
105 #endif // EVENT_LOGGER_LOG_TASK_H
106