LLDB  mainline
SBFunction.cpp
Go to the documentation of this file.
1 //===-- SBFunction.cpp ----------------------------------------------------===//
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/SBFunction.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBProcess.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Core/Disassembler.h"
14 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/Type.h"
20 #include "lldb/Target/Target.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 SBFunction::SBFunction() : m_opaque_ptr(nullptr) {
27 }
28 
30  : m_opaque_ptr(lldb_object_ptr) {}
31 
33  : m_opaque_ptr(rhs.m_opaque_ptr) {
35 }
36 
39  SBFunction, operator=,(const lldb::SBFunction &), rhs);
40 
42  return LLDB_RECORD_RESULT(*this);
43 }
44 
46 
47 bool SBFunction::IsValid() const {
49  return this->operator bool();
50 }
51 SBFunction::operator bool() const {
52  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFunction, operator bool);
53 
54  return m_opaque_ptr != nullptr;
55 }
56 
57 const char *SBFunction::GetName() const {
59 
60  const char *cstr = nullptr;
61  if (m_opaque_ptr)
62  cstr = m_opaque_ptr->GetName().AsCString();
63 
64  return cstr;
65 }
66 
67 const char *SBFunction::GetDisplayName() const {
69 
70  const char *cstr = nullptr;
71  if (m_opaque_ptr)
72  cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
73 
74  return cstr;
75 }
76 
77 const char *SBFunction::GetMangledName() const {
79 
80  const char *cstr = nullptr;
81  if (m_opaque_ptr)
82  cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
83  return cstr;
84 }
85 
86 bool SBFunction::operator==(const SBFunction &rhs) const {
88  bool, SBFunction, operator==,(const lldb::SBFunction &), rhs);
89 
90  return m_opaque_ptr == rhs.m_opaque_ptr;
91 }
92 
93 bool SBFunction::operator!=(const SBFunction &rhs) const {
95  bool, SBFunction, operator!=,(const lldb::SBFunction &), rhs);
96 
97  return m_opaque_ptr != rhs.m_opaque_ptr;
98 }
99 
102 
103  if (m_opaque_ptr) {
104  s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
106  Type *func_type = m_opaque_ptr->GetType();
107  if (func_type)
108  s.Printf(", type = %s", func_type->GetName().AsCString());
109  return true;
110  }
111  s.Printf("No value");
112  return false;
113 }
114 
117  (lldb::SBTarget), target);
118 
119  return LLDB_RECORD_RESULT(GetInstructions(target, nullptr));
120 }
121 
123  const char *flavor) {
125  (lldb::SBTarget, const char *), target, flavor);
126 
127  SBInstructionList sb_instructions;
128  if (m_opaque_ptr) {
129  TargetSP target_sp(target.GetSP());
130  std::unique_lock<std::recursive_mutex> lock;
131  ModuleSP module_sp(
133  if (target_sp && module_sp) {
134  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
135  const bool prefer_file_cache = false;
136  sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
137  module_sp->GetArchitecture(), nullptr, flavor, *target_sp,
138  m_opaque_ptr->GetAddressRange(), prefer_file_cache));
139  }
140  }
141  return LLDB_RECORD_RESULT(sb_instructions);
142 }
143 
145 
147  m_opaque_ptr = lldb_object_ptr;
148 }
149 
152 
153  SBAddress addr;
154  if (m_opaque_ptr)
156  return LLDB_RECORD_RESULT(addr);
157 }
158 
161 
162  SBAddress addr;
163  if (m_opaque_ptr) {
165  if (byte_size > 0) {
167  addr->Slide(byte_size);
168  }
169  }
170  return LLDB_RECORD_RESULT(addr);
171 }
172 
173 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
175  arg_idx);
176 
177  if (m_opaque_ptr) {
178  Block &block = m_opaque_ptr->GetBlock(true);
179  VariableListSP variable_list_sp = block.GetBlockVariableList(true);
180  if (variable_list_sp) {
181  VariableList arguments;
183  arguments, true);
184  lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
185  if (variable_sp)
186  return variable_sp->GetName().GetCString();
187  }
188  }
189  return nullptr;
190 }
191 
194 
195  if (m_opaque_ptr)
197  return 0;
198 }
199 
202 
203  SBType sb_type;
204  if (m_opaque_ptr) {
205  Type *function_type = m_opaque_ptr->GetType();
206  if (function_type)
207  sb_type.ref().SetType(function_type->shared_from_this());
208  }
209  return LLDB_RECORD_RESULT(sb_type);
210 }
211 
214 
215  SBBlock sb_block;
216  if (m_opaque_ptr)
217  sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
218  return LLDB_RECORD_RESULT(sb_block);
219 }
220 
223 
224  if (m_opaque_ptr) {
227  }
229 }
230 
233 
234  if (m_opaque_ptr) {
237  }
238  return false;
239 }
240 
241 namespace lldb_private {
242 namespace repro {
243 
244 template <>
249  SBFunction, operator=,(const lldb::SBFunction &));
251  LLDB_REGISTER_METHOD_CONST(bool, SBFunction, operator bool, ());
252  LLDB_REGISTER_METHOD_CONST(const char *, SBFunction, GetName, ());
256  bool, SBFunction, operator==,(const lldb::SBFunction &));
258  bool, SBFunction, operator!=,(const lldb::SBFunction &));
261  (lldb::SBTarget));
263  (lldb::SBTarget, const char *));
272 }
273 
274 }
275 }
size_t AppendVariablesWithScope(lldb::ValueType type, VariableList &var_list, bool if_unique=true)
The registry contains a unique mapping between functions and their ID.
A class that represents a running process on the host machine.
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
lldb_private::TypeImpl & ref()
Definition: SBType.cpp:94
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:222
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:223
lldb_private::Function * m_opaque_ptr
Definition: SBFunction.h:77
lldb::SBType GetType()
Definition: SBFunction.cpp:200
lldb::SBBlock GetBlock()
Definition: SBFunction.cpp:212
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:165
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
A class that describes a function.
Definition: Function.h:409
lldb::LanguageType GetLanguage()
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:95
const char * GetMangledName() const
Definition: SBFunction.cpp:77
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:388
lldb::SBAddress GetStartAddress()
Definition: SBFunction.cpp:150
A class that describes a single lexical block.
Definition: Block.h:41
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types...
Definition: Function.cpp:500
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:528
lldb::SBAddress GetEndAddress()
Definition: SBFunction.cpp:159
lldb::VariableSP GetVariableAtIndex(size_t idx) const
ConstString GetName()
Definition: Type.cpp:303
bool Slide(int64_t offset)
Definition: Address.h:436
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:608
LanguageType
Programming language type.
bool operator!=(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:93
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
uint32_t GetPrologueByteSize()
Definition: SBFunction.cpp:192
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:69
void RegisterMethods< SBFunction >(Registry &R)
Definition: SBFunction.cpp:245
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:342
ConstString GetName() const
Definition: Function.cpp:654
bool operator==(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:86
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition: Function.cpp:360
bool GetIsOptimized()
Get whether compiler optimizations were enabled for this compile unit.
lldb_private::Function * get()
Definition: SBFunction.cpp:144
const char * GetDisplayName() const
Definition: SBFunction.cpp:67
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp)
const lldb::SBFunction & operator=(const lldb::SBFunction &rhs)
Definition: SBFunction.cpp:37
function argument variables
const Mangled & GetMangled() const
Definition: Function.h:538
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
const char * GetArgumentName(uint32_t arg_idx)
Definition: SBFunction.cpp:173
bool GetDescription(lldb::SBStream &description)
Definition: SBFunction.cpp:100
uint64_t addr_t
Definition: lldb-types.h:83
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:282
Unknown or invalid language value.
void SetType(const lldb::TypeSP &type_sp)
Definition: Type.cpp:816
Definition: SBAddress.h:15
lldb::SBInstructionList GetInstructions(lldb::SBTarget target)
Definition: SBFunction.cpp:115
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:210
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
const AddressRange & GetAddressRange()
Definition: Function.h:457
lldb::LanguageType GetLanguage()
Definition: SBFunction.cpp:221
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:146
const char * GetName() const
Definition: SBFunction.cpp:57
bool IsValid() const
Definition: SBFunction.cpp:47
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)