1 //===-- SymbolFileDWARF.h --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef SymbolFileDWARF_SymbolFileDWARF_h_
10 #define SymbolFileDWARF_SymbolFileDWARF_h_
16 #include <unordered_map>
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Threading.h"
22 #include "lldb/Core/UniqueCStringMap.h"
23 #include "lldb/Core/dwarf.h"
24 #include "lldb/Expression/DWARFExpression.h"
25 #include "lldb/Symbol/DebugMacros.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Symbol/SymbolFile.h"
28 #include "lldb/Utility/ConstString.h"
29 #include "lldb/Utility/Flags.h"
30 #include "lldb/Utility/RangeMap.h"
31 #include "lldb/lldb-private.h"
33 #include "DWARFContext.h"
34 #include "DWARFDataExtractor.h"
35 #include "DWARFDefines.h"
36 #include "DWARFIndex.h"
37 #include "UniqueDWARFASTType.h"
39 // Forward Declarations for this DWARF plugin
41 class DWARFAbbreviationDeclaration;
42 class DWARFAbbreviationDeclarationSet;
43 class DWARFCompileUnit;
44 class DWARFDebugAbbrev;
45 class DWARFDebugAranges;
47 class DWARFDebugInfoEntry;
49 class DWARFDebugRangesBase;
50 class DWARFDeclContext;
53 class SymbolFileDWARFDebugMap;
54 class SymbolFileDWARFDwo;
55 class SymbolFileDWARFDwp;
57 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
59 class SymbolFileDWARF : public lldb_private::SymbolFile,
60 public lldb_private::UserID {
62 friend class SymbolFileDWARFDebugMap;
63 friend class SymbolFileDWARFDwo;
64 friend class DebugMapModule;
65 friend class DWARFCompileUnit;
66 friend class DWARFDIE;
67 friend class DWARFASTParserClang;
70 static void Initialize();
72 static void Terminate();
74 static void DebuggerInitialize(lldb_private::Debugger &debugger);
76 static lldb_private::ConstString GetPluginNameStatic();
78 static const char *GetPluginDescriptionStatic();
80 static lldb_private::SymbolFile *
81 CreateInstance(lldb_private::ObjectFile *obj_file);
83 static lldb_private::FileSpecList GetSymlinkPaths();
85 // Constructors and Destructors
87 SymbolFileDWARF(lldb_private::ObjectFile *ofile,
88 lldb_private::SectionList *dwo_section_list);
90 ~SymbolFileDWARF() override;
92 uint32_t CalculateAbilities() override;
94 void InitializeObject() override;
96 // Compile Unit function calls
98 uint32_t GetNumCompileUnits() override;
100 lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
103 ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
105 size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
107 bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
109 bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
111 bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
112 lldb_private::FileSpecList &support_files) override;
114 bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override;
116 size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
118 bool ParseImportedModules(
119 const lldb_private::SymbolContext &sc,
120 std::vector<lldb_private::SourceModule> &imported_modules) override;
122 size_t ParseBlocksRecursive(lldb_private::Function &func) override;
125 ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
127 lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
128 llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
129 lldb::user_id_t type_uid,
130 const lldb_private::ExecutionContext *exe_ctx) override;
132 bool CompleteType(lldb_private::CompilerType &compiler_type) override;
134 lldb_private::Type *ResolveType(const DWARFDIE &die,
135 bool assert_not_being_parsed = true,
136 bool resolve_function_context = false);
138 lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
140 lldb_private::CompilerDeclContext
141 GetDeclContextForUID(lldb::user_id_t uid) override;
143 lldb_private::CompilerDeclContext
144 GetDeclContextContainingUID(lldb::user_id_t uid) override;
147 ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
149 uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
150 lldb::SymbolContextItem resolve_scope,
151 lldb_private::SymbolContext &sc) override;
154 ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
156 lldb::SymbolContextItem resolve_scope,
157 lldb_private::SymbolContextList &sc_list) override;
160 FindGlobalVariables(lldb_private::ConstString name,
161 const lldb_private::CompilerDeclContext *parent_decl_ctx,
162 uint32_t max_matches,
163 lldb_private::VariableList &variables) override;
165 uint32_t FindGlobalVariables(const lldb_private::RegularExpression ®ex,
166 uint32_t max_matches,
167 lldb_private::VariableList &variables) override;
170 FindFunctions(lldb_private::ConstString name,
171 const lldb_private::CompilerDeclContext *parent_decl_ctx,
172 lldb::FunctionNameType name_type_mask, bool include_inlines,
173 bool append, lldb_private::SymbolContextList &sc_list) override;
175 uint32_t FindFunctions(const lldb_private::RegularExpression ®ex,
176 bool include_inlines, bool append,
177 lldb_private::SymbolContextList &sc_list) override;
179 void GetMangledNamesForFunction(
180 const std::string &scope_qualified_name,
181 std::vector<lldb_private::ConstString> &mangled_names) override;
184 FindTypes(lldb_private::ConstString name,
185 const lldb_private::CompilerDeclContext *parent_decl_ctx,
186 bool append, uint32_t max_matches,
187 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
188 lldb_private::TypeMap &types) override;
190 size_t FindTypes(const std::vector<lldb_private::CompilerContext> &context,
191 bool append, lldb_private::TypeMap &types) override;
193 lldb_private::TypeList *GetTypeList() override;
195 size_t GetTypes(lldb_private::SymbolContextScope *sc_scope,
196 lldb::TypeClass type_mask,
197 lldb_private::TypeList &type_list) override;
199 lldb_private::TypeSystem *
200 GetTypeSystemForLanguage(lldb::LanguageType language) override;
202 lldb_private::CompilerDeclContext FindNamespace(
203 lldb_private::ConstString name,
204 const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
206 void PreloadSymbols() override;
208 std::recursive_mutex &GetModuleMutex() const override;
210 // PluginInterface protocol
211 lldb_private::ConstString GetPluginName() override;
213 uint32_t GetPluginVersion() override;
215 const lldb_private::DWARFDataExtractor &get_debug_loc_data();
216 const lldb_private::DWARFDataExtractor &get_debug_loclists_data();
218 DWARFDebugAbbrev *DebugAbbrev();
220 const DWARFDebugAbbrev *DebugAbbrev() const;
222 DWARFDebugInfo *DebugInfo();
224 const DWARFDebugInfo *DebugInfo() const;
226 DWARFDebugRangesBase *GetDebugRanges();
227 DWARFDebugRangesBase *GetDebugRngLists();
229 const lldb_private::DWARFDataExtractor &DebugLocData();
231 static bool SupportedVersion(uint16_t version);
234 GetDeclContextDIEContainingDIE(const DWARFDIE &die);
237 HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type);
239 lldb_private::CompileUnit *
240 GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu);
242 virtual size_t GetObjCMethodDIEOffsets(lldb_private::ConstString class_name,
243 DIEArray &method_die_offsets);
245 bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu);
247 lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
249 static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
251 virtual lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
253 virtual lldb_private::DWARFExpression::LocationListFormat
254 GetLocationListFormat() const;
256 lldb::ModuleSP GetDWOModule(lldb_private::ConstString name);
258 typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
259 ExternalTypeModuleMap;
261 /// Return the list of Clang modules imported by this SymbolFile.
262 const ExternalTypeModuleMap& getExternalTypeModules() const {
263 return m_external_type_modules;
266 virtual DWARFDIE GetDIE(const DIERef &die_ref);
268 DWARFDIE GetDIE(lldb::user_id_t uid);
270 lldb::user_id_t GetUID(const DWARFBaseDIE &die) {
271 return GetUID(die.GetDIERef());
274 lldb::user_id_t GetUID(const llvm::Optional<DIERef> &ref) {
275 return ref ? GetUID(*ref) : LLDB_INVALID_UID;
278 lldb::user_id_t GetUID(DIERef ref);
280 virtual std::unique_ptr<SymbolFileDWARFDwo>
281 GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
282 const DWARFDebugInfoEntry &cu_die);
284 // For regular SymbolFileDWARF instances the method returns nullptr,
285 // for the instances of the subclass SymbolFileDWARFDwo
286 // the method returns a pointer to the base compile unit.
287 virtual DWARFCompileUnit *GetBaseCompileUnit() { return nullptr; }
289 virtual llvm::Optional<uint32_t> GetDwoNum() { return llvm::None; }
292 DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
293 const DWARFDIE &die);
295 std::vector<lldb_private::CallEdge>
296 ParseCallEdgesInFunction(UserID func_id) override;
298 void Dump(lldb_private::Stream &s) override;
300 void DumpClangAST(lldb_private::Stream &s) override;
302 lldb_private::DWARFContext &GetDWARFContext() { return m_context; }
304 lldb_private::FileSpec GetFile(DWARFUnit &unit, size_t file_idx);
307 typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
309 typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
311 typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
312 lldb::opaque_compiler_type_t>
314 typedef llvm::DenseMap<lldb::opaque_compiler_type_t, lldb::user_id_t>
317 struct DWARFDataSegment {
318 llvm::once_flag m_flag;
319 lldb_private::DWARFDataExtractor m_data;
322 DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
324 const lldb_private::DWARFDataExtractor &
325 GetCachedSectionData(lldb::SectionType sect_type,
326 DWARFDataSegment &data_segment);
328 virtual void LoadSectionData(lldb::SectionType sect_type,
329 lldb_private::DWARFDataExtractor &data);
331 bool DeclContextMatchesThisSymbolFile(
332 const lldb_private::CompilerDeclContext *decl_ctx);
335 GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
337 DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
339 bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
341 lldb_private::Function *ParseFunction(lldb_private::CompileUnit &comp_unit,
342 const DWARFDIE &die);
344 size_t ParseBlocksRecursive(lldb_private::CompileUnit &comp_unit,
345 lldb_private::Block *parent_block,
347 lldb::addr_t subprogram_low_pc, uint32_t depth);
349 size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
350 bool parse_siblings, bool parse_children);
352 lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
353 const DWARFDIE &die, bool *type_is_new);
355 lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
356 bool assert_not_being_parsed);
358 lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
360 lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
362 const lldb::addr_t func_low_pc);
364 size_t ParseVariables(const lldb_private::SymbolContext &sc,
365 const DWARFDIE &orig_die,
366 const lldb::addr_t func_low_pc, bool parse_siblings,
368 lldb_private::VariableList *cc_variable_list = nullptr);
370 bool ClassOrStructIsVirtual(const DWARFDIE &die);
372 // Given a die_offset, figure out the symbol context representing that die.
373 bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
374 lldb_private::SymbolContextList &sc_list);
377 FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
379 virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
380 const DWARFDIE &die, lldb_private::ConstString type_name,
381 bool must_be_implementation);
383 lldb_private::Symbol *
384 GetObjCClassSymbol(lldb_private::ConstString objc_class_name);
386 lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
387 bool resolve_function_context = false);
389 void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
390 m_debug_map_module_wp = module_sp;
393 SymbolFileDWARFDebugMap *GetDebugMapSymfile();
396 FindBlockContainingSpecification(const DIERef &func_die_ref,
397 dw_offset_t spec_block_die_offset);
400 FindBlockContainingSpecification(const DWARFDIE &die,
401 dw_offset_t spec_block_die_offset);
403 virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
405 bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2);
407 bool ClassContainsSelector(const DWARFDIE &class_die,
408 lldb_private::ConstString selector);
410 bool FixupAddress(lldb_private::Address &addr);
412 typedef std::set<lldb_private::Type *> TypeSet;
414 void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
415 dw_offset_t max_die_offset, uint32_t type_mask,
418 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
419 lldb_private::Variable *>
422 GlobalVariableMap &GetGlobalAranges();
424 void UpdateExternalModuleListIfNeeded();
426 virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
428 virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
430 virtual DIEToClangType &GetForwardDeclDieToClangType() {
431 return m_forward_decl_die_to_clang_type;
434 virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
435 return m_forward_decl_clang_type_to_die;
438 void BuildCuTranslationTable();
439 llvm::Optional<uint32_t> GetDWARFUnitIndex(uint32_t cu_idx);
442 SymbolFileDWARF &dwarf;
445 llvm::Optional<DecodedUID> DecodeUID(lldb::user_id_t uid);
447 SymbolFileDWARFDwp *GetDwpSymbolFile();
449 const lldb_private::FileSpecList &GetTypeUnitSupportFiles(DWARFTypeUnit &tu);
451 lldb::ModuleWP m_debug_map_module_wp;
452 SymbolFileDWARFDebugMap *m_debug_map_symfile;
454 llvm::once_flag m_dwp_symfile_once_flag;
455 std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
457 lldb_private::DWARFContext m_context;
459 DWARFDataSegment m_data_debug_loc;
460 DWARFDataSegment m_data_debug_loclists;
462 // The unique pointer items below are generated on demand if and when someone
463 // accesses them through a non const version of this class.
464 std::unique_ptr<DWARFDebugAbbrev> m_abbr;
465 std::unique_ptr<DWARFDebugInfo> m_info;
466 std::unique_ptr<GlobalVariableMap> m_global_aranges_up;
468 typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
470 DebugMacrosMap m_debug_macros_map;
472 ExternalTypeModuleMap m_external_type_modules;
473 std::unique_ptr<lldb_private::DWARFIndex> m_index;
474 bool m_fetched_external_modules : 1;
475 lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
477 typedef std::set<lldb::user_id_t> DIERefSet;
478 typedef llvm::StringMap<DIERefSet> NameToOffsetMap;
479 NameToOffsetMap m_function_scope_qualified_name_map;
480 std::unique_ptr<DWARFDebugRangesBase> m_ranges;
481 std::unique_ptr<DWARFDebugRangesBase> m_rnglists;
482 UniqueDWARFASTTypeMap m_unique_ast_type_map;
483 DIEToTypePtr m_die_to_type;
484 DIEToVariableSP m_die_to_variable_sp;
485 DIEToClangType m_forward_decl_die_to_clang_type;
486 ClangTypeToDIE m_forward_decl_clang_type_to_die;
487 llvm::DenseMap<dw_offset_t, lldb_private::FileSpecList>
488 m_type_unit_support_files;
489 std::vector<uint32_t> m_lldb_cu_to_dwarf_unit;
492 #endif // SymbolFileDWARF_SymbolFileDWARF_h_