Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/NameToDIE.cpp
213845 views
//===-- NameToDIE.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "NameToDIE.h"9#include "DWARFUnit.h"10#include "lldb/Core/DataFileCache.h"11#include "lldb/Symbol/ObjectFile.h"12#include "lldb/Utility/ConstString.h"13#include "lldb/Utility/DataEncoder.h"14#include "lldb/Utility/DataExtractor.h"15#include "lldb/Utility/RegularExpression.h"16#include "lldb/Utility/Stream.h"17#include "lldb/Utility/StreamString.h"18#include <optional>1920using namespace lldb;21using namespace lldb_private;22using namespace lldb_private::plugin::dwarf;2324void NameToDIE::Finalize() {25m_map.Sort(std::less<DIERef>());26m_map.SizeToFit();27}2829void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {30m_map.Append(name, die_ref);31}3233bool NameToDIE::Find(ConstString name,34llvm::function_ref<bool(DIERef ref)> callback) const {35for (const auto &entry : m_map.equal_range(name))36if (!callback(entry.value))37return false;38return true;39}4041bool NameToDIE::Find(const RegularExpression ®ex,42llvm::function_ref<bool(DIERef ref)> callback) const {43for (const auto &entry : m_map)44if (regex.Execute(entry.cstring.GetCString())) {45if (!callback(entry.value))46return false;47}48return true;49}5051void NameToDIE::FindAllEntriesForUnit(52DWARFUnit &s_unit, llvm::function_ref<bool(DIERef ref)> callback) const {53const DWARFUnit &ns_unit = s_unit.GetNonSkeletonUnit();54const uint32_t size = m_map.GetSize();55for (uint32_t i = 0; i < size; ++i) {56const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);57if (ns_unit.GetSymbolFileDWARF().GetFileIndex() == die_ref.file_index() &&58ns_unit.GetDebugSection() == die_ref.section() &&59ns_unit.GetOffset() <= die_ref.die_offset() &&60die_ref.die_offset() < ns_unit.GetNextUnitOffset()) {61if (!callback(die_ref))62return;63}64}65}6667void NameToDIE::Dump(Stream *s) {68const uint32_t size = m_map.GetSize();69for (uint32_t i = 0; i < size; ++i) {70s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),71m_map.GetCStringAtIndexUnchecked(i));72}73}7475void NameToDIE::ForEach(76std::function<bool(ConstString name, const DIERef &die_ref)> const77&callback) const {78const uint32_t size = m_map.GetSize();79for (uint32_t i = 0; i < size; ++i) {80if (!callback(m_map.GetCStringAtIndexUnchecked(i),81m_map.GetValueAtIndexUnchecked(i)))82break;83}84}8586void NameToDIE::Append(const NameToDIE &other) {87const uint32_t size = other.m_map.GetSize();88for (uint32_t i = 0; i < size; ++i) {89m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),90other.m_map.GetValueAtIndexUnchecked(i));91}92}9394constexpr llvm::StringLiteral kIdentifierNameToDIE("N2DI");9596bool NameToDIE::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,97const StringTableReader &strtab) {98m_map.Clear();99llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);100if (identifier != kIdentifierNameToDIE)101return false;102const uint32_t count = data.GetU32(offset_ptr);103m_map.Reserve(count);104for (uint32_t i = 0; i < count; ++i) {105llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));106// No empty strings allowed in the name to DIE maps.107if (str.empty())108return false;109if (std::optional<DIERef> die_ref = DIERef::Decode(data, offset_ptr))110m_map.Append(ConstString(str), *die_ref);111else112return false;113}114// We must sort the UniqueCStringMap after decoding it since it is a vector115// of UniqueCStringMap::Entry objects which contain a ConstString and type T.116// ConstString objects are sorted by "const char *" and then type T and117// the "const char *" are point values that will depend on the order in which118// ConstString objects are created and in which of the 256 string pools they119// are created in. So after we decode all of the entries, we must sort the120// name map to ensure name lookups succeed. If we encode and decode within121// the same process we wouldn't need to sort, so unit testing didn't catch122// this issue when first checked in.123m_map.Sort(std::less<DIERef>());124return true;125}126127void NameToDIE::Encode(DataEncoder &encoder, ConstStringTable &strtab) const {128encoder.AppendData(kIdentifierNameToDIE);129encoder.AppendU32(m_map.GetSize());130for (const auto &entry : m_map) {131// Make sure there are no empty strings.132assert((bool)entry.cstring);133encoder.AppendU32(strtab.Add(entry.cstring));134entry.value.Encode(encoder);135}136}137138bool NameToDIE::operator==(const NameToDIE &rhs) const {139const size_t size = m_map.GetSize();140if (size != rhs.m_map.GetSize())141return false;142for (size_t i = 0; i < size; ++i) {143if (m_map.GetCStringAtIndex(i) != rhs.m_map.GetCStringAtIndex(i))144return false;145if (m_map.GetValueRefAtIndexUnchecked(i) !=146rhs.m_map.GetValueRefAtIndexUnchecked(i))147return false;148}149return true;150}151152153