llvm-project
321 строка · 11.8 Кб
1//===-- DataFileCache.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/DataFileCache.h"10#include "lldb/Core/Module.h"11#include "lldb/Core/ModuleList.h"12#include "lldb/Host/FileSystem.h"13#include "lldb/Symbol/ObjectFile.h"14#include "lldb/Utility/DataEncoder.h"15#include "lldb/Utility/LLDBLog.h"16#include "lldb/Utility/Log.h"17#include "llvm/Support/CachePruning.h"18
19using namespace lldb_private;20
21
22llvm::CachePruningPolicy DataFileCache::GetLLDBIndexCachePolicy() {23static llvm::CachePruningPolicy policy;24static llvm::once_flag once_flag;25
26llvm::call_once(once_flag, []() {27// Prune the cache based off of the LLDB settings each time we create a28// cache object.29ModuleListProperties &properties =30ModuleList::GetGlobalModuleListProperties();31// Only scan once an hour. If we have lots of debug sessions we don't want32// to scan this directory too often. A timestamp file is written to the33// directory to ensure different processes don't scan the directory too34// often. This setting doesn't mean that a thread will continually scan the35// cache directory within this process.36policy.Interval = std::chrono::hours(1);37// Get the user settings for pruning.38policy.MaxSizeBytes = properties.GetLLDBIndexCacheMaxByteSize();39policy.MaxSizePercentageOfAvailableSpace =40properties.GetLLDBIndexCacheMaxPercent();41policy.Expiration =42std::chrono::hours(properties.GetLLDBIndexCacheExpirationDays() * 24);43});44return policy;45}
46
47DataFileCache::DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy) {48m_cache_dir.SetPath(path);49pruneCache(path, policy);50
51// This lambda will get called when the data is gotten from the cache and52// also after the data was set for a given key. We only need to take53// ownership of the data if we are geting the data, so we use the54// m_take_ownership member variable to indicate if we need to take55// ownership.56
57auto add_buffer = [this](unsigned task, const llvm::Twine &moduleName,58std::unique_ptr<llvm::MemoryBuffer> m) {59if (m_take_ownership)60m_mem_buff_up = std::move(m);61};62llvm::Expected<llvm::FileCache> cache_or_err =63llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);64if (cache_or_err)65m_cache_callback = std::move(*cache_or_err);66else {67Log *log = GetLog(LLDBLog::Modules);68LLDB_LOG_ERROR(log, cache_or_err.takeError(),69"failed to create lldb index cache directory: {0}");70}71}
72
73std::unique_ptr<llvm::MemoryBuffer>74DataFileCache::GetCachedData(llvm::StringRef key) {75std::lock_guard<std::mutex> guard(m_mutex);76
77const unsigned task = 1;78m_take_ownership = true;79// If we call the "m_cache_callback" function and the data is cached, it will80// call the "add_buffer" lambda function from the constructor which will in81// turn take ownership of the member buffer that is passed to the callback and82// put it into a member variable.83llvm::Expected<llvm::AddStreamFn> add_stream_or_err =84m_cache_callback(task, key, "");85m_take_ownership = false;86// At this point we either already called the "add_buffer" lambda with87// the data or we haven't. We can tell if we got the cached data by checking88// the add_stream function pointer value below.89if (add_stream_or_err) {90llvm::AddStreamFn &add_stream = *add_stream_or_err;91// If the "add_stream" is nullptr, then the data was cached and we already92// called the "add_buffer" lambda. If it is valid, then if we were to call93// the add_stream function it would cause a cache file to get generated94// and we would be expected to fill in the data. In this function we only95// want to check if the data was cached, so we don't want to call96// "add_stream" in this function.97if (!add_stream)98return std::move(m_mem_buff_up);99} else {100Log *log = GetLog(LLDBLog::Modules);101LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),102"failed to get the cache add stream callback for key: {0}");103}104// Data was not cached.105return std::unique_ptr<llvm::MemoryBuffer>();106}
107
108bool DataFileCache::SetCachedData(llvm::StringRef key,109llvm::ArrayRef<uint8_t> data) {110std::lock_guard<std::mutex> guard(m_mutex);111const unsigned task = 2;112// If we call this function and the data is cached, it will call the113// add_buffer lambda function from the constructor which will ignore the114// data.115llvm::Expected<llvm::AddStreamFn> add_stream_or_err =116m_cache_callback(task, key, "");117// If we reach this code then we either already called the callback with118// the data or we haven't. We can tell if we had the cached data by checking119// the CacheAddStream function pointer value below.120if (add_stream_or_err) {121llvm::AddStreamFn &add_stream = *add_stream_or_err;122// If the "add_stream" is nullptr, then the data was cached. If it is123// valid, then if we call the add_stream function with a task it will124// cause the file to get generated, but we only want to check if the data125// is cached here, so we don't want to call it here. Note that the126// add_buffer will also get called in this case after the data has been127// provided, but we won't take ownership of the memory buffer as we just128// want to write the data.129if (add_stream) {130llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =131add_stream(task, "");132if (file_or_err) {133llvm::CachedFileStream *cfs = file_or_err->get();134cfs->OS->write((const char *)data.data(), data.size());135return true;136} else {137Log *log = GetLog(LLDBLog::Modules);138LLDB_LOG_ERROR(log, file_or_err.takeError(),139"failed to get the cache file stream for key: {0}");140}141}142} else {143Log *log = GetLog(LLDBLog::Modules);144LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),145"failed to get the cache add stream callback for key: {0}");146}147return false;148}
149
150FileSpec DataFileCache::GetCacheFilePath(llvm::StringRef key) {151FileSpec cache_file(m_cache_dir);152std::string filename("llvmcache-");153filename += key.str();154cache_file.AppendPathComponent(filename);155return cache_file;156}
157
158Status DataFileCache::RemoveCacheFile(llvm::StringRef key) {159FileSpec cache_file = GetCacheFilePath(key);160FileSystem &fs = FileSystem::Instance();161if (!fs.Exists(cache_file))162return Status();163return fs.RemoveFile(cache_file);164}
165
166CacheSignature::CacheSignature(lldb_private::Module *module) {167Clear();168UUID uuid = module->GetUUID();169if (uuid.IsValid())170m_uuid = uuid;171
172std::time_t mod_time = 0;173mod_time = llvm::sys::toTimeT(module->GetModificationTime());174if (mod_time != 0)175m_mod_time = mod_time;176
177mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());178if (mod_time != 0)179m_obj_mod_time = mod_time;180}
181
182CacheSignature::CacheSignature(lldb_private::ObjectFile *objfile) {183Clear();184UUID uuid = objfile->GetUUID();185if (uuid.IsValid())186m_uuid = uuid;187
188std::time_t mod_time = 0;189// Grab the modification time of the object file's file. It isn't always the190// same as the module's file when you have a executable file as the main191// executable, and you have a object file for a symbol file.192FileSystem &fs = FileSystem::Instance();193mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));194if (mod_time != 0)195m_mod_time = mod_time;196
197mod_time =198llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());199if (mod_time != 0)200m_obj_mod_time = mod_time;201}
202
203enum SignatureEncoding {204eSignatureUUID = 1u,205eSignatureModTime = 2u,206eSignatureObjectModTime = 3u,207eSignatureEnd = 255u,208};209
210bool CacheSignature::Encode(DataEncoder &encoder) const {211if (!IsValid())212return false; // Invalid signature, return false!213
214if (m_uuid) {215llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();216encoder.AppendU8(eSignatureUUID);217encoder.AppendU8(uuid_bytes.size());218encoder.AppendData(uuid_bytes);219}220if (m_mod_time) {221encoder.AppendU8(eSignatureModTime);222encoder.AppendU32(*m_mod_time);223}224if (m_obj_mod_time) {225encoder.AppendU8(eSignatureObjectModTime);226encoder.AppendU32(*m_obj_mod_time);227}228encoder.AppendU8(eSignatureEnd);229return true;230}
231
232bool CacheSignature::Decode(const lldb_private::DataExtractor &data,233lldb::offset_t *offset_ptr) {234Clear();235while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {236switch (sig_encoding) {237case eSignatureUUID: {238const uint8_t length = data.GetU8(offset_ptr);239const uint8_t *bytes = (const uint8_t *)data.GetData(offset_ptr, length);240if (bytes != nullptr && length > 0)241m_uuid = UUID(llvm::ArrayRef<uint8_t>(bytes, length));242} break;243case eSignatureModTime: {244uint32_t mod_time = data.GetU32(offset_ptr);245if (mod_time > 0)246m_mod_time = mod_time;247} break;248case eSignatureObjectModTime: {249uint32_t mod_time = data.GetU32(offset_ptr);250if (mod_time > 0)251m_obj_mod_time = mod_time;252} break;253case eSignatureEnd:254// The definition of is valid changed to only be valid if the UUID is255// valid so make sure that if we attempt to decode an old cache file256// that we will fail to decode the cache file if the signature isn't257// considered valid.258return IsValid();259default:260break;261}262}263return false;264}
265
266uint32_t ConstStringTable::Add(ConstString s) {267auto pos = m_string_to_offset.find(s);268if (pos != m_string_to_offset.end())269return pos->second;270const uint32_t offset = m_next_offset;271m_strings.push_back(s);272m_string_to_offset[s] = offset;273m_next_offset += s.GetLength() + 1;274return offset;275}
276
277static const llvm::StringRef kStringTableIdentifier("STAB");278
279bool ConstStringTable::Encode(DataEncoder &encoder) {280// Write an 4 character code into the stream. This will help us when decoding281// to make sure we find this identifier when decoding the string table to make282// sure we have the rigth data. It also helps to identify the string table283// when dumping the hex bytes in a cache file.284encoder.AppendData(kStringTableIdentifier);285size_t length_offset = encoder.GetByteSize();286encoder.AppendU32(0); // Total length of all strings which will be fixed up.287size_t strtab_offset = encoder.GetByteSize();288encoder.AppendU8(0); // Start the string table with an empty string.289for (auto s: m_strings) {290// Make sure all of the offsets match up with what we handed out!291assert(m_string_to_offset.find(s)->second ==292encoder.GetByteSize() - strtab_offset);293// Append the C string into the encoder294encoder.AppendCString(s.GetStringRef());295}296// Fixup the string table length.297encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);298return true;299}
300
301bool StringTableReader::Decode(const lldb_private::DataExtractor &data,302lldb::offset_t *offset_ptr) {303llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);304if (identifier != kStringTableIdentifier)305return false;306const uint32_t length = data.GetU32(offset_ptr);307// We always have at least one byte for the empty string at offset zero.308if (length == 0)309return false;310const char *bytes = (const char *)data.GetData(offset_ptr, length);311if (bytes == nullptr)312return false;313m_data = llvm::StringRef(bytes, length);314return true;315}
316
317llvm::StringRef StringTableReader::Get(uint32_t offset) const {318if (offset >= m_data.size())319return llvm::StringRef();320return llvm::StringRef(m_data.data() + offset);321}
322
323