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     return module_sp->GetSymtab();
295   return nullptr;
296 }
297 
298 size_t SBModule::GetNumSymbols() {
299   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSymbols);
300 
301   ModuleSP module_sp(GetSP());
302   if (Symtab *symtab = GetUnifiedSymbolTable(module_sp))
303     return symtab->GetNumSymbols();
304   return 0;
305 }
306 
307 SBSymbol SBModule::GetSymbolAtIndex(size_t idx) {
308   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, GetSymbolAtIndex, (size_t), idx);
309 
310   SBSymbol sb_symbol;
311   ModuleSP module_sp(GetSP());
312   Symtab *symtab = GetUnifiedSymbolTable(module_sp);
313   if (symtab)
314     sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx));
315   return LLDB_RECORD_RESULT(sb_symbol);
316 }
317 
318 lldb::SBSymbol SBModule::FindSymbol(const char *name,
319                                     lldb::SymbolType symbol_type) {
320   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, FindSymbol,
321                      (const char *, lldb::SymbolType), name, symbol_type);
322 
323   SBSymbol sb_symbol;
324   if (name && name[0]) {
325     ModuleSP module_sp(GetSP());
326     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
327     if (symtab)
328       sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(
329           ConstString(name), symbol_type, Symtab::eDebugAny,
330           Symtab::eVisibilityAny));
331   }
332   return LLDB_RECORD_RESULT(sb_symbol);
333 }
334 
335 lldb::SBSymbolContextList SBModule::FindSymbols(const char *name,
336                                                 lldb::SymbolType symbol_type) {
337   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindSymbols,
338                      (const char *, lldb::SymbolType), name, symbol_type);
339 
340   SBSymbolContextList sb_sc_list;
341   if (name && name[0]) {
342     ModuleSP module_sp(GetSP());
343     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
344     if (symtab) {
345       std::vector<uint32_t> matching_symbol_indexes;
346       const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(
347           ConstString(name), symbol_type, matching_symbol_indexes);
348       if (num_matches) {
349         SymbolContext sc;
350         sc.module_sp = module_sp;
351         SymbolContextList &sc_list = *sb_sc_list;
352         for (size_t i = 0; i < num_matches; ++i) {
353           sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]);
354           if (sc.symbol)
355             sc_list.Append(sc);
356         }
357       }
358     }
359   }
360   return LLDB_RECORD_RESULT(sb_sc_list);
361 }
362 
363 size_t SBModule::GetNumSections() {
364   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSections);
365 
366   ModuleSP module_sp(GetSP());
367   if (module_sp) {
368     // Give the symbol vendor a chance to add to the unified section list.
369     module_sp->GetSymbolVendor();
370     SectionList *section_list = module_sp->GetSectionList();
371     if (section_list)
372       return section_list->GetSize();
373   }
374   return 0;
375 }
376 
377 SBSection SBModule::GetSectionAtIndex(size_t idx) {
378   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, GetSectionAtIndex, (size_t),
379                      idx);
380 
381   SBSection sb_section;
382   ModuleSP module_sp(GetSP());
383   if (module_sp) {
384     // Give the symbol vendor a chance to add to the unified section list.
385     module_sp->GetSymbolVendor();
386     SectionList *section_list = module_sp->GetSectionList();
387 
388     if (section_list)
389       sb_section.SetSP(section_list->GetSectionAtIndex(idx));
390   }
391   return LLDB_RECORD_RESULT(sb_section);
392 }
393 
394 lldb::SBSymbolContextList SBModule::FindFunctions(const char *name,
395                                                   uint32_t name_type_mask) {
396   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindFunctions,
397                      (const char *, uint32_t), name, name_type_mask);
398 
399   lldb::SBSymbolContextList sb_sc_list;
400   ModuleSP module_sp(GetSP());
401   if (name && module_sp) {
402     const bool append = true;
403     const bool symbols_ok = true;
404     const bool inlines_ok = true;
405     FunctionNameType type = static_cast<FunctionNameType>(name_type_mask);
406     module_sp->FindFunctions(ConstString(name), nullptr, type, symbols_ok,
407                              inlines_ok, append, *sb_sc_list);
408   }
409   return LLDB_RECORD_RESULT(sb_sc_list);
410 }
411 
412 SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,
413                                           uint32_t max_matches) {
414   LLDB_RECORD_METHOD(lldb::SBValueList, SBModule, FindGlobalVariables,
415                      (lldb::SBTarget &, const char *, uint32_t), target, name,
416                      max_matches);
417 
418   SBValueList sb_value_list;
419   ModuleSP module_sp(GetSP());
420   if (name && module_sp) {
421     VariableList variable_list;
422     const uint32_t match_count = module_sp->FindGlobalVariables(
423         ConstString(name), nullptr, max_matches, variable_list);
424 
425     if (match_count > 0) {
426       for (uint32_t i = 0; i < match_count; ++i) {
427         lldb::ValueObjectSP valobj_sp;
428         TargetSP target_sp(target.GetSP());
429         valobj_sp = ValueObjectVariable::Create(
430             target_sp.get(), variable_list.GetVariableAtIndex(i));
431         if (valobj_sp)
432           sb_value_list.Append(SBValue(valobj_sp));
433       }
434     }
435   }
436 
437   return LLDB_RECORD_RESULT(sb_value_list);
438 }
439 
440 lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target,
441                                                 const char *name) {
442   LLDB_RECORD_METHOD(lldb::SBValue, SBModule, FindFirstGlobalVariable,
443                      (lldb::SBTarget &, const char *), target, name);
444 
445   SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
446   if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
447     return LLDB_RECORD_RESULT(sb_value_list.GetValueAtIndex(0));
448   return LLDB_RECORD_RESULT(SBValue());
449 }
450 
451 lldb::SBType SBModule::FindFirstType(const char *name_cstr) {
452   LLDB_RECORD_METHOD(lldb::SBType, SBModule, FindFirstType, (const char *),
453                      name_cstr);
454 
455   SBType sb_type;
456   ModuleSP module_sp(GetSP());
457   if (name_cstr && module_sp) {
458     SymbolContext sc;
459     const bool exact_match = false;
460     ConstString name(name_cstr);
461 
462     sb_type = SBType(module_sp->FindFirstType(sc, name, exact_match));
463 
464     if (!sb_type.IsValid()) {
465       auto type_system_or_err =
466           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
467       if (auto err = type_system_or_err.takeError()) {
468         llvm::consumeError(std::move(err));
469         return LLDB_RECORD_RESULT(SBType());
470       }
471       sb_type = SBType(type_system_or_err->GetBuiltinTypeByName(name));
472     }
473   }
474   return LLDB_RECORD_RESULT(sb_type);
475 }
476 
477 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {
478   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetBasicType, (lldb::BasicType),
479                      type);
480 
481   ModuleSP module_sp(GetSP());
482   if (module_sp) {
483     auto type_system_or_err =
484         module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
485     if (auto err = type_system_or_err.takeError()) {
486       llvm::consumeError(std::move(err));
487     } else {
488       return LLDB_RECORD_RESULT(
489           SBType(type_system_or_err->GetBasicTypeFromAST(type)));
490     }
491   }
492   return LLDB_RECORD_RESULT(SBType());
493 }
494 
495 lldb::SBTypeList SBModule::FindTypes(const char *type) {
496   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, FindTypes, (const char *),
497                      type);
498 
499   SBTypeList retval;
500 
501   ModuleSP module_sp(GetSP());
502   if (type && module_sp) {
503     TypeList type_list;
504     const bool exact_match = false;
505     ConstString name(type);
506     llvm::DenseSet<SymbolFile *> searched_symbol_files;
507     const uint32_t num_matches = module_sp->FindTypes(
508         name, exact_match, UINT32_MAX, searched_symbol_files, type_list);
509 
510     if (num_matches > 0) {
511       for (size_t idx = 0; idx < num_matches; idx++) {
512         TypeSP type_sp(type_list.GetTypeAtIndex(idx));
513         if (type_sp)
514           retval.Append(SBType(type_sp));
515       }
516     } else {
517       auto type_system_or_err =
518           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
519       if (auto err = type_system_or_err.takeError()) {
520         llvm::consumeError(std::move(err));
521       } else {
522         CompilerType compiler_type =
523             type_system_or_err->GetBuiltinTypeByName(name);
524         if (compiler_type)
525           retval.Append(SBType(compiler_type));
526       }
527     }
528   }
529 
530   return LLDB_RECORD_RESULT(retval);
531 }
532 
533 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {
534   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetTypeByID, (lldb::user_id_t),
535                      uid);
536 
537   ModuleSP module_sp(GetSP());
538   if (module_sp) {
539     SymbolVendor *vendor = module_sp->GetSymbolVendor();
540     if (vendor) {
541       Type *type_ptr = vendor->ResolveTypeUID(uid);
542       if (type_ptr)
543         return LLDB_RECORD_RESULT(SBType(type_ptr->shared_from_this()));
544     }
545   }
546   return LLDB_RECORD_RESULT(SBType());
547 }
548 
549 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {
550   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t),
551                      type_mask);
552 
553   SBTypeList sb_type_list;
554 
555   ModuleSP module_sp(GetSP());
556   if (!module_sp)
557     return LLDB_RECORD_RESULT(sb_type_list);
558   SymbolVendor *vendor = module_sp->GetSymbolVendor();
559   if (!vendor)
560     return LLDB_RECORD_RESULT(sb_type_list);
561 
562   TypeClass type_class = static_cast<TypeClass>(type_mask);
563   TypeList type_list;
564   vendor->GetTypes(nullptr, type_class, type_list);
565   sb_type_list.m_opaque_up->Append(type_list);
566   return LLDB_RECORD_RESULT(sb_type_list);
567 }
568 
569 SBSection SBModule::FindSection(const char *sect_name) {
570   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, FindSection, (const char *),
571                      sect_name);
572 
573   SBSection sb_section;
574 
575   ModuleSP module_sp(GetSP());
576   if (sect_name && module_sp) {
577     // Give the symbol vendor a chance to add to the unified section list.
578     module_sp->GetSymbolVendor();
579     SectionList *section_list = module_sp->GetSectionList();
580     if (section_list) {
581       ConstString const_sect_name(sect_name);
582       SectionSP section_sp(section_list->FindSectionByName(const_sect_name));
583       if (section_sp) {
584         sb_section.SetSP(section_sp);
585       }
586     }
587   }
588   return LLDB_RECORD_RESULT(sb_section);
589 }
590 
591 lldb::ByteOrder SBModule::GetByteOrder() {
592   LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBModule, GetByteOrder);
593 
594   ModuleSP module_sp(GetSP());
595   if (module_sp)
596     return module_sp->GetArchitecture().GetByteOrder();
597   return eByteOrderInvalid;
598 }
599 
600 const char *SBModule::GetTriple() {
601   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModule, GetTriple);
602 
603   ModuleSP module_sp(GetSP());
604   if (module_sp) {
605     std::string triple(module_sp->GetArchitecture().GetTriple().str());
606     // Unique the string so we don't run into ownership issues since the const
607     // strings put the string into the string pool once and the strings never
608     // comes out
609     ConstString const_triple(triple.c_str());
610     return const_triple.GetCString();
611   }
612   return nullptr;
613 }
614 
615 uint32_t SBModule::GetAddressByteSize() {
616   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetAddressByteSize);
617 
618   ModuleSP module_sp(GetSP());
619   if (module_sp)
620     return module_sp->GetArchitecture().GetAddressByteSize();
621   return sizeof(void *);
622 }
623 
624 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {
625   LLDB_RECORD_METHOD(uint32_t, SBModule, GetVersion, (uint32_t *, uint32_t),
626                      versions, num_versions);
627 
628   llvm::VersionTuple version;
629   if (ModuleSP module_sp = GetSP())
630     version = module_sp->GetVersion();
631   uint32_t result = 0;
632   if (!version.empty())
633     ++result;
634   if (version.getMinor())
635     ++result;
636   if(version.getSubminor())
637     ++result;
638 
639   if (!versions)
640     return result;
641 
642   if (num_versions > 0)
643     versions[0] = version.empty() ? UINT32_MAX : version.getMajor();
644   if (num_versions > 1)
645     versions[1] = version.getMinor().getValueOr(UINT32_MAX);
646   if (num_versions > 2)
647     versions[2] = version.getSubminor().getValueOr(UINT32_MAX);
648   for (uint32_t i = 3; i < num_versions; ++i)
649     versions[i] = UINT32_MAX;
650   return result;
651 }
652 
653 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {
654   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
655                                    GetSymbolFileSpec);
656 
657   lldb::SBFileSpec sb_file_spec;
658   ModuleSP module_sp(GetSP());
659   if (module_sp) {
660     SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor();
661     if (symbol_vendor_ptr)
662       sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec());
663   }
664   return LLDB_RECORD_RESULT(sb_file_spec);
665 }
666 
667 lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const {
668   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
669                                    GetObjectFileHeaderAddress);
670 
671   lldb::SBAddress sb_addr;
672   ModuleSP module_sp(GetSP());
673   if (module_sp) {
674     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
675     if (objfile_ptr)
676       sb_addr.ref() = objfile_ptr->GetBaseAddress();
677   }
678   return LLDB_RECORD_RESULT(sb_addr);
679 }
680 
681 lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {
682   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
683                                    GetObjectFileEntryPointAddress);
684 
685   lldb::SBAddress sb_addr;
686   ModuleSP module_sp(GetSP());
687   if (module_sp) {
688     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
689     if (objfile_ptr)
690       sb_addr.ref() = objfile_ptr->GetEntryPointAddress();
691   }
692   return LLDB_RECORD_RESULT(sb_addr);
693 }
694 
695 namespace lldb_private {
696 namespace repro {
697 
698 template <>
699 void RegisterMethods<SBModule>(Registry &R) {
700   LLDB_REGISTER_CONSTRUCTOR(SBModule, ());
701   LLDB_REGISTER_CONSTRUCTOR(SBModule, (const lldb::SBModuleSpec &));
702   LLDB_REGISTER_CONSTRUCTOR(SBModule, (const lldb::SBModule &));
703   LLDB_REGISTER_CONSTRUCTOR(SBModule, (lldb::SBProcess &, lldb::addr_t));
704   LLDB_REGISTER_METHOD(const lldb::SBModule &,
705                        SBModule, operator=,(const lldb::SBModule &));
706   LLDB_REGISTER_METHOD_CONST(bool, SBModule, IsValid, ());
707   LLDB_REGISTER_METHOD_CONST(bool, SBModule, operator bool, ());
708   LLDB_REGISTER_METHOD(void, SBModule, Clear, ());
709   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetFileSpec, ());
710   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetPlatformFileSpec,
711                              ());
712   LLDB_REGISTER_METHOD(bool, SBModule, SetPlatformFileSpec,
713                        (const lldb::SBFileSpec &));
714   LLDB_REGISTER_METHOD(lldb::SBFileSpec, SBModule, GetRemoteInstallFileSpec,
715                        ());
716   LLDB_REGISTER_METHOD(bool, SBModule, SetRemoteInstallFileSpec,
717                        (lldb::SBFileSpec &));
718   LLDB_REGISTER_METHOD_CONST(const char *, SBModule, GetUUIDString, ());
719   LLDB_REGISTER_METHOD_CONST(bool,
720                              SBModule, operator==,(const lldb::SBModule &));
721   LLDB_REGISTER_METHOD_CONST(bool,
722                              SBModule, operator!=,(const lldb::SBModule &));
723   LLDB_REGISTER_METHOD(lldb::SBAddress, SBModule, ResolveFileAddress,
724                        (lldb::addr_t));
725   LLDB_REGISTER_METHOD(lldb::SBSymbolContext, SBModule,
726                        ResolveSymbolContextForAddress,
727                        (const lldb::SBAddress &, uint32_t));
728   LLDB_REGISTER_METHOD(bool, SBModule, GetDescription, (lldb::SBStream &));
729   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetNumCompileUnits, ());
730   LLDB_REGISTER_METHOD(lldb::SBCompileUnit, SBModule, GetCompileUnitAtIndex,
731                        (uint32_t));
732   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindCompileUnits,
733                        (const lldb::SBFileSpec &));
734   LLDB_REGISTER_METHOD(size_t, SBModule, GetNumSymbols, ());
735   LLDB_REGISTER_METHOD(lldb::SBSymbol, SBModule, GetSymbolAtIndex, (size_t));
736   LLDB_REGISTER_METHOD(lldb::SBSymbol, SBModule, FindSymbol,
737                        (const char *, lldb::SymbolType));
738   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindSymbols,
739                        (const char *, lldb::SymbolType));
740   LLDB_REGISTER_METHOD(size_t, SBModule, GetNumSections, ());
741   LLDB_REGISTER_METHOD(lldb::SBSection, SBModule, GetSectionAtIndex,
742                        (size_t));
743   LLDB_REGISTER_METHOD(lldb::SBSymbolContextList, SBModule, FindFunctions,
744                        (const char *, uint32_t));
745   LLDB_REGISTER_METHOD(lldb::SBValueList, SBModule, FindGlobalVariables,
746                        (lldb::SBTarget &, const char *, uint32_t));
747   LLDB_REGISTER_METHOD(lldb::SBValue, SBModule, FindFirstGlobalVariable,
748                        (lldb::SBTarget &, const char *));
749   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, FindFirstType, (const char *));
750   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, GetBasicType,
751                        (lldb::BasicType));
752   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBModule, FindTypes, (const char *));
753   LLDB_REGISTER_METHOD(lldb::SBType, SBModule, GetTypeByID,
754                        (lldb::user_id_t));
755   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t));
756   LLDB_REGISTER_METHOD(lldb::SBSection, SBModule, FindSection,
757                        (const char *));
758   LLDB_REGISTER_METHOD(lldb::ByteOrder, SBModule, GetByteOrder, ());
759   LLDB_REGISTER_METHOD(const char *, SBModule, GetTriple, ());
760   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetAddressByteSize, ());
761   LLDB_REGISTER_METHOD(uint32_t, SBModule, GetVersion,
762                        (uint32_t *, uint32_t));
763   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBModule, GetSymbolFileSpec,
764                              ());
765   LLDB_REGISTER_METHOD_CONST(lldb::SBAddress, SBModule,
766                              GetObjectFileHeaderAddress, ());
767   LLDB_REGISTER_METHOD_CONST(lldb::SBAddress, SBModule,
768                              GetObjectFileEntryPointAddress, ());
769 }
770 
771 }
772 }
773