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", LLVM_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(LLVM_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(LLVM_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(LLVM_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 Module::LookupInfo::LookupInfo(const ConstString &name, uint32_t name_type_mask, lldb::LanguageType language) : 738 m_name(name), 739 m_lookup_name(), 740 m_language(language), 741 m_name_type_mask(0), 742 m_match_name_after_lookup(false) 743 { 744 const char *name_cstr = name.GetCString(); 745 llvm::StringRef basename; 746 llvm::StringRef context; 747 748 if (name_type_mask & eFunctionNameTypeAuto) 749 { 750 if (CPlusPlusLanguage::IsCPPMangledName (name_cstr)) 751 m_name_type_mask = eFunctionNameTypeFull; 752 else if ((language == eLanguageTypeUnknown || 753 Language::LanguageIsObjC(language)) && 754 ObjCLanguage::IsPossibleObjCMethodName (name_cstr)) 755 m_name_type_mask = eFunctionNameTypeFull; 756 else if (Language::LanguageIsC(language)) 757 { 758 m_name_type_mask = eFunctionNameTypeFull; 759 } 760 else 761 { 762 if ((language == eLanguageTypeUnknown || 763 Language::LanguageIsObjC(language)) && 764 ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 765 m_name_type_mask |= eFunctionNameTypeSelector; 766 767 CPlusPlusLanguage::MethodName cpp_method (name); 768 basename = cpp_method.GetBasename(); 769 if (basename.empty()) 770 { 771 if (CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename)) 772 m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 773 else 774 m_name_type_mask |= eFunctionNameTypeFull; 775 } 776 else 777 { 778 m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 779 } 780 } 781 } 782 else 783 { 784 m_name_type_mask = name_type_mask; 785 if (name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase) 786 { 787 // If they've asked for a CPP method or function name and it can't be that, we don't 788 // even need to search for CPP methods or names. 789 CPlusPlusLanguage::MethodName cpp_method (name); 790 if (cpp_method.IsValid()) 791 { 792 basename = cpp_method.GetBasename(); 793 794 if (!cpp_method.GetQualifiers().empty()) 795 { 796 // There is a "const" or other qualifier following the end of the function parens, 797 // this can't be a eFunctionNameTypeBase 798 m_name_type_mask &= ~(eFunctionNameTypeBase); 799 if (m_name_type_mask == eFunctionNameTypeNone) 800 return; 801 } 802 } 803 else 804 { 805 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can. 806 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later. 807 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename); 808 } 809 } 810 811 if (name_type_mask & eFunctionNameTypeSelector) 812 { 813 if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 814 { 815 m_name_type_mask &= ~(eFunctionNameTypeSelector); 816 if (m_name_type_mask == eFunctionNameTypeNone) 817 return; 818 } 819 } 820 821 // Still try and get a basename in case someone specifies a name type mask of 822 // eFunctionNameTypeFull and a name like "A::func" 823 if (basename.empty()) 824 { 825 if (name_type_mask & eFunctionNameTypeFull) 826 { 827 CPlusPlusLanguage::MethodName cpp_method (name); 828 basename = cpp_method.GetBasename(); 829 if (basename.empty()) 830 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename); 831 } 832 } 833 } 834 835 if (!basename.empty()) 836 { 837 // The name supplied was a partial C++ path like "a::count". In this case we want to do a 838 // lookup on the basename "count" and then make sure any matching results contain "a::count" 839 // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup" 840 // to true 841 m_lookup_name.SetString(basename); 842 m_match_name_after_lookup = true; 843 } 844 else 845 { 846 // The name is already correct, just use the exact name as supplied, and we won't need 847 // to check if any matches contain "name" 848 m_lookup_name = name; 849 m_match_name_after_lookup = false; 850 } 851 852 } 853 854 void 855 Module::LookupInfo::Prune(SymbolContextList &sc_list, size_t start_idx) const 856 { 857 if (m_match_name_after_lookup && m_name) 858 { 859 SymbolContext sc; 860 size_t i = start_idx; 861 while (i < sc_list.GetSize()) 862 { 863 if (!sc_list.GetContextAtIndex(i, sc)) 864 break; 865 ConstString full_name(sc.GetFunctionName()); 866 if (full_name && ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) 867 { 868 sc_list.RemoveContextAtIndex(i); 869 } 870 else 871 { 872 ++i; 873 } 874 } 875 } 876 877 // If we have only full name matches we might have tried to set breakpoint on "func" 878 // and specified eFunctionNameTypeFull, but we might have found "a::func()", 879 // "a::b::func()", "c::func()", "func()" and "func". Only "func()" and "func" should 880 // end up matching. 881 if (m_name_type_mask == eFunctionNameTypeFull) 882 { 883 SymbolContext sc; 884 size_t i = start_idx; 885 while (i < sc_list.GetSize()) 886 { 887 if (!sc_list.GetContextAtIndex(i, sc)) 888 break; 889 ConstString full_name(sc.GetFunctionName()); 890 CPlusPlusLanguage::MethodName cpp_method(full_name); 891 if (cpp_method.IsValid()) 892 { 893 if (cpp_method.GetContext().empty()) 894 { 895 if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) 896 { 897 sc_list.RemoveContextAtIndex(i); 898 continue; 899 } 900 } 901 else 902 { 903 std::string qualified_name = cpp_method.GetScopeQualifiedName(); 904 if (qualified_name.compare(m_name.GetCString()) != 0) 905 { 906 sc_list.RemoveContextAtIndex(i); 907 continue; 908 } 909 } 910 } 911 ++i; 912 } 913 } 914 } 915 916 917 size_t 918 Module::FindFunctions (const ConstString &name, 919 const CompilerDeclContext *parent_decl_ctx, 920 uint32_t name_type_mask, 921 bool include_symbols, 922 bool include_inlines, 923 bool append, 924 SymbolContextList& sc_list) 925 { 926 if (!append) 927 sc_list.Clear(); 928 929 const size_t old_size = sc_list.GetSize(); 930 931 // Find all the functions (not symbols, but debug information functions... 932 SymbolVendor *symbols = GetSymbolVendor (); 933 934 if (name_type_mask & eFunctionNameTypeAuto) 935 { 936 LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 937 938 if (symbols) 939 { 940 symbols->FindFunctions(lookup_info.GetLookupName(), 941 parent_decl_ctx, 942 lookup_info.GetNameTypeMask(), 943 include_inlines, 944 append, 945 sc_list); 946 947 // Now check our symbol table for symbols that are code symbols if requested 948 if (include_symbols) 949 { 950 Symtab *symtab = symbols->GetSymtab(); 951 if (symtab) 952 symtab->FindFunctionSymbols(lookup_info.GetLookupName(), lookup_info.GetNameTypeMask(), sc_list); 953 } 954 } 955 956 const size_t new_size = sc_list.GetSize(); 957 958 if (old_size < new_size) 959 lookup_info.Prune (sc_list, old_size); 960 } 961 else 962 { 963 if (symbols) 964 { 965 symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list); 966 967 // Now check our symbol table for symbols that are code symbols if requested 968 if (include_symbols) 969 { 970 Symtab *symtab = symbols->GetSymtab(); 971 if (symtab) 972 symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 973 } 974 } 975 } 976 977 return sc_list.GetSize() - old_size; 978 } 979 980 size_t 981 Module::FindFunctions (const RegularExpression& regex, 982 bool include_symbols, 983 bool include_inlines, 984 bool append, 985 SymbolContextList& sc_list) 986 { 987 if (!append) 988 sc_list.Clear(); 989 990 const size_t start_size = sc_list.GetSize(); 991 992 SymbolVendor *symbols = GetSymbolVendor (); 993 if (symbols) 994 { 995 symbols->FindFunctions(regex, include_inlines, append, sc_list); 996 997 // Now check our symbol table for symbols that are code symbols if requested 998 if (include_symbols) 999 { 1000 Symtab *symtab = symbols->GetSymtab(); 1001 if (symtab) 1002 { 1003 std::vector<uint32_t> symbol_indexes; 1004 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 1005 const size_t num_matches = symbol_indexes.size(); 1006 if (num_matches) 1007 { 1008 SymbolContext sc(this); 1009 const size_t end_functions_added_index = sc_list.GetSize(); 1010 size_t num_functions_added_to_sc_list = end_functions_added_index - start_size; 1011 if (num_functions_added_to_sc_list == 0) 1012 { 1013 // No functions were added, just symbols, so we can just append them 1014 for (size_t i = 0; i < num_matches; ++i) 1015 { 1016 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 1017 SymbolType sym_type = sc.symbol->GetType(); 1018 if (sc.symbol && (sym_type == eSymbolTypeCode || 1019 sym_type == eSymbolTypeResolver)) 1020 sc_list.Append(sc); 1021 } 1022 } 1023 else 1024 { 1025 typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap; 1026 FileAddrToIndexMap file_addr_to_index; 1027 for (size_t i = start_size; i < end_functions_added_index; ++i) 1028 { 1029 const SymbolContext &sc = sc_list[i]; 1030 if (sc.block) 1031 continue; 1032 file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i; 1033 } 1034 1035 FileAddrToIndexMap::const_iterator end = file_addr_to_index.end(); 1036 // Functions were added so we need to merge symbols into any 1037 // existing function symbol contexts 1038 for (size_t i = start_size; i < num_matches; ++i) 1039 { 1040 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 1041 SymbolType sym_type = sc.symbol->GetType(); 1042 if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver)) 1043 { 1044 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress()); 1045 if (pos == end) 1046 sc_list.Append(sc); 1047 else 1048 sc_list[pos->second].symbol = sc.symbol; 1049 } 1050 } 1051 } 1052 } 1053 } 1054 } 1055 } 1056 return sc_list.GetSize() - start_size; 1057 } 1058 1059 void 1060 Module::FindAddressesForLine (const lldb::TargetSP target_sp, 1061 const FileSpec &file, uint32_t line, 1062 Function *function, 1063 std::vector<Address> &output_local, std::vector<Address> &output_extern) 1064 { 1065 SearchFilterByModule filter(target_sp, m_file); 1066 AddressResolverFileLine resolver(file, line, true); 1067 resolver.ResolveAddress (filter); 1068 1069 for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) 1070 { 1071 Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress(); 1072 Function *f = addr.CalculateSymbolContextFunction(); 1073 if (f && f == function) 1074 output_local.push_back (addr); 1075 else 1076 output_extern.push_back (addr); 1077 } 1078 } 1079 1080 size_t 1081 Module::FindTypes_Impl (const SymbolContext& sc, 1082 const ConstString &name, 1083 const CompilerDeclContext *parent_decl_ctx, 1084 bool append, 1085 size_t max_matches, 1086 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1087 TypeMap& types) 1088 { 1089 Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION); 1090 if (!sc.module_sp || sc.module_sp.get() == this) 1091 { 1092 SymbolVendor *symbols = GetSymbolVendor (); 1093 if (symbols) 1094 return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types); 1095 } 1096 return 0; 1097 } 1098 1099 size_t 1100 Module::FindTypesInNamespace (const SymbolContext& sc, 1101 const ConstString &type_name, 1102 const CompilerDeclContext *parent_decl_ctx, 1103 size_t max_matches, 1104 TypeList& type_list) 1105 { 1106 const bool append = true; 1107 TypeMap types_map; 1108 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 1109 size_t num_types = FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, searched_symbol_files, types_map); 1110 if (num_types > 0) 1111 sc.SortTypeList(types_map, type_list); 1112 return num_types; 1113 } 1114 1115 lldb::TypeSP 1116 Module::FindFirstType (const SymbolContext& sc, 1117 const ConstString &name, 1118 bool exact_match) 1119 { 1120 TypeList type_list; 1121 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 1122 const size_t num_matches = FindTypes (sc, name, exact_match, 1, searched_symbol_files, type_list); 1123 if (num_matches) 1124 return type_list.GetTypeAtIndex(0); 1125 return TypeSP(); 1126 } 1127 1128 size_t 1129 Module::FindTypes (const SymbolContext& sc, 1130 const ConstString &name, 1131 bool exact_match, 1132 size_t max_matches, 1133 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1134 TypeList& types) 1135 { 1136 size_t num_matches = 0; 1137 const char *type_name_cstr = name.GetCString(); 1138 std::string type_scope; 1139 std::string type_basename; 1140 const bool append = true; 1141 TypeClass type_class = eTypeClassAny; 1142 TypeMap typesmap; 1143 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class)) 1144 { 1145 // Check if "name" starts with "::" which means the qualified type starts 1146 // from the root namespace and implies and exact match. The typenames we 1147 // get back from clang do not start with "::" so we need to strip this off 1148 // in order to get the qualified names to match 1149 1150 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':') 1151 { 1152 type_scope.erase(0,2); 1153 exact_match = true; 1154 } 1155 ConstString type_basename_const_str (type_basename.c_str()); 1156 if (FindTypes_Impl(sc, type_basename_const_str, nullptr, append, max_matches, searched_symbol_files, typesmap)) 1157 { 1158 typesmap.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match); 1159 num_matches = typesmap.GetSize(); 1160 } 1161 } 1162 else 1163 { 1164 // The type is not in a namespace/class scope, just search for it by basename 1165 if (type_class != eTypeClassAny) 1166 { 1167 // The "type_name_cstr" will have been modified if we have a valid type class 1168 // prefix (like "struct", "class", "union", "typedef" etc). 1169 FindTypes_Impl(sc, ConstString(type_name_cstr), nullptr, append, max_matches, searched_symbol_files, typesmap); 1170 typesmap.RemoveMismatchedTypes (type_class); 1171 num_matches = typesmap.GetSize(); 1172 } 1173 else 1174 { 1175 num_matches = FindTypes_Impl(sc, name, nullptr, append, max_matches, searched_symbol_files, typesmap); 1176 } 1177 } 1178 if (num_matches > 0) 1179 sc.SortTypeList(typesmap, types); 1180 return num_matches; 1181 } 1182 1183 SymbolVendor* 1184 Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm) 1185 { 1186 if (!m_did_load_symbol_vendor.load()) 1187 { 1188 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1189 if (!m_did_load_symbol_vendor.load() && can_create) 1190 { 1191 ObjectFile *obj_file = GetObjectFile (); 1192 if (obj_file != nullptr) 1193 { 1194 Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION); 1195 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm)); 1196 m_did_load_symbol_vendor = true; 1197 } 1198 } 1199 } 1200 return m_symfile_ap.get(); 1201 } 1202 1203 void 1204 Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) 1205 { 1206 // Container objects whose paths do not specify a file directly can call 1207 // this function to correct the file and object names. 1208 m_file = file; 1209 m_mod_time = file.GetModificationTime(); 1210 m_object_name = object_name; 1211 } 1212 1213 const ArchSpec& 1214 Module::GetArchitecture () const 1215 { 1216 return m_arch; 1217 } 1218 1219 std::string 1220 Module::GetSpecificationDescription () const 1221 { 1222 std::string spec(GetFileSpec().GetPath()); 1223 if (m_object_name) 1224 { 1225 spec += '('; 1226 spec += m_object_name.GetCString(); 1227 spec += ')'; 1228 } 1229 return spec; 1230 } 1231 1232 void 1233 Module::GetDescription (Stream *s, lldb::DescriptionLevel level) 1234 { 1235 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1236 1237 if (level >= eDescriptionLevelFull) 1238 { 1239 if (m_arch.IsValid()) 1240 s->Printf("(%s) ", m_arch.GetArchitectureName()); 1241 } 1242 1243 if (level == eDescriptionLevelBrief) 1244 { 1245 const char *filename = m_file.GetFilename().GetCString(); 1246 if (filename) 1247 s->PutCString (filename); 1248 } 1249 else 1250 { 1251 char path[PATH_MAX]; 1252 if (m_file.GetPath(path, sizeof(path))) 1253 s->PutCString(path); 1254 } 1255 1256 const char *object_name = m_object_name.GetCString(); 1257 if (object_name) 1258 s->Printf("(%s)", object_name); 1259 } 1260 1261 void 1262 Module::ReportError (const char *format, ...) 1263 { 1264 if (format && format[0]) 1265 { 1266 StreamString strm; 1267 strm.PutCString("error: "); 1268 GetDescription(&strm, lldb::eDescriptionLevelBrief); 1269 strm.PutChar (' '); 1270 va_list args; 1271 va_start (args, format); 1272 strm.PrintfVarArg(format, args); 1273 va_end (args); 1274 1275 const int format_len = strlen(format); 1276 if (format_len > 0) 1277 { 1278 const char last_char = format[format_len-1]; 1279 if (last_char != '\n' || last_char != '\r') 1280 strm.EOL(); 1281 } 1282 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 1283 } 1284 } 1285 1286 bool 1287 Module::FileHasChanged () const 1288 { 1289 if (!m_file_has_changed) 1290 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time); 1291 return m_file_has_changed; 1292 } 1293 1294 void 1295 Module::ReportErrorIfModifyDetected (const char *format, ...) 1296 { 1297 if (!m_first_file_changed_log) 1298 { 1299 if (FileHasChanged ()) 1300 { 1301 m_first_file_changed_log = true; 1302 if (format) 1303 { 1304 StreamString strm; 1305 strm.PutCString("error: the object file "); 1306 GetDescription(&strm, lldb::eDescriptionLevelFull); 1307 strm.PutCString (" has been modified\n"); 1308 1309 va_list args; 1310 va_start (args, format); 1311 strm.PrintfVarArg(format, args); 1312 va_end (args); 1313 1314 const int format_len = strlen(format); 1315 if (format_len > 0) 1316 { 1317 const char last_char = format[format_len-1]; 1318 if (last_char != '\n' || last_char != '\r') 1319 strm.EOL(); 1320 } 1321 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); 1322 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 1323 } 1324 } 1325 } 1326 } 1327 1328 void 1329 Module::ReportWarning (const char *format, ...) 1330 { 1331 if (format && format[0]) 1332 { 1333 StreamString strm; 1334 strm.PutCString("warning: "); 1335 GetDescription(&strm, lldb::eDescriptionLevelFull); 1336 strm.PutChar (' '); 1337 1338 va_list args; 1339 va_start (args, format); 1340 strm.PrintfVarArg(format, args); 1341 va_end (args); 1342 1343 const int format_len = strlen(format); 1344 if (format_len > 0) 1345 { 1346 const char last_char = format[format_len-1]; 1347 if (last_char != '\n' || last_char != '\r') 1348 strm.EOL(); 1349 } 1350 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); 1351 } 1352 } 1353 1354 void 1355 Module::LogMessage (Log *log, const char *format, ...) 1356 { 1357 if (log != nullptr) 1358 { 1359 StreamString log_message; 1360 GetDescription(&log_message, lldb::eDescriptionLevelFull); 1361 log_message.PutCString (": "); 1362 va_list args; 1363 va_start (args, format); 1364 log_message.PrintfVarArg (format, args); 1365 va_end (args); 1366 log->PutCString(log_message.GetString().c_str()); 1367 } 1368 } 1369 1370 void 1371 Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...) 1372 { 1373 if (log != nullptr) 1374 { 1375 StreamString log_message; 1376 GetDescription(&log_message, lldb::eDescriptionLevelFull); 1377 log_message.PutCString (": "); 1378 va_list args; 1379 va_start (args, format); 1380 log_message.PrintfVarArg (format, args); 1381 va_end (args); 1382 if (log->GetVerbose()) 1383 { 1384 std::string back_trace; 1385 llvm::raw_string_ostream stream(back_trace); 1386 llvm::sys::PrintStackTrace(stream); 1387 log_message.PutCString(back_trace.c_str()); 1388 } 1389 log->PutCString(log_message.GetString().c_str()); 1390 } 1391 } 1392 1393 void 1394 Module::Dump(Stream *s) 1395 { 1396 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1397 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1398 s->Indent(); 1399 s->Printf("Module %s%s%s%s\n", 1400 m_file.GetPath().c_str(), 1401 m_object_name ? "(" : "", 1402 m_object_name ? m_object_name.GetCString() : "", 1403 m_object_name ? ")" : ""); 1404 1405 s->IndentMore(); 1406 1407 ObjectFile *objfile = GetObjectFile (); 1408 if (objfile) 1409 objfile->Dump(s); 1410 1411 SymbolVendor *symbols = GetSymbolVendor (); 1412 if (symbols) 1413 symbols->Dump(s); 1414 1415 s->IndentLess(); 1416 } 1417 1418 TypeList* 1419 Module::GetTypeList () 1420 { 1421 SymbolVendor *symbols = GetSymbolVendor (); 1422 if (symbols) 1423 return &symbols->GetTypeList(); 1424 return nullptr; 1425 } 1426 1427 const ConstString & 1428 Module::GetObjectName() const 1429 { 1430 return m_object_name; 1431 } 1432 1433 ObjectFile * 1434 Module::GetObjectFile() 1435 { 1436 if (!m_did_load_objfile.load()) 1437 { 1438 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1439 if (!m_did_load_objfile.load()) 1440 { 1441 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 1442 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); 1443 DataBufferSP data_sp; 1444 lldb::offset_t data_offset = 0; 1445 const lldb::offset_t file_size = m_file.GetByteSize(); 1446 if (file_size > m_object_offset) 1447 { 1448 m_did_load_objfile = true; 1449 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(), 1450 &m_file, 1451 m_object_offset, 1452 file_size - m_object_offset, 1453 data_sp, 1454 data_offset); 1455 if (m_objfile_sp) 1456 { 1457 // Once we get the object file, update our module with the object file's 1458 // architecture since it might differ in vendor/os if some parts were 1459 // unknown. But since the matching arch might already be more specific 1460 // than the generic COFF architecture, only merge in those values that 1461 // overwrite unspecified unknown values. 1462 ArchSpec new_arch; 1463 m_objfile_sp->GetArchitecture(new_arch); 1464 m_arch.MergeFrom(new_arch); 1465 } 1466 else 1467 { 1468 ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str()); 1469 } 1470 } 1471 } 1472 } 1473 return m_objfile_sp.get(); 1474 } 1475 1476 SectionList * 1477 Module::GetSectionList() 1478 { 1479 // Populate m_unified_sections_ap with sections from objfile. 1480 if (!m_sections_ap) 1481 { 1482 ObjectFile *obj_file = GetObjectFile(); 1483 if (obj_file != nullptr) 1484 obj_file->CreateSections(*GetUnifiedSectionList()); 1485 } 1486 return m_sections_ap.get(); 1487 } 1488 1489 void 1490 Module::SectionFileAddressesChanged () 1491 { 1492 ObjectFile *obj_file = GetObjectFile (); 1493 if (obj_file) 1494 obj_file->SectionFileAddressesChanged (); 1495 SymbolVendor* sym_vendor = GetSymbolVendor(); 1496 if (sym_vendor != nullptr) 1497 sym_vendor->SectionFileAddressesChanged (); 1498 } 1499 1500 SectionList * 1501 Module::GetUnifiedSectionList() 1502 { 1503 // Populate m_unified_sections_ap with sections from objfile. 1504 if (!m_sections_ap) 1505 m_sections_ap.reset(new SectionList()); 1506 return m_sections_ap.get(); 1507 } 1508 1509 const Symbol * 1510 Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) 1511 { 1512 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 1513 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1514 name.AsCString(), 1515 symbol_type); 1516 SymbolVendor* sym_vendor = GetSymbolVendor(); 1517 if (sym_vendor) 1518 { 1519 Symtab *symtab = sym_vendor->GetSymtab(); 1520 if (symtab) 1521 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1522 } 1523 return nullptr; 1524 } 1525 void 1526 Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) 1527 { 1528 // No need to protect this call using m_mutex all other method calls are 1529 // already thread safe. 1530 1531 size_t num_indices = symbol_indexes.size(); 1532 if (num_indices > 0) 1533 { 1534 SymbolContext sc; 1535 CalculateSymbolContext (&sc); 1536 for (size_t i = 0; i < num_indices; i++) 1537 { 1538 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); 1539 if (sc.symbol) 1540 sc_list.Append (sc); 1541 } 1542 } 1543 } 1544 1545 size_t 1546 Module::FindFunctionSymbols (const ConstString &name, 1547 uint32_t name_type_mask, 1548 SymbolContextList& sc_list) 1549 { 1550 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 1551 "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)", 1552 name.AsCString(), 1553 name_type_mask); 1554 SymbolVendor* sym_vendor = GetSymbolVendor(); 1555 if (sym_vendor) 1556 { 1557 Symtab *symtab = sym_vendor->GetSymtab(); 1558 if (symtab) 1559 return symtab->FindFunctionSymbols (name, name_type_mask, sc_list); 1560 } 1561 return 0; 1562 } 1563 1564 size_t 1565 Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) 1566 { 1567 // No need to protect this call using m_mutex all other method calls are 1568 // already thread safe. 1569 1570 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 1571 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1572 name.AsCString(), 1573 symbol_type); 1574 const size_t initial_size = sc_list.GetSize(); 1575 SymbolVendor* sym_vendor = GetSymbolVendor(); 1576 if (sym_vendor) 1577 { 1578 Symtab *symtab = sym_vendor->GetSymtab(); 1579 if (symtab) 1580 { 1581 std::vector<uint32_t> symbol_indexes; 1582 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); 1583 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1584 } 1585 } 1586 return sc_list.GetSize() - initial_size; 1587 } 1588 1589 size_t 1590 Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) 1591 { 1592 // No need to protect this call using m_mutex all other method calls are 1593 // already thread safe. 1594 1595 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 1596 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 1597 regex.GetText(), 1598 symbol_type); 1599 const size_t initial_size = sc_list.GetSize(); 1600 SymbolVendor* sym_vendor = GetSymbolVendor(); 1601 if (sym_vendor) 1602 { 1603 Symtab *symtab = sym_vendor->GetSymtab(); 1604 if (symtab) 1605 { 1606 std::vector<uint32_t> symbol_indexes; 1607 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 1608 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1609 } 1610 } 1611 return sc_list.GetSize() - initial_size; 1612 } 1613 1614 void 1615 Module::SetSymbolFileFileSpec (const FileSpec &file) 1616 { 1617 if (!file.Exists()) 1618 return; 1619 if (m_symfile_ap) 1620 { 1621 // Remove any sections in the unified section list that come from the current symbol vendor. 1622 SectionList *section_list = GetSectionList(); 1623 SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile(); 1624 if (section_list && symbol_file) 1625 { 1626 ObjectFile *obj_file = symbol_file->GetObjectFile(); 1627 // Make sure we have an object file and that the symbol vendor's objfile isn't 1628 // the same as the module's objfile before we remove any sections for it... 1629 if (obj_file) 1630 { 1631 // Check to make sure we aren't trying to specify the file we already have 1632 if (obj_file->GetFileSpec() == file) 1633 { 1634 // We are being told to add the exact same file that we already have 1635 // we don't have to do anything. 1636 return; 1637 } 1638 1639 // Cleare the current symtab as we are going to replace it with a new one 1640 obj_file->ClearSymtab(); 1641 1642 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead 1643 // of a full path to the symbol file within the bundle 1644 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this 1645 1646 if (file.IsDirectory()) 1647 { 1648 std::string new_path(file.GetPath()); 1649 std::string old_path(obj_file->GetFileSpec().GetPath()); 1650 if (old_path.find(new_path) == 0) 1651 { 1652 // We specified the same bundle as the symbol file that we already have 1653 return; 1654 } 1655 } 1656 1657 if (obj_file != m_objfile_sp.get()) 1658 { 1659 size_t num_sections = section_list->GetNumSections (0); 1660 for (size_t idx = num_sections; idx > 0; --idx) 1661 { 1662 lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1)); 1663 if (section_sp->GetObjectFile() == obj_file) 1664 { 1665 section_list->DeleteSection (idx - 1); 1666 } 1667 } 1668 } 1669 } 1670 } 1671 // Keep all old symbol files around in case there are any lingering type references in 1672 // any SBValue objects that might have been handed out. 1673 m_old_symfiles.push_back(std::move(m_symfile_ap)); 1674 } 1675 m_symfile_spec = file; 1676 m_symfile_ap.reset(); 1677 m_did_load_symbol_vendor = false; 1678 } 1679 1680 bool 1681 Module::IsExecutable () 1682 { 1683 if (GetObjectFile() == nullptr) 1684 return false; 1685 else 1686 return GetObjectFile()->IsExecutable(); 1687 } 1688 1689 bool 1690 Module::IsLoadedInTarget (Target *target) 1691 { 1692 ObjectFile *obj_file = GetObjectFile(); 1693 if (obj_file) 1694 { 1695 SectionList *sections = GetSectionList(); 1696 if (sections != nullptr) 1697 { 1698 size_t num_sections = sections->GetSize(); 1699 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) 1700 { 1701 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1702 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) 1703 { 1704 return true; 1705 } 1706 } 1707 } 1708 } 1709 return false; 1710 } 1711 1712 bool 1713 Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream) 1714 { 1715 if (!target) 1716 { 1717 error.SetErrorString("invalid destination Target"); 1718 return false; 1719 } 1720 1721 LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile(); 1722 1723 if (should_load == eLoadScriptFromSymFileFalse) 1724 return false; 1725 1726 Debugger &debugger = target->GetDebugger(); 1727 const ScriptLanguage script_language = debugger.GetScriptLanguage(); 1728 if (script_language != eScriptLanguageNone) 1729 { 1730 1731 PlatformSP platform_sp(target->GetPlatform()); 1732 1733 if (!platform_sp) 1734 { 1735 error.SetErrorString("invalid Platform"); 1736 return false; 1737 } 1738 1739 FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target, 1740 *this, 1741 feedback_stream); 1742 1743 const uint32_t num_specs = file_specs.GetSize(); 1744 if (num_specs) 1745 { 1746 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); 1747 if (script_interpreter) 1748 { 1749 for (uint32_t i = 0; i < num_specs; ++i) 1750 { 1751 FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i)); 1752 if (scripting_fspec && scripting_fspec.Exists()) 1753 { 1754 if (should_load == eLoadScriptFromSymFileWarn) 1755 { 1756 if (feedback_stream) 1757 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in " 1758 "this debug session:\n\n command script import \"%s\"\n\n" 1759 "To run all discovered debug scripts in this session:\n\n" 1760 " settings set target.load-script-from-symbol-file true\n", 1761 GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1762 scripting_fspec.GetPath().c_str()); 1763 return false; 1764 } 1765 StreamString scripting_stream; 1766 scripting_fspec.Dump(&scripting_stream); 1767 const bool can_reload = true; 1768 const bool init_lldb_globals = false; 1769 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), 1770 can_reload, 1771 init_lldb_globals, 1772 error); 1773 if (!did_load) 1774 return false; 1775 } 1776 } 1777 } 1778 else 1779 { 1780 error.SetErrorString("invalid ScriptInterpreter"); 1781 return false; 1782 } 1783 } 1784 } 1785 return true; 1786 } 1787 1788 bool 1789 Module::SetArchitecture (const ArchSpec &new_arch) 1790 { 1791 if (!m_arch.IsValid()) 1792 { 1793 m_arch = new_arch; 1794 return true; 1795 } 1796 return m_arch.IsCompatibleMatch(new_arch); 1797 } 1798 1799 bool 1800 Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed) 1801 { 1802 ObjectFile *object_file = GetObjectFile(); 1803 if (object_file != nullptr) 1804 { 1805 changed = object_file->SetLoadAddress(target, value, value_is_offset); 1806 return true; 1807 } 1808 else 1809 { 1810 changed = false; 1811 } 1812 return false; 1813 } 1814 1815 1816 bool 1817 Module::MatchesModuleSpec (const ModuleSpec &module_ref) 1818 { 1819 const UUID &uuid = module_ref.GetUUID(); 1820 1821 if (uuid.IsValid()) 1822 { 1823 // If the UUID matches, then nothing more needs to match... 1824 return (uuid == GetUUID()); 1825 } 1826 1827 const FileSpec &file_spec = module_ref.GetFileSpec(); 1828 if (file_spec) 1829 { 1830 if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()) && 1831 !FileSpec::Equal (file_spec, m_platform_file, (bool)file_spec.GetDirectory())) 1832 return false; 1833 } 1834 1835 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1836 if (platform_file_spec) 1837 { 1838 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory())) 1839 return false; 1840 } 1841 1842 const ArchSpec &arch = module_ref.GetArchitecture(); 1843 if (arch.IsValid()) 1844 { 1845 if (!m_arch.IsCompatibleMatch(arch)) 1846 return false; 1847 } 1848 1849 const ConstString &object_name = module_ref.GetObjectName(); 1850 if (object_name) 1851 { 1852 if (object_name != GetObjectName()) 1853 return false; 1854 } 1855 return true; 1856 } 1857 1858 bool 1859 Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 1860 { 1861 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1862 return m_source_mappings.FindFile (orig_spec, new_spec); 1863 } 1864 1865 bool 1866 Module::RemapSourceFile (const char *path, std::string &new_path) const 1867 { 1868 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1869 return m_source_mappings.RemapPath(path, new_path); 1870 } 1871 1872 uint32_t 1873 Module::GetVersion (uint32_t *versions, uint32_t num_versions) 1874 { 1875 ObjectFile *obj_file = GetObjectFile(); 1876 if (obj_file) 1877 return obj_file->GetVersion (versions, num_versions); 1878 1879 if (versions != nullptr && num_versions != 0) 1880 { 1881 for (uint32_t i = 0; i < num_versions; ++i) 1882 versions[i] = LLDB_INVALID_MODULE_VERSION; 1883 } 1884 return 0; 1885 } 1886 1887 ModuleSP 1888 Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp) 1889 { 1890 if (delegate_sp) 1891 { 1892 // Must create a module and place it into a shared pointer before 1893 // we can create an object file since it has a std::weak_ptr back 1894 // to the module, so we need to control the creation carefully in 1895 // this static function 1896 ModuleSP module_sp(new Module()); 1897 module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp)); 1898 if (module_sp->m_objfile_sp) 1899 { 1900 // Once we get the object file, update our module with the object file's 1901 // architecture since it might differ in vendor/os if some parts were 1902 // unknown. 1903 module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch); 1904 } 1905 return module_sp; 1906 } 1907 return ModuleSP(); 1908 } 1909 1910 bool 1911 Module::GetIsDynamicLinkEditor() 1912 { 1913 ObjectFile * obj_file = GetObjectFile (); 1914 1915 if (obj_file) 1916 return obj_file->GetIsDynamicLinkEditor(); 1917 1918 return false; 1919 } 1920