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 std::string StabilityGetTempFreqInfo(); 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 int GetFile(std::shared_ptr<SysEvent> event, std::string& logFile, bool isFfrt); 95 bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event); 96 bool WriteStartTime(int fd, uint64_t start); 97 std::string DumpWindowInfo(int fd); 98 bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event); 99 void GetAppFreezeStack(int jsonFd, std::shared_ptr<SysEvent> event, 100 std::string& stack, const std::string& msg, std::string& kernelStack); 101 bool IsKernelStack(const std::string& stack); 102 void GetNoJsonStack(std::string& stack, std::string& contentStack, std::string& kernelStack, bool isFormat); 103 void ParsePeerStack(std::string& binderInfo, std::string& binderPeerStack); 104 void WriteKernelStackToFile(std::shared_ptr<SysEvent> event, int originFd, 105 const std::string& kernelStack); 106 bool WriteFreezeJsonInfo(int fd, int jsonFd, std::shared_ptr<SysEvent> event, 107 std::vector<std::string>& binderPids, std::string& threadStack); 108 void WriteBinderInfo(int jsonFd, std::string& binderInfo, std::vector<std::string>& binderPids, 109 std::string& threadStack, std::string& kernelStack); 110 bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile); 111 void CreateAndPublishEvent(std::string& dirPath, std::string& fileName); 112 bool CheckProcessRepeatFreeze(const std::string& eventName, long pid); 113 bool CheckScreenOnRepeat(std::shared_ptr<SysEvent> event); 114 bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event); 115 void CheckEventOnContinue(std::shared_ptr<SysEvent> event); 116 bool CanProcessRebootEvent(const Event& event); 117 void ProcessRebootEvent(); 118 std::string GetRebootReason() const; 119 void GetCmdlineContent(); 120 void GetRebootReasonConfig(); 121 void GetFailedDumpStackMsg(std::string& stack, std::shared_ptr<SysEvent> event); 122 bool GetMatchString(const std::string& src, std::string& dst, const std::string& pattern) const; 123 void WriteCallStack(std::shared_ptr<SysEvent> event, int fd); 124 long GetEventPid(std::shared_ptr<SysEvent> &sysEvent); 125 void LogStoreSetting(); 126 }; 127 } // namespace HiviewDFX 128 } // namespace OHOS 129 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 130