177fc4c14SDimitry Andric //===-- DataFileCache.cpp -------------------------------------------------===//
277fc4c14SDimitry Andric //
377fc4c14SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477fc4c14SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
577fc4c14SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677fc4c14SDimitry Andric //
777fc4c14SDimitry Andric //===----------------------------------------------------------------------===//
877fc4c14SDimitry Andric
977fc4c14SDimitry Andric #include "lldb/Core/DataFileCache.h"
1077fc4c14SDimitry Andric #include "lldb/Core/Module.h"
1177fc4c14SDimitry Andric #include "lldb/Core/ModuleList.h"
1277fc4c14SDimitry Andric #include "lldb/Host/FileSystem.h"
1377fc4c14SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
1477fc4c14SDimitry Andric #include "lldb/Utility/DataEncoder.h"
15145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1677fc4c14SDimitry Andric #include "lldb/Utility/Log.h"
1777fc4c14SDimitry Andric #include "llvm/Support/CachePruning.h"
1877fc4c14SDimitry Andric
1977fc4c14SDimitry Andric using namespace lldb_private;
2077fc4c14SDimitry Andric
2177fc4c14SDimitry Andric
GetLLDBIndexCachePolicy()22e3b55780SDimitry Andric llvm::CachePruningPolicy DataFileCache::GetLLDBIndexCachePolicy() {
23e3b55780SDimitry Andric static llvm::CachePruningPolicy policy;
24e3b55780SDimitry Andric static llvm::once_flag once_flag;
25e3b55780SDimitry Andric
26e3b55780SDimitry Andric llvm::call_once(once_flag, []() {
27e3b55780SDimitry Andric // Prune the cache based off of the LLDB settings each time we create a
28e3b55780SDimitry Andric // cache object.
2977fc4c14SDimitry Andric ModuleListProperties &properties =
3077fc4c14SDimitry Andric ModuleList::GetGlobalModuleListProperties();
3177fc4c14SDimitry Andric // Only scan once an hour. If we have lots of debug sessions we don't want
3277fc4c14SDimitry Andric // to scan this directory too often. A timestamp file is written to the
33e3b55780SDimitry Andric // directory to ensure different processes don't scan the directory too
34e3b55780SDimitry Andric // often. This setting doesn't mean that a thread will continually scan the
35e3b55780SDimitry Andric // cache directory within this process.
3677fc4c14SDimitry Andric policy.Interval = std::chrono::hours(1);
3777fc4c14SDimitry Andric // Get the user settings for pruning.
3877fc4c14SDimitry Andric policy.MaxSizeBytes = properties.GetLLDBIndexCacheMaxByteSize();
3977fc4c14SDimitry Andric policy.MaxSizePercentageOfAvailableSpace =
4077fc4c14SDimitry Andric properties.GetLLDBIndexCacheMaxPercent();
4177fc4c14SDimitry Andric policy.Expiration =
4277fc4c14SDimitry Andric std::chrono::hours(properties.GetLLDBIndexCacheExpirationDays() * 24);
43e3b55780SDimitry Andric });
44e3b55780SDimitry Andric return policy;
45e3b55780SDimitry Andric }
46e3b55780SDimitry Andric
DataFileCache(llvm::StringRef path,llvm::CachePruningPolicy policy)47e3b55780SDimitry Andric DataFileCache::DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy) {
48e3b55780SDimitry Andric m_cache_dir.SetPath(path);
4977fc4c14SDimitry Andric pruneCache(path, policy);
5077fc4c14SDimitry Andric
5177fc4c14SDimitry Andric // This lambda will get called when the data is gotten from the cache and
5277fc4c14SDimitry Andric // also after the data was set for a given key. We only need to take
5377fc4c14SDimitry Andric // ownership of the data if we are geting the data, so we use the
5477fc4c14SDimitry Andric // m_take_ownership member variable to indicate if we need to take
5577fc4c14SDimitry Andric // ownership.
5677fc4c14SDimitry Andric
57e3b55780SDimitry Andric auto add_buffer = [this](unsigned task, const llvm::Twine &moduleName,
58e3b55780SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> m) {
5977fc4c14SDimitry Andric if (m_take_ownership)
6077fc4c14SDimitry Andric m_mem_buff_up = std::move(m);
6177fc4c14SDimitry Andric };
62145449b1SDimitry Andric llvm::Expected<llvm::FileCache> cache_or_err =
6377fc4c14SDimitry Andric llvm::localCache("LLDBModuleCache", "lldb-module", path, add_buffer);
6477fc4c14SDimitry Andric if (cache_or_err)
6577fc4c14SDimitry Andric m_cache_callback = std::move(*cache_or_err);
6677fc4c14SDimitry Andric else {
67145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Modules);
6877fc4c14SDimitry Andric LLDB_LOG_ERROR(log, cache_or_err.takeError(),
6977fc4c14SDimitry Andric "failed to create lldb index cache directory: {0}");
7077fc4c14SDimitry Andric }
7177fc4c14SDimitry Andric }
7277fc4c14SDimitry Andric
7377fc4c14SDimitry Andric std::unique_ptr<llvm::MemoryBuffer>
GetCachedData(llvm::StringRef key)74145449b1SDimitry Andric DataFileCache::GetCachedData(llvm::StringRef key) {
7577fc4c14SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
7677fc4c14SDimitry Andric
7777fc4c14SDimitry Andric const unsigned task = 1;
7877fc4c14SDimitry Andric m_take_ownership = true;
7977fc4c14SDimitry Andric // If we call the "m_cache_callback" function and the data is cached, it will
8077fc4c14SDimitry Andric // call the "add_buffer" lambda function from the constructor which will in
8177fc4c14SDimitry Andric // turn take ownership of the member buffer that is passed to the callback and
8277fc4c14SDimitry Andric // put it into a member variable.
83145449b1SDimitry Andric llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
84e3b55780SDimitry Andric m_cache_callback(task, key, "");
8577fc4c14SDimitry Andric m_take_ownership = false;
8677fc4c14SDimitry Andric // At this point we either already called the "add_buffer" lambda with
8777fc4c14SDimitry Andric // the data or we haven't. We can tell if we got the cached data by checking
8877fc4c14SDimitry Andric // the add_stream function pointer value below.
8977fc4c14SDimitry Andric if (add_stream_or_err) {
90145449b1SDimitry Andric llvm::AddStreamFn &add_stream = *add_stream_or_err;
9177fc4c14SDimitry Andric // If the "add_stream" is nullptr, then the data was cached and we already
9277fc4c14SDimitry Andric // called the "add_buffer" lambda. If it is valid, then if we were to call
9377fc4c14SDimitry Andric // the add_stream function it would cause a cache file to get generated
9477fc4c14SDimitry Andric // and we would be expected to fill in the data. In this function we only
9577fc4c14SDimitry Andric // want to check if the data was cached, so we don't want to call
9677fc4c14SDimitry Andric // "add_stream" in this function.
9777fc4c14SDimitry Andric if (!add_stream)
9877fc4c14SDimitry Andric return std::move(m_mem_buff_up);
9977fc4c14SDimitry Andric } else {
100145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Modules);
10177fc4c14SDimitry Andric LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
10277fc4c14SDimitry Andric "failed to get the cache add stream callback for key: {0}");
10377fc4c14SDimitry Andric }
10477fc4c14SDimitry Andric // Data was not cached.
10577fc4c14SDimitry Andric return std::unique_ptr<llvm::MemoryBuffer>();
10677fc4c14SDimitry Andric }
10777fc4c14SDimitry Andric
SetCachedData(llvm::StringRef key,llvm::ArrayRef<uint8_t> data)108145449b1SDimitry Andric bool DataFileCache::SetCachedData(llvm::StringRef key,
109145449b1SDimitry Andric llvm::ArrayRef<uint8_t> data) {
11077fc4c14SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
11177fc4c14SDimitry Andric const unsigned task = 2;
11277fc4c14SDimitry Andric // If we call this function and the data is cached, it will call the
11377fc4c14SDimitry Andric // add_buffer lambda function from the constructor which will ignore the
11477fc4c14SDimitry Andric // data.
115145449b1SDimitry Andric llvm::Expected<llvm::AddStreamFn> add_stream_or_err =
116e3b55780SDimitry Andric m_cache_callback(task, key, "");
11777fc4c14SDimitry Andric // If we reach this code then we either already called the callback with
11877fc4c14SDimitry Andric // the data or we haven't. We can tell if we had the cached data by checking
11977fc4c14SDimitry Andric // the CacheAddStream function pointer value below.
12077fc4c14SDimitry Andric if (add_stream_or_err) {
121145449b1SDimitry Andric llvm::AddStreamFn &add_stream = *add_stream_or_err;
12277fc4c14SDimitry Andric // If the "add_stream" is nullptr, then the data was cached. If it is
12377fc4c14SDimitry Andric // valid, then if we call the add_stream function with a task it will
12477fc4c14SDimitry Andric // cause the file to get generated, but we only want to check if the data
12577fc4c14SDimitry Andric // is cached here, so we don't want to call it here. Note that the
12677fc4c14SDimitry Andric // add_buffer will also get called in this case after the data has been
12777fc4c14SDimitry Andric // provided, but we won't take ownership of the memory buffer as we just
12877fc4c14SDimitry Andric // want to write the data.
12977fc4c14SDimitry Andric if (add_stream) {
130145449b1SDimitry Andric llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> file_or_err =
131e3b55780SDimitry Andric add_stream(task, "");
13277fc4c14SDimitry Andric if (file_or_err) {
133145449b1SDimitry Andric llvm::CachedFileStream *cfs = file_or_err->get();
13477fc4c14SDimitry Andric cfs->OS->write((const char *)data.data(), data.size());
13577fc4c14SDimitry Andric return true;
13677fc4c14SDimitry Andric } else {
137145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Modules);
13877fc4c14SDimitry Andric LLDB_LOG_ERROR(log, file_or_err.takeError(),
13977fc4c14SDimitry Andric "failed to get the cache file stream for key: {0}");
14077fc4c14SDimitry Andric }
14177fc4c14SDimitry Andric }
14277fc4c14SDimitry Andric } else {
143145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Modules);
14477fc4c14SDimitry Andric LLDB_LOG_ERROR(log, add_stream_or_err.takeError(),
14577fc4c14SDimitry Andric "failed to get the cache add stream callback for key: {0}");
14677fc4c14SDimitry Andric }
14777fc4c14SDimitry Andric return false;
14877fc4c14SDimitry Andric }
14977fc4c14SDimitry Andric
GetCacheFilePath(llvm::StringRef key)15077fc4c14SDimitry Andric FileSpec DataFileCache::GetCacheFilePath(llvm::StringRef key) {
15177fc4c14SDimitry Andric FileSpec cache_file(m_cache_dir);
15277fc4c14SDimitry Andric std::string filename("llvmcache-");
15377fc4c14SDimitry Andric filename += key.str();
15477fc4c14SDimitry Andric cache_file.AppendPathComponent(filename);
15577fc4c14SDimitry Andric return cache_file;
15677fc4c14SDimitry Andric }
15777fc4c14SDimitry Andric
RemoveCacheFile(llvm::StringRef key)15877fc4c14SDimitry Andric Status DataFileCache::RemoveCacheFile(llvm::StringRef key) {
15977fc4c14SDimitry Andric FileSpec cache_file = GetCacheFilePath(key);
16077fc4c14SDimitry Andric FileSystem &fs = FileSystem::Instance();
16177fc4c14SDimitry Andric if (!fs.Exists(cache_file))
16277fc4c14SDimitry Andric return Status();
16377fc4c14SDimitry Andric return fs.RemoveFile(cache_file);
16477fc4c14SDimitry Andric }
16577fc4c14SDimitry Andric
CacheSignature(lldb_private::Module * module)16677fc4c14SDimitry Andric CacheSignature::CacheSignature(lldb_private::Module *module) {
16777fc4c14SDimitry Andric Clear();
16877fc4c14SDimitry Andric UUID uuid = module->GetUUID();
16977fc4c14SDimitry Andric if (uuid.IsValid())
17077fc4c14SDimitry Andric m_uuid = uuid;
17177fc4c14SDimitry Andric
17277fc4c14SDimitry Andric std::time_t mod_time = 0;
17377fc4c14SDimitry Andric mod_time = llvm::sys::toTimeT(module->GetModificationTime());
17477fc4c14SDimitry Andric if (mod_time != 0)
17577fc4c14SDimitry Andric m_mod_time = mod_time;
17677fc4c14SDimitry Andric
17777fc4c14SDimitry Andric mod_time = llvm::sys::toTimeT(module->GetObjectModificationTime());
17877fc4c14SDimitry Andric if (mod_time != 0)
17977fc4c14SDimitry Andric m_obj_mod_time = mod_time;
18077fc4c14SDimitry Andric }
18177fc4c14SDimitry Andric
CacheSignature(lldb_private::ObjectFile * objfile)18277fc4c14SDimitry Andric CacheSignature::CacheSignature(lldb_private::ObjectFile *objfile) {
18377fc4c14SDimitry Andric Clear();
18477fc4c14SDimitry Andric UUID uuid = objfile->GetUUID();
18577fc4c14SDimitry Andric if (uuid.IsValid())
18677fc4c14SDimitry Andric m_uuid = uuid;
18777fc4c14SDimitry Andric
18877fc4c14SDimitry Andric std::time_t mod_time = 0;
18977fc4c14SDimitry Andric // Grab the modification time of the object file's file. It isn't always the
19077fc4c14SDimitry Andric // same as the module's file when you have a executable file as the main
19177fc4c14SDimitry Andric // executable, and you have a object file for a symbol file.
19277fc4c14SDimitry Andric FileSystem &fs = FileSystem::Instance();
19377fc4c14SDimitry Andric mod_time = llvm::sys::toTimeT(fs.GetModificationTime(objfile->GetFileSpec()));
19477fc4c14SDimitry Andric if (mod_time != 0)
19577fc4c14SDimitry Andric m_mod_time = mod_time;
19677fc4c14SDimitry Andric
19777fc4c14SDimitry Andric mod_time =
19877fc4c14SDimitry Andric llvm::sys::toTimeT(objfile->GetModule()->GetObjectModificationTime());
19977fc4c14SDimitry Andric if (mod_time != 0)
20077fc4c14SDimitry Andric m_obj_mod_time = mod_time;
20177fc4c14SDimitry Andric }
20277fc4c14SDimitry Andric
20377fc4c14SDimitry Andric enum SignatureEncoding {
20477fc4c14SDimitry Andric eSignatureUUID = 1u,
20577fc4c14SDimitry Andric eSignatureModTime = 2u,
20677fc4c14SDimitry Andric eSignatureObjectModTime = 3u,
20777fc4c14SDimitry Andric eSignatureEnd = 255u,
20877fc4c14SDimitry Andric };
20977fc4c14SDimitry Andric
Encode(DataEncoder & encoder) const210145449b1SDimitry Andric bool CacheSignature::Encode(DataEncoder &encoder) const {
21177fc4c14SDimitry Andric if (!IsValid())
21277fc4c14SDimitry Andric return false; // Invalid signature, return false!
21377fc4c14SDimitry Andric
214145449b1SDimitry Andric if (m_uuid) {
21577fc4c14SDimitry Andric llvm::ArrayRef<uint8_t> uuid_bytes = m_uuid->GetBytes();
21677fc4c14SDimitry Andric encoder.AppendU8(eSignatureUUID);
21777fc4c14SDimitry Andric encoder.AppendU8(uuid_bytes.size());
21877fc4c14SDimitry Andric encoder.AppendData(uuid_bytes);
21977fc4c14SDimitry Andric }
220145449b1SDimitry Andric if (m_mod_time) {
22177fc4c14SDimitry Andric encoder.AppendU8(eSignatureModTime);
22277fc4c14SDimitry Andric encoder.AppendU32(*m_mod_time);
22377fc4c14SDimitry Andric }
224145449b1SDimitry Andric if (m_obj_mod_time) {
22577fc4c14SDimitry Andric encoder.AppendU8(eSignatureObjectModTime);
22677fc4c14SDimitry Andric encoder.AppendU32(*m_obj_mod_time);
22777fc4c14SDimitry Andric }
22877fc4c14SDimitry Andric encoder.AppendU8(eSignatureEnd);
22977fc4c14SDimitry Andric return true;
23077fc4c14SDimitry Andric }
23177fc4c14SDimitry Andric
Decode(const lldb_private::DataExtractor & data,lldb::offset_t * offset_ptr)232145449b1SDimitry Andric bool CacheSignature::Decode(const lldb_private::DataExtractor &data,
23377fc4c14SDimitry Andric lldb::offset_t *offset_ptr) {
23477fc4c14SDimitry Andric Clear();
23577fc4c14SDimitry Andric while (uint8_t sig_encoding = data.GetU8(offset_ptr)) {
23677fc4c14SDimitry Andric switch (sig_encoding) {
23777fc4c14SDimitry Andric case eSignatureUUID: {
23877fc4c14SDimitry Andric const uint8_t length = data.GetU8(offset_ptr);
23977fc4c14SDimitry Andric const uint8_t *bytes = (const uint8_t *)data.GetData(offset_ptr, length);
24077fc4c14SDimitry Andric if (bytes != nullptr && length > 0)
241e3b55780SDimitry Andric m_uuid = UUID(llvm::ArrayRef<uint8_t>(bytes, length));
24277fc4c14SDimitry Andric } break;
24377fc4c14SDimitry Andric case eSignatureModTime: {
24477fc4c14SDimitry Andric uint32_t mod_time = data.GetU32(offset_ptr);
24577fc4c14SDimitry Andric if (mod_time > 0)
24677fc4c14SDimitry Andric m_mod_time = mod_time;
24777fc4c14SDimitry Andric } break;
24877fc4c14SDimitry Andric case eSignatureObjectModTime: {
24977fc4c14SDimitry Andric uint32_t mod_time = data.GetU32(offset_ptr);
25077fc4c14SDimitry Andric if (mod_time > 0)
251145449b1SDimitry Andric m_obj_mod_time = mod_time;
25277fc4c14SDimitry Andric } break;
25377fc4c14SDimitry Andric case eSignatureEnd:
254145449b1SDimitry Andric // The definition of is valid changed to only be valid if the UUID is
255145449b1SDimitry Andric // valid so make sure that if we attempt to decode an old cache file
256145449b1SDimitry Andric // that we will fail to decode the cache file if the signature isn't
257145449b1SDimitry Andric // considered valid.
258145449b1SDimitry Andric return IsValid();
25977fc4c14SDimitry Andric default:
26077fc4c14SDimitry Andric break;
26177fc4c14SDimitry Andric }
26277fc4c14SDimitry Andric }
26377fc4c14SDimitry Andric return false;
26477fc4c14SDimitry Andric }
26577fc4c14SDimitry Andric
Add(ConstString s)26677fc4c14SDimitry Andric uint32_t ConstStringTable::Add(ConstString s) {
26777fc4c14SDimitry Andric auto pos = m_string_to_offset.find(s);
26877fc4c14SDimitry Andric if (pos != m_string_to_offset.end())
26977fc4c14SDimitry Andric return pos->second;
27077fc4c14SDimitry Andric const uint32_t offset = m_next_offset;
27177fc4c14SDimitry Andric m_strings.push_back(s);
27277fc4c14SDimitry Andric m_string_to_offset[s] = offset;
27377fc4c14SDimitry Andric m_next_offset += s.GetLength() + 1;
27477fc4c14SDimitry Andric return offset;
27577fc4c14SDimitry Andric }
27677fc4c14SDimitry Andric
27777fc4c14SDimitry Andric static const llvm::StringRef kStringTableIdentifier("STAB");
27877fc4c14SDimitry Andric
Encode(DataEncoder & encoder)27977fc4c14SDimitry Andric bool ConstStringTable::Encode(DataEncoder &encoder) {
28077fc4c14SDimitry Andric // Write an 4 character code into the stream. This will help us when decoding
28177fc4c14SDimitry Andric // to make sure we find this identifier when decoding the string table to make
28277fc4c14SDimitry Andric // sure we have the rigth data. It also helps to identify the string table
28377fc4c14SDimitry Andric // when dumping the hex bytes in a cache file.
28477fc4c14SDimitry Andric encoder.AppendData(kStringTableIdentifier);
28577fc4c14SDimitry Andric size_t length_offset = encoder.GetByteSize();
28677fc4c14SDimitry Andric encoder.AppendU32(0); // Total length of all strings which will be fixed up.
28777fc4c14SDimitry Andric size_t strtab_offset = encoder.GetByteSize();
288b1c73532SDimitry Andric encoder.AppendU8(0); // Start the string table with an empty string.
28977fc4c14SDimitry Andric for (auto s: m_strings) {
29077fc4c14SDimitry Andric // Make sure all of the offsets match up with what we handed out!
29177fc4c14SDimitry Andric assert(m_string_to_offset.find(s)->second ==
29277fc4c14SDimitry Andric encoder.GetByteSize() - strtab_offset);
29377fc4c14SDimitry Andric // Append the C string into the encoder
29477fc4c14SDimitry Andric encoder.AppendCString(s.GetStringRef());
29577fc4c14SDimitry Andric }
29677fc4c14SDimitry Andric // Fixup the string table length.
29777fc4c14SDimitry Andric encoder.PutU32(length_offset, encoder.GetByteSize() - strtab_offset);
29877fc4c14SDimitry Andric return true;
29977fc4c14SDimitry Andric }
30077fc4c14SDimitry Andric
Decode(const lldb_private::DataExtractor & data,lldb::offset_t * offset_ptr)301145449b1SDimitry Andric bool StringTableReader::Decode(const lldb_private::DataExtractor &data,
30277fc4c14SDimitry Andric lldb::offset_t *offset_ptr) {
30377fc4c14SDimitry Andric llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
30477fc4c14SDimitry Andric if (identifier != kStringTableIdentifier)
30577fc4c14SDimitry Andric return false;
30677fc4c14SDimitry Andric const uint32_t length = data.GetU32(offset_ptr);
30777fc4c14SDimitry Andric // We always have at least one byte for the empty string at offset zero.
30877fc4c14SDimitry Andric if (length == 0)
30977fc4c14SDimitry Andric return false;
31077fc4c14SDimitry Andric const char *bytes = (const char *)data.GetData(offset_ptr, length);
31177fc4c14SDimitry Andric if (bytes == nullptr)
31277fc4c14SDimitry Andric return false;
313145449b1SDimitry Andric m_data = llvm::StringRef(bytes, length);
31477fc4c14SDimitry Andric return true;
31577fc4c14SDimitry Andric }
31677fc4c14SDimitry Andric
Get(uint32_t offset) const317145449b1SDimitry Andric llvm::StringRef StringTableReader::Get(uint32_t offset) const {
31877fc4c14SDimitry Andric if (offset >= m_data.size())
319145449b1SDimitry Andric return llvm::StringRef();
320145449b1SDimitry Andric return llvm::StringRef(m_data.data() + offset);
32177fc4c14SDimitry Andric }
322e3b55780SDimitry Andric
323