1 /*
2 * Copyright (c) 2021-2024 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 "hiview_service_ability_stub.h"
17
18 #include <unordered_map>
19 #include <vector>
20
21 #include "accesstoken_kit.h"
22 #include "ash_memory_utils.h"
23 #include "client/trace_collector.h"
24 #include "client/memory_collector.h"
25 #include "errors.h"
26 #include "hiview_err_code.h"
27 #include "ipc_skeleton.h"
28 #include "hiview_logger.h"
29 #include "parameter_ex.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace {
34 DEFINE_LOG_TAG("HiViewSA-HiViewServiceAbilityStub");
35 const std::string ASH_MEM_NAME = "HiviewLogLibrary SharedMemory";
36 constexpr uint32_t ASH_MEM_SIZE = 107 * 5000; // 535k
37 constexpr int32_t MAX_SPLIT_MEMORY_SIZE = 256;
38
39 const std::unordered_map<uint32_t, std::string> ALL_PERMISSION_MAP = {
40 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_LIST),
41 "ohos.permission.READ_HIVIEW_SYSTEM"},
42 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_COPY),
43 "ohos.permission.READ_HIVIEW_SYSTEM"},
44 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_MOVE),
45 "ohos.permission.WRITE_HIVIEW_SYSTEM"},
46 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_REMOVE),
47 "ohos.permission.WRITE_HIVIEW_SYSTEM"},
48 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_SNAPSHOT_TRACE),
49 "ohos.permission.WRITE_HIVIEW_SYSTEM"},
50 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_DUMP_SNAPSHOT_TRACE),
51 "ohos.permission.READ_HIVIEW_SYSTEM"},
52 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_RECORDING_TRACE),
53 "ohos.permission.WRITE_HIVIEW_SYSTEM"},
54 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_ON),
55 "ohos.permission.READ_HIVIEW_SYSTEM"},
56 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_OFF),
57 "ohos.permission.READ_HIVIEW_SYSTEM"},
58 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_CLOSE_TRACE),
59 "ohos.permission.WRITE_HIVIEW_SYSTEM"},
60 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECOVER_TRACE),
61 "ohos.permission.WRITE_HIVIEW_SYSTEM"}
62 };
63
64 const std::unordered_map<uint32_t, std::string> TRACE_PERMISSION_MAP = {
65 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_SNAPSHOT_TRACE),
66 "ohos.permission.DUMP"},
67 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_DUMP_SNAPSHOT_TRACE),
68 "ohos.permission.DUMP"},
69 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_RECORDING_TRACE),
70 "ohos.permission.DUMP"},
71 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_ON),
72 "ohos.permission.DUMP"},
73 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_OFF),
74 "ohos.permission.DUMP"},
75 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_CLOSE_TRACE),
76 "ohos.permission.DUMP"},
77 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECOVER_TRACE),
78 "ohos.permission.DUMP"},
79 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_APP_TRACE), ""},
80 };
81
82 const std::unordered_map<uint32_t, std::string> CPU_PERMISSION_MAP = {
83 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_SYSTEM_CPU_USAGE), ""}
84 };
85
86 const std::unordered_map<uint32_t, std::string> MEMORY_PERMISSION_MAP = {
87 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_SET_APPRESOURCE_LIMIT), ""},
88 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_GRAPHIC_USAGE), ""},
89 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_SET_SPLIT_MEMORY_VALUE), ""}
90 };
91
HasAccessPermission(uint32_t code,const std::unordered_map<uint32_t,std::string> & permissions)92 bool HasAccessPermission(uint32_t code, const std::unordered_map<uint32_t, std::string>& permissions)
93 {
94 using namespace Security::AccessToken;
95 auto iter = permissions.find(code);
96 if (iter == permissions.end()) {
97 return false;
98 }
99 if (iter->second.empty()) {
100 return true;
101 }
102 AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
103 int verifyResult = AccessTokenKit::VerifyAccessToken(callerToken, iter->second);
104 if (verifyResult == PERMISSION_GRANTED) {
105 return true;
106 }
107 HIVIEW_LOGW("%{public}s not granted, code: %{public}u", iter->second.c_str(), code);
108 return false;
109 }
110
WritePracelableToMessage(MessageParcel & dest,Parcelable & data)111 int32_t WritePracelableToMessage(MessageParcel& dest, Parcelable& data)
112 {
113 if (!dest.WriteParcelable(&data)) {
114 HIVIEW_LOGW("failed to write TraceErrorCodeWrapper to parcel");
115 return TraceErrCode::ERR_WRITE_MSG_PARCEL;
116 }
117 return TraceErrCode::ERR_OK;
118 }
119 }
120
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)121 int32_t HiviewServiceAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
122 MessageOption &option)
123 {
124 HIVIEW_LOGI("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
125 std::u16string descripter = HiviewServiceAbilityStub::GetDescriptor();
126 std::u16string remoteDescripter = data.ReadInterfaceToken();
127 if (descripter != remoteDescripter) {
128 return -ERR_INVALID_VALUE;
129 }
130 if (!IsPermissionGranted(code)) {
131 return HiviewNapiErrCode::ERR_PERMISSION_CHECK;
132 }
133 auto requestHandler = GetRequestHandler(code);
134 if (requestHandler == nullptr) {
135 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
136 }
137 return requestHandler(data, reply, option);
138 }
139
IsPermissionGranted(uint32_t code)140 bool HiviewServiceAbilityStub::IsPermissionGranted(uint32_t code)
141 {
142 return HasAccessPermission(code, ALL_PERMISSION_MAP) || HasAccessPermission(code, TRACE_PERMISSION_MAP) ||
143 HasAccessPermission(code, CPU_PERMISSION_MAP) || HasAccessPermission(code, MEMORY_PERMISSION_MAP);
144 }
145
GetRequestHandlers()146 std::unordered_map<uint32_t, RequestHandler> HiviewServiceAbilityStub::GetRequestHandlers()
147 {
148 static std::unordered_map<uint32_t, RequestHandler> requestHandlers = {
149 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_LIST),
150 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
151 return this->HandleListRequest(data, reply, option);
152 }
153 },
154 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_COPY),
155 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
156 return this->HandleCopyRequest(data, reply, option);
157 }
158 },
159 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_MOVE),
160 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
161 return this->HandleMoveRequest(data, reply, option);
162 }
163 },
164 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_REMOVE),
165 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
166 return this->HandleRemoveRequest(data, reply, option);
167 }
168 }
169 };
170 return requestHandlers;
171 }
172
GetTraceRequestHandlers()173 std::unordered_map<uint32_t, RequestHandler> HiviewServiceAbilityStub::GetTraceRequestHandlers()
174 {
175 static std::unordered_map<uint32_t, RequestHandler> requestHandlers = {
176 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_SNAPSHOT_TRACE),
177 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
178 return this->HandleOpenSnapshotTraceRequest(data, reply, option);
179 }
180 },
181 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_DUMP_SNAPSHOT_TRACE),
182 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
183 return this->HandleDumpSnapshotTraceRequest(data, reply, option);
184 }
185 },
186 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_OPEN_RECORDING_TRACE),
187 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
188 return this->HandleOpenRecordingTraceRequest(data, reply, option);
189 }
190 },
191 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_ON),
192 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
193 return this->HandleRecordingTraceOnRequest(data, reply, option);
194 }
195 },
196 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECORDING_TRACE_OFF),
197 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
198 return this->HandleRecordingTraceOffRequest(data, reply, option);
199 }
200 },
201 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_CLOSE_TRACE),
202 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
203 return this->HandleCloseTraceRequest(data, reply, option);
204 }
205 },
206 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_RECOVER_TRACE),
207 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
208 return this->HandleRecoverTraceRequest(data, reply, option);
209 }
210 },
211 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_APP_TRACE),
212 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
213 return this->HandleCaptureDurationTraceRequest(data, reply, option);
214 }
215 }
216 };
217 return requestHandlers;
218 }
219
GetCpuRequestHandlers()220 std::unordered_map<uint32_t, RequestHandler> HiviewServiceAbilityStub::GetCpuRequestHandlers()
221 {
222 static std::unordered_map<uint32_t, RequestHandler> cpuRequestHandlers = {
223 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_SYSTEM_CPU_USAGE),
224 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
225 return HandleGetSysCpuUsageRequest(data, reply, option);
226 }
227 }
228 };
229 return cpuRequestHandlers;
230 }
231
GetMemoryRequestHandlers()232 std::unordered_map<uint32_t, RequestHandler> HiviewServiceAbilityStub::GetMemoryRequestHandlers()
233 {
234 static std::unordered_map<uint32_t, RequestHandler> memoryRequestHandlers = {
235 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_SET_APPRESOURCE_LIMIT),
236 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
237 return HandleSetAppResourceLimitRequest(data, reply, option);
238 }
239 },
240 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_GET_GRAPHIC_USAGE),
241 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
242 return HandleGetGraphicUsageRequest(data, reply, option);
243 }
244 },
245 {static_cast<uint32_t>(HiviewServiceInterfaceCode::HIVIEW_SERVICE_ID_SET_SPLIT_MEMORY_VALUE),
246 [this] (MessageParcel& data, MessageParcel& reply, MessageOption& option) {
247 return HandleSetSplitMemoryValueRequest(data, reply, option);
248 }
249 }
250 };
251 return memoryRequestHandlers;
252 }
253
GetRequestHandler(uint32_t code)254 RequestHandler HiviewServiceAbilityStub::GetRequestHandler(uint32_t code)
255 {
256 std::vector<std::unordered_map<uint32_t, RequestHandler>> allHandlerMaps = {
257 GetRequestHandlers(),
258 GetTraceRequestHandlers(),
259 GetCpuRequestHandlers(),
260 GetMemoryRequestHandlers()
261 };
262 for (const auto &handlerMap : allHandlerMaps) {
263 auto iter = handlerMap.find(code);
264 if (iter == handlerMap.end()) {
265 continue;
266 }
267 return iter->second;
268 }
269 HIVIEW_LOGE("function for handling request isn't found");
270 return nullptr;
271 }
272
HandleListRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)273 int32_t HiviewServiceAbilityStub::HandleListRequest(MessageParcel& data, MessageParcel& reply, MessageOption& option)
274 {
275 std::string logType;
276 if (!data.ReadString(logType)) {
277 HIVIEW_LOGE("cannot get log type");
278 return HiviewNapiErrCode::ERR_DEFAULT;
279 }
280 std::vector<HiviewFileInfo> fileInfos;
281 int32_t ret = List(logType, fileInfos);
282 if (ret != ERR_OK) {
283 return ret;
284 }
285 HIVIEW_LOGW("file list num:%{public}zu", fileInfos.size());
286 sptr<Ashmem> ashmem = AshMemoryUtils::GetAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
287 if (ashmem == nullptr) {
288 HIVIEW_LOGE("ge ashmem failed.");
289 return HiviewNapiErrCode::ERR_DEFAULT;
290 }
291 std::vector<uint32_t> allSize;
292 if (!AshMemoryUtils::WriteBulkData<HiviewFileInfo>(fileInfos, ashmem, ASH_MEM_SIZE, allSize)) {
293 HIVIEW_LOGE("WriteBulkData failed.");
294 return HiviewNapiErrCode::ERR_DEFAULT;
295 }
296 if (!reply.WriteUInt32Vector(allSize)) {
297 HIVIEW_LOGE("write size failed.");
298 return HiviewNapiErrCode::ERR_DEFAULT;
299 }
300 if (!reply.WriteAshmem(ashmem)) {
301 HIVIEW_LOGE("write ashmem failed.");
302 return HiviewNapiErrCode::ERR_DEFAULT;
303 }
304 return ERR_OK;
305 }
306
HandleCopyRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)307 int32_t HiviewServiceAbilityStub::HandleCopyRequest(MessageParcel& data, MessageParcel& reply, MessageOption& option)
308 {
309 return HandleCopyOrMoveRequest(data, reply, option, false);
310 }
311
HandleMoveRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)312 int32_t HiviewServiceAbilityStub::HandleMoveRequest(MessageParcel& data, MessageParcel& reply, MessageOption& option)
313 {
314 return HandleCopyOrMoveRequest(data, reply, option, true);
315 }
316
HandleCopyOrMoveRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option,bool isMove)317 int32_t HiviewServiceAbilityStub::HandleCopyOrMoveRequest(
318 MessageParcel& data, MessageParcel& reply, MessageOption& option, bool isMove)
319 {
320 std::string logType;
321 if (!data.ReadString(logType)) {
322 HIVIEW_LOGW("cannot get logtype");
323 return HiviewNapiErrCode::ERR_DEFAULT;
324 }
325 std::string logName;
326 if (!data.ReadString(logName)) {
327 HIVIEW_LOGW("cannot get log type");
328 return HiviewNapiErrCode::ERR_DEFAULT;
329 }
330 std::string dest;
331 if (!data.ReadString(dest)) {
332 HIVIEW_LOGW("cannot get dest dir");
333 return HiviewNapiErrCode::ERR_DEFAULT;
334 }
335 if (dest.find("..") != std::string::npos) {
336 HIVIEW_LOGW("invalid dest: %{public}s", dest.c_str());
337 return HiviewNapiErrCode::ERR_DEFAULT;
338 }
339 int32_t ret = isMove ? Move(logType, logName, dest) : Copy(logType, logName, dest);
340 if (!reply.WriteInt32(ret)) {
341 return HiviewNapiErrCode::ERR_DEFAULT;
342 }
343 return ERR_OK;
344 }
345
HandleRemoveRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)346 int32_t HiviewServiceAbilityStub::HandleRemoveRequest(MessageParcel& data, MessageParcel& reply, MessageOption& option)
347 {
348 std::string logType;
349 if (!data.ReadString(logType)) {
350 HIVIEW_LOGW("cannot get log type");
351 return HiviewNapiErrCode::ERR_DEFAULT;
352 }
353 std::string logName;
354 if (!data.ReadString(logName)) {
355 HIVIEW_LOGW("cannot get log name");
356 return HiviewNapiErrCode::ERR_DEFAULT;
357 }
358 int32_t ret = Remove(logType, logName);
359 if (!reply.WriteInt32(ret)) {
360 return HiviewNapiErrCode::ERR_DEFAULT;
361 }
362 return ERR_OK;
363 }
364
HandleOpenSnapshotTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)365 int32_t HiviewServiceAbilityStub::HandleOpenSnapshotTraceRequest(MessageParcel& data, MessageParcel& reply,
366 MessageOption& option)
367 {
368 std::vector<std::string> tagGroups;
369 if (!data.ReadStringVector(&tagGroups)) {
370 HIVIEW_LOGW("failed to read tag groups from parcel");
371 return TraceErrCode::ERR_READ_MSG_PARCEL;
372 }
373 auto ret = OpenSnapshotTrace(tagGroups);
374 return WritePracelableToMessage(reply, ret);
375 }
376
HandleDumpSnapshotTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)377 int32_t HiviewServiceAbilityStub::HandleDumpSnapshotTraceRequest(MessageParcel& data, MessageParcel& reply,
378 MessageOption& option)
379 {
380 int32_t caller = UCollect::TraceCaller::OTHER;
381 if (!data.ReadInt32(caller)) {
382 HIVIEW_LOGW("failed to read trace caller from parcel");
383 return TraceErrCode::ERR_READ_MSG_PARCEL;
384 }
385 auto ret = DumpSnapshotTrace(caller);
386 return WritePracelableToMessage(reply, ret);
387 }
388
HandleOpenRecordingTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)389 int32_t HiviewServiceAbilityStub::HandleOpenRecordingTraceRequest(MessageParcel& data, MessageParcel& reply,
390 MessageOption& option)
391 {
392 std::string tags;
393 if (!data.ReadString(tags)) {
394 HIVIEW_LOGW("failed to read tags from parcel");
395 return TraceErrCode::ERR_READ_MSG_PARCEL;
396 }
397 auto ret = OpenRecordingTrace(tags);
398 return WritePracelableToMessage(reply, ret);
399 }
400
HandleRecordingTraceOnRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)401 int32_t HiviewServiceAbilityStub::HandleRecordingTraceOnRequest(MessageParcel& data, MessageParcel& reply,
402 MessageOption& option)
403 {
404 auto ret = RecordingTraceOn();
405 return WritePracelableToMessage(reply, ret);
406 }
407
HandleRecordingTraceOffRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)408 int32_t HiviewServiceAbilityStub::HandleRecordingTraceOffRequest(MessageParcel& data, MessageParcel& reply,
409 MessageOption& option)
410 {
411 auto ret = RecordingTraceOff();
412 return WritePracelableToMessage(reply, ret);
413 }
414
HandleCloseTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)415 int32_t HiviewServiceAbilityStub::HandleCloseTraceRequest(MessageParcel& data, MessageParcel& reply,
416 MessageOption& option)
417 {
418 auto ret = CloseTrace();
419 return WritePracelableToMessage(reply, ret);
420 }
421
HandleRecoverTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)422 int32_t HiviewServiceAbilityStub::HandleRecoverTraceRequest(MessageParcel& data, MessageParcel& reply,
423 MessageOption& option)
424 {
425 auto ret = RecoverTrace();
426 return WritePracelableToMessage(reply, ret);
427 }
428
ReadAppCallerBase(MessageParcel & data,UCollectClient::AppCaller & appCaller,std::string & errField)429 static bool ReadAppCallerBase(MessageParcel& data, UCollectClient::AppCaller &appCaller, std::string &errField)
430 {
431 if (!data.ReadInt32(appCaller.actionId)) {
432 errField = "actionId";
433 return false;
434 }
435
436 if (!data.ReadString(appCaller.bundleName)) {
437 errField = "bundleName";
438 return false;
439 }
440
441 if (!data.ReadString(appCaller.bundleVersion)) {
442 errField = "bundleVersion";
443 return false;
444 }
445
446 if (!data.ReadString(appCaller.threadName)) {
447 errField = "threadName";
448 return false;
449 }
450
451 if (!data.ReadInt32(appCaller.foreground)) {
452 errField = "foreground";
453 return false;
454 }
455 return true;
456 }
457
ReadAppCallerExternal(MessageParcel & data,UCollectClient::AppCaller & appCaller,std::string & errField)458 static bool ReadAppCallerExternal(MessageParcel& data, UCollectClient::AppCaller &appCaller, std::string &errField)
459 {
460 if (!data.ReadInt32(appCaller.uid)) {
461 errField = "uid";
462 return false;
463 }
464
465 if (!data.ReadInt32(appCaller.pid)) {
466 errField = "pid";
467 return false;
468 }
469
470 if (!data.ReadInt64(appCaller.happenTime)) {
471 errField = "happenTime";
472 return false;
473 }
474
475 if (!data.ReadInt64(appCaller.beginTime)) {
476 errField = "beginTime";
477 return false;
478 }
479
480 if (!data.ReadInt64(appCaller.endTime)) {
481 errField = "endTime";
482 return false;
483 }
484
485 if (!data.ReadBool(appCaller.isBusinessJank)) {
486 errField = "isBusinessJank";
487 return false;
488 }
489 return true;
490 }
491
HandleCaptureDurationTraceRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)492 int32_t HiviewServiceAbilityStub::HandleCaptureDurationTraceRequest(MessageParcel& data, MessageParcel& reply,
493 MessageOption& option)
494 {
495 UCollectClient::AppCaller appCaller;
496
497 std::string errField;
498 do {
499 if (!ReadAppCallerBase(data, appCaller, errField)) {
500 break;
501 }
502 if (!ReadAppCallerExternal(data, appCaller, errField)) {
503 break;
504 }
505 } while (0);
506
507 if (!errField.empty()) {
508 HIVIEW_LOGW("failed to read %{public}s from parcel", errField.c_str());
509 return TraceErrCode::ERR_READ_MSG_PARCEL;
510 }
511
512 auto ret = CaptureDurationTrace(appCaller);
513 return WritePracelableToMessage(reply, ret);
514 }
515
HandleGetSysCpuUsageRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)516 int32_t HiviewServiceAbilityStub::HandleGetSysCpuUsageRequest(MessageParcel& data, MessageParcel& reply,
517 MessageOption& option)
518 {
519 auto ret = GetSysCpuUsage();
520 return WritePracelableToMessage(reply, ret);
521 }
522
HandleSetAppResourceLimitRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)523 int32_t HiviewServiceAbilityStub::HandleSetAppResourceLimitRequest(MessageParcel& data, MessageParcel& reply,
524 MessageOption& option)
525 {
526 if (!Parameter::IsBetaVersion() && !Parameter::IsLeakStateMode()) {
527 HIVIEW_LOGE("Called SetAppResourceLimitRequest service failed.");
528 return TraceErrCode::ERR_READ_MSG_PARCEL;
529 }
530 UCollectClient::MemoryCaller memoryCaller;
531 if (!data.ReadInt32(memoryCaller.pid)) {
532 HIVIEW_LOGW("HandleSetAppResourceLimitRequest failed to read pid from parcel");
533 return TraceErrCode::ERR_READ_MSG_PARCEL;
534 }
535
536 if (!data.ReadString(memoryCaller.resourceType)) {
537 HIVIEW_LOGW("HandleSetAppResourceLimitRequest failed to read type from parcel");
538 return TraceErrCode::ERR_READ_MSG_PARCEL;
539 }
540
541 if (!data.ReadInt32(memoryCaller.limitValue)) {
542 HIVIEW_LOGW("HandleSetAppResourceLimitRequest failed to read value from parcel");
543 return TraceErrCode::ERR_READ_MSG_PARCEL;
544 }
545
546 if (!data.ReadBool(memoryCaller.enabledDebugLog)) {
547 HIVIEW_LOGW("HandleSetAppResourceLimitRequest failed to read enabledDebugLog from parcel");
548 return TraceErrCode::ERR_READ_MSG_PARCEL;
549 }
550 memoryCaller.pid = IPCObjectStub::GetCallingPid();
551 if (memoryCaller.pid < 0) {
552 return TraceErrCode::ERR_SEND_REQUEST;
553 }
554 auto ret = SetAppResourceLimit(memoryCaller);
555 return WritePracelableToMessage(reply, ret);
556 }
557
HandleGetGraphicUsageRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)558 int32_t HiviewServiceAbilityStub::HandleGetGraphicUsageRequest(MessageParcel& data, MessageParcel& reply,
559 MessageOption& option)
560 {
561 int32_t pid = IPCObjectStub::GetCallingPid();
562 if (pid < 0) {
563 return TraceErrCode::ERR_SEND_REQUEST;
564 }
565 auto ret = GetGraphicUsage(pid);
566 return WritePracelableToMessage(reply, ret);
567 }
568
HandleSetSplitMemoryValueRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)569 int32_t HiviewServiceAbilityStub::HandleSetSplitMemoryValueRequest(MessageParcel& data, MessageParcel& reply,
570 MessageOption& option)
571 {
572 int32_t size = 0;
573 if (!data.ReadInt32(size) || size <= 0 || size > MAX_SPLIT_MEMORY_SIZE) {
574 HIVIEW_LOGW("HandleSetSplitMemoryValueRequest failed to read list size from parcel");
575 return TraceErrCode::ERR_READ_MSG_PARCEL;
576 }
577
578 std::vector<UCollectClient::MemoryCaller> memList(size);
579 for (int i = 0; i < size; i++) {
580 if (!data.ReadInt32(memList[i].pid)) {
581 HIVIEW_LOGW("HandleSetSplitMemoryValueRequest failed to read pid from parcel");
582 return TraceErrCode::ERR_READ_MSG_PARCEL;
583 }
584
585 if (!data.ReadString(memList[i].resourceType)) {
586 HIVIEW_LOGW("HandleSetSplitMemoryValueRequest failed to read type from parcel");
587 return TraceErrCode::ERR_READ_MSG_PARCEL;
588 }
589
590 if (!data.ReadInt32(memList[i].limitValue)) {
591 HIVIEW_LOGW("HandleSetSplitMemoryValueRequest failed to read value from parcel");
592 return TraceErrCode::ERR_READ_MSG_PARCEL;
593 }
594
595 if (!data.ReadBool(memList[i].enabledDebugLog)) {
596 HIVIEW_LOGW("HandleSetSplitMemoryValueRequest failed to read enabledDebugLog from parcel");
597 return TraceErrCode::ERR_READ_MSG_PARCEL;
598 }
599 }
600 auto ret = SetSplitMemoryValue(memList);
601 return WritePracelableToMessage(reply, ret);
602 }
603 } // namespace HiviewDFX
604 } // namespace OHOS