1 //===-- SymbolFileDWARFDebugMap.h ------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_ 11 #define SymbolFileDWARF_SymbolFileDWARFDebugMap_h_ 12 13 #include <bitset> 14 #include <map> 15 #include <vector> 16 17 #include "lldb/Core/RangeMap.h" 18 #include "lldb/Symbol/SymbolFile.h" 19 #include "llvm/Support/Chrono.h" 20 21 #include "UniqueDWARFASTType.h" 22 23 class SymbolFileDWARF; 24 class DWARFDebugAranges; 25 class DWARFDeclContext; 26 27 class SymbolFileDWARFDebugMap : public lldb_private::SymbolFile { 28 public: 29 //------------------------------------------------------------------ 30 // Static Functions 31 //------------------------------------------------------------------ 32 static void Initialize(); 33 34 static void Terminate(); 35 36 static lldb_private::ConstString GetPluginNameStatic(); 37 38 static const char *GetPluginDescriptionStatic(); 39 40 static lldb_private::SymbolFile * 41 CreateInstance(lldb_private::ObjectFile *obj_file); 42 43 //------------------------------------------------------------------ 44 // Constructors and Destructors 45 //------------------------------------------------------------------ 46 SymbolFileDWARFDebugMap(lldb_private::ObjectFile *ofile); 47 ~SymbolFileDWARFDebugMap() override; 48 49 uint32_t CalculateAbilities() override; 50 void InitializeObject() override; 51 52 //------------------------------------------------------------------ 53 // Compile Unit function calls 54 //------------------------------------------------------------------ 55 uint32_t GetNumCompileUnits() override; 56 lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override; 57 58 lldb::LanguageType 59 ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) override; 60 size_t 61 ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc) override; 62 bool 63 ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc) override; 64 bool 65 ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc) override; 66 bool ParseCompileUnitSupportFiles( 67 const lldb_private::SymbolContext &sc, 68 lldb_private::FileSpecList &support_files) override; 69 bool 70 ParseCompileUnitIsOptimized(const lldb_private::SymbolContext &sc) override; 71 bool ParseImportedModules( 72 const lldb_private::SymbolContext &sc, 73 std::vector<lldb_private::ConstString> &imported_modules) override; 74 size_t ParseFunctionBlocks(const lldb_private::SymbolContext &sc) override; 75 size_t ParseTypes(const lldb_private::SymbolContext &sc) override; 76 size_t 77 ParseVariablesForContext(const lldb_private::SymbolContext &sc) override; 78 79 lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override; 80 lldb_private::CompilerDeclContext 81 GetDeclContextForUID(lldb::user_id_t uid) override; 82 lldb_private::CompilerDeclContext 83 GetDeclContextContainingUID(lldb::user_id_t uid) override; 84 void 85 ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override; 86 87 bool CompleteType(lldb_private::CompilerType &compiler_type) override; 88 uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr, 89 uint32_t resolve_scope, 90 lldb_private::SymbolContext &sc) override; 91 uint32_t 92 ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line, 93 bool check_inlines, uint32_t resolve_scope, 94 lldb_private::SymbolContextList &sc_list) override; 95 uint32_t 96 FindGlobalVariables(const lldb_private::ConstString &name, 97 const lldb_private::CompilerDeclContext *parent_decl_ctx, 98 bool append, uint32_t max_matches, 99 lldb_private::VariableList &variables) override; 100 uint32_t FindGlobalVariables(const lldb_private::RegularExpression ®ex, 101 bool append, uint32_t max_matches, 102 lldb_private::VariableList &variables) override; 103 uint32_t 104 FindFunctions(const lldb_private::ConstString &name, 105 const lldb_private::CompilerDeclContext *parent_decl_ctx, 106 uint32_t name_type_mask, bool include_inlines, bool append, 107 lldb_private::SymbolContextList &sc_list) override; 108 uint32_t FindFunctions(const lldb_private::RegularExpression ®ex, 109 bool include_inlines, bool append, 110 lldb_private::SymbolContextList &sc_list) override; 111 uint32_t 112 FindTypes(const lldb_private::SymbolContext &sc, 113 const lldb_private::ConstString &name, 114 const lldb_private::CompilerDeclContext *parent_decl_ctx, 115 bool append, uint32_t max_matches, 116 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 117 lldb_private::TypeMap &types) override; 118 lldb_private::CompilerDeclContext FindNamespace( 119 const lldb_private::SymbolContext &sc, 120 const lldb_private::ConstString &name, 121 const lldb_private::CompilerDeclContext *parent_decl_ctx) override; 122 size_t GetTypes(lldb_private::SymbolContextScope *sc_scope, 123 uint32_t type_mask, 124 lldb_private::TypeList &type_list) override; 125 126 //------------------------------------------------------------------ 127 // PluginInterface protocol 128 //------------------------------------------------------------------ 129 lldb_private::ConstString GetPluginName() override; 130 131 uint32_t GetPluginVersion() override; 132 133 protected: 134 enum { kHaveInitializedOSOs = (1 << 0), kNumFlags }; 135 136 friend class DebugMapModule; 137 friend struct DIERef; 138 friend class DWARFASTParserClang; 139 friend class DWARFCompileUnit; 140 friend class SymbolFileDWARF; 141 struct OSOInfo { 142 lldb::ModuleSP module_sp; 143 144 OSOInfo() : module_sp() {} 145 }; 146 147 typedef std::shared_ptr<OSOInfo> OSOInfoSP; 148 149 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, 150 lldb::addr_t> 151 FileRangeMap; 152 153 //------------------------------------------------------------------ 154 // Class specific types 155 //------------------------------------------------------------------ 156 struct CompileUnitInfo { 157 lldb_private::FileSpec so_file; 158 lldb_private::ConstString oso_path; 159 llvm::sys::TimePoint<> oso_mod_time; 160 OSOInfoSP oso_sp; 161 lldb::CompUnitSP compile_unit_sp; 162 uint32_t first_symbol_index; 163 uint32_t last_symbol_index; 164 uint32_t first_symbol_id; 165 uint32_t last_symbol_id; 166 FileRangeMap file_range_map; 167 bool file_range_map_valid; 168 169 CompileUnitInfo() 170 : so_file(), oso_path(), oso_mod_time(), oso_sp(), compile_unit_sp(), 171 first_symbol_index(UINT32_MAX), last_symbol_index(UINT32_MAX), 172 first_symbol_id(UINT32_MAX), last_symbol_id(UINT32_MAX), 173 file_range_map(), file_range_map_valid(false) {} 174 175 const FileRangeMap &GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile); 176 }; 177 178 //------------------------------------------------------------------ 179 // Protected Member Functions 180 //------------------------------------------------------------------ 181 void InitOSO(); 182 183 static uint32_t GetOSOIndexFromUserID(lldb::user_id_t uid) { 184 return (uint32_t)((uid >> 32ull) - 1ull); 185 } 186 187 static SymbolFileDWARF *GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file); 188 189 bool GetFileSpecForSO(uint32_t oso_idx, lldb_private::FileSpec &file_spec); 190 191 CompileUnitInfo *GetCompUnitInfo(const lldb_private::SymbolContext &sc); 192 193 size_t GetCompUnitInfosForModule(const lldb_private::Module *oso_module, 194 std::vector<CompileUnitInfo *> &cu_infos); 195 196 lldb_private::Module * 197 GetModuleByCompUnitInfo(CompileUnitInfo *comp_unit_info); 198 199 lldb_private::Module *GetModuleByOSOIndex(uint32_t oso_idx); 200 201 lldb_private::ObjectFile * 202 GetObjectFileByCompUnitInfo(CompileUnitInfo *comp_unit_info); 203 204 lldb_private::ObjectFile *GetObjectFileByOSOIndex(uint32_t oso_idx); 205 206 uint32_t GetCompUnitInfoIndex(const CompileUnitInfo *comp_unit_info); 207 208 SymbolFileDWARF *GetSymbolFile(const lldb_private::SymbolContext &sc); 209 210 SymbolFileDWARF *GetSymbolFileByCompUnitInfo(CompileUnitInfo *comp_unit_info); 211 212 SymbolFileDWARF *GetSymbolFileByOSOIndex(uint32_t oso_idx); 213 214 // If closure returns "false", iteration continues. If it returns 215 // "true", iteration terminates. 216 void ForEachSymbolFile(std::function<bool(SymbolFileDWARF *)> closure) { 217 for (uint32_t oso_idx = 0, num_oso_idxs = m_compile_unit_infos.size(); 218 oso_idx < num_oso_idxs; ++oso_idx) { 219 if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) { 220 if (closure(oso_dwarf)) 221 return; 222 } 223 } 224 } 225 226 CompileUnitInfo *GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx, 227 uint32_t *oso_idx_ptr); 228 229 CompileUnitInfo *GetCompileUnitInfoForSymbolWithID(lldb::user_id_t symbol_id, 230 uint32_t *oso_idx_ptr); 231 232 static int 233 SymbolContainsSymbolWithIndex(uint32_t *symbol_idx_ptr, 234 const CompileUnitInfo *comp_unit_info); 235 236 static int SymbolContainsSymbolWithID(lldb::user_id_t *symbol_idx_ptr, 237 const CompileUnitInfo *comp_unit_info); 238 239 uint32_t PrivateFindGlobalVariables( 240 const lldb_private::ConstString &name, 241 const lldb_private::CompilerDeclContext *parent_decl_ctx, 242 const std::vector<uint32_t> &name_symbol_indexes, uint32_t max_matches, 243 lldb_private::VariableList &variables); 244 245 void SetCompileUnit(SymbolFileDWARF *oso_dwarf, 246 const lldb::CompUnitSP &cu_sp); 247 248 lldb::CompUnitSP GetCompileUnit(SymbolFileDWARF *oso_dwarf); 249 250 CompileUnitInfo *GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf); 251 252 lldb::TypeSP 253 FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx); 254 255 bool Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF *skip_dwarf_oso); 256 257 lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE( 258 const DWARFDIE &die, const lldb_private::ConstString &type_name, 259 bool must_be_implementation); 260 261 UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap() { 262 return m_unique_ast_type_map; 263 } 264 265 //------------------------------------------------------------------ 266 // OSOEntry 267 //------------------------------------------------------------------ 268 class OSOEntry { 269 public: 270 OSOEntry() 271 : m_exe_sym_idx(UINT32_MAX), m_oso_file_addr(LLDB_INVALID_ADDRESS) {} 272 273 OSOEntry(uint32_t exe_sym_idx, lldb::addr_t oso_file_addr) 274 : m_exe_sym_idx(exe_sym_idx), m_oso_file_addr(oso_file_addr) {} 275 276 uint32_t GetExeSymbolIndex() const { return m_exe_sym_idx; } 277 278 bool operator<(const OSOEntry &rhs) const { 279 return m_exe_sym_idx < rhs.m_exe_sym_idx; 280 } 281 282 lldb::addr_t GetOSOFileAddress() const { return m_oso_file_addr; } 283 284 void SetOSOFileAddress(lldb::addr_t oso_file_addr) { 285 m_oso_file_addr = oso_file_addr; 286 } 287 288 protected: 289 uint32_t m_exe_sym_idx; 290 lldb::addr_t m_oso_file_addr; 291 }; 292 293 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry> 294 DebugMap; 295 296 //------------------------------------------------------------------ 297 // Member Variables 298 //------------------------------------------------------------------ 299 std::bitset<kNumFlags> m_flags; 300 std::vector<CompileUnitInfo> m_compile_unit_infos; 301 std::vector<uint32_t> m_func_indexes; // Sorted by address 302 std::vector<uint32_t> m_glob_indexes; 303 std::map<lldb_private::ConstString, OSOInfoSP> m_oso_map; 304 UniqueDWARFASTTypeMap m_unique_ast_type_map; 305 lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type; 306 DebugMap m_debug_map; 307 308 //------------------------------------------------------------------ 309 // When an object file from the debug map gets parsed in 310 // SymbolFileDWARF, it needs to tell the debug map about the object 311 // files addresses by calling this function once for each N_FUN, 312 // N_GSYM and N_STSYM and after all entries in the debug map have 313 // been matched up, FinalizeOSOFileRanges() should be called. 314 //------------------------------------------------------------------ 315 bool AddOSOFileRange(CompileUnitInfo *cu_info, lldb::addr_t exe_file_addr, 316 lldb::addr_t exe_byte_size, lldb::addr_t oso_file_addr, 317 lldb::addr_t oso_byte_size); 318 319 //------------------------------------------------------------------ 320 // Called after calling AddOSOFileRange() for each object file debug 321 // map entry to finalize the info for the unlinked compile unit. 322 //------------------------------------------------------------------ 323 void FinalizeOSOFileRanges(CompileUnitInfo *cu_info); 324 325 //------------------------------------------------------------------ 326 /// Convert \a addr from a .o file address, to an executable address. 327 /// 328 /// @param[in] addr 329 /// A section offset address from a .o file 330 /// 331 /// @return 332 /// Returns true if \a addr was converted to be an executable 333 /// section/offset address, false otherwise. 334 //------------------------------------------------------------------ 335 bool LinkOSOAddress(lldb_private::Address &addr); 336 337 //------------------------------------------------------------------ 338 /// Convert a .o file "file address" to an executable "file address". 339 /// 340 /// @param[in] oso_symfile 341 /// The DWARF symbol file that contains \a oso_file_addr 342 /// 343 /// @param[in] oso_file_addr 344 /// A .o file "file address" to convert. 345 /// 346 /// @return 347 /// LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the 348 /// linked executable, otherwise a valid "file address" from the 349 /// linked executable that contains the debug map. 350 //------------------------------------------------------------------ 351 lldb::addr_t LinkOSOFileAddress(SymbolFileDWARF *oso_symfile, 352 lldb::addr_t oso_file_addr); 353 354 //------------------------------------------------------------------ 355 /// Given a line table full of lines with "file addresses" that are 356 /// for a .o file represented by \a oso_symfile, link a new line table 357 /// and return it. 358 /// 359 /// @param[in] oso_symfile 360 /// The DWARF symbol file that produced the \a line_table 361 /// 362 /// @param[in] addr 363 /// A section offset address from a .o file 364 /// 365 /// @return 366 /// Returns a valid line table full of linked addresses, or NULL 367 /// if none of the line table addresses exist in the main 368 /// executable. 369 //------------------------------------------------------------------ 370 lldb_private::LineTable * 371 LinkOSOLineTable(SymbolFileDWARF *oso_symfile, 372 lldb_private::LineTable *line_table); 373 374 size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data, 375 DWARFDebugAranges *debug_aranges); 376 }; 377 378 #endif // #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_ 379