1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
//===-- AppleDWARFIndex.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 "Plugins/SymbolFile/DWARF/AppleDWARFIndex.h"
#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
#include "Plugins/SymbolFile/DWARF/LogChannelDWARF.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/Function.h"
#include "llvm/Support/DJB.h"
using namespace lldb_private;
using namespace lldb;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;
std::unique_ptr<AppleDWARFIndex> AppleDWARFIndex::Create(
Module &module, DWARFDataExtractor apple_names,
DWARFDataExtractor apple_namespaces, DWARFDataExtractor apple_types,
DWARFDataExtractor apple_objc, DWARFDataExtractor debug_str) {
llvm::DataExtractor llvm_debug_str = debug_str.GetAsLLVM();
auto apple_names_table_up = std::make_unique<llvm::AppleAcceleratorTable>(
apple_names.GetAsLLVMDWARF(), llvm_debug_str);
auto apple_namespaces_table_up =
std::make_unique<llvm::AppleAcceleratorTable>(
apple_namespaces.GetAsLLVMDWARF(), llvm_debug_str);
auto apple_types_table_up = std::make_unique<llvm::AppleAcceleratorTable>(
apple_types.GetAsLLVMDWARF(), llvm_debug_str);
auto apple_objc_table_up = std::make_unique<llvm::AppleAcceleratorTable>(
apple_objc.GetAsLLVMDWARF(), llvm_debug_str);
auto extract_and_check = [](auto &TablePtr) {
if (auto E = TablePtr->extract()) {
llvm::consumeError(std::move(E));
TablePtr.reset();
}
};
extract_and_check(apple_names_table_up);
extract_and_check(apple_namespaces_table_up);
extract_and_check(apple_types_table_up);
extract_and_check(apple_objc_table_up);
assert(apple_names.GetByteSize() == 0 || apple_names.GetSharedDataBuffer());
assert(apple_namespaces.GetByteSize() == 0 ||
apple_namespaces.GetSharedDataBuffer());
assert(apple_types.GetByteSize() == 0 || apple_types.GetSharedDataBuffer());
assert(apple_objc.GetByteSize() == 0 || apple_objc.GetSharedDataBuffer());
if (apple_names_table_up || apple_namespaces_table_up ||
apple_types_table_up || apple_objc_table_up)
return std::make_unique<AppleDWARFIndex>(
module, std::move(apple_names_table_up),
std::move(apple_namespaces_table_up), std::move(apple_types_table_up),
std::move(apple_objc_table_up), apple_names.GetSharedDataBuffer(),
apple_namespaces.GetSharedDataBuffer(),
apple_types.GetSharedDataBuffer(), apple_objc.GetSharedDataBuffer());
return nullptr;
}
/// Returns true if `tag` is a class_type of structure_type tag.
static bool IsClassOrStruct(dw_tag_t tag) {
return tag == DW_TAG_class_type || tag == DW_TAG_structure_type;
}
/// Returns true if `entry` has an extractable DW_ATOM_qual_name_hash and it
/// matches `expected_hash`.
static bool
EntryHasMatchingQualhash(const llvm::AppleAcceleratorTable::Entry &entry,
uint32_t expected_hash) {
std::optional<llvm::DWARFFormValue> form_value =
entry.lookup(dwarf::DW_ATOM_qual_name_hash);
if (!form_value)
return false;
std::optional<uint64_t> hash = form_value->getAsUnsignedConstant();
return hash && (*hash == expected_hash);
}
/// Returns true if `entry` has an extractable DW_ATOM_die_tag and it matches
/// `expected_tag`. We also consider it a match if the tags are different but
/// in the set of {TAG_class_type, TAG_struct_type}.
static bool EntryHasMatchingTag(const llvm::AppleAcceleratorTable::Entry &entry,
dw_tag_t expected_tag) {
std::optional<llvm::DWARFFormValue> form_value =
entry.lookup(dwarf::DW_ATOM_die_tag);
if (!form_value)
return false;
std::optional<uint64_t> maybe_tag = form_value->getAsUnsignedConstant();
if (!maybe_tag)
return false;
auto tag = static_cast<dw_tag_t>(*maybe_tag);
return tag == expected_tag ||
(IsClassOrStruct(tag) && IsClassOrStruct(expected_tag));
}
/// Returns true if `entry` has an extractable DW_ATOM_type_flags and the flag
/// "DW_FLAG_type_implementation" is set.
static bool
HasImplementationFlag(const llvm::AppleAcceleratorTable::Entry &entry) {
std::optional<llvm::DWARFFormValue> form_value =
entry.lookup(dwarf::DW_ATOM_type_flags);
if (!form_value)
return false;
std::optional<uint64_t> Flags = form_value->getAsUnsignedConstant();
return Flags &&
(*Flags & llvm::dwarf::AcceleratorTable::DW_FLAG_type_implementation);
}
void AppleDWARFIndex::SearchFor(const llvm::AppleAcceleratorTable &table,
llvm::StringRef name,
llvm::function_ref<bool(DWARFDIE die)> callback,
std::optional<dw_tag_t> search_for_tag,
std::optional<uint32_t> search_for_qualhash) {
auto converted_cb = DIERefCallback(callback, name);
for (const auto &entry : table.equal_range(name)) {
if (search_for_qualhash &&
!EntryHasMatchingQualhash(entry, *search_for_qualhash))
continue;
if (search_for_tag && !EntryHasMatchingTag(entry, *search_for_tag))
continue;
if (!converted_cb(entry))
break;
}
}
void AppleDWARFIndex::GetGlobalVariables(
ConstString basename, llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_names_up)
return;
SearchFor(*m_apple_names_up, basename, callback);
}
void AppleDWARFIndex::GetGlobalVariables(
const RegularExpression ®ex,
llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_names_up)
return;
DIERefCallbackImpl converted_cb = DIERefCallback(callback, regex.GetText());
for (const auto &entry : m_apple_names_up->entries())
if (std::optional<llvm::StringRef> name = entry.readName();
name && Mangled(*name).NameMatches(regex))
if (!converted_cb(entry.BaseEntry))
return;
}
void AppleDWARFIndex::GetGlobalVariables(
DWARFUnit &cu, llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_names_up)
return;
const DWARFUnit &non_skeleton_cu = cu.GetNonSkeletonUnit();
dw_offset_t lower_bound = non_skeleton_cu.GetOffset();
dw_offset_t upper_bound = non_skeleton_cu.GetNextUnitOffset();
auto is_in_range = [lower_bound, upper_bound](std::optional<uint32_t> val) {
return val.has_value() && *val >= lower_bound && *val < upper_bound;
};
DIERefCallbackImpl converted_cb = DIERefCallback(callback);
for (auto entry : m_apple_names_up->entries()) {
if (is_in_range(entry.BaseEntry.getDIESectionOffset()))
if (!converted_cb(entry.BaseEntry))
return;
}
}
void AppleDWARFIndex::GetObjCMethods(
ConstString class_name, llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_objc_up)
return;
SearchFor(*m_apple_objc_up, class_name, callback);
}
void AppleDWARFIndex::GetCompleteObjCClass(
ConstString class_name, bool must_be_implementation,
llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_types_up)
return;
llvm::SmallVector<DIERef> decl_dies;
auto converted_cb = DIERefCallback(callback, class_name);
for (const auto &entry : m_apple_types_up->equal_range(class_name)) {
if (HasImplementationFlag(entry)) {
converted_cb(entry);
return;
}
decl_dies.emplace_back(std::nullopt, DIERef::Section::DebugInfo,
*entry.getDIESectionOffset());
}
if (must_be_implementation)
return;
for (DIERef ref : decl_dies)
if (!converted_cb(ref))
return;
}
void AppleDWARFIndex::GetTypes(
ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_types_up)
return;
SearchFor(*m_apple_types_up, name, callback);
}
void AppleDWARFIndex::GetTypes(
const DWARFDeclContext &context,
llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_types_up)
return;
Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
const bool entries_have_tag =
m_apple_types_up->containsAtomType(DW_ATOM_die_tag);
const bool entries_have_qual_hash =
m_apple_types_up->containsAtomType(DW_ATOM_qual_name_hash);
llvm::StringRef expected_name = context[0].name;
if (entries_have_tag && entries_have_qual_hash) {
const dw_tag_t expected_tag = context[0].tag;
const uint32_t expected_qualname_hash =
llvm::djbHash(context.GetQualifiedName());
if (log)
m_module.LogMessage(log, "FindByNameAndTagAndQualifiedNameHash()");
SearchFor(*m_apple_types_up, expected_name, callback, expected_tag,
expected_qualname_hash);
return;
}
// Historically, if there are no tags, we also ignore qual_hash (why?)
if (!entries_have_tag) {
SearchFor(*m_apple_names_up, expected_name, callback);
return;
}
// We have a tag but no qual hash.
// When searching for a scoped type (for example,
// "std::vector<int>::const_iterator") searching for the innermost
// name alone ("const_iterator") could yield many false
// positives. By searching for the parent type ("vector<int>")
// first we can avoid extracting type DIEs from object files that
// would fail the filter anyway.
if ((context.GetSize() > 1) && IsClassOrStruct(context[1].tag))
if (m_apple_types_up->equal_range(context[1].name).empty())
return;
if (log)
m_module.LogMessage(log, "FindByNameAndTag()");
const dw_tag_t expected_tag = context[0].tag;
SearchFor(*m_apple_types_up, expected_name, callback, expected_tag);
return;
}
void AppleDWARFIndex::GetNamespaces(
ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_namespaces_up)
return;
SearchFor(*m_apple_namespaces_up, name, callback);
}
void AppleDWARFIndex::GetFunctions(
const Module::LookupInfo &lookup_info, SymbolFileDWARF &dwarf,
const CompilerDeclContext &parent_decl_ctx,
llvm::function_ref<bool(DWARFDIE die)> callback) {
if (!m_apple_names_up)
return;
ConstString name = lookup_info.GetLookupName();
for (const auto &entry : m_apple_names_up->equal_range(name)) {
DIERef die_ref(std::nullopt, DIERef::Section::DebugInfo,
*entry.getDIESectionOffset());
if (!ProcessFunctionDIE(lookup_info, die_ref, dwarf, parent_decl_ctx,
callback))
return;
}
}
void AppleDWARFIndex::GetFunctions(
const RegularExpression ®ex,
llvm::function_ref<bool(DWARFDIE die)> callback) {
return GetGlobalVariables(regex, callback);
}
void AppleDWARFIndex::Dump(Stream &s) {
if (m_apple_names_up)
s.PutCString(".apple_names index present\n");
if (m_apple_namespaces_up)
s.PutCString(".apple_namespaces index present\n");
if (m_apple_types_up)
s.PutCString(".apple_types index present\n");
if (m_apple_objc_up)
s.PutCString(".apple_objc index present\n");
// TODO: Dump index contents
}
|