1 /*
2 * Copyright (c) 2021 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 "faultlog_info_ohos.h"
16
17 #include "hiview_logger.h"
18 #include "parcel.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 DEFINE_LOG_LABEL(0xD002D11, "FaultLogInfoOhos");
Marshalling(Parcel & parcel) const23 bool FaultLogInfoOhos::Marshalling(Parcel& parcel) const
24 {
25 if (!parcel.WriteInt64(time) || !parcel.WriteInt32(uid) ||
26 !parcel.WriteInt32(pid) || !parcel.WriteInt32(faultLogType) || !parcel.WriteUint32(logFileCutoffSizeBytes)) {
27 HIVIEW_LOGE("Parcel failed to write int number.");
28 return false;
29 }
30 if (!parcel.WriteString(module)) {
31 HIVIEW_LOGE("Parcel failed to write module name(%{public}s).", module.c_str());
32 return false;
33 }
34 if (!parcel.WriteString(reason)) {
35 HIVIEW_LOGE("Parcel failed to write reason(%{public}s).", reason.c_str());
36 return false;
37 }
38 if (!parcel.WriteString(summary)) {
39 HIVIEW_LOGE("Parcel failed to write summary(%{public}s).", summary.c_str());
40 return false;
41 }
42 if (!parcel.WriteString(logPath)) {
43 HIVIEW_LOGE("Parcel failed to write log path(%{public}s).", logPath.c_str());
44 return false;
45 }
46 if (!parcel.WriteString(registers)) {
47 HIVIEW_LOGE("Parcel failed to write registers(%{public}s).", registers.c_str());
48 return false;
49 }
50 uint32_t size = sectionMaps.size();
51 if (!parcel.WriteUint32(size)) {
52 return false;
53 }
54 for (const auto& [key, value] : sectionMaps) {
55 if (!parcel.WriteString(key)) {
56 HIVIEW_LOGE("Parcel failed to write key of sectionMaps(%{public}s).", key.c_str());
57 return false;
58 }
59 if (!parcel.WriteString(value)) {
60 HIVIEW_LOGE("Parcel failed to write value of sectionMaps(%{public}s).", value.c_str());
61 return false;
62 }
63 }
64 return true;
65 }
66
ReadString(Parcel & parcel,std::string strItem,std::string & strValue)67 bool FaultLogInfoOhos::ReadString(Parcel& parcel, std::string strItem, std::string& strValue)
68 {
69 if (!parcel.ReadString(strValue)) {
70 HIVIEW_LOGE("Parcel failed to read %{public}s(%{public}s).", strItem.c_str(), strValue.c_str());
71 return false;
72 }
73 return true;
74 }
75
Unmarshalling(Parcel & parcel)76 sptr<FaultLogInfoOhos> FaultLogInfoOhos::Unmarshalling(Parcel& parcel)
77 {
78 sptr<FaultLogInfoOhos> FaultLogInfo = new FaultLogInfoOhos();
79
80 if (!parcel.ReadInt64(FaultLogInfo->time) || !parcel.ReadInt32(FaultLogInfo->uid) ||
81 !parcel.ReadInt32(FaultLogInfo->pid) || !parcel.ReadInt32(FaultLogInfo->faultLogType) ||
82 !parcel.ReadUint32(FaultLogInfo->logFileCutoffSizeBytes)) {
83 HIVIEW_LOGE("Parcel failed to read int number.");
84 return nullptr;
85 }
86 if (!FaultLogInfoOhos::ReadString(parcel, "module", FaultLogInfo->module) ||
87 !FaultLogInfoOhos::ReadString(parcel, "reason", FaultLogInfo->reason) ||
88 !FaultLogInfoOhos::ReadString(parcel, "summary", FaultLogInfo->summary) ||
89 !FaultLogInfoOhos::ReadString(parcel, "logPath", FaultLogInfo->logPath) ||
90 !FaultLogInfoOhos::ReadString(parcel, "registers", FaultLogInfo->registers)) {
91 return nullptr;
92 }
93
94 const uint32_t maxSize = 128;
95 uint32_t size = 0;
96 if (!parcel.ReadUint32(size) || (size > maxSize)) {
97 return nullptr;
98 }
99 for (uint32_t i = 0; i < size; i++) {
100 std::string key;
101 std::string value;
102 if (!parcel.ReadString(key)) {
103 HIVIEW_LOGE("Parcel failed to read key of sectionMaps(%{public}s).", key.c_str());
104 return nullptr;
105 }
106 if (!parcel.ReadString(value)) {
107 HIVIEW_LOGE("Parcel failed to read value of sectionMaps(%{public}s).", value.c_str());
108 return nullptr;
109 }
110 FaultLogInfo->sectionMaps[key] = value;
111 }
112
113 return FaultLogInfo;
114 }
115 } // namespace hiview
116 } // namespace OHOS
117