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)) {
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 HIVIEW_LOGE("Parcel failed to read int number.");
83 return nullptr;
84 }
85 if (!FaultLogInfoOhos::ReadString(parcel, "module", FaultLogInfo->module) ||
86 !FaultLogInfoOhos::ReadString(parcel, "reason", FaultLogInfo->reason) ||
87 !FaultLogInfoOhos::ReadString(parcel, "summary", FaultLogInfo->summary) ||
88 !FaultLogInfoOhos::ReadString(parcel, "logPath", FaultLogInfo->logPath) ||
89 !FaultLogInfoOhos::ReadString(parcel, "registers", FaultLogInfo->registers)) {
90 return nullptr;
91 }
92
93 const uint32_t maxSize = 128;
94 uint32_t size = 0;
95 if (!parcel.ReadUint32(size) || (size > maxSize)) {
96 return nullptr;
97 }
98 for (uint32_t i = 0; i < size; i++) {
99 std::string key;
100 std::string value;
101 if (!parcel.ReadString(key)) {
102 HIVIEW_LOGE("Parcel failed to read key of sectionMaps(%{public}s).", key.c_str());
103 return nullptr;
104 }
105 if (!parcel.ReadString(value)) {
106 HIVIEW_LOGE("Parcel failed to read value of sectionMaps(%{public}s).", value.c_str());
107 return nullptr;
108 }
109 FaultLogInfo->sectionMaps[key] = value;
110 }
111
112 return FaultLogInfo;
113 }
114 } // namespace hiview
115 } // namespace OHOS
116