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