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 const inline std::string LOGGER_EVENT_LOG_PATH = "/data/log/eventlog";
60     static const inline std::string MONITOR_STACK_LOG_PATH = "/data/log/faultlog/temp";
61     static const inline std::string LONG_PRESS = "LONG_PRESS";
62     static const inline std::string AP_S_PRESS6S = "AP_S_PRESS6S";
63     static const inline std::string REBOOT_REASON = "reboot_reason";
64     static const inline std::string NORMAL_RESET_TYPE = "normal_reset_type";
65     static const inline std::string PATTERN_WITHOUT_SPACE = "\\s*=\\s*([^ \\n]*)";
66     static const inline std::string DOMAIN_LONGPRESS = "KERNEL_VENDOR";
67     static const inline std::string STRINGID_LONGPRESS = "COM_LONG_PRESS";
68     static const inline std::string LONGPRESS_LEVEL = "CRITICAL";
69     static const inline std::string MONITOR_STACK_FLIE_NAME[] = {
70         "jsstack",
71     };
72     static const inline std::string MONITOR_LOG_PATH[] = {
73         MONITOR_STACK_LOG_PATH,
74     };
75     static const inline std::vector<std::string> DUMP_FFRT = {
76         "THREAD_BLOCK_6S", "UI_BLOCK_6S", "APP_INPUT_BLOCK",
77         "LIFECYCLE_TIMEOUT", "SERVICE_BLOCK",
78         "GET_DISPLAY_SNAPSHOT", "CREATE_VIRTUAL_SCREEN",
79         "BUSSINESS_THREAD_BLOCK_6S"
80     };
81 #ifdef WINDOW_MANAGER_ENABLE
82     static constexpr int BACK_FREEZE_TIME_LIMIT = 2000;
83     static constexpr int BACK_FREEZE_COUNT_LIMIT = 5;
84     static constexpr int CLICK_FREEZE_TIME_LIMIT = 3000;
85     static constexpr int TOP_WINDOW_NUM = 3;
86     static constexpr uint8_t USER_PANIC_WARNING_PRIVACY = 2;
87 #endif
88     static constexpr int DUMP_TIME_RATIO = 2;
89     static constexpr int EVENT_MAX_ID = 1000000;
90     static constexpr int MAX_FILE_NUM = 500;
91     static constexpr int MAX_FOLDER_SIZE = 500 * 1024 * 1024;
92     static constexpr int QUERY_PROCESS_KILL_INTERVAL = 10000;
93     static constexpr int HISTORY_EVENT_LIMIT = 500;
94     static constexpr uint8_t LONGPRESS_PRIVACY = 1;
95 
96 #ifdef WINDOW_MANAGER_ENABLE
97     std::vector<uint64_t> backTimes_;
98 #endif
99     std::unique_ptr<DBHelper> dbHelper_ = nullptr;
100     std::shared_ptr<FreezeCommon> freezeCommon_ = nullptr;
101     std::shared_ptr<LogStoreEx> logStore_;
102     long lastPid_ = 0;
103     uint64_t startTime_;
104     std::unordered_map<std::string, std::time_t> eventTagTime_;
105     std::unordered_map<int, std::string> fileMap_;
106     std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_;
107     std::shared_ptr<EventLoop> threadLoop_ = nullptr;
108     int const maxEventPoolCount = 5;
109     ffrt::mutex intervalMutex_;
110     std::unique_ptr<ActiveKeyEvent> activeKeyEvent_;
111     std::string cmdlinePath_ = "/proc/cmdline";
112     std::string cmdlineContent_ = "";
113     std::string lastEventName_ = "";
114     std::vector<std::string> rebootReasons_;
115 
116 #ifdef WINDOW_MANAGER_ENABLE
117     void ReportUserPanicWarning(std::shared_ptr<SysEvent> event, long pid);
118 #endif
119     void StartFfrtDump(std::shared_ptr<SysEvent> event);
120     void CollectMemInfo(int fd, std::shared_ptr<SysEvent> event);
121     void SaveDbToFile(const std::shared_ptr<SysEvent>& event);
122     void StartLogCollect(std::shared_ptr<SysEvent> event);
123     int GetFile(std::shared_ptr<SysEvent> event, std::string& logFile, bool isFfrt);
124     bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event);
125     bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event);
126     void GetAppFreezeStack(int jsonFd, std::shared_ptr<SysEvent> event,
127         std::string& stack, const std::string& msg, std::string& kernelStack);
128     bool IsKernelStack(const std::string& stack);
129     void GetNoJsonStack(std::string& stack, std::string& contentStack, std::string& kernelStack, bool isFormat);
130     void ParsePeerStack(std::string& binderInfo, std::string& binderPeerStack);
131     void WriteKernelStackToFile(std::shared_ptr<SysEvent> event, int originFd,
132         const std::string& kernelStack);
133     bool WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event);
134     bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile);
135     void CreateAndPublishEvent(std::string& dirPath, std::string& fileName);
136     bool CheckProcessRepeatFreeze(const std::string& eventName, long pid);
137     bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event);
138     void CheckEventOnContinue(std::shared_ptr<SysEvent> event);
139     bool CanProcessRebootEvent(const Event& event);
140     void ProcessRebootEvent();
141     std::string GetRebootReason() const;
142     void GetCmdlineContent();
143     void GetRebootReasonConfig();
144     void GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event);
145     bool GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const;
146     void WriteCallStack(std::shared_ptr<SysEvent> event, int fd);
147     std::string GetStringFromFile(const std::string path);
148     int GetNumFromString(const std::string &mem);
149     void CheckString(int fd, const std::string &mem, std::string &data, const std::string key, const std::string path);
150     long GetEventPid(std::shared_ptr<SysEvent> &sysEvent);
151 };
152 } // namespace HiviewDFX
153 } // namespace OHOS
154 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
155