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