LLDB  mainline
SBFunction.cpp
Go to the documentation of this file.
1 //===-- SBFunction.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/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(NULL) {
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 
41  m_opaque_ptr = rhs.m_opaque_ptr;
42  return LLDB_RECORD_RESULT(*this);
43 }
44 
45 SBFunction::~SBFunction() { m_opaque_ptr = NULL; }
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 != NULL;
55 }
56 
57 const char *SBFunction::GetName() const {
59 
60  const char *cstr = NULL;
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 = NULL;
71  if (m_opaque_ptr)
72  cstr = m_opaque_ptr->GetMangled()
73  .GetDisplayDemangledName(m_opaque_ptr->GetLanguage())
74  .AsCString();
75 
76  return cstr;
77 }
78 
79 const char *SBFunction::GetMangledName() const {
81 
82  const char *cstr = NULL;
83  if (m_opaque_ptr)
84  cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
85  return cstr;
86 }
87 
88 bool SBFunction::operator==(const SBFunction &rhs) const {
90  bool, SBFunction, operator==,(const lldb::SBFunction &), rhs);
91 
92  return m_opaque_ptr == rhs.m_opaque_ptr;
93 }
94 
95 bool SBFunction::operator!=(const SBFunction &rhs) const {
97  bool, SBFunction, operator!=,(const lldb::SBFunction &), rhs);
98 
99  return m_opaque_ptr != rhs.m_opaque_ptr;
100 }
101 
104 
105  if (m_opaque_ptr) {
106  s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
107  m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString());
108  Type *func_type = m_opaque_ptr->GetType();
109  if (func_type)
110  s.Printf(", type = %s", func_type->GetName().AsCString());
111  return true;
112  }
113  s.Printf("No value");
114  return false;
115 }
116 
119  (lldb::SBTarget), target);
120 
121  return LLDB_RECORD_RESULT(GetInstructions(target, NULL));
122 }
123 
125  const char *flavor) {
127  (lldb::SBTarget, const char *), target, flavor);
128 
129  SBInstructionList sb_instructions;
130  if (m_opaque_ptr) {
131  ExecutionContext exe_ctx;
132  TargetSP target_sp(target.GetSP());
133  std::unique_lock<std::recursive_mutex> lock;
134  if (target_sp) {
135  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
136  target_sp->CalculateExecutionContext(exe_ctx);
137  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
138  }
139  ModuleSP module_sp(
140  m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
141  if (module_sp) {
142  const bool prefer_file_cache = false;
143  sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
144  module_sp->GetArchitecture(), NULL, flavor, exe_ctx,
145  m_opaque_ptr->GetAddressRange(), prefer_file_cache));
146  }
147  }
148  return LLDB_RECORD_RESULT(sb_instructions);
149 }
150 
151 lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }
152 
154  m_opaque_ptr = lldb_object_ptr;
155 }
156 
159 
160  SBAddress addr;
161  if (m_opaque_ptr)
162  addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
163  return LLDB_RECORD_RESULT(addr);
164 }
165 
168 
169  SBAddress addr;
170  if (m_opaque_ptr) {
171  addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
172  if (byte_size > 0) {
173  addr.SetAddress(&m_opaque_ptr->GetAddressRange().GetBaseAddress());
174  addr->Slide(byte_size);
175  }
176  }
177  return LLDB_RECORD_RESULT(addr);
178 }
179 
180 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
182  arg_idx);
183 
184  if (m_opaque_ptr) {
185  Block &block = m_opaque_ptr->GetBlock(true);
186  VariableListSP variable_list_sp = block.GetBlockVariableList(true);
187  if (variable_list_sp) {
188  VariableList arguments;
190  arguments, true);
191  lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
192  if (variable_sp)
193  return variable_sp->GetName().GetCString();
194  }
195  }
196  return nullptr;
197 }
198 
201 
202  if (m_opaque_ptr)
203  return m_opaque_ptr->GetPrologueByteSize();
204  return 0;
205 }
206 
209 
210  SBType sb_type;
211  if (m_opaque_ptr) {
212  Type *function_type = m_opaque_ptr->GetType();
213  if (function_type)
214  sb_type.ref().SetType(function_type->shared_from_this());
215  }
216  return LLDB_RECORD_RESULT(sb_type);
217 }
218 
221 
222  SBBlock sb_block;
223  if (m_opaque_ptr)
224  sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
225  return LLDB_RECORD_RESULT(sb_block);
226 }
227 
230 
231  if (m_opaque_ptr) {
232  if (m_opaque_ptr->GetCompileUnit())
233  return m_opaque_ptr->GetCompileUnit()->GetLanguage();
234  }
236 }
237 
240 
241  if (m_opaque_ptr) {
242  if (m_opaque_ptr->GetCompileUnit())
243  return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
244  }
245  return false;
246 }
247 
248 namespace lldb_private {
249 namespace repro {
250 
251 template <>
256  SBFunction, operator=,(const lldb::SBFunction &));
258  LLDB_REGISTER_METHOD_CONST(bool, SBFunction, operator bool, ());
259  LLDB_REGISTER_METHOD_CONST(const char *, SBFunction, GetName, ());
263  bool, SBFunction, operator==,(const lldb::SBFunction &));
265  bool, SBFunction, operator!=,(const lldb::SBFunction &));
268  (lldb::SBTarget));
270  (lldb::SBTarget, const char *));
279 }
280 
281 }
282 }
size_t AppendVariablesWithScope(lldb::ValueType type, VariableList &var_list, bool if_unique=true)
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#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:232
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
lldb::SBType GetType()
Definition: SBFunction.cpp:207
lldb::SBBlock GetBlock()
Definition: SBFunction.cpp:219
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
A class that describes a function.
Definition: Function.h:323
lldb::LanguageType GetLanguage()
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:95
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const char * GetMangledName() const
Definition: SBFunction.cpp:79
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:387
lldb::SBAddress GetStartAddress()
Definition: SBFunction.cpp:157
A class that describes a single lexical block.
Definition: Block.h:41
lldb::LanguageType GetLanguage() const
Definition: Function.cpp:591
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types...
Definition: Function.cpp:443
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:476
lldb::SBAddress GetEndAddress()
Definition: SBFunction.cpp:166
lldb::VariableSP GetVariableAtIndex(size_t idx) const
ConstString GetName()
Definition: Type.cpp:275
bool Slide(int64_t offset)
Definition: Address.h:430
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:600
LanguageType
Programming language type.
bool operator!=(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:95
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
uint32_t GetPrologueByteSize()
Definition: SBFunction.cpp:199
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:62
void RegisterMethods< SBFunction >(Registry &R)
Definition: SBFunction.cpp:252
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:280
ConstString GetName() const
Definition: Function.cpp:598
bool operator==(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:88
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition: Function.cpp:297
bool GetIsOptimized()
Get whether compiler optimizations were enabled for this compile unit.
lldb_private::Function * get()
Definition: SBFunction.cpp:151
const char * GetDisplayName() const
Definition: SBFunction.cpp:67
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp)
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor to set only the process shared pointer.
const lldb::SBFunction & operator=(const lldb::SBFunction &rhs)
Definition: SBFunction.cpp:37
const Mangled & GetMangled() const
Definition: Function.h:447
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
const char * GetArgumentName(uint32_t arg_idx)
Definition: SBFunction.cpp:180
bool GetDescription(lldb::SBStream &description)
Definition: SBFunction.cpp:102
uint64_t addr_t
Definition: lldb-types.h:83
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:264
Unknown or invalid language value.
void SetType(const lldb::TypeSP &type_sp)
Definition: Type.cpp:790
Definition: SBAddress.h:15
lldb::SBInstructionList GetInstructions(lldb::SBTarget target)
Definition: SBFunction.cpp:117
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
const AddressRange & GetAddressRange()
Definition: Function.h:371
lldb::LanguageType GetLanguage()
Definition: SBFunction.cpp:228
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:153
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)