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