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