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 
26 
28  : m_opaque_ptr(lldb_object_ptr) {}
29 
31  : m_opaque_ptr(rhs.m_opaque_ptr) {
33 }
34 
37  SBFunction, operator=,(const lldb::SBFunction &), rhs);
38 
40  return LLDB_RECORD_RESULT(*this);
41 }
42 
44 
45 bool SBFunction::IsValid() const {
47  return this->operator bool();
48 }
49 SBFunction::operator bool() const {
50  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFunction, operator bool);
51 
52  return m_opaque_ptr != nullptr;
53 }
54 
55 const char *SBFunction::GetName() const {
57 
58  const char *cstr = nullptr;
59  if (m_opaque_ptr)
60  cstr = m_opaque_ptr->GetName().AsCString();
61 
62  return cstr;
63 }
64 
65 const char *SBFunction::GetDisplayName() const {
67 
68  const char *cstr = nullptr;
69  if (m_opaque_ptr)
70  cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
71 
72  return cstr;
73 }
74 
75 const char *SBFunction::GetMangledName() const {
77 
78  const char *cstr = nullptr;
79  if (m_opaque_ptr)
80  cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
81  return cstr;
82 }
83 
84 bool SBFunction::operator==(const SBFunction &rhs) const {
86  bool, SBFunction, operator==,(const lldb::SBFunction &), rhs);
87 
88  return m_opaque_ptr == rhs.m_opaque_ptr;
89 }
90 
91 bool SBFunction::operator!=(const SBFunction &rhs) const {
93  bool, SBFunction, operator!=,(const lldb::SBFunction &), rhs);
94 
95  return m_opaque_ptr != rhs.m_opaque_ptr;
96 }
97 
100 
101  if (m_opaque_ptr) {
102  s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
104  Type *func_type = m_opaque_ptr->GetType();
105  if (func_type)
106  s.Printf(", type = %s", func_type->GetName().AsCString());
107  return true;
108  }
109  s.Printf("No value");
110  return false;
111 }
112 
115  (lldb::SBTarget), target);
116 
117  return LLDB_RECORD_RESULT(GetInstructions(target, nullptr));
118 }
119 
121  const char *flavor) {
123  (lldb::SBTarget, const char *), target, flavor);
124 
125  SBInstructionList sb_instructions;
126  if (m_opaque_ptr) {
127  TargetSP target_sp(target.GetSP());
128  std::unique_lock<std::recursive_mutex> lock;
129  ModuleSP module_sp(
131  if (target_sp && module_sp) {
132  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
133  const bool force_live_memory = true;
134  sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
135  module_sp->GetArchitecture(), nullptr, flavor, *target_sp,
136  m_opaque_ptr->GetAddressRange(), force_live_memory));
137  }
138  }
139  return LLDB_RECORD_RESULT(sb_instructions);
140 }
141 
143 
145  m_opaque_ptr = lldb_object_ptr;
146 }
147 
150 
151  SBAddress addr;
152  if (m_opaque_ptr)
154  return LLDB_RECORD_RESULT(addr);
155 }
156 
159 
160  SBAddress addr;
161  if (m_opaque_ptr) {
163  if (byte_size > 0) {
165  addr->Slide(byte_size);
166  }
167  }
168  return LLDB_RECORD_RESULT(addr);
169 }
170 
171 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
173  arg_idx);
174 
175  if (m_opaque_ptr) {
176  Block &block = m_opaque_ptr->GetBlock(true);
177  VariableListSP variable_list_sp = block.GetBlockVariableList(true);
178  if (variable_list_sp) {
179  VariableList arguments;
181  arguments, true);
182  lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
183  if (variable_sp)
184  return variable_sp->GetName().GetCString();
185  }
186  }
187  return nullptr;
188 }
189 
192 
193  if (m_opaque_ptr)
195  return 0;
196 }
197 
200 
201  SBType sb_type;
202  if (m_opaque_ptr) {
203  Type *function_type = m_opaque_ptr->GetType();
204  if (function_type)
205  sb_type.ref().SetType(function_type->shared_from_this());
206  }
207  return LLDB_RECORD_RESULT(sb_type);
208 }
209 
212 
213  SBBlock sb_block;
214  if (m_opaque_ptr)
215  sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
216  return LLDB_RECORD_RESULT(sb_block);
217 }
218 
221 
222  if (m_opaque_ptr) {
225  }
227 }
228 
231 
232  if (m_opaque_ptr) {
235  }
236  return false;
237 }
238 
239 namespace lldb_private {
240 namespace repro {
241 
242 template <>
247  SBFunction, operator=,(const lldb::SBFunction &));
248  LLDB_REGISTER_METHOD_CONST(bool, SBFunction, IsValid, ());
249  LLDB_REGISTER_METHOD_CONST(bool, SBFunction, operator bool, ());
250  LLDB_REGISTER_METHOD_CONST(const char *, SBFunction, GetName, ());
251  LLDB_REGISTER_METHOD_CONST(const char *, SBFunction, GetDisplayName, ());
252  LLDB_REGISTER_METHOD_CONST(const char *, SBFunction, GetMangledName, ());
254  bool, SBFunction, operator==,(const lldb::SBFunction &));
256  bool, SBFunction, operator!=,(const lldb::SBFunction &));
257  LLDB_REGISTER_METHOD(bool, SBFunction, GetDescription, (lldb::SBStream &));
259  (lldb::SBTarget));
261  (lldb::SBTarget, const char *));
262  LLDB_REGISTER_METHOD(lldb::SBAddress, SBFunction, GetStartAddress, ());
263  LLDB_REGISTER_METHOD(lldb::SBAddress, SBFunction, GetEndAddress, ());
264  LLDB_REGISTER_METHOD(const char *, SBFunction, GetArgumentName, (uint32_t));
265  LLDB_REGISTER_METHOD(uint32_t, SBFunction, GetPrologueByteSize, ());
269  LLDB_REGISTER_METHOD(bool, SBFunction, GetIsOptimized, ());
270 }
271 
272 }
273 }
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
lldb_private::Block::GetBlockVariableList
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:388
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:69
lldb::SBFunction::~SBFunction
~SBFunction()
Definition: SBFunction.cpp:43
CompileUnit.h
lldb_private::Function::GetAddressRange
const AddressRange & GetAddressRange()
Definition: Function.h:461
lldb::SBFunction::GetEndAddress
lldb::SBAddress GetEndAddress()
Definition: SBFunction.cpp:157
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
lldb::SBFunction::GetMangledName
const char * GetMangledName() const
Definition: SBFunction.cpp:75
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb_private::VariableList::AppendVariablesWithScope
size_t AppendVariablesWithScope(lldb::ValueType type, VariableList &var_list, bool if_unique=true)
Definition: VariableList.cpp:128
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBFunction::GetName
const char * GetName() const
Definition: SBFunction.cpp:55
lldb::SBFunction::m_opaque_ptr
lldb_private::Function * m_opaque_ptr
Definition: SBFunction.h:77
lldb_private::AddressRange::GetByteSize
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:221
Module.h
lldb::SBFunction::SBFunction
SBFunction()
Definition: SBFunction.cpp:25
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
Disassembler.h
lldb_private::Type::GetName
ConstString GetName()
Definition: Type.cpp:301
lldb::SBBlock::SetPtr
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:163
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb::SBAddress::SetAddress
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:92
SBReproducerPrivate.h
lldb::SBFunction::GetPrologueByteSize
uint32_t GetPrologueByteSize()
Definition: SBFunction.cpp:190
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBInstructionList
Definition: SBInstructionList.h:18
lldb_private::VariableList
Definition: VariableList.h:18
lldb::SBFunction::GetIsOptimized
bool GetIsOptimized()
Definition: SBFunction.cpp:229
lldb_private::VariableList::GetVariableAtIndex
lldb::VariableSP GetVariableAtIndex(size_t idx) const
Definition: VariableList.cpp:47
Target.h
R
#define R(base)
lldb::SBTarget
Definition: SBTarget.h:29
SBStream.h
lldb_private::Function
Definition: Function.h:413
lldb_private::Function::GetCompileUnit
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition: Function.cpp:362
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::Function::GetName
ConstString GetName() const
Definition: Function.cpp:655
lldb_private::Function::GetBlock
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:344
lldb::SBFunction::GetStartAddress
lldb::SBAddress GetStartAddress()
Definition: SBFunction.cpp:148
GetName
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition: XcodeSDK.cpp:21
Type.h
SBFunction.h
lldb::SBBlock
Definition: SBBlock.h:19
lldb::SBFunction
Definition: SBFunction.h:18
lldb::SBFunction::reset
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:144
lldb_private::CompileUnit::GetIsOptimized
bool GetIsOptimized()
Get whether compiler optimizations were enabled for this compile unit.
Definition: CompileUnit.cpp:351
lldb::eValueTypeVariableArgument
@ eValueTypeVariableArgument
function argument variables
Definition: lldb-enumerations.h:319
lldb::SBFunction::GetArgumentName
const char * GetArgumentName(uint32_t arg_idx)
Definition: SBFunction.cpp:171
lldb_private::CompileUnit::GetLanguage
lldb::LanguageType GetLanguage()
Definition: CompileUnit.cpp:150
lldb::SBFunction::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBFunction.cpp:98
VariableList.h
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
lldb::SBType
Definition: SBType.h:104
lldb::SBType::ref
lldb_private::TypeImpl & ref()
Definition: SBType.cpp:95
lldb::SBFunction::IsValid
bool IsValid() const
Definition: SBFunction.cpp:45
lldb::SBFunction::GetLanguage
lldb::LanguageType GetLanguage()
Definition: SBFunction.cpp:219
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb_private::Block
Definition: Block.h:41
lldb::eLanguageTypeUnknown
@ eLanguageTypeUnknown
Unknown or invalid language value.
Definition: lldb-enumerations.h:437
uint32_t
lldb::SBFunction::GetInstructions
lldb::SBInstructionList GetInstructions(lldb::SBTarget target)
Definition: SBFunction.cpp:113
lldb_private::TypeImpl::SetType
void SetType(const lldb::TypeSP &type_sp)
Definition: Type.cpp:828
lldb_private::Function::GetPrologueByteSize
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:529
lldb_private::Type
Definition: Type.h:66
lldb::SBInstructionList::SetDisassembler
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp)
Definition: SBInstructionList.cpp:117
lldb_private::repro::RegisterMethods< SBFunction >
void RegisterMethods< SBFunction >(Registry &R)
Definition: SBFunction.cpp:243
lldb::SBFunction::operator!=
bool operator!=(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:91
lldb::SBFunction::operator=
const lldb::SBFunction & operator=(const lldb::SBFunction &rhs)
Definition: SBFunction.cpp:35
Function.h
lldb_private::Function::GetType
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types.
Definition: Function.cpp:501
lldb::SBAddress
Definition: SBAddress.h:17
lldb_private::Function::GetMangled
const Mangled & GetMangled() const
Definition: Function.h:542
lldb::SBFunction::operator==
bool operator==(const lldb::SBFunction &rhs) const
Definition: SBFunction.cpp:84
lldb::SBFunction::get
lldb_private::Function * get()
Definition: SBFunction.cpp:142
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBFunction::GetBlock
lldb::SBBlock GetBlock()
Definition: SBFunction.cpp:210
lldb::SBTarget::GetSP
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:613
SBProcess.h
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:282
lldb::SBStream
Definition: SBStream.h:18
LLDB_RECORD_METHOD_CONST
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:161
lldb::SBFunction::GetType
lldb::SBType GetType()
Definition: SBFunction.cpp:198
lldb::SBFunction::GetDisplayName
const char * GetDisplayName() const
Definition: SBFunction.cpp:65
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:440
lldb
Definition: SBAddress.h:15
ExecutionContext.h
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93