Module.cpp revision 40e278caddf5442776f9408f20d757cc85fe630a
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/Error.h" 13#include "lldb/Core/Module.h" 14#include "lldb/Core/DataBuffer.h" 15#include "lldb/Core/DataBufferHeap.h" 16#include "lldb/Core/Log.h" 17#include "lldb/Core/ModuleList.h" 18#include "lldb/Core/ModuleSpec.h" 19#include "lldb/Core/RegularExpression.h" 20#include "lldb/Core/Section.h" 21#include "lldb/Core/StreamString.h" 22#include "lldb/Core/Timer.h" 23#include "lldb/Host/Host.h" 24#include "lldb/Host/Symbols.h" 25#include "lldb/Interpreter/CommandInterpreter.h" 26#include "lldb/Interpreter/ScriptInterpreter.h" 27#include "lldb/lldb-private-log.h" 28#include "lldb/Symbol/CompileUnit.h" 29#include "lldb/Symbol/ObjectFile.h" 30#include "lldb/Symbol/SymbolContext.h" 31#include "lldb/Symbol/SymbolVendor.h" 32#include "lldb/Target/Process.h" 33#include "lldb/Target/Target.h" 34 35using namespace lldb; 36using namespace lldb_private; 37 38// Shared pointers to modules track module lifetimes in 39// targets and in the global module, but this collection 40// will track all module objects that are still alive 41typedef std::vector<Module *> ModuleCollection; 42 43static ModuleCollection & 44GetModuleCollection() 45{ 46 // This module collection needs to live past any module, so we could either make it a 47 // shared pointer in each module or just leak is. Since it is only an empty vector by 48 // the time all the modules have gone away, we just leak it for now. If we decide this 49 // is a big problem we can introduce a Finalize method that will tear everything down in 50 // a predictable order. 51 52 static ModuleCollection *g_module_collection = NULL; 53 if (g_module_collection == NULL) 54 g_module_collection = new ModuleCollection(); 55 56 return *g_module_collection; 57} 58 59Mutex * 60Module::GetAllocationModuleCollectionMutex() 61{ 62 // NOTE: The mutex below must be leaked since the global module list in 63 // the ModuleList class will get torn at some point, and we can't know 64 // if it will tear itself down before the "g_module_collection_mutex" below 65 // will. So we leak a Mutex object below to safeguard against that 66 67 static Mutex *g_module_collection_mutex = NULL; 68 if (g_module_collection_mutex == NULL) 69 g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak 70 return g_module_collection_mutex; 71} 72 73size_t 74Module::GetNumberAllocatedModules () 75{ 76 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 77 return GetModuleCollection().size(); 78} 79 80Module * 81Module::GetAllocatedModuleAtIndex (size_t idx) 82{ 83 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 84 ModuleCollection &modules = GetModuleCollection(); 85 if (idx < modules.size()) 86 return modules[idx]; 87 return NULL; 88} 89#if 0 90 91// These functions help us to determine if modules are still loaded, yet don't require that 92// you have a command interpreter and can easily be called from an external debugger. 93namespace lldb { 94 95 void 96 ClearModuleInfo (void) 97 { 98 const bool mandatory = true; 99 ModuleList::RemoveOrphanSharedModules(mandatory); 100 } 101 102 void 103 DumpModuleInfo (void) 104 { 105 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex()); 106 ModuleCollection &modules = GetModuleCollection(); 107 const size_t count = modules.size(); 108 printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count); 109 for (size_t i=0; i<count; ++i) 110 { 111 112 StreamString strm; 113 Module *module = modules[i]; 114 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module); 115 module->GetDescription(&strm, eDescriptionLevelFull); 116 printf ("%p: shared = %i, ref_count = %3u, module = %s\n", 117 module, 118 in_shared_module_list, 119 (uint32_t)module->use_count(), 120 strm.GetString().c_str()); 121 } 122 } 123} 124 125#endif 126 127Module::Module (const ModuleSpec &module_spec) : 128 m_mutex (Mutex::eMutexTypeRecursive), 129 m_mod_time (module_spec.GetFileSpec().GetModificationTime()), 130 m_arch (module_spec.GetArchitecture()), 131 m_uuid (), 132 m_file (module_spec.GetFileSpec()), 133 m_platform_file(module_spec.GetPlatformFileSpec()), 134 m_symfile_spec (module_spec.GetSymbolFileSpec()), 135 m_object_name (module_spec.GetObjectName()), 136 m_object_offset (module_spec.GetObjectOffset()), 137 m_objfile_sp (), 138 m_symfile_ap (), 139 m_ast (), 140 m_source_mappings (), 141 m_did_load_objfile (false), 142 m_did_load_symbol_vendor (false), 143 m_did_parse_uuid (false), 144 m_did_init_ast (false), 145 m_is_dynamic_loader_module (false), 146 m_file_has_changed (false), 147 m_first_file_changed_log (false) 148{ 149 // Scope for locker below... 150 { 151 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 152 GetModuleCollection().push_back(this); 153 } 154 155 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 156 if (log) 157 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')", 158 this, 159 m_arch.GetArchitectureName(), 160 m_file.GetDirectory().AsCString(""), 161 m_file.GetFilename().AsCString(""), 162 m_object_name.IsEmpty() ? "" : "(", 163 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 164 m_object_name.IsEmpty() ? "" : ")"); 165} 166 167Module::Module(const FileSpec& file_spec, 168 const ArchSpec& arch, 169 const ConstString *object_name, 170 off_t object_offset) : 171 m_mutex (Mutex::eMutexTypeRecursive), 172 m_mod_time (file_spec.GetModificationTime()), 173 m_arch (arch), 174 m_uuid (), 175 m_file (file_spec), 176 m_platform_file(), 177 m_symfile_spec (), 178 m_object_name (), 179 m_object_offset (object_offset), 180 m_objfile_sp (), 181 m_symfile_ap (), 182 m_ast (), 183 m_source_mappings (), 184 m_did_load_objfile (false), 185 m_did_load_symbol_vendor (false), 186 m_did_parse_uuid (false), 187 m_did_init_ast (false), 188 m_is_dynamic_loader_module (false), 189 m_file_has_changed (false), 190 m_first_file_changed_log (false) 191{ 192 // Scope for locker below... 193 { 194 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 195 GetModuleCollection().push_back(this); 196 } 197 198 if (object_name) 199 m_object_name = *object_name; 200 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 201 if (log) 202 log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')", 203 this, 204 m_arch.GetArchitectureName(), 205 m_file.GetDirectory().AsCString(""), 206 m_file.GetFilename().AsCString(""), 207 m_object_name.IsEmpty() ? "" : "(", 208 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 209 m_object_name.IsEmpty() ? "" : ")"); 210} 211 212Module::~Module() 213{ 214 // Scope for locker below... 215 { 216 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 217 ModuleCollection &modules = GetModuleCollection(); 218 ModuleCollection::iterator end = modules.end(); 219 ModuleCollection::iterator pos = std::find(modules.begin(), end, this); 220 assert (pos != end); 221 modules.erase(pos); 222 } 223 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES)); 224 if (log) 225 log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')", 226 this, 227 m_arch.GetArchitectureName(), 228 m_file.GetDirectory().AsCString(""), 229 m_file.GetFilename().AsCString(""), 230 m_object_name.IsEmpty() ? "" : "(", 231 m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 232 m_object_name.IsEmpty() ? "" : ")"); 233 // Release any auto pointers before we start tearing down our member 234 // variables since the object file and symbol files might need to make 235 // function calls back into this module object. The ordering is important 236 // here because symbol files can require the module object file. So we tear 237 // down the symbol file first, then the object file. 238 m_symfile_ap.reset(); 239 m_objfile_sp.reset(); 240} 241 242ObjectFile * 243Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error) 244{ 245 if (m_objfile_sp) 246 { 247 error.SetErrorString ("object file already exists"); 248 } 249 else 250 { 251 Mutex::Locker locker (m_mutex); 252 if (process_sp) 253 { 254 m_did_load_objfile = true; 255 std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0)); 256 Error readmem_error; 257 const size_t bytes_read = process_sp->ReadMemory (header_addr, 258 data_ap->GetBytes(), 259 data_ap->GetByteSize(), 260 readmem_error); 261 if (bytes_read == 512) 262 { 263 DataBufferSP data_sp(data_ap.release()); 264 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp); 265 if (m_objfile_sp) 266 { 267 StreamString s; 268 s.Printf("0x%16.16" PRIx64, header_addr); 269 m_object_name.SetCString (s.GetData()); 270 271 // Once we get the object file, update our module with the object file's 272 // architecture since it might differ in vendor/os if some parts were 273 // unknown. 274 m_objfile_sp->GetArchitecture (m_arch); 275 } 276 else 277 { 278 error.SetErrorString ("unable to find suitable object file plug-in"); 279 } 280 } 281 else 282 { 283 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString()); 284 } 285 } 286 else 287 { 288 error.SetErrorString ("invalid process"); 289 } 290 } 291 return m_objfile_sp.get(); 292} 293 294 295const lldb_private::UUID& 296Module::GetUUID() 297{ 298 Mutex::Locker locker (m_mutex); 299 if (m_did_parse_uuid == false) 300 { 301 ObjectFile * obj_file = GetObjectFile (); 302 303 if (obj_file != NULL) 304 { 305 obj_file->GetUUID(&m_uuid); 306 m_did_parse_uuid = true; 307 } 308 } 309 return m_uuid; 310} 311 312ClangASTContext & 313Module::GetClangASTContext () 314{ 315 Mutex::Locker locker (m_mutex); 316 if (m_did_init_ast == false) 317 { 318 ObjectFile * objfile = GetObjectFile(); 319 ArchSpec object_arch; 320 if (objfile && objfile->GetArchitecture(object_arch)) 321 { 322 m_did_init_ast = true; 323 324 // LLVM wants this to be set to iOS or MacOSX; if we're working on 325 // a bare-boards type image, change the triple for llvm's benefit. 326 if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple 327 && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) 328 { 329 if (object_arch.GetTriple().getArch() == llvm::Triple::arm || 330 object_arch.GetTriple().getArch() == llvm::Triple::thumb) 331 { 332 object_arch.GetTriple().setOS(llvm::Triple::IOS); 333 } 334 else 335 { 336 object_arch.GetTriple().setOS(llvm::Triple::MacOSX); 337 } 338 } 339 m_ast.SetArchitecture (object_arch); 340 } 341 } 342 return m_ast; 343} 344 345void 346Module::ParseAllDebugSymbols() 347{ 348 Mutex::Locker locker (m_mutex); 349 uint32_t num_comp_units = GetNumCompileUnits(); 350 if (num_comp_units == 0) 351 return; 352 353 SymbolContext sc; 354 sc.module_sp = shared_from_this(); 355 uint32_t cu_idx; 356 SymbolVendor *symbols = GetSymbolVendor (); 357 358 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++) 359 { 360 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); 361 if (sc.comp_unit) 362 { 363 sc.function = NULL; 364 symbols->ParseVariablesForContext(sc); 365 366 symbols->ParseCompileUnitFunctions(sc); 367 368 uint32_t func_idx; 369 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx) 370 { 371 symbols->ParseFunctionBlocks(sc); 372 373 // Parse the variables for this function and all its blocks 374 symbols->ParseVariablesForContext(sc); 375 } 376 377 378 // Parse all types for this compile unit 379 sc.function = NULL; 380 symbols->ParseTypes(sc); 381 } 382 } 383} 384 385void 386Module::CalculateSymbolContext(SymbolContext* sc) 387{ 388 sc->module_sp = shared_from_this(); 389} 390 391ModuleSP 392Module::CalculateSymbolContextModule () 393{ 394 return shared_from_this(); 395} 396 397void 398Module::DumpSymbolContext(Stream *s) 399{ 400 s->Printf(", Module{%p}", this); 401} 402 403uint32_t 404Module::GetNumCompileUnits() 405{ 406 Mutex::Locker locker (m_mutex); 407 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this); 408 SymbolVendor *symbols = GetSymbolVendor (); 409 if (symbols) 410 return symbols->GetNumCompileUnits(); 411 return 0; 412} 413 414CompUnitSP 415Module::GetCompileUnitAtIndex (uint32_t index) 416{ 417 Mutex::Locker locker (m_mutex); 418 uint32_t num_comp_units = GetNumCompileUnits (); 419 CompUnitSP cu_sp; 420 421 if (index < num_comp_units) 422 { 423 SymbolVendor *symbols = GetSymbolVendor (); 424 if (symbols) 425 cu_sp = symbols->GetCompileUnitAtIndex(index); 426 } 427 return cu_sp; 428} 429 430bool 431Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) 432{ 433 Mutex::Locker locker (m_mutex); 434 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr); 435 ObjectFile* ofile = GetObjectFile(); 436 if (ofile) 437 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList()); 438 return false; 439} 440 441uint32_t 442Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 443{ 444 Mutex::Locker locker (m_mutex); 445 uint32_t resolved_flags = 0; 446 447 // Clear the result symbol context in case we don't find anything 448 sc.Clear(); 449 450 // Get the section from the section/offset address. 451 SectionSP section_sp (so_addr.GetSection()); 452 453 // Make sure the section matches this module before we try and match anything 454 if (section_sp && section_sp->GetModule().get() == this) 455 { 456 // If the section offset based address resolved itself, then this 457 // is the right module. 458 sc.module_sp = shared_from_this(); 459 resolved_flags |= eSymbolContextModule; 460 461 // Resolve the compile unit, function, block, line table or line 462 // entry if requested. 463 if (resolve_scope & eSymbolContextCompUnit || 464 resolve_scope & eSymbolContextFunction || 465 resolve_scope & eSymbolContextBlock || 466 resolve_scope & eSymbolContextLineEntry ) 467 { 468 SymbolVendor *symbols = GetSymbolVendor (); 469 if (symbols) 470 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc); 471 } 472 473 // Resolve the symbol if requested, but don't re-look it up if we've already found it. 474 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol)) 475 { 476 ObjectFile* ofile = GetObjectFile(); 477 if (ofile) 478 { 479 Symtab *symtab = ofile->GetSymtab(); 480 if (symtab) 481 { 482 if (so_addr.IsSectionOffset()) 483 { 484 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress()); 485 if (sc.symbol) 486 resolved_flags |= eSymbolContextSymbol; 487 } 488 } 489 } 490 } 491 } 492 return resolved_flags; 493} 494 495uint32_t 496Module::ResolveSymbolContextForFilePath 497( 498 const char *file_path, 499 uint32_t line, 500 bool check_inlines, 501 uint32_t resolve_scope, 502 SymbolContextList& sc_list 503) 504{ 505 FileSpec file_spec(file_path, false); 506 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 507} 508 509uint32_t 510Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 511{ 512 Mutex::Locker locker (m_mutex); 513 Timer scoped_timer(__PRETTY_FUNCTION__, 514 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)", 515 file_spec.GetDirectory().AsCString(""), 516 file_spec.GetDirectory() ? "/" : "", 517 file_spec.GetFilename().AsCString(""), 518 line, 519 check_inlines ? "yes" : "no", 520 resolve_scope); 521 522 const uint32_t initial_count = sc_list.GetSize(); 523 524 SymbolVendor *symbols = GetSymbolVendor (); 525 if (symbols) 526 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list); 527 528 return sc_list.GetSize() - initial_count; 529} 530 531 532uint32_t 533Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) 534{ 535 SymbolVendor *symbols = GetSymbolVendor (); 536 if (symbols) 537 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables); 538 return 0; 539} 540uint32_t 541Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 542{ 543 SymbolVendor *symbols = GetSymbolVendor (); 544 if (symbols) 545 return symbols->FindGlobalVariables(regex, append, max_matches, variables); 546 return 0; 547} 548 549uint32_t 550Module::FindCompileUnits (const FileSpec &path, 551 bool append, 552 SymbolContextList &sc_list) 553{ 554 if (!append) 555 sc_list.Clear(); 556 557 const uint32_t start_size = sc_list.GetSize(); 558 const uint32_t num_compile_units = GetNumCompileUnits(); 559 SymbolContext sc; 560 sc.module_sp = shared_from_this(); 561 const bool compare_directory = path.GetDirectory(); 562 for (uint32_t i=0; i<num_compile_units; ++i) 563 { 564 sc.comp_unit = GetCompileUnitAtIndex(i).get(); 565 if (sc.comp_unit) 566 { 567 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory)) 568 sc_list.Append(sc); 569 } 570 } 571 return sc_list.GetSize() - start_size; 572} 573 574uint32_t 575Module::FindFunctions (const ConstString &name, 576 const ClangNamespaceDecl *namespace_decl, 577 uint32_t name_type_mask, 578 bool include_symbols, 579 bool include_inlines, 580 bool append, 581 SymbolContextList& sc_list) 582{ 583 if (!append) 584 sc_list.Clear(); 585 586 const uint32_t start_size = sc_list.GetSize(); 587 588 // Find all the functions (not symbols, but debug information functions... 589 SymbolVendor *symbols = GetSymbolVendor (); 590 if (symbols) 591 symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list); 592 593 // Now check our symbol table for symbols that are code symbols if requested 594 if (include_symbols) 595 { 596 ObjectFile *objfile = GetObjectFile(); 597 if (objfile) 598 { 599 Symtab *symtab = objfile->GetSymtab(); 600 if (symtab) 601 { 602 std::vector<uint32_t> symbol_indexes; 603 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 604 const uint32_t num_matches = symbol_indexes.size(); 605 if (num_matches) 606 { 607 const bool merge_symbol_into_function = true; 608 SymbolContext sc(this); 609 for (uint32_t i=0; i<num_matches; i++) 610 { 611 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 612 sc_list.AppendIfUnique (sc, merge_symbol_into_function); 613 } 614 } 615 } 616 } 617 } 618 return sc_list.GetSize() - start_size; 619} 620 621uint32_t 622Module::FindFunctions (const RegularExpression& regex, 623 bool include_symbols, 624 bool include_inlines, 625 bool append, 626 SymbolContextList& sc_list) 627{ 628 if (!append) 629 sc_list.Clear(); 630 631 const uint32_t start_size = sc_list.GetSize(); 632 633 SymbolVendor *symbols = GetSymbolVendor (); 634 if (symbols) 635 symbols->FindFunctions(regex, include_inlines, append, sc_list); 636 // Now check our symbol table for symbols that are code symbols if requested 637 if (include_symbols) 638 { 639 ObjectFile *objfile = GetObjectFile(); 640 if (objfile) 641 { 642 Symtab *symtab = objfile->GetSymtab(); 643 if (symtab) 644 { 645 std::vector<uint32_t> symbol_indexes; 646 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 647 const uint32_t num_matches = symbol_indexes.size(); 648 if (num_matches) 649 { 650 const bool merge_symbol_into_function = true; 651 SymbolContext sc(this); 652 for (uint32_t i=0; i<num_matches; i++) 653 { 654 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 655 sc_list.AppendIfUnique (sc, merge_symbol_into_function); 656 } 657 } 658 } 659 } 660 } 661 return sc_list.GetSize() - start_size; 662} 663 664uint32_t 665Module::FindTypes_Impl (const SymbolContext& sc, 666 const ConstString &name, 667 const ClangNamespaceDecl *namespace_decl, 668 bool append, 669 uint32_t max_matches, 670 TypeList& types) 671{ 672 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 673 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this) 674 { 675 SymbolVendor *symbols = GetSymbolVendor (); 676 if (symbols) 677 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types); 678 } 679 return 0; 680} 681 682uint32_t 683Module::FindTypesInNamespace (const SymbolContext& sc, 684 const ConstString &type_name, 685 const ClangNamespaceDecl *namespace_decl, 686 uint32_t max_matches, 687 TypeList& type_list) 688{ 689 const bool append = true; 690 return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list); 691} 692 693lldb::TypeSP 694Module::FindFirstType (const SymbolContext& sc, 695 const ConstString &name, 696 bool exact_match) 697{ 698 TypeList type_list; 699 const uint32_t num_matches = FindTypes (sc, name, exact_match, 1, type_list); 700 if (num_matches) 701 return type_list.GetTypeAtIndex(0); 702 return TypeSP(); 703} 704 705 706uint32_t 707Module::FindTypes (const SymbolContext& sc, 708 const ConstString &name, 709 bool exact_match, 710 uint32_t max_matches, 711 TypeList& types) 712{ 713 uint32_t num_matches = 0; 714 const char *type_name_cstr = name.GetCString(); 715 std::string type_scope; 716 std::string type_basename; 717 const bool append = true; 718 TypeClass type_class = eTypeClassAny; 719 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class)) 720 { 721 // Check if "name" starts with "::" which means the qualified type starts 722 // from the root namespace and implies and exact match. The typenames we 723 // get back from clang do not start with "::" so we need to strip this off 724 // in order to get the qualfied names to match 725 726 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':') 727 { 728 type_scope.erase(0,2); 729 exact_match = true; 730 } 731 ConstString type_basename_const_str (type_basename.c_str()); 732 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types)) 733 { 734 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match); 735 num_matches = types.GetSize(); 736 } 737 } 738 else 739 { 740 // The type is not in a namespace/class scope, just search for it by basename 741 if (type_class != eTypeClassAny) 742 { 743 // The "type_name_cstr" will have been modified if we have a valid type class 744 // prefix (like "struct", "class", "union", "typedef" etc). 745 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types); 746 types.RemoveMismatchedTypes (type_class); 747 num_matches = types.GetSize(); 748 } 749 else 750 { 751 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types); 752 } 753 } 754 755 return num_matches; 756 757} 758 759//uint32_t 760//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types) 761//{ 762// Timer scoped_timer(__PRETTY_FUNCTION__); 763// SymbolVendor *symbols = GetSymbolVendor (); 764// if (symbols) 765// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types); 766// return 0; 767// 768//} 769 770SymbolVendor* 771Module::GetSymbolVendor (bool can_create) 772{ 773 Mutex::Locker locker (m_mutex); 774 if (m_did_load_symbol_vendor == false && can_create) 775 { 776 ObjectFile *obj_file = GetObjectFile (); 777 if (obj_file != NULL) 778 { 779 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 780 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this())); 781 m_did_load_symbol_vendor = true; 782 } 783 } 784 return m_symfile_ap.get(); 785} 786 787void 788Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) 789{ 790 // Container objects whose paths do not specify a file directly can call 791 // this function to correct the file and object names. 792 m_file = file; 793 m_mod_time = file.GetModificationTime(); 794 m_object_name = object_name; 795} 796 797const ArchSpec& 798Module::GetArchitecture () const 799{ 800 return m_arch; 801} 802 803void 804Module::GetDescription (Stream *s, lldb::DescriptionLevel level) 805{ 806 Mutex::Locker locker (m_mutex); 807 808 if (level >= eDescriptionLevelFull) 809 { 810 if (m_arch.IsValid()) 811 s->Printf("(%s) ", m_arch.GetArchitectureName()); 812 } 813 814 if (level == eDescriptionLevelBrief) 815 { 816 const char *filename = m_file.GetFilename().GetCString(); 817 if (filename) 818 s->PutCString (filename); 819 } 820 else 821 { 822 char path[PATH_MAX]; 823 if (m_file.GetPath(path, sizeof(path))) 824 s->PutCString(path); 825 } 826 827 const char *object_name = m_object_name.GetCString(); 828 if (object_name) 829 s->Printf("(%s)", object_name); 830} 831 832void 833Module::ReportError (const char *format, ...) 834{ 835 if (format && format[0]) 836 { 837 StreamString strm; 838 strm.PutCString("error: "); 839 GetDescription(&strm, lldb::eDescriptionLevelBrief); 840 strm.PutChar (' '); 841 va_list args; 842 va_start (args, format); 843 strm.PrintfVarArg(format, args); 844 va_end (args); 845 846 const int format_len = strlen(format); 847 if (format_len > 0) 848 { 849 const char last_char = format[format_len-1]; 850 if (last_char != '\n' || last_char != '\r') 851 strm.EOL(); 852 } 853 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 854 855 } 856} 857 858bool 859Module::FileHasChanged () const 860{ 861 if (m_file_has_changed == false) 862 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time); 863 return m_file_has_changed; 864} 865 866void 867Module::ReportErrorIfModifyDetected (const char *format, ...) 868{ 869 if (m_first_file_changed_log == false) 870 { 871 if (FileHasChanged ()) 872 { 873 m_first_file_changed_log = true; 874 if (format) 875 { 876 StreamString strm; 877 strm.PutCString("error: the object file "); 878 GetDescription(&strm, lldb::eDescriptionLevelFull); 879 strm.PutCString (" has been modified\n"); 880 881 va_list args; 882 va_start (args, format); 883 strm.PrintfVarArg(format, args); 884 va_end (args); 885 886 const int format_len = strlen(format); 887 if (format_len > 0) 888 { 889 const char last_char = format[format_len-1]; 890 if (last_char != '\n' || last_char != '\r') 891 strm.EOL(); 892 } 893 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); 894 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 895 } 896 } 897 } 898} 899 900void 901Module::ReportWarning (const char *format, ...) 902{ 903 if (format && format[0]) 904 { 905 StreamString strm; 906 strm.PutCString("warning: "); 907 GetDescription(&strm, lldb::eDescriptionLevelFull); 908 strm.PutChar (' '); 909 910 va_list args; 911 va_start (args, format); 912 strm.PrintfVarArg(format, args); 913 va_end (args); 914 915 const int format_len = strlen(format); 916 if (format_len > 0) 917 { 918 const char last_char = format[format_len-1]; 919 if (last_char != '\n' || last_char != '\r') 920 strm.EOL(); 921 } 922 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); 923 } 924} 925 926void 927Module::LogMessage (Log *log, const char *format, ...) 928{ 929 if (log) 930 { 931 StreamString log_message; 932 GetDescription(&log_message, lldb::eDescriptionLevelFull); 933 log_message.PutCString (": "); 934 va_list args; 935 va_start (args, format); 936 log_message.PrintfVarArg (format, args); 937 va_end (args); 938 log->PutCString(log_message.GetString().c_str()); 939 } 940} 941 942void 943Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...) 944{ 945 if (log) 946 { 947 StreamString log_message; 948 GetDescription(&log_message, lldb::eDescriptionLevelFull); 949 log_message.PutCString (": "); 950 va_list args; 951 va_start (args, format); 952 log_message.PrintfVarArg (format, args); 953 va_end (args); 954 if (log->GetVerbose()) 955 Host::Backtrace (log_message, 1024); 956 log->PutCString(log_message.GetString().c_str()); 957 } 958} 959 960void 961Module::Dump(Stream *s) 962{ 963 Mutex::Locker locker (m_mutex); 964 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 965 s->Indent(); 966 s->Printf("Module %s/%s%s%s%s\n", 967 m_file.GetDirectory().AsCString(), 968 m_file.GetFilename().AsCString(), 969 m_object_name ? "(" : "", 970 m_object_name ? m_object_name.GetCString() : "", 971 m_object_name ? ")" : ""); 972 973 s->IndentMore(); 974 ObjectFile *objfile = GetObjectFile (); 975 976 if (objfile) 977 objfile->Dump(s); 978 979 SymbolVendor *symbols = GetSymbolVendor (); 980 981 if (symbols) 982 symbols->Dump(s); 983 984 s->IndentLess(); 985} 986 987 988TypeList* 989Module::GetTypeList () 990{ 991 SymbolVendor *symbols = GetSymbolVendor (); 992 if (symbols) 993 return &symbols->GetTypeList(); 994 return NULL; 995} 996 997const ConstString & 998Module::GetObjectName() const 999{ 1000 return m_object_name; 1001} 1002 1003ObjectFile * 1004Module::GetObjectFile() 1005{ 1006 Mutex::Locker locker (m_mutex); 1007 if (m_did_load_objfile == false) 1008 { 1009 m_did_load_objfile = true; 1010 Timer scoped_timer(__PRETTY_FUNCTION__, 1011 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); 1012 DataBufferSP file_data_sp; 1013 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(), 1014 &m_file, 1015 m_object_offset, 1016 m_file.GetByteSize(), 1017 file_data_sp); 1018 if (m_objfile_sp) 1019 { 1020 // Once we get the object file, update our module with the object file's 1021 // architecture since it might differ in vendor/os if some parts were 1022 // unknown. 1023 m_objfile_sp->GetArchitecture (m_arch); 1024 } 1025 } 1026 return m_objfile_sp.get(); 1027} 1028 1029 1030const Symbol * 1031Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) 1032{ 1033 Timer scoped_timer(__PRETTY_FUNCTION__, 1034 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1035 name.AsCString(), 1036 symbol_type); 1037 ObjectFile *objfile = GetObjectFile(); 1038 if (objfile) 1039 { 1040 Symtab *symtab = objfile->GetSymtab(); 1041 if (symtab) 1042 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1043 } 1044 return NULL; 1045} 1046void 1047Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) 1048{ 1049 // No need to protect this call using m_mutex all other method calls are 1050 // already thread safe. 1051 1052 size_t num_indices = symbol_indexes.size(); 1053 if (num_indices > 0) 1054 { 1055 SymbolContext sc; 1056 CalculateSymbolContext (&sc); 1057 for (size_t i = 0; i < num_indices; i++) 1058 { 1059 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); 1060 if (sc.symbol) 1061 sc_list.Append (sc); 1062 } 1063 } 1064} 1065 1066size_t 1067Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) 1068{ 1069 // No need to protect this call using m_mutex all other method calls are 1070 // already thread safe. 1071 1072 1073 Timer scoped_timer(__PRETTY_FUNCTION__, 1074 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1075 name.AsCString(), 1076 symbol_type); 1077 const size_t initial_size = sc_list.GetSize(); 1078 ObjectFile *objfile = GetObjectFile (); 1079 if (objfile) 1080 { 1081 Symtab *symtab = objfile->GetSymtab(); 1082 if (symtab) 1083 { 1084 std::vector<uint32_t> symbol_indexes; 1085 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); 1086 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1087 } 1088 } 1089 return sc_list.GetSize() - initial_size; 1090} 1091 1092size_t 1093Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) 1094{ 1095 // No need to protect this call using m_mutex all other method calls are 1096 // already thread safe. 1097 1098 Timer scoped_timer(__PRETTY_FUNCTION__, 1099 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 1100 regex.GetText(), 1101 symbol_type); 1102 const size_t initial_size = sc_list.GetSize(); 1103 ObjectFile *objfile = GetObjectFile (); 1104 if (objfile) 1105 { 1106 Symtab *symtab = objfile->GetSymtab(); 1107 if (symtab) 1108 { 1109 std::vector<uint32_t> symbol_indexes; 1110 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 1111 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1112 } 1113 } 1114 return sc_list.GetSize() - initial_size; 1115} 1116 1117const TimeValue & 1118Module::GetModificationTime () const 1119{ 1120 return m_mod_time; 1121} 1122 1123bool 1124Module::IsExecutable () 1125{ 1126 if (GetObjectFile() == NULL) 1127 return false; 1128 else 1129 return GetObjectFile()->IsExecutable(); 1130} 1131 1132bool 1133Module::IsLoadedInTarget (Target *target) 1134{ 1135 ObjectFile *obj_file = GetObjectFile(); 1136 if (obj_file) 1137 { 1138 SectionList *sections = obj_file->GetSectionList(); 1139 if (sections != NULL) 1140 { 1141 size_t num_sections = sections->GetSize(); 1142 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) 1143 { 1144 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1145 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) 1146 { 1147 return true; 1148 } 1149 } 1150 } 1151 } 1152 return false; 1153} 1154 1155bool 1156Module::LoadScriptingResourceInTarget (Target *target, Error& error) 1157{ 1158 if (!target) 1159 { 1160 error.SetErrorString("invalid destination Target"); 1161 return false; 1162 } 1163 1164 PlatformSP platform_sp(target->GetPlatform()); 1165 1166 if (!platform_sp) 1167 { 1168 error.SetErrorString("invalid Platform"); 1169 return false; 1170 } 1171 1172 ModuleSpec module_spec(GetFileSpec()); 1173 FileSpec scripting_fspec = platform_sp->LocateExecutableScriptingResource(module_spec); 1174 Debugger &debugger(target->GetDebugger()); 1175 if (scripting_fspec && scripting_fspec.Exists()) 1176 { 1177 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); 1178 if (script_interpreter) 1179 { 1180 StreamString scripting_stream; 1181 scripting_fspec.Dump(&scripting_stream); 1182 const bool can_reload = false; 1183 const bool init_lldb_globals = false; 1184 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), can_reload, init_lldb_globals, error); 1185 if (!did_load) 1186 return false; 1187 } 1188 else 1189 { 1190 error.SetErrorString("invalid ScriptInterpreter"); 1191 return false; 1192 } 1193 } 1194 return true; 1195} 1196 1197bool 1198Module::SetArchitecture (const ArchSpec &new_arch) 1199{ 1200 if (!m_arch.IsValid()) 1201 { 1202 m_arch = new_arch; 1203 return true; 1204 } 1205 return m_arch.IsExactMatch(new_arch); 1206} 1207 1208bool 1209Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed) 1210{ 1211 size_t num_loaded_sections = 0; 1212 ObjectFile *objfile = GetObjectFile(); 1213 if (objfile) 1214 { 1215 SectionList *section_list = objfile->GetSectionList (); 1216 if (section_list) 1217 { 1218 const size_t num_sections = section_list->GetSize(); 1219 size_t sect_idx = 0; 1220 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) 1221 { 1222 // Iterate through the object file sections to find the 1223 // first section that starts of file offset zero and that 1224 // has bytes in the file... 1225 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx)); 1226 // Only load non-thread specific sections when given a slide 1227 if (section_sp && !section_sp->IsThreadSpecific()) 1228 { 1229 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset)) 1230 ++num_loaded_sections; 1231 } 1232 } 1233 } 1234 } 1235 changed = num_loaded_sections > 0; 1236 return num_loaded_sections > 0; 1237} 1238 1239 1240bool 1241Module::MatchesModuleSpec (const ModuleSpec &module_ref) 1242{ 1243 const UUID &uuid = module_ref.GetUUID(); 1244 1245 if (uuid.IsValid()) 1246 { 1247 // If the UUID matches, then nothing more needs to match... 1248 if (uuid == GetUUID()) 1249 return true; 1250 else 1251 return false; 1252 } 1253 1254 const FileSpec &file_spec = module_ref.GetFileSpec(); 1255 if (file_spec) 1256 { 1257 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory())) 1258 return false; 1259 } 1260 1261 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1262 if (platform_file_spec) 1263 { 1264 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory())) 1265 return false; 1266 } 1267 1268 const ArchSpec &arch = module_ref.GetArchitecture(); 1269 if (arch.IsValid()) 1270 { 1271 if (!m_arch.IsCompatibleMatch(arch)) 1272 return false; 1273 } 1274 1275 const ConstString &object_name = module_ref.GetObjectName(); 1276 if (object_name) 1277 { 1278 if (object_name != GetObjectName()) 1279 return false; 1280 } 1281 return true; 1282} 1283 1284bool 1285Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 1286{ 1287 Mutex::Locker locker (m_mutex); 1288 return m_source_mappings.FindFile (orig_spec, new_spec); 1289} 1290 1291bool 1292Module::RemapSourceFile (const char *path, std::string &new_path) const 1293{ 1294 Mutex::Locker locker (m_mutex); 1295 return m_source_mappings.RemapPath(path, new_path); 1296} 1297 1298uint32_t 1299Module::GetVersion (uint32_t *versions, uint32_t num_versions) 1300{ 1301 ObjectFile *obj_file = GetObjectFile(); 1302 if (obj_file) 1303 return obj_file->GetVersion (versions, num_versions); 1304 1305 if (versions && num_versions) 1306 { 1307 for (uint32_t i=0; i<num_versions; ++i) 1308 versions[i] = UINT32_MAX; 1309 } 1310 return 0; 1311} 1312