• 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         close(info.pipeFd);
50     }
51 
52     MessageParcel reply;
53     MessageOption option;
54     auto flags = option.GetFlags();
55     option.SetFlags(flags | 0x01); // 0X01 return immediately
56     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ADD_FAULTLOG),
57         data, reply, option) != ERR_OK) {
58         return;
59     }
60 }
61 
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)62 sptr<IRemoteObject> FaultLoggerServiceProxy::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
63 {
64     sptr<IRemoteObject> remote = Remote();
65     if (remote == nullptr) {
66         return nullptr;
67     }
68 
69     MessageParcel data;
70     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
71         return nullptr;
72     }
73 
74     if (!data.WriteInt32(faultType)) {
75         return nullptr;
76     }
77 
78     if (!data.WriteInt32(maxNum)) {
79         return nullptr;
80     }
81 
82     MessageParcel reply;
83     MessageOption option;
84     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::QUERY_SELF_FAULTLOG),
85         data, reply, option) != ERR_OK) {
86         return nullptr;
87     }
88 
89     sptr<IRemoteObject> remoteObject = reply.ReadRemoteObject();
90     if (remoteObject == nullptr) {
91         HIVIEW_LOGE("Failed to transfer Result.");
92     }
93     return remoteObject;
94 }
95 
Destroy()96 void FaultLoggerServiceProxy::Destroy()
97 {
98     sptr<IRemoteObject> remote = Remote();
99     if (remote == nullptr) {
100         return;
101     }
102 
103     MessageParcel data;
104     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
105         return;
106     }
107 
108     MessageParcel reply;
109     MessageOption option;
110     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DESTROY),
111         data, reply, option) != ERR_OK) {
112         return;
113     }
114 }
115 } // namespace HiviewDFX
116 } // namespace OHOS
117