1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci#ifndef HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
16020a203aSopenharmony_ci#define HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
17020a203aSopenharmony_ci
18020a203aSopenharmony_ci#include <ctime>
19020a203aSopenharmony_ci#include <map>
20020a203aSopenharmony_ci#include <memory>
21020a203aSopenharmony_ci#include <string>
22020a203aSopenharmony_ci#include <unordered_map>
23020a203aSopenharmony_ci#include <mutex>
24020a203aSopenharmony_ci
25020a203aSopenharmony_ci#include "event.h"
26020a203aSopenharmony_ci#include "event_loop.h"
27020a203aSopenharmony_ci#include "ffrt.h"
28020a203aSopenharmony_ci#include "log_store_ex.h"
29020a203aSopenharmony_ci#include "hiview_logger.h"
30020a203aSopenharmony_ci#include "plugin.h"
31020a203aSopenharmony_ci#include "sys_event.h"
32020a203aSopenharmony_ci
33020a203aSopenharmony_ci#include "active_key_event.h"
34020a203aSopenharmony_ci#include "db_helper.h"
35020a203aSopenharmony_ci#include "event_logger_config.h"
36020a203aSopenharmony_ci#include "freeze_common.h"
37020a203aSopenharmony_ci
38020a203aSopenharmony_cinamespace OHOS {
39020a203aSopenharmony_cinamespace HiviewDFX {
40020a203aSopenharmony_cistruct BinderInfo {
41020a203aSopenharmony_ci    int client;
42020a203aSopenharmony_ci    int server;
43020a203aSopenharmony_ci    unsigned long wait;
44020a203aSopenharmony_ci};
45020a203aSopenharmony_ci
46020a203aSopenharmony_ciclass EventLogger : public EventListener, public Plugin {
47020a203aSopenharmony_cipublic:
48020a203aSopenharmony_ci    EventLogger() : logStore_(std::make_shared<LogStoreEx>(LOGGER_EVENT_LOG_PATH, true)),
49020a203aSopenharmony_ci        startTime_(time(nullptr)) {};
50020a203aSopenharmony_ci    ~EventLogger() {};
51020a203aSopenharmony_ci    bool OnEvent(std::shared_ptr<Event> &event) override;
52020a203aSopenharmony_ci    void OnLoad() override;
53020a203aSopenharmony_ci    void OnUnload() override;
54020a203aSopenharmony_ci    bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override;
55020a203aSopenharmony_ci    std::string GetListenerName() override;
56020a203aSopenharmony_ci    void OnUnorderedEvent(const Event& msg) override;
57020a203aSopenharmony_ci    std::string GetAppFreezeFile(std::string& stackPath);
58020a203aSopenharmony_ciprivate:
59020a203aSopenharmony_ci    static constexpr const char* const LOGGER_EVENT_LOG_PATH = "/data/log/eventlog";
60020a203aSopenharmony_ci
61020a203aSopenharmony_ci#ifdef WINDOW_MANAGER_ENABLE
62020a203aSopenharmony_ci    std::vector<uint64_t> backTimes_;
63020a203aSopenharmony_ci#endif
64020a203aSopenharmony_ci    std::unique_ptr<DBHelper> dbHelper_ = nullptr;
65020a203aSopenharmony_ci    std::shared_ptr<FreezeCommon> freezeCommon_ = nullptr;
66020a203aSopenharmony_ci    std::shared_ptr<LogStoreEx> logStore_;
67020a203aSopenharmony_ci    long lastPid_ = 0;
68020a203aSopenharmony_ci    uint64_t startTime_;
69020a203aSopenharmony_ci    std::unordered_map<std::string, std::time_t> eventTagTime_;
70020a203aSopenharmony_ci    std::unordered_map<int, std::string> fileMap_;
71020a203aSopenharmony_ci    std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_;
72020a203aSopenharmony_ci    std::shared_ptr<EventLoop> threadLoop_ = nullptr;
73020a203aSopenharmony_ci    int const maxEventPoolCount = 5;
74020a203aSopenharmony_ci    ffrt::mutex intervalMutex_;
75020a203aSopenharmony_ci    std::unique_ptr<ActiveKeyEvent> activeKeyEvent_;
76020a203aSopenharmony_ci    std::string cmdlinePath_ = "/proc/cmdline";
77020a203aSopenharmony_ci    std::string cmdlineContent_ = "";
78020a203aSopenharmony_ci    std::string lastEventName_ = "";
79020a203aSopenharmony_ci    std::vector<std::string> rebootReasons_;
80020a203aSopenharmony_ci
81020a203aSopenharmony_ci#ifdef WINDOW_MANAGER_ENABLE
82020a203aSopenharmony_ci    void ReportUserPanicWarning(std::shared_ptr<SysEvent> event, long pid);
83020a203aSopenharmony_ci#endif
84020a203aSopenharmony_ci    void StartFfrtDump(std::shared_ptr<SysEvent> event);
85020a203aSopenharmony_ci    void CollectMemInfo(int fd, std::shared_ptr<SysEvent> event);
86020a203aSopenharmony_ci    void SaveDbToFile(const std::shared_ptr<SysEvent>& event);
87020a203aSopenharmony_ci    std::string StabilityGetTempFreqInfo();
88020a203aSopenharmony_ci    void StartLogCollect(std::shared_ptr<SysEvent> event);
89020a203aSopenharmony_ci    int GetFile(std::shared_ptr<SysEvent> event, std::string& logFile, bool isFfrt);
90020a203aSopenharmony_ci    bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event);
91020a203aSopenharmony_ci    bool WriteStartTime(int fd, uint64_t start);
92020a203aSopenharmony_ci    std::string DumpWindowInfo(int fd);
93020a203aSopenharmony_ci    bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event);
94020a203aSopenharmony_ci    void GetAppFreezeStack(int jsonFd, std::shared_ptr<SysEvent> event,
95020a203aSopenharmony_ci        std::string& stack, const std::string& msg, std::string& kernelStack);
96020a203aSopenharmony_ci    bool IsKernelStack(const std::string& stack);
97020a203aSopenharmony_ci    void GetNoJsonStack(std::string& stack, std::string& contentStack, std::string& kernelStack, bool isFormat);
98020a203aSopenharmony_ci    void ParsePeerStack(std::string& binderInfo, std::string& binderPeerStack);
99020a203aSopenharmony_ci    void WriteKernelStackToFile(std::shared_ptr<SysEvent> event, int originFd,
100020a203aSopenharmony_ci        const std::string& kernelStack);
101020a203aSopenharmony_ci    bool WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event,
102020a203aSopenharmony_ci        std::vector<std::string>& binderPids);
103020a203aSopenharmony_ci    bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile);
104020a203aSopenharmony_ci    void CreateAndPublishEvent(std::string& dirPath, std::string& fileName);
105020a203aSopenharmony_ci    bool CheckProcessRepeatFreeze(const std::string& eventName, long pid);
106020a203aSopenharmony_ci    bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event);
107020a203aSopenharmony_ci    void CheckEventOnContinue(std::shared_ptr<SysEvent> event);
108020a203aSopenharmony_ci    bool CanProcessRebootEvent(const Event& event);
109020a203aSopenharmony_ci    void ProcessRebootEvent();
110020a203aSopenharmony_ci    std::string GetRebootReason() const;
111020a203aSopenharmony_ci    void GetCmdlineContent();
112020a203aSopenharmony_ci    void GetRebootReasonConfig();
113020a203aSopenharmony_ci    void GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event);
114020a203aSopenharmony_ci    bool GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const;
115020a203aSopenharmony_ci    void WriteCallStack(std::shared_ptr<SysEvent> event, int fd);
116020a203aSopenharmony_ci};
117020a203aSopenharmony_ci} // namespace HiviewDFX
118020a203aSopenharmony_ci} // namespace OHOS
119020a203aSopenharmony_ci#endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
120