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 HIVIEWDFX_HIVIEW_FAULTLOGGER_H 16 #define HIVIEWDFX_HIVIEW_FAULTLOGGER_H 17 #include <cstdint> 18 #include <memory> 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "event.h" 24 #include "json/json.h" 25 #include "plugin.h" 26 #include "sys_event.h" 27 28 #include "faultlog_info.h" 29 #include "faultlog_manager.h" 30 #include "faultlog_query_result_inner.h" 31 #include "freeze_json_util.h" 32 33 namespace OHOS { 34 namespace HiviewDFX { 35 struct DumpRequest { 36 bool requestDetail; 37 bool requestList; 38 bool compatFlag; 39 std::string fileName; 40 std::string moduleName; 41 time_t time; 42 }; 43 44 class Faultlogger : public Plugin { 45 public: Faultlogger()46 Faultlogger() : mgr_(nullptr), hasInit_(false) {}; ~Faultlogger()47 virtual ~Faultlogger(){}; 48 49 // implementations of Plugin interfaces 50 // for intercepting AppFreeze from collectors pipeline 51 bool OnEvent(std::shared_ptr<Event> &event) override; 52 bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override; 53 bool CanProcessEvent(std::shared_ptr<Event> event) override; 54 bool ReadyToLoad() override; 55 void OnLoad() override; 56 57 // dump debug infos through cmdline 58 void Dump(int fd, const std::vector<std::string> &cmds) override; 59 60 void AddFaultLog(FaultLogInfo& info); 61 std::unique_ptr<FaultLogQueryResultInner> QuerySelfFaultLog(int32_t uid, 62 int32_t pid, int32_t faultType, int32_t maxNum); 63 64 static int RunSanitizerd(); 65 66 private: 67 bool VerifiedDumpPermission(); 68 void AddFaultLogIfNeed(FaultLogInfo& info, std::shared_ptr<Event> event); 69 void AddPublicInfo(FaultLogInfo& info); 70 static void AddBundleInfo(FaultLogInfo& info); 71 static void AddForegroundInfo(FaultLogInfo& info); 72 static void UpdateTerminalThreadStack(FaultLogInfo& info); 73 void AddCppCrashInfo(FaultLogInfo& info); 74 void AddDebugSignalInfo(FaultLogInfo& info) const; 75 void Dump(int fd, const DumpRequest& request) const; 76 void StartBootScan(); 77 bool JudgmentRateLimiting(std::shared_ptr<Event> event); 78 static void HandleNotify(int32_t type, const std::string& fname); 79 void ReportCppCrashToAppEvent(const FaultLogInfo& info) const; 80 bool GetHilog(int32_t pid, std::string& log) const; 81 static int DoGetHilogProcess(int32_t pid, int writeFd); 82 void GetStackInfo(const FaultLogInfo& info, std::string& stackInfo) const; 83 void ReportJsOrCjErrorToAppEvent(std::shared_ptr<SysEvent> sysEvent, FaultLogType faultType) const; 84 void ReportSanitizerToAppEvent(std::shared_ptr<SysEvent> sysEvent) const; 85 std::string GetMemoryStrByPid(long pid) const; 86 FreezeJsonUtil::FreezeJsonCollector GetFreezeJsonCollector(const FaultLogInfo& info) const; 87 void ReportAppFreezeToAppEvent(const FaultLogInfo& info, bool isAppHicollie = false) const; 88 void ReportEventToAppEvent(const FaultLogInfo& info); 89 bool CheckFaultLog(FaultLogInfo info); 90 void CheckFaultLogAsync(const FaultLogInfo& info); 91 void FillHilog(const std::string &hilogStr, Json::Value &hilog) const; 92 void FaultlogLimit(const std::string &logPath, int32_t faultType) const; 93 FaultLogInfo FillFaultLogInfo(SysEvent &sysEvent) const; 94 void AddBootScanEvent(); 95 static bool ReadHilog(int fd, std::string& log); 96 97 std::unique_ptr<FaultLogManager> mgr_; 98 volatile bool hasInit_; 99 std::unordered_map<std::string, std::time_t> eventTagTime_; 100 class FaultloggerListener; 101 std::shared_ptr<FaultloggerListener> eventListener_; 102 }; 103 } // namespace HiviewDFX 104 } // namespace OHOS 105 #endif // HIVIEWDFX_HIVIEW_FAULTLOGGER_H 106 107