xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/NameToDIE.cpp (revision e67e85659c0de33e617e5fbf1028c6e8b49eee53)
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"
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 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 void NameToDIE::Finalize() {
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 
32 bool NameToDIE::Find(ConstString name,
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 
40 bool NameToDIE::Find(const RegularExpression &regex,
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 
50 void NameToDIE::FindAllEntriesForUnit(
51     DWARFUnit &s_unit, llvm::function_ref<bool(DIERef ref)> callback) const {
52   lldbassert(!s_unit.GetSymbolFileDWARF().GetDwoNum());
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 
67 void NameToDIE::Dump(Stream *s) {
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 
75 void NameToDIE::ForEach(
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),
90                  other.m_map.GetValueAtIndexUnchecked(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   for (uint32_t i = 0; i < count; ++i) {
104     llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
105     // No empty strings allowed in the name to DIE maps.
106     if (str.empty())
107       return false;
108     if (llvm::Optional<DIERef> die_ref = DIERef::Decode(data, offset_ptr))
109       m_map.Append(ConstString(str), die_ref.getValue());
110     else
111       return false;
112   }
113   return true;
114 }
115 
116 void NameToDIE::Encode(DataEncoder &encoder, ConstStringTable &strtab) const {
117   encoder.AppendData(kIdentifierNameToDIE);
118   encoder.AppendU32(m_map.GetSize());
119   for (const auto &entry : m_map) {
120     // Make sure there are no empty strings.
121     assert((bool)entry.cstring);
122     encoder.AppendU32(strtab.Add(entry.cstring));
123     entry.value.Encode(encoder);
124   }
125 }
126 
127 bool NameToDIE::operator==(const NameToDIE &rhs) const {
128   const size_t size = m_map.GetSize();
129   if (size != rhs.m_map.GetSize())
130     return false;
131   for (size_t i = 0; i < size; ++i) {
132     if (m_map.GetCStringAtIndex(i) != rhs.m_map.GetCStringAtIndex(i))
133       return false;
134     if (m_map.GetValueRefAtIndexUnchecked(i) !=
135         rhs.m_map.GetValueRefAtIndexUnchecked(i))
136       return false;
137   }
138   return true;
139 }
140