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