• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.logFileCutoffSizeBytes = info.logFileCutoffSizeBytes;
97     infoOhos.module = GetPrintableStr(info.module);
98     infoOhos.reason = info.reason;
99     infoOhos.summary = info.summary;
100     infoOhos.logPath = info.logPath;
101     infoOhos.registers = info.registers;
102     infoOhos.sectionMaps = info.sectionMaps;
103     infoOhos.fd = -1;
104     service->AddFaultLog(infoOhos);
105 }
106 
AddFaultLog(int64_t time,int32_t logType,const std::string & module,const std::string & summary)107 void AddFaultLog(int64_t time, int32_t logType, const std::string &module, const std::string &summary)
108 {
109     auto service = GetFaultloggerService();
110     if (service == nullptr) {
111         HIVIEW_LOGI("Fail to get service.");
112         return;
113     }
114 
115     FaultLogInfoOhos infoOhos;
116     infoOhos.time = time;
117     infoOhos.uid = getuid();
118     infoOhos.pid = getpid();
119     infoOhos.faultLogType = logType;
120     infoOhos.module = module;
121     infoOhos.summary = summary;
122     infoOhos.fd = -1;
123     service->AddFaultLog(infoOhos);
124 }
125 
QuerySelfFaultLog(FaultLogType faultType,int32_t maxNum)126 std::unique_ptr<FaultLogQueryResult> QuerySelfFaultLog(FaultLogType faultType, int32_t maxNum)
127 {
128     auto service = GetFaultloggerService();
129     if (service == nullptr) {
130         HIVIEW_LOGI("Fail to get service.");
131         return nullptr;
132     }
133 
134     auto result = service->QuerySelfFaultLog(static_cast<int32_t>(faultType), maxNum);
135     if (result == nullptr) {
136         HIVIEW_LOGI("Fail to query result.");
137         return nullptr;
138     }
139 
140     sptr<FaultLogQueryResultProxy> proxy = new FaultLogQueryResultProxy(result);
141     return std::make_unique<FaultLogQueryResult>(new FaultLogQueryResultImpl(proxy));
142 }
143 
EnableGwpAsanGrayscale(bool alwaysEnabled,double sampleRate,double maxSimutaneousAllocations,int32_t duration)144 bool EnableGwpAsanGrayscale(bool alwaysEnabled, double sampleRate,
145     double maxSimutaneousAllocations, int32_t duration)
146 {
147     auto service = GetFaultloggerService();
148     if (service == nullptr) {
149         HIVIEW_LOGE("Fail to enbale gwpAsanGrayscale, get service failed.");
150         return false;
151     }
152 
153     auto result = service->EnableGwpAsanGrayscale(alwaysEnabled, sampleRate,
154         maxSimutaneousAllocations, duration);
155     if (!result) {
156         HIVIEW_LOGE("Fail to enable the GWP-ASAN grayscale of your application.");
157     }
158     return result;
159 }
160 
DisableGwpAsanGrayscale()161 void DisableGwpAsanGrayscale()
162 {
163     auto service = GetFaultloggerService();
164     if (service == nullptr) {
165         HIVIEW_LOGE("Fail to disable gwpAsanGrayscale, get service failed.");
166         return;
167     }
168 
169     service->DisableGwpAsanGrayscale();
170 }
171 
GetGwpAsanGrayscaleState()172 uint32_t GetGwpAsanGrayscaleState()
173 {
174     auto service = GetFaultloggerService();
175     if (service == nullptr) {
176         HIVIEW_LOGE("Fail to get gwpAsanGrayscale state, get service failed.");
177         return 0;
178     }
179 
180     return service->GetGwpAsanGrayscaleState();
181 }
182 
ReportCppCrashEvent(const FaultLogInfoInner & info)183 void ReportCppCrashEvent(const FaultLogInfoInner &info)
184 {
185     HiSysEventWrite(HiSysEvent::Domain::RELIABILITY,
186         "CPP_CRASH",
187         HiSysEvent::EventType::FAULT,
188         "MODULE", GetPrintableStr(info.module),
189         "REASON", info.reason,
190         "PID", info.pid,
191         "UID", info.id,
192         "FAULT_TYPE", std::to_string(info.faultLogType),
193         "HAPPEN_TIME", info.time,
194         "SUMMARY", info.summary);
195 }
196 }  // namespace HiviewDFX
197 }  // namespace OHOS
198 
AddFaultLog(FaultLogInfoInner * info)199 __attribute__((visibility ("default"))) void AddFaultLog(FaultLogInfoInner* info)
200 {
201     OHOS::HiviewDFX::AddFaultLog(*info);
202 }
203 
ReportCppCrashEvent(FaultLogInfoInner * info)204 __attribute__((visibility ("default"))) void ReportCppCrashEvent(FaultLogInfoInner* info)
205 {
206     OHOS::HiviewDFX::ReportCppCrashEvent(*info);
207 }
208