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
16 #include <cstdint>
17 #include <ctime>
18 #include <mutex>
19 #include <string>
20 #include <sys/stat.h>
21 #include <vector>
22
23 #include "constants.h"
24 #include "faultlog_info.h"
25 #include "string_util.h"
26 #include "time_util.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 constexpr int DEFAULT_BUFFER_SIZE = 64;
32 } // namespace
33
GetFormatedTime(uint64_t target)34 std::string GetFormatedTime(uint64_t target)
35 {
36 time_t now = time(nullptr);
37 if (target > static_cast<uint64_t>(now)) {
38 target = target / 1000; // 1000 : convert millisecond to seconds
39 }
40
41 time_t out = static_cast<time_t>(target);
42 struct tm tmStruct {0};
43 struct tm* timeInfo = localtime_r(&out, &tmStruct);
44 if (timeInfo == nullptr) {
45 return "00000000000000";
46 }
47
48 char buf[DEFAULT_BUFFER_SIZE] = {0};
49 strftime(buf, DEFAULT_BUFFER_SIZE - 1, "%Y%m%d%H%M%S", timeInfo);
50 return std::string(buf, strlen(buf));
51 }
52
GetFaultNameByType(int32_t faultType,bool asFileName)53 std::string GetFaultNameByType(int32_t faultType, bool asFileName)
54 {
55 switch (faultType) {
56 case FaultLogType::JS_CRASH:
57 return asFileName ? "jscrash" : "JS_ERROR";
58 case FaultLogType::CPP_CRASH:
59 return asFileName ? "cppcrash" : "CPP_CRASH";
60 case FaultLogType::APP_FREEZE:
61 return asFileName ? "appfreeze" : "APP_FREEZE";
62 case FaultLogType::SYS_FREEZE:
63 return asFileName ? "sysfreeze" : "SYS_FREEZE";
64 case FaultLogType::RUST_PANIC:
65 return asFileName ? "rustpanic" : "RUST_PANIC";
66 default:
67 break;
68 }
69 return "Unknown";
70 }
71
GetFaultLogName(const FaultLogInfo & info)72 std::string GetFaultLogName(const FaultLogInfo& info)
73 {
74 std::string name = info.module;
75 if (name.find("/") != std::string::npos) {
76 name = info.module.substr(info.module.find_last_of("/") + 1);
77 }
78
79 std::string ret = "";
80 ret.append(GetFaultNameByType(info.faultLogType, true));
81 ret.append("-");
82 ret.append(name);
83 ret.append("-");
84 ret.append(std::to_string(info.id));
85 ret.append("-");
86 ret.append(GetFormatedTime(info.time));
87 return ret;
88 }
89
GetLogTypeByName(const std::string & type)90 int32_t GetLogTypeByName(const std::string& type)
91 {
92 if (type == "jscrash") {
93 return FaultLogType::JS_CRASH;
94 } else if (type == "cppcrash") {
95 return FaultLogType::CPP_CRASH;
96 } else if (type == "appfreeze") {
97 return FaultLogType::APP_FREEZE;
98 } else if (type == "sysfreeze") {
99 return FaultLogType::SYS_FREEZE;
100 } else if (type == "all" || type == "ALL") {
101 return FaultLogType::ALL;
102 } else {
103 return -1;
104 }
105 }
106
ExtractInfoFromFileName(const std::string & fileName)107 FaultLogInfo ExtractInfoFromFileName(const std::string& fileName)
108 {
109 // FileName LogType-PackageName-Uid-YYYYMMDDHHMMSS
110 FaultLogInfo info;
111 std::vector<std::string> splitStr;
112 const int32_t expectedVecSize = 4;
113 StringUtil::SplitStr(fileName, "-", splitStr);
114 if (splitStr.size() == expectedVecSize) {
115 info.faultLogType = GetLogTypeByName(splitStr[0]); // 0 : index of log type
116 info.module = splitStr[1]; // 1 : index of module name
117 StringUtil::ConvertStringTo<int32_t>(splitStr[2], info.id); // 2 : index of uid
118 info.time = TimeUtil::StrToTimeStamp(splitStr[3], "%Y%m%d%H%M%S"); // 3 : index of timestamp
119 }
120 info.pid = 0;
121 return info;
122 }
123
ExtractInfoFromTempFile(const std::string & fileName)124 FaultLogInfo ExtractInfoFromTempFile(const std::string& fileName)
125 {
126 // FileName LogType-pid-time
127 FaultLogInfo info;
128 std::vector<std::string> splitStr;
129 const int32_t expectedVecSize = 3;
130 StringUtil::SplitStr(fileName, "-", splitStr);
131 if (splitStr.size() == expectedVecSize) {
132 info.faultLogType = GetLogTypeByName(splitStr[0]); // 0 : index of log type
133 StringUtil::ConvertStringTo<int32_t>(splitStr[1], info.pid); // 1 : index of pid
134 StringUtil::ConvertStringTo<int64_t>(splitStr[2], info.time); // 2 : index of timestamp
135 }
136 return info;
137 }
138
RegulateModuleNameIfNeed(const std::string & name)139 std::string RegulateModuleNameIfNeed(const std::string& name)
140 {
141 std::vector<std::string> splitStr;
142 StringUtil::SplitStr(name, "/", splitStr);
143 auto size = splitStr.size();
144 if (size > 0) {
145 return splitStr[size - 1];
146 }
147 return name;
148 }
149
GetFileLastAccessTimeStamp(const std::string & fileName)150 time_t GetFileLastAccessTimeStamp(const std::string& fileName)
151 {
152 struct stat fileInfo;
153 if (stat(fileName.c_str(), &fileInfo) != 0) {
154 return 0;
155 }
156 return fileInfo.st_atime;
157 }
158 } // namespace HiviewDFX
159 } // namespace OHOS
160