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