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