1 //===-- SymbolVendor.mm -----------------------------------------*- 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 #include "lldb/Symbol/SymbolVendor.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Symbol/CompileUnit.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 #include "lldb/Symbol/SymbolFile.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 27 //---------------------------------------------------------------------- 28 // FindPlugin 29 // 30 // Platforms can register a callback to use when creating symbol 31 // vendors to allow for complex debug information file setups, and to 32 // also allow for finding separate debug information files. 33 //---------------------------------------------------------------------- 34 SymbolVendor* 35 SymbolVendor::FindPlugin (const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm) 36 { 37 std::unique_ptr<SymbolVendor> instance_ap; 38 SymbolVendorCreateInstance create_callback; 39 40 for (size_t idx = 0; (create_callback = PluginManager::GetSymbolVendorCreateCallbackAtIndex(idx)) != nullptr; ++idx) 41 { 42 instance_ap.reset(create_callback(module_sp, feedback_strm)); 43 44 if (instance_ap.get()) 45 { 46 return instance_ap.release(); 47 } 48 } 49 // The default implementation just tries to create debug information using the 50 // file representation for the module. 51 instance_ap.reset(new SymbolVendor(module_sp)); 52 if (instance_ap.get()) 53 { 54 ObjectFile *objfile = module_sp->GetObjectFile(); 55 if (objfile) 56 instance_ap->AddSymbolFileRepresentation(objfile->shared_from_this()); 57 } 58 return instance_ap.release(); 59 } 60 61 //---------------------------------------------------------------------- 62 // SymbolVendor constructor 63 //---------------------------------------------------------------------- 64 SymbolVendor::SymbolVendor(const lldb::ModuleSP &module_sp) : 65 ModuleChild (module_sp), 66 m_type_list(), 67 m_compile_units(), 68 m_sym_file_ap() 69 { 70 } 71 72 //---------------------------------------------------------------------- 73 // Destructor 74 //---------------------------------------------------------------------- 75 SymbolVendor::~SymbolVendor() 76 { 77 } 78 79 //---------------------------------------------------------------------- 80 // Add a representation given an object file. 81 //---------------------------------------------------------------------- 82 void 83 SymbolVendor::AddSymbolFileRepresentation(const ObjectFileSP &objfile_sp) 84 { 85 ModuleSP module_sp(GetModule()); 86 if (module_sp) 87 { 88 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 89 if (objfile_sp) 90 { 91 m_objfile_sp = objfile_sp; 92 m_sym_file_ap.reset(SymbolFile::FindPlugin(objfile_sp.get())); 93 } 94 } 95 } 96 97 bool 98 SymbolVendor::SetCompileUnitAtIndex (size_t idx, const CompUnitSP &cu_sp) 99 { 100 ModuleSP module_sp(GetModule()); 101 if (module_sp) 102 { 103 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 104 const size_t num_compile_units = GetNumCompileUnits(); 105 if (idx < num_compile_units) 106 { 107 // Fire off an assertion if this compile unit already exists for now. 108 // The partial parsing should take care of only setting the compile 109 // unit once, so if this assertion fails, we need to make sure that 110 // we don't have a race condition, or have a second parse of the same 111 // compile unit. 112 assert(m_compile_units[idx].get() == nullptr); 113 m_compile_units[idx] = cu_sp; 114 return true; 115 } 116 else 117 { 118 // This should NOT happen, and if it does, we want to crash and know 119 // about it 120 assert (idx < num_compile_units); 121 } 122 } 123 return false; 124 } 125 126 size_t 127 SymbolVendor::GetNumCompileUnits() 128 { 129 ModuleSP module_sp(GetModule()); 130 if (module_sp) 131 { 132 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 133 if (m_compile_units.empty()) 134 { 135 if (m_sym_file_ap.get()) 136 { 137 // Resize our array of compile unit shared pointers -- which will 138 // each remain NULL until someone asks for the actual compile unit 139 // information. When this happens, the symbol file will be asked 140 // to parse this compile unit information. 141 m_compile_units.resize(m_sym_file_ap->GetNumCompileUnits()); 142 } 143 } 144 } 145 return m_compile_units.size(); 146 } 147 148 lldb::LanguageType 149 SymbolVendor::ParseCompileUnitLanguage (const SymbolContext& sc) 150 { 151 ModuleSP module_sp(GetModule()); 152 if (module_sp) 153 { 154 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 155 if (m_sym_file_ap.get()) 156 return m_sym_file_ap->ParseCompileUnitLanguage(sc); 157 } 158 return eLanguageTypeUnknown; 159 } 160 161 162 size_t 163 SymbolVendor::ParseCompileUnitFunctions (const SymbolContext &sc) 164 { 165 ModuleSP module_sp(GetModule()); 166 if (module_sp) 167 { 168 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 169 if (m_sym_file_ap.get()) 170 return m_sym_file_ap->ParseCompileUnitFunctions(sc); 171 } 172 return 0; 173 } 174 175 bool 176 SymbolVendor::ParseCompileUnitLineTable (const SymbolContext &sc) 177 { 178 ModuleSP module_sp(GetModule()); 179 if (module_sp) 180 { 181 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 182 if (m_sym_file_ap.get()) 183 return m_sym_file_ap->ParseCompileUnitLineTable(sc); 184 } 185 return false; 186 } 187 188 bool 189 SymbolVendor::ParseCompileUnitDebugMacros (const SymbolContext &sc) 190 { 191 ModuleSP module_sp(GetModule()); 192 if (module_sp) 193 { 194 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 195 if (m_sym_file_ap.get()) 196 return m_sym_file_ap->ParseCompileUnitDebugMacros(sc); 197 } 198 return false; 199 } 200 bool 201 SymbolVendor::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files) 202 { 203 ModuleSP module_sp(GetModule()); 204 if (module_sp) 205 { 206 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 207 if (m_sym_file_ap.get()) 208 return m_sym_file_ap->ParseCompileUnitSupportFiles(sc, support_files); 209 } 210 return false; 211 } 212 213 bool 214 SymbolVendor::ParseImportedModules (const SymbolContext &sc, 215 std::vector<ConstString> &imported_modules) 216 { 217 ModuleSP module_sp(GetModule()); 218 if (module_sp) 219 { 220 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 221 if (m_sym_file_ap.get()) 222 return m_sym_file_ap->ParseImportedModules(sc, imported_modules); 223 } 224 return false; 225 226 } 227 228 size_t 229 SymbolVendor::ParseFunctionBlocks (const SymbolContext &sc) 230 { 231 ModuleSP module_sp(GetModule()); 232 if (module_sp) 233 { 234 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 235 if (m_sym_file_ap.get()) 236 return m_sym_file_ap->ParseFunctionBlocks(sc); 237 } 238 return 0; 239 } 240 241 size_t 242 SymbolVendor::ParseTypes (const SymbolContext &sc) 243 { 244 ModuleSP module_sp(GetModule()); 245 if (module_sp) 246 { 247 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 248 if (m_sym_file_ap.get()) 249 return m_sym_file_ap->ParseTypes(sc); 250 } 251 return 0; 252 } 253 254 size_t 255 SymbolVendor::ParseVariablesForContext (const SymbolContext& sc) 256 { 257 ModuleSP module_sp(GetModule()); 258 if (module_sp) 259 { 260 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 261 if (m_sym_file_ap.get()) 262 return m_sym_file_ap->ParseVariablesForContext(sc); 263 } 264 return 0; 265 } 266 267 Type* 268 SymbolVendor::ResolveTypeUID(lldb::user_id_t type_uid) 269 { 270 ModuleSP module_sp(GetModule()); 271 if (module_sp) 272 { 273 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 274 if (m_sym_file_ap.get()) 275 return m_sym_file_ap->ResolveTypeUID(type_uid); 276 } 277 return nullptr; 278 } 279 280 281 uint32_t 282 SymbolVendor::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 283 { 284 ModuleSP module_sp(GetModule()); 285 if (module_sp) 286 { 287 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 288 if (m_sym_file_ap.get()) 289 return m_sym_file_ap->ResolveSymbolContext(so_addr, resolve_scope, sc); 290 } 291 return 0; 292 } 293 294 uint32_t 295 SymbolVendor::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 296 { 297 ModuleSP module_sp(GetModule()); 298 if (module_sp) 299 { 300 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 301 if (m_sym_file_ap.get()) 302 return m_sym_file_ap->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list); 303 } 304 return 0; 305 } 306 307 size_t 308 SymbolVendor::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, size_t max_matches, VariableList& variables) 309 { 310 ModuleSP module_sp(GetModule()); 311 if (module_sp) 312 { 313 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 314 if (m_sym_file_ap.get()) 315 return m_sym_file_ap->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables); 316 } 317 return 0; 318 } 319 320 size_t 321 SymbolVendor::FindGlobalVariables (const RegularExpression& regex, bool append, size_t max_matches, VariableList& variables) 322 { 323 ModuleSP module_sp(GetModule()); 324 if (module_sp) 325 { 326 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 327 if (m_sym_file_ap.get()) 328 return m_sym_file_ap->FindGlobalVariables(regex, append, max_matches, variables); 329 } 330 return 0; 331 } 332 333 size_t 334 SymbolVendor::FindFunctions(const ConstString &name, const CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list) 335 { 336 ModuleSP module_sp(GetModule()); 337 if (module_sp) 338 { 339 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 340 if (m_sym_file_ap.get()) 341 return m_sym_file_ap->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list); 342 } 343 return 0; 344 } 345 346 size_t 347 SymbolVendor::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list) 348 { 349 ModuleSP module_sp(GetModule()); 350 if (module_sp) 351 { 352 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 353 if (m_sym_file_ap.get()) 354 return m_sym_file_ap->FindFunctions(regex, include_inlines, append, sc_list); 355 } 356 return 0; 357 } 358 359 360 size_t 361 SymbolVendor::FindTypes (const SymbolContext& sc, const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, size_t max_matches, llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, TypeMap& types) 362 { 363 ModuleSP module_sp(GetModule()); 364 if (module_sp) 365 { 366 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 367 if (m_sym_file_ap.get()) 368 return m_sym_file_ap->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types); 369 } 370 if (!append) 371 types.Clear(); 372 return 0; 373 } 374 375 size_t 376 SymbolVendor::FindTypes (const std::vector<CompilerContext> &context, bool append, TypeMap& types) 377 { 378 ModuleSP module_sp(GetModule()); 379 if (module_sp) 380 { 381 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 382 if (m_sym_file_ap.get()) 383 return m_sym_file_ap->FindTypes(context, append, types); 384 } 385 if (!append) 386 types.Clear(); 387 return 0; 388 } 389 390 size_t 391 SymbolVendor::GetTypes (SymbolContextScope *sc_scope, 392 uint32_t type_mask, 393 lldb_private::TypeList &type_list) 394 { 395 ModuleSP module_sp(GetModule()); 396 if (module_sp) 397 { 398 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 399 if (m_sym_file_ap.get()) 400 return m_sym_file_ap->GetTypes (sc_scope, type_mask, type_list); 401 } 402 return 0; 403 } 404 405 CompilerDeclContext 406 SymbolVendor::FindNamespace(const SymbolContext& sc, const ConstString &name, const CompilerDeclContext *parent_decl_ctx) 407 { 408 CompilerDeclContext namespace_decl_ctx; 409 ModuleSP module_sp(GetModule()); 410 if (module_sp) 411 { 412 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 413 if (m_sym_file_ap.get()) 414 namespace_decl_ctx = m_sym_file_ap->FindNamespace (sc, name, parent_decl_ctx); 415 } 416 return namespace_decl_ctx; 417 } 418 419 void 420 SymbolVendor::Dump(Stream *s) 421 { 422 ModuleSP module_sp(GetModule()); 423 if (module_sp) 424 { 425 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 426 427 bool show_context = false; 428 429 s->Printf("%p: ", static_cast<void*>(this)); 430 s->Indent(); 431 s->PutCString("SymbolVendor"); 432 if (m_sym_file_ap.get()) 433 { 434 ObjectFile *objfile = m_sym_file_ap->GetObjectFile(); 435 if (objfile) 436 { 437 const FileSpec &objfile_file_spec = objfile->GetFileSpec(); 438 if (objfile_file_spec) 439 { 440 s->PutCString(" ("); 441 objfile_file_spec.Dump(s); 442 s->PutChar(')'); 443 } 444 } 445 } 446 s->EOL(); 447 s->IndentMore(); 448 m_type_list.Dump(s, show_context); 449 450 CompileUnitConstIter cu_pos, cu_end; 451 cu_end = m_compile_units.end(); 452 for (cu_pos = m_compile_units.begin(); cu_pos != cu_end; ++cu_pos) 453 { 454 // We currently only dump the compile units that have been parsed 455 if (cu_pos->get()) 456 (*cu_pos)->Dump(s, show_context); 457 } 458 459 s->IndentLess(); 460 } 461 } 462 463 CompUnitSP 464 SymbolVendor::GetCompileUnitAtIndex(size_t idx) 465 { 466 CompUnitSP cu_sp; 467 ModuleSP module_sp(GetModule()); 468 if (module_sp) 469 { 470 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 471 const size_t num_compile_units = GetNumCompileUnits(); 472 if (idx < num_compile_units) 473 { 474 cu_sp = m_compile_units[idx]; 475 if (cu_sp.get() == nullptr) 476 { 477 m_compile_units[idx] = m_sym_file_ap->ParseCompileUnitAtIndex(idx); 478 cu_sp = m_compile_units[idx]; 479 } 480 } 481 } 482 return cu_sp; 483 } 484 485 FileSpec 486 SymbolVendor::GetMainFileSpec() const 487 { 488 if (m_sym_file_ap.get()) 489 { 490 const ObjectFile *symfile_objfile = m_sym_file_ap->GetObjectFile(); 491 if (symfile_objfile) 492 return symfile_objfile->GetFileSpec(); 493 } 494 495 return FileSpec(); 496 } 497 498 Symtab * 499 SymbolVendor::GetSymtab () 500 { 501 ModuleSP module_sp(GetModule()); 502 if (module_sp) 503 { 504 ObjectFile *objfile = module_sp->GetObjectFile(); 505 if (objfile) 506 { 507 // Get symbol table from unified section list. 508 return objfile->GetSymtab (); 509 } 510 } 511 return nullptr; 512 } 513 514 void 515 SymbolVendor::ClearSymtab() 516 { 517 ModuleSP module_sp(GetModule()); 518 if (module_sp) 519 { 520 ObjectFile *objfile = module_sp->GetObjectFile(); 521 if (objfile) 522 { 523 // Clear symbol table from unified section list. 524 objfile->ClearSymtab (); 525 } 526 } 527 } 528 529 void 530 SymbolVendor::SectionFileAddressesChanged () 531 { 532 ModuleSP module_sp(GetModule()); 533 if (module_sp) 534 { 535 ObjectFile *module_objfile = module_sp->GetObjectFile (); 536 if (m_sym_file_ap.get()) 537 { 538 ObjectFile *symfile_objfile = m_sym_file_ap->GetObjectFile (); 539 if (symfile_objfile != module_objfile) 540 symfile_objfile->SectionFileAddressesChanged (); 541 } 542 Symtab *symtab = GetSymtab (); 543 if (symtab) 544 { 545 symtab->SectionFileAddressesChanged (); 546 } 547 } 548 } 549 550 //------------------------------------------------------------------ 551 // PluginInterface protocol 552 //------------------------------------------------------------------ 553 lldb_private::ConstString 554 SymbolVendor::GetPluginName() 555 { 556 static ConstString g_name("vendor-default"); 557 return g_name; 558 } 559 560 uint32_t 561 SymbolVendor::GetPluginVersion() 562 { 563 return 1; 564 } 565 566