#include "lldb/Core/DataFileCache.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/DataEncoder.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "llvm/Support/CachePruning.h"
using namespace lldb_private;
llvm::CachePruningPolicy DataFileCache::GetLLDBIndexCachePolicy() {
static llvm::CachePruningPolicy policy;
static llvm::once_flag once_flag;
llvm::call_once(once_flag, []() {
ModuleListProperties &properties =
ModuleList::GetGlobalModuleListProperties();
policy.Interval = std::chrono::hours(1);
policy.MaxSizeBytes = properties.GetLLDBIndexCacheMaxByteSize();
policy.MaxSizePercentageOfAvailableSpace =
properties.GetLLDBIndexCacheMaxPercent();
policy.Expiration =
std::chrono::hours(properties.GetLLDBIndexCacheExpirationDays() * 24);
});
return policy;
}
DataFileCache::DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy) {
m_cache_dir.SetPath(path);
pruneCache(path, policy);
auto add_buffer = [this](unsigned task, const llvm::Twine &moduleName,
std::unique_ptr<llvm::MemoryBuffer> m) {
if (m_take_ownership)
m_mem_buff_up = std::move(m);
};
llvm::Expected<llvm::FileCache> cache_or_err =
llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);
if (cache_or_err)
m_cache_callback = std::move(*cache_or_err);
else {
Log *log = GetLog(LLDBLog::Modules);
LLDB_LOG_ERROR(log, cache_or_err.takeError(),
"failed to create lldb index cache directory: {0}");
}
}
std::unique_ptr<llvm::MemoryBuffer>
DataFileCache::GetCachedData(llvm::StringRef key) {
std::lock_guard<std::mutex> guard(m_mutex);
const unsigned task = 1;
m_take_ownership = true;
llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
m_cache_callback(task, key, "");
m_take_ownership = false;
if (add_stream_or_err) {
llvm::AddStreamFn &add_stream = *add_stream_or_err;
if (!add_stream)
return std::move(m_mem_buff_up);
} else {
Log *log = GetLog(LLDBLog::Modules);
LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
"failed to get the cache add stream callback for key: {0}");
}
return std::unique_ptr<llvm::MemoryBuffer>();
}
bool DataFileCache::SetCachedData(llvm::StringRef key,
llvm::ArrayRef<uint8_t> data) {
std::lock_guard<std::mutex> guard(m_mutex);
const unsigned task = 2;
llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
m_cache_callback(task, key, "");
if (add_stream_or_err) {
llvm::AddStreamFn &add_stream = *add_stream_or_err;
if (add_stream) {
llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =
add_stream(task, "");
if (file_or_err) {
llvm::CachedFileStream *cfs = file_or_err->get();
cfs->OS->write((const char *)data.data(), data.size());
return true;
} else {
Log *log = GetLog(LLDBLog::Modules);
LLDB_LOG_ERROR(log, file_or_err.takeError(),
"failed to get the cache file stream for key: {0}");
}
}
} else {
Log *log = GetLog(LLDBLog::Modules);
LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
"failed to get the cache add stream callback for key: {0}");
}
return false;
}
FileSpec DataFileCache::GetCacheFilePath(llvm::StringRef key) {
FileSpec cache_file(m_cache_dir);
std::string filename("llvmcache-");
filename += key.str();
cache_file.AppendPathComponent(filename);
return cache_file;
}
Status DataFileCache::RemoveCacheFile(llvm::StringRef key) {
FileSpec cache_file = GetCacheFilePath(key);
FileSystem &fs = FileSystem::Instance();
if (!fs.Exists(cache_file))
return Status();
return fs.RemoveFile(cache_file);
}
CacheSignature::CacheSignature(lldb_private::Module *module) {
Clear();
UUID uuid = module->GetUUID();
if (uuid.IsValid())
m_uuid = uuid;
std::time_t mod_time = 0;
mod_time = llvm::sys::toTimeT(module->GetModificationTime());
if (mod_time != 0)
m_mod_time = mod_time;
mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());
if (mod_time != 0)
m_obj_mod_time = mod_time;
}
CacheSignature::CacheSignature(lldb_private::ObjectFile *objfile) {
Clear();
UUID uuid = objfile->GetUUID();
if (uuid.IsValid())
m_uuid = uuid;
std::time_t mod_time = 0;
FileSystem &fs = FileSystem::Instance();
mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));
if (mod_time != 0)
m_mod_time = mod_time;
mod_time =
llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());
if (mod_time != 0)
m_obj_mod_time = mod_time;
}
enum SignatureEncoding {
eSignatureUUID = 1u,
eSignatureModTime = 2u,
eSignatureObjectModTime = 3u,
eSignatureEnd = 255u,
};
bool CacheSignature::Encode(DataEncoder &encoder) const {
if (!IsValid())
return false;
if (m_uuid) {
llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();
encoder.AppendU8(eSignatureUUID);
encoder.AppendU8(uuid_bytes.size());
encoder.AppendData(uuid_bytes);
}
if (m_mod_time) {
encoder.AppendU8(eSignatureModTime);
encoder.AppendU32(*m_mod_time);
}
if (m_obj_mod_time) {
encoder.AppendU8(eSignatureObjectModTime);
encoder.AppendU32(*m_obj_mod_time);
}
encoder.AppendU8(eSignatureEnd);
return true;
}
bool CacheSignature::Decode(const lldb_private::DataExtractor &data,
lldb::offset_t *offset_ptr) {
Clear();
while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {
switch (sig_encoding) {
case eSignatureUUID: {
const uint8_t length = data.GetU8(offset_ptr);
const uint8_t *bytes = (const uint8_t *)data.GetData(offset_ptr, length);
if (bytes != nullptr && length > 0)
m_uuid = UUID(llvm::ArrayRef<uint8_t>(bytes, length));
} break;
case eSignatureModTime: {
uint32_t mod_time = data.GetU32(offset_ptr);
if (mod_time > 0)
m_mod_time = mod_time;
} break;
case eSignatureObjectModTime: {
uint32_t mod_time = data.GetU32(offset_ptr);
if (mod_time > 0)
m_obj_mod_time = mod_time;
} break;
case eSignatureEnd:
return IsValid();
default:
break;
}
}
return false;
}
uint32_t ConstStringTable::Add(ConstString s) {
auto pos = m_string_to_offset.find(s);
if (pos != m_string_to_offset.end())
return pos->second;
const uint32_t offset = m_next_offset;
m_strings.push_back(s);
m_string_to_offset[s] = offset;
m_next_offset += s.GetLength() + 1;
return offset;
}
static const llvm::StringRef kStringTableIdentifier("STAB");
bool ConstStringTable::Encode(DataEncoder &encoder) {
encoder.AppendData(kStringTableIdentifier);
size_t length_offset = encoder.GetByteSize();
encoder.AppendU32(0);
size_t strtab_offset = encoder.GetByteSize();
encoder.AppendU8(0);
for (auto s: m_strings) {
assert(m_string_to_offset.find(s)->second ==
encoder.GetByteSize() - strtab_offset);
encoder.AppendCString(s.GetStringRef());
}
encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);
return true;
}
bool StringTableReader::Decode(const lldb_private::DataExtractor &data,
lldb::offset_t *offset_ptr) {
llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
if (identifier != kStringTableIdentifier)
return false;
const uint32_t length = data.GetU32(offset_ptr);
if (length == 0)
return false;
const char *bytes = (const char *)data.GetData(offset_ptr, length);
if (bytes == nullptr)
return false;
m_data = llvm::StringRef(bytes, length);
return true;
}
llvm::StringRef StringTableReader::Get(uint32_t offset) const {
if (offset >= m_data.size())
return llvm::StringRef();
return llvm::StringRef(m_data.data() + offset);
}