• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "plugin.h"
25 #include "sys_event.h"
26 
27 #include "faultlog_info.h"
28 #include "faultlog_manager.h"
29 #include "faultlog_query_result_inner.h"
30 #include "faultlogger_plugin.h"
31 #include "freeze_json_util.h"
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 struct DumpRequest {
36     bool requestDetail;
37     bool requestList;
38     std::string fileName;
39     std::string moduleName;
40     time_t time;
41 };
42 
43 class Faultlogger : public FaultloggerPlugin, public EventListener {
44 public:
Faultlogger()45     Faultlogger() : mgr_(nullptr), hasInit_(false) {};
~Faultlogger()46     virtual ~Faultlogger(){};
47 
48     // implementations of Plugin interfaces
49     // for intercepting AppFreeze from collectors pipeline
50     bool OnEvent(std::shared_ptr<Event> &event) override;
51     bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override;
52     bool CanProcessEvent(std::shared_ptr<Event> event) override;
53     bool ReadyToLoad() override;
54     void OnLoad() override;
55 
56     // dump debug infos through cmdline
57     void Dump(int fd, const std::vector<std::string> &cmds) override;
58 
59     // implementations of FaultloggerPlugin interfaces
60     void AddFaultLog(FaultLogInfo& info) override;
61     std::unique_ptr<FaultLogInfo> GetFaultLogInfo(const std::string& logPath) override;
62     std::unique_ptr<FaultLogQueryResultInner> QuerySelfFaultLog(int32_t uid,
63         int32_t pid, int32_t faultType, int32_t maxNum) override;
64 
65     // implementations of EventListener interfaces
66     // for intercepting JsCrash from engine pipeline
67     void OnUnorderedEvent(const Event &msg) override;
68     std::string GetListenerName() override;
69     static int RunSanitizerd();
70 
71 private:
72     bool VerifiedDumpPermission();
73     void AddFaultLogIfNeed(FaultLogInfo& info, std::shared_ptr<Event> event);
74     void AddPublicInfo(FaultLogInfo& info);
75     void AddCppCrashInfo(FaultLogInfo& info);
76     void Dump(int fd, const DumpRequest& request) const;
77     void StartBootScan();
78     bool JudgmentRateLimiting(std::shared_ptr<Event> event);
79     std::unique_ptr<FaultLogManager> mgr_;
80     volatile bool hasInit_;
81     std::unordered_map<std::string, std::time_t> eventTagTime_;
82     static void HandleNotify(int32_t type, const std::string& fname);
83     void ReportCppCrashToAppEvent(const FaultLogInfo& info) const;
84     bool GetHilog(int32_t pid, std::string& log) const;
85     void DoGetHilogProcess(int32_t pid, int writeFd) const;
86     void GetStackInfo(const FaultLogInfo& info, std::string& stackInfo) const;
87     void ReportJsErrorToAppEvent(std::shared_ptr<SysEvent> sysEvent) const;
88     std::string GetMemoryStrByPid(long pid) const;
89     FreezeJsonUtil::FreezeJsonCollector GetFreezeJsonCollector(const FaultLogInfo& info) const;
90     void ReportAppFreezeToAppEvent(const FaultLogInfo& info) const;
91 };
92 }  // namespace HiviewDFX
93 }  // namespace OHOS
94 #endif  // HIVIEWDFX_HIVIEW_FAULTLOGGER_H
95 
96