1 /*
2 * Copyright (c) 2023-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 "fault_data.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20 #include "hilog_tag_wrapper.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)24 bool FaultData::ReadFromParcel(Parcel &parcel)
25 {
26 std::string strValue;
27 if (!parcel.ReadString(strValue)) {
28 TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed.");
29 return false;
30 }
31 errorObject.name = strValue;
32
33 if (!parcel.ReadString(strValue)) {
34 TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed.");
35 return false;
36 }
37 errorObject.message = strValue;
38
39 if (!parcel.ReadString(strValue)) {
40 TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed.");
41 return false;
42 }
43 errorObject.stack = strValue;
44
45 int type = 0;
46 if (!parcel.ReadInt32(type)) {
47 TAG_LOGE(AAFwkTag::APPMGR, "FaultType read int32 failed.");
48 return false;
49 }
50 faultType = static_cast<FaultDataType>(type);
51
52 if (!parcel.ReadString(strValue)) {
53 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed.");
54 return false;
55 }
56 timeoutMarkers = strValue;
57
58 waitSaveState = parcel.ReadBool();
59 notifyApp = parcel.ReadBool();
60 forceExit = parcel.ReadBool();
61 needKillProcess = parcel.ReadBool();
62 state = parcel.ReadUint32();
63 eventId = parcel.ReadInt32();
64 tid = parcel.ReadInt32();
65 stuckTimeout = parcel.ReadUint32();
66 if (parcel.ReadBool()) {
67 token = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
68 }
69 if (!parcel.ReadString(strValue)) {
70 TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo read string failed.");
71 return false;
72 }
73 appfreezeInfo = strValue;
74 return ReadContent(parcel);
75 }
76
ReadContent(Parcel & parcel)77 bool FaultData::ReadContent(Parcel &parcel)
78 {
79 std::string strValue;
80 if (!parcel.ReadString(strValue)) {
81 TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed.");
82 return false;
83 }
84 appRunningUniqueId = strValue;
85 if (!parcel.ReadString(strValue)) {
86 TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed.");
87 return false;
88 }
89 procStatm = strValue;
90 return true;
91 }
92
Unmarshalling(Parcel & parcel)93 FaultData *FaultData::Unmarshalling(Parcel &parcel)
94 {
95 FaultData *info = new FaultData();
96 if (!info->ReadFromParcel(parcel)) {
97 delete info;
98 info = nullptr;
99 }
100 return info;
101 }
102
WriteContent(Parcel & parcel) const103 bool FaultData::WriteContent(Parcel &parcel) const
104 {
105 if (!parcel.WriteUint32(stuckTimeout)) {
106 TAG_LOGE(AAFwkTag::APPMGR, "stuckTimeout [%{public}u] write uint32 failed.", stuckTimeout);
107 return false;
108 }
109
110 if (token == nullptr) {
111 if (!parcel.WriteBool(false)) {
112 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed.");
113 return false;
114 }
115 } else {
116 if (!parcel.WriteBool(true) || !(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(token)) {
117 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed.");
118 return false;
119 }
120 }
121
122 if (!parcel.WriteString(appfreezeInfo)) {
123 TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str());
124 return false;
125 }
126
127 if (!parcel.WriteString(appRunningUniqueId)) {
128 TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str());
129 return false;
130 }
131
132 if (!parcel.WriteString(procStatm)) {
133 TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str());
134 return false;
135 }
136 return true;
137 }
138
Marshalling(Parcel & parcel) const139 bool FaultData::Marshalling(Parcel &parcel) const
140 {
141 if (!parcel.WriteString(errorObject.name)) {
142 TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str());
143 return false;
144 }
145
146 if (!parcel.WriteString(errorObject.message)) {
147 TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str());
148 return false;
149 }
150
151 if (!parcel.WriteString(errorObject.stack)) {
152 TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str());
153 return false;
154 }
155
156 if (!parcel.WriteInt32(static_cast<int32_t>(faultType))) {
157 TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast<int32_t>(faultType));
158 return false;
159 }
160
161 if (!parcel.WriteString(timeoutMarkers)) {
162 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str());
163 return false;
164 }
165
166 if (!parcel.WriteBool(waitSaveState)) {
167 TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false");
168 return false;
169 }
170
171 if (!parcel.WriteBool(notifyApp)) {
172 TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false");
173 return false;
174 }
175
176 if (!parcel.WriteBool(forceExit)) {
177 TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false");
178 return false;
179 }
180
181 if (!parcel.WriteBool(needKillProcess)) {
182 TAG_LOGE(AAFwkTag::APPMGR, "needKillProcess [%{public}s] write bool failed.",
183 needKillProcess ? "true" : "false");
184 return false;
185 }
186
187 if (!parcel.WriteUint32(state)) {
188 TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state);
189 return false;
190 }
191
192 if (!parcel.WriteInt32(eventId)) {
193 TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId);
194 return false;
195 }
196
197 if (!parcel.WriteInt32(tid)) {
198 TAG_LOGE(AAFwkTag::APPMGR, "Tid [%{public}u] write int32 failed.", tid);
199 return false;
200 }
201
202 return WriteContent(parcel);
203 }
204
ReadFromParcel(Parcel & parcel)205 bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel)
206 {
207 std::string strValue;
208 if (!parcel.ReadString(strValue)) {
209 TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed.");
210 return false;
211 }
212 errorObject.name = strValue;
213
214 if (!parcel.ReadString(strValue)) {
215 TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed.");
216 return false;
217 }
218 errorObject.message = strValue;
219
220 if (!parcel.ReadString(strValue)) {
221 TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed.");
222 return false;
223 }
224 errorObject.stack = strValue;
225
226 int type = 0;
227 if (!parcel.ReadInt32(type)) {
228 TAG_LOGE(AAFwkTag::APPMGR, "Type read int32 failed.");
229 return false;
230 }
231 faultType = static_cast<FaultDataType>(type);
232
233 if (!parcel.ReadInt32(pid)) {
234 TAG_LOGE(AAFwkTag::APPMGR, "Pid read int32 failed.");
235 return false;
236 }
237
238 if (!parcel.ReadString(strValue)) {
239 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed.");
240 return false;
241 }
242 timeoutMarkers = strValue;
243
244 waitSaveState = parcel.ReadBool();
245 notifyApp = parcel.ReadBool();
246 forceExit = parcel.ReadBool();
247 needKillProcess = parcel.ReadBool();
248 state = parcel.ReadUint32();
249 eventId = parcel.ReadInt32();
250 if (parcel.ReadBool()) {
251 token = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
252 }
253
254 if (!parcel.ReadString(strValue)) {
255 TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo read string failed.");
256 return false;
257 }
258 appfreezeInfo = strValue;
259 return ReadContent(parcel);
260 }
261
ReadContent(Parcel & parcel)262 bool AppFaultDataBySA::ReadContent(Parcel &parcel)
263 {
264 std::string strValue;
265 if (!parcel.ReadString(strValue)) {
266 TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed.");
267 return false;
268 }
269 appRunningUniqueId = strValue;
270
271 if (!parcel.ReadString(strValue)) {
272 TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed.");
273 return false;
274 }
275 procStatm = strValue;
276 return true;
277 }
278
Unmarshalling(Parcel & parcel)279 AppFaultDataBySA *AppFaultDataBySA::Unmarshalling(Parcel &parcel)
280 {
281 AppFaultDataBySA *info = new AppFaultDataBySA();
282 if (!info->ReadFromParcel(parcel)) {
283 delete info;
284 info = nullptr;
285 }
286 return info;
287 }
288
WriteContent(Parcel & parcel) const289 bool AppFaultDataBySA::WriteContent(Parcel &parcel) const
290 {
291 if (token == nullptr) {
292 if (!parcel.WriteBool(false)) {
293 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed.");
294 return false;
295 }
296 } else {
297 if (!parcel.WriteBool(true) || !(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(token)) {
298 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed.");
299 return false;
300 }
301 }
302
303 if (!parcel.WriteString(appfreezeInfo)) {
304 TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str());
305 return false;
306 }
307
308 if (!parcel.WriteString(appRunningUniqueId)) {
309 TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str());
310 return false;
311 }
312
313 if (!parcel.WriteString(procStatm)) {
314 TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str());
315 return false;
316 }
317 return true;
318 }
319
Marshalling(Parcel & parcel) const320 bool AppFaultDataBySA::Marshalling(Parcel &parcel) const
321 {
322 if (!WriteErrorObject(parcel)) {
323 return false;
324 }
325
326 if (!parcel.WriteInt32(static_cast<int32_t>(faultType))) {
327 TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast<int32_t>(faultType));
328 return false;
329 }
330
331 if (!parcel.WriteInt32(pid)) {
332 TAG_LOGE(AAFwkTag::APPMGR, "Pid [%{public}d] write int32 failed.", static_cast<int32_t>(pid));
333 return false;
334 }
335
336 if (!parcel.WriteString(timeoutMarkers)) {
337 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str());
338 return false;
339 }
340
341 if (!parcel.WriteBool(waitSaveState)) {
342 TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false");
343 return false;
344 }
345
346 if (!parcel.WriteBool(notifyApp)) {
347 TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false");
348 return false;
349 }
350
351 if (!parcel.WriteBool(forceExit)) {
352 TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false");
353 return false;
354 }
355
356 if (!parcel.WriteBool(needKillProcess)) {
357 TAG_LOGE(AAFwkTag::APPMGR, "needKillProcess [%{public}s] write bool failed.",
358 needKillProcess ? "true" : "false");
359 return false;
360 }
361
362 if (!parcel.WriteUint32(state)) {
363 TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state);
364 return false;
365 }
366
367 if (!parcel.WriteInt32(eventId)) {
368 TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId);
369 return false;
370 }
371
372 return WriteContent(parcel);
373 }
374
WriteErrorObject(Parcel & parcel) const375 bool AppFaultDataBySA::WriteErrorObject(Parcel &parcel) const
376 {
377 if (!parcel.WriteString(errorObject.name)) {
378 TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str());
379 return false;
380 }
381
382 if (!parcel.WriteString(errorObject.message)) {
383 TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str());
384 return false;
385 }
386
387 if (!parcel.WriteString(errorObject.stack)) {
388 TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str());
389 return false;
390 }
391 return true;
392 }
393 } // namespace AppExecFwk
394 } // namespace OHOS