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