Module.cpp revision d891f9b872103235cfd2ed452c6f14a4394d9b3a
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 693uint32_t 694Module::FindTypes (const SymbolContext& sc, 695 const ConstString &name, 696 bool exact_match, 697 uint32_t max_matches, 698 TypeList& types) 699{ 700 uint32_t num_matches = 0; 701 const char *type_name_cstr = name.GetCString(); 702 std::string type_scope; 703 std::string type_basename; 704 const bool append = true; 705 TypeClass type_class = eTypeClassAny; 706 if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class)) 707 { 708 // Check if "name" starts with "::" which means the qualified type starts 709 // from the root namespace and implies and exact match. The typenames we 710 // get back from clang do not start with "::" so we need to strip this off 711 // in order to get the qualfied names to match 712 713 if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':') 714 { 715 type_scope.erase(0,2); 716 exact_match = true; 717 } 718 ConstString type_basename_const_str (type_basename.c_str()); 719 if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types)) 720 { 721 types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match); 722 num_matches = types.GetSize(); 723 } 724 } 725 else 726 { 727 // The type is not in a namespace/class scope, just search for it by basename 728 if (type_class != eTypeClassAny) 729 { 730 // The "type_name_cstr" will have been modified if we have a valid type class 731 // prefix (like "struct", "class", "union", "typedef" etc). 732 num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types); 733 types.RemoveMismatchedTypes (type_class); 734 num_matches = types.GetSize(); 735 } 736 else 737 { 738 num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types); 739 } 740 } 741 742 return num_matches; 743 744} 745 746//uint32_t 747//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types) 748//{ 749// Timer scoped_timer(__PRETTY_FUNCTION__); 750// SymbolVendor *symbols = GetSymbolVendor (); 751// if (symbols) 752// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types); 753// return 0; 754// 755//} 756 757SymbolVendor* 758Module::GetSymbolVendor (bool can_create) 759{ 760 Mutex::Locker locker (m_mutex); 761 if (m_did_load_symbol_vendor == false && can_create) 762 { 763 ObjectFile *obj_file = GetObjectFile (); 764 if (obj_file != NULL) 765 { 766 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 767 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this())); 768 m_did_load_symbol_vendor = true; 769 } 770 } 771 return m_symfile_ap.get(); 772} 773 774void 775Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) 776{ 777 // Container objects whose paths do not specify a file directly can call 778 // this function to correct the file and object names. 779 m_file = file; 780 m_mod_time = file.GetModificationTime(); 781 m_object_name = object_name; 782} 783 784const ArchSpec& 785Module::GetArchitecture () const 786{ 787 return m_arch; 788} 789 790void 791Module::GetDescription (Stream *s, lldb::DescriptionLevel level) 792{ 793 Mutex::Locker locker (m_mutex); 794 795 if (level >= eDescriptionLevelFull) 796 { 797 if (m_arch.IsValid()) 798 s->Printf("(%s) ", m_arch.GetArchitectureName()); 799 } 800 801 if (level == eDescriptionLevelBrief) 802 { 803 const char *filename = m_file.GetFilename().GetCString(); 804 if (filename) 805 s->PutCString (filename); 806 } 807 else 808 { 809 char path[PATH_MAX]; 810 if (m_file.GetPath(path, sizeof(path))) 811 s->PutCString(path); 812 } 813 814 const char *object_name = m_object_name.GetCString(); 815 if (object_name) 816 s->Printf("(%s)", object_name); 817} 818 819void 820Module::ReportError (const char *format, ...) 821{ 822 if (format && format[0]) 823 { 824 StreamString strm; 825 strm.PutCString("error: "); 826 GetDescription(&strm, lldb::eDescriptionLevelBrief); 827 strm.PutChar (' '); 828 va_list args; 829 va_start (args, format); 830 strm.PrintfVarArg(format, args); 831 va_end (args); 832 833 const int format_len = strlen(format); 834 if (format_len > 0) 835 { 836 const char last_char = format[format_len-1]; 837 if (last_char != '\n' || last_char != '\r') 838 strm.EOL(); 839 } 840 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 841 842 } 843} 844 845bool 846Module::FileHasChanged () const 847{ 848 if (m_file_has_changed == false) 849 m_file_has_changed = (m_file.GetModificationTime() != m_mod_time); 850 return m_file_has_changed; 851} 852 853void 854Module::ReportErrorIfModifyDetected (const char *format, ...) 855{ 856 if (m_first_file_changed_log == false) 857 { 858 if (FileHasChanged ()) 859 { 860 m_first_file_changed_log = true; 861 if (format) 862 { 863 StreamString strm; 864 strm.PutCString("error: the object file "); 865 GetDescription(&strm, lldb::eDescriptionLevelFull); 866 strm.PutCString (" has been modified\n"); 867 868 va_list args; 869 va_start (args, format); 870 strm.PrintfVarArg(format, args); 871 va_end (args); 872 873 const int format_len = strlen(format); 874 if (format_len > 0) 875 { 876 const char last_char = format[format_len-1]; 877 if (last_char != '\n' || last_char != '\r') 878 strm.EOL(); 879 } 880 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); 881 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 882 } 883 } 884 } 885} 886 887void 888Module::ReportWarning (const char *format, ...) 889{ 890 if (format && format[0]) 891 { 892 StreamString strm; 893 strm.PutCString("warning: "); 894 GetDescription(&strm, lldb::eDescriptionLevelFull); 895 strm.PutChar (' '); 896 897 va_list args; 898 va_start (args, format); 899 strm.PrintfVarArg(format, args); 900 va_end (args); 901 902 const int format_len = strlen(format); 903 if (format_len > 0) 904 { 905 const char last_char = format[format_len-1]; 906 if (last_char != '\n' || last_char != '\r') 907 strm.EOL(); 908 } 909 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); 910 } 911} 912 913void 914Module::LogMessage (Log *log, const char *format, ...) 915{ 916 if (log) 917 { 918 StreamString log_message; 919 GetDescription(&log_message, lldb::eDescriptionLevelFull); 920 log_message.PutCString (": "); 921 va_list args; 922 va_start (args, format); 923 log_message.PrintfVarArg (format, args); 924 va_end (args); 925 log->PutCString(log_message.GetString().c_str()); 926 } 927} 928 929void 930Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...) 931{ 932 if (log) 933 { 934 StreamString log_message; 935 GetDescription(&log_message, lldb::eDescriptionLevelFull); 936 log_message.PutCString (": "); 937 va_list args; 938 va_start (args, format); 939 log_message.PrintfVarArg (format, args); 940 va_end (args); 941 if (log->GetVerbose()) 942 Host::Backtrace (log_message, 1024); 943 log->PutCString(log_message.GetString().c_str()); 944 } 945} 946 947void 948Module::Dump(Stream *s) 949{ 950 Mutex::Locker locker (m_mutex); 951 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 952 s->Indent(); 953 s->Printf("Module %s/%s%s%s%s\n", 954 m_file.GetDirectory().AsCString(), 955 m_file.GetFilename().AsCString(), 956 m_object_name ? "(" : "", 957 m_object_name ? m_object_name.GetCString() : "", 958 m_object_name ? ")" : ""); 959 960 s->IndentMore(); 961 ObjectFile *objfile = GetObjectFile (); 962 963 if (objfile) 964 objfile->Dump(s); 965 966 SymbolVendor *symbols = GetSymbolVendor (); 967 968 if (symbols) 969 symbols->Dump(s); 970 971 s->IndentLess(); 972} 973 974 975TypeList* 976Module::GetTypeList () 977{ 978 SymbolVendor *symbols = GetSymbolVendor (); 979 if (symbols) 980 return &symbols->GetTypeList(); 981 return NULL; 982} 983 984const ConstString & 985Module::GetObjectName() const 986{ 987 return m_object_name; 988} 989 990ObjectFile * 991Module::GetObjectFile() 992{ 993 Mutex::Locker locker (m_mutex); 994 if (m_did_load_objfile == false) 995 { 996 m_did_load_objfile = true; 997 Timer scoped_timer(__PRETTY_FUNCTION__, 998 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); 999 DataBufferSP file_data_sp; 1000 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(), 1001 &m_file, 1002 m_object_offset, 1003 m_file.GetByteSize(), 1004 file_data_sp); 1005 if (m_objfile_sp) 1006 { 1007 // Once we get the object file, update our module with the object file's 1008 // architecture since it might differ in vendor/os if some parts were 1009 // unknown. 1010 m_objfile_sp->GetArchitecture (m_arch); 1011 } 1012 } 1013 return m_objfile_sp.get(); 1014} 1015 1016 1017const Symbol * 1018Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) 1019{ 1020 Timer scoped_timer(__PRETTY_FUNCTION__, 1021 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1022 name.AsCString(), 1023 symbol_type); 1024 ObjectFile *objfile = GetObjectFile(); 1025 if (objfile) 1026 { 1027 Symtab *symtab = objfile->GetSymtab(); 1028 if (symtab) 1029 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1030 } 1031 return NULL; 1032} 1033void 1034Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) 1035{ 1036 // No need to protect this call using m_mutex all other method calls are 1037 // already thread safe. 1038 1039 size_t num_indices = symbol_indexes.size(); 1040 if (num_indices > 0) 1041 { 1042 SymbolContext sc; 1043 CalculateSymbolContext (&sc); 1044 for (size_t i = 0; i < num_indices; i++) 1045 { 1046 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); 1047 if (sc.symbol) 1048 sc_list.Append (sc); 1049 } 1050 } 1051} 1052 1053size_t 1054Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) 1055{ 1056 // No need to protect this call using m_mutex all other method calls are 1057 // already thread safe. 1058 1059 1060 Timer scoped_timer(__PRETTY_FUNCTION__, 1061 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1062 name.AsCString(), 1063 symbol_type); 1064 const size_t initial_size = sc_list.GetSize(); 1065 ObjectFile *objfile = GetObjectFile (); 1066 if (objfile) 1067 { 1068 Symtab *symtab = objfile->GetSymtab(); 1069 if (symtab) 1070 { 1071 std::vector<uint32_t> symbol_indexes; 1072 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); 1073 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1074 } 1075 } 1076 return sc_list.GetSize() - initial_size; 1077} 1078 1079size_t 1080Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) 1081{ 1082 // No need to protect this call using m_mutex all other method calls are 1083 // already thread safe. 1084 1085 Timer scoped_timer(__PRETTY_FUNCTION__, 1086 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 1087 regex.GetText(), 1088 symbol_type); 1089 const size_t initial_size = sc_list.GetSize(); 1090 ObjectFile *objfile = GetObjectFile (); 1091 if (objfile) 1092 { 1093 Symtab *symtab = objfile->GetSymtab(); 1094 if (symtab) 1095 { 1096 std::vector<uint32_t> symbol_indexes; 1097 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 1098 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 1099 } 1100 } 1101 return sc_list.GetSize() - initial_size; 1102} 1103 1104const TimeValue & 1105Module::GetModificationTime () const 1106{ 1107 return m_mod_time; 1108} 1109 1110bool 1111Module::IsExecutable () 1112{ 1113 if (GetObjectFile() == NULL) 1114 return false; 1115 else 1116 return GetObjectFile()->IsExecutable(); 1117} 1118 1119bool 1120Module::IsLoadedInTarget (Target *target) 1121{ 1122 ObjectFile *obj_file = GetObjectFile(); 1123 if (obj_file) 1124 { 1125 SectionList *sections = obj_file->GetSectionList(); 1126 if (sections != NULL) 1127 { 1128 size_t num_sections = sections->GetSize(); 1129 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) 1130 { 1131 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1132 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) 1133 { 1134 return true; 1135 } 1136 } 1137 } 1138 } 1139 return false; 1140} 1141 1142bool 1143Module::LoadScriptingResourceInTarget (Target *target, Error& error) 1144{ 1145 if (!target) 1146 { 1147 error.SetErrorString("invalid destination Target"); 1148 return false; 1149 } 1150 1151 PlatformSP platform_sp(target->GetPlatform()); 1152 1153 if (!platform_sp) 1154 { 1155 error.SetErrorString("invalid Platform"); 1156 return false; 1157 } 1158 1159 ModuleSpec module_spec(GetFileSpec()); 1160 FileSpec scripting_fspec = platform_sp->LocateExecutableScriptingResource(module_spec); 1161 Debugger &debugger(target->GetDebugger()); 1162 if (scripting_fspec && scripting_fspec.Exists()) 1163 { 1164 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); 1165 if (script_interpreter) 1166 { 1167 StreamString scripting_stream; 1168 scripting_fspec.Dump(&scripting_stream); 1169 bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), false, true, error); 1170 if (!did_load) 1171 return false; 1172 } 1173 else 1174 { 1175 error.SetErrorString("invalid ScriptInterpreter"); 1176 return false; 1177 } 1178 } 1179 return true; 1180} 1181 1182bool 1183Module::SetArchitecture (const ArchSpec &new_arch) 1184{ 1185 if (!m_arch.IsValid()) 1186 { 1187 m_arch = new_arch; 1188 return true; 1189 } 1190 return m_arch == new_arch; 1191} 1192 1193bool 1194Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed) 1195{ 1196 size_t num_loaded_sections = 0; 1197 ObjectFile *objfile = GetObjectFile(); 1198 if (objfile) 1199 { 1200 SectionList *section_list = objfile->GetSectionList (); 1201 if (section_list) 1202 { 1203 const size_t num_sections = section_list->GetSize(); 1204 size_t sect_idx = 0; 1205 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) 1206 { 1207 // Iterate through the object file sections to find the 1208 // first section that starts of file offset zero and that 1209 // has bytes in the file... 1210 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx)); 1211 // Only load non-thread specific sections when given a slide 1212 if (section_sp && !section_sp->IsThreadSpecific()) 1213 { 1214 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + offset)) 1215 ++num_loaded_sections; 1216 } 1217 } 1218 } 1219 } 1220 changed = num_loaded_sections > 0; 1221 return num_loaded_sections > 0; 1222} 1223 1224 1225bool 1226Module::MatchesModuleSpec (const ModuleSpec &module_ref) 1227{ 1228 const UUID &uuid = module_ref.GetUUID(); 1229 1230 if (uuid.IsValid()) 1231 { 1232 // If the UUID matches, then nothing more needs to match... 1233 if (uuid == GetUUID()) 1234 return true; 1235 else 1236 return false; 1237 } 1238 1239 const FileSpec &file_spec = module_ref.GetFileSpec(); 1240 if (file_spec) 1241 { 1242 if (!FileSpec::Equal (file_spec, m_file, file_spec.GetDirectory())) 1243 return false; 1244 } 1245 1246 const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1247 if (platform_file_spec) 1248 { 1249 if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), platform_file_spec.GetDirectory())) 1250 return false; 1251 } 1252 1253 const ArchSpec &arch = module_ref.GetArchitecture(); 1254 if (arch.IsValid()) 1255 { 1256 if (m_arch != arch) 1257 return false; 1258 } 1259 1260 const ConstString &object_name = module_ref.GetObjectName(); 1261 if (object_name) 1262 { 1263 if (object_name != GetObjectName()) 1264 return false; 1265 } 1266 return true; 1267} 1268 1269bool 1270Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 1271{ 1272 Mutex::Locker locker (m_mutex); 1273 return m_source_mappings.FindFile (orig_spec, new_spec); 1274} 1275 1276bool 1277Module::RemapSourceFile (const char *path, std::string &new_path) const 1278{ 1279 Mutex::Locker locker (m_mutex); 1280 return m_source_mappings.RemapPath(path, new_path); 1281} 1282 1283uint32_t 1284Module::GetVersion (uint32_t *versions, uint32_t num_versions) 1285{ 1286 ObjectFile *obj_file = GetObjectFile(); 1287 if (obj_file) 1288 return obj_file->GetVersion (versions, num_versions); 1289 1290 if (versions && num_versions) 1291 { 1292 for (uint32_t i=0; i<num_versions; ++i) 1293 versions[i] = UINT32_MAX; 1294 } 1295 return 0; 1296} 1297