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_formatter.h"
16
17 #include <cstdint>
18 #include <fstream>
19 #include <list>
20 #include <sstream>
21 #include <string>
22
23 #include "faultlog_info.h"
24 #include "faultlog_util.h"
25 #include "file_util.h"
26 #include "string_util.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace FaultLogger {
31 constexpr int LOG_MAP_KEY = 0;
32 constexpr int LOG_MAP_VALUE = 1;
33 static const char *DEVICE_INFO[] = {"DEVICE_INFO", "Device info:"};
34 static const char *BUILD_INFO[] = {"BUILD_INFO", "Build info:"};
35 static const char *MODULE_NAME[] = {"MODULE", "Module name:"};
36 static const char *PROCESS_NAME[] = {"PNAME", "Process name:"};
37 static const char *MODULE_PID[] = {"PID", "Pid:"};
38 static const char *MODULE_UID[] = {"UID", "Uid:"};
39 static const char *MODULE_VERSION[] = {"VERSION", "Version:"};
40 static const char *FAULT_TYPE[] = {"FAULT_TYPE", "Fault type:"};
41 static const char *SYSVMTYPE[] = {"SYSVMTYPE", "SYSVMTYPE:"};
42 static const char *APPVMTYPE[] = {"APPVMTYPE", "APPVMTYPE:"};
43 static const char *FOREGROUND[] = {"FG", "Foreground:"};
44 static const char *LIFETIME[] = {"LIFETIME", "Up time:"};
45 static const char *REASON[] = {"REASON", "Reason:"};
46 static const char *FAULT_MESSAGE[] = {"FAULT_MESSAGE", "Fault message:"};
47 static const char *STACKTRACE[] = {"TRUSTSTACK", "Selected stacktrace:\n"};
48 static const char *ROOT_CAUSE[] = {"BINDERMAX", "Blocked chain:\n"};
49 static const char *MSG_QUEUE_INFO[] = {"MSG_QUEUE_INFO", "Message queue info:\n"};
50 static const char *BINDER_TRANSACTION_INFO[] = {"BINDER_TRANSACTION_INFO", "Binder transaction info:\n"};
51 static const char *PROCESS_STACKTRACE[] = {"PROCESS_STACKTRACE", "Process stacktrace:\n"};
52 static const char *OTHER_THREAD_INFO[] = {"OTHER_THREAD_INFO", "Other thread info:\n"};
53 static const char *KEY_THREAD_INFO[] = {"KEY_THREAD_INFO", "Fault thread Info:\n"};
54 static const char *KEY_THREAD_REGISTERS[] = {"KEY_THREAD_REGISTERS", "Registers:\n"};
55 static const char *MEMORY_USAGE[] = {"MEM_USAGE", "Memory Usage:\n"};
56 static const char *CPU_USAGE[] = {"FAULTCPU", "CPU Usage:"};
57 static const char *TRACE_ID[] = {"TRACEID", "Trace-Id:"};
58 static const char *SUMMARY[] = {"SUMMARY", "Summary:\n"};
59 static const char *TIMESTAMP[] = {"TIMESTAMP", "Timestamp:"};
60
61 auto CPP_CRASH_LOG_SEQUENCE = {
62 DEVICE_INFO, BUILD_INFO, TIMESTAMP, MODULE_NAME, MODULE_VERSION, MODULE_PID, MODULE_UID, FAULT_TYPE,
63 SYSVMTYPE, APPVMTYPE, REASON, FAULT_MESSAGE, TRACE_ID, PROCESS_NAME, KEY_THREAD_INFO,
64 KEY_THREAD_REGISTERS, OTHER_THREAD_INFO
65 };
66
67 auto JAVASCRIPT_CRASH_LOG_SEQUENCE = {
68 DEVICE_INFO, BUILD_INFO, TIMESTAMP, MODULE_NAME, MODULE_VERSION, MODULE_PID,
69 MODULE_UID, FAULT_TYPE, FAULT_MESSAGE, SYSVMTYPE, APPVMTYPE,
70 FOREGROUND, LIFETIME, REASON, TRACE_ID
71 };
72
73 auto APP_FREEZE_LOG_SEQUENCE = {
74 DEVICE_INFO, BUILD_INFO, TIMESTAMP, MODULE_NAME, MODULE_VERSION, MODULE_PID,
75 MODULE_UID, FAULT_TYPE, SYSVMTYPE, APPVMTYPE, REASON,
76 TRACE_ID, CPU_USAGE, MEMORY_USAGE, ROOT_CAUSE, STACKTRACE,
77 MSG_QUEUE_INFO, BINDER_TRANSACTION_INFO, PROCESS_STACKTRACE
78 };
79
80 auto SYS_FREEZE_LOG_SEQUENCE = {
81 DEVICE_INFO, BUILD_INFO, TIMESTAMP, MODULE_NAME, MODULE_VERSION, MODULE_PID,
82 MODULE_UID, FAULT_TYPE, SYSVMTYPE, APPVMTYPE, REASON,
83 TRACE_ID, CPU_USAGE, MEMORY_USAGE, ROOT_CAUSE, STACKTRACE,
84 MSG_QUEUE_INFO, BINDER_TRANSACTION_INFO, PROCESS_STACKTRACE
85 };
86
87 auto RUST_PANIC_LOG_SEQUENCE = {
88 DEVICE_INFO, BUILD_INFO, TIMESTAMP, MODULE_NAME, MODULE_VERSION, MODULE_PID,
89 MODULE_UID, FAULT_TYPE, FAULT_MESSAGE, APPVMTYPE, REASON
90 };
91
GetLogParseList(int32_t logType)92 std::list<const char **> GetLogParseList(int32_t logType)
93 {
94 switch (logType) {
95 case FaultLogType::CPP_CRASH:
96 return CPP_CRASH_LOG_SEQUENCE;
97 case FaultLogType::JS_CRASH:
98 return JAVASCRIPT_CRASH_LOG_SEQUENCE;
99 case FaultLogType::APP_FREEZE:
100 return APP_FREEZE_LOG_SEQUENCE;
101 case FaultLogType::SYS_FREEZE:
102 return SYS_FREEZE_LOG_SEQUENCE;
103 case FaultLogType::RUST_PANIC:
104 return RUST_PANIC_LOG_SEQUENCE;
105 default:
106 return std::list<const char **>();
107 }
108 }
109
GetSummaryByType(int32_t logType,std::map<std::string,std::string> sections)110 std::string GetSummaryByType(int32_t logType, std::map<std::string, std::string> sections)
111 {
112 std::string summary = "";
113 switch (logType) {
114 case FaultLogType::JS_CRASH:
115 case FaultLogType::APP_FREEZE:
116 case FaultLogType::SYS_FREEZE:
117 summary = sections[STACKTRACE[LOG_MAP_KEY]];
118 break;
119 case FaultLogType::CPP_CRASH:
120 summary = sections[KEY_THREAD_INFO[LOG_MAP_KEY]];
121 break;
122 default:
123 summary = "Could not figure out summary for this fault.";
124 break;
125 }
126
127 return summary;
128 }
129
ParseFaultLogLine(const std::list<const char ** > & parseList,const std::string & line,const std::string & multline,std::string & multlineName,FaultLogInfo & info)130 bool ParseFaultLogLine(const std::list<const char **>& parseList, const std::string& line, const std::string& multline,
131 std::string& multlineName, FaultLogInfo& info)
132 {
133 for (auto &item : parseList) {
134 std::string sectionHead = std::string(item[LOG_MAP_VALUE], strlen(item[LOG_MAP_VALUE]) - 1);
135 if (line.find(sectionHead) != std::string::npos) {
136 if (line.at(line.size() - 1) == ':') {
137 if ((item[LOG_MAP_KEY] != multlineName) && (!multline.empty())) {
138 info.sectionMap[multlineName] = multline;
139 }
140 multlineName = item[LOG_MAP_KEY];
141 } else {
142 info.sectionMap[item[LOG_MAP_KEY]] = line.substr(line.find_first_of(":") + 1);
143 }
144 return false;
145 }
146 }
147 return true;
148 }
149
WriteStackTraceFromLog(int32_t fd,const std::string & pidStr,const std::string & path)150 void WriteStackTraceFromLog(int32_t fd, const std::string& pidStr, const std::string& path)
151 {
152 std::string realPath;
153 if (!FileUtil::PathToRealPath(path, realPath)) {
154 FileUtil::SaveStringToFd(fd, "Log file not exist.\n");
155 return;
156 }
157
158 std::ifstream logFile(realPath);
159 std::string line;
160 bool startWrite = false;
161 while (std::getline(logFile, line)) {
162 if (!logFile.good()) {
163 break;
164 }
165
166 if (line.empty()) {
167 continue;
168 }
169
170 if ((line.find("----- pid") != std::string::npos) &&
171 (line.find(pidStr) != std::string::npos)) {
172 startWrite = true;
173 }
174
175 if ((line.find("----- end") != std::string::npos) &&
176 (line.find(pidStr) != std::string::npos)) {
177 FileUtil::SaveStringToFd(fd, line + "\n");
178 break;
179 }
180
181 if (startWrite) {
182 FileUtil::SaveStringToFd(fd, line + "\n");
183 }
184 }
185 }
186
WriteDfxLogToFile(int32_t fd)187 void WriteDfxLogToFile(int32_t fd)
188 {
189 std::string dfxStr = std::string("Generated by HiviewDFX@OpenHarmony\n");
190 std::string sepStr = std::string("================================================================\n");
191 FileUtil::SaveStringToFd(fd, dfxStr);
192 FileUtil::SaveStringToFd(fd, sepStr);
193 }
194
WriteFaultLogToFile(int32_t fd,int32_t logType,std::map<std::string,std::string> sections)195 void WriteFaultLogToFile(int32_t fd, int32_t logType, std::map<std::string, std::string> sections)
196 {
197 std::list<const char **> seq = GetLogParseList(logType);
198 for (auto &item : seq) {
199 auto value = sections[item[LOG_MAP_KEY]];
200 if (!value.empty()) {
201 FileUtil::SaveStringToFd(fd, item[LOG_MAP_VALUE]);
202 if (value.back() != '\n') {
203 value.append("\n");
204 }
205 FileUtil::SaveStringToFd(fd, value);
206 }
207 }
208
209 auto summary = sections[SUMMARY[LOG_MAP_KEY]];
210 if (!summary.empty()) {
211 FileUtil::SaveStringToFd(fd, summary);
212 }
213
214 if (!sections["KEYLOGFILE"].empty()) {
215 FileUtil::SaveStringToFd(fd, "Additional Logs:\n");
216 WriteStackTraceFromLog(fd, sections["PID"], sections["KEYLOGFILE"]);
217 }
218 }
219
UpdateFaultLogInfoFromTempFile(FaultLogInfo & info)220 static void UpdateFaultLogInfoFromTempFile(FaultLogInfo& info)
221 {
222 if (!info.module.empty()) {
223 return;
224 }
225
226 StringUtil::ConvertStringTo<int32_t>(info.sectionMap[MODULE_UID[LOG_MAP_KEY]], info.id);
227 info.module = info.sectionMap[PROCESS_NAME[LOG_MAP_KEY]];
228 info.reason = info.sectionMap[REASON[LOG_MAP_KEY]];
229 info.summary = info.sectionMap[KEY_THREAD_INFO[LOG_MAP_KEY]];
230 size_t removeStartPos = info.summary.find("Tid:");
231 size_t removeEndPos = info.summary.find("Name:");
232 if (removeStartPos != std::string::npos && removeEndPos != std::string::npos) {
233 auto iterator = info.summary.begin() + removeEndPos;
234 while (iterator != info.summary.end() && *iterator != '\n') {
235 if (isdigit(*iterator)) {
236 iterator = info.summary.erase(iterator);
237 } else {
238 iterator++;
239 }
240 }
241 info.summary.replace(removeStartPos, removeEndPos - removeStartPos + 1, "Thread n");
242 }
243 info.sectionMap.clear();
244 }
245
ParseFaultLogInfoFromFile(const std::string & path,bool isTempFile)246 FaultLogInfo ParseFaultLogInfoFromFile(const std::string &path, bool isTempFile)
247 {
248 auto fileName = FileUtil::ExtractFileName(path);
249 FaultLogInfo info;
250 if (!isTempFile) {
251 info = ExtractInfoFromFileName(fileName);
252 } else {
253 info = ExtractInfoFromTempFile(fileName);
254 }
255
256 auto parseList = GetLogParseList(info.faultLogType);
257 std::ifstream logFile(path);
258 std::string line;
259 std::string multline;
260 std::string multlineName;
261 while (std::getline(logFile, line)) {
262 if (!logFile.good()) {
263 break;
264 }
265
266 if (line.empty()) {
267 continue;
268 }
269
270 if (ParseFaultLogLine(parseList, line, multline, multlineName, info)) {
271 multline.append(line).append("\n");
272 }
273 }
274
275 if (!multline.empty() && !multlineName.empty()) {
276 info.sectionMap[multlineName] = multline;
277 }
278 UpdateFaultLogInfoFromTempFile(info);
279 return info;
280 }
281
WriteLogToFile(int32_t fd,const std::string & path)282 bool WriteLogToFile(int32_t fd, const std::string& path)
283 {
284 if ((fd < 0) || path.empty()) {
285 return false;
286 }
287
288 std::string line;
289 std::ifstream logFile(path);
290 while (std::getline(logFile, line)) {
291 if (!logFile.good()) {
292 return false;
293 }
294 FileUtil::SaveStringToFd(fd, line);
295 FileUtil::SaveStringToFd(fd, "\n");
296 }
297 return true;
298 }
299 } // namespace FaultLogger
300 } // namespace HiviewDFX
301 } // namespace OHOS
302