• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 
16 
17 #include "native_memory_profiler_sa_proxy.h"
18 
19 #include "logging.h"
20 
21 namespace OHOS::Developtools::NativeDaemon {
NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> & impl)22 NativeMemoryProfilerSaProxy::NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<INativeMemoryProfilerSa>(impl) {}
24 
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config)25 int32_t NativeMemoryProfilerSaProxy::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
26 {
27     MessageParcel data;
28     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
29         PROFILER_LOG_ERROR(LOG_CORE, "Start failed to write descriptor");
30         return RET_ERR;
31     }
32     CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
33 
34     MessageParcel reply;
35     MessageOption option;
36     sptr<IRemoteObject> remote = Remote();
37     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
38     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::START),
39                                       data, reply, option);
40     if (ret != RET_OK) {
41         PROFILER_LOG_ERROR(LOG_CORE, "Start failed");
42         return ret;
43     }
44     return RET_OK;
45 }
46 
DumpData(uint32_t fd,std::shared_ptr<NativeMemoryProfilerSaConfig> & config)47 int32_t NativeMemoryProfilerSaProxy::DumpData(uint32_t fd, std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
48 {
49     MessageParcel data;
50     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
51         PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write descriptor");
52         return RET_ERR;
53     }
54 
55     CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
56     if (!data.WriteFileDescriptor(fd)) {
57         PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write fd");
58         return RET_ERR;
59     }
60 
61     MessageParcel reply;
62     MessageOption option;
63     sptr<IRemoteObject> remote = Remote();
64     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
65     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_DATA),
66                                       data, reply, option);
67     if (ret != RET_OK) {
68         PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed");
69         return ret;
70     }
71     return RET_OK;
72 }
73 
Stop(uint32_t pid)74 int32_t NativeMemoryProfilerSaProxy::Stop(uint32_t pid)
75 {
76     MessageParcel data;
77     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
78         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
79         return RET_ERR;
80     }
81     WRITEUINT32(data, pid, RET_ERR);
82     MessageParcel reply;
83     MessageOption option;
84     sptr<IRemoteObject> remote = Remote();
85     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
86     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_PID),
87                                       data, reply, option);
88     if (ret != RET_OK) {
89         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
90         return ret;
91     }
92     return RET_OK;
93 }
94 
Stop(const std::string & name)95 int32_t NativeMemoryProfilerSaProxy::Stop(const std::string& name)
96 {
97     MessageParcel data;
98     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
99         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
100         return RET_ERR;
101     }
102     WRITESTRING(data, name, RET_ERR);
103     MessageParcel reply;
104     MessageOption option;
105     sptr<IRemoteObject> remote = Remote();
106     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
107     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_NAME),
108                                       data, reply, option);
109     if (ret != RET_OK) {
110         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
111         return ret;
112     }
113     return RET_OK;
114 }
115 } // namespace OHOS::Developtools::NativeDaemon