• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "faultlog_manager.h"
16 
17 #include <cstdint>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include "constants.h"
29 #include "defines.h"
30 #include "file_util.h"
31 #include "log_store_ex.h"
32 #include "logger.h"
33 #include "time_util.h"
34 
35 #include "faultlog_database.h"
36 #include "faultlog_formatter.h"
37 #include "faultlog_info.h"
38 #include "faultlog_util.h"
39 
40 namespace OHOS {
41 namespace HiviewDFX {
42 using namespace FaultLogger;
43 DEFINE_LOG_TAG("FaultLogManager");
CreateLogFileFilter(time_t time,int32_t id,int32_t faultLogType,const std::string & module)44 LogStoreEx::LogFileFilter CreateLogFileFilter(time_t time, int32_t id, int32_t faultLogType, const std::string& module)
45 {
46     LogStoreEx::LogFileFilter filter = [time, id, faultLogType, module](const LogFile &file) {
47         FaultLogInfo info = ExtractInfoFromFileName(file.name_);
48         if (info.time <= time) {
49             return false;
50         }
51 
52         if ((id != -1) && (info.id != id)) {
53             return false;
54         }
55 
56         if ((faultLogType != 0) && (info.faultLogType != faultLogType)) {
57             return false;
58         }
59 
60         if ((!module.empty()) && (info.module != module)) {
61             return false;
62         }
63         return true;
64     };
65     return filter;
66 }
67 
~FaultLogManager()68 FaultLogManager::~FaultLogManager()
69 {
70     if (faultLogDb_ != nullptr) {
71         delete faultLogDb_;
72         faultLogDb_ = nullptr;
73     }
74 }
75 
CreateTempFaultLogFile(time_t time,int32_t id,int32_t faultType,const std::string & module) const76 int32_t FaultLogManager::CreateTempFaultLogFile(time_t time, int32_t id, int32_t faultType,
77     const std::string &module) const
78 {
79     FaultLogInfo info;
80     info.time = time;
81     info.id = id;
82     info.faultLogType = faultType;
83     info.module = module;
84     auto fileName = GetFaultLogName(info);
85     return store_->CreateLogFile(fileName);
86 }
87 
Init()88 void FaultLogManager::Init()
89 {
90     store_ = std::make_unique<LogStoreEx>(FaultLogger::DEFAULT_FAULTLOG_FOLDER, true);
91     LogStoreEx::LogFileComparator comparator = [](const LogFile &lhs, const LogFile &rhs) {
92         FaultLogInfo lhsInfo = ExtractInfoFromFileName(lhs.name_);
93         FaultLogInfo rhsInfo = ExtractInfoFromFileName(rhs.name_);
94         return lhsInfo.time > rhsInfo.time;
95     };
96     store_->SetLogFileComparator(comparator);
97     store_->Init();
98     faultLogDb_ = new FaultLogDatabase();
99 }
100 
SaveFaultLogToFile(FaultLogInfo & info) const101 std::string FaultLogManager::SaveFaultLogToFile(FaultLogInfo &info) const
102 {
103     auto fileName = GetFaultLogName(info);
104     auto fd = store_->CreateLogFile(fileName);
105     if (fd < 0) {
106         return "";
107     }
108 
109     FaultLogger::WriteDfxLogToFile(fd);
110     FaultLogger::WriteFaultLogToFile(fd, info.faultLogType, info.sectionMap);
111     FaultLogger::WriteLogToFile(fd, info.logPath);
112     if (info.sectionMap.count("HILOG") == 1) {
113         FileUtil::SaveStringToFd(fd, "\nHiLog:\n");
114         FileUtil::SaveStringToFd(fd, info.sectionMap["HILOG"]);
115     }
116     close(fd);
117 
118     std::string logFile = info.logPath;
119     if (logFile != "" && FileUtil::FileExists(logFile)) {
120         if (!FileUtil::RemoveFile(logFile)) {
121             HIVIEW_LOGW("remove logFile %{public}s failed.", logFile.c_str());
122         } else {
123             HIVIEW_LOGI("remove logFile %{public}s.", logFile.c_str());
124         }
125     }
126     store_->ClearSameLogFilesIfNeeded(CreateLogFileFilter(0, info.id, info.faultLogType, info.module),
127         MAX_FAULT_LOG_PER_HAP);
128     info.logPath = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + fileName;
129     HIVIEW_LOGI("create log %{public}s", fileName.c_str());
130     return fileName;
131 }
132 
GetFaultInfoList(const std::string & module,int32_t id,int32_t faultType,int32_t maxNum) const133 std::list<FaultLogInfo> FaultLogManager::GetFaultInfoList(const std::string& module,
134     int32_t id, int32_t faultType, int32_t maxNum) const
135 {
136     std::list<FaultLogInfo> ret;
137     if (faultLogDb_ != nullptr) {
138         ret = faultLogDb_->GetFaultInfoList(module, id, faultType, maxNum);
139         HIVIEW_LOGI("Find %{public}zu fault records for uid:%{public}d type:%{public}d",
140             ret.size(), id, faultType);
141     }
142     return ret;
143 }
144 
SaveFaultInfoToRawDb(FaultLogInfo & info) const145 void FaultLogManager::SaveFaultInfoToRawDb(FaultLogInfo& info) const
146 {
147     if (faultLogDb_ != nullptr) {
148         faultLogDb_->SaveFaultLogInfo(info);
149     }
150 }
151 
ReduceLogFileListSize(std::list<std::string> & infoVec,int32_t maxNum) const152 void FaultLogManager::ReduceLogFileListSize(std::list<std::string> &infoVec, int32_t maxNum) const
153 {
154     if ((maxNum < 0) || (infoVec.size() <= static_cast<uint32_t>(maxNum))) {
155         return;
156     }
157 
158     auto begin = infoVec.begin();
159     std::advance(begin, maxNum);
160     infoVec.erase(begin, infoVec.end());
161 }
162 
GetFaultLogFileList(const std::string & module,time_t time,int32_t id,int32_t faultType,int32_t maxNum) const163 std::list<std::string> FaultLogManager::GetFaultLogFileList(const std::string &module, time_t time, int32_t id,
164                                                             int32_t faultType, int32_t maxNum) const
165 {
166     LogStoreEx::LogFileFilter filter = CreateLogFileFilter(time, id, faultType, module);
167     auto vec = store_->GetLogFiles(filter);
168     std::list<std::string> ret;
169     std::transform(vec.begin(), vec.end(), std::back_inserter(ret), [](const LogFile &file) { return file.path_; });
170     ReduceLogFileListSize(ret, maxNum);
171     return ret;
172 }
173 
GetFaultLogContent(const std::string & name,std::string & content) const174 bool FaultLogManager::GetFaultLogContent(const std::string &name, std::string &content) const
175 {
176     auto path = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + name;
177     return FileUtil::LoadStringFromFile(path, content);
178 }
179 
IsProcessedFault(int32_t pid,int32_t uid,int32_t faultType)180 bool FaultLogManager::IsProcessedFault(int32_t pid, int32_t uid, int32_t faultType)
181 {
182     if (faultLogDb_ == nullptr) {
183         return false;
184     }
185 
186     return faultLogDb_->IsFaultExist(pid, uid, faultType);
187 }
188 } // namespace HiviewDFX
189 } // namespace OHOS
190