• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #ifndef FAULT_LOGGER_SERVICE_H_
17 #define FAULT_LOGGER_SERVICE_H_
18 
19 #include "string"
20 #include "vector"
21 #include "cstdint"
22 #include "dfx_socket_request.h"
23 #include "temp_file_manager.h"
24 #include "dfx_exception.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 
29 class IFaultLoggerService {
30 public:
31     virtual int32_t OnReceiveMsg(const std::string& socketName, int32_t connectionFd,
32                               ssize_t nRead, const std::vector<uint8_t>& buf) = 0;
33     virtual ~IFaultLoggerService() = default;
34 };
35 
36 template<typename T>
37 class FaultLoggerService : public IFaultLoggerService {
38 public:
39     FaultLoggerService() = default;
OnReceiveMsg(const std::string & socketName,int32_t connectionFd,const ssize_t nRead,const std::vector<uint8_t> & buf)40     int32_t OnReceiveMsg(const std::string& socketName, int32_t connectionFd,
41                       const ssize_t nRead, const std::vector<uint8_t>& buf) final
42     {
43         if (nRead != sizeof(T)) {
44             return ResponseCode::INVALID_REQUEST_DATA;
45         }
46         return OnRequest(socketName, connectionFd, *(reinterpret_cast<const T*>(buf.data())));
47     };
48 protected:
49     virtual int32_t OnRequest(const std::string& socketName, int32_t connectionFd, const T& requestData) = 0;
50 };
51 
52 class FileDesService : public FaultLoggerService<FaultLoggerdRequest> {
53 public:
54     int32_t OnRequest(const std::string& socketName, int32_t connectionFd,
55                       const FaultLoggerdRequest& requestData) override;
56 private:
57     static bool Filter(const std::string& socketName, int32_t connectionFd, const FaultLoggerdRequest& requestData);
58 };
59 
60 #ifndef HISYSEVENT_DISABLE
61 class ExceptionReportService : public FaultLoggerService<CrashDumpException> {
62 public:
63     int32_t OnRequest(const std::string& socketName, int32_t connectionFd,
64                       const CrashDumpException& requestData) override;
65 private:
66     static bool Filter(int32_t connectionFd, const CrashDumpException& requestData);
67 };
68 
69 struct DumpStats {
70     int32_t pid = 0;
71     uint64_t requestTime = 0;
72     uint64_t signalTime = 0;
73     uint64_t processdumpStartTime = 0;
74     uint64_t processdumpFinishTime = 0;
75     uint64_t dumpCatcherFinishTime = 0;
76     int32_t result = 0;
77     std::string summary;
78     std::string callerProcessName;
79     std::string callerElfName;
80     std::string targetProcessName;
81 };
82 
83 class StatsService : public FaultLoggerService<FaultLoggerdStatsRequest> {
84 public:
85     int32_t OnRequest(const std::string& socketName, int32_t connectionFd,
86                       const FaultLoggerdStatsRequest& requestData) override;
87 private:
88     void RemoveTimeoutDumpStats();
89     void ReportDumpStats(const DumpStats& stat);
90     static std::string GetElfName(const FaultLoggerdStatsRequest& request);
91     void StartDelayTask(std::function<void()> workFunc, int32_t delayTime);
92     std::list<DumpStats> stats_{};
93 };
94 #endif
95 
96 #ifndef is_ohos_lite
97 class SdkDumpService : public FaultLoggerService<SdkDumpRequestData> {
98 public:
99 
100     int32_t OnRequest(const std::string& socketName, int32_t connectionFd,
101                       const SdkDumpRequestData& requestData) override;
102 private:
103     static int32_t Filter(const std::string& socketName, const SdkDumpRequestData& requestData, uint32_t uid);
104 };
105 
106 class PipeService : public FaultLoggerService<PipFdRequestData> {
107 public:
108     int32_t OnRequest(const std::string& socketName, int32_t connectionFd,
109                       const PipFdRequestData& requestData) override;
110 private:
111     static bool Filter(const std::string& socketName, int32_t connectionFd, const PipFdRequestData& requestData);
112 };
113 #endif
114 }
115 }
116 #endif // FAULT_LOGGER_SERVICE_H_
117