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