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 HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
16 #define HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
17 
18 #include <ctime>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <mutex>
24 
25 #include "event.h"
26 #include "event_loop.h"
27 #include "ffrt.h"
28 #include "log_store_ex.h"
29 #include "hiview_logger.h"
30 #include "plugin.h"
31 #include "sys_event.h"
32 
33 #include "active_key_event.h"
34 #include "db_helper.h"
35 #include "event_logger_config.h"
36 #include "freeze_common.h"
37 
38 namespace OHOS {
39 namespace HiviewDFX {
40 struct BinderInfo {
41     int client;
42     int server;
43     unsigned long wait;
44 };
45 
46 class EventLogger : public EventListener, public Plugin {
47 public:
EventLogger()48     EventLogger() : logStore_(std::make_shared<LogStoreEx>(LOGGER_EVENT_LOG_PATH, true)),
49         startTime_(time(nullptr)) {};
~EventLogger()50     ~EventLogger() {};
51     bool OnEvent(std::shared_ptr<Event> &event) override;
52     void OnLoad() override;
53     void OnUnload() override;
54     bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override;
55     std::string GetListenerName() override;
56     void OnUnorderedEvent(const Event& msg) override;
57     std::string GetAppFreezeFile(std::string& stackPath);
58 private:
59     static constexpr const char* const LOGGER_EVENT_LOG_PATH = "/data/log/eventlog";
60 
61 #ifdef WINDOW_MANAGER_ENABLE
62     std::vector<uint64_t> backTimes_;
63 #endif
64     std::unique_ptr<DBHelper> dbHelper_ = nullptr;
65     std::shared_ptr<FreezeCommon> freezeCommon_ = nullptr;
66     std::shared_ptr<LogStoreEx> logStore_;
67     long lastPid_ = 0;
68     uint64_t startTime_;
69     std::unordered_map<std::string, std::time_t> eventTagTime_;
70     std::unordered_map<int, std::string> fileMap_;
71     std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_;
72     std::shared_ptr<EventLoop> threadLoop_ = nullptr;
73     int const maxEventPoolCount = 5;
74     ffrt::mutex intervalMutex_;
75     std::unique_ptr<ActiveKeyEvent> activeKeyEvent_;
76     std::string cmdlinePath_ = "/proc/cmdline";
77     std::string cmdlineContent_ = "";
78     std::string lastEventName_ = "";
79     std::vector<std::string> rebootReasons_;
80 
81 #ifdef WINDOW_MANAGER_ENABLE
82     void ReportUserPanicWarning(std::shared_ptr<SysEvent> event, long pid);
83 #endif
84     void StartFfrtDump(std::shared_ptr<SysEvent> event);
85     void CollectMemInfo(int fd, std::shared_ptr<SysEvent> event);
86     void SaveDbToFile(const std::shared_ptr<SysEvent>& event);
87     std::string StabilityGetTempFreqInfo();
88     void StartLogCollect(std::shared_ptr<SysEvent> event);
89     int GetFile(std::shared_ptr<SysEvent> event, std::string& logFile, bool isFfrt);
90     bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event);
91     bool WriteStartTime(int fd, uint64_t start);
92     std::string DumpWindowInfo(int fd);
93     bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event);
94     void GetAppFreezeStack(int jsonFd, std::shared_ptr<SysEvent> event,
95         std::string& stack, const std::string& msg, std::string& kernelStack);
96     bool IsKernelStack(const std::string& stack);
97     void GetNoJsonStack(std::string& stack, std::string& contentStack, std::string& kernelStack, bool isFormat);
98     void ParsePeerStack(std::string& binderInfo, std::string& binderPeerStack);
99     void WriteKernelStackToFile(std::shared_ptr<SysEvent> event, int originFd,
100         const std::string& kernelStack);
101     bool WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event,
102         std::vector<std::string>& binderPids);
103     bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile);
104     void CreateAndPublishEvent(std::string& dirPath, std::string& fileName);
105     bool CheckProcessRepeatFreeze(const std::string& eventName, long pid);
106     bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event);
107     void CheckEventOnContinue(std::shared_ptr<SysEvent> event);
108     bool CanProcessRebootEvent(const Event& event);
109     void ProcessRebootEvent();
110     std::string GetRebootReason() const;
111     void GetCmdlineContent();
112     void GetRebootReasonConfig();
113     void GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event);
114     bool GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const;
115     void WriteCallStack(std::shared_ptr<SysEvent> event, int fd);
116 };
117 } // namespace HiviewDFX
118 } // namespace OHOS
119 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
120