xref: /llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.h (revision cb04d3378096b83e5e357490ff8b1c479f34c469)
1 //===-- DebugNamesDWARFIndex.h ---------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DEBUGNAMESDWARFINDEX_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DEBUGNAMESDWARFINDEX_H
11 
12 #include "Plugins/SymbolFile/DWARF/DWARFIndex.h"
13 #include "Plugins/SymbolFile/DWARF/ManualDWARFIndex.h"
14 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
17 #include <optional>
18 
19 namespace lldb_private::plugin {
20 namespace dwarf {
21 class DebugNamesDWARFIndex : public DWARFIndex {
22 public:
23   static llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>>
24   Create(Module &module, DWARFDataExtractor debug_names,
25          DWARFDataExtractor debug_str, SymbolFileDWARF &dwarf);
26 
27   void Preload() override { m_fallback.Preload(); }
28 
29   void
30   GetGlobalVariables(ConstString basename,
31                      llvm::function_ref<bool(DWARFDIE die)> callback) override;
32   void
33   GetGlobalVariables(const RegularExpression &regex,
34                      llvm::function_ref<bool(DWARFDIE die)> callback) override;
35   void
36   GetGlobalVariables(DWARFUnit &cu,
37                      llvm::function_ref<bool(DWARFDIE die)> callback) override;
38   void
39   GetObjCMethods(ConstString class_name,
40                  llvm::function_ref<bool(DWARFDIE die)> callback) override {}
41   void GetCompleteObjCClass(
42       ConstString class_name, bool must_be_implementation,
43       llvm::function_ref<bool(DWARFDIE die)> callback) override;
44 
45   /// Uses DWARF5's IDX_parent fields, when available, to speed up this query.
46   void GetFullyQualifiedType(
47       const DWARFDeclContext &context,
48       llvm::function_ref<bool(DWARFDIE die)> callback) override;
49   void GetTypes(ConstString name,
50                 llvm::function_ref<bool(DWARFDIE die)> callback) override;
51   void GetTypes(const DWARFDeclContext &context,
52                 llvm::function_ref<bool(DWARFDIE die)> callback) override;
53   void GetNamespaces(ConstString name,
54                      llvm::function_ref<bool(DWARFDIE die)> callback) override;
55   void
56   GetTypesWithQuery(TypeQuery &query,
57                     llvm::function_ref<bool(DWARFDIE die)> callback) override;
58   void GetNamespacesWithParents(
59       ConstString name, const CompilerDeclContext &parent_decl_ctx,
60       llvm::function_ref<bool(DWARFDIE die)> callback) override;
61   void GetFunctions(const Module::LookupInfo &lookup_info,
62                     SymbolFileDWARF &dwarf,
63                     const CompilerDeclContext &parent_decl_ctx,
64                     llvm::function_ref<bool(DWARFDIE die)> callback) override;
65   void GetFunctions(const RegularExpression &regex,
66                     llvm::function_ref<bool(DWARFDIE die)> callback) override;
67 
68   void Dump(Stream &s) override;
69 
70 private:
71   DebugNamesDWARFIndex(Module &module,
72                        std::unique_ptr<llvm::DWARFDebugNames> debug_names_up,
73                        DWARFDataExtractor debug_names_data,
74                        DWARFDataExtractor debug_str_data,
75                        SymbolFileDWARF &dwarf)
76       : DWARFIndex(module), m_debug_info(dwarf.DebugInfo()),
77         m_debug_names_data(debug_names_data), m_debug_str_data(debug_str_data),
78         m_debug_names_up(std::move(debug_names_up)),
79         m_fallback(module, dwarf, GetUnits(*m_debug_names_up),
80                    GetTypeUnitSignatures(*m_debug_names_up)) {}
81 
82   DWARFDebugInfo &m_debug_info;
83 
84   // LLVM DWARFDebugNames will hold a non-owning reference to this data, so keep
85   // track of the ownership here.
86   DWARFDataExtractor m_debug_names_data;
87   DWARFDataExtractor m_debug_str_data;
88 
89   using DebugNames = llvm::DWARFDebugNames;
90   std::unique_ptr<DebugNames> m_debug_names_up;
91   ManualDWARFIndex m_fallback;
92 
93   DWARFUnit *GetNonSkeletonUnit(const DebugNames::Entry &entry) const;
94   DWARFDIE GetDIE(const DebugNames::Entry &entry) const;
95 
96   /// Checks if an entry is a foreign TU and fetch the type unit.
97   ///
98   /// This function checks if the DebugNames::Entry refers to a foreign TU and
99   /// returns an optional with a value of the \a entry is a foreign type unit
100   /// entry. A valid pointer will be returned if this entry is from a .dwo file
101   /// or if it is from a .dwp file and it matches the type unit's originating
102   /// .dwo file by verifying that the DW_TAG_type_unit DIE has a DW_AT_dwo_name
103   /// that matches the DWO name from the originating skeleton compile unit.
104   ///
105   /// \param[in] entry
106   ///   The accelerator table entry to check.
107   ///
108   /// \returns
109   ///   A std::optional that has a value if this entry represents a foreign type
110   ///   unit. If the pointer is valid, then we were able to find and match the
111   ///   entry to the type unit in the .dwo or .dwp file. The returned value can
112   ///   have a valid, yet contain NULL in the following cases:
113   ///   - we were not able to load the .dwo file (missing or DWO ID mismatch)
114   ///   - we were able to load the .dwp file, but the type units DWO name
115   ///     doesn't match the originating skeleton compile unit's entry
116   ///   Returns std::nullopt if this entry is not a foreign type unit entry.
117   std::optional<DWARFTypeUnit *>
118   GetForeignTypeUnit(const DebugNames::Entry &entry) const;
119 
120   bool ProcessEntry(const DebugNames::Entry &entry,
121                     llvm::function_ref<bool(DWARFDIE die)> callback);
122 
123   /// Returns true if `parent_entries` have identical names to `parent_names`.
124   bool SameParentChain(llvm::ArrayRef<llvm::StringRef> parent_names,
125                        llvm::ArrayRef<DebugNames::Entry> parent_entries) const;
126 
127   bool SameParentChain(llvm::ArrayRef<CompilerContext> parent_contexts,
128                        llvm::ArrayRef<DebugNames::Entry> parent_entries) const;
129 
130   /// Returns true if \a parent_contexts entries are within \a parent_chain.
131   /// This is diffferent from SameParentChain() which checks for exact match.
132   /// This function is required because \a parent_chain can contain inline
133   /// namespace entries which may not be specified in \a parent_contexts by
134   /// client.
135   ///
136   /// \param[in] parent_contexts
137   ///   The list of parent contexts to check for.
138   ///
139   /// \param[in] parent_chain
140   ///   The fully qualified parent chain entries from .debug_names index table
141   ///   to check against.
142   ///
143   /// \returns
144   ///   True if all \a parent_contexts entries are can be sequentially found
145   ///   inside
146   ///   \a parent_chain, otherwise False.
147   bool WithinParentChain(llvm::ArrayRef<CompilerContext> parent_contexts,
148                          llvm::ArrayRef<DebugNames::Entry> parent_chain) const;
149 
150   /// Returns true if .debug_names pool entry \p entry matches \p query_context.
151   bool SameAsEntryContext(const CompilerContext &query_context,
152                           const DebugNames::Entry &entry) const;
153 
154   llvm::SmallVector<CompilerContext>
155   GetTypeQueryParentContexts(TypeQuery &query);
156 
157   static void MaybeLogLookupError(llvm::Error error,
158                                   const DebugNames::NameIndex &ni,
159                                   llvm::StringRef name);
160 
161   static llvm::DenseSet<dw_offset_t> GetUnits(const DebugNames &debug_names);
162   static llvm::DenseSet<uint64_t>
163   GetTypeUnitSignatures(const DebugNames &debug_names);
164 };
165 
166 } // namespace dwarf
167 } // namespace lldb_private::plugin
168 
169 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DEBUGNAMESDWARFINDEX_H
170