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/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 =
28       ModuleList::GetGlobalModuleListProperties();
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 
142 FileSpec DataFileCache::GetCacheFilePath(llvm::StringRef key) {
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 
150 Status DataFileCache::RemoveCacheFile(llvm::StringRef key) {
151   FileSpec cache_file = GetCacheFilePath(key);
152   FileSystem &fs = FileSystem::Instance();
153   if (!fs.Exists(cache_file))
154     return Status();
155   return fs.RemoveFile(cache_file);
156 }
157 
158 CacheSignature::CacheSignature(lldb_private::Module *module) {
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 
174 CacheSignature::CacheSignature(lldb_private::ObjectFile *objfile) {
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.
184   FileSystem &fs = FileSystem::Instance();
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 
195 enum SignatureEncoding {
196   eSignatureUUID = 1u,
197   eSignatureModTime = 2u,
198   eSignatureObjectModTime = 3u,
199   eSignatureEnd = 255u,
200 };
201 
202 bool CacheSignature::Encode(DataEncoder &encoder) const {
203   if (!IsValid())
204     return false; // Invalid signature, return false!
205 
206   if (m_uuid) {
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) {
213     encoder.AppendU8(eSignatureModTime);
214     encoder.AppendU32(*m_mod_time);
215   }
216   if (m_obj_mod_time) {
217     encoder.AppendU8(eSignatureObjectModTime);
218     encoder.AppendU32(*m_obj_mod_time);
219   }
220   encoder.AppendU8(eSignatureEnd);
221   return true;
222 }
223 
224 bool CacheSignature::Decode(const lldb_private::DataExtractor &data,
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;
240     case eSignatureObjectModTime: {
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 
258 uint32_t ConstStringTable::Add(ConstString s) {
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 
293 bool StringTableReader::Decode(const lldb_private::DataExtractor &data,
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 }
314