brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · b34fda5 Raw
156 lines · cpp
1//===-- NameToDIE.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "NameToDIE.h"10#include "DWARFUnit.h"11#include "lldb/Core/DataFileCache.h"12#include "lldb/Symbol/ObjectFile.h"13#include "lldb/Utility/ConstString.h"14#include "lldb/Utility/DataEncoder.h"15#include "lldb/Utility/DataExtractor.h"16#include "lldb/Utility/RegularExpression.h"17#include "lldb/Utility/Stream.h"18#include "lldb/Utility/StreamString.h"19#include "lldb/lldb-private-enumerations.h"20#include <optional>21 22using namespace lldb;23using namespace lldb_private;24using namespace lldb_private::plugin::dwarf;25 26void NameToDIE::Finalize() {27  m_map.Sort(std::less<DIERef>());28  m_map.SizeToFit();29}30 31void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {32  m_map.Append(name, die_ref);33}34 35bool NameToDIE::Find(36    ConstString name,37    llvm::function_ref<IterationAction(DIERef ref)> callback) const {38  for (const auto &entry : m_map.equal_range(name))39    if (callback(entry.value) == IterationAction::Stop)40      return false;41  return true;42}43 44bool NameToDIE::Find(45    const RegularExpression &regex,46    llvm::function_ref<IterationAction(DIERef ref)> callback) const {47  for (const auto &entry : m_map)48    if (regex.Execute(entry.cstring.GetCString())) {49      if (callback(entry.value) == IterationAction::Stop)50        return false;51    }52  return true;53}54 55void NameToDIE::FindAllEntriesForUnit(56    DWARFUnit &s_unit,57    llvm::function_ref<IterationAction(DIERef ref)> callback) const {58  const DWARFUnit &ns_unit = s_unit.GetNonSkeletonUnit();59  const uint32_t size = m_map.GetSize();60  for (uint32_t i = 0; i < size; ++i) {61    const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);62    if (ns_unit.GetSymbolFileDWARF().GetFileIndex() == die_ref.file_index() &&63        ns_unit.GetDebugSection() == die_ref.section() &&64        ns_unit.GetOffset() <= die_ref.die_offset() &&65        die_ref.die_offset() < ns_unit.GetNextUnitOffset()) {66      if (callback(die_ref) == IterationAction::Stop)67        return;68    }69  }70}71 72void NameToDIE::Dump(Stream *s) {73  const uint32_t size = m_map.GetSize();74  for (uint32_t i = 0; i < size; ++i) {75    s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),76              m_map.GetCStringAtIndexUnchecked(i));77  }78}79 80void NameToDIE::ForEach(81    std::function<bool(ConstString name, const DIERef &die_ref)> const82        &callback) const {83  const uint32_t size = m_map.GetSize();84  for (uint32_t i = 0; i < size; ++i) {85    if (!callback(m_map.GetCStringAtIndexUnchecked(i),86                  m_map.GetValueAtIndexUnchecked(i)))87      break;88  }89}90 91void NameToDIE::Append(const NameToDIE &other) {92  const uint32_t size = other.m_map.GetSize();93  for (uint32_t i = 0; i < size; ++i) {94    m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),95                 other.m_map.GetValueAtIndexUnchecked(i));96  }97}98 99constexpr llvm::StringLiteral kIdentifierNameToDIE("N2DI");100 101bool NameToDIE::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,102                       const StringTableReader &strtab) {103  m_map.Clear();104  llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);105  if (identifier != kIdentifierNameToDIE)106    return false;107  const uint32_t count = data.GetU32(offset_ptr);108  m_map.Reserve(count);109  for (uint32_t i = 0; i < count; ++i) {110    llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));111    // No empty strings allowed in the name to DIE maps.112    if (str.empty())113      return false;114    if (std::optional<DIERef> die_ref = DIERef::Decode(data, offset_ptr))115      m_map.Append(ConstString(str), *die_ref);116    else117      return false;118  }119  // We must sort the UniqueCStringMap after decoding it since it is a vector120  // of UniqueCStringMap::Entry objects which contain a ConstString and type T.121  // ConstString objects are sorted by "const char *" and then type T and122  // the "const char *" are point values that will depend on the order in which123  // ConstString objects are created and in which of the 256 string pools they124  // are created in. So after we decode all of the entries, we must sort the125  // name map to ensure name lookups succeed. If we encode and decode within126  // the same process we wouldn't need to sort, so unit testing didn't catch127  // this issue when first checked in.128  m_map.Sort(std::less<DIERef>());129  return true;130}131 132void NameToDIE::Encode(DataEncoder &encoder, ConstStringTable &strtab) const {133  encoder.AppendData(kIdentifierNameToDIE);134  encoder.AppendU32(m_map.GetSize());135  for (const auto &entry : m_map) {136    // Make sure there are no empty strings.137    assert((bool)entry.cstring);138    encoder.AppendU32(strtab.Add(entry.cstring));139    entry.value.Encode(encoder);140  }141}142 143bool NameToDIE::operator==(const NameToDIE &rhs) const {144  const size_t size = m_map.GetSize();145  if (size != rhs.m_map.GetSize())146    return false;147  for (size_t i = 0; i < size; ++i) {148    if (m_map.GetCStringAtIndex(i) != rhs.m_map.GetCStringAtIndex(i))149      return false;150    if (m_map.GetValueRefAtIndexUnchecked(i) !=151        rhs.m_map.GetValueRefAtIndexUnchecked(i))152      return false;153  }154  return true;155}156