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