1 //===-- NameToDIE.cpp -------------------------------------------*- C++ -*-===//
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 "lldb/Symbol/ObjectFile.h"
11 #include "lldb/Utility/ConstString.h"
12 #include "lldb/Utility/RegularExpression.h"
13 #include "lldb/Utility/Stream.h"
14 #include "lldb/Utility/StreamString.h"
15 
16 #include "DWARFDebugInfo.h"
17 #include "DWARFDebugInfoEntry.h"
18 #include "SymbolFileDWARF.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 void NameToDIE::Finalize() {
24   m_map.Sort();
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 size_t NameToDIE::Find(ConstString name, DIEArray &info_array) const {
33   return m_map.GetValues(name, info_array);
34 }
35 
36 size_t NameToDIE::Find(const RegularExpression &regex,
37                        DIEArray &info_array) const {
38   return m_map.GetValues(regex, info_array);
39 }
40 
41 size_t NameToDIE::FindAllEntriesForCompileUnit(dw_offset_t cu_offset,
42                                                DIEArray &info_array) const {
43   const size_t initial_size = info_array.size();
44   const uint32_t size = m_map.GetSize();
45   for (uint32_t i = 0; i < size; ++i) {
46     const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
47     if (cu_offset == die_ref.cu_offset)
48       info_array.push_back(die_ref);
49   }
50   return info_array.size() - initial_size;
51 }
52 
53 void NameToDIE::Dump(Stream *s) {
54   const uint32_t size = m_map.GetSize();
55   for (uint32_t i = 0; i < size; ++i) {
56     ConstString cstr = m_map.GetCStringAtIndex(i);
57     const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
58     s->Printf("%p: {0x%8.8x/0x%8.8x} \"%s\"\n", (const void *)cstr.GetCString(),
59               die_ref.cu_offset, die_ref.die_offset, cstr.GetCString());
60   }
61 }
62 
63 void NameToDIE::ForEach(
64     std::function<bool(ConstString name, const DIERef &die_ref)> const
65         &callback) const {
66   const uint32_t size = m_map.GetSize();
67   for (uint32_t i = 0; i < size; ++i) {
68     if (!callback(m_map.GetCStringAtIndexUnchecked(i),
69                   m_map.GetValueAtIndexUnchecked(i)))
70       break;
71   }
72 }
73 
74 void NameToDIE::Append(const NameToDIE &other) {
75   const uint32_t size = other.m_map.GetSize();
76   for (uint32_t i = 0; i < size; ++i) {
77     m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
78                  other.m_map.GetValueAtIndexUnchecked(i));
79   }
80 }
81