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