• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
16 #include "native_memory_profiler_sa_stub.h"
17 
18 #include "string_ex.h"
19 #include "logging.h"
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 namespace OHOS::Developtools::NativeDaemon {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & options)24 int32_t NativeMemoryProfilerSaStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
25     MessageOption& options)
26 {
27     std::u16string descriptor = data.ReadInterfaceToken();
28     if (descriptor != INativeMemoryProfilerSa::GetDescriptor()) {
29         PROFILER_LOG_ERROR(LOG_CORE, "Get unexpect descriptor:%s", Str16ToStr8(descriptor).c_str());
30         return ERR_INVALID_STATE;
31     }
32 
33     switch (code) {
34         case static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::START) : {
35             return StubStart(data, reply);
36         }
37         case static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_PID) : {
38             return StubStopPid(data, reply);
39         }
40         case static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_NAME) : {
41             return StubStopName(data, reply);
42         }
43         case static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_DATA) : {
44             return StubDumpFile(data, reply);
45         }
46         default : {
47             PROFILER_LOG_ERROR(LOG_CORE, "Unknown code:%u", code);
48             return IPCObjectStub::OnRemoteRequest(code, data, reply, options);
49         }
50     }
51 }
52 
StubStart(MessageParcel & data,MessageParcel & reply)53 int32_t NativeMemoryProfilerSaStub::StubStart(MessageParcel &data, MessageParcel &reply)
54 {
55     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
56     if (!NativeMemoryProfilerSaConfig::Unmarshalling(data, config)) {
57         return RET_ERR;
58     }
59     return Start(config);
60 }
61 
StubStopPid(MessageParcel & data,MessageParcel & reply)62 int32_t NativeMemoryProfilerSaStub::StubStopPid(MessageParcel &data, MessageParcel &reply)
63 {
64     uint32_t pid = 0;
65     READUINT32(data, pid, RET_ERR);
66     return Stop(pid);
67 }
68 
StubStopName(MessageParcel & data,MessageParcel & reply)69 int32_t NativeMemoryProfilerSaStub::StubStopName(MessageParcel &data, MessageParcel &reply)
70 {
71     std::string processName;
72     READSTRING(data, processName, RET_ERR);
73     return Stop(processName);
74 }
75 
StubDumpFile(MessageParcel & data,MessageParcel & reply)76 int32_t NativeMemoryProfilerSaStub::StubDumpFile(MessageParcel &data, MessageParcel &reply)
77 {
78     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
79     if (!NativeMemoryProfilerSaConfig::Unmarshalling(data, config)) {
80         return RET_ERR;
81     }
82     if (config->pid_ > 0) {
83         struct stat statBuf;
84         std::string pidPath = "/proc/" + std::to_string(config->pid_) + "/status";
85         if (stat(pidPath.c_str(), &statBuf) != 0) {
86             PROFILER_LOG_ERROR(LOG_CORE, "StubDumpFile hook process does not exist");
87             return RET_ERR;
88         }
89     }
90     uint32_t fd = static_cast<uint32_t>(data.ReadFileDescriptor());
91     return DumpData(fd, config);
92 }
93 } // namespace OHOS::Developtools::NativeDaemon