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