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"
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::plugin::dwarf;
25
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
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
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
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
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
81 std::function<bool(ConstString name, const DIERef &die_ref)> const
82 &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 else
117 return false;
118 }
119 // We must sort the UniqueCStringMap after decoding it since it is a vector
120 // of UniqueCStringMap::Entry objects which contain a ConstString and type T.
121 // ConstString objects are sorted by "const char *" and then type T and
122 // the "const char *" are point values that will depend on the order in which
123 // ConstString objects are created and in which of the 256 string pools they
124 // are created in. So after we decode all of the entries, we must sort the
125 // name map to ensure name lookups succeed. If we encode and decode within
126 // the same process we wouldn't need to sort, so unit testing didn't catch
127 // 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 {
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}
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.
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:352
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:31
std::optional< uint32_t > file_index() const
Definition DIERef.h:60
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:68
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition DWARFUnit.h:200
DIERef::Section GetDebugSection() const
Definition DWARFUnit.h:222
dw_offset_t GetNextUnitOffset() const
Definition DWARFUnit.h:116
void Append(const NameToDIE &other)
Definition NameToDIE.cpp:91
bool Find(ConstString name, llvm::function_ref< IterationAction(DIERef ref)> callback) const
Definition NameToDIE.cpp:35
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr, const StringTableReader &strtab)
Decode a serialized version of this object from data.
void Insert(ConstString name, const DIERef &die_ref)
Definition NameToDIE.cpp:31
bool operator==(const NameToDIE &rhs) const
Used for unit testing the encoding and decoding.
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const
Encode this object into a data encoder object.
void ForEach(std::function< bool(ConstString name, const DIERef &die_ref)> const &callback) const
Definition NameToDIE.cpp:80
UniqueCStringMap< DIERef > m_map
Definition NameToDIE.h:90
void FindAllEntriesForUnit(DWARFUnit &unit, llvm::function_ref< IterationAction(DIERef ref)> callback) const
unit must be the skeleton unit if possible, not GetNonSkeletonUnit().
Definition NameToDIE.cpp:55
std::optional< uint64_t > GetFileIndex() const
A class that represents a running process on the host machine.
IterationAction
Useful for callbacks whose return type indicates whether to continue iteration or short-circuit.
uint64_t offset_t
Definition lldb-types.h:85