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