1 //===-- SBModule.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Symbol/Symtab.h"
21 #include "lldb/Symbol/TypeSystem.h"
22 #include "lldb/Symbol/VariableList.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/Instrumentation.h"
25 #include "lldb/Utility/StreamString.h"
26 #include "lldb/ValueObject/ValueObjectList.h"
27 #include "lldb/ValueObject/ValueObjectVariable.h"
30 using namespace lldb_private
;
32 SBModule::SBModule() { LLDB_INSTRUMENT_VA(this); }
34 SBModule::SBModule(const lldb::ModuleSP
&module_sp
) : m_opaque_sp(module_sp
) {}
36 SBModule::SBModule(const SBModuleSpec
&module_spec
) {
37 LLDB_INSTRUMENT_VA(this, module_spec
);
40 Status error
= ModuleList::GetSharedModule(
41 *module_spec
.m_opaque_up
, module_sp
, nullptr, nullptr, nullptr);
46 SBModule::SBModule(const SBModule
&rhs
) : m_opaque_sp(rhs
.m_opaque_sp
) {
47 LLDB_INSTRUMENT_VA(this, rhs
);
50 SBModule::SBModule(lldb::SBProcess
&process
, lldb::addr_t header_addr
) {
51 LLDB_INSTRUMENT_VA(this, process
, header_addr
);
53 ProcessSP
process_sp(process
.GetSP());
55 m_opaque_sp
= process_sp
->ReadModuleFromMemory(FileSpec(), header_addr
);
57 Target
&target
= process_sp
->GetTarget();
59 m_opaque_sp
->SetLoadAddress(target
, 0, true, changed
);
60 target
.GetImages().Append(m_opaque_sp
);
65 const SBModule
&SBModule::operator=(const SBModule
&rhs
) {
66 LLDB_INSTRUMENT_VA(this, rhs
);
69 m_opaque_sp
= rhs
.m_opaque_sp
;
73 SBModule::~SBModule() = default;
75 bool SBModule::IsValid() const {
76 LLDB_INSTRUMENT_VA(this);
77 return this->operator bool();
79 SBModule::operator bool() const {
80 LLDB_INSTRUMENT_VA(this);
82 return m_opaque_sp
.get() != nullptr;
85 void SBModule::Clear() {
86 LLDB_INSTRUMENT_VA(this);
91 bool SBModule::IsFileBacked() const {
92 LLDB_INSTRUMENT_VA(this);
94 ModuleSP
module_sp(GetSP());
98 ObjectFile
*obj_file
= module_sp
->GetObjectFile();
102 return !obj_file
->IsInMemory();
105 SBFileSpec
SBModule::GetFileSpec() const {
106 LLDB_INSTRUMENT_VA(this);
108 SBFileSpec file_spec
;
109 ModuleSP
module_sp(GetSP());
111 file_spec
.SetFileSpec(module_sp
->GetFileSpec());
116 lldb::SBFileSpec
SBModule::GetPlatformFileSpec() const {
117 LLDB_INSTRUMENT_VA(this);
119 SBFileSpec file_spec
;
120 ModuleSP
module_sp(GetSP());
122 file_spec
.SetFileSpec(module_sp
->GetPlatformFileSpec());
127 bool SBModule::SetPlatformFileSpec(const lldb::SBFileSpec
&platform_file
) {
128 LLDB_INSTRUMENT_VA(this, platform_file
);
132 ModuleSP
module_sp(GetSP());
134 module_sp
->SetPlatformFileSpec(*platform_file
);
141 lldb::SBFileSpec
SBModule::GetRemoteInstallFileSpec() {
142 LLDB_INSTRUMENT_VA(this);
144 SBFileSpec sb_file_spec
;
145 ModuleSP
module_sp(GetSP());
147 sb_file_spec
.SetFileSpec(module_sp
->GetRemoteInstallFileSpec());
151 bool SBModule::SetRemoteInstallFileSpec(lldb::SBFileSpec
&file
) {
152 LLDB_INSTRUMENT_VA(this, file
);
154 ModuleSP
module_sp(GetSP());
156 module_sp
->SetRemoteInstallFileSpec(file
.ref());
162 const uint8_t *SBModule::GetUUIDBytes() const {
163 LLDB_INSTRUMENT_VA(this);
165 const uint8_t *uuid_bytes
= nullptr;
166 ModuleSP
module_sp(GetSP());
168 uuid_bytes
= module_sp
->GetUUID().GetBytes().data();
173 const char *SBModule::GetUUIDString() const {
174 LLDB_INSTRUMENT_VA(this);
176 ModuleSP
module_sp(GetSP());
180 // We are going to return a "const char *" value through the public API, so
181 // we need to constify it so it gets added permanently the string pool and
182 // then we don't need to worry about the lifetime of the string as it will
183 // never go away once it has been put into the ConstString string pool
184 const char *uuid_cstr
=
185 ConstString(module_sp
->GetUUID().GetAsString()).GetCString();
186 // Note: SBModule::GetUUIDString's expected behavior is to return nullptr if
187 // the string we get is empty, so we must perform this check before returning.
188 if (uuid_cstr
&& uuid_cstr
[0])
193 bool SBModule::operator==(const SBModule
&rhs
) const {
194 LLDB_INSTRUMENT_VA(this, rhs
);
197 return m_opaque_sp
.get() == rhs
.m_opaque_sp
.get();
201 bool SBModule::operator!=(const SBModule
&rhs
) const {
202 LLDB_INSTRUMENT_VA(this, rhs
);
205 return m_opaque_sp
.get() != rhs
.m_opaque_sp
.get();
209 ModuleSP
SBModule::GetSP() const { return m_opaque_sp
; }
211 void SBModule::SetSP(const ModuleSP
&module_sp
) { m_opaque_sp
= module_sp
; }
213 SBAddress
SBModule::ResolveFileAddress(lldb::addr_t vm_addr
) {
214 LLDB_INSTRUMENT_VA(this, vm_addr
);
216 lldb::SBAddress sb_addr
;
217 ModuleSP
module_sp(GetSP());
220 if (module_sp
->ResolveFileAddress(vm_addr
, addr
))
221 sb_addr
.ref() = addr
;
227 SBModule::ResolveSymbolContextForAddress(const SBAddress
&addr
,
228 uint32_t resolve_scope
) {
229 LLDB_INSTRUMENT_VA(this, addr
, resolve_scope
);
231 SBSymbolContext sb_sc
;
232 ModuleSP
module_sp(GetSP());
233 SymbolContextItem scope
= static_cast<SymbolContextItem
>(resolve_scope
);
234 if (module_sp
&& addr
.IsValid())
235 module_sp
->ResolveSymbolContextForAddress(addr
.ref(), scope
, *sb_sc
);
239 bool SBModule::GetDescription(SBStream
&description
) {
240 LLDB_INSTRUMENT_VA(this, description
);
242 Stream
&strm
= description
.ref();
244 ModuleSP
module_sp(GetSP());
246 module_sp
->GetDescription(strm
.AsRawOstream());
248 strm
.PutCString("No value");
253 uint32_t SBModule::GetNumCompileUnits() {
254 LLDB_INSTRUMENT_VA(this);
256 ModuleSP
module_sp(GetSP());
258 return module_sp
->GetNumCompileUnits();
263 SBCompileUnit
SBModule::GetCompileUnitAtIndex(uint32_t index
) {
264 LLDB_INSTRUMENT_VA(this, index
);
267 ModuleSP
module_sp(GetSP());
269 CompUnitSP cu_sp
= module_sp
->GetCompileUnitAtIndex(index
);
270 sb_cu
.reset(cu_sp
.get());
275 SBSymbolContextList
SBModule::FindCompileUnits(const SBFileSpec
&sb_file_spec
) {
276 LLDB_INSTRUMENT_VA(this, sb_file_spec
);
278 SBSymbolContextList sb_sc_list
;
279 const ModuleSP
module_sp(GetSP());
280 if (sb_file_spec
.IsValid() && module_sp
) {
281 module_sp
->FindCompileUnits(*sb_file_spec
, *sb_sc_list
);
286 static Symtab
*GetUnifiedSymbolTable(const lldb::ModuleSP
&module_sp
) {
288 return module_sp
->GetSymtab();
292 size_t SBModule::GetNumSymbols() {
293 LLDB_INSTRUMENT_VA(this);
295 ModuleSP
module_sp(GetSP());
296 if (Symtab
*symtab
= GetUnifiedSymbolTable(module_sp
))
297 return symtab
->GetNumSymbols();
301 SBSymbol
SBModule::GetSymbolAtIndex(size_t idx
) {
302 LLDB_INSTRUMENT_VA(this, idx
);
305 ModuleSP
module_sp(GetSP());
306 Symtab
*symtab
= GetUnifiedSymbolTable(module_sp
);
308 sb_symbol
.SetSymbol(symtab
->SymbolAtIndex(idx
));
312 lldb::SBSymbol
SBModule::FindSymbol(const char *name
,
313 lldb::SymbolType symbol_type
) {
314 LLDB_INSTRUMENT_VA(this, name
, symbol_type
);
317 if (name
&& name
[0]) {
318 ModuleSP
module_sp(GetSP());
319 Symtab
*symtab
= GetUnifiedSymbolTable(module_sp
);
321 sb_symbol
.SetSymbol(symtab
->FindFirstSymbolWithNameAndType(
322 ConstString(name
), symbol_type
, Symtab::eDebugAny
,
323 Symtab::eVisibilityAny
));
328 lldb::SBSymbolContextList
SBModule::FindSymbols(const char *name
,
329 lldb::SymbolType symbol_type
) {
330 LLDB_INSTRUMENT_VA(this, name
, symbol_type
);
332 SBSymbolContextList sb_sc_list
;
333 if (name
&& name
[0]) {
334 ModuleSP
module_sp(GetSP());
335 Symtab
*symtab
= GetUnifiedSymbolTable(module_sp
);
337 std::vector
<uint32_t> matching_symbol_indexes
;
338 symtab
->FindAllSymbolsWithNameAndType(ConstString(name
), symbol_type
,
339 matching_symbol_indexes
);
340 const size_t num_matches
= matching_symbol_indexes
.size();
343 sc
.module_sp
= module_sp
;
344 SymbolContextList
&sc_list
= *sb_sc_list
;
345 for (size_t i
= 0; i
< num_matches
; ++i
) {
346 sc
.symbol
= symtab
->SymbolAtIndex(matching_symbol_indexes
[i
]);
356 size_t SBModule::GetNumSections() {
357 LLDB_INSTRUMENT_VA(this);
359 ModuleSP
module_sp(GetSP());
361 // Give the symbol vendor a chance to add to the unified section list.
362 module_sp
->GetSymbolFile();
363 SectionList
*section_list
= module_sp
->GetSectionList();
365 return section_list
->GetSize();
370 SBSection
SBModule::GetSectionAtIndex(size_t idx
) {
371 LLDB_INSTRUMENT_VA(this, idx
);
373 SBSection sb_section
;
374 ModuleSP
module_sp(GetSP());
376 // Give the symbol vendor a chance to add to the unified section list.
377 module_sp
->GetSymbolFile();
378 SectionList
*section_list
= module_sp
->GetSectionList();
381 sb_section
.SetSP(section_list
->GetSectionAtIndex(idx
));
386 lldb::SBSymbolContextList
SBModule::FindFunctions(const char *name
,
387 uint32_t name_type_mask
) {
388 LLDB_INSTRUMENT_VA(this, name
, name_type_mask
);
390 lldb::SBSymbolContextList sb_sc_list
;
391 ModuleSP
module_sp(GetSP());
392 if (name
&& module_sp
) {
394 ModuleFunctionSearchOptions function_options
;
395 function_options
.include_symbols
= true;
396 function_options
.include_inlines
= true;
397 FunctionNameType type
= static_cast<FunctionNameType
>(name_type_mask
);
398 module_sp
->FindFunctions(ConstString(name
), CompilerDeclContext(), type
,
399 function_options
, *sb_sc_list
);
404 SBValueList
SBModule::FindGlobalVariables(SBTarget
&target
, const char *name
,
405 uint32_t max_matches
) {
406 LLDB_INSTRUMENT_VA(this, target
, name
, max_matches
);
408 SBValueList sb_value_list
;
409 ModuleSP
module_sp(GetSP());
410 if (name
&& module_sp
) {
411 VariableList variable_list
;
412 module_sp
->FindGlobalVariables(ConstString(name
), CompilerDeclContext(),
413 max_matches
, variable_list
);
414 for (const VariableSP
&var_sp
: variable_list
) {
415 lldb::ValueObjectSP valobj_sp
;
416 TargetSP
target_sp(target
.GetSP());
417 valobj_sp
= ValueObjectVariable::Create(target_sp
.get(), var_sp
);
419 sb_value_list
.Append(SBValue(valobj_sp
));
423 return sb_value_list
;
426 lldb::SBValue
SBModule::FindFirstGlobalVariable(lldb::SBTarget
&target
,
428 LLDB_INSTRUMENT_VA(this, target
, name
);
430 SBValueList
sb_value_list(FindGlobalVariables(target
, name
, 1));
431 if (sb_value_list
.IsValid() && sb_value_list
.GetSize() > 0)
432 return sb_value_list
.GetValueAtIndex(0);
436 lldb::SBType
SBModule::FindFirstType(const char *name_cstr
) {
437 LLDB_INSTRUMENT_VA(this, name_cstr
);
439 ModuleSP
module_sp(GetSP());
440 if (name_cstr
&& module_sp
) {
441 ConstString
name(name_cstr
);
442 TypeQuery
query(name
.GetStringRef(), TypeQueryOptions::e_find_one
);
444 module_sp
->FindTypes(query
, results
);
445 TypeSP type_sp
= results
.GetFirstType();
447 return SBType(type_sp
);
449 auto type_system_or_err
=
450 module_sp
->GetTypeSystemForLanguage(eLanguageTypeC
);
451 if (auto err
= type_system_or_err
.takeError()) {
452 llvm::consumeError(std::move(err
));
456 if (auto ts
= *type_system_or_err
)
457 return SBType(ts
->GetBuiltinTypeByName(name
));
462 lldb::SBType
SBModule::GetBasicType(lldb::BasicType type
) {
463 LLDB_INSTRUMENT_VA(this, type
);
465 ModuleSP
module_sp(GetSP());
467 auto type_system_or_err
=
468 module_sp
->GetTypeSystemForLanguage(eLanguageTypeC
);
469 if (auto err
= type_system_or_err
.takeError()) {
470 llvm::consumeError(std::move(err
));
472 if (auto ts
= *type_system_or_err
)
473 return SBType(ts
->GetBasicTypeFromAST(type
));
479 lldb::SBTypeList
SBModule::FindTypes(const char *type
) {
480 LLDB_INSTRUMENT_VA(this, type
);
484 ModuleSP
module_sp(GetSP());
485 if (type
&& module_sp
) {
487 TypeQuery
query(type
);
489 module_sp
->FindTypes(query
, results
);
490 if (results
.GetTypeMap().Empty()) {
491 ConstString
name(type
);
492 auto type_system_or_err
=
493 module_sp
->GetTypeSystemForLanguage(eLanguageTypeC
);
494 if (auto err
= type_system_or_err
.takeError()) {
495 llvm::consumeError(std::move(err
));
497 if (auto ts
= *type_system_or_err
)
498 if (CompilerType compiler_type
= ts
->GetBuiltinTypeByName(name
))
499 retval
.Append(SBType(compiler_type
));
502 for (const TypeSP
&type_sp
: results
.GetTypeMap().Types())
503 retval
.Append(SBType(type_sp
));
509 lldb::SBType
SBModule::GetTypeByID(lldb::user_id_t uid
) {
510 LLDB_INSTRUMENT_VA(this, uid
);
512 ModuleSP
module_sp(GetSP());
514 if (SymbolFile
*symfile
= module_sp
->GetSymbolFile()) {
515 Type
*type_ptr
= symfile
->ResolveTypeUID(uid
);
517 return SBType(type_ptr
->shared_from_this());
523 lldb::SBTypeList
SBModule::GetTypes(uint32_t type_mask
) {
524 LLDB_INSTRUMENT_VA(this, type_mask
);
526 SBTypeList sb_type_list
;
528 ModuleSP
module_sp(GetSP());
531 SymbolFile
*symfile
= module_sp
->GetSymbolFile();
535 TypeClass type_class
= static_cast<TypeClass
>(type_mask
);
537 symfile
->GetTypes(nullptr, type_class
, type_list
);
538 sb_type_list
.m_opaque_up
->Append(type_list
);
542 SBSection
SBModule::FindSection(const char *sect_name
) {
543 LLDB_INSTRUMENT_VA(this, sect_name
);
545 SBSection sb_section
;
547 ModuleSP
module_sp(GetSP());
548 if (sect_name
&& module_sp
) {
549 // Give the symbol vendor a chance to add to the unified section list.
550 module_sp
->GetSymbolFile();
551 SectionList
*section_list
= module_sp
->GetSectionList();
553 ConstString
const_sect_name(sect_name
);
554 SectionSP
section_sp(section_list
->FindSectionByName(const_sect_name
));
556 sb_section
.SetSP(section_sp
);
563 lldb::ByteOrder
SBModule::GetByteOrder() {
564 LLDB_INSTRUMENT_VA(this);
566 ModuleSP
module_sp(GetSP());
568 return module_sp
->GetArchitecture().GetByteOrder();
569 return eByteOrderInvalid
;
572 const char *SBModule::GetTriple() {
573 LLDB_INSTRUMENT_VA(this);
575 ModuleSP
module_sp(GetSP());
579 std::string
triple(module_sp
->GetArchitecture().GetTriple().str());
580 // Unique the string so we don't run into ownership issues since the const
581 // strings put the string into the string pool once and the strings never
583 ConstString
const_triple(triple
.c_str());
584 return const_triple
.GetCString();
587 uint32_t SBModule::GetAddressByteSize() {
588 LLDB_INSTRUMENT_VA(this);
590 ModuleSP
module_sp(GetSP());
592 return module_sp
->GetArchitecture().GetAddressByteSize();
593 return sizeof(void *);
596 uint32_t SBModule::GetVersion(uint32_t *versions
, uint32_t num_versions
) {
597 LLDB_INSTRUMENT_VA(this, versions
, num_versions
);
599 llvm::VersionTuple version
;
600 if (ModuleSP module_sp
= GetSP())
601 version
= module_sp
->GetVersion();
603 if (!version
.empty())
605 if (version
.getMinor())
607 if (version
.getSubminor())
613 if (num_versions
> 0)
614 versions
[0] = version
.empty() ? UINT32_MAX
: version
.getMajor();
615 if (num_versions
> 1)
616 versions
[1] = version
.getMinor().value_or(UINT32_MAX
);
617 if (num_versions
> 2)
618 versions
[2] = version
.getSubminor().value_or(UINT32_MAX
);
619 for (uint32_t i
= 3; i
< num_versions
; ++i
)
620 versions
[i
] = UINT32_MAX
;
624 lldb::SBFileSpec
SBModule::GetSymbolFileSpec() const {
625 LLDB_INSTRUMENT_VA(this);
627 lldb::SBFileSpec sb_file_spec
;
628 ModuleSP
module_sp(GetSP());
630 if (SymbolFile
*symfile
= module_sp
->GetSymbolFile())
631 sb_file_spec
.SetFileSpec(symfile
->GetObjectFile()->GetFileSpec());
636 lldb::SBAddress
SBModule::GetObjectFileHeaderAddress() const {
637 LLDB_INSTRUMENT_VA(this);
639 lldb::SBAddress sb_addr
;
640 ModuleSP
module_sp(GetSP());
642 ObjectFile
*objfile_ptr
= module_sp
->GetObjectFile();
644 sb_addr
.ref() = objfile_ptr
->GetBaseAddress();
649 lldb::SBAddress
SBModule::GetObjectFileEntryPointAddress() const {
650 LLDB_INSTRUMENT_VA(this);
652 lldb::SBAddress sb_addr
;
653 ModuleSP
module_sp(GetSP());
655 ObjectFile
*objfile_ptr
= module_sp
->GetObjectFile();
657 sb_addr
.ref() = objfile_ptr
->GetEntryPointAddress();
662 uint32_t SBModule::GetNumberAllocatedModules() {
665 return Module::GetNumberAllocatedModules();
668 void SBModule::GarbageCollectAllocatedModules() {
671 const bool mandatory
= false;
672 ModuleList::RemoveOrphanSharedModules(mandatory
);