180814287SRaphael Isemann //===-- SBModuleSpec.cpp --------------------------------------------------===//
2226cce25SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6226cce25SGreg Clayton //
7226cce25SGreg Clayton //===----------------------------------------------------------------------===//
8226cce25SGreg Clayton 
9226cce25SGreg Clayton #include "lldb/API/SBModuleSpec.h"
10d51402acSJonas Devlieghere #include "lldb/Utility/ReproducerInstrumentation.h"
11bd4bf82aSJonas Devlieghere #include "Utils.h"
12226cce25SGreg Clayton #include "lldb/API/SBStream.h"
13226cce25SGreg Clayton #include "lldb/Core/Module.h"
14226cce25SGreg Clayton #include "lldb/Core/ModuleSpec.h"
15226cce25SGreg Clayton #include "lldb/Host/Host.h"
16226cce25SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
17bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
18226cce25SGreg Clayton 
19226cce25SGreg Clayton using namespace lldb;
20226cce25SGreg Clayton using namespace lldb_private;
21226cce25SGreg Clayton 
22baf5664fSJonas Devlieghere SBModuleSpec::SBModuleSpec() : m_opaque_up(new lldb_private::ModuleSpec()) {
23baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModuleSpec);
24baf5664fSJonas Devlieghere }
25226cce25SGreg Clayton 
26a3436f73SKazu Hirata SBModuleSpec::SBModuleSpec(const SBModuleSpec &rhs) {
27baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBModuleSpec, (const lldb::SBModuleSpec &), rhs);
28baf5664fSJonas Devlieghere 
29bd4bf82aSJonas Devlieghere   m_opaque_up = clone(rhs.m_opaque_up);
30bd4bf82aSJonas Devlieghere }
31226cce25SGreg Clayton 
32b9c1b51eSKate Stone const SBModuleSpec &SBModuleSpec::operator=(const SBModuleSpec &rhs) {
33baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(const lldb::SBModuleSpec &,
34baf5664fSJonas Devlieghere                      SBModuleSpec, operator=,(const lldb::SBModuleSpec &), rhs);
35baf5664fSJonas Devlieghere 
36226cce25SGreg Clayton   if (this != &rhs)
37bd4bf82aSJonas Devlieghere     m_opaque_up = clone(rhs.m_opaque_up);
38*d232abc3SJonas Devlieghere   return *this;
39226cce25SGreg Clayton }
40226cce25SGreg Clayton 
41866b7a65SJonas Devlieghere SBModuleSpec::~SBModuleSpec() = default;
42226cce25SGreg Clayton 
43baf5664fSJonas Devlieghere bool SBModuleSpec::IsValid() const {
44baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModuleSpec, IsValid);
457f5237bcSPavel Labath   return this->operator bool();
467f5237bcSPavel Labath }
477f5237bcSPavel Labath SBModuleSpec::operator bool() const {
487f5237bcSPavel Labath   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModuleSpec, operator bool);
49226cce25SGreg Clayton 
50baf5664fSJonas Devlieghere   return m_opaque_up->operator bool();
51baf5664fSJonas Devlieghere }
52baf5664fSJonas Devlieghere 
53baf5664fSJonas Devlieghere void SBModuleSpec::Clear() {
54baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(void, SBModuleSpec, Clear);
55baf5664fSJonas Devlieghere 
56baf5664fSJonas Devlieghere   m_opaque_up->Clear();
57baf5664fSJonas Devlieghere }
58226cce25SGreg Clayton 
59b9c1b51eSKate Stone SBFileSpec SBModuleSpec::GetFileSpec() {
60baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec, GetFileSpec);
61baf5664fSJonas Devlieghere 
62d5b44036SJonas Devlieghere   SBFileSpec sb_spec(m_opaque_up->GetFileSpec());
63*d232abc3SJonas Devlieghere   return sb_spec;
64226cce25SGreg Clayton }
65226cce25SGreg Clayton 
66b9c1b51eSKate Stone void SBModuleSpec::SetFileSpec(const lldb::SBFileSpec &sb_spec) {
67baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpec, SetFileSpec,
68baf5664fSJonas Devlieghere                      (const lldb::SBFileSpec &), sb_spec);
69baf5664fSJonas Devlieghere 
70d5b44036SJonas Devlieghere   m_opaque_up->GetFileSpec() = *sb_spec;
71226cce25SGreg Clayton }
72226cce25SGreg Clayton 
73b9c1b51eSKate Stone lldb::SBFileSpec SBModuleSpec::GetPlatformFileSpec() {
74baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec,
75baf5664fSJonas Devlieghere                              GetPlatformFileSpec);
76baf5664fSJonas Devlieghere 
77*d232abc3SJonas Devlieghere   return SBFileSpec(m_opaque_up->GetPlatformFileSpec());
78226cce25SGreg Clayton }
79226cce25SGreg Clayton 
80b9c1b51eSKate Stone void SBModuleSpec::SetPlatformFileSpec(const lldb::SBFileSpec &sb_spec) {
81baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpec, SetPlatformFileSpec,
82baf5664fSJonas Devlieghere                      (const lldb::SBFileSpec &), sb_spec);
83baf5664fSJonas Devlieghere 
84d5b44036SJonas Devlieghere   m_opaque_up->GetPlatformFileSpec() = *sb_spec;
85226cce25SGreg Clayton }
86226cce25SGreg Clayton 
87b9c1b51eSKate Stone lldb::SBFileSpec SBModuleSpec::GetSymbolFileSpec() {
88baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec, GetSymbolFileSpec);
89baf5664fSJonas Devlieghere 
90*d232abc3SJonas Devlieghere   return SBFileSpec(m_opaque_up->GetSymbolFileSpec());
91226cce25SGreg Clayton }
92226cce25SGreg Clayton 
93b9c1b51eSKate Stone void SBModuleSpec::SetSymbolFileSpec(const lldb::SBFileSpec &sb_spec) {
94baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpec, SetSymbolFileSpec,
95baf5664fSJonas Devlieghere                      (const lldb::SBFileSpec &), sb_spec);
96baf5664fSJonas Devlieghere 
97d5b44036SJonas Devlieghere   m_opaque_up->GetSymbolFileSpec() = *sb_spec;
98226cce25SGreg Clayton }
99226cce25SGreg Clayton 
100b9c1b51eSKate Stone const char *SBModuleSpec::GetObjectName() {
101baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModuleSpec, GetObjectName);
102baf5664fSJonas Devlieghere 
103d5b44036SJonas Devlieghere   return m_opaque_up->GetObjectName().GetCString();
104226cce25SGreg Clayton }
105226cce25SGreg Clayton 
106b9c1b51eSKate Stone void SBModuleSpec::SetObjectName(const char *name) {
107baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpec, SetObjectName, (const char *), name);
108baf5664fSJonas Devlieghere 
109d5b44036SJonas Devlieghere   m_opaque_up->GetObjectName().SetCString(name);
110226cce25SGreg Clayton }
111226cce25SGreg Clayton 
112b9c1b51eSKate Stone const char *SBModuleSpec::GetTriple() {
113baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModuleSpec, GetTriple);
114baf5664fSJonas Devlieghere 
115d5b44036SJonas Devlieghere   std::string triple(m_opaque_up->GetArchitecture().GetTriple().str());
11605097246SAdrian Prantl   // Unique the string so we don't run into ownership issues since the const
11705097246SAdrian Prantl   // strings put the string into the string pool once and the strings never
11805097246SAdrian Prantl   // comes out
119226cce25SGreg Clayton   ConstString const_triple(triple.c_str());
120226cce25SGreg Clayton   return const_triple.GetCString();
121226cce25SGreg Clayton }
122226cce25SGreg Clayton 
123b9c1b51eSKate Stone void SBModuleSpec::SetTriple(const char *triple) {
124baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpec, SetTriple, (const char *), triple);
125baf5664fSJonas Devlieghere 
126d5b44036SJonas Devlieghere   m_opaque_up->GetArchitecture().SetTriple(triple);
127226cce25SGreg Clayton }
128226cce25SGreg Clayton 
129b9c1b51eSKate Stone const uint8_t *SBModuleSpec::GetUUIDBytes() {
130d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().GetBytes().data();
131226cce25SGreg Clayton }
132226cce25SGreg Clayton 
133b9c1b51eSKate Stone size_t SBModuleSpec::GetUUIDLength() {
134baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModuleSpec, GetUUIDLength);
135baf5664fSJonas Devlieghere 
136d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().GetBytes().size();
137226cce25SGreg Clayton }
138226cce25SGreg Clayton 
139b9c1b51eSKate Stone bool SBModuleSpec::SetUUIDBytes(const uint8_t *uuid, size_t uuid_len) {
140d5b44036SJonas Devlieghere   m_opaque_up->GetUUID() = UUID::fromOptionalData(uuid, uuid_len);
141d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().IsValid();
142226cce25SGreg Clayton }
143226cce25SGreg Clayton 
144b9c1b51eSKate Stone bool SBModuleSpec::GetDescription(lldb::SBStream &description) {
145baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(bool, SBModuleSpec, GetDescription, (lldb::SBStream &),
146baf5664fSJonas Devlieghere                      description);
147baf5664fSJonas Devlieghere 
148d5b44036SJonas Devlieghere   m_opaque_up->Dump(description.ref());
149226cce25SGreg Clayton   return true;
150226cce25SGreg Clayton }
151226cce25SGreg Clayton 
152baf5664fSJonas Devlieghere SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
153baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModuleSpecList);
154baf5664fSJonas Devlieghere }
155226cce25SGreg Clayton 
156b9c1b51eSKate Stone SBModuleSpecList::SBModuleSpecList(const SBModuleSpecList &rhs)
157baf5664fSJonas Devlieghere     : m_opaque_up(new ModuleSpecList(*rhs.m_opaque_up)) {
158baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBModuleSpecList, (const lldb::SBModuleSpecList &),
159baf5664fSJonas Devlieghere                           rhs);
160baf5664fSJonas Devlieghere }
161226cce25SGreg Clayton 
162b9c1b51eSKate Stone SBModuleSpecList &SBModuleSpecList::operator=(const SBModuleSpecList &rhs) {
163baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(
164baf5664fSJonas Devlieghere       lldb::SBModuleSpecList &,
165baf5664fSJonas Devlieghere       SBModuleSpecList, operator=,(const lldb::SBModuleSpecList &), rhs);
166baf5664fSJonas Devlieghere 
167226cce25SGreg Clayton   if (this != &rhs)
168d5b44036SJonas Devlieghere     *m_opaque_up = *rhs.m_opaque_up;
169*d232abc3SJonas Devlieghere   return *this;
170226cce25SGreg Clayton }
171226cce25SGreg Clayton 
172866b7a65SJonas Devlieghere SBModuleSpecList::~SBModuleSpecList() = default;
173226cce25SGreg Clayton 
174b9c1b51eSKate Stone SBModuleSpecList SBModuleSpecList::GetModuleSpecifications(const char *path) {
175baf5664fSJonas Devlieghere   LLDB_RECORD_STATIC_METHOD(lldb::SBModuleSpecList, SBModuleSpecList,
176baf5664fSJonas Devlieghere                             GetModuleSpecifications, (const char *), path);
177baf5664fSJonas Devlieghere 
178226cce25SGreg Clayton   SBModuleSpecList specs;
1798f3be7a3SJonas Devlieghere   FileSpec file_spec(path);
1808f3be7a3SJonas Devlieghere   FileSystem::Instance().Resolve(file_spec);
181226cce25SGreg Clayton   Host::ResolveExecutableInBundle(file_spec);
182d5b44036SJonas Devlieghere   ObjectFile::GetModuleSpecifications(file_spec, 0, 0, *specs.m_opaque_up);
183*d232abc3SJonas Devlieghere   return specs;
184226cce25SGreg Clayton }
185226cce25SGreg Clayton 
186b9c1b51eSKate Stone void SBModuleSpecList::Append(const SBModuleSpec &spec) {
187baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpecList, Append,
188baf5664fSJonas Devlieghere                      (const lldb::SBModuleSpec &), spec);
189baf5664fSJonas Devlieghere 
190d5b44036SJonas Devlieghere   m_opaque_up->Append(*spec.m_opaque_up);
191226cce25SGreg Clayton }
192226cce25SGreg Clayton 
193b9c1b51eSKate Stone void SBModuleSpecList::Append(const SBModuleSpecList &spec_list) {
194baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBModuleSpecList, Append,
195baf5664fSJonas Devlieghere                      (const lldb::SBModuleSpecList &), spec_list);
196baf5664fSJonas Devlieghere 
197d5b44036SJonas Devlieghere   m_opaque_up->Append(*spec_list.m_opaque_up);
198226cce25SGreg Clayton }
199226cce25SGreg Clayton 
200baf5664fSJonas Devlieghere size_t SBModuleSpecList::GetSize() {
201baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModuleSpecList, GetSize);
202baf5664fSJonas Devlieghere 
203baf5664fSJonas Devlieghere   return m_opaque_up->GetSize();
204baf5664fSJonas Devlieghere }
205226cce25SGreg Clayton 
206b9c1b51eSKate Stone SBModuleSpec SBModuleSpecList::GetSpecAtIndex(size_t i) {
207baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(lldb::SBModuleSpec, SBModuleSpecList, GetSpecAtIndex,
208baf5664fSJonas Devlieghere                      (size_t), i);
209baf5664fSJonas Devlieghere 
210226cce25SGreg Clayton   SBModuleSpec sb_module_spec;
211d5b44036SJonas Devlieghere   m_opaque_up->GetModuleSpecAtIndex(i, *sb_module_spec.m_opaque_up);
212*d232abc3SJonas Devlieghere   return sb_module_spec;
213226cce25SGreg Clayton }
214226cce25SGreg Clayton 
215226cce25SGreg Clayton SBModuleSpec
216b9c1b51eSKate Stone SBModuleSpecList::FindFirstMatchingSpec(const SBModuleSpec &match_spec) {
217baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(lldb::SBModuleSpec, SBModuleSpecList,
218baf5664fSJonas Devlieghere                      FindFirstMatchingSpec, (const lldb::SBModuleSpec &),
219baf5664fSJonas Devlieghere                      match_spec);
220baf5664fSJonas Devlieghere 
221226cce25SGreg Clayton   SBModuleSpec sb_module_spec;
222d5b44036SJonas Devlieghere   m_opaque_up->FindMatchingModuleSpec(*match_spec.m_opaque_up,
223d5b44036SJonas Devlieghere                                       *sb_module_spec.m_opaque_up);
224*d232abc3SJonas Devlieghere   return sb_module_spec;
225226cce25SGreg Clayton }
226226cce25SGreg Clayton 
227226cce25SGreg Clayton SBModuleSpecList
228b9c1b51eSKate Stone SBModuleSpecList::FindMatchingSpecs(const SBModuleSpec &match_spec) {
229baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(lldb::SBModuleSpecList, SBModuleSpecList,
230baf5664fSJonas Devlieghere                      FindMatchingSpecs, (const lldb::SBModuleSpec &),
231baf5664fSJonas Devlieghere                      match_spec);
232baf5664fSJonas Devlieghere 
233226cce25SGreg Clayton   SBModuleSpecList specs;
234d5b44036SJonas Devlieghere   m_opaque_up->FindMatchingModuleSpecs(*match_spec.m_opaque_up,
235d5b44036SJonas Devlieghere                                        *specs.m_opaque_up);
236*d232abc3SJonas Devlieghere   return specs;
237226cce25SGreg Clayton }
238226cce25SGreg Clayton 
239b9c1b51eSKate Stone bool SBModuleSpecList::GetDescription(lldb::SBStream &description) {
240baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(bool, SBModuleSpecList, GetDescription, (lldb::SBStream &),
241baf5664fSJonas Devlieghere                      description);
242baf5664fSJonas Devlieghere 
243d5b44036SJonas Devlieghere   m_opaque_up->Dump(description.ref());
244226cce25SGreg Clayton   return true;
245226cce25SGreg Clayton }
246