Path: blob/main/contrib/llvm-project/lldb/source/API/SBModule.cpp
39587 views
//===-- SBModule.cpp ------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBModule.h"9#include "lldb/API/SBAddress.h"10#include "lldb/API/SBFileSpec.h"11#include "lldb/API/SBModuleSpec.h"12#include "lldb/API/SBProcess.h"13#include "lldb/API/SBStream.h"14#include "lldb/API/SBSymbolContextList.h"15#include "lldb/Core/Module.h"16#include "lldb/Core/Section.h"17#include "lldb/Core/ValueObjectList.h"18#include "lldb/Core/ValueObjectVariable.h"19#include "lldb/Symbol/ObjectFile.h"20#include "lldb/Symbol/SymbolFile.h"21#include "lldb/Symbol/Symtab.h"22#include "lldb/Symbol/TypeSystem.h"23#include "lldb/Symbol/VariableList.h"24#include "lldb/Target/Target.h"25#include "lldb/Utility/Instrumentation.h"26#include "lldb/Utility/StreamString.h"2728using namespace lldb;29using namespace lldb_private;3031SBModule::SBModule() { LLDB_INSTRUMENT_VA(this); }3233SBModule::SBModule(const lldb::ModuleSP &module_sp) : m_opaque_sp(module_sp) {}3435SBModule::SBModule(const SBModuleSpec &module_spec) {36LLDB_INSTRUMENT_VA(this, module_spec);3738ModuleSP module_sp;39Status error = ModuleList::GetSharedModule(40*module_spec.m_opaque_up, module_sp, nullptr, nullptr, nullptr);41if (module_sp)42SetSP(module_sp);43}4445SBModule::SBModule(const SBModule &rhs) : m_opaque_sp(rhs.m_opaque_sp) {46LLDB_INSTRUMENT_VA(this, rhs);47}4849SBModule::SBModule(lldb::SBProcess &process, lldb::addr_t header_addr) {50LLDB_INSTRUMENT_VA(this, process, header_addr);5152ProcessSP process_sp(process.GetSP());53if (process_sp) {54m_opaque_sp = process_sp->ReadModuleFromMemory(FileSpec(), header_addr);55if (m_opaque_sp) {56Target &target = process_sp->GetTarget();57bool changed = false;58m_opaque_sp->SetLoadAddress(target, 0, true, changed);59target.GetImages().Append(m_opaque_sp);60}61}62}6364const SBModule &SBModule::operator=(const SBModule &rhs) {65LLDB_INSTRUMENT_VA(this, rhs);6667if (this != &rhs)68m_opaque_sp = rhs.m_opaque_sp;69return *this;70}7172SBModule::~SBModule() = default;7374bool SBModule::IsValid() const {75LLDB_INSTRUMENT_VA(this);76return this->operator bool();77}78SBModule::operator bool() const {79LLDB_INSTRUMENT_VA(this);8081return m_opaque_sp.get() != nullptr;82}8384void SBModule::Clear() {85LLDB_INSTRUMENT_VA(this);8687m_opaque_sp.reset();88}8990bool SBModule::IsFileBacked() const {91LLDB_INSTRUMENT_VA(this);9293ModuleSP module_sp(GetSP());94if (!module_sp)95return false;9697ObjectFile *obj_file = module_sp->GetObjectFile();98if (!obj_file)99return false;100101return !obj_file->IsInMemory();102}103104SBFileSpec SBModule::GetFileSpec() const {105LLDB_INSTRUMENT_VA(this);106107SBFileSpec file_spec;108ModuleSP module_sp(GetSP());109if (module_sp)110file_spec.SetFileSpec(module_sp->GetFileSpec());111112return file_spec;113}114115lldb::SBFileSpec SBModule::GetPlatformFileSpec() const {116LLDB_INSTRUMENT_VA(this);117118SBFileSpec file_spec;119ModuleSP module_sp(GetSP());120if (module_sp)121file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());122123return file_spec;124}125126bool SBModule::SetPlatformFileSpec(const lldb::SBFileSpec &platform_file) {127LLDB_INSTRUMENT_VA(this, platform_file);128129bool result = false;130131ModuleSP module_sp(GetSP());132if (module_sp) {133module_sp->SetPlatformFileSpec(*platform_file);134result = true;135}136137return result;138}139140lldb::SBFileSpec SBModule::GetRemoteInstallFileSpec() {141LLDB_INSTRUMENT_VA(this);142143SBFileSpec sb_file_spec;144ModuleSP module_sp(GetSP());145if (module_sp)146sb_file_spec.SetFileSpec(module_sp->GetRemoteInstallFileSpec());147return sb_file_spec;148}149150bool SBModule::SetRemoteInstallFileSpec(lldb::SBFileSpec &file) {151LLDB_INSTRUMENT_VA(this, file);152153ModuleSP module_sp(GetSP());154if (module_sp) {155module_sp->SetRemoteInstallFileSpec(file.ref());156return true;157}158return false;159}160161const uint8_t *SBModule::GetUUIDBytes() const {162LLDB_INSTRUMENT_VA(this);163164const uint8_t *uuid_bytes = nullptr;165ModuleSP module_sp(GetSP());166if (module_sp)167uuid_bytes = module_sp->GetUUID().GetBytes().data();168169return uuid_bytes;170}171172const char *SBModule::GetUUIDString() const {173LLDB_INSTRUMENT_VA(this);174175ModuleSP module_sp(GetSP());176if (!module_sp)177return nullptr;178179// We are going to return a "const char *" value through the public API, so180// we need to constify it so it gets added permanently the string pool and181// then we don't need to worry about the lifetime of the string as it will182// never go away once it has been put into the ConstString string pool183const char *uuid_cstr =184ConstString(module_sp->GetUUID().GetAsString()).GetCString();185// Note: SBModule::GetUUIDString's expected behavior is to return nullptr if186// the string we get is empty, so we must perform this check before returning.187if (uuid_cstr && uuid_cstr[0])188return uuid_cstr;189return nullptr;190}191192bool SBModule::operator==(const SBModule &rhs) const {193LLDB_INSTRUMENT_VA(this, rhs);194195if (m_opaque_sp)196return m_opaque_sp.get() == rhs.m_opaque_sp.get();197return false;198}199200bool SBModule::operator!=(const SBModule &rhs) const {201LLDB_INSTRUMENT_VA(this, rhs);202203if (m_opaque_sp)204return m_opaque_sp.get() != rhs.m_opaque_sp.get();205return false;206}207208ModuleSP SBModule::GetSP() const { return m_opaque_sp; }209210void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; }211212SBAddress SBModule::ResolveFileAddress(lldb::addr_t vm_addr) {213LLDB_INSTRUMENT_VA(this, vm_addr);214215lldb::SBAddress sb_addr;216ModuleSP module_sp(GetSP());217if (module_sp) {218Address addr;219if (module_sp->ResolveFileAddress(vm_addr, addr))220sb_addr.ref() = addr;221}222return sb_addr;223}224225SBSymbolContext226SBModule::ResolveSymbolContextForAddress(const SBAddress &addr,227uint32_t resolve_scope) {228LLDB_INSTRUMENT_VA(this, addr, resolve_scope);229230SBSymbolContext sb_sc;231ModuleSP module_sp(GetSP());232SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);233if (module_sp && addr.IsValid())234module_sp->ResolveSymbolContextForAddress(addr.ref(), scope, *sb_sc);235return sb_sc;236}237238bool SBModule::GetDescription(SBStream &description) {239LLDB_INSTRUMENT_VA(this, description);240241Stream &strm = description.ref();242243ModuleSP module_sp(GetSP());244if (module_sp) {245module_sp->GetDescription(strm.AsRawOstream());246} else247strm.PutCString("No value");248249return true;250}251252uint32_t SBModule::GetNumCompileUnits() {253LLDB_INSTRUMENT_VA(this);254255ModuleSP module_sp(GetSP());256if (module_sp) {257return module_sp->GetNumCompileUnits();258}259return 0;260}261262SBCompileUnit SBModule::GetCompileUnitAtIndex(uint32_t index) {263LLDB_INSTRUMENT_VA(this, index);264265SBCompileUnit sb_cu;266ModuleSP module_sp(GetSP());267if (module_sp) {268CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(index);269sb_cu.reset(cu_sp.get());270}271return sb_cu;272}273274SBSymbolContextList SBModule::FindCompileUnits(const SBFileSpec &sb_file_spec) {275LLDB_INSTRUMENT_VA(this, sb_file_spec);276277SBSymbolContextList sb_sc_list;278const ModuleSP module_sp(GetSP());279if (sb_file_spec.IsValid() && module_sp) {280module_sp->FindCompileUnits(*sb_file_spec, *sb_sc_list);281}282return sb_sc_list;283}284285static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) {286if (module_sp)287return module_sp->GetSymtab();288return nullptr;289}290291size_t SBModule::GetNumSymbols() {292LLDB_INSTRUMENT_VA(this);293294ModuleSP module_sp(GetSP());295if (Symtab *symtab = GetUnifiedSymbolTable(module_sp))296return symtab->GetNumSymbols();297return 0;298}299300SBSymbol SBModule::GetSymbolAtIndex(size_t idx) {301LLDB_INSTRUMENT_VA(this, idx);302303SBSymbol sb_symbol;304ModuleSP module_sp(GetSP());305Symtab *symtab = GetUnifiedSymbolTable(module_sp);306if (symtab)307sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx));308return sb_symbol;309}310311lldb::SBSymbol SBModule::FindSymbol(const char *name,312lldb::SymbolType symbol_type) {313LLDB_INSTRUMENT_VA(this, name, symbol_type);314315SBSymbol sb_symbol;316if (name && name[0]) {317ModuleSP module_sp(GetSP());318Symtab *symtab = GetUnifiedSymbolTable(module_sp);319if (symtab)320sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(321ConstString(name), symbol_type, Symtab::eDebugAny,322Symtab::eVisibilityAny));323}324return sb_symbol;325}326327lldb::SBSymbolContextList SBModule::FindSymbols(const char *name,328lldb::SymbolType symbol_type) {329LLDB_INSTRUMENT_VA(this, name, symbol_type);330331SBSymbolContextList sb_sc_list;332if (name && name[0]) {333ModuleSP module_sp(GetSP());334Symtab *symtab = GetUnifiedSymbolTable(module_sp);335if (symtab) {336std::vector<uint32_t> matching_symbol_indexes;337symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type,338matching_symbol_indexes);339const size_t num_matches = matching_symbol_indexes.size();340if (num_matches) {341SymbolContext sc;342sc.module_sp = module_sp;343SymbolContextList &sc_list = *sb_sc_list;344for (size_t i = 0; i < num_matches; ++i) {345sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]);346if (sc.symbol)347sc_list.Append(sc);348}349}350}351}352return sb_sc_list;353}354355size_t SBModule::GetNumSections() {356LLDB_INSTRUMENT_VA(this);357358ModuleSP module_sp(GetSP());359if (module_sp) {360// Give the symbol vendor a chance to add to the unified section list.361module_sp->GetSymbolFile();362SectionList *section_list = module_sp->GetSectionList();363if (section_list)364return section_list->GetSize();365}366return 0;367}368369SBSection SBModule::GetSectionAtIndex(size_t idx) {370LLDB_INSTRUMENT_VA(this, idx);371372SBSection sb_section;373ModuleSP module_sp(GetSP());374if (module_sp) {375// Give the symbol vendor a chance to add to the unified section list.376module_sp->GetSymbolFile();377SectionList *section_list = module_sp->GetSectionList();378379if (section_list)380sb_section.SetSP(section_list->GetSectionAtIndex(idx));381}382return sb_section;383}384385lldb::SBSymbolContextList SBModule::FindFunctions(const char *name,386uint32_t name_type_mask) {387LLDB_INSTRUMENT_VA(this, name, name_type_mask);388389lldb::SBSymbolContextList sb_sc_list;390ModuleSP module_sp(GetSP());391if (name && module_sp) {392393ModuleFunctionSearchOptions function_options;394function_options.include_symbols = true;395function_options.include_inlines = true;396FunctionNameType type = static_cast<FunctionNameType>(name_type_mask);397module_sp->FindFunctions(ConstString(name), CompilerDeclContext(), type,398function_options, *sb_sc_list);399}400return sb_sc_list;401}402403SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,404uint32_t max_matches) {405LLDB_INSTRUMENT_VA(this, target, name, max_matches);406407SBValueList sb_value_list;408ModuleSP module_sp(GetSP());409if (name && module_sp) {410VariableList variable_list;411module_sp->FindGlobalVariables(ConstString(name), CompilerDeclContext(),412max_matches, variable_list);413for (const VariableSP &var_sp : variable_list) {414lldb::ValueObjectSP valobj_sp;415TargetSP target_sp(target.GetSP());416valobj_sp = ValueObjectVariable::Create(target_sp.get(), var_sp);417if (valobj_sp)418sb_value_list.Append(SBValue(valobj_sp));419}420}421422return sb_value_list;423}424425lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target,426const char *name) {427LLDB_INSTRUMENT_VA(this, target, name);428429SBValueList sb_value_list(FindGlobalVariables(target, name, 1));430if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)431return sb_value_list.GetValueAtIndex(0);432return SBValue();433}434435lldb::SBType SBModule::FindFirstType(const char *name_cstr) {436LLDB_INSTRUMENT_VA(this, name_cstr);437438ModuleSP module_sp(GetSP());439if (name_cstr && module_sp) {440ConstString name(name_cstr);441TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_find_one);442TypeResults results;443module_sp->FindTypes(query, results);444TypeSP type_sp = results.GetFirstType();445if (type_sp)446return SBType(type_sp);447448auto type_system_or_err =449module_sp->GetTypeSystemForLanguage(eLanguageTypeC);450if (auto err = type_system_or_err.takeError()) {451llvm::consumeError(std::move(err));452return {};453}454455if (auto ts = *type_system_or_err)456return SBType(ts->GetBuiltinTypeByName(name));457}458return {};459}460461lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {462LLDB_INSTRUMENT_VA(this, type);463464ModuleSP module_sp(GetSP());465if (module_sp) {466auto type_system_or_err =467module_sp->GetTypeSystemForLanguage(eLanguageTypeC);468if (auto err = type_system_or_err.takeError()) {469llvm::consumeError(std::move(err));470} else {471if (auto ts = *type_system_or_err)472return SBType(ts->GetBasicTypeFromAST(type));473}474}475return SBType();476}477478lldb::SBTypeList SBModule::FindTypes(const char *type) {479LLDB_INSTRUMENT_VA(this, type);480481SBTypeList retval;482483ModuleSP module_sp(GetSP());484if (type && module_sp) {485TypeList type_list;486TypeQuery query(type);487TypeResults results;488module_sp->FindTypes(query, results);489if (results.GetTypeMap().Empty()) {490ConstString name(type);491auto type_system_or_err =492module_sp->GetTypeSystemForLanguage(eLanguageTypeC);493if (auto err = type_system_or_err.takeError()) {494llvm::consumeError(std::move(err));495} else {496if (auto ts = *type_system_or_err)497if (CompilerType compiler_type = ts->GetBuiltinTypeByName(name))498retval.Append(SBType(compiler_type));499}500} else {501for (const TypeSP &type_sp : results.GetTypeMap().Types())502retval.Append(SBType(type_sp));503}504}505return retval;506}507508lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {509LLDB_INSTRUMENT_VA(this, uid);510511ModuleSP module_sp(GetSP());512if (module_sp) {513if (SymbolFile *symfile = module_sp->GetSymbolFile()) {514Type *type_ptr = symfile->ResolveTypeUID(uid);515if (type_ptr)516return SBType(type_ptr->shared_from_this());517}518}519return SBType();520}521522lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {523LLDB_INSTRUMENT_VA(this, type_mask);524525SBTypeList sb_type_list;526527ModuleSP module_sp(GetSP());528if (!module_sp)529return sb_type_list;530SymbolFile *symfile = module_sp->GetSymbolFile();531if (!symfile)532return sb_type_list;533534TypeClass type_class = static_cast<TypeClass>(type_mask);535TypeList type_list;536symfile->GetTypes(nullptr, type_class, type_list);537sb_type_list.m_opaque_up->Append(type_list);538return sb_type_list;539}540541SBSection SBModule::FindSection(const char *sect_name) {542LLDB_INSTRUMENT_VA(this, sect_name);543544SBSection sb_section;545546ModuleSP module_sp(GetSP());547if (sect_name && module_sp) {548// Give the symbol vendor a chance to add to the unified section list.549module_sp->GetSymbolFile();550SectionList *section_list = module_sp->GetSectionList();551if (section_list) {552ConstString const_sect_name(sect_name);553SectionSP section_sp(section_list->FindSectionByName(const_sect_name));554if (section_sp) {555sb_section.SetSP(section_sp);556}557}558}559return sb_section;560}561562lldb::ByteOrder SBModule::GetByteOrder() {563LLDB_INSTRUMENT_VA(this);564565ModuleSP module_sp(GetSP());566if (module_sp)567return module_sp->GetArchitecture().GetByteOrder();568return eByteOrderInvalid;569}570571const char *SBModule::GetTriple() {572LLDB_INSTRUMENT_VA(this);573574ModuleSP module_sp(GetSP());575if (!module_sp)576return nullptr;577578std::string triple(module_sp->GetArchitecture().GetTriple().str());579// Unique the string so we don't run into ownership issues since the const580// strings put the string into the string pool once and the strings never581// comes out582ConstString const_triple(triple.c_str());583return const_triple.GetCString();584}585586uint32_t SBModule::GetAddressByteSize() {587LLDB_INSTRUMENT_VA(this);588589ModuleSP module_sp(GetSP());590if (module_sp)591return module_sp->GetArchitecture().GetAddressByteSize();592return sizeof(void *);593}594595uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {596LLDB_INSTRUMENT_VA(this, versions, num_versions);597598llvm::VersionTuple version;599if (ModuleSP module_sp = GetSP())600version = module_sp->GetVersion();601uint32_t result = 0;602if (!version.empty())603++result;604if (version.getMinor())605++result;606if (version.getSubminor())607++result;608609if (!versions)610return result;611612if (num_versions > 0)613versions[0] = version.empty() ? UINT32_MAX : version.getMajor();614if (num_versions > 1)615versions[1] = version.getMinor().value_or(UINT32_MAX);616if (num_versions > 2)617versions[2] = version.getSubminor().value_or(UINT32_MAX);618for (uint32_t i = 3; i < num_versions; ++i)619versions[i] = UINT32_MAX;620return result;621}622623lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {624LLDB_INSTRUMENT_VA(this);625626lldb::SBFileSpec sb_file_spec;627ModuleSP module_sp(GetSP());628if (module_sp) {629if (SymbolFile *symfile = module_sp->GetSymbolFile())630sb_file_spec.SetFileSpec(symfile->GetObjectFile()->GetFileSpec());631}632return sb_file_spec;633}634635lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const {636LLDB_INSTRUMENT_VA(this);637638lldb::SBAddress sb_addr;639ModuleSP module_sp(GetSP());640if (module_sp) {641ObjectFile *objfile_ptr = module_sp->GetObjectFile();642if (objfile_ptr)643sb_addr.ref() = objfile_ptr->GetBaseAddress();644}645return sb_addr;646}647648lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {649LLDB_INSTRUMENT_VA(this);650651lldb::SBAddress sb_addr;652ModuleSP module_sp(GetSP());653if (module_sp) {654ObjectFile *objfile_ptr = module_sp->GetObjectFile();655if (objfile_ptr)656sb_addr.ref() = objfile_ptr->GetEntryPointAddress();657}658return sb_addr;659}660661uint32_t SBModule::GetNumberAllocatedModules() {662LLDB_INSTRUMENT();663664return Module::GetNumberAllocatedModules();665}666667void SBModule::GarbageCollectAllocatedModules() {668LLDB_INSTRUMENT();669670const bool mandatory = false;671ModuleList::RemoveOrphanSharedModules(mandatory);672}673674675