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