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