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