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