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