1 /*
2 * Copyright (c) 2021-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 #include "faultlogger_client.h"
16
17 #include <unistd.h>
18
19 #include "hilog/log_cpp.h"
20 #include "hisysevent.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "refbase.h"
25 #include "system_ability_definition.h"
26
27 #include "faultlog_info_ohos.h"
28 #include "faultlog_query_result.h"
29 #include "faultlog_query_result_impl.h"
30
31 #include "faultlogger_service_proxy.h"
32 #include "ifaultlogger_service.h"
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD002D10, "FaultloggerClient"};
37
GetPrintableStr(const std::string & str)38 std::string GetPrintableStr(const std::string& str)
39 {
40 size_t index = 0;
41 for (char c : str) {
42 if (std::isprint(c)) {
43 index++;
44 } else {
45 break;
46 }
47 }
48 return str.substr(0, index);
49 }
50
CheckFaultloggerStatus()51 bool CheckFaultloggerStatus()
52 {
53 sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (serviceManager == nullptr) {
55 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find samgr, exit.");
56 return false;
57 }
58 if (serviceManager->CheckSystemAbility(DFX_FAULT_LOGGER_ABILITY_ID) == nullptr) {
59 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find faultlogger service, exit.");
60 return false;
61 }
62 return true;
63 }
64
GetFaultloggerService()65 sptr<FaultLoggerServiceProxy> GetFaultloggerService()
66 {
67 sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68 if (serviceManager == nullptr) {
69 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find samgr, exit.");
70 return nullptr;
71 }
72
73 auto service = serviceManager->CheckSystemAbility(DFX_FAULT_LOGGER_ABILITY_ID);
74 if (service == nullptr) {
75 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find faultlogger service, exit.");
76 return nullptr;
77 }
78
79 sptr<FaultLoggerServiceProxy> proxy = new FaultLoggerServiceProxy(service);
80 return proxy;
81 }
82
AddFaultLog(const FaultLogInfoInner & info)83 void AddFaultLog(const FaultLogInfoInner &info)
84 {
85 auto service = GetFaultloggerService();
86 if (service == nullptr) {
87 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
88 return;
89 }
90
91 FaultLogInfoOhos infoOhos;
92 infoOhos.time = info.time;
93 infoOhos.uid = info.id;
94 infoOhos.pid = info.pid;
95 infoOhos.faultLogType = info.faultLogType;
96 infoOhos.module = GetPrintableStr(info.module);
97 infoOhos.reason = info.reason;
98 infoOhos.summary = info.summary;
99 infoOhos.logPath = info.logPath;
100 infoOhos.sectionMaps = info.sectionMaps;
101 infoOhos.fd = -1;
102 service->AddFaultLog(infoOhos);
103 }
104
AddFaultLog(int64_t time,int32_t logType,const std::string & module,const std::string & summary)105 void AddFaultLog(int64_t time, int32_t logType, const std::string &module, const std::string &summary)
106 {
107 auto service = GetFaultloggerService();
108 if (service == nullptr) {
109 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
110 return;
111 }
112
113 FaultLogInfoOhos infoOhos;
114 infoOhos.time = time;
115 infoOhos.uid = getuid();
116 infoOhos.pid = getpid();
117 infoOhos.faultLogType = logType;
118 infoOhos.module = module;
119 infoOhos.summary = summary;
120 infoOhos.fd = -1;
121 service->AddFaultLog(infoOhos);
122 }
123
QuerySelfFaultLog(FaultLogType faultType,int32_t maxNum)124 std::unique_ptr<FaultLogQueryResult> QuerySelfFaultLog(FaultLogType faultType, int32_t maxNum)
125 {
126 auto service = GetFaultloggerService();
127 if (service == nullptr) {
128 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
129 return nullptr;
130 }
131
132 auto result = service->QuerySelfFaultLog(static_cast<int32_t>(faultType), maxNum);
133 if (result == nullptr) {
134 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to query result.");
135 return nullptr;
136 }
137
138 sptr<FaultLogQueryResultProxy> proxy = new FaultLogQueryResultProxy(result);
139 return std::make_unique<FaultLogQueryResult>(new FaultLogQueryResultImpl(proxy));
140 }
141
ReportCppCrashEvent(const FaultLogInfoInner & info)142 void ReportCppCrashEvent(const FaultLogInfoInner &info)
143 {
144 HiSysEvent::Write("RELIABILITY",
145 "CPP_CRASH",
146 HiSysEvent::EventType::FAULT,
147 "MODULE", GetPrintableStr(info.module),
148 "REASON", info.reason,
149 "PID", info.pid,
150 "UID", info.id,
151 "FAULT_TYPE", std::to_string(info.faultLogType),
152 "HAPPEN_TIME", info.time,
153 "SUMMARY", info.summary);
154 }
155 } // namespace HiviewDFX
156 } // namespace OHOS
157
AddFaultLog(FaultLogInfoInner * info)158 __attribute__((visibility ("default"))) void AddFaultLog(FaultLogInfoInner* info)
159 {
160 OHOS::HiviewDFX::AddFaultLog(*info);
161 }
162
ReportCppCrashEvent(FaultLogInfoInner * info)163 __attribute__((visibility ("default"))) void ReportCppCrashEvent(FaultLogInfoInner* info)
164 {
165 OHOS::HiviewDFX::ReportCppCrashEvent(*info);
166 }
167