1 //===-- Module.cpp ----------------------------------------------*- 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/Core/Module.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 #include "llvm/Support/raw_os_ostream.h" 16 #include "llvm/Support/Signals.h" 17 18 // Project includes 19 #include "lldb/Core/AddressResolverFileLine.h" 20 #include "lldb/Core/Error.h" 21 #include "lldb/Core/DataBuffer.h" 22 #include "lldb/Core/DataBufferHeap.h" 23 #include "lldb/Core/Log.h" 24 #include "lldb/Core/ModuleList.h" 25 #include "lldb/Core/ModuleSpec.h" 26 #include "lldb/Core/PluginManager.h" 27 #include "lldb/Core/RegularExpression.h" 28 #include "lldb/Core/Section.h" 29 #include "lldb/Core/StreamString.h" 30 #include "lldb/Core/Timer.h" 31 #include "lldb/Host/Host.h" 32 #include "lldb/Host/Symbols.h" 33 #include "lldb/Interpreter/CommandInterpreter.h" 34 #include "lldb/Interpreter/ScriptInterpreter.h" 35 #include "lldb/Symbol/CompileUnit.h" 36 #include "lldb/Symbol/ObjectFile.h" 37 #include "lldb/Symbol/SymbolContext.h" 38 #include "lldb/Symbol/SymbolFile.h" 39 #include "lldb/Symbol/SymbolVendor.h" 40 #include "lldb/Symbol/TypeSystem.h" 41 #include "lldb/Target/Language.h" 42 #include "lldb/Target/Process.h" 43 #include "lldb/Target/SectionLoadList.h" 44 #include "lldb/Target/Target.h" 45 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 46 #include "Plugins/Language/ObjC/ObjCLanguage.h" 47 #include "lldb/Symbol/TypeMap.h" 48 49 #include "Plugins/ObjectFile/JIT/ObjectFileJIT.h" 50 51 using namespace lldb; 52 using namespace lldb_private; 53 54 // Shared pointers to modules track module lifetimes in 55 // targets and in the global module, but this collection 56 // will track all module objects that are still alive 57 typedef std::vector<Module *> ModuleCollection; 58 59 static ModuleCollection & 60 GetModuleCollection() 61 { 62 // This module collection needs to live past any module, so we could either make it a 63 // shared pointer in each module or just leak is. Since it is only an empty vector by 64 // the time all the modules have gone away, we just leak it for now. If we decide this 65 // is a big problem we can introduce a Finalize method that will tear everything down in 66 // a predictable order. 67 68 static ModuleCollection *g_module_collection = nullptr; 69 if (g_module_collection == nullptr) 70 g_module_collection = new ModuleCollection(); 71 72 return *g_module_collection; 73 } 74 75 Mutex * 76 Module::GetAllocationModuleCollectionMutex() 77 { 78 // NOTE: The mutex below must be leaked since the global module list in 79 // the ModuleList class will get torn at some point, and we can't know 80 // if it will tear itself down before the "g_module_collection_mutex" below 81 // will. So we leak a Mutex object below to safeguard against that 82 83 static Mutex *g_module_collection_mutex = nullptr; 84 if (g_module_collection_mutex == nullptr) 85 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak 86 return g_module_collection_mutex; 87 } 88 89 size_t 90 Module::GetNumberAllocatedModules () 91 { 92 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 93 return GetModuleCollection().size(); 94 } 95 96 Module * 97 Module::GetAllocatedModuleAtIndex (size_t idx) 98 { 99 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 100 ModuleCollection &modules = GetModuleCollection(); 101 if (idx < modules.size()) 102 return modules[idx]; 103 return nullptr; 104 } 105 106 #if 0 107 // These functions help us to determine if modules are still loaded, yet don't require that 108 // you have a command interpreter and can easily be called from an external debugger. 109 namespace lldb { 110 111 void 112 ClearModuleInfo (void) 113 { 114 const bool mandatory = true; 115 ModuleList::RemoveOrphanSharedModules(mandatory); 116 } 117 118 void 119 DumpModuleInfo (void) 120 { 121 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex()); 122 ModuleCollection &modules = GetModuleCollection(); 123 const size_t count = modules.size(); 124 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count); 125 for (size_t i = 0; i < count; ++i) 126 { 127 128 StreamString strm; 129 Module *module = modules[i]; 130 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module); 131 module->GetDescription(&strm, eDescriptionLevelFull); 132 printf ("%p: shared = %i, ref_count = %3u, module = %s\n", 133 module, 134 in_shared_module_list, 135 (uint32_t)module->use_count(), 136 strm.GetString().c_str()); 137 } 138 } 139 } 140 141 #endif 142 143 Module::Module (const ModuleSpec &module_spec) : 144 m_mutex (Mutex::eMutexTypeRecursive), 145 m_mod_time (), 146 m_arch (), 147 m_uuid (), 148 m_file (), 149 m_platform_file(), 150 m_remote_install_file(), 151 m_symfile_spec (), 152 m_object_name (), 153 m_object_offset (), 154 m_object_mod_time (), 155 m_objfile_sp (), 156 m_symfile_ap (), 157 m_type_system_map(), 158 m_source_mappings (), 159 m_sections_ap(), 160 m_did_load_objfile (false), 161 m_did_load_symbol_vendor (false), 162 m_did_parse_uuid (false), 163 m_file_has_changed (false), 164 m_first_file_changed_log (false) 165 { 166 // Scope for locker below... 167 { 168 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 169 GetModuleCollection().push_back(this); 170 } 171 172 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 173 if (log != nullptr) 174 log->Printf ("%p Module::Module((%s) '%s%s%s%s')", 175 static_cast<void*>(this), 176 module_spec.GetArchitecture().GetArchitectureName(), 177 module_spec.GetFileSpec().GetPath().c_str(), 178 module_spec.GetObjectName().IsEmpty() ? "" : "(", 179 module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""), 180 module_spec.GetObjectName().IsEmpty() ? "" : ")"); 181 182 // First extract all module specifications from the file using the local 183 // file path. If there are no specifications, then don't fill anything in 184 ModuleSpecList modules_specs; 185 if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0) 186 return; 187 188 // Now make sure that one of the module specifications matches what we just 189 // extract. We might have a module specification that specifies a file "/usr/lib/dyld" 190 // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has 191 // UUID YYY and we don't want those to match. If they don't match, just don't 192 // fill any ivars in so we don't accidentally grab the wrong file later since 193 // they don't match... 194 ModuleSpec matching_module_spec; 195 if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0) 196 return; 197 198 if (module_spec.GetFileSpec()) 199 m_mod_time = module_spec.GetFileSpec().GetModificationTime(); 200 else if (matching_module_spec.GetFileSpec()) 201 m_mod_time = matching_module_spec.GetFileSpec().GetModificationTime(); 202 203 // Copy the architecture from the actual spec if we got one back, else use the one that was specified 204 if (matching_module_spec.GetArchitecture().IsValid()) 205 m_arch = matching_module_spec.GetArchitecture(); 206 else if (module_spec.GetArchitecture().IsValid()) 207 m_arch = module_spec.GetArchitecture(); 208 209 // Copy the file spec over and use the specified one (if there was one) so we 210 // don't use a path that might have gotten resolved a path in 'matching_module_spec' 211 if (module_spec.GetFileSpec()) 212 m_file = module_spec.GetFileSpec(); 213 else if (matching_module_spec.GetFileSpec()) 214 m_file = matching_module_spec.GetFileSpec(); 215 216 // Copy the platform file spec over 217 if (module_spec.GetPlatformFileSpec()) 218 m_platform_file = module_spec.GetPlatformFileSpec(); 219 else if (matching_module_spec.GetPlatformFileSpec()) 220 m_platform_file = matching_module_spec.GetPlatformFileSpec(); 221 222 // Copy the symbol file spec over 223 if (module_spec.GetSymbolFileSpec()) 224 m_symfile_spec = module_spec.GetSymbolFileSpec(); 225 else if (matching_module_spec.GetSymbolFileSpec()) 226 m_symfile_spec = matching_module_spec.GetSymbolFileSpec(); 227 228 // Copy the object name over 229 if (matching_module_spec.GetObjectName()) 230 m_object_name = matching_module_spec.GetObjectName(); 231 else 232 m_object_name = module_spec.GetObjectName(); 233 234 // Always trust the object offset (file offset) and object modification 235 // time (for mod time in a BSD static archive) of from the matching 236 // module specification 237 m_object_offset = matching_module_spec.GetObjectOffset(); 238 m_object_mod_time = matching_module_spec.GetObjectModificationTime(); 239 240 } 241 242 Module::Module(const FileSpec& file_spec, 243 const ArchSpec& arch, 244 const ConstString *object_name, 245 lldb::offset_t object_offset, 246 const TimeValue *object_mod_time_ptr) : 247 m_mutex (Mutex::eMutexTypeRecursive), 248 m_mod_time (file_spec.GetModificationTime()), 249 m_arch (arch), 250 m_uuid (), 251 m_file (file_spec), 252 m_platform_file(), 253 m_remote_install_file (), 254 m_symfile_spec (), 255 m_object_name (), 256 m_object_offset (object_offset), 257 m_object_mod_time (), 258 m_objfile_sp (), 259 m_symfile_ap (), 260 m_type_system_map(), 261 m_source_mappings (), 262 m_sections_ap(), 263 m_did_load_objfile (false), 264 m_did_load_symbol_vendor (false), 265 m_did_parse_uuid (false), 266 m_file_has_changed (false), 267 m_first_file_changed_log (false) 268 { 269 // Scope for locker below... 270 { 271 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 272 GetModuleCollection().push_back(this); 273 } 274 275 if (object_name) 276 m_object_name = *object_name; 277 278 if (object_mod_time_ptr) 279 m_object_mod_time = *object_mod_time_ptr; 280 281 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 282 if (log != nullptr) 283 log->Printf ("%p Module::Module((%s) '%s%s%s%s')", 284 static_cast<void*>(this), m_arch.GetArchitectureName(), 285 m_file.GetPath().c_str(), 286 m_object_name.IsEmpty() ? "" : "(", 287 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 288 m_object_name.IsEmpty() ? "" : ")"); 289 } 290 291 Module::Module () : 292 m_mutex (Mutex::eMutexTypeRecursive), 293 m_mod_time (), 294 m_arch (), 295 m_uuid (), 296 m_file (), 297 m_platform_file(), 298 m_remote_install_file (), 299 m_symfile_spec (), 300 m_object_name (), 301 m_object_offset (0), 302 m_object_mod_time (), 303 m_objfile_sp (), 304 m_symfile_ap (), 305 m_type_system_map(), 306 m_source_mappings (), 307 m_sections_ap(), 308 m_did_load_objfile (false), 309 m_did_load_symbol_vendor (false), 310 m_did_parse_uuid (false), 311 m_file_has_changed (false), 312 m_first_file_changed_log (false) 313 { 314 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 315 GetModuleCollection().push_back(this); 316 } 317 318 Module::~Module() 319 { 320 // Lock our module down while we tear everything down to make sure 321 // we don't get any access to the module while it is being destroyed 322 Mutex::Locker locker (m_mutex); 323 // Scope for locker below... 324 { 325 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 326 ModuleCollection &modules = GetModuleCollection(); 327 ModuleCollection::iterator end = modules.end(); 328 ModuleCollection::iterator pos = std::find(modules.begin(), end, this); 329 assert (pos != end); 330 modules.erase(pos); 331 } 332 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 333 if (log != nullptr) 334 log->Printf ("%p Module::~Module((%s) '%s%s%s%s')", 335 static_cast<void*>(this), 336 m_arch.GetArchitectureName(), 337 m_file.GetPath().c_str(), 338 m_object_name.IsEmpty() ? "" : "(", 339 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 340 m_object_name.IsEmpty() ? "" : ")"); 341 // Release any auto pointers before we start tearing down our member 342 // variables since the object file and symbol files might need to make 343 // function calls back into this module object. The ordering is important 344 // here because symbol files can require the module object file. So we tear 345 // down the symbol file first, then the object file. 346 m_sections_ap.reset(); 347 m_symfile_ap.reset(); 348 m_objfile_sp.reset(); 349 } 350 351 ObjectFile * 352 Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error, size_t size_to_read) 353 { 354 if (m_objfile_sp) 355 { 356 error.SetErrorString ("object file already exists"); 357 } 358 else 359 { 360 Mutex::Locker locker (m_mutex); 361 if (process_sp) 362 { 363 m_did_load_objfile = true; 364 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0)); 365 Error readmem_error; 366 const size_t bytes_read = process_sp->ReadMemory (header_addr, 367 data_ap->GetBytes(), 368 data_ap->GetByteSize(), 369 readmem_error); 370 if (bytes_read == size_to_read) 371 { 372 DataBufferSP data_sp(data_ap.release()); 373 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp); 374 if (m_objfile_sp) 375 { 376 StreamString s; 377 s.Printf("0x%16.16" PRIx64, header_addr); 378 m_object_name.SetCString (s.GetData()); 379 380 // Once we get the object file, update our module with the object file's 381 // architecture since it might differ in vendor/os if some parts were 382 // unknown. 383 m_objfile_sp->GetArchitecture (m_arch); 384 } 385 else 386 { 387 error.SetErrorString ("unable to find suitable object file plug-in"); 388 } 389 } 390 else 391 { 392 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString()); 393 } 394 } 395 else 396 { 397 error.SetErrorString ("invalid process"); 398 } 399 } 400 return m_objfile_sp.get(); 401 } 402 403 const lldb_private::UUID& 404 Module::GetUUID() 405 { 406 if (!m_did_parse_uuid.load()) 407 { 408 Mutex::Locker locker (m_mutex); 409 if (!m_did_parse_uuid.load()) 410 { 411 ObjectFile * obj_file = GetObjectFile (); 412 413 if (obj_file != nullptr) 414 { 415 obj_file->GetUUID(&m_uuid); 416 m_did_parse_uuid = true; 417 } 418 } 419 } 420 return m_uuid; 421 } 422 423 TypeSystem * 424 Module::GetTypeSystemForLanguage (LanguageType language) 425 { 426 return m_type_system_map.GetTypeSystemForLanguage(language, this, true); 427 } 428 429 void 430 Module::ParseAllDebugSymbols() 431 { 432 Mutex::Locker locker (m_mutex); 433 size_t num_comp_units = GetNumCompileUnits(); 434 if (num_comp_units == 0) 435 return; 436 437 SymbolContext sc; 438 sc.module_sp = shared_from_this(); 439 SymbolVendor *symbols = GetSymbolVendor (); 440 441 for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) 442 { 443 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); 444 if (sc.comp_unit) 445 { 446 sc.function = nullptr; 447 symbols->ParseVariablesForContext(sc); 448 449 symbols->ParseCompileUnitFunctions(sc); 450 451 for (size_t func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != nullptr; ++func_idx) 452 { 453 symbols->ParseFunctionBlocks(sc); 454 455 // Parse the variables for this function and all its blocks 456 symbols->ParseVariablesForContext(sc); 457 } 458 459 // Parse all types for this compile unit 460 sc.function = nullptr; 461 symbols->ParseTypes(sc); 462 } 463 } 464 } 465 466 void 467 Module::CalculateSymbolContext(SymbolContext* sc) 468 { 469 sc->module_sp = shared_from_this(); 470 } 471 472 ModuleSP 473 Module::CalculateSymbolContextModule () 474 { 475 return shared_from_this(); 476 } 477 478 void 479 Module::DumpSymbolContext(Stream *s) 480 { 481 s->Printf(", Module{%p}", static_cast<void*>(this)); 482 } 483 484 size_t 485 Module::GetNumCompileUnits() 486 { 487 Mutex::Locker locker (m_mutex); 488 Timer scoped_timer(__PRETTY_FUNCTION__, 489 "Module::GetNumCompileUnits (module = %p)", 490 static_cast<void*>(this)); 491 SymbolVendor *symbols = GetSymbolVendor (); 492 if (symbols) 493 return symbols->GetNumCompileUnits(); 494 return 0; 495 } 496 497 CompUnitSP 498 Module::GetCompileUnitAtIndex (size_t index) 499 { 500 Mutex::Locker locker (m_mutex); 501 size_t num_comp_units = GetNumCompileUnits (); 502 CompUnitSP cu_sp; 503 504 if (index < num_comp_units) 505 { 506 SymbolVendor *symbols = GetSymbolVendor (); 507 if (symbols) 508 cu_sp = symbols->GetCompileUnitAtIndex(index); 509 } 510 return cu_sp; 511 } 512 513 bool 514 Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) 515 { 516 Mutex::Locker locker (m_mutex); 517 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr); 518 SectionList *section_list = GetSectionList(); 519 if (section_list) 520 return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list); 521 return false; 522 } 523 524 uint32_t 525 Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc, 526 bool resolve_tail_call_address) 527 { 528 Mutex::Locker locker (m_mutex); 529 uint32_t resolved_flags = 0; 530 531 // Clear the result symbol context in case we don't find anything, but don't clear the target 532 sc.Clear(false); 533 534 // Get the section from the section/offset address. 535 SectionSP section_sp (so_addr.GetSection()); 536 537 // Make sure the section matches this module before we try and match anything 538 if (section_sp && section_sp->GetModule().get() == this) 539 { 540 // If the section offset based address resolved itself, then this 541 // is the right module. 542 sc.module_sp = shared_from_this(); 543 resolved_flags |= eSymbolContextModule; 544 545 SymbolVendor* sym_vendor = GetSymbolVendor(); 546 if (!sym_vendor) 547 return resolved_flags; 548 549 // Resolve the compile unit, function, block, line table or line 550 // entry if requested. 551 if (resolve_scope & eSymbolContextCompUnit || 552 resolve_scope & eSymbolContextFunction || 553 resolve_scope & eSymbolContextBlock || 554 resolve_scope & eSymbolContextLineEntry ) 555 { 556 resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc); 557 } 558 559 // Resolve the symbol if requested, but don't re-look it up if we've already found it. 560 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol)) 561 { 562 Symtab *symtab = sym_vendor->GetSymtab(); 563 if (symtab && so_addr.IsSectionOffset()) 564 { 565 Symbol *matching_symbol = nullptr; 566 567 symtab->ForEachSymbolContainingFileAddress(so_addr.GetFileAddress(), 568 [&matching_symbol](Symbol *symbol) -> bool { 569 if (symbol->GetType() != eSymbolTypeInvalid) 570 { 571 matching_symbol = symbol; 572 return false; // Stop iterating 573 } 574 return true; // Keep iterating 575 }); 576 sc.symbol = matching_symbol; 577 if (!sc.symbol && 578 resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction)) 579 { 580 bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address. 581 if (ObjectFile *obj_file = sc.module_sp->GetObjectFile()) 582 sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique); 583 } 584 585 if (sc.symbol) 586 { 587 if (sc.symbol->IsSynthetic()) 588 { 589 // We have a synthetic symbol so lets check if the object file 590 // from the symbol file in the symbol vendor is different than 591 // the object file for the module, and if so search its symbol 592 // table to see if we can come up with a better symbol. For example 593 // dSYM files on MacOSX have an unstripped symbol table inside of 594 // them. 595 ObjectFile *symtab_objfile = symtab->GetObjectFile(); 596 if (symtab_objfile && symtab_objfile->IsStripped()) 597 { 598 SymbolFile *symfile = sym_vendor->GetSymbolFile(); 599 if (symfile) 600 { 601 ObjectFile *symfile_objfile = symfile->GetObjectFile(); 602 if (symfile_objfile != symtab_objfile) 603 { 604 Symtab *symfile_symtab = symfile_objfile->GetSymtab(); 605 if (symfile_symtab) 606 { 607 Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress()); 608 if (symbol && !symbol->IsSynthetic()) 609 { 610 sc.symbol = symbol; 611 } 612 } 613 } 614 } 615 } 616 } 617 resolved_flags |= eSymbolContextSymbol; 618 } 619 } 620 } 621 622 // For function symbols, so_addr may be off by one. This is a convention consistent 623 // with FDE row indices in eh_frame sections, but requires extra logic here to permit 624 // symbol lookup for disassembly and unwind. 625 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) && 626 resolve_tail_call_address && so_addr.IsSectionOffset()) 627 { 628 Address previous_addr = so_addr; 629 previous_addr.Slide(-1); 630 631 bool do_resolve_tail_call_address = false; // prevent recursion 632 const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc, 633 do_resolve_tail_call_address); 634 if (flags & eSymbolContextSymbol) 635 { 636 AddressRange addr_range; 637 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range)) 638 { 639 if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection()) 640 { 641 // If the requested address is one past the address range of a function (i.e. a tail call), 642 // or the decremented address is the start of a function (i.e. some forms of trampoline), 643 // indicate that the symbol has been resolved. 644 if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() || 645 so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize()) 646 { 647 resolved_flags |= flags; 648 } 649 } 650 else 651 { 652 sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match. 653 } 654 } 655 } 656 } 657 } 658 return resolved_flags; 659 } 660 661 uint32_t 662 Module::ResolveSymbolContextForFilePath 663 ( 664 const char *file_path, 665 uint32_t line, 666 bool check_inlines, 667 uint32_t resolve_scope, 668 SymbolContextList& sc_list 669 ) 670 { 671 FileSpec file_spec(file_path, false); 672 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 673 } 674 675 uint32_t 676 Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 677 { 678 Mutex::Locker locker (m_mutex); 679 Timer scoped_timer(__PRETTY_FUNCTION__, 680 "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)", 681 file_spec.GetPath().c_str(), 682 line, 683 check_inlines ? "yes" : "no", 684 resolve_scope); 685 686 const uint32_t initial_count = sc_list.GetSize(); 687 688 SymbolVendor *symbols = GetSymbolVendor (); 689 if (symbols) 690 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list); 691 692 return sc_list.GetSize() - initial_count; 693 } 694 695 size_t 696 Module::FindGlobalVariables (const ConstString &name, 697 const CompilerDeclContext *parent_decl_ctx, 698 bool append, 699 size_t max_matches, 700 VariableList& variables) 701 { 702 SymbolVendor *symbols = GetSymbolVendor (); 703 if (symbols) 704 return symbols->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables); 705 return 0; 706 } 707 708 size_t 709 Module::FindGlobalVariables (const RegularExpression& regex, 710 bool append, 711 size_t max_matches, 712 VariableList& variables) 713 { 714 SymbolVendor *symbols = GetSymbolVendor (); 715 if (symbols) 716 return symbols->FindGlobalVariables(regex, append, max_matches, variables); 717 return 0; 718 } 719 720 size_t 721 Module::FindCompileUnits (const FileSpec &path, 722 bool append, 723 SymbolContextList &sc_list) 724 { 725 if (!append) 726 sc_list.Clear(); 727 728 const size_t start_size = sc_list.GetSize(); 729 const size_t num_compile_units = GetNumCompileUnits(); 730 SymbolContext sc; 731 sc.module_sp = shared_from_this(); 732 const bool compare_directory = (bool)path.GetDirectory(); 733 for (size_t i = 0; i < num_compile_units; ++i) 734 { 735 sc.comp_unit = GetCompileUnitAtIndex(i).get(); 736 if (sc.comp_unit) 737 { 738 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory)) 739 sc_list.Append(sc); 740 } 741 } 742 return sc_list.GetSize() - start_size; 743 } 744 745 size_t 746 Module::FindFunctions (const ConstString &name, 747 const CompilerDeclContext *parent_decl_ctx, 748 uint32_t name_type_mask, 749 bool include_symbols, 750 bool include_inlines, 751 bool append, 752 SymbolContextList& sc_list) 753 { 754 if (!append) 755 sc_list.Clear(); 756 757 const size_t old_size = sc_list.GetSize(); 758 759 // Find all the functions (not symbols, but debug information functions... 760 SymbolVendor *symbols = GetSymbolVendor (); 761 762 if (name_type_mask & eFunctionNameTypeAuto) 763 { 764 ConstString lookup_name; 765 uint32_t lookup_name_type_mask = 0; 766 bool match_name_after_lookup = false; 767 Module::PrepareForFunctionNameLookup (name, 768 name_type_mask, 769 eLanguageTypeUnknown, // TODO: add support 770 lookup_name, 771 lookup_name_type_mask, 772 match_name_after_lookup); 773 774 if (symbols) 775 { 776 symbols->FindFunctions(lookup_name, 777 parent_decl_ctx, 778 lookup_name_type_mask, 779 include_inlines, 780 append, 781 sc_list); 782 783 // Now check our symbol table for symbols that are code symbols if requested 784 if (include_symbols) 785 { 786 Symtab *symtab = symbols->GetSymtab(); 787 if (symtab) 788 symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list); 789 } 790 } 791 792 if (match_name_after_lookup) 793 { 794 SymbolContext sc; 795 size_t i = old_size; 796 while (i < sc_list.GetSize()) 797 { 798 if (sc_list.GetContextAtIndex(i, sc)) 799 { 800 const char *func_name = sc.GetFunctionName().GetCString(); 801 if (func_name && strstr(func_name, name.GetCString()) == nullptr) 802 { 803 // Remove the current context 804 sc_list.RemoveContextAtIndex(i); 805 // Don't increment i and continue in the loop 806 continue; 807 } 808 } 809 ++i; 810 } 811 } 812 } 813 else 814 { 815 if (symbols) 816 { 817 symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list); 818 819 // Now check our symbol table for symbols that are code symbols if requested 820 if (include_symbols) 821 { 822 Symtab *symtab = symbols->GetSymtab(); 823 if (symtab) 824 symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 825 } 826 } 827 } 828 829 return sc_list.GetSize() - old_size; 830 } 831 832 size_t 833 Module::FindFunctions (const RegularExpression& regex, 834 bool include_symbols, 835 bool include_inlines, 836 bool append, 837 SymbolContextList& sc_list) 838 { 839 if (!append) 840 sc_list.Clear(); 841 842 const size_t start_size = sc_list.GetSize(); 843 844 SymbolVendor *symbols = GetSymbolVendor (); 845 if (symbols) 846 { 847 symbols->FindFunctions(regex, include_inlines, append, sc_list); 848 849 // Now check our symbol table for symbols that are code symbols if requested 850 if (include_symbols) 851 { 852 Symtab *symtab = symbols->GetSymtab(); 853 if (symtab) 854 { 855 std::vector<uint32_t> symbol_indexes; 856 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 857 const size_t num_matches = symbol_indexes.size(); 858 if (num_matches) 859 { 860 SymbolContext sc(this); 861 const size_t end_functions_added_index = sc_list.GetSize(); 862 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size; 863 if (num_functions_added_to_sc_list == 0) 864 { 865 // No functions were added, just symbols, so we can just append them 866 for (size_t i = 0; i < num_matches; ++i) 867 { 868 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 869 SymbolType sym_type = sc.symbol->GetType(); 870 if (sc.symbol && (sym_type == eSymbolTypeCode || 871 sym_type == eSymbolTypeResolver)) 872 sc_list.Append(sc); 873 } 874 } 875 else 876 { 877 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap; 878 FileAddrToIndexMap file_addr_to_index; 879 for (size_t i = start_size; i < end_functions_added_index; ++i) 880 { 881 const SymbolContext &sc = sc_list[i]; 882 if (sc.block) 883 continue; 884 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i; 885 } 886 887 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end(); 888 // Functions were added so we need to merge symbols into any 889 // existing function symbol contexts 890 for (size_t i = start_size; i < num_matches; ++i) 891 { 892 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 893 SymbolType sym_type = sc.symbol->GetType(); 894 if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver)) 895 { 896 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress()); 897 if (pos == end) 898 sc_list.Append(sc); 899 else 900 sc_list[pos->second].symbol = sc.symbol; 901 } 902 } 903 } 904 } 905 } 906 } 907 } 908 return sc_list.GetSize() - start_size; 909 } 910 911 void 912 Module::FindAddressesForLine (const lldb::TargetSP target_sp, 913 const FileSpec &file, uint32_t line, 914 Function *function, 915 std::vector<Address> &output_local, std::vector<Address> &output_extern) 916 { 917 SearchFilterByModule filter(target_sp, m_file); 918 AddressResolverFileLine resolver(file, line, true); 919 resolver.ResolveAddress (filter); 920 921 for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) 922 { 923 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress(); 924 Function *f = addr.CalculateSymbolContextFunction(); 925 if (f && f == function) 926 output_local.push_back (addr); 927 else 928 output_extern.push_back (addr); 929 } 930 } 931 932 size_t 933 Module::FindTypes_Impl (const SymbolContext& sc, 934 const ConstString &name, 935 const CompilerDeclContext *parent_decl_ctx, 936 bool append, 937 size_t max_matches, 938 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 939 TypeMap& types) 940 { 941 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 942 if (!sc.module_sp || sc.module_sp.get() == this) 943 { 944 SymbolVendor *symbols = GetSymbolVendor (); 945 if (symbols) 946 return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types); 947 } 948 return 0; 949 } 950 951 size_t 952 Module::FindTypesInNamespace (const SymbolContext& sc, 953 const ConstString &type_name, 954 const CompilerDeclContext *parent_decl_ctx, 955 size_t max_matches, 956 TypeList& type_list) 957 { 958 const bool append = true; 959 TypeMap types_map; 960 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 961 size_t num_types = FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, searched_symbol_files, types_map); 962 if (num_types > 0) 963 sc.SortTypeList(types_map, type_list); 964 return num_types; 965 } 966 967 lldb::TypeSP 968 Module::FindFirstType (const SymbolContext& sc, 969 const ConstString &name, 970 bool exact_match) 971 { 972 TypeList type_list; 973 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 974 const size_t num_matches = FindTypes (sc, name, exact_match, 1, searched_symbol_files, type_list); 975 if (num_matches) 976 return type_list.GetTypeAtIndex(0); 977 return TypeSP(); 978 } 979 980 size_t 981 Module::FindTypes (const SymbolContext& sc, 982 const ConstString &name, 983 bool exact_match, 984 size_t max_matches, 985 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 986 TypeList& types) 987 { 988 size_t num_matches = 0; 989 const char *type_name_cstr = name.GetCString(); 990 std::string type_scope; 991 std::string type_basename; 992 const bool append = true; 993 TypeClass type_class = eTypeClassAny; 994 TypeMap typesmap; 995 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class)) 996 { 997 // Check if "name" starts with "::" which means the qualified type starts 998 // from the root namespace and implies and exact match. The typenames we 999 // get back from clang do not start with "::" so we need to strip this off 1000 // in order to get the qualified names to match 1001 1002 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':') 1003 { 1004 type_scope.erase(0,2); 1005 exact_match = true; 1006 } 1007 ConstString type_basename_const_str (type_basename.c_str()); 1008 if (FindTypes_Impl(sc, type_basename_const_str, nullptr, append, max_matches, searched_symbol_files, typesmap)) 1009 { 1010 typesmap.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match); 1011 num_matches = typesmap.GetSize(); 1012 } 1013 } 1014 else 1015 { 1016 // The type is not in a namespace/class scope, just search for it by basename 1017 if (type_class != eTypeClassAny) 1018 { 1019 // The "type_name_cstr" will have been modified if we have a valid type class 1020 // prefix (like "struct", "class", "union", "typedef" etc). 1021 FindTypes_Impl(sc, ConstString(type_name_cstr), nullptr, append, max_matches, searched_symbol_files, typesmap); 1022 typesmap.RemoveMismatchedTypes (type_class); 1023 num_matches = typesmap.GetSize(); 1024 } 1025 else 1026 { 1027 num_matches = FindTypes_Impl(sc, name, nullptr, append, max_matches, searched_symbol_files, typesmap); 1028 } 1029 } 1030 if (num_matches > 0) 1031 sc.SortTypeList(typesmap, types); 1032 return num_matches; 1033 } 1034 1035 SymbolVendor* 1036 Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm) 1037 { 1038 if (!m_did_load_symbol_vendor.load()) 1039 { 1040 Mutex::Locker locker (m_mutex); 1041 if (!m_did_load_symbol_vendor.load() && can_create) 1042 { 1043 ObjectFile *obj_file = GetObjectFile (); 1044 if (obj_file != nullptr) 1045 { 1046 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 1047 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm)); 1048 m_did_load_symbol_vendor = true; 1049 } 1050 } 1051 } 1052 return m_symfile_ap.get(); 1053 } 1054 1055 void 1056 Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) 1057 { 1058 // Container objects whose paths do not specify a file directly can call 1059 // this function to correct the file and object names. 1060 m_file = file; 1061 m_mod_time = file.GetModificationTime(); 1062 m_object_name = object_name; 1063 } 1064 1065 const ArchSpec& 1066 Module::GetArchitecture () const 1067 { 1068 return m_arch; 1069 } 1070 1071 std::string 1072 Module::GetSpecificationDescription () const 1073 { 1074 std::string spec(GetFileSpec().GetPath()); 1075 if (m_object_name) 1076 { 1077 spec += '('; 1078 spec += m_object_name.GetCString(); 1079 spec += ')'; 1080 } 1081 return spec; 1082 } 1083 1084 void 1085 Module::GetDescription (Stream *s, lldb::DescriptionLevel level) 1086 { 1087 Mutex::Locker locker (m_mutex); 1088 1089 if (level >= eDescriptionLevelFull) 1090 { 1091 if (m_arch.IsValid()) 1092 s->Printf("(%s) ", m_arch.GetArchitectureName()); 1093 } 1094 1095 if (level == eDescriptionLevelBrief) 1096 { 1097 const char *filename = m_file.GetFilename().GetCString(); 1098 if (filename) 1099 s->PutCString (filename); 1100 } 1101 else 1102 { 1103 char path[PATH_MAX]; 1104 if (m_file.GetPath(path, sizeof(path))) 1105 s->PutCString(path); 1106 } 1107 1108 const char *object_name = m_object_name.GetCString(); 1109 if (object_name) 1110 s->Printf("(%s)", object_name); 1111 } 1112 1113 void 1114 Module::ReportError (const char *format, ...) 1115 { 1116 if (format && format[0]) 1117 { 1118 StreamString strm; 1119 strm.PutCString("error: "); 1120 GetDescription(&strm, lldb::eDescriptionLevelBrief); 1121 strm.PutChar (' '); 1122 va_list args; 1123 va_start (args, format); 1124 strm.PrintfVarArg(format, args); 1125 va_end (args); 1126 1127 const int format_len = strlen(format); 1128 if (format_len > 0) 1129 { 1130 const char last_char = format[format_len-1]; 1131 if (last_char != '\n' || last_char != '\r') 1132 strm.EOL(); 1133 } 1134 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 1135 } 1136 } 1137 1138 bool 1139 Module::FileHasChanged () const 1140 { 1141 if (!m_file_has_changed) 1142 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time); 1143 return m_file_has_changed; 1144 } 1145 1146 void 1147 Module::ReportErrorIfModifyDetected (const char *format, ...) 1148 { 1149 if (!m_first_file_changed_log) 1150 { 1151 if (FileHasChanged ()) 1152 { 1153 m_first_file_changed_log = true; 1154 if (format) 1155 { 1156 StreamString strm; 1157 strm.PutCString("error: the object file "); 1158 GetDescription(&strm, lldb::eDescriptionLevelFull); 1159 strm.PutCString (" has been modified\n"); 1160 1161 va_list args; 1162 va_start (args, format); 1163 strm.PrintfVarArg(format, args); 1164 va_end (args); 1165 1166 const int format_len = strlen(format); 1167 if (format_len > 0) 1168 { 1169 const char last_char = format[format_len-1]; 1170 if (last_char != '\n' || last_char != '\r') 1171 strm.EOL(); 1172 } 1173 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); 1174 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 1175 } 1176 } 1177 } 1178 } 1179 1180 void 1181 Module::ReportWarning (const char *format, ...) 1182 { 1183 if (format && format[0]) 1184 { 1185 StreamString strm; 1186 strm.PutCString("warning: "); 1187 GetDescription(&strm, lldb::eDescriptionLevelFull); 1188 strm.PutChar (' '); 1189 1190 va_list args; 1191 va_start (args, format); 1192 strm.PrintfVarArg(format, args); 1193 va_end (args); 1194 1195 const int format_len = strlen(format); 1196 if (format_len > 0) 1197 { 1198 const char last_char = format[format_len-1]; 1199 if (last_char != '\n' || last_char != '\r') 1200 strm.EOL(); 1201 } 1202 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); 1203 } 1204 } 1205 1206 void 1207 Module::LogMessage (Log *log, const char *format, ...) 1208 { 1209 if (log != nullptr) 1210 { 1211 StreamString log_message; 1212 GetDescription(&log_message, lldb::eDescriptionLevelFull); 1213 log_message.PutCString (": "); 1214 va_list args; 1215 va_start (args, format); 1216 log_message.PrintfVarArg (format, args); 1217 va_end (args); 1218 log->PutCString(log_message.GetString().c_str()); 1219 } 1220 } 1221 1222 void 1223 Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...) 1224 { 1225 if (log != nullptr) 1226 { 1227 StreamString log_message; 1228 GetDescription(&log_message, lldb::eDescriptionLevelFull); 1229 log_message.PutCString (": "); 1230 va_list args; 1231 va_start (args, format); 1232 log_message.PrintfVarArg (format, args); 1233 va_end (args); 1234 if (log->GetVerbose()) 1235 { 1236 std::string back_trace; 1237 llvm::raw_string_ostream stream(back_trace); 1238 llvm::sys::PrintStackTrace(stream); 1239 log_message.PutCString(back_trace.c_str()); 1240 } 1241 log->PutCString(log_message.GetString().c_str()); 1242 } 1243 } 1244 1245 void 1246 Module::Dump(Stream *s) 1247 { 1248 Mutex::Locker locker (m_mutex); 1249 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1250 s->Indent(); 1251 s->Printf("Module %s%s%s%s\n", 1252 m_file.GetPath().c_str(), 1253 m_object_name ? "(" : "", 1254 m_object_name ? m_object_name.GetCString() : "", 1255 m_object_name ? ")" : ""); 1256 1257 s->IndentMore(); 1258 1259 ObjectFile *objfile = GetObjectFile (); 1260 if (objfile) 1261 objfile->Dump(s); 1262 1263 SymbolVendor *symbols = GetSymbolVendor (); 1264 if (symbols) 1265 symbols->Dump(s); 1266 1267 s->IndentLess(); 1268 } 1269 1270 TypeList* 1271 Module::GetTypeList () 1272 { 1273 SymbolVendor *symbols = GetSymbolVendor (); 1274 if (symbols) 1275 return &symbols->GetTypeList(); 1276 return nullptr; 1277 } 1278 1279 const ConstString & 1280 Module::GetObjectName() const 1281 { 1282 return m_object_name; 1283 } 1284 1285 ObjectFile * 1286 Module::GetObjectFile() 1287 { 1288 if (!m_did_load_objfile.load()) 1289 { 1290 Mutex::Locker locker (m_mutex); 1291 if (!m_did_load_objfile.load()) 1292 { 1293 Timer scoped_timer(__PRETTY_FUNCTION__, 1294 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); 1295 DataBufferSP data_sp; 1296 lldb::offset_t data_offset = 0; 1297 const lldb::offset_t file_size = m_file.GetByteSize(); 1298 if (file_size > m_object_offset) 1299 { 1300 m_did_load_objfile = true; 1301 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(), 1302 &m_file, 1303 m_object_offset, 1304 file_size - m_object_offset, 1305 data_sp, 1306 data_offset); 1307 if (m_objfile_sp) 1308 { 1309 // Once we get the object file, update our module with the object file's 1310 // architecture since it might differ in vendor/os if some parts were 1311 // unknown. But since the matching arch might already be more specific 1312 // than the generic COFF architecture, only merge in those values that 1313 // overwrite unspecified unknown values. 1314 ArchSpec new_arch; 1315 m_objfile_sp->GetArchitecture(new_arch); 1316 m_arch.MergeFrom(new_arch); 1317 } 1318 else 1319 { 1320 ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str()); 1321 } 1322 } 1323 } 1324 } 1325 return m_objfile_sp.get(); 1326 } 1327 1328 SectionList * 1329 Module::GetSectionList() 1330 { 1331 // Populate m_unified_sections_ap with sections from objfile. 1332 if (!m_sections_ap) 1333 { 1334 ObjectFile *obj_file = GetObjectFile(); 1335 if (obj_file != nullptr) 1336 obj_file->CreateSections(*GetUnifiedSectionList()); 1337 } 1338 return m_sections_ap.get(); 1339 } 1340 1341 void 1342 Module::SectionFileAddressesChanged () 1343 { 1344 ObjectFile *obj_file = GetObjectFile (); 1345 if (obj_file) 1346 obj_file->SectionFileAddressesChanged (); 1347 SymbolVendor* sym_vendor = GetSymbolVendor(); 1348 if (sym_vendor != nullptr) 1349 sym_vendor->SectionFileAddressesChanged (); 1350 } 1351 1352 SectionList * 1353 Module::GetUnifiedSectionList() 1354 { 1355 // Populate m_unified_sections_ap with sections from objfile. 1356 if (!m_sections_ap) 1357 m_sections_ap.reset(new SectionList()); 1358 return m_sections_ap.get(); 1359 } 1360 1361 const Symbol * 1362 Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) 1363 { 1364 Timer scoped_timer(__PRETTY_FUNCTION__, 1365 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1366 name.AsCString(), 1367 symbol_type); 1368 SymbolVendor* sym_vendor = GetSymbolVendor(); 1369 if (sym_vendor) 1370 { 1371 Symtab *symtab = sym_vendor->GetSymtab(); 1372 if (symtab) 1373 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1374 } 1375 return nullptr; 1376 } 1377 void 1378 Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) 1379 { 1380 // No need to protect this call using m_mutex all other method calls are 1381 // already thread safe. 1382 1383 size_t num_indices = symbol_indexes.size(); 1384 if (num_indices > 0) 1385 { 1386 SymbolContext sc; 1387 CalculateSymbolContext (&sc); 1388 for (size_t i = 0; i < num_indices; i++) 1389 { 1390 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); 1391 if (sc.symbol) 1392 sc_list.Append (sc); 1393 } 1394 } 1395 } 1396 1397 size_t 1398 Module::FindFunctionSymbols (const ConstString &name, 1399 uint32_t name_type_mask, 1400 SymbolContextList& sc_list) 1401 { 1402 Timer scoped_timer(__PRETTY_FUNCTION__, 1403 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)", 1404 name.AsCString(), 1405 name_type_mask); 1406 SymbolVendor* sym_vendor = GetSymbolVendor(); 1407 if (sym_vendor) 1408 { 1409 Symtab *symtab = sym_vendor->GetSymtab(); 1410 if (symtab) 1411 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list); 1412 } 1413 return 0; 1414 } 1415 1416 size_t 1417 Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) 1418 { 1419 // No need to protect this call using m_mutex all other method calls are 1420 // already thread safe. 1421 1422 Timer scoped_timer(__PRETTY_FUNCTION__, 1423 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1424 name.AsCString(), 1425 symbol_type); 1426 const size_t initial_size = sc_list.GetSize(); 1427 SymbolVendor* sym_vendor = GetSymbolVendor(); 1428 if (sym_vendor) 1429 { 1430 Symtab *symtab = sym_vendor->GetSymtab(); 1431 if (symtab) 1432 { 1433 std::vector<uint32_t> symbol_indexes; 1434 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); 1435 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1436 } 1437 } 1438 return sc_list.GetSize() - initial_size; 1439 } 1440 1441 size_t 1442 Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) 1443 { 1444 // No need to protect this call using m_mutex all other method calls are 1445 // already thread safe. 1446 1447 Timer scoped_timer(__PRETTY_FUNCTION__, 1448 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 1449 regex.GetText(), 1450 symbol_type); 1451 const size_t initial_size = sc_list.GetSize(); 1452 SymbolVendor* sym_vendor = GetSymbolVendor(); 1453 if (sym_vendor) 1454 { 1455 Symtab *symtab = sym_vendor->GetSymtab(); 1456 if (symtab) 1457 { 1458 std::vector<uint32_t> symbol_indexes; 1459 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 1460 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1461 } 1462 } 1463 return sc_list.GetSize() - initial_size; 1464 } 1465 1466 void 1467 Module::SetSymbolFileFileSpec (const FileSpec &file) 1468 { 1469 if (!file.Exists()) 1470 return; 1471 if (m_symfile_ap) 1472 { 1473 // Remove any sections in the unified section list that come from the current symbol vendor. 1474 SectionList *section_list = GetSectionList(); 1475 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile(); 1476 if (section_list && symbol_file) 1477 { 1478 ObjectFile *obj_file = symbol_file->GetObjectFile(); 1479 // Make sure we have an object file and that the symbol vendor's objfile isn't 1480 // the same as the module's objfile before we remove any sections for it... 1481 if (obj_file) 1482 { 1483 // Check to make sure we aren't trying to specify the file we already have 1484 if (obj_file->GetFileSpec() == file) 1485 { 1486 // We are being told to add the exact same file that we already have 1487 // we don't have to do anything. 1488 return; 1489 } 1490 1491 // Cleare the current symtab as we are going to replace it with a new one 1492 obj_file->ClearSymtab(); 1493 1494 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead 1495 // of a full path to the symbol file within the bundle 1496 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this 1497 1498 if (file.IsDirectory()) 1499 { 1500 std::string new_path(file.GetPath()); 1501 std::string old_path(obj_file->GetFileSpec().GetPath()); 1502 if (old_path.find(new_path) == 0) 1503 { 1504 // We specified the same bundle as the symbol file that we already have 1505 return; 1506 } 1507 } 1508 1509 if (obj_file != m_objfile_sp.get()) 1510 { 1511 size_t num_sections = section_list->GetNumSections (0); 1512 for (size_t idx = num_sections; idx > 0; --idx) 1513 { 1514 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1)); 1515 if (section_sp->GetObjectFile() == obj_file) 1516 { 1517 section_list->DeleteSection (idx - 1); 1518 } 1519 } 1520 } 1521 } 1522 } 1523 // Keep all old symbol files around in case there are any lingering type references in 1524 // any SBValue objects that might have been handed out. 1525 m_old_symfiles.push_back(std::move(m_symfile_ap)); 1526 } 1527 m_symfile_spec = file; 1528 m_symfile_ap.reset(); 1529 m_did_load_symbol_vendor = false; 1530 } 1531 1532 bool 1533 Module::IsExecutable () 1534 { 1535 if (GetObjectFile() == nullptr) 1536 return false; 1537 else 1538 return GetObjectFile()->IsExecutable(); 1539 } 1540 1541 bool 1542 Module::IsLoadedInTarget (Target *target) 1543 { 1544 ObjectFile *obj_file = GetObjectFile(); 1545 if (obj_file) 1546 { 1547 SectionList *sections = GetSectionList(); 1548 if (sections != nullptr) 1549 { 1550 size_t num_sections = sections->GetSize(); 1551 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) 1552 { 1553 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1554 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) 1555 { 1556 return true; 1557 } 1558 } 1559 } 1560 } 1561 return false; 1562 } 1563 1564 bool 1565 Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream) 1566 { 1567 if (!target) 1568 { 1569 error.SetErrorString("invalid destination Target"); 1570 return false; 1571 } 1572 1573 LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile(); 1574 1575 if (should_load == eLoadScriptFromSymFileFalse) 1576 return false; 1577 1578 Debugger &debugger = target->GetDebugger(); 1579 const ScriptLanguage script_language = debugger.GetScriptLanguage(); 1580 if (script_language != eScriptLanguageNone) 1581 { 1582 1583 PlatformSP platform_sp(target->GetPlatform()); 1584 1585 if (!platform_sp) 1586 { 1587 error.SetErrorString("invalid Platform"); 1588 return false; 1589 } 1590 1591 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target, 1592 *this, 1593 feedback_stream); 1594 1595 const uint32_t num_specs = file_specs.GetSize(); 1596 if (num_specs) 1597 { 1598 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); 1599 if (script_interpreter) 1600 { 1601 for (uint32_t i = 0; i < num_specs; ++i) 1602 { 1603 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i)); 1604 if (scripting_fspec && scripting_fspec.Exists()) 1605 { 1606 if (should_load == eLoadScriptFromSymFileWarn) 1607 { 1608 if (feedback_stream) 1609 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in " 1610 "this debug session:\n\n command script import \"%s\"\n\n" 1611 "To run all discovered debug scripts in this session:\n\n" 1612 " settings set target.load-script-from-symbol-file true\n", 1613 GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1614 scripting_fspec.GetPath().c_str()); 1615 return false; 1616 } 1617 StreamString scripting_stream; 1618 scripting_fspec.Dump(&scripting_stream); 1619 const bool can_reload = true; 1620 const bool init_lldb_globals = false; 1621 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), 1622 can_reload, 1623 init_lldb_globals, 1624 error); 1625 if (!did_load) 1626 return false; 1627 } 1628 } 1629 } 1630 else 1631 { 1632 error.SetErrorString("invalid ScriptInterpreter"); 1633 return false; 1634 } 1635 } 1636 } 1637 return true; 1638 } 1639 1640 bool 1641 Module::SetArchitecture (const ArchSpec &new_arch) 1642 { 1643 if (!m_arch.IsValid()) 1644 { 1645 m_arch = new_arch; 1646 return true; 1647 } 1648 return m_arch.IsCompatibleMatch(new_arch); 1649 } 1650 1651 bool 1652 Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed) 1653 { 1654 ObjectFile *object_file = GetObjectFile(); 1655 if (object_file != nullptr) 1656 { 1657 changed = object_file->SetLoadAddress(target, value, value_is_offset); 1658 return true; 1659 } 1660 else 1661 { 1662 changed = false; 1663 } 1664 return false; 1665 } 1666 1667 1668 bool 1669 Module::MatchesModuleSpec (const ModuleSpec &module_ref) 1670 { 1671 const UUID &uuid = module_ref.GetUUID(); 1672 1673 if (uuid.IsValid()) 1674 { 1675 // If the UUID matches, then nothing more needs to match... 1676 return (uuid == GetUUID()); 1677 } 1678 1679 const FileSpec &file_spec = module_ref.GetFileSpec(); 1680 if (file_spec) 1681 { 1682 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()) && 1683 !FileSpec::Equal (file_spec, m_platform_file, (bool)file_spec.GetDirectory())) 1684 return false; 1685 } 1686 1687 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1688 if (platform_file_spec) 1689 { 1690 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory())) 1691 return false; 1692 } 1693 1694 const ArchSpec &arch = module_ref.GetArchitecture(); 1695 if (arch.IsValid()) 1696 { 1697 if (!m_arch.IsCompatibleMatch(arch)) 1698 return false; 1699 } 1700 1701 const ConstString &object_name = module_ref.GetObjectName(); 1702 if (object_name) 1703 { 1704 if (object_name != GetObjectName()) 1705 return false; 1706 } 1707 return true; 1708 } 1709 1710 bool 1711 Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 1712 { 1713 Mutex::Locker locker (m_mutex); 1714 return m_source_mappings.FindFile (orig_spec, new_spec); 1715 } 1716 1717 bool 1718 Module::RemapSourceFile (const char *path, std::string &new_path) const 1719 { 1720 Mutex::Locker locker (m_mutex); 1721 return m_source_mappings.RemapPath(path, new_path); 1722 } 1723 1724 uint32_t 1725 Module::GetVersion (uint32_t *versions, uint32_t num_versions) 1726 { 1727 ObjectFile *obj_file = GetObjectFile(); 1728 if (obj_file) 1729 return obj_file->GetVersion (versions, num_versions); 1730 1731 if (versions != nullptr && num_versions != 0) 1732 { 1733 for (uint32_t i = 0; i < num_versions; ++i) 1734 versions[i] = LLDB_INVALID_MODULE_VERSION; 1735 } 1736 return 0; 1737 } 1738 1739 void 1740 Module::PrepareForFunctionNameLookup (const ConstString &name, 1741 uint32_t name_type_mask, 1742 LanguageType language, 1743 ConstString &lookup_name, 1744 uint32_t &lookup_name_type_mask, 1745 bool &match_name_after_lookup) 1746 { 1747 const char *name_cstr = name.GetCString(); 1748 lookup_name_type_mask = eFunctionNameTypeNone; 1749 match_name_after_lookup = false; 1750 1751 llvm::StringRef basename; 1752 llvm::StringRef context; 1753 1754 if (name_type_mask & eFunctionNameTypeAuto) 1755 { 1756 if (CPlusPlusLanguage::IsCPPMangledName (name_cstr)) 1757 lookup_name_type_mask = eFunctionNameTypeFull; 1758 else if ((language == eLanguageTypeUnknown || 1759 Language::LanguageIsObjC(language)) && 1760 ObjCLanguage::IsPossibleObjCMethodName (name_cstr)) 1761 lookup_name_type_mask = eFunctionNameTypeFull; 1762 else if (Language::LanguageIsC(language)) 1763 { 1764 lookup_name_type_mask = eFunctionNameTypeFull; 1765 } 1766 else 1767 { 1768 if ((language == eLanguageTypeUnknown || 1769 Language::LanguageIsObjC(language)) && 1770 ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 1771 lookup_name_type_mask |= eFunctionNameTypeSelector; 1772 1773 CPlusPlusLanguage::MethodName cpp_method (name); 1774 basename = cpp_method.GetBasename(); 1775 if (basename.empty()) 1776 { 1777 if (CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename)) 1778 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 1779 else 1780 lookup_name_type_mask |= eFunctionNameTypeFull; 1781 } 1782 else 1783 { 1784 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 1785 } 1786 } 1787 } 1788 else 1789 { 1790 lookup_name_type_mask = name_type_mask; 1791 if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase) 1792 { 1793 // If they've asked for a CPP method or function name and it can't be that, we don't 1794 // even need to search for CPP methods or names. 1795 CPlusPlusLanguage::MethodName cpp_method (name); 1796 if (cpp_method.IsValid()) 1797 { 1798 basename = cpp_method.GetBasename(); 1799 1800 if (!cpp_method.GetQualifiers().empty()) 1801 { 1802 // There is a "const" or other qualifier following the end of the function parens, 1803 // this can't be a eFunctionNameTypeBase 1804 lookup_name_type_mask &= ~(eFunctionNameTypeBase); 1805 if (lookup_name_type_mask == eFunctionNameTypeNone) 1806 return; 1807 } 1808 } 1809 else 1810 { 1811 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can. 1812 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later. 1813 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename); 1814 } 1815 } 1816 1817 if (lookup_name_type_mask & eFunctionNameTypeSelector) 1818 { 1819 if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 1820 { 1821 lookup_name_type_mask &= ~(eFunctionNameTypeSelector); 1822 if (lookup_name_type_mask == eFunctionNameTypeNone) 1823 return; 1824 } 1825 } 1826 } 1827 1828 if (!basename.empty()) 1829 { 1830 // The name supplied was a partial C++ path like "a::count". In this case we want to do a 1831 // lookup on the basename "count" and then make sure any matching results contain "a::count" 1832 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup" 1833 // to true 1834 lookup_name.SetString(basename); 1835 match_name_after_lookup = true; 1836 } 1837 else 1838 { 1839 // The name is already correct, just use the exact name as supplied, and we won't need 1840 // to check if any matches contain "name" 1841 lookup_name = name; 1842 match_name_after_lookup = false; 1843 } 1844 } 1845 1846 ModuleSP 1847 Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp) 1848 { 1849 if (delegate_sp) 1850 { 1851 // Must create a module and place it into a shared pointer before 1852 // we can create an object file since it has a std::weak_ptr back 1853 // to the module, so we need to control the creation carefully in 1854 // this static function 1855 ModuleSP module_sp(new Module()); 1856 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp)); 1857 if (module_sp->m_objfile_sp) 1858 { 1859 // Once we get the object file, update our module with the object file's 1860 // architecture since it might differ in vendor/os if some parts were 1861 // unknown. 1862 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch); 1863 } 1864 return module_sp; 1865 } 1866 return ModuleSP(); 1867 } 1868 1869 bool 1870 Module::GetIsDynamicLinkEditor() 1871 { 1872 ObjectFile * obj_file = GetObjectFile (); 1873 1874 if (obj_file) 1875 return obj_file->GetIsDynamicLinkEditor(); 1876 1877 return false; 1878 } 1879