1 //===-- SBModule.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/SBModule.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBModuleSpec.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBSymbolContextList.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/ValueObjectVariable.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Symbol/SymbolVendor.h"
24 #include "lldb/Symbol/Symtab.h"
25 #include "lldb/Symbol/TypeSystem.h"
26 #include "lldb/Symbol/VariableList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/StreamString.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 SBModule::SBModule() : m_opaque_sp() {
34   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModule);
35 }
36 
37 SBModule::SBModule(const lldb::ModuleSP &module_sp) : m_opaque_sp(module_sp) {}
38 
39 SBModule::SBModule(const SBModuleSpec &module_spec) : m_opaque_sp() {
40   LLDB_RECORD_CONSTRUCTOR(SBModule, (const lldb::SBModuleSpec &), module_spec);
41 
42   ModuleSP module_sp;
43   Status error = ModuleList::GetSharedModule(
44       *module_spec.m_opaque_up, module_sp, nullptr, nullptr, nullptr);
45   if (module_sp)
46     SetSP(module_sp);
47 }
48 
49 SBModule::SBModule(const SBModule &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
50   LLDB_RECORD_CONSTRUCTOR(SBModule, (const lldb::SBModule &), rhs);
51 }
52 
53 SBModule::SBModule(lldb::SBProcess &process, lldb::addr_t header_addr)
54     : m_opaque_sp() {
55   LLDB_RECORD_CONSTRUCTOR(SBModule, (lldb::SBProcess &, lldb::addr_t), process,
56                           header_addr);
57 
58   ProcessSP process_sp(process.GetSP());
59   if (process_sp) {
60     m_opaque_sp = process_sp->ReadModuleFromMemory(FileSpec(), header_addr);
61     if (m_opaque_sp) {
62       Target &target = process_sp->GetTarget();
63       bool changed = false;
64       m_opaque_sp->SetLoadAddress(target, 0, true, changed);
65       target.GetImages().Append(m_opaque_sp);
66     }
67   }
68 }
69 
70 const SBModule &SBModule::operator=(const SBModule &rhs) {
71   LLDB_RECORD_METHOD(const lldb::SBModule &,
72                      SBModule, operator=,(const lldb::SBModule &), rhs);
73 
74   if (this != &rhs)
75     m_opaque_sp = rhs.m_opaque_sp;
76   return LLDB_RECORD_RESULT(*this);
77 }
78 
79 SBModule::~SBModule() {}
80 
81 bool SBModule::IsValid() const {
82   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModule, IsValid);
83   return this->operator bool();
84 }
85 SBModule::operator bool() const {
86   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModule, operator bool);
87 
88   return m_opaque_sp.get() != nullptr;
89 }
90 
91 void SBModule::Clear() {
92   LLDB_RECORD_METHOD_NO_ARGS(void, SBModule, Clear);
93 
94   m_opaque_sp.reset();
95 }
96 
97 SBFileSpec SBModule::GetFileSpec() const {
98   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule, GetFileSpec);
99 
100   SBFileSpec file_spec;
101   ModuleSP module_sp(GetSP());
102   if (module_sp)
103     file_spec.SetFileSpec(module_sp->GetFileSpec());
104 
105   return LLDB_RECORD_RESULT(file_spec);
106 }
107 
108 lldb::SBFileSpec SBModule::GetPlatformFileSpec() const {
109   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
110                                    GetPlatformFileSpec);
111 
112 
113   SBFileSpec file_spec;
114   ModuleSP module_sp(GetSP());
115   if (module_sp)
116     file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
117 
118   return LLDB_RECORD_RESULT(file_spec);
119 }
120 
121 bool SBModule::SetPlatformFileSpec(const lldb::SBFileSpec &platform_file) {
122   LLDB_RECORD_METHOD(bool, SBModule, SetPlatformFileSpec,
123                      (const lldb::SBFileSpec &), platform_file);
124 
125   bool result = false;
126 
127   ModuleSP module_sp(GetSP());
128   if (module_sp) {
129     module_sp->SetPlatformFileSpec(*platform_file);
130     result = true;
131   }
132 
133   return result;
134 }
135 
136 lldb::SBFileSpec SBModule::GetRemoteInstallFileSpec() {
137   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModule,
138                              GetRemoteInstallFileSpec);
139 
140   SBFileSpec sb_file_spec;
141   ModuleSP module_sp(GetSP());
142   if (module_sp)
143     sb_file_spec.SetFileSpec(module_sp->GetRemoteInstallFileSpec());
144   return LLDB_RECORD_RESULT(sb_file_spec);
145 }
146 
147 bool SBModule::SetRemoteInstallFileSpec(lldb::SBFileSpec &file) {
148   LLDB_RECORD_METHOD(bool, SBModule, SetRemoteInstallFileSpec,
149                      (lldb::SBFileSpec &), file);
150 
151   ModuleSP module_sp(GetSP());
152   if (module_sp) {
153     module_sp->SetRemoteInstallFileSpec(file.ref());
154     return true;
155   }
156   return false;
157 }
158 
159 const uint8_t *SBModule::GetUUIDBytes() const {
160   LLDB_RECORD_METHOD_CONST_NO_ARGS(const uint8_t *, SBModule, GetUUIDBytes);
161 
162   const uint8_t *uuid_bytes = nullptr;
163   ModuleSP module_sp(GetSP());
164   if (module_sp)
165     uuid_bytes = module_sp->GetUUID().GetBytes().data();
166 
167   return uuid_bytes;
168 }
169 
170 const char *SBModule::GetUUIDString() const {
171   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBModule, GetUUIDString);
172 
173   const char *uuid_cstr = nullptr;
174   ModuleSP module_sp(GetSP());
175   if (module_sp) {
176     // We are going to return a "const char *" value through the public API, so
177     // we need to constify it so it gets added permanently the string pool and
178     // then we don't need to worry about the lifetime of the string as it will
179     // never go away once it has been put into the ConstString string pool
180     uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString();
181   }
182 
183   if (uuid_cstr && uuid_cstr[0]) {
184     return uuid_cstr;
185   }
186 
187   return nullptr;
188 }
189 
190 bool SBModule::operator==(const SBModule &rhs) const {
191   LLDB_RECORD_METHOD_CONST(bool, SBModule, operator==,(const lldb::SBModule &),
192                            rhs);
193 
194   if (m_opaque_sp)
195     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
196   return false;
197 }
198 
199 bool SBModule::operator!=(const SBModule &rhs) const {
200   LLDB_RECORD_METHOD_CONST(bool, SBModule, operator!=,(const lldb::SBModule &),
201                            rhs);
202 
203   if (m_opaque_sp)
204     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
205   return false;
206 }
207 
208 ModuleSP SBModule::GetSP() const { return m_opaque_sp; }
209 
210 void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; }
211 
212 SBAddress SBModule::ResolveFileAddress(lldb::addr_t vm_addr) {
213   LLDB_RECORD_METHOD(lldb::SBAddress, SBModule, ResolveFileAddress,
214                      (lldb::addr_t), vm_addr);
215 
216   lldb::SBAddress sb_addr;
217   ModuleSP module_sp(GetSP());
218   if (module_sp) {
219     Address addr;
220     if (module_sp->ResolveFileAddress(vm_addr, addr))
221       sb_addr.ref() = addr;
222   }
223   return LLDB_RECORD_RESULT(sb_addr);
224 }
225 
226 SBSymbolContext
227 SBModule::ResolveSymbolContextForAddress(const SBAddress &addr,
228                                          uint32_t resolve_scope) {
229   LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBModule,
230                      ResolveSymbolContextForAddress,
231                      (const lldb::SBAddress &, uint32_t), addr, resolve_scope);
232 
233   SBSymbolContext sb_sc;
234   ModuleSP module_sp(GetSP());
235   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
236   if (module_sp && addr.IsValid())
237     module_sp->ResolveSymbolContextForAddress(addr.ref(), scope, *sb_sc);
238   return LLDB_RECORD_RESULT(sb_sc);
239 }
240 
241 bool SBModule::GetDescription(SBStream &description) {
242   LLDB_RECORD_METHOD(bool, SBModule, GetDescription, (lldb::SBStream &),
243                      description);
244 
245   Stream &strm = description.ref();
246 
247   ModuleSP module_sp(GetSP());
248   if (module_sp) {
249     module_sp->GetDescription(&strm);
250   } else
251     strm.PutCString("No value");
252 
253   return true;
254 }
255 
256 uint32_t SBModule::GetNumCompileUnits() {
257   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetNumCompileUnits);
258 
259   ModuleSP module_sp(GetSP());
260   if (module_sp) {
261     return module_sp->GetNumCompileUnits();
262   }
263   return 0;
264 }
265 
266 SBCompileUnit SBModule::GetCompileUnitAtIndex(uint32_t index) {
267   LLDB_RECORD_METHOD(lldb::SBCompileUnit, SBModule, GetCompileUnitAtIndex,
268                      (uint32_t), index);
269 
270   SBCompileUnit sb_cu;
271   ModuleSP module_sp(GetSP());
272   if (module_sp) {
273     CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(index);
274     sb_cu.reset(cu_sp.get());
275   }
276   return LLDB_RECORD_RESULT(sb_cu);
277 }
278 
279 SBSymbolContextList SBModule::FindCompileUnits(const SBFileSpec &sb_file_spec) {
280   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindCompileUnits,
281                      (const lldb::SBFileSpec &), sb_file_spec);
282 
283   SBSymbolContextList sb_sc_list;
284   const ModuleSP module_sp(GetSP());
285   if (sb_file_spec.IsValid() && module_sp) {
286     const bool append = true;
287     module_sp->FindCompileUnits(*sb_file_spec, append, *sb_sc_list);
288   }
289   return LLDB_RECORD_RESULT(sb_sc_list);
290 }
291 
292 static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) {
293   if (module_sp) {
294     SymbolVendor *symbols = module_sp->GetSymbolVendor();
295     if (symbols)
296       return symbols->GetSymtab();
297   }
298   return nullptr;
299 }
300 
301 size_t SBModule::GetNumSymbols() {
302   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSymbols);
303 
304   ModuleSP module_sp(GetSP());
305   if (module_sp) {
306     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
307     if (symtab)
308       return symtab->GetNumSymbols();
309   }
310   return 0;
311 }
312 
313 SBSymbol SBModule::GetSymbolAtIndex(size_t idx) {
314   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, GetSymbolAtIndex, (size_t), idx);
315 
316   SBSymbol sb_symbol;
317   ModuleSP module_sp(GetSP());
318   Symtab *symtab = GetUnifiedSymbolTable(module_sp);
319   if (symtab)
320     sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx));
321   return LLDB_RECORD_RESULT(sb_symbol);
322 }
323 
324 lldb::SBSymbol SBModule::FindSymbol(const char *name,
325                                     lldb::SymbolType symbol_type) {
326   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, FindSymbol,
327                      (const char *, lldb::SymbolType), name, symbol_type);
328 
329   SBSymbol sb_symbol;
330   if (name && name[0]) {
331     ModuleSP module_sp(GetSP());
332     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
333     if (symtab)
334       sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(
335           ConstString(name), symbol_type, Symtab::eDebugAny,
336           Symtab::eVisibilityAny));
337   }
338   return LLDB_RECORD_RESULT(sb_symbol);
339 }
340 
341 lldb::SBSymbolContextList SBModule::FindSymbols(const char *name,
342                                                 lldb::SymbolType symbol_type) {
343   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindSymbols,
344                      (const char *, lldb::SymbolType), name, symbol_type);
345 
346   SBSymbolContextList sb_sc_list;
347   if (name && name[0]) {
348     ModuleSP module_sp(GetSP());
349     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
350     if (symtab) {
351       std::vector<uint32_t> matching_symbol_indexes;
352       const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(
353           ConstString(name), symbol_type, matching_symbol_indexes);
354       if (num_matches) {
355         SymbolContext sc;
356         sc.module_sp = module_sp;
357         SymbolContextList &sc_list = *sb_sc_list;
358         for (size_t i = 0; i < num_matches; ++i) {
359           sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]);
360           if (sc.symbol)
361             sc_list.Append(sc);
362         }
363       }
364     }
365   }
366   return LLDB_RECORD_RESULT(sb_sc_list);
367 }
368 
369 size_t SBModule::GetNumSections() {
370   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSections);
371 
372   ModuleSP module_sp(GetSP());
373   if (module_sp) {
374     // Give the symbol vendor a chance to add to the unified section list.
375     module_sp->GetSymbolVendor();
376     SectionList *section_list = module_sp->GetSectionList();
377     if (section_list)
378       return section_list->GetSize();
379   }
380   return 0;
381 }
382 
383 SBSection SBModule::GetSectionAtIndex(size_t idx) {
384   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, GetSectionAtIndex, (size_t),
385                      idx);
386 
387   SBSection sb_section;
388   ModuleSP module_sp(GetSP());
389   if (module_sp) {
390     // Give the symbol vendor a chance to add to the unified section list.
391     module_sp->GetSymbolVendor();
392     SectionList *section_list = module_sp->GetSectionList();
393 
394     if (section_list)
395       sb_section.SetSP(section_list->GetSectionAtIndex(idx));
396   }
397   return LLDB_RECORD_RESULT(sb_section);
398 }
399 
400 lldb::SBSymbolContextList SBModule::FindFunctions(const char *name,
401                                                   uint32_t name_type_mask) {
402   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindFunctions,
403                      (const char *, uint32_t), name, name_type_mask);
404 
405   lldb::SBSymbolContextList sb_sc_list;
406   ModuleSP module_sp(GetSP());
407   if (name && module_sp) {
408     const bool append = true;
409     const bool symbols_ok = true;
410     const bool inlines_ok = true;
411     FunctionNameType type = static_cast<FunctionNameType>(name_type_mask);
412     module_sp->FindFunctions(ConstString(name), nullptr, type, symbols_ok,
413                              inlines_ok, append, *sb_sc_list);
414   }
415   return LLDB_RECORD_RESULT(sb_sc_list);
416 }
417 
418 SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,
419                                           uint32_t max_matches) {
420   LLDB_RECORD_METHOD(lldb::SBValueList, SBModule, FindGlobalVariables,
421                      (lldb::SBTarget &, const char *, uint32_t), target, name,
422                      max_matches);
423 
424   SBValueList sb_value_list;
425   ModuleSP module_sp(GetSP());
426   if (name && module_sp) {
427     VariableList variable_list;
428     const uint32_t match_count = module_sp->FindGlobalVariables(
429         ConstString(name), nullptr, max_matches, variable_list);
430 
431     if (match_count > 0) {
432       for (uint32_t i = 0; i < match_count; ++i) {
433         lldb::ValueObjectSP valobj_sp;
434         TargetSP target_sp(target.GetSP());
435         valobj_sp = ValueObjectVariable::Create(
436             target_sp.get(), variable_list.GetVariableAtIndex(i));
437         if (valobj_sp)
438           sb_value_list.Append(SBValue(valobj_sp));
439       }
440     }
441   }
442 
443   return LLDB_RECORD_RESULT(sb_value_list);
444 }
445 
446 lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target,
447                                                 const char *name) {
448   LLDB_RECORD_METHOD(lldb::SBValue, SBModule, FindFirstGlobalVariable,
449                      (lldb::SBTarget &, const char *), target, name);
450 
451   SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
452   if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
453     return LLDB_RECORD_RESULT(sb_value_list.GetValueAtIndex(0));
454   return LLDB_RECORD_RESULT(SBValue());
455 }
456 
457 lldb::SBType SBModule::FindFirstType(const char *name_cstr) {
458   LLDB_RECORD_METHOD(lldb::SBType, SBModule, FindFirstType, (const char *),
459                      name_cstr);
460 
461   SBType sb_type;
462   ModuleSP module_sp(GetSP());
463   if (name_cstr && module_sp) {
464     SymbolContext sc;
465     const bool exact_match = false;
466     ConstString name(name_cstr);
467 
468     sb_type = SBType(module_sp->FindFirstType(sc, name, exact_match));
469 
470     if (!sb_type.IsValid()) {
471       auto type_system_or_err =
472           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
473       if (auto err = type_system_or_err.takeError()) {
474         llvm::consumeError(std::move(err));
475         return LLDB_RECORD_RESULT(SBType());
476       }
477       sb_type = SBType(type_system_or_err->GetBuiltinTypeByName(name));
478     }
479   }
480   return LLDB_RECORD_RESULT(sb_type);
481 }
482 
483 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {
484   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetBasicType, (lldb::BasicType),
485                      type);
486 
487   ModuleSP module_sp(GetSP());
488   if (module_sp) {
489     auto type_system_or_err =
490         module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
491     if (auto err = type_system_or_err.takeError()) {
492       llvm::consumeError(std::move(err));
493     } else {
494       return LLDB_RECORD_RESULT(
495           SBType(type_system_or_err->GetBasicTypeFromAST(type)));
496     }
497   }
498   return LLDB_RECORD_RESULT(SBType());
499 }
500 
501 lldb::SBTypeList SBModule::FindTypes(const char *type) {
502   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, FindTypes, (const char *),
503                      type);
504 
505   SBTypeList retval;
506 
507   ModuleSP module_sp(GetSP());
508   if (type && module_sp) {
509     TypeList type_list;
510     const bool exact_match = false;
511     ConstString name(type);
512     llvm::DenseSet<SymbolFile *> searched_symbol_files;
513     const uint32_t num_matches = module_sp->FindTypes(
514         name, exact_match, UINT32_MAX, searched_symbol_files, type_list);
515 
516     if (num_matches > 0) {
517       for (size_t idx = 0; idx < num_matches; idx++) {
518         TypeSP type_sp(type_list.GetTypeAtIndex(idx));
519         if (type_sp)
520           retval.Append(SBType(type_sp));
521       }
522     } else {
523       auto type_system_or_err =
524           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
525       if (auto err = type_system_or_err.takeError()) {
526         llvm::consumeError(std::move(err));
527       } else {
528         CompilerType compiler_type =
529             type_system_or_err->GetBuiltinTypeByName(name);
530         if (compiler_type)
531           retval.Append(SBType(compiler_type));
532       }
533     }
534   }
535 
536   return LLDB_RECORD_RESULT(retval);
537 }
538 
539 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {
540   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetTypeByID, (lldb::user_id_t),
541                      uid);
542 
543   ModuleSP module_sp(GetSP());
544   if (module_sp) {
545     SymbolVendor *vendor = module_sp->GetSymbolVendor();
546     if (vendor) {
547       Type *type_ptr = vendor->ResolveTypeUID(uid);
548       if (type_ptr)
549         return LLDB_RECORD_RESULT(SBType(type_ptr->shared_from_this()));
550     }
551   }
552   return LLDB_RECORD_RESULT(SBType());
553 }
554 
555 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {
556   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t),
557                      type_mask);
558 
559   SBTypeList sb_type_list;
560 
561   ModuleSP module_sp(GetSP());
562   if (!module_sp)
563     return LLDB_RECORD_RESULT(sb_type_list);
564   SymbolVendor *vendor = module_sp->GetSymbolVendor();
565   if (!vendor)
566     return LLDB_RECORD_RESULT(sb_type_list);
567 
568   TypeClass type_class = static_cast<TypeClass>(type_mask);
569   TypeList type_list;
570   vendor->GetTypes(nullptr, type_class, type_list);
571   sb_type_list.m_opaque_up->Append(type_list);
572   return LLDB_RECORD_RESULT(sb_type_list);
573 }
574 
575 SBSection SBModule::FindSection(const char *sect_name) {
576   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, FindSection, (const char *),
577                      sect_name);
578 
579   SBSection sb_section;
580 
581   ModuleSP module_sp(GetSP());
582   if (sect_name && module_sp) {
583     // Give the symbol vendor a chance to add to the unified section list.
584     module_sp->GetSymbolVendor();
585     SectionList *section_list = module_sp->GetSectionList();
586     if (section_list) {
587       ConstString const_sect_name(sect_name);
588       SectionSP section_sp(section_list->FindSectionByName(const_sect_name));
589       if (section_sp) {
590         sb_section.SetSP(section_sp);
591       }
592     }
593   }
594   return LLDB_RECORD_RESULT(sb_section);
595 }
596 
597 lldb::ByteOrder SBModule::GetByteOrder() {
598   LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBModule, GetByteOrder);
599 
600   ModuleSP module_sp(GetSP());
601   if (module_sp)
602     return module_sp->GetArchitecture().GetByteOrder();
603   return eByteOrderInvalid;
604 }
605 
606 const char *SBModule::GetTriple() {
607   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModule, GetTriple);
608 
609   ModuleSP module_sp(GetSP());
610   if (module_sp) {
611     std::string triple(module_sp->GetArchitecture().GetTriple().str());
612     // Unique the string so we don't run into ownership issues since the const
613     // strings put the string into the string pool once and the strings never
614     // comes out
615     ConstString const_triple(triple.c_str());
616     return const_triple.GetCString();
617   }
618   return nullptr;
619 }
620 
621 uint32_t SBModule::GetAddressByteSize() {
622   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetAddressByteSize);
623 
624   ModuleSP module_sp(GetSP());
625   if (module_sp)
626     return module_sp->GetArchitecture().GetAddressByteSize();
627   return sizeof(void *);
628 }
629 
630 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {
631   LLDB_RECORD_METHOD(uint32_t, SBModule, GetVersion, (uint32_t *, uint32_t),
632                      versions, num_versions);
633 
634   llvm::VersionTuple version;
635   if (ModuleSP module_sp = GetSP())
636     version = module_sp->GetVersion();
637   uint32_t result = 0;
638   if (!version.empty())
639     ++result;
640   if (version.getMinor())
641     ++result;
642   if(version.getSubminor())
643     ++result;
644 
645   if (!versions)
646     return result;
647 
648   if (num_versions > 0)
649     versions[0] = version.empty() ? UINT32_MAX : version.getMajor();
650   if (num_versions > 1)
651     versions[1] = version.getMinor().getValueOr(UINT32_MAX);
652   if (num_versions > 2)
653     versions[2] = version.getSubminor().getValueOr(UINT32_MAX);
654   for (uint32_t i = 3; i < num_versions; ++i)
655     versions[i] = UINT32_MAX;
656   return result;
657 }
658 
659 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {
660   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
661                                    GetSymbolFileSpec);
662 
663   lldb::SBFileSpec sb_file_spec;
664   ModuleSP module_sp(GetSP());
665   if (module_sp) {
666     SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor();
667     if (symbol_vendor_ptr)
668       sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec());
669   }
670   return LLDB_RECORD_RESULT(sb_file_spec);
671 }
672 
673 lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const {
674   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
675                                    GetObjectFileHeaderAddress);
676 
677   lldb::SBAddress sb_addr;
678   ModuleSP module_sp(GetSP());
679   if (module_sp) {
680     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
681     if (objfile_ptr)
682       sb_addr.ref() = objfile_ptr->GetBaseAddress();
683   }
684   return LLDB_RECORD_RESULT(sb_addr);
685 }
686 
687 lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {
688   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
689                                    GetObjectFileEntryPointAddress);
690 
691   lldb::SBAddress sb_addr;
692   ModuleSP module_sp(GetSP());
693   if (module_sp) {
694     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
695     if (objfile_ptr)
696       sb_addr.ref() = objfile_ptr->GetEntryPointAddress();
697   }
698   return LLDB_RECORD_RESULT(sb_addr);
699 }
700 
701 namespace lldb_private {
702 namespace repro {
703 
704 template <>
705 void RegisterMethods<SBModule>(Registry &R) {
706   LLDB_REGISTER_CONSTRUCTOR(SBModule, ());
707   LLDB_REGISTER_CONSTRUCTOR(SBModule, (const lldb::SBModuleSpec &));
708   LLDB_REGISTER_CONSTRUCTOR(SBModule, (const lldb::SBModule &));
709   LLDB_REGISTER_CONSTRUCTOR(SBModule, (lldb::SBProcess &, lldb::addr_t));
710   LLDB_REGISTER_METHOD(const lldb::SBModule &,
711                        SBModule, operator=,(const lldb::SBModule &));
712   LLDB_REGISTER_METHOD_CONST(bool, SBModule, IsValid, ());
713   LLDB_REGISTER_METHOD_CONST(bool, SBModule, operator bool, ());
714   LLDB_REGISTER_METHOD(void, SBModule, Clear, ());
715   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetFileSpec, ());
716   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetPlatformFileSpec,
717                              ());
718   LLDB_REGISTER_METHOD(bool, SBModule, SetPlatformFileSpec,
719                        (const lldb::SBFileSpec &));
720   LLDB_REGISTER_METHOD(lldb::SBFileSpec, SBModule, GetRemoteInstallFileSpec,
721                        ());
722   LLDB_REGISTER_METHOD(bool, SBModule, SetRemoteInstallFileSpec,
723                        (lldb::SBFileSpec &));
724   LLDB_REGISTER_METHOD_CONST(const char *, SBModule, GetUUIDString, ());
725   LLDB_REGISTER_METHOD_CONST(bool,
726                              SBModule, operator==,(const lldb::SBModule &));
727   LLDB_REGISTER_METHOD_CONST(bool,
728                              SBModule, operator!=,(const lldb::SBModule &));
729   LLDB_REGISTER_METHOD(lldb::SBAddress, SBModule, ResolveFileAddress,
730                        (lldb::addr_t));
731   LLDB_REGISTER_METHOD(lldb::SBSymbolContext, SBModule,
732                        ResolveSymbolContextForAddress,
733                        (const lldb::SBAddress &, uint32_t));
734   LLDB_REGISTER_METHOD(bool, SBModule, GetDescription, (lldb::SBStream &));
735   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetNumCompileUnits, ());
736   LLDB_REGISTER_METHOD(lldb::SBCompileUnit, SBModule, GetCompileUnitAtIndex,
737                        (uint32_t));
738   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindCompileUnits,
739                        (const lldb::SBFileSpec &));
740   LLDB_REGISTER_METHOD(size_t, SBModule, GetNumSymbols, ());
741   LLDB_REGISTER_METHOD(lldb::SBSymbol, SBModule, GetSymbolAtIndex, (size_t));
742   LLDB_REGISTER_METHOD(lldb::SBSymbol, SBModule, FindSymbol,
743                        (const char *, lldb::SymbolType));
744   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindSymbols,
745                        (const char *, lldb::SymbolType));
746   LLDB_REGISTER_METHOD(size_t, SBModule, GetNumSections, ());
747   LLDB_REGISTER_METHOD(lldb::SBSection, SBModule, GetSectionAtIndex,
748                        (size_t));
749   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindFunctions,
750                        (const char *, uint32_t));
751   LLDB_REGISTER_METHOD(lldb::SBValueList, SBModule, FindGlobalVariables,
752                        (lldb::SBTarget &, const char *, uint32_t));
753   LLDB_REGISTER_METHOD(lldb::SBValue, SBModule, FindFirstGlobalVariable,
754                        (lldb::SBTarget &, const char *));
755   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, FindFirstType, (const char *));
756   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, GetBasicType,
757                        (lldb::BasicType));
758   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBModule, FindTypes, (const char *));
759   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, GetTypeByID,
760                        (lldb::user_id_t));
761   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t));
762   LLDB_REGISTER_METHOD(lldb::SBSection, SBModule, FindSection,
763                        (const char *));
764   LLDB_REGISTER_METHOD(lldb::ByteOrder, SBModule, GetByteOrder, ());
765   LLDB_REGISTER_METHOD(const char *, SBModule, GetTriple, ());
766   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetAddressByteSize, ());
767   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetVersion,
768                        (uint32_t *, uint32_t));
769   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetSymbolFileSpec,
770                              ());
771   LLDB_REGISTER_METHOD_CONST(lldb::SBAddress, SBModule,
772                              GetObjectFileHeaderAddress, ());
773   LLDB_REGISTER_METHOD_CONST(lldb::SBAddress, SBModule,
774                              GetObjectFileEntryPointAddress, ());
775 }
776 
777 }
778 }
779