1cfca06d7SDimitry Andric //===-- SBModule.cpp ------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBModule.h"
10f034231aSEd Maste #include "lldb/API/SBAddress.h"
11f034231aSEd Maste #include "lldb/API/SBFileSpec.h"
12f034231aSEd Maste #include "lldb/API/SBModuleSpec.h"
13f034231aSEd Maste #include "lldb/API/SBProcess.h"
14f034231aSEd Maste #include "lldb/API/SBStream.h"
15f034231aSEd Maste #include "lldb/API/SBSymbolContextList.h"
1614f1b3e8SDimitry Andric #include "lldb/Core/Module.h"
17f034231aSEd Maste #include "lldb/Core/Section.h"
18f034231aSEd Maste #include "lldb/Core/ValueObjectList.h"
19f034231aSEd Maste #include "lldb/Core/ValueObjectVariable.h"
20f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
21f3fbd1c0SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
22f034231aSEd Maste #include "lldb/Symbol/Symtab.h"
23e81d9d49SDimitry Andric #include "lldb/Symbol/TypeSystem.h"
24f034231aSEd Maste #include "lldb/Symbol/VariableList.h"
25f034231aSEd Maste #include "lldb/Target/Target.h"
266f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
2774a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
28f034231aSEd Maste
29f034231aSEd Maste using namespace lldb;
30f034231aSEd Maste using namespace lldb_private;
31f034231aSEd Maste
SBModule()326f8fc217SDimitry Andric SBModule::SBModule() { LLDB_INSTRUMENT_VA(this); }
33f034231aSEd Maste
SBModule(const lldb::ModuleSP & module_sp)3414f1b3e8SDimitry Andric SBModule::SBModule(const lldb::ModuleSP &module_sp) : m_opaque_sp(module_sp) {}
35f034231aSEd Maste
SBModule(const SBModuleSpec & module_spec)366f8fc217SDimitry Andric SBModule::SBModule(const SBModuleSpec &module_spec) {
376f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, module_spec);
385f29bb8aSDimitry Andric
39f034231aSEd Maste ModuleSP module_sp;
405f29bb8aSDimitry Andric Status error = ModuleList::GetSharedModule(
415f29bb8aSDimitry Andric *module_spec.m_opaque_up, module_sp, nullptr, nullptr, nullptr);
42f034231aSEd Maste if (module_sp)
43f034231aSEd Maste SetSP(module_sp);
44f034231aSEd Maste }
45f034231aSEd Maste
SBModule(const SBModule & rhs)465f29bb8aSDimitry Andric SBModule::SBModule(const SBModule &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
476f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
485f29bb8aSDimitry Andric }
49f034231aSEd Maste
SBModule(lldb::SBProcess & process,lldb::addr_t header_addr)506f8fc217SDimitry Andric SBModule::SBModule(lldb::SBProcess &process, lldb::addr_t header_addr) {
516f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, process, header_addr);
525f29bb8aSDimitry Andric
53f034231aSEd Maste ProcessSP process_sp(process.GetSP());
5414f1b3e8SDimitry Andric if (process_sp) {
55f034231aSEd Maste m_opaque_sp = process_sp->ReadModuleFromMemory(FileSpec(), header_addr);
5614f1b3e8SDimitry Andric if (m_opaque_sp) {
57f034231aSEd Maste Target &target = process_sp->GetTarget();
58f034231aSEd Maste bool changed = false;
59866dcdacSEd Maste m_opaque_sp->SetLoadAddress(target, 0, true, changed);
60f034231aSEd Maste target.GetImages().Append(m_opaque_sp);
61f034231aSEd Maste }
62f034231aSEd Maste }
63f034231aSEd Maste }
64f034231aSEd Maste
operator =(const SBModule & rhs)6514f1b3e8SDimitry Andric const SBModule &SBModule::operator=(const SBModule &rhs) {
666f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
675f29bb8aSDimitry Andric
68f034231aSEd Maste if (this != &rhs)
69f034231aSEd Maste m_opaque_sp = rhs.m_opaque_sp;
706f8fc217SDimitry Andric return *this;
71f034231aSEd Maste }
72f034231aSEd Maste
73cfca06d7SDimitry Andric SBModule::~SBModule() = default;
74f034231aSEd Maste
IsValid() const755f29bb8aSDimitry Andric bool SBModule::IsValid() const {
766f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
775f29bb8aSDimitry Andric return this->operator bool();
785f29bb8aSDimitry Andric }
operator bool() const795f29bb8aSDimitry Andric SBModule::operator bool() const {
806f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
81f034231aSEd Maste
825f29bb8aSDimitry Andric return m_opaque_sp.get() != nullptr;
835f29bb8aSDimitry Andric }
845f29bb8aSDimitry Andric
Clear()855f29bb8aSDimitry Andric void SBModule::Clear() {
866f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
875f29bb8aSDimitry Andric
885f29bb8aSDimitry Andric m_opaque_sp.reset();
895f29bb8aSDimitry Andric }
90f034231aSEd Maste
IsFileBacked() const916f8fc217SDimitry Andric bool SBModule::IsFileBacked() const {
926f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
936f8fc217SDimitry Andric
946f8fc217SDimitry Andric ModuleSP module_sp(GetSP());
956f8fc217SDimitry Andric if (!module_sp)
966f8fc217SDimitry Andric return false;
976f8fc217SDimitry Andric
986f8fc217SDimitry Andric ObjectFile *obj_file = module_sp->GetObjectFile();
996f8fc217SDimitry Andric if (!obj_file)
1006f8fc217SDimitry Andric return false;
1016f8fc217SDimitry Andric
1026f8fc217SDimitry Andric return !obj_file->IsInMemory();
1036f8fc217SDimitry Andric }
1046f8fc217SDimitry Andric
GetFileSpec() const10514f1b3e8SDimitry Andric SBFileSpec SBModule::GetFileSpec() const {
1066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
107f034231aSEd Maste
108f034231aSEd Maste SBFileSpec file_spec;
109f034231aSEd Maste ModuleSP module_sp(GetSP());
110f034231aSEd Maste if (module_sp)
111f034231aSEd Maste file_spec.SetFileSpec(module_sp->GetFileSpec());
112f034231aSEd Maste
1136f8fc217SDimitry Andric return file_spec;
114f034231aSEd Maste }
115f034231aSEd Maste
GetPlatformFileSpec() const11614f1b3e8SDimitry Andric lldb::SBFileSpec SBModule::GetPlatformFileSpec() const {
1176f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1185f29bb8aSDimitry Andric
119f034231aSEd Maste SBFileSpec file_spec;
120f034231aSEd Maste ModuleSP module_sp(GetSP());
121f034231aSEd Maste if (module_sp)
122f034231aSEd Maste file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
123f034231aSEd Maste
1246f8fc217SDimitry Andric return file_spec;
125f034231aSEd Maste }
126f034231aSEd Maste
SetPlatformFileSpec(const lldb::SBFileSpec & platform_file)12714f1b3e8SDimitry Andric bool SBModule::SetPlatformFileSpec(const lldb::SBFileSpec &platform_file) {
1286f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, platform_file);
1295f29bb8aSDimitry Andric
130f034231aSEd Maste bool result = false;
131f034231aSEd Maste
132f034231aSEd Maste ModuleSP module_sp(GetSP());
13314f1b3e8SDimitry Andric if (module_sp) {
134f034231aSEd Maste module_sp->SetPlatformFileSpec(*platform_file);
135f034231aSEd Maste result = true;
136f034231aSEd Maste }
137f034231aSEd Maste
138f034231aSEd Maste return result;
139f034231aSEd Maste }
140f034231aSEd Maste
GetRemoteInstallFileSpec()14114f1b3e8SDimitry Andric lldb::SBFileSpec SBModule::GetRemoteInstallFileSpec() {
1426f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1435f29bb8aSDimitry Andric
14486758c71SEd Maste SBFileSpec sb_file_spec;
14586758c71SEd Maste ModuleSP module_sp(GetSP());
14686758c71SEd Maste if (module_sp)
14786758c71SEd Maste sb_file_spec.SetFileSpec(module_sp->GetRemoteInstallFileSpec());
1486f8fc217SDimitry Andric return sb_file_spec;
14986758c71SEd Maste }
15086758c71SEd Maste
SetRemoteInstallFileSpec(lldb::SBFileSpec & file)15114f1b3e8SDimitry Andric bool SBModule::SetRemoteInstallFileSpec(lldb::SBFileSpec &file) {
1526f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, file);
1535f29bb8aSDimitry Andric
15486758c71SEd Maste ModuleSP module_sp(GetSP());
15514f1b3e8SDimitry Andric if (module_sp) {
15686758c71SEd Maste module_sp->SetRemoteInstallFileSpec(file.ref());
15786758c71SEd Maste return true;
15886758c71SEd Maste }
15986758c71SEd Maste return false;
16086758c71SEd Maste }
161f034231aSEd Maste
GetUUIDBytes() const16214f1b3e8SDimitry Andric const uint8_t *SBModule::GetUUIDBytes() const {
1636f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
164f034231aSEd Maste
1655f29bb8aSDimitry Andric const uint8_t *uuid_bytes = nullptr;
166f034231aSEd Maste ModuleSP module_sp(GetSP());
167f034231aSEd Maste if (module_sp)
168f73363f1SDimitry Andric uuid_bytes = module_sp->GetUUID().GetBytes().data();
169f034231aSEd Maste
170f034231aSEd Maste return uuid_bytes;
171f034231aSEd Maste }
172f034231aSEd Maste
GetUUIDString() const17314f1b3e8SDimitry Andric const char *SBModule::GetUUIDString() const {
1746f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
175f034231aSEd Maste
176f034231aSEd Maste ModuleSP module_sp(GetSP());
1777fa27ce4SDimitry Andric if (!module_sp)
1787fa27ce4SDimitry Andric return nullptr;
1797fa27ce4SDimitry Andric
180f73363f1SDimitry Andric // We are going to return a "const char *" value through the public API, so
181f73363f1SDimitry Andric // we need to constify it so it gets added permanently the string pool and
182f73363f1SDimitry Andric // then we don't need to worry about the lifetime of the string as it will
183f73363f1SDimitry Andric // never go away once it has been put into the ConstString string pool
1847fa27ce4SDimitry Andric const char *uuid_cstr =
1857fa27ce4SDimitry Andric ConstString(module_sp->GetUUID().GetAsString()).GetCString();
1867fa27ce4SDimitry Andric // Note: SBModule::GetUUIDString's expected behavior is to return nullptr if
1877fa27ce4SDimitry Andric // the string we get is empty, so we must perform this check before returning.
1887fa27ce4SDimitry Andric if (uuid_cstr && uuid_cstr[0])
1895e95aa85SEd Maste return uuid_cstr;
1905f29bb8aSDimitry Andric return nullptr;
191f034231aSEd Maste }
192f034231aSEd Maste
operator ==(const SBModule & rhs) const19314f1b3e8SDimitry Andric bool SBModule::operator==(const SBModule &rhs) const {
1946f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
1955f29bb8aSDimitry Andric
196f034231aSEd Maste if (m_opaque_sp)
197f034231aSEd Maste return m_opaque_sp.get() == rhs.m_opaque_sp.get();
198f034231aSEd Maste return false;
199f034231aSEd Maste }
200f034231aSEd Maste
operator !=(const SBModule & rhs) const20114f1b3e8SDimitry Andric bool SBModule::operator!=(const SBModule &rhs) const {
2026f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
2035f29bb8aSDimitry Andric
204f034231aSEd Maste if (m_opaque_sp)
205f034231aSEd Maste return m_opaque_sp.get() != rhs.m_opaque_sp.get();
206f034231aSEd Maste return false;
207f034231aSEd Maste }
208f034231aSEd Maste
GetSP() const20914f1b3e8SDimitry Andric ModuleSP SBModule::GetSP() const { return m_opaque_sp; }
210f034231aSEd Maste
SetSP(const ModuleSP & module_sp)21114f1b3e8SDimitry Andric void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; }
212f034231aSEd Maste
ResolveFileAddress(lldb::addr_t vm_addr)21314f1b3e8SDimitry Andric SBAddress SBModule::ResolveFileAddress(lldb::addr_t vm_addr) {
2146f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, vm_addr);
2155f29bb8aSDimitry Andric
216f034231aSEd Maste lldb::SBAddress sb_addr;
217f034231aSEd Maste ModuleSP module_sp(GetSP());
21814f1b3e8SDimitry Andric if (module_sp) {
219f034231aSEd Maste Address addr;
220f034231aSEd Maste if (module_sp->ResolveFileAddress(vm_addr, addr))
221f034231aSEd Maste sb_addr.ref() = addr;
222f034231aSEd Maste }
2236f8fc217SDimitry Andric return sb_addr;
224f034231aSEd Maste }
225f034231aSEd Maste
226f034231aSEd Maste SBSymbolContext
ResolveSymbolContextForAddress(const SBAddress & addr,uint32_t resolve_scope)22714f1b3e8SDimitry Andric SBModule::ResolveSymbolContextForAddress(const SBAddress &addr,
22814f1b3e8SDimitry Andric uint32_t resolve_scope) {
2296f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, resolve_scope);
2305f29bb8aSDimitry Andric
231f034231aSEd Maste SBSymbolContext sb_sc;
232f034231aSEd Maste ModuleSP module_sp(GetSP());
23394994d37SDimitry Andric SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
234f034231aSEd Maste if (module_sp && addr.IsValid())
23594994d37SDimitry Andric module_sp->ResolveSymbolContextForAddress(addr.ref(), scope, *sb_sc);
2366f8fc217SDimitry Andric return sb_sc;
237f034231aSEd Maste }
238f034231aSEd Maste
GetDescription(SBStream & description)23914f1b3e8SDimitry Andric bool SBModule::GetDescription(SBStream &description) {
2406f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
2415f29bb8aSDimitry Andric
242f034231aSEd Maste Stream &strm = description.ref();
243f034231aSEd Maste
244f034231aSEd Maste ModuleSP module_sp(GetSP());
24514f1b3e8SDimitry Andric if (module_sp) {
246706b4fc4SDimitry Andric module_sp->GetDescription(strm.AsRawOstream());
24714f1b3e8SDimitry Andric } else
248f034231aSEd Maste strm.PutCString("No value");
249f034231aSEd Maste
250f034231aSEd Maste return true;
251f034231aSEd Maste }
252f034231aSEd Maste
GetNumCompileUnits()25314f1b3e8SDimitry Andric uint32_t SBModule::GetNumCompileUnits() {
2546f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
2555f29bb8aSDimitry Andric
256f034231aSEd Maste ModuleSP module_sp(GetSP());
25714f1b3e8SDimitry Andric if (module_sp) {
258f034231aSEd Maste return module_sp->GetNumCompileUnits();
259f034231aSEd Maste }
260f034231aSEd Maste return 0;
261f034231aSEd Maste }
262f034231aSEd Maste
GetCompileUnitAtIndex(uint32_t index)26314f1b3e8SDimitry Andric SBCompileUnit SBModule::GetCompileUnitAtIndex(uint32_t index) {
2646f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, index);
2655f29bb8aSDimitry Andric
266f034231aSEd Maste SBCompileUnit sb_cu;
267f034231aSEd Maste ModuleSP module_sp(GetSP());
26814f1b3e8SDimitry Andric if (module_sp) {
269f034231aSEd Maste CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(index);
270f034231aSEd Maste sb_cu.reset(cu_sp.get());
271f034231aSEd Maste }
2726f8fc217SDimitry Andric return sb_cu;
273f034231aSEd Maste }
274f034231aSEd Maste
FindCompileUnits(const SBFileSpec & sb_file_spec)2755f29bb8aSDimitry Andric SBSymbolContextList SBModule::FindCompileUnits(const SBFileSpec &sb_file_spec) {
2766f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, sb_file_spec);
2775f29bb8aSDimitry Andric
278f73363f1SDimitry Andric SBSymbolContextList sb_sc_list;
279f73363f1SDimitry Andric const ModuleSP module_sp(GetSP());
280f73363f1SDimitry Andric if (sb_file_spec.IsValid() && module_sp) {
281ead24645SDimitry Andric module_sp->FindCompileUnits(*sb_file_spec, *sb_sc_list);
282f73363f1SDimitry Andric }
2836f8fc217SDimitry Andric return sb_sc_list;
284f73363f1SDimitry Andric }
285f73363f1SDimitry Andric
GetUnifiedSymbolTable(const lldb::ModuleSP & module_sp)28614f1b3e8SDimitry Andric static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) {
287ead24645SDimitry Andric if (module_sp)
288ead24645SDimitry Andric return module_sp->GetSymtab();
2895f29bb8aSDimitry Andric return nullptr;
290f034231aSEd Maste }
291f034231aSEd Maste
GetNumSymbols()29214f1b3e8SDimitry Andric size_t SBModule::GetNumSymbols() {
2936f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
2945f29bb8aSDimitry Andric
295f034231aSEd Maste ModuleSP module_sp(GetSP());
296ead24645SDimitry Andric if (Symtab *symtab = GetUnifiedSymbolTable(module_sp))
297f034231aSEd Maste return symtab->GetNumSymbols();
298f034231aSEd Maste return 0;
299f034231aSEd Maste }
300f034231aSEd Maste
GetSymbolAtIndex(size_t idx)30114f1b3e8SDimitry Andric SBSymbol SBModule::GetSymbolAtIndex(size_t idx) {
3026f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
3035f29bb8aSDimitry Andric
304f034231aSEd Maste SBSymbol sb_symbol;
305f034231aSEd Maste ModuleSP module_sp(GetSP());
306f034231aSEd Maste Symtab *symtab = GetUnifiedSymbolTable(module_sp);
307f034231aSEd Maste if (symtab)
308f034231aSEd Maste sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx));
3096f8fc217SDimitry Andric return sb_symbol;
310f034231aSEd Maste }
311f034231aSEd Maste
FindSymbol(const char * name,lldb::SymbolType symbol_type)31214f1b3e8SDimitry Andric lldb::SBSymbol SBModule::FindSymbol(const char *name,
31314f1b3e8SDimitry Andric lldb::SymbolType symbol_type) {
3146f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, name, symbol_type);
3155f29bb8aSDimitry Andric
316f034231aSEd Maste SBSymbol sb_symbol;
31714f1b3e8SDimitry Andric if (name && name[0]) {
318f034231aSEd Maste ModuleSP module_sp(GetSP());
319f034231aSEd Maste Symtab *symtab = GetUnifiedSymbolTable(module_sp);
320f034231aSEd Maste if (symtab)
32114f1b3e8SDimitry Andric sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(
32214f1b3e8SDimitry Andric ConstString(name), symbol_type, Symtab::eDebugAny,
32314f1b3e8SDimitry Andric Symtab::eVisibilityAny));
324f034231aSEd Maste }
3256f8fc217SDimitry Andric return sb_symbol;
326f034231aSEd Maste }
327f034231aSEd Maste
FindSymbols(const char * name,lldb::SymbolType symbol_type)32814f1b3e8SDimitry Andric lldb::SBSymbolContextList SBModule::FindSymbols(const char *name,
32914f1b3e8SDimitry Andric lldb::SymbolType symbol_type) {
3306f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, name, symbol_type);
3315f29bb8aSDimitry Andric
332f034231aSEd Maste SBSymbolContextList sb_sc_list;
33314f1b3e8SDimitry Andric if (name && name[0]) {
334f034231aSEd Maste ModuleSP module_sp(GetSP());
335f034231aSEd Maste Symtab *symtab = GetUnifiedSymbolTable(module_sp);
33614f1b3e8SDimitry Andric if (symtab) {
337f034231aSEd Maste std::vector<uint32_t> matching_symbol_indexes;
338ead24645SDimitry Andric symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type,
339ead24645SDimitry Andric matching_symbol_indexes);
340ead24645SDimitry Andric const size_t num_matches = matching_symbol_indexes.size();
34114f1b3e8SDimitry Andric if (num_matches) {
342f034231aSEd Maste SymbolContext sc;
343f034231aSEd Maste sc.module_sp = module_sp;
344f034231aSEd Maste SymbolContextList &sc_list = *sb_sc_list;
34514f1b3e8SDimitry Andric for (size_t i = 0; i < num_matches; ++i) {
346f034231aSEd Maste sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]);
347f034231aSEd Maste if (sc.symbol)
348f034231aSEd Maste sc_list.Append(sc);
349f034231aSEd Maste }
350f034231aSEd Maste }
351f034231aSEd Maste }
352f034231aSEd Maste }
3536f8fc217SDimitry Andric return sb_sc_list;
354f034231aSEd Maste }
355f034231aSEd Maste
GetNumSections()35614f1b3e8SDimitry Andric size_t SBModule::GetNumSections() {
3576f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
3585f29bb8aSDimitry Andric
359f034231aSEd Maste ModuleSP module_sp(GetSP());
36014f1b3e8SDimitry Andric if (module_sp) {
361f034231aSEd Maste // Give the symbol vendor a chance to add to the unified section list.
362ead24645SDimitry Andric module_sp->GetSymbolFile();
363f034231aSEd Maste SectionList *section_list = module_sp->GetSectionList();
364f034231aSEd Maste if (section_list)
365f034231aSEd Maste return section_list->GetSize();
366f034231aSEd Maste }
367f034231aSEd Maste return 0;
368f034231aSEd Maste }
369f034231aSEd Maste
GetSectionAtIndex(size_t idx)37014f1b3e8SDimitry Andric SBSection SBModule::GetSectionAtIndex(size_t idx) {
3716f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
3725f29bb8aSDimitry Andric
373f034231aSEd Maste SBSection sb_section;
374f034231aSEd Maste ModuleSP module_sp(GetSP());
37514f1b3e8SDimitry Andric if (module_sp) {
376f034231aSEd Maste // Give the symbol vendor a chance to add to the unified section list.
377ead24645SDimitry Andric module_sp->GetSymbolFile();
378f034231aSEd Maste SectionList *section_list = module_sp->GetSectionList();
379f034231aSEd Maste
380f034231aSEd Maste if (section_list)
381f034231aSEd Maste sb_section.SetSP(section_list->GetSectionAtIndex(idx));
382f034231aSEd Maste }
3836f8fc217SDimitry Andric return sb_section;
384f034231aSEd Maste }
385f034231aSEd Maste
FindFunctions(const char * name,uint32_t name_type_mask)38614f1b3e8SDimitry Andric lldb::SBSymbolContextList SBModule::FindFunctions(const char *name,
38714f1b3e8SDimitry Andric uint32_t name_type_mask) {
3886f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, name, name_type_mask);
3895f29bb8aSDimitry Andric
390f034231aSEd Maste lldb::SBSymbolContextList sb_sc_list;
391f034231aSEd Maste ModuleSP module_sp(GetSP());
39214f1b3e8SDimitry Andric if (name && module_sp) {
393c0981da4SDimitry Andric
394c0981da4SDimitry Andric ModuleFunctionSearchOptions function_options;
395c0981da4SDimitry Andric function_options.include_symbols = true;
396c0981da4SDimitry Andric function_options.include_inlines = true;
39794994d37SDimitry Andric FunctionNameType type = static_cast<FunctionNameType>(name_type_mask);
398cfca06d7SDimitry Andric module_sp->FindFunctions(ConstString(name), CompilerDeclContext(), type,
399c0981da4SDimitry Andric function_options, *sb_sc_list);
400f034231aSEd Maste }
4016f8fc217SDimitry Andric return sb_sc_list;
402f034231aSEd Maste }
403f034231aSEd Maste
FindGlobalVariables(SBTarget & target,const char * name,uint32_t max_matches)40414f1b3e8SDimitry Andric SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,
40514f1b3e8SDimitry Andric uint32_t max_matches) {
4066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, target, name, max_matches);
4075f29bb8aSDimitry Andric
408f034231aSEd Maste SBValueList sb_value_list;
409f034231aSEd Maste ModuleSP module_sp(GetSP());
41014f1b3e8SDimitry Andric if (name && module_sp) {
411f034231aSEd Maste VariableList variable_list;
412cfca06d7SDimitry Andric module_sp->FindGlobalVariables(ConstString(name), CompilerDeclContext(),
413cfca06d7SDimitry Andric max_matches, variable_list);
414706b4fc4SDimitry Andric for (const VariableSP &var_sp : variable_list) {
415f034231aSEd Maste lldb::ValueObjectSP valobj_sp;
416f034231aSEd Maste TargetSP target_sp(target.GetSP());
417706b4fc4SDimitry Andric valobj_sp = ValueObjectVariable::Create(target_sp.get(), var_sp);
418f034231aSEd Maste if (valobj_sp)
419f034231aSEd Maste sb_value_list.Append(SBValue(valobj_sp));
420f034231aSEd Maste }
421f034231aSEd Maste }
422f034231aSEd Maste
4236f8fc217SDimitry Andric return sb_value_list;
424f034231aSEd Maste }
425f034231aSEd Maste
FindFirstGlobalVariable(lldb::SBTarget & target,const char * name)42614f1b3e8SDimitry Andric lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target,
42714f1b3e8SDimitry Andric const char *name) {
4286f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, target, name);
4295f29bb8aSDimitry Andric
430f034231aSEd Maste SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
431f034231aSEd Maste if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
4326f8fc217SDimitry Andric return sb_value_list.GetValueAtIndex(0);
4336f8fc217SDimitry Andric return SBValue();
434f034231aSEd Maste }
435f034231aSEd Maste
FindFirstType(const char * name_cstr)43614f1b3e8SDimitry Andric lldb::SBType SBModule::FindFirstType(const char *name_cstr) {
4376f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, name_cstr);
4385f29bb8aSDimitry Andric
439f034231aSEd Maste ModuleSP module_sp(GetSP());
440312c0ed1SDimitry Andric if (name_cstr && module_sp) {
441f034231aSEd Maste ConstString name(name_cstr);
442312c0ed1SDimitry Andric TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_find_one);
443312c0ed1SDimitry Andric TypeResults results;
444312c0ed1SDimitry Andric module_sp->FindTypes(query, results);
445312c0ed1SDimitry Andric TypeSP type_sp = results.GetFirstType();
446312c0ed1SDimitry Andric if (type_sp)
447312c0ed1SDimitry Andric return SBType(type_sp);
448f034231aSEd Maste
449312c0ed1SDimitry Andric auto type_system_or_err =
450312c0ed1SDimitry Andric module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
451ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
452ead24645SDimitry Andric llvm::consumeError(std::move(err));
453e3b55780SDimitry Andric return {};
454ead24645SDimitry Andric }
455e3b55780SDimitry Andric
456e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
457e3b55780SDimitry Andric return SBType(ts->GetBuiltinTypeByName(name));
458312c0ed1SDimitry Andric }
459e3b55780SDimitry Andric return {};
460f034231aSEd Maste }
461f034231aSEd Maste
GetBasicType(lldb::BasicType type)46214f1b3e8SDimitry Andric lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {
4636f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, type);
4645f29bb8aSDimitry Andric
465f034231aSEd Maste ModuleSP module_sp(GetSP());
46614f1b3e8SDimitry Andric if (module_sp) {
467ead24645SDimitry Andric auto type_system_or_err =
46814f1b3e8SDimitry Andric module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
469ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
470ead24645SDimitry Andric llvm::consumeError(std::move(err));
471ead24645SDimitry Andric } else {
472e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
473e3b55780SDimitry Andric return SBType(ts->GetBasicTypeFromAST(type));
474ead24645SDimitry Andric }
475e81d9d49SDimitry Andric }
4766f8fc217SDimitry Andric return SBType();
477f034231aSEd Maste }
478f034231aSEd Maste
FindTypes(const char * type)47914f1b3e8SDimitry Andric lldb::SBTypeList SBModule::FindTypes(const char *type) {
4806f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, type);
4815f29bb8aSDimitry Andric
482f034231aSEd Maste SBTypeList retval;
483f034231aSEd Maste
484f034231aSEd Maste ModuleSP module_sp(GetSP());
48514f1b3e8SDimitry Andric if (type && module_sp) {
486f034231aSEd Maste TypeList type_list;
487312c0ed1SDimitry Andric TypeQuery query(type);
488312c0ed1SDimitry Andric TypeResults results;
489312c0ed1SDimitry Andric module_sp->FindTypes(query, results);
490312c0ed1SDimitry Andric if (results.GetTypeMap().Empty()) {
491f034231aSEd Maste ConstString name(type);
492ead24645SDimitry Andric auto type_system_or_err =
493ead24645SDimitry Andric module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
494ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
495ead24645SDimitry Andric llvm::consumeError(std::move(err));
496ead24645SDimitry Andric } else {
497e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
498e3b55780SDimitry Andric if (CompilerType compiler_type = ts->GetBuiltinTypeByName(name))
499ead24645SDimitry Andric retval.Append(SBType(compiler_type));
500ead24645SDimitry Andric }
501ead24645SDimitry Andric } else {
502312c0ed1SDimitry Andric for (const TypeSP &type_sp : results.GetTypeMap().Types())
503f034231aSEd Maste retval.Append(SBType(type_sp));
504f034231aSEd Maste }
505e81d9d49SDimitry Andric }
5066f8fc217SDimitry Andric return retval;
507f034231aSEd Maste }
508f034231aSEd Maste
GetTypeByID(lldb::user_id_t uid)50914f1b3e8SDimitry Andric lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {
5106f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, uid);
5115f29bb8aSDimitry Andric
512866dcdacSEd Maste ModuleSP module_sp(GetSP());
51314f1b3e8SDimitry Andric if (module_sp) {
514ead24645SDimitry Andric if (SymbolFile *symfile = module_sp->GetSymbolFile()) {
515ead24645SDimitry Andric Type *type_ptr = symfile->ResolveTypeUID(uid);
516866dcdacSEd Maste if (type_ptr)
5176f8fc217SDimitry Andric return SBType(type_ptr->shared_from_this());
518866dcdacSEd Maste }
519866dcdacSEd Maste }
5206f8fc217SDimitry Andric return SBType();
521866dcdacSEd Maste }
522866dcdacSEd Maste
GetTypes(uint32_t type_mask)52314f1b3e8SDimitry Andric lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {
5246f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, type_mask);
5255f29bb8aSDimitry Andric
526f034231aSEd Maste SBTypeList sb_type_list;
527f034231aSEd Maste
528f034231aSEd Maste ModuleSP module_sp(GetSP());
52994994d37SDimitry Andric if (!module_sp)
5306f8fc217SDimitry Andric return sb_type_list;
531ead24645SDimitry Andric SymbolFile *symfile = module_sp->GetSymbolFile();
532ead24645SDimitry Andric if (!symfile)
5336f8fc217SDimitry Andric return sb_type_list;
53494994d37SDimitry Andric
53594994d37SDimitry Andric TypeClass type_class = static_cast<TypeClass>(type_mask);
536f034231aSEd Maste TypeList type_list;
537ead24645SDimitry Andric symfile->GetTypes(nullptr, type_class, type_list);
5385f29bb8aSDimitry Andric sb_type_list.m_opaque_up->Append(type_list);
5396f8fc217SDimitry Andric return sb_type_list;
540f034231aSEd Maste }
541f034231aSEd Maste
FindSection(const char * sect_name)54214f1b3e8SDimitry Andric SBSection SBModule::FindSection(const char *sect_name) {
5436f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, sect_name);
5445f29bb8aSDimitry Andric
545f034231aSEd Maste SBSection sb_section;
546f034231aSEd Maste
547f034231aSEd Maste ModuleSP module_sp(GetSP());
54814f1b3e8SDimitry Andric if (sect_name && module_sp) {
549f034231aSEd Maste // Give the symbol vendor a chance to add to the unified section list.
550ead24645SDimitry Andric module_sp->GetSymbolFile();
551f034231aSEd Maste SectionList *section_list = module_sp->GetSectionList();
55214f1b3e8SDimitry Andric if (section_list) {
553f034231aSEd Maste ConstString const_sect_name(sect_name);
554f034231aSEd Maste SectionSP section_sp(section_list->FindSectionByName(const_sect_name));
55514f1b3e8SDimitry Andric if (section_sp) {
556f034231aSEd Maste sb_section.SetSP(section_sp);
557f034231aSEd Maste }
558f034231aSEd Maste }
559f034231aSEd Maste }
5606f8fc217SDimitry Andric return sb_section;
561f034231aSEd Maste }
562f034231aSEd Maste
GetByteOrder()56314f1b3e8SDimitry Andric lldb::ByteOrder SBModule::GetByteOrder() {
5646f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
5655f29bb8aSDimitry Andric
566f034231aSEd Maste ModuleSP module_sp(GetSP());
567f034231aSEd Maste if (module_sp)
568f034231aSEd Maste return module_sp->GetArchitecture().GetByteOrder();
569f034231aSEd Maste return eByteOrderInvalid;
570f034231aSEd Maste }
571f034231aSEd Maste
GetTriple()57214f1b3e8SDimitry Andric const char *SBModule::GetTriple() {
5736f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
5745f29bb8aSDimitry Andric
575f034231aSEd Maste ModuleSP module_sp(GetSP());
5767fa27ce4SDimitry Andric if (!module_sp)
5777fa27ce4SDimitry Andric return nullptr;
5787fa27ce4SDimitry Andric
579f034231aSEd Maste std::string triple(module_sp->GetArchitecture().GetTriple().str());
580f73363f1SDimitry Andric // Unique the string so we don't run into ownership issues since the const
581f73363f1SDimitry Andric // strings put the string into the string pool once and the strings never
582f73363f1SDimitry Andric // comes out
583f034231aSEd Maste ConstString const_triple(triple.c_str());
584f034231aSEd Maste return const_triple.GetCString();
585f034231aSEd Maste }
586f034231aSEd Maste
GetAddressByteSize()58714f1b3e8SDimitry Andric uint32_t SBModule::GetAddressByteSize() {
5886f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
5895f29bb8aSDimitry Andric
590f034231aSEd Maste ModuleSP module_sp(GetSP());
591f034231aSEd Maste if (module_sp)
592f034231aSEd Maste return module_sp->GetArchitecture().GetAddressByteSize();
593f034231aSEd Maste return sizeof(void *);
594f034231aSEd Maste }
595f034231aSEd Maste
GetVersion(uint32_t * versions,uint32_t num_versions)59614f1b3e8SDimitry Andric uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {
5976f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, versions, num_versions);
5985f29bb8aSDimitry Andric
599f73363f1SDimitry Andric llvm::VersionTuple version;
600f73363f1SDimitry Andric if (ModuleSP module_sp = GetSP())
601f73363f1SDimitry Andric version = module_sp->GetVersion();
602f73363f1SDimitry Andric uint32_t result = 0;
603f73363f1SDimitry Andric if (!version.empty())
604f73363f1SDimitry Andric ++result;
605f73363f1SDimitry Andric if (version.getMinor())
606f73363f1SDimitry Andric ++result;
607f73363f1SDimitry Andric if (version.getSubminor())
608f73363f1SDimitry Andric ++result;
609f73363f1SDimitry Andric
610f73363f1SDimitry Andric if (!versions)
611f73363f1SDimitry Andric return result;
612f73363f1SDimitry Andric
613f73363f1SDimitry Andric if (num_versions > 0)
614f73363f1SDimitry Andric versions[0] = version.empty() ? UINT32_MAX : version.getMajor();
615f73363f1SDimitry Andric if (num_versions > 1)
616145449b1SDimitry Andric versions[1] = version.getMinor().value_or(UINT32_MAX);
617f73363f1SDimitry Andric if (num_versions > 2)
618145449b1SDimitry Andric versions[2] = version.getSubminor().value_or(UINT32_MAX);
619f73363f1SDimitry Andric for (uint32_t i = 3; i < num_versions; ++i)
620f034231aSEd Maste versions[i] = UINT32_MAX;
621f73363f1SDimitry Andric return result;
622f034231aSEd Maste }
623f034231aSEd Maste
GetSymbolFileSpec() const62414f1b3e8SDimitry Andric lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {
6256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
6265f29bb8aSDimitry Andric
6275e95aa85SEd Maste lldb::SBFileSpec sb_file_spec;
6285e95aa85SEd Maste ModuleSP module_sp(GetSP());
62914f1b3e8SDimitry Andric if (module_sp) {
630ead24645SDimitry Andric if (SymbolFile *symfile = module_sp->GetSymbolFile())
631ead24645SDimitry Andric sb_file_spec.SetFileSpec(symfile->GetObjectFile()->GetFileSpec());
6325e95aa85SEd Maste }
6336f8fc217SDimitry Andric return sb_file_spec;
6345e95aa85SEd Maste }
6355e95aa85SEd Maste
GetObjectFileHeaderAddress() const63614f1b3e8SDimitry Andric lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const {
6376f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
6385f29bb8aSDimitry Andric
6395e95aa85SEd Maste lldb::SBAddress sb_addr;
6405e95aa85SEd Maste ModuleSP module_sp(GetSP());
64114f1b3e8SDimitry Andric if (module_sp) {
6425e95aa85SEd Maste ObjectFile *objfile_ptr = module_sp->GetObjectFile();
6435e95aa85SEd Maste if (objfile_ptr)
64494994d37SDimitry Andric sb_addr.ref() = objfile_ptr->GetBaseAddress();
64594994d37SDimitry Andric }
6466f8fc217SDimitry Andric return sb_addr;
64794994d37SDimitry Andric }
64894994d37SDimitry Andric
GetObjectFileEntryPointAddress() const64994994d37SDimitry Andric lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {
6506f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
6515f29bb8aSDimitry Andric
65294994d37SDimitry Andric lldb::SBAddress sb_addr;
65394994d37SDimitry Andric ModuleSP module_sp(GetSP());
65494994d37SDimitry Andric if (module_sp) {
65594994d37SDimitry Andric ObjectFile *objfile_ptr = module_sp->GetObjectFile();
65694994d37SDimitry Andric if (objfile_ptr)
65794994d37SDimitry Andric sb_addr.ref() = objfile_ptr->GetEntryPointAddress();
6585e95aa85SEd Maste }
6596f8fc217SDimitry Andric return sb_addr;
6605f29bb8aSDimitry Andric }
6615f29bb8aSDimitry Andric
GetNumberAllocatedModules()662cfca06d7SDimitry Andric uint32_t SBModule::GetNumberAllocatedModules() {
6636f8fc217SDimitry Andric LLDB_INSTRUMENT();
664cfca06d7SDimitry Andric
665cfca06d7SDimitry Andric return Module::GetNumberAllocatedModules();
666cfca06d7SDimitry Andric }
667cfca06d7SDimitry Andric
GarbageCollectAllocatedModules()668b60736ecSDimitry Andric void SBModule::GarbageCollectAllocatedModules() {
6696f8fc217SDimitry Andric LLDB_INSTRUMENT();
670b60736ecSDimitry Andric
671b60736ecSDimitry Andric const bool mandatory = false;
672b60736ecSDimitry Andric ModuleList::RemoveOrphanSharedModules(mandatory);
673b60736ecSDimitry Andric }
674