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