1 /*
2 * Copyright (c) 2021 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 "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "refbase.h"
24
25 #include "faultlog_info_ohos.h"
26 #include "faultlog_query_result.h"
27 #include "faultlog_query_result_impl.h"
28
29 #include "faultlogger_service_proxy.h"
30 #include "ifaultlogger_service.h"
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD002D10, "FaultloggerClient"};
35 constexpr int32_t DFX_HIVIEW_FAULTLOGGER_ID = 1202;
GetFaultloggerService()36 sptr<FaultLoggerServiceProxy> GetFaultloggerService()
37 {
38 sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 if (serviceManager == nullptr) {
40 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find samgr, exit.");
41 return nullptr;
42 }
43
44 auto service = serviceManager->CheckSystemAbility(DFX_HIVIEW_FAULTLOGGER_ID);
45 if (service == nullptr) {
46 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "Failed to find faultlogger service, exit.");
47 return nullptr;
48 }
49
50 sptr<FaultLoggerServiceProxy> proxy = new FaultLoggerServiceProxy(service);
51 return proxy;
52 }
53
AddFaultLog(const FaultLogInfoInner & info)54 void AddFaultLog(const FaultLogInfoInner &info)
55 {
56 auto service = GetFaultloggerService();
57 if (service == nullptr) {
58 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
59 return;
60 }
61
62 std::string module = info.module;
63 module.erase(std::remove_if(module.begin(), module.end(),
64 [](unsigned char c) { return !std::isprint(c); }), module.end());
65
66 FaultLogInfoOhos infoOhos;
67 infoOhos.time = info.time;
68 infoOhos.uid = info.id;
69 infoOhos.pid = info.pid;
70 infoOhos.faultLogType = info.faultLogType;
71 infoOhos.module = module;
72 infoOhos.reason = info.reason;
73 infoOhos.summary = info.summary;
74 infoOhos.logPath = info.logPath;
75 infoOhos.sectionMaps = info.sectionMaps;
76 infoOhos.fd = -1;
77 service->AddFaultLog(infoOhos);
78 }
79
AddFaultLog(int64_t time,int32_t logType,const std::string & module,const std::string & summary)80 void AddFaultLog(int64_t time, int32_t logType, const std::string &module, const std::string &summary)
81 {
82 auto service = GetFaultloggerService();
83 if (service == nullptr) {
84 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
85 return;
86 }
87
88 FaultLogInfoOhos infoOhos;
89 infoOhos.time = time;
90 infoOhos.uid = getuid();
91 infoOhos.pid = getpid();
92 infoOhos.faultLogType = logType;
93 infoOhos.module = module;
94 infoOhos.summary = summary;
95 infoOhos.fd = -1;
96 service->AddFaultLog(infoOhos);
97 }
98
QuerySelfFaultLog(FaultLogType faultType,int32_t maxNum)99 std::unique_ptr<FaultLogQueryResult> QuerySelfFaultLog(FaultLogType faultType, int32_t maxNum)
100 {
101 auto service = GetFaultloggerService();
102 if (service == nullptr) {
103 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to get service.");
104 return nullptr;
105 }
106
107 auto result = service->QuerySelfFaultLog(static_cast<int32_t>(faultType), maxNum);
108 if (result == nullptr) {
109 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "Fail to query result.");
110 return nullptr;
111 }
112
113 sptr<FaultLogQueryResultProxy> proxy = new FaultLogQueryResultProxy(result);
114 return std::make_unique<FaultLogQueryResult>(new FaultLogQueryResultImpl(proxy));
115 }
116 } // namespace HiviewDFX
117 } // namespace OHOS
118
AddFaultLog(FaultLogInfoInner * info)119 __attribute__((visibility ("default"))) void AddFaultLog(FaultLogInfoInner* info)
120 {
121 OHOS::HiviewDFX::AddFaultLog(*info);
122 }