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 "faultlogger_plugin.h"
32 #include "freeze_json_util.h"
33 
34 namespace OHOS {
35 namespace HiviewDFX {
36 struct DumpRequest {
37     bool requestDetail;
38     bool requestList;
39     std::string fileName;
40     std::string moduleName;
41     time_t time;
42 };
43 
44 class Faultlogger : public FaultloggerPlugin, public EventListener {
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     // implementations of FaultloggerPlugin interfaces
61     void AddFaultLog(FaultLogInfo& info) override;
62     std::unique_ptr<FaultLogInfo> GetFaultLogInfo(const std::string& logPath) override;
63     std::unique_ptr<FaultLogQueryResultInner> QuerySelfFaultLog(int32_t uid,
64         int32_t pid, int32_t faultType, int32_t maxNum) override;
65 
66     // implementations of EventListener interfaces
67     // for intercepting JsCrash from engine pipeline
68     void OnUnorderedEvent(const Event &msg) override;
69     std::string GetListenerName() override;
70     static int RunSanitizerd();
71 
72 private:
73     bool VerifiedDumpPermission();
74     void AddFaultLogIfNeed(FaultLogInfo& info, std::shared_ptr<Event> event);
75     void AddPublicInfo(FaultLogInfo& info);
76     void AddCppCrashInfo(FaultLogInfo& info);
77     void Dump(int fd, const DumpRequest& request) const;
78     void StartBootScan();
79     bool JudgmentRateLimiting(std::shared_ptr<Event> event);
80     std::unique_ptr<FaultLogManager> mgr_;
81     volatile bool hasInit_;
82     std::unordered_map<std::string, std::time_t> eventTagTime_;
83     static void HandleNotify(int32_t type, const std::string& fname);
84     void ReportCppCrashToAppEvent(const FaultLogInfo& info) const;
85     bool GetHilog(int32_t pid, std::string& log) const;
86     int DoGetHilogProcess(int32_t pid, int writeFd) const;
87     void GetStackInfo(const FaultLogInfo& info, std::string& stackInfo) const;
88     void ReportJsErrorToAppEvent(std::shared_ptr<SysEvent> sysEvent) const;
89     void ReportSanitizerToAppEvent(std::shared_ptr<SysEvent> sysEvent) const;
90     std::string GetSanitizerReason(const int32_t faultLogType, const std::string &reason) const;
91     std::string GetMemoryStrByPid(long pid) const;
92     FreezeJsonUtil::FreezeJsonCollector GetFreezeJsonCollector(const FaultLogInfo& info) const;
93     void ReportAppFreezeToAppEvent(const FaultLogInfo& info) const;
94     bool CheckFaultLog(FaultLogInfo info);
95     void CheckFaultLogAsync(const FaultLogInfo& info);
96     void FillHilog(const std::string &hilogStr, Json::Value &hilog) const;
97     void FaultlogLimit(const std::string &logPath, int32_t faultType) const;
98 };
99 }  // namespace HiviewDFX
100 }  // namespace OHOS
101 #endif  // HIVIEWDFX_HIVIEW_FAULTLOGGER_H
102 
103