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