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