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