• 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     close(fd);
113 
114     std::string logFile = info.logPath;
115     if (logFile != "" && FileUtil::FileExists(logFile)) {
116         if (!FileUtil::RemoveFile(logFile)) {
117             HIVIEW_LOGW("remove logFile %{public}s filed.", logFile.c_str());
118         }
119     }
120     store_->ClearSameLogFilesIfNeeded(CreateLogFileFilter(0, info.id, info.faultLogType, info.module),
121         MAX_FAULT_LOG_PER_HAP);
122     info.logPath = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + fileName;
123     HIVIEW_LOGI("create log %{public}s", fileName.c_str());
124     return fileName;
125 }
126 
GetFaultInfoList(const std::string & module,int32_t id,int32_t faultType,int32_t maxNum) const127 std::list<FaultLogInfo> FaultLogManager::GetFaultInfoList(const std::string& module,
128     int32_t id, int32_t faultType, int32_t maxNum) const
129 {
130     std::list<FaultLogInfo> ret;
131     if (faultLogDb_ != nullptr) {
132         ret = faultLogDb_->GetFaultInfoList(module, id, faultType, maxNum);
133         HIVIEW_LOGI("Find %{public}zu fault records for uid:%{public}d type:%{public}d",
134             ret.size(), id, faultType);
135     }
136     return ret;
137 }
138 
SaveFaultInfoToRawDb(FaultLogInfo & info) const139 void FaultLogManager::SaveFaultInfoToRawDb(FaultLogInfo& info) const
140 {
141     if (faultLogDb_ != nullptr) {
142         faultLogDb_->SaveFaultLogInfo(info);
143     }
144 }
145 
ReduceLogFileListSize(std::list<std::string> & infoVec,int32_t maxNum) const146 void FaultLogManager::ReduceLogFileListSize(std::list<std::string> &infoVec, int32_t maxNum) const
147 {
148     if ((maxNum < 0) || (infoVec.size() <= static_cast<uint32_t>(maxNum))) {
149         return;
150     }
151 
152     auto begin = infoVec.begin();
153     std::advance(begin, maxNum);
154     infoVec.erase(begin, infoVec.end());
155 }
156 
GetFaultLogFileList(const std::string & module,time_t time,int32_t id,int32_t faultType,int32_t maxNum) const157 std::list<std::string> FaultLogManager::GetFaultLogFileList(const std::string &module, time_t time, int32_t id,
158                                                             int32_t faultType, int32_t maxNum) const
159 {
160     LogStoreEx::LogFileFilter filter = CreateLogFileFilter(time, id, faultType, module);
161     auto vec = store_->GetLogFiles(filter);
162     std::list<std::string> ret;
163     std::transform(vec.begin(), vec.end(), std::back_inserter(ret), [](const LogFile &file) { return file.path_; });
164     ReduceLogFileListSize(ret, maxNum);
165     return ret;
166 }
167 
GetFaultLogContent(const std::string & name,std::string & content) const168 bool FaultLogManager::GetFaultLogContent(const std::string &name, std::string &content) const
169 {
170     auto path = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + name;
171     return FileUtil::LoadStringFromFile(path, content);
172 }
173 
IsProcessedFault(int32_t pid,int32_t uid,int32_t faultType)174 bool FaultLogManager::IsProcessedFault(int32_t pid, int32_t uid, int32_t faultType)
175 {
176     if (faultLogDb_ == nullptr) {
177         return false;
178     }
179 
180     return faultLogDb_->IsFaultExist(pid, uid, faultType);
181 }
182 } // namespace HiviewDFX
183 } // namespace OHOS
184