]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/API/SBModule.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / API / SBModule.cpp
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       TypeSystem *type_system =
472           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
473       if (type_system)
474         sb_type = SBType(type_system->GetBuiltinTypeByName(name));
475     }
476   }
477   return LLDB_RECORD_RESULT(sb_type);
478 }
479
480 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {
481   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetBasicType, (lldb::BasicType),
482                      type);
483
484   ModuleSP module_sp(GetSP());
485   if (module_sp) {
486     TypeSystem *type_system =
487         module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
488     if (type_system)
489       return LLDB_RECORD_RESULT(SBType(type_system->GetBasicTypeFromAST(type)));
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     const uint32_t num_matches = module_sp->FindTypes(
507         name, exact_match, UINT32_MAX, searched_symbol_files, type_list);
508
509     if (num_matches > 0) {
510       for (size_t idx = 0; idx < num_matches; idx++) {
511         TypeSP type_sp(type_list.GetTypeAtIndex(idx));
512         if (type_sp)
513           retval.Append(SBType(type_sp));
514       }
515     } else {
516       TypeSystem *type_system =
517           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
518       if (type_system) {
519         CompilerType compiler_type = type_system->GetBuiltinTypeByName(name);
520         if (compiler_type)
521           retval.Append(SBType(compiler_type));
522       }
523     }
524   }
525
526   return LLDB_RECORD_RESULT(retval);
527 }
528
529 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {
530   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetTypeByID, (lldb::user_id_t),
531                      uid);
532
533   ModuleSP module_sp(GetSP());
534   if (module_sp) {
535     SymbolVendor *vendor = module_sp->GetSymbolVendor();
536     if (vendor) {
537       Type *type_ptr = vendor->ResolveTypeUID(uid);
538       if (type_ptr)
539         return LLDB_RECORD_RESULT(SBType(type_ptr->shared_from_this()));
540     }
541   }
542   return LLDB_RECORD_RESULT(SBType());
543 }
544
545 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {
546   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t),
547                      type_mask);
548
549   SBTypeList sb_type_list;
550
551   ModuleSP module_sp(GetSP());
552   if (!module_sp)
553     return LLDB_RECORD_RESULT(sb_type_list);
554   SymbolVendor *vendor = module_sp->GetSymbolVendor();
555   if (!vendor)
556     return LLDB_RECORD_RESULT(sb_type_list);
557
558   TypeClass type_class = static_cast<TypeClass>(type_mask);
559   TypeList type_list;
560   vendor->GetTypes(nullptr, type_class, type_list);
561   sb_type_list.m_opaque_up->Append(type_list);
562   return LLDB_RECORD_RESULT(sb_type_list);
563 }
564
565 SBSection SBModule::FindSection(const char *sect_name) {
566   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, FindSection, (const char *),
567                      sect_name);
568
569   SBSection sb_section;
570
571   ModuleSP module_sp(GetSP());
572   if (sect_name && module_sp) {
573     // Give the symbol vendor a chance to add to the unified section list.
574     module_sp->GetSymbolVendor();
575     SectionList *section_list = module_sp->GetSectionList();
576     if (section_list) {
577       ConstString const_sect_name(sect_name);
578       SectionSP section_sp(section_list->FindSectionByName(const_sect_name));
579       if (section_sp) {
580         sb_section.SetSP(section_sp);
581       }
582     }
583   }
584   return LLDB_RECORD_RESULT(sb_section);
585 }
586
587 lldb::ByteOrder SBModule::GetByteOrder() {
588   LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBModule, GetByteOrder);
589
590   ModuleSP module_sp(GetSP());
591   if (module_sp)
592     return module_sp->GetArchitecture().GetByteOrder();
593   return eByteOrderInvalid;
594 }
595
596 const char *SBModule::GetTriple() {
597   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModule, GetTriple);
598
599   ModuleSP module_sp(GetSP());
600   if (module_sp) {
601     std::string triple(module_sp->GetArchitecture().GetTriple().str());
602     // Unique the string so we don't run into ownership issues since the const
603     // strings put the string into the string pool once and the strings never
604     // comes out
605     ConstString const_triple(triple.c_str());
606     return const_triple.GetCString();
607   }
608   return nullptr;
609 }
610
611 uint32_t SBModule::GetAddressByteSize() {
612   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetAddressByteSize);
613
614   ModuleSP module_sp(GetSP());
615   if (module_sp)
616     return module_sp->GetArchitecture().GetAddressByteSize();
617   return sizeof(void *);
618 }
619
620 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {
621   LLDB_RECORD_METHOD(uint32_t, SBModule, GetVersion, (uint32_t *, uint32_t),
622                      versions, num_versions);
623
624   llvm::VersionTuple version;
625   if (ModuleSP module_sp = GetSP())
626     version = module_sp->GetVersion();
627   uint32_t result = 0;
628   if (!version.empty())
629     ++result;
630   if (version.getMinor())
631     ++result;
632   if(version.getSubminor())
633     ++result;
634
635   if (!versions)
636     return result;
637
638   if (num_versions > 0)
639     versions[0] = version.empty() ? UINT32_MAX : version.getMajor();
640   if (num_versions > 1)
641     versions[1] = version.getMinor().getValueOr(UINT32_MAX);
642   if (num_versions > 2)
643     versions[2] = version.getSubminor().getValueOr(UINT32_MAX);
644   for (uint32_t i = 3; i < num_versions; ++i)
645     versions[i] = UINT32_MAX;
646   return result;
647 }
648
649 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {
650   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
651                                    GetSymbolFileSpec);
652
653   lldb::SBFileSpec sb_file_spec;
654   ModuleSP module_sp(GetSP());
655   if (module_sp) {
656     SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor();
657     if (symbol_vendor_ptr)
658       sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec());
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 }