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
16 #include <cstdint>
17 #include <mutex>
18 #include <string>
19 #include <vector>
20
21 #include "bundle_info.h"
22 #include "bundle_mgr_interface.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26
27 #include "constants.h"
28 #include "faultlog_info.h"
29 #include "logger.h"
30 #include "string_util.h"
31 #include "time_util.h"
32
33 using namespace OHOS::AAFwk;
34
35 namespace OHOS {
36 namespace HiviewDFX {
37 DEFINE_LOG_TAG("Faultlogger-util");
38 namespace {
39 constexpr int DEFAULT_BUFFER_SIZE = 64;
40
GetBundleMgrProxy()41 sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy()
42 {
43 sptr<ISystemAbilityManager> systemAbilityManager =
44 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (!systemAbilityManager) {
46 return nullptr;
47 }
48
49 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
50 if (!remoteObject) {
51 return nullptr;
52 }
53
54 return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
55 }
56 } // namespace
57
GetFormatedTime(uint64_t time)58 std::string GetFormatedTime(uint64_t time)
59 {
60 if (time > LONG_MAX) {
61 return "00000000000000";
62 }
63
64 time_t out = static_cast<time_t>(time);
65 struct tm tmStruct {0};
66 struct tm* timeInfo = localtime_r(&out, &tmStruct);
67 if (timeInfo == nullptr) {
68 return "00000000000000";
69 }
70
71 char buf[DEFAULT_BUFFER_SIZE] = {0};
72 strftime(buf, DEFAULT_BUFFER_SIZE - 1, "%Y%m%d%H%M%S", timeInfo);
73 return std::string(buf, strlen(buf));
74 }
75
GetFaultNameByType(int32_t faultType,bool asFileName)76 std::string GetFaultNameByType(int32_t faultType, bool asFileName)
77 {
78 switch (faultType) {
79 case FaultLogType::JS_CRASH:
80 return asFileName ? "jscrash" : "JS_ERROR";
81 case FaultLogType::JAVA_CRASH:
82 return asFileName ? "javacrash" : "JAVA_CRASH";
83 case FaultLogType::CPP_CRASH:
84 return asFileName ? "cppcrash" : "CPP_CRASH";
85 case FaultLogType::APP_FREEZE:
86 return asFileName ? "appfreeze" : "APP_FREEZE";
87 case FaultLogType::SYS_FREEZE:
88 return asFileName ? "sysfreeze" : "SYS_FREEZE";
89 default:
90 break;
91 }
92 return "Unknown";
93 }
94
GetFaultLogName(const FaultLogInfo & info)95 std::string GetFaultLogName(const FaultLogInfo& info)
96 {
97 std::string name = info.module;
98 if (name.find("/") != std::string::npos) {
99 name = info.module.substr(info.module.find_last_of("/") + 1);
100 }
101
102 std::string ret = "";
103 ret.append(GetFaultNameByType(info.faultLogType, true));
104 ret.append("-");
105 ret.append(name);
106 ret.append("-");
107 ret.append(std::to_string(info.id));
108 ret.append("-");
109 ret.append(GetFormatedTime(info.time));
110 return ret;
111 }
112
GetLogTypeByName(const std::string & type)113 int32_t GetLogTypeByName(const std::string& type)
114 {
115 if (type == "jscrash") {
116 return FaultLogType::JS_CRASH;
117 } else if (type == "javacrash") {
118 return FaultLogType::JAVA_CRASH;
119 } else if (type == "cppcrash") {
120 return FaultLogType::CPP_CRASH;
121 } else if (type == "appfreeze") {
122 return FaultLogType::APP_FREEZE;
123 } else if (type == "sysfreeze") {
124 return FaultLogType::SYS_FREEZE;
125 } else if (type == "all" || type == "ALL") {
126 return FaultLogType::ALL;
127 } else {
128 return -1;
129 }
130 }
131
ExtractInfoFromFileName(const std::string & fileName)132 FaultLogInfo ExtractInfoFromFileName(const std::string& fileName)
133 {
134 // FileName LogType-PackageName-Uid-YYYYMMDDHHMMSS
135 FaultLogInfo info;
136 std::vector<std::string> splitStr;
137 const int32_t expectedVecSize = 4;
138 StringUtil::SplitStr(fileName, "-", splitStr);
139 if (splitStr.size() == expectedVecSize) {
140 info.faultLogType = GetLogTypeByName(splitStr[0]); // 0 : index of log type
141 info.module = splitStr[1]; // 1 : index of module name
142 StringUtil::ConvertStringTo<int32_t>(splitStr[2], info.id); // 2 : index of uid
143 info.time = TimeUtil::StrToTimeStamp(splitStr[3], "%Y%m%d%H%M%S"); // 3 : index of timestamp
144 }
145 info.pid = 0;
146 return info;
147 }
148
ExtractInfoFromTempFile(const std::string & fileName)149 FaultLogInfo ExtractInfoFromTempFile(const std::string& fileName)
150 {
151 // FileName LogType-pid-time
152 FaultLogInfo info;
153 std::vector<std::string> splitStr;
154 const int32_t expectedVecSize = 3;
155 StringUtil::SplitStr(fileName, "-", splitStr);
156 if (splitStr.size() == expectedVecSize) {
157 info.faultLogType = GetLogTypeByName(splitStr[0]); // 0 : index of log type
158 StringUtil::ConvertStringTo<int32_t>(splitStr[1], info.pid); // 1 : index of pid
159 StringUtil::ConvertStringTo<int64_t>(splitStr[2], info.time); // 2 : index of timestamp
160 }
161 return info;
162 }
163
RegulateModuleNameIfNeed(const std::string & name)164 std::string RegulateModuleNameIfNeed(const std::string& name)
165 {
166 std::vector<std::string> splitStr;
167 StringUtil::SplitStr(name, "/\\", splitStr);
168 auto size = splitStr.size();
169 if (size > 0) {
170 return splitStr[size - 1];
171 }
172 return name;
173 }
174
GetApplicationNamesById(int32_t uid)175 std::vector<std::string> GetApplicationNamesById(int32_t uid)
176 {
177 std::vector<std::string> bundleNames;
178 sptr<AppExecFwk::IBundleMgr> mgr = GetBundleMgrProxy();
179 if (mgr != nullptr) {
180 HIVIEW_LOGD("mgr != nullptr");
181 mgr->GetBundlesForUid(uid, bundleNames);
182 } else {
183 HIVIEW_LOGD("mgr == nullptr");
184 }
185 HIVIEW_LOGD("bundleNames is %{public}d", bundleNames.size());
186 return bundleNames;
187 }
188
GetApplicationNameById(int32_t uid)189 std::string GetApplicationNameById(int32_t uid)
190 {
191 HIVIEW_LOGD("called");
192 std::vector<std::string> bundleNames = GetApplicationNamesById(uid);
193 if (bundleNames.empty()) {
194 return "";
195 }
196
197 return bundleNames.front();
198 }
199
GetApplicationVersion(int32_t uid,const std::string & bundleName)200 std::string GetApplicationVersion(int32_t uid, const std::string& bundleName)
201 {
202 sptr<AppExecFwk::IBundleMgr> mgr = GetBundleMgrProxy();
203 AppExecFwk::BundleInfo info;
204 if ((mgr != nullptr) &&
205 (mgr->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, info) != ERR_OK)) {
206 return "";
207 }
208 return info.versionName;
209 }
210 } // namespace HiviewDFX
211 } // namespace OHOS
212