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"
11 #include "lldb/Core/ModuleList.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Log.h"
17 #include "llvm/Support/CachePruning.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 
20 using namespace lldb_private;
21 
22 DataFileCache::DataFileCache(llvm::StringRef path) {
23  m_cache_dir.SetPath(path);
24 
25  // Prune the cache based off of the LLDB settings each time we create a cache
26  // object.
27  ModuleListProperties &properties =
29  llvm::CachePruningPolicy policy;
30  // Only scan once an hour. If we have lots of debug sessions we don't want
31  // to scan this directory too often. A timestamp file is written to the
32  // directory to ensure different processes don't scan the directory too often.
33  // This setting doesn't mean that a thread will continually scan the cache
34  // directory within this process.
35  policy.Interval = std::chrono::hours(1);
36  // Get the user settings for pruning.
37  policy.MaxSizeBytes = properties.GetLLDBIndexCacheMaxByteSize();
38  policy.MaxSizePercentageOfAvailableSpace =
39  properties.GetLLDBIndexCacheMaxPercent();
40  policy.Expiration =
41  std::chrono::hours(properties.GetLLDBIndexCacheExpirationDays() * 24);
42  pruneCache(path, policy);
43 
44  // This lambda will get called when the data is gotten from the cache and
45  // also after the data was set for a given key. We only need to take
46  // ownership of the data if we are geting the data, so we use the
47  // m_take_ownership member variable to indicate if we need to take
48  // ownership.
49 
50  auto add_buffer = [this](unsigned task, std::unique_ptr<llvm::MemoryBuffer> m) {
51  if (m_take_ownership)
52  m_mem_buff_up = std::move(m);
53  };
54  llvm::Expected<llvm::FileCache> cache_or_err =
55  llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);
56  if (cache_or_err)
57  m_cache_callback = std::move(*cache_or_err);
58  else {
59  Log *log = GetLog(LLDBLog::Modules);
60  LLDB_LOG_ERROR(log, cache_or_err.takeError(),
61  "failed to create lldb index cache directory: {0}");
62  }
63 }
64 
65 std::unique_ptr<llvm::MemoryBuffer>
66 DataFileCache::GetCachedData(llvm::StringRef key) {
67  std::lock_guard<std::mutex> guard(m_mutex);
68 
69  const unsigned task = 1;
70  m_take_ownership = true;
71  // If we call the "m_cache_callback" function and the data is cached, it will
72  // call the "add_buffer" lambda function from the constructor which will in
73  // turn take ownership of the member buffer that is passed to the callback and
74  // put it into a member variable.
75  llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
76  m_cache_callback(task, key);
77  m_take_ownership = false;
78  // At this point we either already called the "add_buffer" lambda with
79  // the data or we haven't. We can tell if we got the cached data by checking
80  // the add_stream function pointer value below.
81  if (add_stream_or_err) {
82  llvm::AddStreamFn &add_stream = *add_stream_or_err;
83  // If the "add_stream" is nullptr, then the data was cached and we already
84  // called the "add_buffer" lambda. If it is valid, then if we were to call
85  // the add_stream function it would cause a cache file to get generated
86  // and we would be expected to fill in the data. In this function we only
87  // want to check if the data was cached, so we don't want to call
88  // "add_stream" in this function.
89  if (!add_stream)
90  return std::move(m_mem_buff_up);
91  } else {
92  Log *log = GetLog(LLDBLog::Modules);
93  LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
94  "failed to get the cache add stream callback for key: {0}");
95  }
96  // Data was not cached.
97  return std::unique_ptr<llvm::MemoryBuffer>();
98 }
99 
100 bool DataFileCache::SetCachedData(llvm::StringRef key,
101  llvm::ArrayRef<uint8_t> data) {
102  std::lock_guard<std::mutex> guard(m_mutex);
103  const unsigned task = 2;
104  // If we call this function and the data is cached, it will call the
105  // add_buffer lambda function from the constructor which will ignore the
106  // data.
107  llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
108  m_cache_callback(task, key);
109  // If we reach this code then we either already called the callback with
110  // the data or we haven't. We can tell if we had the cached data by checking
111  // the CacheAddStream function pointer value below.
112  if (add_stream_or_err) {
113  llvm::AddStreamFn &add_stream = *add_stream_or_err;
114  // If the "add_stream" is nullptr, then the data was cached. If it is
115  // valid, then if we call the add_stream function with a task it will
116  // cause the file to get generated, but we only want to check if the data
117  // is cached here, so we don't want to call it here. Note that the
118  // add_buffer will also get called in this case after the data has been
119  // provided, but we won't take ownership of the memory buffer as we just
120  // want to write the data.
121  if (add_stream) {
122  llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =
123  add_stream(task);
124  if (file_or_err) {
125  llvm::CachedFileStream *cfs = file_or_err->get();
126  cfs->OS->write((const char *)data.data(), data.size());
127  return true;
128  } else {
129  Log *log = GetLog(LLDBLog::Modules);
130  LLDB_LOG_ERROR(log, file_or_err.takeError(),
131  "failed to get the cache file stream for key: {0}");
132  }
133  }
134  } else {
135  Log *log = GetLog(LLDBLog::Modules);
136  LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
137  "failed to get the cache add stream callback for key: {0}");
138  }
139  return false;
140 }
141 
143  FileSpec cache_file(m_cache_dir);
144  std::string filename("llvmcache-");
145  filename += key.str();
146  cache_file.AppendPathComponent(filename);
147  return cache_file;
148 }
149 
151  FileSpec cache_file = GetCacheFilePath(key);
153  if (!fs.Exists(cache_file))
154  return Status();
155  return fs.RemoveFile(cache_file);
156 }
157 
159  Clear();
160  UUID uuid = module->GetUUID();
161  if (uuid.IsValid())
162  m_uuid = uuid;
163 
164  std::time_t mod_time = 0;
165  mod_time = llvm::sys::toTimeT(module->GetModificationTime());
166  if (mod_time != 0)
167  m_mod_time = mod_time;
168 
169  mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());
170  if (mod_time != 0)
171  m_obj_mod_time = mod_time;
172 }
173 
175  Clear();
176  UUID uuid = objfile->GetUUID();
177  if (uuid.IsValid())
178  m_uuid = uuid;
179 
180  std::time_t mod_time = 0;
181  // Grab the modification time of the object file's file. It isn't always the
182  // same as the module's file when you have a executable file as the main
183  // executable, and you have a object file for a symbol file.
185  mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));
186  if (mod_time != 0)
187  m_mod_time = mod_time;
188 
189  mod_time =
190  llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());
191  if (mod_time != 0)
192  m_obj_mod_time = mod_time;
193 }
194 
200 };
201 
202 bool CacheSignature::Encode(DataEncoder &encoder) const {
203  if (!IsValid())
204  return false; // Invalid signature, return false!
205 
206  if (m_uuid.hasValue()) {
207  llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();
208  encoder.AppendU8(eSignatureUUID);
209  encoder.AppendU8(uuid_bytes.size());
210  encoder.AppendData(uuid_bytes);
211  }
212  if (m_mod_time.hasValue()) {
213  encoder.AppendU8(eSignatureModTime);
214  encoder.AppendU32(*m_mod_time);
215  }
216  if (m_obj_mod_time.hasValue()) {
217  encoder.AppendU8(eSignatureObjectModTime);
218  encoder.AppendU32(*m_obj_mod_time);
219  }
220  encoder.AppendU8(eSignatureEnd);
221  return true;
222 }
223 
225  lldb::offset_t *offset_ptr) {
226  Clear();
227  while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {
228  switch (sig_encoding) {
229  case eSignatureUUID: {
230  const uint8_t length = data.GetU8(offset_ptr);
231  const uint8_t *bytes = (const uint8_t *)data.GetData(offset_ptr, length);
232  if (bytes != nullptr && length > 0)
233  m_uuid = UUID::fromData(llvm::ArrayRef<uint8_t>(bytes, length));
234  } break;
235  case eSignatureModTime: {
236  uint32_t mod_time = data.GetU32(offset_ptr);
237  if (mod_time > 0)
238  m_mod_time = mod_time;
239  } break;
241  uint32_t mod_time = data.GetU32(offset_ptr);
242  if (mod_time > 0)
243  m_obj_mod_time = mod_time;
244  } break;
245  case eSignatureEnd:
246  // The definition of is valid changed to only be valid if the UUID is
247  // valid so make sure that if we attempt to decode an old cache file
248  // that we will fail to decode the cache file if the signature isn't
249  // considered valid.
250  return IsValid();
251  default:
252  break;
253  }
254  }
255  return false;
256 }
257 
259  auto pos = m_string_to_offset.find(s);
260  if (pos != m_string_to_offset.end())
261  return pos->second;
262  const uint32_t offset = m_next_offset;
263  m_strings.push_back(s);
264  m_string_to_offset[s] = offset;
265  m_next_offset += s.GetLength() + 1;
266  return offset;
267 }
268 
269 static const llvm::StringRef kStringTableIdentifier("STAB");
270 
271 bool ConstStringTable::Encode(DataEncoder &encoder) {
272  // Write an 4 character code into the stream. This will help us when decoding
273  // to make sure we find this identifier when decoding the string table to make
274  // sure we have the rigth data. It also helps to identify the string table
275  // when dumping the hex bytes in a cache file.
276  encoder.AppendData(kStringTableIdentifier);
277  size_t length_offset = encoder.GetByteSize();
278  encoder.AppendU32(0); // Total length of all strings which will be fixed up.
279  size_t strtab_offset = encoder.GetByteSize();
280  encoder.AppendU8(0); // Start the string table with with an empty string.
281  for (auto s: m_strings) {
282  // Make sure all of the offsets match up with what we handed out!
283  assert(m_string_to_offset.find(s)->second ==
284  encoder.GetByteSize() - strtab_offset);
285  // Append the C string into the encoder
286  encoder.AppendCString(s.GetStringRef());
287  }
288  // Fixup the string table length.
289  encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);
290  return true;
291 }
292 
294  lldb::offset_t *offset_ptr) {
295  llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
296  if (identifier != kStringTableIdentifier)
297  return false;
298  const uint32_t length = data.GetU32(offset_ptr);
299  // We always have at least one byte for the empty string at offset zero.
300  if (length == 0)
301  return false;
302  const char *bytes = (const char *)data.GetData(offset_ptr, length);
303  if (bytes == nullptr)
304  return false;
305  m_data = llvm::StringRef(bytes, length);
306  return true;
307 }
308 
309 llvm::StringRef StringTableReader::Get(uint32_t offset) const {
310  if (offset >= m_data.size())
311  return llvm::StringRef();
312  return llvm::StringRef(m_data.data() + offset);
313 }
lldb_private::UUID::fromData
static UUID fromData(const void *bytes, uint32_t num_bytes)
Creates a UUID from the data pointed to by the bytes argument.
Definition: UUID.h:45
lldb_private::UUID
Definition: UUID.h:23
lldb_private::ConstStringTable::Encode
bool Encode(DataEncoder &encoder)
Definition: DataFileCache.cpp:271
lldb_private::ModuleList::GetGlobalModuleListProperties
static ModuleListProperties & GetGlobalModuleListProperties()
Definition: ModuleList.cpp:755
lldb_private::ObjectFile::GetFileSpec
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:292
FileSystem.h
lldb_private::DataFileCache::DataFileCache
DataFileCache(llvm::StringRef path)
Create a data file cache in the directory path that is specified.
Definition: DataFileCache.cpp:22
lldb_private::DataFileCache::GetCachedData
std::unique_ptr< llvm::MemoryBuffer > GetCachedData(llvm::StringRef key)
Get cached data from the cache directory for the specified key.
Definition: DataFileCache.cpp:66
lldb_private::UUID::IsValid
bool IsValid() const
Definition: UUID.h:79
Module.h
lldb_private::CacheSignature::IsValid
bool IsValid() const
Return true only if the CacheSignature is valid.
Definition: DataFileCache.h:128
lldb_private::CacheSignature::m_obj_mod_time
llvm::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_...
Definition: DataFileCache.h:106
lldb_private::Module
Definition: Module.h:85
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::FileSystem::Exists
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
Definition: common/FileSystem.cpp:150
lldb_private::FileSystem
Definition: FileSystem.h:28
lldb_private::FileSpec::SetPath
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
Definition: FileSpec.h:270
lldb_private::LLDBLog::Modules
@ Modules
eSignatureModTime
@ eSignatureModTime
Definition: DataFileCache.cpp:197
DataFileCache.h
lldb_private::CacheSignature::Decode
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
Decode a serialized version of this object from data.
Definition: DataFileCache.cpp:224
lldb_private::CacheSignature::Encode
bool Encode(DataEncoder &encoder) const
Encode this object into a data encoder object.
Definition: DataFileCache.cpp:202
lldb_private::ConstStringTable::Add
uint32_t Add(ConstString s)
Add a string into the string table.
Definition: DataFileCache.cpp:258
lldb_private::DataFileCache::SetCachedData
bool SetCachedData(llvm::StringRef key, llvm::ArrayRef< uint8_t > data)
Set cached data for the specified key.
Definition: DataFileCache.cpp:100
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::DataFileCache::m_mutex
std::mutex m_mutex
Definition: DataFileCache.h:84
lldb_private::ModuleListProperties
Definition: ModuleList.h:50
lldb_private::DataFileCache::RemoveCacheFile
Status RemoveCacheFile(llvm::StringRef key)
Remove the cache file associated with the key.
Definition: DataFileCache.cpp:150
eSignatureEnd
@ eSignatureEnd
Definition: DataFileCache.cpp:199
lldb_private::CacheSignature::CacheSignature
CacheSignature()=default
eSignatureObjectModTime
@ eSignatureObjectModTime
Definition: DataFileCache.cpp:198
ModuleList.h
lldb_private::DataExtractor
Definition: DataExtractor.h:48
Log.h
lldb_private::StringTableReader::Decode
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
Definition: DataFileCache.cpp:293
lldb_private::ObjectFile::GetUUID
virtual UUID GetUUID()=0
Gets the UUID for this object file.
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::DataExtractor::GetU8
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
Definition: DataExtractor.cpp:316
lldb_private::DataFileCache::m_mem_buff_up
std::unique_ptr< llvm::MemoryBuffer > m_mem_buff_up
Definition: DataFileCache.h:85
DataEncoder.h
lldb_private::ModuleListProperties::GetLLDBIndexCacheMaxPercent
uint64_t GetLLDBIndexCacheMaxPercent()
Definition: ModuleList.cpp:150
lldb_private::FileSystem::GetModificationTime
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
Definition: common/FileSystem.cpp:96
lldb_private::DataFileCache::m_take_ownership
bool m_take_ownership
Definition: DataFileCache.h:86
lldb_private::ConstStringTable::m_strings
std::vector< ConstString > m_strings
Definition: DataFileCache.h:193
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ConstString::GetLength
size_t GetLength() const
Get the length in bytes of string value.
Definition: ConstString.cpp:237
lldb_private::DataFileCache::m_cache_callback
llvm::FileCache m_cache_callback
Definition: DataFileCache.h:82
ObjectFile.h
lldb_private::StringTableReader::m_data
llvm::StringRef m_data
All of the strings in the string table are contained in m_data.
Definition: DataFileCache.h:212
lldb_private::FileSystem::RemoveFile
Status RemoveFile(const FileSpec &file_spec)
Remove a single file.
Definition: common/FileSystem.cpp:476
lldb_private::CacheSignature::Clear
void Clear()
Definition: DataFileCache.h:116
lldb_private::CacheSignature::m_mod_time
llvm::Optional< std::time_t > m_mod_time
Modification time of file on disk.
Definition: DataFileCache.h:102
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::ModuleChild::GetModule
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
lldb_private::DataFileCache::GetCacheFilePath
FileSpec GetCacheFilePath(llvm::StringRef key)
Return the cache file that is associated with the key.
Definition: DataFileCache.cpp:142
lldb_private::DataExtractor::GetU32
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:425
lldb_private::ConstStringTable::m_string_to_offset
llvm::DenseMap< ConstString, uint32_t > m_string_to_offset
Definition: DataFileCache.h:194
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:46
lldb_private::DataFileCache::m_cache_dir
FileSpec m_cache_dir
Definition: DataFileCache.h:83
lldb_private::FileSpec::AppendPathComponent
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:434
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ModuleListProperties::GetLLDBIndexCacheMaxByteSize
uint64_t GetLLDBIndexCacheMaxByteSize()
Definition: ModuleList.cpp:144
lldb_private::CacheSignature::m_uuid
llvm::Optional< UUID > m_uuid
UUID of object file or module.
Definition: DataFileCache.h:100
lldb_private::Module::GetObjectModificationTime
const llvm::sys::TimePoint & GetObjectModificationTime() const
Definition: Module.h:517
lldb_private::DataExtractor::GetData
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
Definition: DataExtractor.h:337
kStringTableIdentifier
static const llvm::StringRef kStringTableIdentifier("STAB")
lldb_private::Log
Definition: Log.h:48
eSignatureUUID
@ eSignatureUUID
Definition: DataFileCache.cpp:196
lldb_private::Module::GetUUID
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:345
lldb_private::ModuleListProperties::GetLLDBIndexCacheExpirationDays
uint64_t GetLLDBIndexCacheExpirationDays()
Definition: ModuleList.cpp:156
lldb_private::ConstStringTable::m_next_offset
uint32_t m_next_offset
Skip one byte to start the string table off with an empty string.
Definition: DataFileCache.h:196
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:235
SignatureEncoding
SignatureEncoding
Definition: DataFileCache.cpp:195
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:286
LLDBLog.h
lldb_private::Module::GetModificationTime
const llvm::sys::TimePoint & GetModificationTime() const
Definition: Module.h:513
lldb_private::ObjectFile
Definition: ObjectFile.h:60
lldb_private::StringTableReader::Get
llvm::StringRef Get(uint32_t offset) const
Definition: DataFileCache.cpp:309