17#include "llvm/Support/CachePruning.h"
23 static llvm::CachePruningPolicy policy;
24 static llvm::once_flag once_flag;
26 llvm::call_once(once_flag, []() {
36 policy.Interval = std::chrono::hours(1);
39 policy.MaxSizePercentageOfAvailableSpace =
49 llvm::Expected<bool> err_or_pruned = pruneCache(path, policy);
53 "failed to prune lldb index cache directory: {0}");
62 auto add_buffer = [
this](
unsigned task,
const llvm::Twine &moduleName,
63 std::unique_ptr<llvm::MemoryBuffer> m) {
67 llvm::Expected<llvm::FileCache> cache_or_err =
68 llvm::localCache(
"LLDBModuleCache",
"lldb-module", path, add_buffer);
74 "failed to create lldb index cache directory: {0}");
78std::unique_ptr<llvm::MemoryBuffer>
80 std::lock_guard<std::mutex> guard(
m_mutex);
82 const unsigned task = 1;
88 llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
94 if (add_stream_or_err) {
95 llvm::AddStreamFn &add_stream = *add_stream_or_err;
107 "failed to get the cache add stream callback for key: {0}");
110 return std::unique_ptr<llvm::MemoryBuffer>();
114 llvm::ArrayRef<uint8_t> data) {
115 std::lock_guard<std::mutex> guard(
m_mutex);
116 const unsigned task = 2;
120 llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
125 if (add_stream_or_err) {
126 llvm::AddStreamFn &add_stream = *add_stream_or_err;
135 llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =
136 add_stream(task,
"");
138 llvm::CachedFileStream *cfs = file_or_err->get();
139 cfs->OS->write((
const char *)data.data(), data.size());
140 if (llvm::Error err = cfs->commit()) {
143 "failed to commit to the cache for key: {0}");
149 "failed to get the cache file stream for key: {0}");
155 "failed to get the cache add stream callback for key: {0}");
162 std::string filename(
"llvmcache-");
163 filename += key.str();
171 if (!fs.
Exists(cache_file))
178 UUID uuid =
module->GetUUID();
182 std::time_t mod_time = 0;
198 std::time_t mod_time = 0;
208 llvm::sys::toTimeT(objfile->
GetModule()->GetObjectModificationTime());
225 llvm::ArrayRef<uint8_t> uuid_bytes =
m_uuid->GetBytes();
227 encoder.
AppendU8(uuid_bytes.size());
245 while (uint8_t sig_encoding = data.
GetU8(offset_ptr)) {
246 switch (sig_encoding) {
248 const uint8_t length = data.
GetU8(offset_ptr);
249 const uint8_t *bytes = (
const uint8_t *)data.
GetData(offset_ptr, length);
250 if (bytes !=
nullptr && length > 0)
251 m_uuid =
UUID(llvm::ArrayRef<uint8_t>(bytes, length));
254 uint32_t mod_time = data.
GetU32(offset_ptr);
259 uint32_t mod_time = data.
GetU32(offset_ptr);
311 llvm::StringRef identifier((
const char *)data.
GetData(offset_ptr, 4), 4);
314 const uint32_t length = data.
GetU32(offset_ptr);
318 const char *bytes = (
const char *)data.
GetData(offset_ptr, length);
319 if (bytes ==
nullptr)
321 m_data = llvm::StringRef(bytes, length);
326 if (offset >=
m_data.size())
327 return llvm::StringRef();
328 return llvm::StringRef(
m_data.data() + offset);
static const llvm::StringRef kStringTableIdentifier("STAB")
@ eSignatureObjectModTime
#define LLDB_LOG_ERROR(log, error,...)
bool Encode(DataEncoder &encoder)
std::vector< ConstString > m_strings
llvm::DenseMap< ConstString, uint32_t > m_string_to_offset
uint32_t Add(ConstString s)
Add a string into the string table.
uint32_t m_next_offset
Skip one byte to start the string table off with an empty string.
A uniqued constant string class.
size_t GetLength() const
Get the length in bytes of string value.
An binary data encoding class.
void AppendCString(llvm::StringRef data)
Append a C string to the end of the owned data.
void AppendU32(uint32_t value)
size_t GetByteSize() const
Get the number of bytes contained in this object.
uint32_t PutU32(uint32_t offset, uint32_t value)
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append a bytes to the end of the owned data.
FileSpec GetCacheFilePath(llvm::StringRef key)
Return the cache file that is associated with the key.
std::unique_ptr< llvm::MemoryBuffer > GetCachedData(llvm::StringRef key)
Get cached data from the cache directory for the specified key.
bool SetCachedData(llvm::StringRef key, llvm::ArrayRef< uint8_t > data)
Set cached data for the specified key.
llvm::FileCache m_cache_callback
std::unique_ptr< llvm::MemoryBuffer > m_mem_buff_up
DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy=DataFileCache::GetLLDBIndexCachePolicy())
Create a data file cache in the directory path that is specified, using the specified policy.
static llvm::CachePruningPolicy GetLLDBIndexCachePolicy()
Gets the default LLDB index cache policy, which is controlled by the "LLDBIndexCache" family of setti...
Status RemoveCacheFile(llvm::StringRef key)
Remove the cache file associated with the key.
void AppendPathComponent(llvm::StringRef component)
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
Status RemoveFile(const FileSpec &file_spec)
Remove a single file.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
uint64_t GetLLDBIndexCacheMaxByteSize()
uint64_t GetLLDBIndexCacheMaxPercent()
uint64_t GetLLDBIndexCacheExpirationDays()
static ModuleListProperties & GetGlobalModuleListProperties()
A class that describes an executable image and its associated object and symbol files.
const llvm::sys::TimePoint & GetObjectModificationTime() const
const llvm::sys::TimePoint & GetModificationTime() const
A plug-in interface definition class for object file parsers.
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
virtual UUID GetUUID()=0
Gets the UUID for this object file.
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
llvm::StringRef m_data
All of the strings in the string table are contained in m_data.
llvm::StringRef Get(uint32_t offset) const
Represents UUID's of various sizes.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
Decode a serialized version of this object from data.
bool Encode(DataEncoder &encoder) const
Encode this object into a data encoder object.
bool IsValid() const
Return true only if the CacheSignature is valid.
std::optional< std::time_t > m_mod_time
Modification time of file on disk.
std::optional< std::time_t > m_obj_mod_time
If this describes a .o file with a BSD archive, the BSD archive's modification time will be in m_mod_...
std::optional< UUID > m_uuid
UUID of object file or module.