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