• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 CRASH_VALIDATOR_H
16 #define CRASH_VALIDATOR_H
17 
18 #include "plugin.h"
19 #include "sys_event.h"
20 
21 #include <memory>
22 #include <mutex>
23 #include <vector>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class CrashEvent {
28 public:
29     uint64_t time {0};
30     uint64_t pid {0};
31     uint64_t uid {0};
32     std::string path {""};
33     std::string name {""};
34     bool isCppCrash {false};
35 };
36 // every CPP_CRASH should have corresponding PROCESS_EXIT event and
37 // every PROCESS_EXIT event that triggered by crash signal should have corresponding CPP_CRASH events
38 // check the existence of these event to judge whether we have loss some crash log
39 class CrashValidator : public EventListener, public Plugin {
40 public:
41     CrashValidator();
42     ~CrashValidator();
43 
44     bool ReadyToLoad() override;
45     void OnLoad() override;
46     void OnUnload() override;
47     bool OnEvent(std::shared_ptr<Event>& event) override;
48     void OnUnorderedEvent(const Event &event) override;
49     bool CanProcessEvent(std::shared_ptr<Event> event) override;
50     std::string GetListenerName() override;
51     void Dump(int fd, const std::vector<std::string>& cmds) override;
52 
53 private:
54     bool ValidateLogContent(const CrashEvent& event);
55     bool RemoveSimilarEvent(const CrashEvent& event);
56     void HandleCppCrashEvent(SysEvent& sysEvent);
57     void HandleProcessExitEvent(SysEvent& sysEvent);
58     void PrintEvents(int fd, const std::vector<CrashEvent>& events);
59     void ReadServiceCrashStatus();
60     void CheckOutOfDateEvents();
61     bool stopReadKmsg_;
62     uint32_t totalEventCount_;
63     uint32_t normalEventCount_;
64     std::unique_ptr<std::thread> kmsgReaderThread_;
65     std::vector<CrashEvent> pendingEvents_;
66     std::vector<CrashEvent> noLogEvents_;
67     std::vector<CrashEvent> matchedEvents_;
68     std::mutex lock_;
69 };
70 } // namespace HiviewDFX
71 } // namespace OHOS
72 #endif // CRASH_VALIDATOR_H