Module.cpp revision b5a8f1498e1ddaeed5187a878d57ea0b74af9c26
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 26using namespace lldb; 27using 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 32typedef std::vector<Module *> ModuleCollection; 33 34static ModuleCollection & 35GetModuleCollection() 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 50Mutex * 51Module::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 64size_t 65Module::GetNumberAllocatedModules () 66{ 67 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 68 return GetModuleCollection().size(); 69} 70 71Module * 72Module::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. 84namespace lldb { 85 86 void 87 ClearModuleInfo (void) 88 { 89 ModuleList::RemoveOrphanSharedModules(); 90 } 91 92 void 93 DumpModuleInfo (void) 94 { 95 Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex()); 96 ModuleCollection &modules = GetModuleCollection(); 97 const size_t count = modules.size(); 98 printf ("%s: %zu modules:\n", __PRETTY_FUNCTION__, count); 99 for (size_t i=0; i<count; ++i) 100 { 101 102 StreamString strm; 103 Module *module = modules[i]; 104 const bool in_shared_module_list = ModuleList::ModuleIsInCache (module); 105 module->GetDescription(&strm, eDescriptionLevelFull); 106 printf ("%p: shared = %i, ref_count = %3u, module = %s\n", 107 module, 108 in_shared_module_list, 109 (uint32_t)module->use_count(), 110 strm.GetString().c_str()); 111 } 112 } 113} 114 115#endif 116 117 118Module::Module(const FileSpec& file_spec, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) : 119 m_mutex (Mutex::eMutexTypeRecursive), 120 m_mod_time (), 121 m_arch (), 122 m_uuid (), 123 m_file (file_spec), 124 m_platform_file(), 125 m_object_name (), 126 m_object_offset (), 127 m_objfile_sp (), 128 m_symfile_ap (), 129 m_ast (), 130 m_did_load_objfile (false), 131 m_did_load_symbol_vendor (false), 132 m_did_parse_uuid (false), 133 m_did_init_ast (false), 134 m_is_dynamic_loader_module (false), 135 m_was_modified (false) 136{ 137 // Scope for locker below... 138 { 139 Mutex::Locker locker (GetAllocationModuleCollectionMutex()); 140 GetModuleCollection().push_back(this); 141 } 142 StreamString s; 143 if (m_file.GetFilename()) 144 s << m_file.GetFilename(); 145 s.Printf("[0x%16.16llx]", header_addr); 146 m_file.GetFilename().SetCString (s.GetData()); 147 Mutex::Locker locker (m_mutex); 148 DataBufferSP data_sp; 149 if (process_sp) 150 { 151 m_did_load_objfile = true; 152 std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0)); 153 Error error; 154 const size_t bytes_read = process_sp->ReadMemory (header_addr, 155 data_ap->GetBytes(), 156 data_ap->GetByteSize(), 157 error); 158 if (bytes_read == 512) 159 { 160 data_sp.reset (data_ap.release()); 161 m_objfile_sp = ObjectFile::FindPlugin(this, process_sp, header_addr, data_sp); 162 if (m_objfile_sp) 163 { 164 // Once we get the object file, update our module with the object file's 165 // architecture since it might differ in vendor/os if some parts were 166 // unknown. 167 m_objfile_sp->GetArchitecture (m_arch); 168 } 169 } 170 } 171} 172 173Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) : 174 m_mutex (Mutex::eMutexTypeRecursive), 175 m_mod_time (file_spec.GetModificationTime()), 176 m_arch (arch), 177 m_uuid (), 178 m_file (file_spec), 179 m_platform_file(), 180 m_object_name (), 181 m_object_offset (object_offset), 182 m_objfile_sp (), 183 m_symfile_ap (), 184 m_ast (), 185 m_did_load_objfile (false), 186 m_did_load_symbol_vendor (false), 187 m_did_parse_uuid (false), 188 m_did_init_ast (false), 189 m_is_dynamic_loader_module (false), 190 m_was_modified (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::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 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 if (pos != end) 221 modules.erase(pos); 222 } 223 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 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 242 243const lldb_private::UUID& 244Module::GetUUID() 245{ 246 Mutex::Locker locker (m_mutex); 247 if (m_did_parse_uuid == false) 248 { 249 ObjectFile * obj_file = GetObjectFile (); 250 251 if (obj_file != NULL) 252 { 253 obj_file->GetUUID(&m_uuid); 254 m_did_parse_uuid = true; 255 } 256 } 257 return m_uuid; 258} 259 260ClangASTContext & 261Module::GetClangASTContext () 262{ 263 Mutex::Locker locker (m_mutex); 264 if (m_did_init_ast == false) 265 { 266 ObjectFile * objfile = GetObjectFile(); 267 ArchSpec object_arch; 268 if (objfile && objfile->GetArchitecture(object_arch)) 269 { 270 m_did_init_ast = true; 271 m_ast.SetArchitecture (object_arch); 272 } 273 } 274 return m_ast; 275} 276 277void 278Module::ParseAllDebugSymbols() 279{ 280 Mutex::Locker locker (m_mutex); 281 uint32_t num_comp_units = GetNumCompileUnits(); 282 if (num_comp_units == 0) 283 return; 284 285 SymbolContext sc; 286 sc.module_sp = shared_from_this(); 287 uint32_t cu_idx; 288 SymbolVendor *symbols = GetSymbolVendor (); 289 290 for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++) 291 { 292 sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); 293 if (sc.comp_unit) 294 { 295 sc.function = NULL; 296 symbols->ParseVariablesForContext(sc); 297 298 symbols->ParseCompileUnitFunctions(sc); 299 300 uint32_t func_idx; 301 for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx) 302 { 303 symbols->ParseFunctionBlocks(sc); 304 305 // Parse the variables for this function and all its blocks 306 symbols->ParseVariablesForContext(sc); 307 } 308 309 310 // Parse all types for this compile unit 311 sc.function = NULL; 312 symbols->ParseTypes(sc); 313 } 314 } 315} 316 317void 318Module::CalculateSymbolContext(SymbolContext* sc) 319{ 320 sc->module_sp = shared_from_this(); 321} 322 323Module * 324Module::CalculateSymbolContextModule () 325{ 326 return this; 327} 328 329void 330Module::DumpSymbolContext(Stream *s) 331{ 332 s->Printf(", Module{%p}", this); 333} 334 335uint32_t 336Module::GetNumCompileUnits() 337{ 338 Mutex::Locker locker (m_mutex); 339 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this); 340 SymbolVendor *symbols = GetSymbolVendor (); 341 if (symbols) 342 return symbols->GetNumCompileUnits(); 343 return 0; 344} 345 346CompUnitSP 347Module::GetCompileUnitAtIndex (uint32_t index) 348{ 349 Mutex::Locker locker (m_mutex); 350 uint32_t num_comp_units = GetNumCompileUnits (); 351 CompUnitSP cu_sp; 352 353 if (index < num_comp_units) 354 { 355 SymbolVendor *symbols = GetSymbolVendor (); 356 if (symbols) 357 cu_sp = symbols->GetCompileUnitAtIndex(index); 358 } 359 return cu_sp; 360} 361 362bool 363Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) 364{ 365 Mutex::Locker locker (m_mutex); 366 Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr); 367 ObjectFile* ofile = GetObjectFile(); 368 if (ofile) 369 return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList()); 370 return false; 371} 372 373uint32_t 374Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 375{ 376 Mutex::Locker locker (m_mutex); 377 uint32_t resolved_flags = 0; 378 379 // Clear the result symbol context in case we don't find anything 380 sc.Clear(); 381 382 // Get the section from the section/offset address. 383 const Section *section = so_addr.GetSection(); 384 385 // Make sure the section matches this module before we try and match anything 386 if (section && section->GetModule() == this) 387 { 388 // If the section offset based address resolved itself, then this 389 // is the right module. 390 sc.module_sp = shared_from_this(); 391 resolved_flags |= eSymbolContextModule; 392 393 // Resolve the compile unit, function, block, line table or line 394 // entry if requested. 395 if (resolve_scope & eSymbolContextCompUnit || 396 resolve_scope & eSymbolContextFunction || 397 resolve_scope & eSymbolContextBlock || 398 resolve_scope & eSymbolContextLineEntry ) 399 { 400 SymbolVendor *symbols = GetSymbolVendor (); 401 if (symbols) 402 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc); 403 } 404 405 // Resolve the symbol if requested, but don't re-look it up if we've already found it. 406 if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol)) 407 { 408 ObjectFile* ofile = GetObjectFile(); 409 if (ofile) 410 { 411 Symtab *symtab = ofile->GetSymtab(); 412 if (symtab) 413 { 414 if (so_addr.IsSectionOffset()) 415 { 416 sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress()); 417 if (sc.symbol) 418 resolved_flags |= eSymbolContextSymbol; 419 } 420 } 421 } 422 } 423 } 424 return resolved_flags; 425} 426 427uint32_t 428Module::ResolveSymbolContextForFilePath 429( 430 const char *file_path, 431 uint32_t line, 432 bool check_inlines, 433 uint32_t resolve_scope, 434 SymbolContextList& sc_list 435) 436{ 437 FileSpec file_spec(file_path, false); 438 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 439} 440 441uint32_t 442Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 443{ 444 Mutex::Locker locker (m_mutex); 445 Timer scoped_timer(__PRETTY_FUNCTION__, 446 "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)", 447 file_spec.GetDirectory().AsCString(""), 448 file_spec.GetDirectory() ? "/" : "", 449 file_spec.GetFilename().AsCString(""), 450 line, 451 check_inlines ? "yes" : "no", 452 resolve_scope); 453 454 const uint32_t initial_count = sc_list.GetSize(); 455 456 SymbolVendor *symbols = GetSymbolVendor (); 457 if (symbols) 458 symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list); 459 460 return sc_list.GetSize() - initial_count; 461} 462 463 464uint32_t 465Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) 466{ 467 SymbolVendor *symbols = GetSymbolVendor (); 468 if (symbols) 469 return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables); 470 return 0; 471} 472uint32_t 473Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 474{ 475 SymbolVendor *symbols = GetSymbolVendor (); 476 if (symbols) 477 return symbols->FindGlobalVariables(regex, append, max_matches, variables); 478 return 0; 479} 480 481uint32_t 482Module::FindCompileUnits (const FileSpec &path, 483 bool append, 484 SymbolContextList &sc_list) 485{ 486 if (!append) 487 sc_list.Clear(); 488 489 const uint32_t start_size = sc_list.GetSize(); 490 const uint32_t num_compile_units = GetNumCompileUnits(); 491 SymbolContext sc; 492 sc.module_sp = shared_from_this(); 493 const bool compare_directory = path.GetDirectory(); 494 for (uint32_t i=0; i<num_compile_units; ++i) 495 { 496 sc.comp_unit = GetCompileUnitAtIndex(i).get(); 497 if (FileSpec::Equal (*sc.comp_unit, path, compare_directory)) 498 sc_list.Append(sc); 499 } 500 return sc_list.GetSize() - start_size; 501} 502 503uint32_t 504Module::FindFunctions (const ConstString &name, 505 const ClangNamespaceDecl *namespace_decl, 506 uint32_t name_type_mask, 507 bool include_symbols, 508 bool append, 509 SymbolContextList& sc_list) 510{ 511 if (!append) 512 sc_list.Clear(); 513 514 const uint32_t start_size = sc_list.GetSize(); 515 516 // Find all the functions (not symbols, but debug information functions... 517 SymbolVendor *symbols = GetSymbolVendor (); 518 if (symbols) 519 symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list); 520 521 // Now check our symbol table for symbols that are code symbols if requested 522 if (include_symbols) 523 { 524 ObjectFile *objfile = GetObjectFile(); 525 if (objfile) 526 { 527 Symtab *symtab = objfile->GetSymtab(); 528 if (symtab) 529 { 530 std::vector<uint32_t> symbol_indexes; 531 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 532 const uint32_t num_matches = symbol_indexes.size(); 533 if (num_matches) 534 { 535 const bool merge_symbol_into_function = true; 536 SymbolContext sc(this); 537 for (uint32_t i=0; i<num_matches; i++) 538 { 539 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 540 sc_list.AppendIfUnique (sc, merge_symbol_into_function); 541 } 542 } 543 } 544 } 545 } 546 return sc_list.GetSize() - start_size; 547} 548 549uint32_t 550Module::FindFunctions (const RegularExpression& regex, 551 bool include_symbols, 552 bool append, 553 SymbolContextList& sc_list) 554{ 555 if (!append) 556 sc_list.Clear(); 557 558 const uint32_t start_size = sc_list.GetSize(); 559 560 SymbolVendor *symbols = GetSymbolVendor (); 561 if (symbols) 562 symbols->FindFunctions(regex, append, sc_list); 563 // Now check our symbol table for symbols that are code symbols if requested 564 if (include_symbols) 565 { 566 ObjectFile *objfile = GetObjectFile(); 567 if (objfile) 568 { 569 Symtab *symtab = objfile->GetSymtab(); 570 if (symtab) 571 { 572 std::vector<uint32_t> symbol_indexes; 573 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 574 const uint32_t num_matches = symbol_indexes.size(); 575 if (num_matches) 576 { 577 const bool merge_symbol_into_function = true; 578 SymbolContext sc(this); 579 for (uint32_t i=0; i<num_matches; i++) 580 { 581 sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 582 sc_list.AppendIfUnique (sc, merge_symbol_into_function); 583 } 584 } 585 } 586 } 587 } 588 return sc_list.GetSize() - start_size; 589} 590 591uint32_t 592Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types) 593{ 594 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 595 if (sc.module_sp.get() == NULL || sc.module_sp.get() == this) 596 { 597 SymbolVendor *symbols = GetSymbolVendor (); 598 if (symbols) 599 return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types); 600 } 601 return 0; 602} 603 604// depending on implementation details, type lookup might fail because of 605// embedded spurious namespace:: prefixes. this call strips them, paying 606// attention to the fact that a type might have namespace'd type names as 607// arguments to templates, and those must not be stripped off 608static const char* 609StripTypeName(const char* name_cstr) 610{ 611 // Protect against null c string. 612 if (!name_cstr) 613 return name_cstr; 614 const char* skip_namespace = strstr(name_cstr, "::"); 615 const char* template_arg_char = strchr(name_cstr, '<'); 616 while (skip_namespace != NULL) 617 { 618 if (template_arg_char != NULL && 619 skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go 620 break; 621 name_cstr = skip_namespace+2; 622 skip_namespace = strstr(name_cstr, "::"); 623 } 624 return name_cstr; 625} 626 627uint32_t 628Module::FindTypes (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types) 629{ 630 uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types); 631 632 if (retval == 0) 633 { 634 const char *orig_name = name.GetCString(); 635 const char *stripped = StripTypeName(orig_name); 636 // Only do this lookup if StripTypeName has stripped the name: 637 if (stripped != orig_name) 638 return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types); 639 else 640 return 0; 641 } 642 else 643 return retval; 644 645} 646 647//uint32_t 648//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types) 649//{ 650// Timer scoped_timer(__PRETTY_FUNCTION__); 651// SymbolVendor *symbols = GetSymbolVendor (); 652// if (symbols) 653// return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types); 654// return 0; 655// 656//} 657 658SymbolVendor* 659Module::GetSymbolVendor (bool can_create) 660{ 661 Mutex::Locker locker (m_mutex); 662 if (m_did_load_symbol_vendor == false && can_create) 663 { 664 ObjectFile *obj_file = GetObjectFile (); 665 if (obj_file != NULL) 666 { 667 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); 668 m_symfile_ap.reset(SymbolVendor::FindPlugin(this)); 669 m_did_load_symbol_vendor = true; 670 } 671 } 672 return m_symfile_ap.get(); 673} 674 675void 676Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name) 677{ 678 // Container objects whose paths do not specify a file directly can call 679 // this function to correct the file and object names. 680 m_file = file; 681 m_mod_time = file.GetModificationTime(); 682 m_object_name = object_name; 683} 684 685const ArchSpec& 686Module::GetArchitecture () const 687{ 688 return m_arch; 689} 690 691void 692Module::GetDescription (Stream *s, lldb::DescriptionLevel level) 693{ 694 Mutex::Locker locker (m_mutex); 695 696 if (level >= eDescriptionLevelFull) 697 { 698 if (m_arch.IsValid()) 699 s->Printf("(%s) ", m_arch.GetArchitectureName()); 700 } 701 702 if (level == eDescriptionLevelBrief) 703 { 704 const char *filename = m_file.GetFilename().GetCString(); 705 if (filename) 706 s->PutCString (filename); 707 } 708 else 709 { 710 char path[PATH_MAX]; 711 if (m_file.GetPath(path, sizeof(path))) 712 s->PutCString(path); 713 } 714 715 const char *object_name = m_object_name.GetCString(); 716 if (object_name) 717 s->Printf("(%s)", object_name); 718} 719 720void 721Module::ReportError (const char *format, ...) 722{ 723 if (format && format[0]) 724 { 725 StreamString strm; 726 strm.PutCString("error: "); 727 GetDescription(&strm, lldb::eDescriptionLevelBrief); 728 strm.PutChar (' '); 729 va_list args; 730 va_start (args, format); 731 strm.PrintfVarArg(format, args); 732 va_end (args); 733 734 const int format_len = strlen(format); 735 if (format_len > 0) 736 { 737 const char last_char = format[format_len-1]; 738 if (last_char != '\n' || last_char != '\r') 739 strm.EOL(); 740 } 741 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 742 743 } 744} 745 746void 747Module::ReportErrorIfModifyDetected (const char *format, ...) 748{ 749 if (!GetModified(true) && GetModified(false)) 750 { 751 if (format) 752 { 753 StreamString strm; 754 strm.PutCString("error: the object file "); 755 GetDescription(&strm, lldb::eDescriptionLevelFull); 756 strm.PutCString (" has been modified\n"); 757 758 va_list args; 759 va_start (args, format); 760 strm.PrintfVarArg(format, args); 761 va_end (args); 762 763 const int format_len = strlen(format); 764 if (format_len > 0) 765 { 766 const char last_char = format[format_len-1]; 767 if (last_char != '\n' || last_char != '\r') 768 strm.EOL(); 769 } 770 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n"); 771 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str()); 772 } 773 } 774} 775 776void 777Module::ReportWarning (const char *format, ...) 778{ 779 if (format && format[0]) 780 { 781 StreamString strm; 782 strm.PutCString("warning: "); 783 GetDescription(&strm, lldb::eDescriptionLevelFull); 784 strm.PutChar (' '); 785 786 va_list args; 787 va_start (args, format); 788 strm.PrintfVarArg(format, args); 789 va_end (args); 790 791 const int format_len = strlen(format); 792 if (format_len > 0) 793 { 794 const char last_char = format[format_len-1]; 795 if (last_char != '\n' || last_char != '\r') 796 strm.EOL(); 797 } 798 Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str()); 799 } 800} 801 802void 803Module::LogMessage (Log *log, const char *format, ...) 804{ 805 if (log) 806 { 807 StreamString log_message; 808 GetDescription(&log_message, lldb::eDescriptionLevelFull); 809 log_message.PutCString (": "); 810 va_list args; 811 va_start (args, format); 812 log_message.PrintfVarArg (format, args); 813 va_end (args); 814 log->PutCString(log_message.GetString().c_str()); 815 } 816} 817 818bool 819Module::GetModified (bool use_cached_only) 820{ 821 if (m_was_modified == false && use_cached_only == false) 822 { 823 TimeValue curr_mod_time (m_file.GetModificationTime()); 824 m_was_modified = curr_mod_time != m_mod_time; 825 } 826 return m_was_modified; 827} 828 829bool 830Module::SetModified (bool b) 831{ 832 const bool prev_value = m_was_modified; 833 m_was_modified = b; 834 return prev_value; 835} 836 837 838void 839Module::Dump(Stream *s) 840{ 841 Mutex::Locker locker (m_mutex); 842 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 843 s->Indent(); 844 s->Printf("Module %s/%s%s%s%s\n", 845 m_file.GetDirectory().AsCString(), 846 m_file.GetFilename().AsCString(), 847 m_object_name ? "(" : "", 848 m_object_name ? m_object_name.GetCString() : "", 849 m_object_name ? ")" : ""); 850 851 s->IndentMore(); 852 ObjectFile *objfile = GetObjectFile (); 853 854 if (objfile) 855 objfile->Dump(s); 856 857 SymbolVendor *symbols = GetSymbolVendor (); 858 859 if (symbols) 860 symbols->Dump(s); 861 862 s->IndentLess(); 863} 864 865 866TypeList* 867Module::GetTypeList () 868{ 869 SymbolVendor *symbols = GetSymbolVendor (); 870 if (symbols) 871 return &symbols->GetTypeList(); 872 return NULL; 873} 874 875const ConstString & 876Module::GetObjectName() const 877{ 878 return m_object_name; 879} 880 881ObjectFile * 882Module::GetObjectFile() 883{ 884 Mutex::Locker locker (m_mutex); 885 if (m_did_load_objfile == false) 886 { 887 m_did_load_objfile = true; 888 Timer scoped_timer(__PRETTY_FUNCTION__, 889 "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString("")); 890 DataBufferSP file_data_sp; 891 m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp); 892 if (m_objfile_sp) 893 { 894 // Once we get the object file, update our module with the object file's 895 // architecture since it might differ in vendor/os if some parts were 896 // unknown. 897 m_objfile_sp->GetArchitecture (m_arch); 898 } 899 } 900 return m_objfile_sp.get(); 901} 902 903 904const Symbol * 905Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type) 906{ 907 Timer scoped_timer(__PRETTY_FUNCTION__, 908 "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 909 name.AsCString(), 910 symbol_type); 911 ObjectFile *objfile = GetObjectFile(); 912 if (objfile) 913 { 914 Symtab *symtab = objfile->GetSymtab(); 915 if (symtab) 916 return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 917 } 918 return NULL; 919} 920void 921Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) 922{ 923 // No need to protect this call using m_mutex all other method calls are 924 // already thread safe. 925 926 size_t num_indices = symbol_indexes.size(); 927 if (num_indices > 0) 928 { 929 SymbolContext sc; 930 CalculateSymbolContext (&sc); 931 for (size_t i = 0; i < num_indices; i++) 932 { 933 sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]); 934 if (sc.symbol) 935 sc_list.Append (sc); 936 } 937 } 938} 939 940size_t 941Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list) 942{ 943 // No need to protect this call using m_mutex all other method calls are 944 // already thread safe. 945 946 947 Timer scoped_timer(__PRETTY_FUNCTION__, 948 "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 949 name.AsCString(), 950 symbol_type); 951 const size_t initial_size = sc_list.GetSize(); 952 ObjectFile *objfile = GetObjectFile (); 953 if (objfile) 954 { 955 Symtab *symtab = objfile->GetSymtab(); 956 if (symtab) 957 { 958 std::vector<uint32_t> symbol_indexes; 959 symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes); 960 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 961 } 962 } 963 return sc_list.GetSize() - initial_size; 964} 965 966size_t 967Module::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, SymbolType symbol_type, SymbolContextList &sc_list) 968{ 969 // No need to protect this call using m_mutex all other method calls are 970 // already thread safe. 971 972 Timer scoped_timer(__PRETTY_FUNCTION__, 973 "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 974 regex.GetText(), 975 symbol_type); 976 const size_t initial_size = sc_list.GetSize(); 977 ObjectFile *objfile = GetObjectFile (); 978 if (objfile) 979 { 980 Symtab *symtab = objfile->GetSymtab(); 981 if (symtab) 982 { 983 std::vector<uint32_t> symbol_indexes; 984 symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes); 985 SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list); 986 } 987 } 988 return sc_list.GetSize() - initial_size; 989} 990 991const TimeValue & 992Module::GetModificationTime () const 993{ 994 return m_mod_time; 995} 996 997bool 998Module::IsExecutable () 999{ 1000 if (GetObjectFile() == NULL) 1001 return false; 1002 else 1003 return GetObjectFile()->IsExecutable(); 1004} 1005 1006bool 1007Module::IsLoadedInTarget (Target *target) 1008{ 1009 ObjectFile *obj_file = GetObjectFile(); 1010 if (obj_file) 1011 { 1012 SectionList *sections = obj_file->GetSectionList(); 1013 if (sections != NULL) 1014 { 1015 size_t num_sections = sections->GetSize(); 1016 for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) 1017 { 1018 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1019 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) 1020 { 1021 return true; 1022 } 1023 } 1024 } 1025 } 1026 return false; 1027} 1028bool 1029Module::SetArchitecture (const ArchSpec &new_arch) 1030{ 1031 if (!m_arch.IsValid()) 1032 { 1033 m_arch = new_arch; 1034 return true; 1035 } 1036 return m_arch == new_arch; 1037} 1038 1039bool 1040Module::SetLoadAddress (Target &target, lldb::addr_t offset, bool &changed) 1041{ 1042 changed = false; 1043 ObjectFile *image_object_file = GetObjectFile(); 1044 if (image_object_file) 1045 { 1046 SectionList *section_list = image_object_file->GetSectionList (); 1047 if (section_list) 1048 { 1049 const size_t num_sections = section_list->GetSize(); 1050 size_t sect_idx = 0; 1051 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) 1052 { 1053 // Iterate through the object file sections to find the 1054 // first section that starts of file offset zero and that 1055 // has bytes in the file... 1056 Section *section = section_list->GetSectionAtIndex (sect_idx).get(); 1057 if (section) 1058 { 1059 if (target.GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + offset)) 1060 changed = true; 1061 } 1062 } 1063 return sect_idx > 0; 1064 } 1065 } 1066 return false; 1067} 1068 1069