llvm-project/lldb/source/Utility/DataEncoder.cpp
Greg Clayton da816ca0cb Added the ability to cache the finalized symbol tables subsequent debug sessions to start faster.
This is an updated version of the https://reviews.llvm.org/D113789 patch with the following changes:
- We no longer modify modification times of the cache files
- Use LLVM caching and cache pruning instead of making a new cache mechanism (See DataFileCache.h/.cpp)
- Add signature to start of each file since we are not using modification times so we can tell when caches are stale and remove and re-create the cache file as files are changed
- Add settings to control the cache size, disk percentage and expiration in days to keep cache size under control

This patch enables symbol tables to be cached in the LLDB index cache directory. All cache files are in a single directory and the files use unique names to ensure that files from the same path will re-use the same file as files get modified. This means as files change, their cache files will be deleted and updated. The modification time of each of the cache files is not modified so that access based pruning of the cache can be implemented.

The symbol table cache files start with a signature that uniquely identifies a file on disk and contains one or more of the following items:
- object file UUID if available
- object file mod time if available
- object name for BSD archive .o files that are in .a files if available

If none of these signature items are available, then the file will not be cached. This keeps temporary object files from expressions from being cached.

When the cache files are loaded on subsequent debug sessions, the signature is compare and if the file has been modified (uuid changes, mod time changes, or object file mod time changes) then the cache file is deleted and re-created.

Module caching must be enabled by the user before this can be used:

symbols.enable-lldb-index-cache (boolean) = false

(lldb) settings set symbols.enable-lldb-index-cache true

There is also a setting that allows the user to specify a module cache directory that defaults to a directory that defaults to being next to the symbols.clang-modules-cache-path directory in a temp directory:

(lldb) settings show symbols.lldb-index-cache-path
/var/folders/9p/472sr0c55l9b20x2zg36b91h0000gn/C/lldb/IndexCache

If this setting is enabled, the finalized symbol tables will be serialized and saved to disc so they can be quickly loaded next time you debug.

Each module can cache one or more files in the index cache directory. The cache file names must be unique to a file on disk and its architecture and object name for .o files in BSD archives. This allows universal mach-o files to support caching multuple architectures in the same module cache directory. Making the file based on the this info allows this cache file to be deleted and replaced when the file gets updated on disk. This keeps the cache from growing over time during the compile/edit/debug cycle and prevents out of space issues.

If the cache is enabled, the symbol table will be loaded from the cache the next time you debug if the module has not changed.

The cache also has settings to control the size of the cache on disk. Each time LLDB starts up with the index cache enable, the cache will be pruned to ensure it stays within the user defined settings:

(lldb) settings set symbols.lldb-index-cache-expiration-days <days>

A value of zero will disable cache files from expiring when the cache is pruned. The default value is 7 currently.

(lldb) settings set symbols.lldb-index-cache-max-byte-size <size>

A value of zero will disable pruning based on a total byte size. The default value is zero currently.
(lldb) settings set symbols.lldb-index-cache-max-percent <percentage-of-disk-space>

A value of 100 will allow the disc to be filled to the max, a value of zero will disable percentage pruning. The default value is zero.

Reviewed By: labath, wallace

Differential Revision: https://reviews.llvm.org/D115324
2021-12-16 09:59:55 -08:00

192 lines
5.4 KiB
C++

//===-- DataEncoder.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/DataEncoder.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/Endian.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstddef>
#include <cstring>
using namespace lldb;
using namespace lldb_private;
using namespace llvm::support::endian;
DataEncoder::DataEncoder()
: m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
m_addr_size(sizeof(void *)) {}
DataEncoder::DataEncoder(const void *data, uint32_t length, ByteOrder endian,
uint8_t addr_size)
: m_data_sp(new DataBufferHeap(data, length)), m_byte_order(endian),
m_addr_size(addr_size) {}
DataEncoder::DataEncoder(ByteOrder endian, uint8_t addr_size)
: m_data_sp(new DataBufferHeap()), m_byte_order(endian),
m_addr_size(addr_size) {}
DataEncoder::~DataEncoder() = default;
llvm::ArrayRef<uint8_t> DataEncoder::GetData() const {
return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), GetByteSize());
}
size_t DataEncoder::GetByteSize() const { return m_data_sp->GetByteSize(); }
// Extract a single unsigned char from the binary data and update the offset
// pointed to by "offset_ptr".
//
// RETURNS the byte that was extracted, or zero on failure.
uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
if (ValidOffset(offset)) {
m_data_sp->GetBytes()[offset] = value;
return offset + 1;
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
if (m_byte_order != endian::InlHostByteOrder())
write16be(m_data_sp->GetBytes() + offset, value);
else
write16le(m_data_sp->GetBytes() + offset, value);
return offset + sizeof(value);
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
if (m_byte_order != endian::InlHostByteOrder())
write32be(m_data_sp->GetBytes() + offset, value);
else
write32le(m_data_sp->GetBytes() + offset, value);
return offset + sizeof(value);
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
if (m_byte_order != endian::InlHostByteOrder())
write64be(m_data_sp->GetBytes() + offset, value);
else
write64le(m_data_sp->GetBytes() + offset, value);
return offset + sizeof(value);
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
uint64_t value) {
switch (byte_size) {
case 1:
return PutU8(offset, value);
case 2:
return PutU16(offset, value);
case 4:
return PutU32(offset, value);
case 8:
return PutU64(offset, value);
default:
llvm_unreachable("GetMax64 unhandled case!");
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
uint32_t src_len) {
if (src == nullptr || src_len == 0)
return offset;
if (ValidOffsetForDataOfSize(offset, src_len)) {
memcpy(m_data_sp->GetBytes() + offset, src, src_len);
return offset + src_len;
}
return UINT32_MAX;
}
uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
return PutUnsigned(offset, m_addr_size, addr);
}
uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
if (cstr != nullptr)
return PutData(offset, cstr, strlen(cstr) + 1);
return UINT32_MAX;
}
void DataEncoder::AppendU8(uint8_t value) {
m_data_sp->AppendData(&value, sizeof(value));
}
void DataEncoder::AppendU16(uint16_t value) {
uint32_t offset = m_data_sp->GetByteSize();
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
PutU16(offset, value);
}
void DataEncoder::AppendU32(uint32_t value) {
uint32_t offset = m_data_sp->GetByteSize();
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
PutU32(offset, value);
}
void DataEncoder::AppendU64(uint64_t value) {
uint32_t offset = m_data_sp->GetByteSize();
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
PutU64(offset, value);
}
void DataEncoder::AppendAddress(lldb::addr_t addr) {
switch (m_addr_size) {
case 4:
AppendU32(addr);
break;
case 8:
AppendU64(addr);
break;
default:
llvm_unreachable("AppendAddress unhandled case!");
}
}
void DataEncoder::AppendData(llvm::StringRef data) {
const char *bytes = data.data();
const size_t length = data.size();
if (bytes && length > 0)
m_data_sp->AppendData(bytes, length);
}
void DataEncoder::AppendData(llvm::ArrayRef<uint8_t> data) {
const uint8_t *bytes = data.data();
const size_t length = data.size();
if (bytes && length > 0)
m_data_sp->AppendData(bytes, length);
}
void DataEncoder::AppendCString(llvm::StringRef data) {
const char *bytes = data.data();
const size_t length = data.size();
if (bytes) {
if (length > 0)
m_data_sp->AppendData(bytes, length);
if (length == 0 || bytes[length - 1] != '\0')
AppendU8(0);
}
}