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