1 //===-- SBModule.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/API/SBModule.h" 11 #include "lldb/API/SBAddress.h" 12 #include "lldb/API/SBFileSpec.h" 13 #include "lldb/API/SBModuleSpec.h" 14 #include "lldb/API/SBProcess.h" 15 #include "lldb/API/SBStream.h" 16 #include "lldb/API/SBSymbolContextList.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/Log.h" 19 #include "lldb/Core/Section.h" 20 #include "lldb/Core/StreamString.h" 21 #include "lldb/Core/ValueObjectList.h" 22 #include "lldb/Core/ValueObjectVariable.h" 23 #include "lldb/Symbol/ObjectFile.h" 24 #include "lldb/Symbol/SymbolFile.h" 25 #include "lldb/Symbol/SymbolVendor.h" 26 #include "lldb/Symbol/Symtab.h" 27 #include "lldb/Symbol/TypeSystem.h" 28 #include "lldb/Symbol/VariableList.h" 29 #include "lldb/Target/Target.h" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 35 SBModule::SBModule () : 36 m_opaque_sp () 37 { 38 } 39 40 SBModule::SBModule (const lldb::ModuleSP& module_sp) : 41 m_opaque_sp (module_sp) 42 { 43 } 44 45 SBModule::SBModule(const SBModuleSpec &module_spec) : 46 m_opaque_sp () 47 { 48 ModuleSP module_sp; 49 Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap, 50 module_sp, 51 NULL, 52 NULL, 53 NULL); 54 if (module_sp) 55 SetSP(module_sp); 56 } 57 58 SBModule::SBModule(const SBModule &rhs) : 59 m_opaque_sp (rhs.m_opaque_sp) 60 { 61 } 62 63 SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) : 64 m_opaque_sp () 65 { 66 ProcessSP process_sp (process.GetSP()); 67 if (process_sp) 68 { 69 m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr); 70 if (m_opaque_sp) 71 { 72 Target &target = process_sp->GetTarget(); 73 bool changed = false; 74 m_opaque_sp->SetLoadAddress(target, 0, true, changed); 75 target.GetImages().Append(m_opaque_sp); 76 } 77 } 78 } 79 80 const SBModule & 81 SBModule::operator = (const SBModule &rhs) 82 { 83 if (this != &rhs) 84 m_opaque_sp = rhs.m_opaque_sp; 85 return *this; 86 } 87 88 SBModule::~SBModule () 89 { 90 } 91 92 bool 93 SBModule::IsValid () const 94 { 95 return m_opaque_sp.get() != NULL; 96 } 97 98 void 99 SBModule::Clear() 100 { 101 m_opaque_sp.reset(); 102 } 103 104 SBFileSpec 105 SBModule::GetFileSpec () const 106 { 107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 108 109 SBFileSpec file_spec; 110 ModuleSP module_sp (GetSP ()); 111 if (module_sp) 112 file_spec.SetFileSpec(module_sp->GetFileSpec()); 113 114 if (log) 115 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)", 116 static_cast<void*>(module_sp.get()), 117 static_cast<const void*>(file_spec.get())); 118 119 return file_spec; 120 } 121 122 lldb::SBFileSpec 123 SBModule::GetPlatformFileSpec () const 124 { 125 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 126 127 SBFileSpec file_spec; 128 ModuleSP module_sp (GetSP ()); 129 if (module_sp) 130 file_spec.SetFileSpec(module_sp->GetPlatformFileSpec()); 131 132 if (log) 133 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)", 134 static_cast<void*>(module_sp.get()), 135 static_cast<const void*>(file_spec.get())); 136 137 return file_spec; 138 } 139 140 bool 141 SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file) 142 { 143 bool result = false; 144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 145 146 ModuleSP module_sp (GetSP ()); 147 if (module_sp) 148 { 149 module_sp->SetPlatformFileSpec(*platform_file); 150 result = true; 151 } 152 153 if (log) 154 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i", 155 static_cast<void*>(module_sp.get()), 156 static_cast<const void*>(platform_file.get()), 157 platform_file->GetPath().c_str(), result); 158 return result; 159 } 160 161 lldb::SBFileSpec 162 SBModule::GetRemoteInstallFileSpec () 163 { 164 SBFileSpec sb_file_spec; 165 ModuleSP module_sp (GetSP ()); 166 if (module_sp) 167 sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec()); 168 return sb_file_spec; 169 } 170 171 bool 172 SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file) 173 { 174 ModuleSP module_sp (GetSP ()); 175 if (module_sp) 176 { 177 module_sp->SetRemoteInstallFileSpec(file.ref()); 178 return true; 179 } 180 return false; 181 } 182 183 184 const uint8_t * 185 SBModule::GetUUIDBytes () const 186 { 187 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 188 189 const uint8_t *uuid_bytes = NULL; 190 ModuleSP module_sp (GetSP ()); 191 if (module_sp) 192 uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes(); 193 194 if (log) 195 { 196 if (uuid_bytes) 197 { 198 StreamString s; 199 module_sp->GetUUID().Dump (&s); 200 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", 201 static_cast<void*>(module_sp.get()), s.GetData()); 202 } 203 else 204 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", 205 static_cast<void*>(module_sp.get())); 206 } 207 return uuid_bytes; 208 } 209 210 211 const char * 212 SBModule::GetUUIDString () const 213 { 214 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 215 216 const char *uuid_cstr = NULL; 217 ModuleSP module_sp (GetSP ()); 218 if (module_sp) 219 { 220 // We are going to return a "const char *" value through the public 221 // API, so we need to constify it so it gets added permanently the 222 // string pool and then we don't need to worry about the lifetime of the 223 // string as it will never go away once it has been put into the ConstString 224 // string pool 225 uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString(); 226 } 227 228 if (uuid_cstr && uuid_cstr[0]) 229 { 230 if (log) 231 log->Printf ("SBModule(%p)::GetUUIDString () => %s", static_cast<void*>(module_sp.get()), uuid_cstr); 232 return uuid_cstr; 233 } 234 235 if (log) 236 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", static_cast<void*>(module_sp.get())); 237 return NULL; 238 } 239 240 241 bool 242 SBModule::operator == (const SBModule &rhs) const 243 { 244 if (m_opaque_sp) 245 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 246 return false; 247 } 248 249 bool 250 SBModule::operator != (const SBModule &rhs) const 251 { 252 if (m_opaque_sp) 253 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 254 return false; 255 } 256 257 ModuleSP 258 SBModule::GetSP () const 259 { 260 return m_opaque_sp; 261 } 262 263 void 264 SBModule::SetSP (const ModuleSP &module_sp) 265 { 266 m_opaque_sp = module_sp; 267 } 268 269 SBAddress 270 SBModule::ResolveFileAddress (lldb::addr_t vm_addr) 271 { 272 lldb::SBAddress sb_addr; 273 ModuleSP module_sp (GetSP ()); 274 if (module_sp) 275 { 276 Address addr; 277 if (module_sp->ResolveFileAddress (vm_addr, addr)) 278 sb_addr.ref() = addr; 279 } 280 return sb_addr; 281 } 282 283 SBSymbolContext 284 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope) 285 { 286 SBSymbolContext sb_sc; 287 ModuleSP module_sp (GetSP ()); 288 if (module_sp && addr.IsValid()) 289 module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc); 290 return sb_sc; 291 } 292 293 bool 294 SBModule::GetDescription (SBStream &description) 295 { 296 Stream &strm = description.ref(); 297 298 ModuleSP module_sp (GetSP ()); 299 if (module_sp) 300 { 301 module_sp->GetDescription (&strm); 302 } 303 else 304 strm.PutCString ("No value"); 305 306 return true; 307 } 308 309 uint32_t 310 SBModule::GetNumCompileUnits() 311 { 312 ModuleSP module_sp (GetSP ()); 313 if (module_sp) 314 { 315 return module_sp->GetNumCompileUnits (); 316 } 317 return 0; 318 } 319 320 SBCompileUnit 321 SBModule::GetCompileUnitAtIndex (uint32_t index) 322 { 323 SBCompileUnit sb_cu; 324 ModuleSP module_sp (GetSP ()); 325 if (module_sp) 326 { 327 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index); 328 sb_cu.reset(cu_sp.get()); 329 } 330 return sb_cu; 331 } 332 333 static Symtab * 334 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp) 335 { 336 if (module_sp) 337 { 338 SymbolVendor *symbols = module_sp->GetSymbolVendor(); 339 if (symbols) 340 return symbols->GetSymtab(); 341 } 342 return NULL; 343 } 344 345 size_t 346 SBModule::GetNumSymbols () 347 { 348 ModuleSP module_sp (GetSP ()); 349 if (module_sp) 350 { 351 Symtab *symtab = GetUnifiedSymbolTable (module_sp); 352 if (symtab) 353 return symtab->GetNumSymbols(); 354 } 355 return 0; 356 } 357 358 SBSymbol 359 SBModule::GetSymbolAtIndex (size_t idx) 360 { 361 SBSymbol sb_symbol; 362 ModuleSP module_sp (GetSP ()); 363 Symtab *symtab = GetUnifiedSymbolTable (module_sp); 364 if (symtab) 365 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx)); 366 return sb_symbol; 367 } 368 369 lldb::SBSymbol 370 SBModule::FindSymbol (const char *name, 371 lldb::SymbolType symbol_type) 372 { 373 SBSymbol sb_symbol; 374 if (name && name[0]) 375 { 376 ModuleSP module_sp (GetSP ()); 377 Symtab *symtab = GetUnifiedSymbolTable (module_sp); 378 if (symtab) 379 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny)); 380 } 381 return sb_symbol; 382 } 383 384 385 lldb::SBSymbolContextList 386 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type) 387 { 388 SBSymbolContextList sb_sc_list; 389 if (name && name[0]) 390 { 391 ModuleSP module_sp (GetSP ()); 392 Symtab *symtab = GetUnifiedSymbolTable (module_sp); 393 if (symtab) 394 { 395 std::vector<uint32_t> matching_symbol_indexes; 396 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes); 397 if (num_matches) 398 { 399 SymbolContext sc; 400 sc.module_sp = module_sp; 401 SymbolContextList &sc_list = *sb_sc_list; 402 for (size_t i=0; i<num_matches; ++i) 403 { 404 sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]); 405 if (sc.symbol) 406 sc_list.Append(sc); 407 } 408 } 409 } 410 } 411 return sb_sc_list; 412 413 } 414 415 416 417 size_t 418 SBModule::GetNumSections () 419 { 420 ModuleSP module_sp (GetSP ()); 421 if (module_sp) 422 { 423 // Give the symbol vendor a chance to add to the unified section list. 424 module_sp->GetSymbolVendor(); 425 SectionList *section_list = module_sp->GetSectionList(); 426 if (section_list) 427 return section_list->GetSize(); 428 } 429 return 0; 430 } 431 432 SBSection 433 SBModule::GetSectionAtIndex (size_t idx) 434 { 435 SBSection sb_section; 436 ModuleSP module_sp (GetSP ()); 437 if (module_sp) 438 { 439 // Give the symbol vendor a chance to add to the unified section list. 440 module_sp->GetSymbolVendor(); 441 SectionList *section_list = module_sp->GetSectionList (); 442 443 if (section_list) 444 sb_section.SetSP(section_list->GetSectionAtIndex (idx)); 445 } 446 return sb_section; 447 } 448 449 lldb::SBSymbolContextList 450 SBModule::FindFunctions (const char *name, 451 uint32_t name_type_mask) 452 { 453 lldb::SBSymbolContextList sb_sc_list; 454 ModuleSP module_sp (GetSP ()); 455 if (name && module_sp) 456 { 457 const bool append = true; 458 const bool symbols_ok = true; 459 const bool inlines_ok = true; 460 module_sp->FindFunctions (ConstString(name), 461 NULL, 462 name_type_mask, 463 symbols_ok, 464 inlines_ok, 465 append, 466 *sb_sc_list); 467 } 468 return sb_sc_list; 469 } 470 471 472 SBValueList 473 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches) 474 { 475 SBValueList sb_value_list; 476 ModuleSP module_sp (GetSP ()); 477 if (name && module_sp) 478 { 479 VariableList variable_list; 480 const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name), 481 NULL, 482 false, 483 max_matches, 484 variable_list); 485 486 if (match_count > 0) 487 { 488 for (uint32_t i=0; i<match_count; ++i) 489 { 490 lldb::ValueObjectSP valobj_sp; 491 TargetSP target_sp (target.GetSP()); 492 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i)); 493 if (valobj_sp) 494 sb_value_list.Append(SBValue(valobj_sp)); 495 } 496 } 497 } 498 499 return sb_value_list; 500 } 501 502 lldb::SBValue 503 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name) 504 { 505 SBValueList sb_value_list(FindGlobalVariables(target, name, 1)); 506 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 507 return sb_value_list.GetValueAtIndex(0); 508 return SBValue(); 509 } 510 511 lldb::SBType 512 SBModule::FindFirstType (const char *name_cstr) 513 { 514 SBType sb_type; 515 ModuleSP module_sp (GetSP ()); 516 if (name_cstr && module_sp) 517 { 518 SymbolContext sc; 519 const bool exact_match = false; 520 ConstString name(name_cstr); 521 522 sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match)); 523 524 if (!sb_type.IsValid()) 525 { 526 TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 527 if (type_system) 528 sb_type = SBType (type_system->GetBuiltinTypeByName(name)); 529 } 530 } 531 return sb_type; 532 } 533 534 lldb::SBType 535 SBModule::GetBasicType(lldb::BasicType type) 536 { 537 ModuleSP module_sp (GetSP ()); 538 if (module_sp) 539 { 540 TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 541 if (type_system) 542 return SBType (type_system->GetBasicTypeFromAST(type)); 543 } 544 return SBType(); 545 } 546 547 lldb::SBTypeList 548 SBModule::FindTypes (const char *type) 549 { 550 SBTypeList retval; 551 552 ModuleSP module_sp (GetSP ()); 553 if (type && module_sp) 554 { 555 SymbolContext sc; 556 TypeList type_list; 557 const bool exact_match = false; 558 ConstString name(type); 559 llvm::DenseSet<SymbolFile *> searched_symbol_files; 560 const uint32_t num_matches = module_sp->FindTypes (sc, 561 name, 562 exact_match, 563 UINT32_MAX, 564 searched_symbol_files, 565 type_list); 566 567 if (num_matches > 0) 568 { 569 for (size_t idx = 0; idx < num_matches; idx++) 570 { 571 TypeSP type_sp (type_list.GetTypeAtIndex(idx)); 572 if (type_sp) 573 retval.Append(SBType(type_sp)); 574 } 575 } 576 else 577 { 578 TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 579 if (type_system) 580 { 581 CompilerType compiler_type = type_system->GetBuiltinTypeByName(name); 582 if (compiler_type) 583 retval.Append(SBType(compiler_type)); 584 } 585 } 586 } 587 588 return retval; 589 } 590 591 lldb::SBType 592 SBModule::GetTypeByID (lldb::user_id_t uid) 593 { 594 ModuleSP module_sp (GetSP ()); 595 if (module_sp) 596 { 597 SymbolVendor* vendor = module_sp->GetSymbolVendor(); 598 if (vendor) 599 { 600 Type *type_ptr = vendor->ResolveTypeUID(uid); 601 if (type_ptr) 602 return SBType(type_ptr->shared_from_this()); 603 } 604 } 605 return SBType(); 606 } 607 608 lldb::SBTypeList 609 SBModule::GetTypes (uint32_t type_mask) 610 { 611 SBTypeList sb_type_list; 612 613 ModuleSP module_sp (GetSP ()); 614 if (module_sp) 615 { 616 SymbolVendor* vendor = module_sp->GetSymbolVendor(); 617 if (vendor) 618 { 619 TypeList type_list; 620 vendor->GetTypes (NULL, type_mask, type_list); 621 sb_type_list.m_opaque_ap->Append(type_list); 622 } 623 } 624 return sb_type_list; 625 } 626 627 SBSection 628 SBModule::FindSection (const char *sect_name) 629 { 630 SBSection sb_section; 631 632 ModuleSP module_sp (GetSP ()); 633 if (sect_name && module_sp) 634 { 635 // Give the symbol vendor a chance to add to the unified section list. 636 module_sp->GetSymbolVendor(); 637 SectionList *section_list = module_sp->GetSectionList(); 638 if (section_list) 639 { 640 ConstString const_sect_name(sect_name); 641 SectionSP section_sp (section_list->FindSectionByName(const_sect_name)); 642 if (section_sp) 643 { 644 sb_section.SetSP (section_sp); 645 } 646 } 647 } 648 return sb_section; 649 } 650 651 lldb::ByteOrder 652 SBModule::GetByteOrder () 653 { 654 ModuleSP module_sp (GetSP ()); 655 if (module_sp) 656 return module_sp->GetArchitecture().GetByteOrder(); 657 return eByteOrderInvalid; 658 } 659 660 const char * 661 SBModule::GetTriple () 662 { 663 ModuleSP module_sp (GetSP ()); 664 if (module_sp) 665 { 666 std::string triple (module_sp->GetArchitecture().GetTriple().str()); 667 // Unique the string so we don't run into ownership issues since 668 // the const strings put the string into the string pool once and 669 // the strings never comes out 670 ConstString const_triple (triple.c_str()); 671 return const_triple.GetCString(); 672 } 673 return NULL; 674 } 675 676 uint32_t 677 SBModule::GetAddressByteSize() 678 { 679 ModuleSP module_sp (GetSP ()); 680 if (module_sp) 681 return module_sp->GetArchitecture().GetAddressByteSize(); 682 return sizeof(void*); 683 } 684 685 686 uint32_t 687 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions) 688 { 689 ModuleSP module_sp (GetSP ()); 690 if (module_sp) 691 return module_sp->GetVersion(versions, num_versions); 692 else 693 { 694 if (versions && num_versions) 695 { 696 for (uint32_t i=0; i<num_versions; ++i) 697 versions[i] = UINT32_MAX; 698 } 699 return 0; 700 } 701 } 702 703 lldb::SBFileSpec 704 SBModule::GetSymbolFileSpec() const 705 { 706 lldb::SBFileSpec sb_file_spec; 707 ModuleSP module_sp(GetSP()); 708 if (module_sp) 709 { 710 SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor(); 711 if (symbol_vendor_ptr) 712 sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec()); 713 } 714 return sb_file_spec; 715 } 716 717 lldb::SBAddress 718 SBModule::GetObjectFileHeaderAddress() const 719 { 720 lldb::SBAddress sb_addr; 721 ModuleSP module_sp (GetSP ()); 722 if (module_sp) 723 { 724 ObjectFile *objfile_ptr = module_sp->GetObjectFile(); 725 if (objfile_ptr) 726 sb_addr.ref() = objfile_ptr->GetHeaderAddress(); 727 } 728 return sb_addr; 729 } 730