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 "dump_broker_proxy.h"
16 #include <message_parcel.h>
17 #include "dump_errors.h"
18 #include "hidumper_service_ipc_interface_code.h"
19 #include "hilog_wrapper.h"
20 namespace OHOS {
21 namespace HiviewDFX {
Request(std::vector<std::u16string> & args,int outfd)22 int32_t DumpBrokerProxy::Request(std::vector<std::u16string> &args, int outfd)
23 {
24     int32_t ret = -1;
25     sptr<IRemoteObject> remote = Remote();
26     if (remote == nullptr) {
27         return ret;
28     }
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option;
32     if (!data.WriteInterfaceToken(DumpBrokerProxy::GetDescriptor())) {
33         return ret;
34     }
35     if (!data.WriteString16Vector(args)) {
36         return ERROR_WRITE_PARCEL;
37     }
38     if (!data.WriteFileDescriptor(outfd)) {
39         return ERROR_WRITE_PARCEL;
40     }
41     int res = remote->SendRequest(static_cast<int>(HidumperServiceInterfaceCode::DUMP_REQUEST_FILEFD),
42         data, reply, option);
43     if (res != ERR_OK) {
44         DUMPER_HILOGE(MODULE_ZIDL, "error|SendRequest error code: %{public}d", res);
45         return ret;
46     }
47     if (!reply.ReadInt32(ret)) {
48         return ERROR_READ_PARCEL;
49     }
50     return ret;
51 }
52 
ScanPidOverLimit(std::string requestType,int32_t limitSize,std::vector<int32_t> & pidList)53 int32_t DumpBrokerProxy::ScanPidOverLimit(std::string requestType, int32_t limitSize, std::vector<int32_t> &pidList)
54 {
55     int32_t ret = -1;
56     sptr<IRemoteObject> remote = Remote();
57     if (remote == nullptr) {
58         return ret;
59     }
60     MessageParcel data;
61     MessageOption option;
62     MessageParcel reply;
63     if (!data.WriteInterfaceToken(DumpBrokerProxy::GetDescriptor())) {
64         return ret;
65     }
66     data.WriteString(requestType);
67     data.WriteInt32(limitSize);
68     int res = remote->SendRequest(static_cast<int>(HidumperServiceInterfaceCode::SCAN_PID_OVER_LIMIT),
69         data, reply, option);
70     if (res != ERR_OK) {
71         DUMPER_HILOGE(MODULE_ZIDL, "send ScanPidOverLimit error code: %{public}d.", res);
72         return ret;
73     }
74     if (!reply.ReadInt32Vector(&pidList)) {
75         return ERROR_READ_PARCEL;
76     }
77     if (!reply.ReadInt32(ret)) {
78         return ERROR_READ_PARCEL;
79     }
80     return ret;
81 }
82 
CountFdNums(int32_t pid,uint32_t & fdNums,std::string & detailFdInfo,std::string & topLeakedType)83 int32_t DumpBrokerProxy::CountFdNums(int32_t pid, uint32_t &fdNums,
84     std::string &detailFdInfo, std::string &topLeakedType)
85 {
86     int32_t ret = -1;
87     sptr<IRemoteObject> remote = Remote();
88     if (remote == nullptr) {
89         return ret;
90     }
91     MessageParcel data;
92     if (!data.WriteInterfaceToken(DumpBrokerProxy::GetDescriptor())) {
93         return ret;
94     }
95     data.WriteInt32(pid);
96     MessageParcel reply;
97     MessageOption option;
98     int res = remote->SendRequest(static_cast<int>(HidumperServiceInterfaceCode::COUNT_FD_NUMS),
99         data, reply, option);
100     if (res != ERR_OK) {
101         DUMPER_HILOGE(MODULE_ZIDL, "send CountFdNums error code: %{public}d.", res);
102         return ret;
103     }
104     if (!reply.ReadUint32(fdNums)) {
105         return ERROR_READ_PARCEL;
106     }
107     if (!reply.ReadString(detailFdInfo)) {
108         return ERROR_READ_PARCEL;
109     }
110     if (!reply.ReadString(topLeakedType)) {
111         return ERROR_READ_PARCEL;
112     }
113     if (!reply.ReadInt32(ret)) {
114         return ERROR_READ_PARCEL;
115     }
116     DUMPER_HILOGI(MODULE_ZIDL, "sucess to count fd nums, pid is %{public}d", pid);
117     return ret;
118 }
119 } // namespace HiviewDFX
120 } // namespace OHOS
121