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/Log.h"
16 #include "lldb/Utility/Logging.h"
17 #include "llvm/Support/CachePruning.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 
20 using namespace llvm;
21 using namespace lldb_private;
22 
23 DataFileCache::DataFileCache(StringRef path) {
24   m_cache_dir.SetPath(path);
25 
26   // Prune the cache based off of the LLDB settings each time we create a cache
27   // object.
28   ModuleListProperties &properties =
29       ModuleList::GetGlobalModuleListProperties();
30   CachePruningPolicy policy;
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 often.
34   // This setting doesn't mean that a thread will continually scan the cache
35   // 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   pruneCache(path, policy);
44 
45   // This lambda will get called when the data is gotten from the cache and
46   // also after the data was set for a given key. We only need to take
47   // ownership of the data if we are geting the data, so we use the
48   // m_take_ownership member variable to indicate if we need to take
49   // ownership.
50 
51   auto add_buffer = [this](unsigned task, std::unique_ptr<llvm::MemoryBuffer> m) {
52     if (m_take_ownership)
53       m_mem_buff_up = std::move(m);
54   };
55   Expected<FileCache> cache_or_err =
56       llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);
57   if (cache_or_err)
58     m_cache_callback = std::move(*cache_or_err);
59   else {
60     Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
61     LLDB_LOG_ERROR(log, cache_or_err.takeError(),
62                    "failed to create lldb index cache directory: {0}");
63   }
64 }
65 
66 std::unique_ptr<llvm::MemoryBuffer>
67 DataFileCache::GetCachedData(StringRef key) {
68   std::lock_guard<std::mutex> guard(m_mutex);
69 
70   const unsigned task = 1;
71   m_take_ownership = true;
72   // If we call the "m_cache_callback" function and the data is cached, it will
73   // call the "add_buffer" lambda function from the constructor which will in
74   // turn take ownership of the member buffer that is passed to the callback and
75   // put it into a member variable.
76   Expected<AddStreamFn> add_stream_or_err = 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     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(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_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(StringRef key, llvm::ArrayRef<uint8_t> data) {
101   std::lock_guard<std::mutex> guard(m_mutex);
102   const unsigned task = 2;
103   // If we call this function and the data is cached, it will call the
104   // add_buffer lambda function from the constructor which will ignore the
105   // data.
106   Expected<AddStreamFn> add_stream_or_err = m_cache_callback(task, key);
107   // If we reach this code then we either already called the callback with
108   // the data or we haven't. We can tell if we had the cached data by checking
109   // the CacheAddStream function pointer value below.
110   if (add_stream_or_err) {
111     AddStreamFn &add_stream = *add_stream_or_err;
112     // If the "add_stream" is nullptr, then the data was cached. If it is
113     // valid, then if we call the add_stream function with a task it will
114     // cause the file to get generated, but we only want to check if the data
115     // is cached here, so we don't want to call it here. Note that the
116     // add_buffer will also get called in this case after the data has been
117     // provided, but we won't take ownership of the memory buffer as we just
118     // want to write the data.
119     if (add_stream) {
120       Expected<std::unique_ptr<CachedFileStream>> file_or_err =
121           add_stream(task);
122       if (file_or_err) {
123         CachedFileStream *cfs = file_or_err->get();
124         cfs->OS->write((const char *)data.data(), data.size());
125         return true;
126       } else {
127         Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
128         LLDB_LOG_ERROR(log, file_or_err.takeError(),
129                        "failed to get the cache file stream for key: {0}");
130       }
131     }
132   } else {
133     Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
134     LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
135                    "failed to get the cache add stream callback for key: {0}");
136   }
137   return false;
138 }
139 
140 FileSpec DataFileCache::GetCacheFilePath(llvm::StringRef key) {
141   FileSpec cache_file(m_cache_dir);
142   std::string filename("llvmcache-");
143   filename += key.str();
144   cache_file.AppendPathComponent(filename);
145   return cache_file;
146 }
147 
148 Status DataFileCache::RemoveCacheFile(llvm::StringRef key) {
149   FileSpec cache_file = GetCacheFilePath(key);
150   FileSystem &fs = FileSystem::Instance();
151   if (!fs.Exists(cache_file))
152     return Status();
153   return fs.RemoveFile(cache_file);
154 }
155 
156 CacheSignature::CacheSignature(lldb_private::Module *module) {
157   Clear();
158   UUID uuid = module->GetUUID();
159   if (uuid.IsValid())
160     m_uuid = uuid;
161 
162   std::time_t mod_time = 0;
163   mod_time = llvm::sys::toTimeT(module->GetModificationTime());
164   if (mod_time != 0)
165     m_mod_time = mod_time;
166 
167   mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());
168   if (mod_time != 0)
169     m_obj_mod_time = mod_time;
170 }
171 
172 CacheSignature::CacheSignature(lldb_private::ObjectFile *objfile) {
173   Clear();
174   UUID uuid = objfile->GetUUID();
175   if (uuid.IsValid())
176     m_uuid = uuid;
177 
178   std::time_t mod_time = 0;
179   // Grab the modification time of the object file's file. It isn't always the
180   // same as the module's file when you have a executable file as the main
181   // executable, and you have a object file for a symbol file.
182   FileSystem &fs = FileSystem::Instance();
183   mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));
184   if (mod_time != 0)
185     m_mod_time = mod_time;
186 
187   mod_time =
188       llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());
189   if (mod_time != 0)
190     m_obj_mod_time = mod_time;
191 }
192 
193 enum SignatureEncoding {
194   eSignatureUUID = 1u,
195   eSignatureModTime = 2u,
196   eSignatureObjectModTime = 3u,
197   eSignatureEnd = 255u,
198 };
199 
200 bool CacheSignature::Encode(DataEncoder &encoder) {
201   if (!IsValid())
202     return false; // Invalid signature, return false!
203 
204   if (m_uuid.hasValue()) {
205     llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();
206     encoder.AppendU8(eSignatureUUID);
207     encoder.AppendU8(uuid_bytes.size());
208     encoder.AppendData(uuid_bytes);
209   }
210   if (m_mod_time.hasValue()) {
211     encoder.AppendU8(eSignatureModTime);
212     encoder.AppendU32(*m_mod_time);
213   }
214   if (m_obj_mod_time.hasValue()) {
215     encoder.AppendU8(eSignatureObjectModTime);
216     encoder.AppendU32(*m_obj_mod_time);
217   }
218   encoder.AppendU8(eSignatureEnd);
219   return true;
220 }
221 
222 bool CacheSignature::Decode(const DataExtractor &data,
223                             lldb::offset_t *offset_ptr) {
224   Clear();
225   while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {
226     switch (sig_encoding) {
227     case eSignatureUUID: {
228       const uint8_t length = data.GetU8(offset_ptr);
229       const uint8_t *bytes = (const uint8_t *)data.GetData(offset_ptr, length);
230       if (bytes != nullptr && length > 0)
231         m_uuid = UUID::fromData(llvm::ArrayRef<uint8_t>(bytes, length));
232     } break;
233     case eSignatureModTime: {
234       uint32_t mod_time = data.GetU32(offset_ptr);
235       if (mod_time > 0)
236         m_mod_time = mod_time;
237     } break;
238     case eSignatureObjectModTime: {
239       uint32_t mod_time = data.GetU32(offset_ptr);
240       if (mod_time > 0)
241         m_mod_time = mod_time;
242     } break;
243     case eSignatureEnd:
244       return true;
245     default:
246       break;
247     }
248   }
249   return false;
250 }
251 
252 uint32_t ConstStringTable::Add(ConstString s) {
253   auto pos = m_string_to_offset.find(s);
254   if (pos != m_string_to_offset.end())
255     return pos->second;
256   const uint32_t offset = m_next_offset;
257   m_strings.push_back(s);
258   m_string_to_offset[s] = offset;
259   m_next_offset += s.GetLength() + 1;
260   return offset;
261 }
262 
263 static const llvm::StringRef kStringTableIdentifier("STAB");
264 
265 bool ConstStringTable::Encode(DataEncoder &encoder) {
266   // Write an 4 character code into the stream. This will help us when decoding
267   // to make sure we find this identifier when decoding the string table to make
268   // sure we have the rigth data. It also helps to identify the string table
269   // when dumping the hex bytes in a cache file.
270   encoder.AppendData(kStringTableIdentifier);
271   size_t length_offset = encoder.GetByteSize();
272   encoder.AppendU32(0); // Total length of all strings which will be fixed up.
273   size_t strtab_offset = encoder.GetByteSize();
274   encoder.AppendU8(0); // Start the string table with with an empty string.
275   for (auto s: m_strings) {
276     // Make sure all of the offsets match up with what we handed out!
277     assert(m_string_to_offset.find(s)->second ==
278            encoder.GetByteSize() - strtab_offset);
279     // Append the C string into the encoder
280     encoder.AppendCString(s.GetStringRef());
281   }
282   // Fixup the string table length.
283   encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);
284   return true;
285 }
286 
287 bool StringTableReader::Decode(const DataExtractor &data,
288                                lldb::offset_t *offset_ptr) {
289   llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
290   if (identifier != kStringTableIdentifier)
291     return false;
292   const uint32_t length = data.GetU32(offset_ptr);
293   // We always have at least one byte for the empty string at offset zero.
294   if (length == 0)
295     return false;
296   const char *bytes = (const char *)data.GetData(offset_ptr, length);
297   if (bytes == nullptr)
298     return false;
299   m_data = StringRef(bytes, length);
300   return true;
301 }
302 
303 StringRef StringTableReader::Get(uint32_t offset) const {
304   if (offset >= m_data.size())
305     return StringRef();
306   return StringRef(m_data.data() + offset);
307 }
308