• 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         case static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_SIMP_DATA) : {
47             return StubDumpSimpFile(data, reply);
48         }
49         default : {
50             PROFILER_LOG_ERROR(LOG_CORE, "Unknown code:%u", code);
51             return IPCObjectStub::OnRemoteRequest(code, data, reply, options);
52         }
53     }
54 }
55 
StubStart(MessageParcel & data,MessageParcel & reply)56 int32_t NativeMemoryProfilerSaStub::StubStart(MessageParcel &data, MessageParcel &reply)
57 {
58     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
59     if (!NativeMemoryProfilerSaConfig::Unmarshalling(data, config)) {
60         return RET_ERR;
61     }
62     return Start(config);
63 }
64 
StubStopPid(MessageParcel & data,MessageParcel & reply)65 int32_t NativeMemoryProfilerSaStub::StubStopPid(MessageParcel &data, MessageParcel &reply)
66 {
67     uint32_t pid = 0;
68     READUINT32(data, pid, RET_ERR);
69     return Stop(pid);
70 }
71 
StubStopName(MessageParcel & data,MessageParcel & reply)72 int32_t NativeMemoryProfilerSaStub::StubStopName(MessageParcel &data, MessageParcel &reply)
73 {
74     std::string processName;
75     READSTRING(data, processName, RET_ERR);
76     return Stop(processName);
77 }
78 
StubDumpFile(MessageParcel & data,MessageParcel & reply)79 int32_t NativeMemoryProfilerSaStub::StubDumpFile(MessageParcel &data, MessageParcel &reply)
80 {
81     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
82     if (!NativeMemoryProfilerSaConfig::Unmarshalling(data, config)) {
83         return RET_ERR;
84     }
85     if (config->pid_ > 0) {
86         struct stat statBuf;
87         std::string pidPath = "/proc/" + std::to_string(config->pid_) + "/status";
88         if (stat(pidPath.c_str(), &statBuf) != 0) {
89             PROFILER_LOG_ERROR(LOG_CORE, "StubDumpFile hook process does not exist");
90             return RET_ERR;
91         }
92     }
93     uint32_t fd = static_cast<uint32_t>(data.ReadFileDescriptor());
94     return DumpData(fd, config);
95 }
96 
StubDumpSimpFile(MessageParcel & data,MessageParcel & reply)97 int32_t NativeMemoryProfilerSaStub::StubDumpSimpFile(MessageParcel &data, MessageParcel &reply)
98 {
99     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
100     if (!NativeMemoryProfilerSaConfig::Unmarshalling(data, config)) {
101         return RET_ERR;
102     }
103     if (config->pid_ > 0) {
104         struct stat statBuf;
105         std::string pidPath = "/proc/" + std::to_string(config->pid_) + "/status";
106         if (stat(pidPath.c_str(), &statBuf) != 0) {
107             PROFILER_LOG_ERROR(LOG_CORE, "StubDumpFile hook process does not exist");
108             return RET_ERR;
109         }
110     }
111     return Start(config, reply);
112 }
113 } // namespace OHOS::Developtools::NativeDaemon