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 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H 10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H 11 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> 20 21 #include "UniqueDWARFASTType.h" 22 #include "lldb/Utility/StructuredData.h" 23 24 class DWARFASTParserClang; 25 26 namespace lldb_private::plugin { 27 namespace dwarf { 28 class SymbolFileDWARF; 29 class DWARFCompileUnit; 30 class DWARFDebugAranges; 31 class DWARFDeclContext; 32 33 class SymbolFileDWARFDebugMap : public SymbolFileCommon { 34 /// LLVM RTTI support. 35 static char ID; 36 37 public: 38 /// LLVM RTTI support. 39 /// \{ 40 bool isA(const void *ClassID) const override { 41 return ClassID == &ID || SymbolFileCommon::isA(ClassID); 42 } 43 static bool classof(const SymbolFile *obj) { return obj->isA(&ID); } 44 /// \} 45 46 // Static Functions 47 static void Initialize(); 48 49 static void Terminate(); 50 51 static llvm::StringRef GetPluginNameStatic() { return "dwarf-debugmap"; } 52 53 static llvm::StringRef GetPluginDescriptionStatic(); 54 55 static SymbolFile *CreateInstance(lldb::ObjectFileSP objfile_sp); 56 57 // Constructors and Destructors 58 SymbolFileDWARFDebugMap(lldb::ObjectFileSP objfile_sp); 59 ~SymbolFileDWARFDebugMap() override; 60 61 uint32_t CalculateAbilities() override; 62 void InitializeObject() override; 63 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; 72 73 bool ForEachExternalModule(CompileUnit &, llvm::DenseSet<SymbolFile *> &, 74 llvm::function_ref<bool(Module &)>) override; 75 76 bool ParseSupportFiles(CompileUnit &comp_unit, 77 SupportFileList &support_files) override; 78 79 bool ParseIsOptimized(CompileUnit &comp_unit) override; 80 81 size_t ParseTypes(CompileUnit &comp_unit) override; 82 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; 88 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; 93 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; 99 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; 107 108 Status CalculateFrameVariableError(StackFrame &frame) override; 109 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 ®ex, 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 ®ex, 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; 130 131 void DumpClangAST(Stream &s) override; 132 133 /// List separate oso files. 134 bool GetSeparateDebugInfo(StructuredData::Dictionary &d, 135 bool errors_only) override; 136 137 // PluginInterface protocol 138 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 139 140 // Statistics overrides. 141 ModuleList GetDebugInfoModules() override; 142 143 void 144 GetCompileOptions(std::unordered_map<lldb::CompUnitSP, Args> &args) override; 145 146 protected: 147 enum { kHaveInitializedOSOs = (1 << 0), kNumFlags }; 148 149 friend class DebugMapModule; 150 friend class ::DWARFASTParserClang; 151 friend class DWARFCompileUnit; 152 friend class SymbolFileDWARF; 153 struct OSOInfo { 154 lldb::ModuleSP module_sp; 155 156 OSOInfo() : module_sp() {} 157 }; 158 159 typedef std::shared_ptr<OSOInfo> OSOInfoSP; 160 161 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t> 162 FileRangeMap; 163 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; 181 182 CompileUnitInfo() = default; 183 184 const FileRangeMap &GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile); 185 }; 186 187 // Protected Member Functions 188 void InitOSO(); 189 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; 198 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; 202 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; 207 } 208 209 static SymbolFileDWARF *GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file); 210 211 bool GetFileSpecForSO(uint32_t oso_idx, FileSpec &file_spec); 212 213 CompileUnitInfo *GetCompUnitInfo(const SymbolContext &sc); 214 CompileUnitInfo *GetCompUnitInfo(const CompileUnit &comp_unit); 215 216 size_t GetCompUnitInfosForModule(const Module *oso_module, 217 std::vector<CompileUnitInfo *> &cu_infos); 218 219 Module *GetModuleByCompUnitInfo(CompileUnitInfo *comp_unit_info); 220 221 Module *GetModuleByOSOIndex(uint32_t oso_idx); 222 223 ObjectFile *GetObjectFileByCompUnitInfo(CompileUnitInfo *comp_unit_info); 224 225 ObjectFile *GetObjectFileByOSOIndex(uint32_t oso_idx); 226 227 uint32_t GetCompUnitInfoIndex(const CompileUnitInfo *comp_unit_info); 228 229 SymbolFileDWARF *GetSymbolFile(const SymbolContext &sc); 230 SymbolFileDWARF *GetSymbolFile(const CompileUnit &comp_unit); 231 232 SymbolFileDWARF *GetSymbolFileByCompUnitInfo(CompileUnitInfo *comp_unit_info); 233 234 SymbolFileDWARF *GetSymbolFileByOSOIndex(uint32_t oso_idx); 235 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; 244 } 245 } 246 } 247 248 CompileUnitInfo *GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx, 249 uint32_t *oso_idx_ptr); 250 251 CompileUnitInfo *GetCompileUnitInfoForSymbolWithID(lldb::user_id_t symbol_id, 252 uint32_t *oso_idx_ptr); 253 254 static int 255 SymbolContainsSymbolWithIndex(uint32_t *symbol_idx_ptr, 256 const CompileUnitInfo *comp_unit_info); 257 258 static int SymbolContainsSymbolWithID(lldb::user_id_t *symbol_idx_ptr, 259 const CompileUnitInfo *comp_unit_info); 260 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); 266 267 void SetCompileUnit(SymbolFileDWARF *oso_dwarf, 268 const lldb::CompUnitSP &cu_sp); 269 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); 275 276 CompileUnitInfo *GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf); 277 278 lldb::TypeSP FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die); 279 280 bool Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF *skip_dwarf_oso); 281 282 lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE( 283 const DWARFDIE &die, ConstString type_name, bool must_be_implementation); 284 285 UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap() { 286 return m_unique_ast_type_map; 287 } 288 289 // OSOEntry 290 class OSOEntry { 291 public: 292 OSOEntry() = default; 293 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) {} 296 297 uint32_t GetExeSymbolIndex() const { return m_exe_sym_idx; } 298 299 bool operator<(const OSOEntry &rhs) const { 300 return m_exe_sym_idx < rhs.m_exe_sym_idx; 301 } 302 303 lldb::addr_t GetOSOFileAddress() const { return m_oso_file_addr; } 304 305 void SetOSOFileAddress(lldb::addr_t oso_file_addr) { 306 m_oso_file_addr = oso_file_addr; 307 } 308 309 protected: 310 uint32_t m_exe_sym_idx = UINT32_MAX; 311 lldb::addr_t m_oso_file_addr = LLDB_INVALID_ADDRESS; 312 }; 313 314 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry> DebugMap; 315 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; 325 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); 334 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); 338 339 /// Convert \a addr from a .o file address, to an executable address. 340 /// 341 /// \param[in] addr 342 /// A section offset address from a .o file 343 /// 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); 348 349 /// Convert a .o file "file address" to an executable "file address". 350 /// 351 /// \param[in] oso_symfile 352 /// The DWARF symbol file that contains \a oso_file_addr 353 /// 354 /// \param[in] oso_file_addr 355 /// A .o file "file address" to convert. 356 /// 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); 363 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. 367 /// 368 /// \param[in] oso_symfile 369 /// The DWARF symbol file that produced the \a line_table 370 /// 371 /// \param[in] line_table 372 /// A pointer to the line table. 373 /// 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); 380 381 size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data, 382 DWARFDebugAranges *debug_aranges); 383 }; 384 } // namespace dwarf 385 } // namespace lldb_private::plugin 386 387 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H 388