[lldb][DWARFUnit] Implement PeekDIEName query (#78486)
[llvm-project.git] / lldb / source / Plugins / SymbolFile / DWARF / SymbolFileDWARFDebugMap.h
blobd639ee500080d52f4fc3dd36a89e2616c52b37f1
1 //===-- SymbolFileDWARFDebugMap.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 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H
12 #include "DIERef.h"
13 #include "lldb/Symbol/SymbolFile.h"
14 #include "lldb/Utility/RangeMap.h"
15 #include "llvm/Support/Chrono.h"
16 #include <bitset>
17 #include <map>
18 #include <optional>
19 #include <vector>
21 #include "UniqueDWARFASTType.h"
22 #include "lldb/Utility/StructuredData.h"
24 class DWARFASTParserClang;
26 namespace lldb_private::plugin {
27 namespace dwarf {
28 class SymbolFileDWARF;
29 class DWARFCompileUnit;
30 class DWARFDebugAranges;
31 class DWARFDeclContext;
33 class SymbolFileDWARFDebugMap : public SymbolFileCommon {
34 /// LLVM RTTI support.
35 static char ID;
37 public:
38 /// LLVM RTTI support.
39 /// \{
40 bool isA(const void *ClassID) const override {
41 return ClassID == &ID || SymbolFileCommon::isA(ClassID);
43 static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
44 /// \}
46 // Static Functions
47 static void Initialize();
49 static void Terminate();
51 static llvm::StringRef GetPluginNameStatic() { return "dwarf-debugmap"; }
53 static llvm::StringRef GetPluginDescriptionStatic();
55 static SymbolFile *CreateInstance(lldb::ObjectFileSP objfile_sp);
57 // Constructors and Destructors
58 SymbolFileDWARFDebugMap(lldb::ObjectFileSP objfile_sp);
59 ~SymbolFileDWARFDebugMap() override;
61 uint32_t CalculateAbilities() override;
62 void InitializeObject() override;
64 // Compile Unit function calls
65 lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override;
66 XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) override;
67 llvm::SmallSet<lldb::LanguageType, 4>
68 ParseAllLanguages(CompileUnit &comp_unit) override;
69 size_t ParseFunctions(CompileUnit &comp_unit) override;
70 bool ParseLineTable(CompileUnit &comp_unit) override;
71 bool ParseDebugMacros(CompileUnit &comp_unit) override;
73 bool ForEachExternalModule(CompileUnit &, llvm::DenseSet<SymbolFile *> &,
74 llvm::function_ref<bool(Module &)>) override;
76 bool ParseSupportFiles(CompileUnit &comp_unit,
77 SupportFileList &support_files) override;
79 bool ParseIsOptimized(CompileUnit &comp_unit) override;
81 size_t ParseTypes(CompileUnit &comp_unit) override;
83 bool
84 ParseImportedModules(const SymbolContext &sc,
85 std::vector<SourceModule> &imported_modules) override;
86 size_t ParseBlocksRecursive(Function &func) override;
87 size_t ParseVariablesForContext(const SymbolContext &sc) override;
89 Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
90 std::optional<ArrayInfo>
91 GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,
92 const ExecutionContext *exe_ctx) override;
94 CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) override;
95 CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
96 std::vector<CompilerContext>
97 GetCompilerContextForUID(lldb::user_id_t uid) override;
98 void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
100 bool CompleteType(CompilerType &compiler_type) override;
101 uint32_t ResolveSymbolContext(const Address &so_addr,
102 lldb::SymbolContextItem resolve_scope,
103 SymbolContext &sc) override;
104 uint32_t ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
105 lldb::SymbolContextItem resolve_scope,
106 SymbolContextList &sc_list) override;
108 Status CalculateFrameVariableError(StackFrame &frame) override;
110 void FindGlobalVariables(ConstString name,
111 const CompilerDeclContext &parent_decl_ctx,
112 uint32_t max_matches,
113 VariableList &variables) override;
114 void FindGlobalVariables(const RegularExpression &regex, uint32_t max_matches,
115 VariableList &variables) override;
116 void FindFunctions(const Module::LookupInfo &lookup_info,
117 const CompilerDeclContext &parent_decl_ctx,
118 bool include_inlines, SymbolContextList &sc_list) override;
119 void FindFunctions(const RegularExpression &regex, bool include_inlines,
120 SymbolContextList &sc_list) override;
121 void FindTypes(const lldb_private::TypeQuery &match,
122 lldb_private::TypeResults &results) override;
123 CompilerDeclContext FindNamespace(ConstString name,
124 const CompilerDeclContext &parent_decl_ctx,
125 bool only_root_namespaces) override;
126 void GetTypes(SymbolContextScope *sc_scope, lldb::TypeClass type_mask,
127 TypeList &type_list) override;
128 std::vector<std::unique_ptr<CallEdge>>
129 ParseCallEdgesInFunction(UserID func_id) override;
131 void DumpClangAST(Stream &s) override;
133 /// List separate oso files.
134 bool GetSeparateDebugInfo(StructuredData::Dictionary &d,
135 bool errors_only) override;
137 // PluginInterface protocol
138 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
140 // Statistics overrides.
141 ModuleList GetDebugInfoModules() override;
143 void
144 GetCompileOptions(std::unordered_map<lldb::CompUnitSP, Args> &args) override;
146 protected:
147 enum { kHaveInitializedOSOs = (1 << 0), kNumFlags };
149 friend class DebugMapModule;
150 friend class ::DWARFASTParserClang;
151 friend class DWARFCompileUnit;
152 friend class SymbolFileDWARF;
153 struct OSOInfo {
154 lldb::ModuleSP module_sp;
156 OSOInfo() : module_sp() {}
159 typedef std::shared_ptr<OSOInfo> OSOInfoSP;
161 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t>
162 FileRangeMap;
164 // Class specific types
165 struct CompileUnitInfo {
166 FileSpec so_file;
167 ConstString oso_path;
168 llvm::sys::TimePoint<> oso_mod_time;
169 Status oso_load_error;
170 OSOInfoSP oso_sp;
171 /// The compile units that an object file contains.
172 llvm::SmallVector<lldb::CompUnitSP, 2> compile_units_sps;
173 /// A map from the compile unit ID to its index in the vector.
174 llvm::SmallDenseMap<uint64_t, uint64_t, 2> id_to_index_map;
175 uint32_t first_symbol_index = UINT32_MAX;
176 uint32_t last_symbol_index = UINT32_MAX;
177 uint32_t first_symbol_id = UINT32_MAX;
178 uint32_t last_symbol_id = UINT32_MAX;
179 FileRangeMap file_range_map;
180 bool file_range_map_valid = false;
182 CompileUnitInfo() = default;
184 const FileRangeMap &GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile);
187 // Protected Member Functions
188 void InitOSO();
190 /// This function actually returns the number of object files, which may be
191 /// less than the actual number of compile units, since an object file may
192 /// contain more than one compile unit. SymbolFileDWARFDebugMap looks up the
193 /// number of compile units by reading the nlist symbol table, which
194 /// currently, on macOS, only reports one compile unit per object file, and
195 /// there's no efficient way to calculate the actual number of compile units
196 /// upfront.
197 uint32_t CalculateNumCompileUnits() override;
199 /// This function actually returns the first compile unit the object file at
200 /// the given index contains.
201 lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
203 static uint32_t GetOSOIndexFromUserID(lldb::user_id_t uid) {
204 std::optional<uint32_t> OsoNum = DIERef(uid).file_index();
205 lldbassert(OsoNum && "Invalid OSO Index");
206 return *OsoNum;
209 static SymbolFileDWARF *GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file);
211 bool GetFileSpecForSO(uint32_t oso_idx, FileSpec &file_spec);
213 CompileUnitInfo *GetCompUnitInfo(const SymbolContext &sc);
214 CompileUnitInfo *GetCompUnitInfo(const CompileUnit &comp_unit);
216 size_t GetCompUnitInfosForModule(const Module *oso_module,
217 std::vector<CompileUnitInfo *> &cu_infos);
219 Module *GetModuleByCompUnitInfo(CompileUnitInfo *comp_unit_info);
221 Module *GetModuleByOSOIndex(uint32_t oso_idx);
223 ObjectFile *GetObjectFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
225 ObjectFile *GetObjectFileByOSOIndex(uint32_t oso_idx);
227 uint32_t GetCompUnitInfoIndex(const CompileUnitInfo *comp_unit_info);
229 SymbolFileDWARF *GetSymbolFile(const SymbolContext &sc);
230 SymbolFileDWARF *GetSymbolFile(const CompileUnit &comp_unit);
232 SymbolFileDWARF *GetSymbolFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
234 SymbolFileDWARF *GetSymbolFileByOSOIndex(uint32_t oso_idx);
236 // If closure returns "false", iteration continues. If it returns
237 // "true", iteration terminates.
238 void ForEachSymbolFile(std::function<bool(SymbolFileDWARF *)> closure) {
239 for (uint32_t oso_idx = 0, num_oso_idxs = m_compile_unit_infos.size();
240 oso_idx < num_oso_idxs; ++oso_idx) {
241 if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) {
242 if (closure(oso_dwarf))
243 return;
248 CompileUnitInfo *GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,
249 uint32_t *oso_idx_ptr);
251 CompileUnitInfo *GetCompileUnitInfoForSymbolWithID(lldb::user_id_t symbol_id,
252 uint32_t *oso_idx_ptr);
254 static int
255 SymbolContainsSymbolWithIndex(uint32_t *symbol_idx_ptr,
256 const CompileUnitInfo *comp_unit_info);
258 static int SymbolContainsSymbolWithID(lldb::user_id_t *symbol_idx_ptr,
259 const CompileUnitInfo *comp_unit_info);
261 void
262 PrivateFindGlobalVariables(ConstString name,
263 const CompilerDeclContext &parent_decl_ctx,
264 const std::vector<uint32_t> &name_symbol_indexes,
265 uint32_t max_matches, VariableList &variables);
267 void SetCompileUnit(SymbolFileDWARF *oso_dwarf,
268 const lldb::CompUnitSP &cu_sp);
270 /// Returns the compile unit associated with the dwarf compile unit. This may
271 /// be one of the extra compile units an object file contains which isn't
272 /// reachable by ParseCompileUnitAtIndex(uint32_t).
273 lldb::CompUnitSP GetCompileUnit(SymbolFileDWARF *oso_dwarf,
274 DWARFCompileUnit &dwarf_cu);
276 CompileUnitInfo *GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf);
278 lldb::TypeSP FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die);
280 bool Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF *skip_dwarf_oso);
282 lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
283 const DWARFDIE &die, ConstString type_name, bool must_be_implementation);
285 UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap() {
286 return m_unique_ast_type_map;
289 // OSOEntry
290 class OSOEntry {
291 public:
292 OSOEntry() = default;
294 OSOEntry(uint32_t exe_sym_idx, lldb::addr_t oso_file_addr)
295 : m_exe_sym_idx(exe_sym_idx), m_oso_file_addr(oso_file_addr) {}
297 uint32_t GetExeSymbolIndex() const { return m_exe_sym_idx; }
299 bool operator<(const OSOEntry &rhs) const {
300 return m_exe_sym_idx < rhs.m_exe_sym_idx;
303 lldb::addr_t GetOSOFileAddress() const { return m_oso_file_addr; }
305 void SetOSOFileAddress(lldb::addr_t oso_file_addr) {
306 m_oso_file_addr = oso_file_addr;
309 protected:
310 uint32_t m_exe_sym_idx = UINT32_MAX;
311 lldb::addr_t m_oso_file_addr = LLDB_INVALID_ADDRESS;
314 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry> DebugMap;
316 // Member Variables
317 std::bitset<kNumFlags> m_flags;
318 std::vector<CompileUnitInfo> m_compile_unit_infos;
319 std::vector<uint32_t> m_func_indexes; // Sorted by address
320 std::vector<uint32_t> m_glob_indexes;
321 std::map<std::pair<ConstString, llvm::sys::TimePoint<>>, OSOInfoSP> m_oso_map;
322 UniqueDWARFASTTypeMap m_unique_ast_type_map;
323 LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
324 DebugMap m_debug_map;
326 // When an object file from the debug map gets parsed in
327 // SymbolFileDWARF, it needs to tell the debug map about the object
328 // files addresses by calling this function once for each N_FUN,
329 // N_GSYM and N_STSYM and after all entries in the debug map have
330 // been matched up, FinalizeOSOFileRanges() should be called.
331 bool AddOSOFileRange(CompileUnitInfo *cu_info, lldb::addr_t exe_file_addr,
332 lldb::addr_t exe_byte_size, lldb::addr_t oso_file_addr,
333 lldb::addr_t oso_byte_size);
335 // Called after calling AddOSOFileRange() for each object file debug
336 // map entry to finalize the info for the unlinked compile unit.
337 void FinalizeOSOFileRanges(CompileUnitInfo *cu_info);
339 /// Convert \a addr from a .o file address, to an executable address.
341 /// \param[in] addr
342 /// A section offset address from a .o file
344 /// \return
345 /// Returns true if \a addr was converted to be an executable
346 /// section/offset address, false otherwise.
347 bool LinkOSOAddress(Address &addr);
349 /// Convert a .o file "file address" to an executable "file address".
351 /// \param[in] oso_symfile
352 /// The DWARF symbol file that contains \a oso_file_addr
354 /// \param[in] oso_file_addr
355 /// A .o file "file address" to convert.
357 /// \return
358 /// LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
359 /// linked executable, otherwise a valid "file address" from the
360 /// linked executable that contains the debug map.
361 lldb::addr_t LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
362 lldb::addr_t oso_file_addr);
364 /// Given a line table full of lines with "file addresses" that are
365 /// for a .o file represented by \a oso_symfile, link a new line table
366 /// and return it.
368 /// \param[in] oso_symfile
369 /// The DWARF symbol file that produced the \a line_table
371 /// \param[in] line_table
372 /// A pointer to the line table.
374 /// \return
375 /// Returns a valid line table full of linked addresses, or NULL
376 /// if none of the line table addresses exist in the main
377 /// executable.
378 LineTable *LinkOSOLineTable(SymbolFileDWARF *oso_symfile,
379 LineTable *line_table);
381 size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data,
382 DWARFDebugAranges *debug_aranges);
384 } // namespace dwarf
385 } // namespace lldb_private::plugin
387 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H