LLDB mainline
DataFileCache.cpp
Go to the documentation of this file.
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
10#include "lldb/Core/Module.h"
16#include "lldb/Utility/Log.h"
17#include "llvm/Support/CachePruning.h"
18
19using namespace lldb_private;
20
21
22llvm::CachePruningPolicy DataFileCache::GetLLDBIndexCachePolicy() {
23 static llvm::CachePruningPolicy policy;
24 static llvm::once_flag once_flag;
25
26 llvm::call_once(once_flag, []() {
27 // Prune the cache based off of the LLDB settings each time we create a
28 // cache object.
29 ModuleListProperties &properties =
31 // Only scan once an hour. If we have lots of debug sessions we don't want
32 // to scan this directory too often. A timestamp file is written to the
33 // directory to ensure different processes don't scan the directory too
34 // often. This setting doesn't mean that a thread will continually scan the
35 // cache directory within this process.
36 policy.Interval = std::chrono::hours(1);
37 // Get the user settings for pruning.
38 policy.MaxSizeBytes = properties.GetLLDBIndexCacheMaxByteSize();
39 policy.MaxSizePercentageOfAvailableSpace =
40 properties.GetLLDBIndexCacheMaxPercent();
41 policy.Expiration =
42 std::chrono::hours(properties.GetLLDBIndexCacheExpirationDays() * 24);
43 });
44 return policy;
45}
46
47DataFileCache::DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy) {
48 m_cache_dir.SetPath(path);
49 llvm::Expected<bool> err_or_pruned = pruneCache(path, policy);
50 if (!err_or_pruned) {
52 LLDB_LOG_ERROR(log, err_or_pruned.takeError(),
53 "failed to prune lldb index cache directory: {0}");
54 }
55
56 // This lambda will get called when the data is gotten from the cache and
57 // also after the data was set for a given key. We only need to take
58 // ownership of the data if we are geting the data, so we use the
59 // m_take_ownership member variable to indicate if we need to take
60 // ownership.
61
62 auto add_buffer = [this](unsigned task, const llvm::Twine &moduleName,
63 std::unique_ptr<llvm::MemoryBuffer> m) {
65 m_mem_buff_up = std::move(m);
66 };
67 llvm::Expected<llvm::FileCache> cache_or_err =
68 llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);
69 if (cache_or_err)
70 m_cache_callback = std::move(*cache_or_err);
71 else {
73 LLDB_LOG_ERROR(log, cache_or_err.takeError(),
74 "failed to create lldb index cache directory: {0}");
75 }
76}
77
78std::unique_ptr<llvm::MemoryBuffer>
79DataFileCache::GetCachedData(llvm::StringRef key) {
80 std::lock_guard<std::mutex> guard(m_mutex);
81
82 const unsigned task = 1;
83 m_take_ownership = true;
84 // If we call the "m_cache_callback" function and the data is cached, it will
85 // call the "add_buffer" lambda function from the constructor which will in
86 // turn take ownership of the member buffer that is passed to the callback and
87 // put it into a member variable.
88 llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
89 m_cache_callback(task, key, "");
90 m_take_ownership = false;
91 // At this point we either already called the "add_buffer" lambda with
92 // the data or we haven't. We can tell if we got the cached data by checking
93 // the add_stream function pointer value below.
94 if (add_stream_or_err) {
95 llvm::AddStreamFn &add_stream = *add_stream_or_err;
96 // If the "add_stream" is nullptr, then the data was cached and we already
97 // called the "add_buffer" lambda. If it is valid, then if we were to call
98 // the add_stream function it would cause a cache file to get generated
99 // and we would be expected to fill in the data. In this function we only
100 // want to check if the data was cached, so we don't want to call
101 // "add_stream" in this function.
102 if (!add_stream)
103 return std::move(m_mem_buff_up);
104 } else {
106 LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
107 "failed to get the cache add stream callback for key: {0}");
108 }
109 // Data was not cached.
110 return std::unique_ptr<llvm::MemoryBuffer>();
111}
112
113bool DataFileCache::SetCachedData(llvm::StringRef key,
114 llvm::ArrayRef<uint8_t> data) {
115 std::lock_guard<std::mutex> guard(m_mutex);
116 const unsigned task = 2;
117 // If we call this function and the data is cached, it will call the
118 // add_buffer lambda function from the constructor which will ignore the
119 // data.
120 llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
121 m_cache_callback(task, key, "");
122 // If we reach this code then we either already called the callback with
123 // the data or we haven't. We can tell if we had the cached data by checking
124 // the CacheAddStream function pointer value below.
125 if (add_stream_or_err) {
126 llvm::AddStreamFn &add_stream = *add_stream_or_err;
127 // If the "add_stream" is nullptr, then the data was cached. If it is
128 // valid, then if we call the add_stream function with a task it will
129 // cause the file to get generated, but we only want to check if the data
130 // is cached here, so we don't want to call it here. Note that the
131 // add_buffer will also get called in this case after the data has been
132 // provided, but we won't take ownership of the memory buffer as we just
133 // want to write the data.
134 if (add_stream) {
135 llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =
136 add_stream(task, "");
137 if (file_or_err) {
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()) {
142 LLDB_LOG_ERROR(log, std::move(err),
143 "failed to commit to the cache for key: {0}");
144 }
145 return true;
146 } else {
148 LLDB_LOG_ERROR(log, file_or_err.takeError(),
149 "failed to get the cache file stream for key: {0}");
150 }
151 }
152 } else {
154 LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
155 "failed to get the cache add stream callback for key: {0}");
156 }
157 return false;
158}
159
161 FileSpec cache_file(m_cache_dir);
162 std::string filename("llvmcache-");
163 filename += key.str();
164 cache_file.AppendPathComponent(filename);
165 return cache_file;
166}
167
169 FileSpec cache_file = GetCacheFilePath(key);
171 if (!fs.Exists(cache_file))
172 return Status();
173 return fs.RemoveFile(cache_file);
174}
175
177 Clear();
178 UUID uuid = module->GetUUID();
179 if (uuid.IsValid())
180 m_uuid = uuid;
181
182 std::time_t mod_time = 0;
183 mod_time = llvm::sys::toTimeT(module->GetModificationTime());
184 if (mod_time != 0)
185 m_mod_time = mod_time;
186
187 mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());
188 if (mod_time != 0)
189 m_obj_mod_time = mod_time;
190}
191
193 Clear();
194 UUID uuid = objfile->GetUUID();
195 if (uuid.IsValid())
196 m_uuid = uuid;
197
198 std::time_t mod_time = 0;
199 // Grab the modification time of the object file's file. It isn't always the
200 // same as the module's file when you have a executable file as the main
201 // executable, and you have a object file for a symbol file.
203 mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));
204 if (mod_time != 0)
205 m_mod_time = mod_time;
206
207 mod_time =
208 llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());
209 if (mod_time != 0)
210 m_obj_mod_time = mod_time;
211}
212
219
221 if (!IsValid())
222 return false; // Invalid signature, return false!
223
224 if (m_uuid) {
225 llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();
226 encoder.AppendU8(eSignatureUUID);
227 encoder.AppendU8(uuid_bytes.size());
228 encoder.AppendData(uuid_bytes);
229 }
230 if (m_mod_time) {
232 encoder.AppendU32(*m_mod_time);
233 }
234 if (m_obj_mod_time) {
236 encoder.AppendU32(*m_obj_mod_time);
237 }
238 encoder.AppendU8(eSignatureEnd);
239 return true;
240}
241
243 lldb::offset_t *offset_ptr) {
244 Clear();
245 while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {
246 switch (sig_encoding) {
247 case eSignatureUUID: {
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));
252 } break;
253 case eSignatureModTime: {
254 uint32_t mod_time = data.GetU32(offset_ptr);
255 if (mod_time > 0)
256 m_mod_time = mod_time;
257 } break;
259 uint32_t mod_time = data.GetU32(offset_ptr);
260 if (mod_time > 0)
261 m_obj_mod_time = mod_time;
262 } break;
263 case eSignatureEnd:
264 // The definition of is valid changed to only be valid if the UUID is
265 // valid so make sure that if we attempt to decode an old cache file
266 // that we will fail to decode the cache file if the signature isn't
267 // considered valid.
268 return IsValid();
269 default:
270 break;
271 }
272 }
273 return false;
274}
275
277 auto [pos, inserted] = m_string_to_offset.try_emplace(s, m_next_offset);
278 if (inserted) {
279 m_strings.push_back(s);
280 m_next_offset += s.GetLength() + 1;
281 }
282 return pos->second;
283}
284
285static const llvm::StringRef kStringTableIdentifier("STAB");
286
288 // Write an 4 character code into the stream. This will help us when decoding
289 // to make sure we find this identifier when decoding the string table to make
290 // sure we have the rigth data. It also helps to identify the string table
291 // when dumping the hex bytes in a cache file.
293 size_t length_offset = encoder.GetByteSize();
294 encoder.AppendU32(0); // Total length of all strings which will be fixed up.
295 size_t strtab_offset = encoder.GetByteSize();
296 encoder.AppendU8(0); // Start the string table with an empty string.
297 for (auto s: m_strings) {
298 // Make sure all of the offsets match up with what we handed out!
299 assert(m_string_to_offset.find(s)->second ==
300 encoder.GetByteSize() - strtab_offset);
301 // Append the C string into the encoder
302 encoder.AppendCString(s.GetStringRef());
303 }
304 // Fixup the string table length.
305 encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);
306 return true;
307}
308
310 lldb::offset_t *offset_ptr) {
311 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
312 if (identifier != kStringTableIdentifier)
313 return false;
314 const uint32_t length = data.GetU32(offset_ptr);
315 // We always have at least one byte for the empty string at offset zero.
316 if (length == 0)
317 return false;
318 const char *bytes = (const char *)data.GetData(offset_ptr, length);
319 if (bytes == nullptr)
320 return false;
321 m_data = llvm::StringRef(bytes, length);
322 return true;
323}
324
325llvm::StringRef StringTableReader::Get(uint32_t offset) const {
326 if (offset >= m_data.size())
327 return llvm::StringRef();
328 return llvm::StringRef(m_data.data() + offset);
329}
330
static const llvm::StringRef kStringTableIdentifier("STAB")
SignatureEncoding
@ eSignatureModTime
@ eSignatureUUID
@ eSignatureObjectModTime
@ eSignatureEnd
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
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.
Definition ConstString.h:40
size_t GetLength() const
Get the length in bytes of string value.
An binary data encoding class.
Definition DataEncoder.h:42
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.
An data extractor class.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
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.
A file utility class.
Definition FileSpec.h:57
void AppendPathComponent(llvm::StringRef component)
Definition FileSpec.cpp:454
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.
static ModuleListProperties & GetGlobalModuleListProperties()
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:90
const llvm::sys::TimePoint & GetObjectModificationTime() const
Definition Module.h:486
const llvm::sys::TimePoint & GetModificationTime() const
Definition Module.h:482
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition ObjectFile.h:280
virtual UUID GetUUID()=0
Gets the UUID for this object file.
An error handling class.
Definition Status.h:118
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.
Definition UUID.h:27
bool IsValid() const
Definition UUID.h:69
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.
Definition Log.h:327
uint64_t offset_t
Definition lldb-types.h:85
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.