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