• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "faultlogger_service_stub.h"
16 
17 #include "ipc_types.h"
18 #include "message_parcel.h"
19 
20 #include "faultlog_info.h"
21 #include "faultlog_info_ohos.h"
22 #include "hiviewfaultlogger_ipc_interface_code.h"
23 
24 #include "hiview_logger.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_LABEL(0xD002D11, "FaultLoggerServiceStub");
HandleOtherRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int FaultLoggerServiceStub::HandleOtherRemoteRequest(uint32_t code, MessageParcel &data,
30     MessageParcel &reply, MessageOption &option)
31 {
32     int ret = ERR_FLATTEN_OBJECT;
33     switch (code) {
34         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ENABLE_GWP_ASAN_GRAYSALE): {
35             bool alwaysEnabled = data.ReadBool();
36             double sampleRate = data.ReadDouble();
37             double maxSimutaneousAllocations = data.ReadDouble();
38             int32_t duration = data.ReadInt32();
39             if (sampleRate <= 0 || maxSimutaneousAllocations <= 0 || duration <= 0) {
40                 HIVIEW_LOGE("failed to enable gwp asan grayscale, sampleRate: %{public}f"
41                     ", maxSimutaneousAllocations: %{public}f,  duration: %{public}d.",
42                     sampleRate, maxSimutaneousAllocations, duration);
43                 break;
44             }
45             auto result = EnableGwpAsanGrayscale(alwaysEnabled, sampleRate,
46                 maxSimutaneousAllocations, duration);
47             if (reply.WriteBool(result)) {
48                 ret = ERR_OK;
49             } else {
50                 HIVIEW_LOGE("failed to enable gwp asan grayscale.");
51             }
52             break;
53         }
54         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DISABLE_GWP_ASAN_GRAYSALE): {
55             DisableGwpAsanGrayscale();
56             ret = ERR_OK;
57             break;
58         }
59         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::GET_GWP_ASAN_GRAYSALE): {
60             auto result = GetGwpAsanGrayscaleState();
61             if (reply.WriteUint32(result)) {
62                 ret = ERR_OK;
63             } else {
64                 HIVIEW_LOGE("failed to get gwp asan grayscale state.");
65             }
66             break;
67         }
68         default:
69             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70     }
71     return ret;
72 }
73 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)74 int FaultLoggerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
75     MessageParcel &reply, MessageOption &option)
76 {
77     std::u16string descripter = FaultLoggerServiceStub::GetDescriptor();
78     std::u16string remoteDescripter = data.ReadInterfaceToken();
79     if (descripter != remoteDescripter) {
80         HIVIEW_LOGE("read descriptor failed.");
81         return -1;
82     }
83 
84     switch (code) {
85         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ADD_FAULTLOG): {
86             sptr<FaultLogInfoOhos> ohosInfo = FaultLogInfoOhos::Unmarshalling(data);
87             if (ohosInfo == nullptr) {
88                 HIVIEW_LOGE("failed to Unmarshalling info.");
89                 return ERR_FLATTEN_OBJECT;
90             }
91             if (data.ContainFileDescriptors()) {
92                 ohosInfo->pipeFd = data.ReadFileDescriptor();
93             }
94             FaultLogInfoOhos info(*ohosInfo);
95             AddFaultLog(info);
96             return ERR_OK;
97         }
98         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::QUERY_SELF_FAULTLOG): {
99             int32_t type = data.ReadInt32();
100             int32_t maxNum = data.ReadInt32();
101             auto result = QuerySelfFaultLog(type, maxNum);
102             if (result == nullptr) {
103                 HIVIEW_LOGE("failed to query self log.");
104                 return -1;
105             }
106 
107             if (!reply.WriteRemoteObject(result)) {
108                 HIVIEW_LOGE("failed to write query result.");
109                 return ERR_FLATTEN_OBJECT;
110             }
111             return ERR_OK;
112         }
113         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DESTROY): {
114             Destroy();
115             return ERR_OK;
116         }
117 
118         default:
119             return HandleOtherRemoteRequest(code, data, reply, option);
120     }
121 }
122 } // namespace HiviewDFX
123 } // namespace OHOS
124