1 //===-- SBModuleSpec.cpp ----------------------------------------*- C++ -*-===//
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/SBModuleSpec.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Host/Host.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/Stream.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 SBModuleSpec::SBModuleSpec() : m_opaque_up(new lldb_private::ModuleSpec()) {
23   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModuleSpec);
24 }
25 
26 SBModuleSpec::SBModuleSpec(const SBModuleSpec &rhs) : m_opaque_up() {
27   LLDB_RECORD_CONSTRUCTOR(SBModuleSpec, (const lldb::SBModuleSpec &), rhs);
28 
29   m_opaque_up = clone(rhs.m_opaque_up);
30 }
31 
32 const SBModuleSpec &SBModuleSpec::operator=(const SBModuleSpec &rhs) {
33   LLDB_RECORD_METHOD(const lldb::SBModuleSpec &,
34                      SBModuleSpec, operator=,(const lldb::SBModuleSpec &), rhs);
35 
36   if (this != &rhs)
37     m_opaque_up = clone(rhs.m_opaque_up);
38   return *this;
39 }
40 
41 SBModuleSpec::~SBModuleSpec() {}
42 
43 bool SBModuleSpec::IsValid() const {
44   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModuleSpec, IsValid);
45 
46   return m_opaque_up->operator bool();
47 }
48 
49 void SBModuleSpec::Clear() {
50   LLDB_RECORD_METHOD_NO_ARGS(void, SBModuleSpec, Clear);
51 
52   m_opaque_up->Clear();
53 }
54 
55 SBFileSpec SBModuleSpec::GetFileSpec() {
56   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec, GetFileSpec);
57 
58   SBFileSpec sb_spec(m_opaque_up->GetFileSpec());
59   return LLDB_RECORD_RESULT(sb_spec);
60 }
61 
62 void SBModuleSpec::SetFileSpec(const lldb::SBFileSpec &sb_spec) {
63   LLDB_RECORD_METHOD(void, SBModuleSpec, SetFileSpec,
64                      (const lldb::SBFileSpec &), sb_spec);
65 
66   m_opaque_up->GetFileSpec() = *sb_spec;
67 }
68 
69 lldb::SBFileSpec SBModuleSpec::GetPlatformFileSpec() {
70   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec,
71                              GetPlatformFileSpec);
72 
73   return LLDB_RECORD_RESULT(SBFileSpec(m_opaque_up->GetPlatformFileSpec()));
74 }
75 
76 void SBModuleSpec::SetPlatformFileSpec(const lldb::SBFileSpec &sb_spec) {
77   LLDB_RECORD_METHOD(void, SBModuleSpec, SetPlatformFileSpec,
78                      (const lldb::SBFileSpec &), sb_spec);
79 
80   m_opaque_up->GetPlatformFileSpec() = *sb_spec;
81 }
82 
83 lldb::SBFileSpec SBModuleSpec::GetSymbolFileSpec() {
84   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModuleSpec, GetSymbolFileSpec);
85 
86   return LLDB_RECORD_RESULT(SBFileSpec(m_opaque_up->GetSymbolFileSpec()));
87 }
88 
89 void SBModuleSpec::SetSymbolFileSpec(const lldb::SBFileSpec &sb_spec) {
90   LLDB_RECORD_METHOD(void, SBModuleSpec, SetSymbolFileSpec,
91                      (const lldb::SBFileSpec &), sb_spec);
92 
93   m_opaque_up->GetSymbolFileSpec() = *sb_spec;
94 }
95 
96 const char *SBModuleSpec::GetObjectName() {
97   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModuleSpec, GetObjectName);
98 
99   return m_opaque_up->GetObjectName().GetCString();
100 }
101 
102 void SBModuleSpec::SetObjectName(const char *name) {
103   LLDB_RECORD_METHOD(void, SBModuleSpec, SetObjectName, (const char *), name);
104 
105   m_opaque_up->GetObjectName().SetCString(name);
106 }
107 
108 const char *SBModuleSpec::GetTriple() {
109   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModuleSpec, GetTriple);
110 
111   std::string triple(m_opaque_up->GetArchitecture().GetTriple().str());
112   // Unique the string so we don't run into ownership issues since the const
113   // strings put the string into the string pool once and the strings never
114   // comes out
115   ConstString const_triple(triple.c_str());
116   return const_triple.GetCString();
117 }
118 
119 void SBModuleSpec::SetTriple(const char *triple) {
120   LLDB_RECORD_METHOD(void, SBModuleSpec, SetTriple, (const char *), triple);
121 
122   m_opaque_up->GetArchitecture().SetTriple(triple);
123 }
124 
125 const uint8_t *SBModuleSpec::GetUUIDBytes() {
126   return m_opaque_up->GetUUID().GetBytes().data();
127 }
128 
129 size_t SBModuleSpec::GetUUIDLength() {
130   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModuleSpec, GetUUIDLength);
131 
132   return m_opaque_up->GetUUID().GetBytes().size();
133 }
134 
135 bool SBModuleSpec::SetUUIDBytes(const uint8_t *uuid, size_t uuid_len) {
136   m_opaque_up->GetUUID() = UUID::fromOptionalData(uuid, uuid_len);
137   return m_opaque_up->GetUUID().IsValid();
138 }
139 
140 bool SBModuleSpec::GetDescription(lldb::SBStream &description) {
141   LLDB_RECORD_METHOD(bool, SBModuleSpec, GetDescription, (lldb::SBStream &),
142                      description);
143 
144   m_opaque_up->Dump(description.ref());
145   return true;
146 }
147 
148 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
149   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModuleSpecList);
150 }
151 
152 SBModuleSpecList::SBModuleSpecList(const SBModuleSpecList &rhs)
153     : m_opaque_up(new ModuleSpecList(*rhs.m_opaque_up)) {
154   LLDB_RECORD_CONSTRUCTOR(SBModuleSpecList, (const lldb::SBModuleSpecList &),
155                           rhs);
156 }
157 
158 SBModuleSpecList &SBModuleSpecList::operator=(const SBModuleSpecList &rhs) {
159   LLDB_RECORD_METHOD(
160       lldb::SBModuleSpecList &,
161       SBModuleSpecList, operator=,(const lldb::SBModuleSpecList &), rhs);
162 
163   if (this != &rhs)
164     *m_opaque_up = *rhs.m_opaque_up;
165   return *this;
166 }
167 
168 SBModuleSpecList::~SBModuleSpecList() {}
169 
170 SBModuleSpecList SBModuleSpecList::GetModuleSpecifications(const char *path) {
171   LLDB_RECORD_STATIC_METHOD(lldb::SBModuleSpecList, SBModuleSpecList,
172                             GetModuleSpecifications, (const char *), path);
173 
174   SBModuleSpecList specs;
175   FileSpec file_spec(path);
176   FileSystem::Instance().Resolve(file_spec);
177   Host::ResolveExecutableInBundle(file_spec);
178   ObjectFile::GetModuleSpecifications(file_spec, 0, 0, *specs.m_opaque_up);
179   return LLDB_RECORD_RESULT(specs);
180 }
181 
182 void SBModuleSpecList::Append(const SBModuleSpec &spec) {
183   LLDB_RECORD_METHOD(void, SBModuleSpecList, Append,
184                      (const lldb::SBModuleSpec &), spec);
185 
186   m_opaque_up->Append(*spec.m_opaque_up);
187 }
188 
189 void SBModuleSpecList::Append(const SBModuleSpecList &spec_list) {
190   LLDB_RECORD_METHOD(void, SBModuleSpecList, Append,
191                      (const lldb::SBModuleSpecList &), spec_list);
192 
193   m_opaque_up->Append(*spec_list.m_opaque_up);
194 }
195 
196 size_t SBModuleSpecList::GetSize() {
197   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModuleSpecList, GetSize);
198 
199   return m_opaque_up->GetSize();
200 }
201 
202 SBModuleSpec SBModuleSpecList::GetSpecAtIndex(size_t i) {
203   LLDB_RECORD_METHOD(lldb::SBModuleSpec, SBModuleSpecList, GetSpecAtIndex,
204                      (size_t), i);
205 
206   SBModuleSpec sb_module_spec;
207   m_opaque_up->GetModuleSpecAtIndex(i, *sb_module_spec.m_opaque_up);
208   return LLDB_RECORD_RESULT(sb_module_spec);
209 }
210 
211 SBModuleSpec
212 SBModuleSpecList::FindFirstMatchingSpec(const SBModuleSpec &match_spec) {
213   LLDB_RECORD_METHOD(lldb::SBModuleSpec, SBModuleSpecList,
214                      FindFirstMatchingSpec, (const lldb::SBModuleSpec &),
215                      match_spec);
216 
217   SBModuleSpec sb_module_spec;
218   m_opaque_up->FindMatchingModuleSpec(*match_spec.m_opaque_up,
219                                       *sb_module_spec.m_opaque_up);
220   return LLDB_RECORD_RESULT(sb_module_spec);
221 }
222 
223 SBModuleSpecList
224 SBModuleSpecList::FindMatchingSpecs(const SBModuleSpec &match_spec) {
225   LLDB_RECORD_METHOD(lldb::SBModuleSpecList, SBModuleSpecList,
226                      FindMatchingSpecs, (const lldb::SBModuleSpec &),
227                      match_spec);
228 
229   SBModuleSpecList specs;
230   m_opaque_up->FindMatchingModuleSpecs(*match_spec.m_opaque_up,
231                                        *specs.m_opaque_up);
232   return LLDB_RECORD_RESULT(specs);
233 }
234 
235 bool SBModuleSpecList::GetDescription(lldb::SBStream &description) {
236   LLDB_RECORD_METHOD(bool, SBModuleSpecList, GetDescription, (lldb::SBStream &),
237                      description);
238 
239   m_opaque_up->Dump(description.ref());
240   return true;
241 }
242