LLDB  mainline
NameToDIE.cpp
Go to the documentation of this file.
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "NameToDIE.h"
10 #include "DWARFUnit.h"
12 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/Stream.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
24  m_map.Sort(std::less<DIERef>());
25  m_map.SizeToFit();
26 }
27 
28 void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {
29  m_map.Append(name, die_ref);
30 }
31 
33  llvm::function_ref<bool(DIERef ref)> callback) const {
34  for (const auto &entry : m_map.equal_range(name))
35  if (!callback(entry.value))
36  return false;
37  return true;
38 }
39 
41  llvm::function_ref<bool(DIERef ref)> callback) const {
42  for (const auto &entry : m_map)
43  if (regex.Execute(entry.cstring.GetCString())) {
44  if (!callback(entry.value))
45  return false;
46  }
47  return true;
48 }
49 
51  DWARFUnit &s_unit, llvm::function_ref<bool(DIERef ref)> callback) const {
53  const DWARFUnit &ns_unit = s_unit.GetNonSkeletonUnit();
54  const uint32_t size = m_map.GetSize();
55  for (uint32_t i = 0; i < size; ++i) {
56  const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
57  if (ns_unit.GetSymbolFileDWARF().GetDwoNum() == die_ref.dwo_num() &&
58  ns_unit.GetDebugSection() == die_ref.section() &&
59  ns_unit.GetOffset() <= die_ref.die_offset() &&
60  die_ref.die_offset() < ns_unit.GetNextUnitOffset()) {
61  if (!callback(die_ref))
62  return;
63  }
64  }
65 }
66 
68  const uint32_t size = m_map.GetSize();
69  for (uint32_t i = 0; i < size; ++i) {
70  s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),
71  m_map.GetCStringAtIndexUnchecked(i));
72  }
73 }
74 
76  std::function<bool(ConstString name, const DIERef &die_ref)> const
77  &callback) const {
78  const uint32_t size = m_map.GetSize();
79  for (uint32_t i = 0; i < size; ++i) {
80  if (!callback(m_map.GetCStringAtIndexUnchecked(i),
81  m_map.GetValueAtIndexUnchecked(i)))
82  break;
83  }
84 }
85 
86 void NameToDIE::Append(const NameToDIE &other) {
87  const uint32_t size = other.m_map.GetSize();
88  for (uint32_t i = 0; i < size; ++i) {
89  m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
91  }
92 }
93 
94 constexpr llvm::StringLiteral kIdentifierNameToDIE("N2DI");
95 
96 bool NameToDIE::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
97  const StringTableReader &strtab) {
98  m_map.Clear();
99  llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
100  if (identifier != kIdentifierNameToDIE)
101  return false;
102  const uint32_t count = data.GetU32(offset_ptr);
103  m_map.Reserve(count);
104  for (uint32_t i = 0; i < count; ++i) {
105  llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
106  // No empty strings allowed in the name to DIE maps.
107  if (str.empty())
108  return false;
109  if (llvm::Optional<DIERef> die_ref = DIERef::Decode(data, offset_ptr))
110  m_map.Append(ConstString(str), *die_ref);
111  else
112  return false;
113  }
114  // We must sort the UniqueCStringMap after decoding it since it is a vector
115  // of UniqueCStringMap::Entry objects which contain a ConstString and type T.
116  // ConstString objects are sorted by "const char *" and then type T and
117  // the "const char *" are point values that will depend on the order in which
118  // ConstString objects are created and in which of the 256 string pools they
119  // are created in. So after we decode all of the entries, we must sort the
120  // name map to ensure name lookups succeed. If we encode and decode within
121  // the same process we wouldn't need to sort, so unit testing didn't catch
122  // this issue when first checked in.
123  m_map.Sort(std::less<DIERef>());
124  return true;
125 }
126 
127 void NameToDIE::Encode(DataEncoder &encoder, ConstStringTable &strtab) const {
128  encoder.AppendData(kIdentifierNameToDIE);
129  encoder.AppendU32(m_map.GetSize());
130  for (const auto &entry : m_map) {
131  // Make sure there are no empty strings.
132  assert((bool)entry.cstring);
133  encoder.AppendU32(strtab.Add(entry.cstring));
134  entry.value.Encode(encoder);
135  }
136 }
137 
138 bool NameToDIE::operator==(const NameToDIE &rhs) const {
139  const size_t size = m_map.GetSize();
140  if (size != rhs.m_map.GetSize())
141  return false;
142  for (size_t i = 0; i < size; ++i) {
143  if (m_map.GetCStringAtIndex(i) != rhs.m_map.GetCStringAtIndex(i))
144  return false;
145  if (m_map.GetValueRefAtIndexUnchecked(i) !=
147  return false;
148  }
149  return true;
150 }
RegularExpression.h
lldb_private::Stream::Format
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
DIERef
Identifies a DWARF debug info entry within a given Module.
Definition: DIERef.h:26
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::StringTableReader
Many cache files require string tables to store data efficiently.
Definition: DataFileCache.h:212
NameToDIE::Encode
void Encode(lldb_private::DataEncoder &encoder, lldb_private::ConstStringTable &strtab) const
Encode this object into a data encoder object.
Definition: NameToDIE.cpp:127
lldb_private::ConstStringTable
Many cache files require string tables to store data efficiently.
Definition: DataFileCache.h:180
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Stream
Definition: Stream.h:28
DIERef::die_offset
dw_offset_t die_offset() const
Definition: DIERef.h:45
DataFileCache.h
lldb_private::ConstStringTable::Add
uint32_t Add(ConstString s)
Add a string into the string table.
Definition: DataFileCache.cpp:266
lldb_private::UniqueCStringMap::GetValueAtIndexUnchecked
T GetValueAtIndexUnchecked(uint32_t idx) const
Definition: UniqueCStringMap.h:68
DWARFUnit
Definition: DWARFUnit.h:83
DWARFUnit::GetNextUnitOffset
dw_offset_t GetNextUnitOffset() const
Definition: DWARFUnit.h:149
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::UniqueCStringMap::GetSize
size_t GetSize() const
Definition: UniqueCStringMap.h:148
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:39
DataEncoder.h
DIERef::Decode
static llvm::Optional< DIERef > Decode(const lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr)
Decode a serialized version of this object from data.
Definition: DIERef.cpp:29
NameToDIE::FindAllEntriesForUnit
void FindAllEntriesForUnit(DWARFUnit &unit, llvm::function_ref< bool(DIERef ref)> callback) const
unit must be the skeleton unit if possible, not GetNonSkeletonUnit().
Definition: NameToDIE.cpp:50
DWARFUnit.h
NameToDIE::Insert
void Insert(lldb_private::ConstString name, const DIERef &die_ref)
Definition: NameToDIE.cpp:28
NameToDIE::Find
bool Find(lldb_private::ConstString name, llvm::function_ref< bool(DIERef ref)> callback) const
Definition: NameToDIE.cpp:32
ObjectFile.h
DWARFUnit::GetOffset
dw_offset_t GetOffset() const
Definition: DWARFUnit.h:133
lldb_private::UniqueCStringMap::GetCStringAtIndex
ConstString GetCStringAtIndex(uint32_t idx) const
Definition: UniqueCStringMap.h:76
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
kIdentifierNameToDIE
constexpr llvm::StringLiteral kIdentifierNameToDIE("N2DI")
uint32_t
NameToDIE::Decode
bool Decode(const lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, const lldb_private::StringTableReader &strtab)
Decode a serialized version of this object from data.
Definition: NameToDIE.cpp:96
DIERef::section
Section section() const
Definition: DIERef.h:43
NameToDIE::Append
void Append(const NameToDIE &other)
Definition: NameToDIE.cpp:86
DWARFUnit::GetNonSkeletonUnit
DWARFUnit & GetNonSkeletonUnit()
Definition: DWARFUnit.cpp:654
lldb_private::UniqueCStringMap::GetValueRefAtIndexUnchecked
const T & GetValueRefAtIndexUnchecked(uint32_t idx) const
Definition: UniqueCStringMap.h:72
NameToDIE::m_map
lldb_private::UniqueCStringMap< DIERef > m_map
Definition: NameToDIE.h:90
lldb_private::DataExtractor::GetU32
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:425
DIERef::dwo_num
llvm::Optional< uint32_t > dwo_num() const
Definition: DIERef.h:37
NameToDIE.h
DataExtractor.h
NameToDIE::operator==
bool operator==(const NameToDIE &rhs) const
Used for unit testing the encoding and decoding.
Definition: NameToDIE.cpp:138
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
NameToDIE::Dump
void Dump(lldb_private::Stream *s)
Definition: NameToDIE.cpp:67
lldb_private::RegularExpression::Execute
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
Definition: RegularExpression.cpp:23
SymbolFileDWARF::GetDwoNum
virtual llvm::Optional< uint32_t > GetDwoNum()
Definition: SymbolFileDWARF.h:281
ConstString.h
lldb_private::DataExtractor::GetData
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
Definition: DataExtractor.h:337
Stream.h
DWARFUnit::GetSymbolFileDWARF
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition: DWARFUnit.h:199
NameToDIE::Finalize
void Finalize()
Definition: NameToDIE.cpp:23
DWARFUnit::GetDebugSection
DIERef::Section GetDebugSection() const
Definition: DWARFUnit.h:221
lldb
Definition: SBAddress.h:15
NameToDIE
Definition: NameToDIE.h:21
NameToDIE::ForEach
void ForEach(std::function< bool(lldb_private::ConstString name, const DIERef &die_ref)> const &callback) const
Definition: NameToDIE.cpp:75
lldb_private::StringTableReader::Get
llvm::StringRef Get(uint32_t offset) const
Definition: DataFileCache.cpp:317
lldb_private::UniqueCStringMap::GetCStringAtIndexUnchecked
ConstString GetCStringAtIndexUnchecked(uint32_t idx) const
Definition: UniqueCStringMap.h:62