• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_service_proxy.h"
16 
17 #include <unistd.h>
18 #include "ipc_types.h"
19 #include "message_parcel.h"
20 #include "hiview_logger.h"
21 #include "faultlog_info_ohos.h"
22 #include "faultlog_query_result_proxy.h"
23 #include "hiview_logger.h"
24 #include "hiviewfaultlogger_ipc_interface_code.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_LABEL(0xD002D11, "FaultLoggerProxy");
AddFaultLog(const FaultLogInfoOhos & info)29 void FaultLoggerServiceProxy::AddFaultLog(const FaultLogInfoOhos& info)
30 {
31     sptr<IRemoteObject> remote = Remote();
32     if (remote == nullptr) {
33         return;
34     }
35 
36     MessageParcel data;
37     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
38         return;
39     }
40 
41     if (!info.Marshalling(data)) {
42         return;
43     }
44 
45     if (info.pipeFd != -1) {
46         if (!data.WriteFileDescriptor(info.pipeFd)) {
47             HIVIEW_LOGE("failed to write file descriptor.");
48         }
49     }
50 
51     MessageParcel reply;
52     MessageOption option;
53     auto flags = option.GetFlags();
54     option.SetFlags(flags | 0x01); // 0X01 return immediately
55     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ADD_FAULTLOG),
56         data, reply, option) != ERR_OK) {
57         return;
58     }
59 }
60 
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)61 sptr<IRemoteObject> FaultLoggerServiceProxy::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
62 {
63     sptr<IRemoteObject> remote = Remote();
64     if (remote == nullptr) {
65         return nullptr;
66     }
67 
68     MessageParcel data;
69     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
70         return nullptr;
71     }
72 
73     if (!data.WriteInt32(faultType)) {
74         return nullptr;
75     }
76 
77     if (!data.WriteInt32(maxNum)) {
78         return nullptr;
79     }
80 
81     MessageParcel reply;
82     MessageOption option;
83     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::QUERY_SELF_FAULTLOG),
84         data, reply, option) != ERR_OK) {
85         return nullptr;
86     }
87 
88     sptr<IRemoteObject> remoteObject = reply.ReadRemoteObject();
89     if (remoteObject == nullptr) {
90         HIVIEW_LOGE("Failed to transfer Result.");
91     }
92     return remoteObject;
93 }
94 
EnableGwpAsanGrayscale(bool alwaysEnabled,double sampleRate,double maxSimutaneousAllocations,int32_t duration)95 bool FaultLoggerServiceProxy::EnableGwpAsanGrayscale(bool alwaysEnabled, double sampleRate,
96     double maxSimutaneousAllocations, int32_t duration)
97 {
98     sptr<IRemoteObject> remote = Remote();
99     if (remote == nullptr) {
100         return false;
101     }
102 
103     MessageParcel data;
104     MessageParcel reply;
105     MessageOption option;
106     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
107         return false;
108     }
109 
110     if (!data.WriteBool(alwaysEnabled)) {
111         return false;
112     }
113 
114     if (!data.WriteDouble(sampleRate)) {
115         return false;
116     }
117 
118     if (!data.WriteDouble(maxSimutaneousAllocations)) {
119         return false;
120     }
121 
122     if (!data.WriteInt32(duration)) {
123         return false;
124     }
125 
126     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ENABLE_GWP_ASAN_GRAYSALE),
127         data, reply, option) != ERR_OK) {
128         return false;
129     }
130     return reply.ReadBool();
131 }
132 
DisableGwpAsanGrayscale()133 void FaultLoggerServiceProxy::DisableGwpAsanGrayscale()
134 {
135     sptr<IRemoteObject> remote = Remote();
136     if (remote == nullptr) {
137         return;
138     }
139 
140     MessageParcel data;
141     MessageParcel reply;
142     MessageOption option;
143     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
144         return;
145     }
146 
147     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DISABLE_GWP_ASAN_GRAYSALE),
148         data, reply, option) != ERR_OK) {
149         return;
150     }
151 }
152 
GetGwpAsanGrayscaleState()153 uint32_t FaultLoggerServiceProxy::GetGwpAsanGrayscaleState()
154 {
155     sptr<IRemoteObject> remote = Remote();
156     if (remote == nullptr) {
157         return 0;
158     }
159 
160     MessageParcel data;
161     MessageParcel reply;
162     MessageOption option;
163     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
164         return 0;
165     }
166 
167     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::GET_GWP_ASAN_GRAYSALE),
168         data, reply, option) != ERR_OK) {
169         return 0;
170     }
171     return reply.ReadUint32();
172 }
173 
Destroy()174 void FaultLoggerServiceProxy::Destroy()
175 {
176     sptr<IRemoteObject> remote = Remote();
177     if (remote == nullptr) {
178         return;
179     }
180 
181     MessageParcel data;
182     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
183         return;
184     }
185 
186     MessageParcel reply;
187     MessageOption option;
188     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DESTROY),
189         data, reply, option) != ERR_OK) {
190         return;
191     }
192 }
193 } // namespace HiviewDFX
194 } // namespace OHOS
195