LLDB  mainline
SBBlock.cpp
Go to the documentation of this file.
1 //===-- SBBlock.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/SBBlock.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBFrame.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/API/SBValue.h"
16 #include "lldb/Core/AddressRange.h"
18 #include "lldb/Symbol/Block.h"
19 #include "lldb/Symbol/Function.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
29 
31  : m_opaque_ptr(lldb_object_ptr) {}
32 
33 SBBlock::SBBlock(const SBBlock &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
35 }
36 
37 const SBBlock &SBBlock::operator=(const SBBlock &rhs) {
39  SBBlock, operator=,(const lldb::SBBlock &), rhs);
40 
42  return LLDB_RECORD_RESULT(*this);
43 }
44 
46 
47 bool SBBlock::IsValid() const {
49  return this->operator bool();
50 }
51 SBBlock::operator bool() const {
52  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBBlock, operator bool);
53 
54  return m_opaque_ptr != nullptr;
55 }
56 
57 bool SBBlock::IsInlined() const {
59 
60  if (m_opaque_ptr)
61  return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
62  return false;
63 }
64 
65 const char *SBBlock::GetInlinedName() const {
67 
68  if (m_opaque_ptr) {
69  const InlineFunctionInfo *inlined_info =
71  if (inlined_info) {
72  return inlined_info->GetName().AsCString(nullptr);
73  }
74  }
75  return nullptr;
76 }
77 
81 
82  SBFileSpec sb_file;
83  if (m_opaque_ptr) {
84  const InlineFunctionInfo *inlined_info =
86  if (inlined_info)
87  sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
88  }
89  return LLDB_RECORD_RESULT(sb_file);
90 }
91 
94 
95  if (m_opaque_ptr) {
96  const InlineFunctionInfo *inlined_info =
98  if (inlined_info)
99  return inlined_info->GetCallSite().GetLine();
100  }
101  return 0;
102 }
103 
106 
107  if (m_opaque_ptr) {
108  const InlineFunctionInfo *inlined_info =
110  if (inlined_info)
111  return inlined_info->GetCallSite().GetColumn();
112  }
113  return 0;
114 }
115 
116 void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
117  lldb_private::VariableList *var_list) {
118  if (IsValid()) {
119  bool show_inline = true;
120  m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
121  [](Variable *) { return true; }, var_list);
122  }
123 }
124 
127 
128  SBBlock sb_block;
129  if (m_opaque_ptr)
130  sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
131  return LLDB_RECORD_RESULT(sb_block);
132 }
133 
136 
137  SBBlock sb_block;
138  if (m_opaque_ptr)
140  return LLDB_RECORD_RESULT(sb_block);
141 }
142 
145 
146  SBBlock sb_block;
147  if (m_opaque_ptr)
148  sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
149  return LLDB_RECORD_RESULT(sb_block);
150 }
151 
154 
155  SBBlock sb_block;
156  if (m_opaque_ptr)
158  return LLDB_RECORD_RESULT(sb_block);
159 }
160 
162 
164 
165 bool SBBlock::GetDescription(SBStream &description) {
167  description);
168 
169  Stream &strm = description.ref();
170 
171  if (m_opaque_ptr) {
173  strm.Printf("Block: {id: %" PRIu64 "} ", id);
174  if (IsInlined()) {
175  strm.Printf(" (inlined, '%s') ", GetInlinedName());
176  }
179  if (sc.function) {
181  &strm,
183  }
184  } else
185  strm.PutCString("No value");
186 
187  return true;
188 }
189 
192 
193  if (m_opaque_ptr)
194  return m_opaque_ptr->GetNumRanges();
195  return 0;
196 }
197 
200  idx);
201 
202  lldb::SBAddress sb_addr;
203  if (m_opaque_ptr) {
204  AddressRange range;
205  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
206  sb_addr.ref() = range.GetBaseAddress();
207  }
208  }
209  return LLDB_RECORD_RESULT(sb_addr);
210 }
211 
214  idx);
215 
216  lldb::SBAddress sb_addr;
217  if (m_opaque_ptr) {
218  AddressRange range;
219  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
220  sb_addr.ref() = range.GetBaseAddress();
221  sb_addr.ref().Slide(range.GetByteSize());
222  }
223  }
224  return LLDB_RECORD_RESULT(sb_addr);
225 }
226 
229  (lldb::SBAddress), block_addr);
230 
231  if (m_opaque_ptr && block_addr.IsValid()) {
232  return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
233  }
234 
235  return UINT32_MAX;
236 }
237 
239  bool locals, bool statics,
240  lldb::DynamicValueType use_dynamic) {
243  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType), frame,
244  arguments, locals, statics, use_dynamic);
245 
246  Block *block = GetPtr();
247  SBValueList value_list;
248  if (block) {
249  StackFrameSP frame_sp(frame.GetFrameSP());
250  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
251 
252  if (variable_list_sp) {
253  const size_t num_variables = variable_list_sp->GetSize();
254  if (num_variables) {
255  for (size_t i = 0; i < num_variables; ++i) {
256  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
257  if (variable_sp) {
258  bool add_variable = false;
259  switch (variable_sp->GetScope()) {
263  add_variable = statics;
264  break;
265 
267  add_variable = arguments;
268  break;
269 
271  add_variable = locals;
272  break;
273 
274  default:
275  break;
276  }
277  if (add_variable) {
278  if (frame_sp) {
279  lldb::ValueObjectSP valobj_sp(
280  frame_sp->GetValueObjectForFrameVariable(variable_sp,
282  SBValue value_sb;
283  value_sb.SetSP(valobj_sp, use_dynamic);
284  value_list.Append(value_sb);
285  }
286  }
287  }
288  }
289  }
290  }
291  }
292  return LLDB_RECORD_RESULT(value_list);
293 }
294 
296  bool locals, bool statics) {
298  (lldb::SBTarget &, bool, bool, bool), target, arguments,
299  locals, statics);
300 
301  Block *block = GetPtr();
302 
303  SBValueList value_list;
304  if (block) {
305  TargetSP target_sp(target.GetSP());
306 
307  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
308 
309  if (variable_list_sp) {
310  const size_t num_variables = variable_list_sp->GetSize();
311  if (num_variables) {
312  for (size_t i = 0; i < num_variables; ++i) {
313  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
314  if (variable_sp) {
315  bool add_variable = false;
316  switch (variable_sp->GetScope()) {
320  add_variable = statics;
321  break;
322 
324  add_variable = arguments;
325  break;
326 
328  add_variable = locals;
329  break;
330 
331  default:
332  break;
333  }
334  if (add_variable) {
335  if (target_sp)
336  value_list.Append(
337  ValueObjectVariable::Create(target_sp.get(), variable_sp));
338  }
339  }
340  }
341  }
342  }
343  }
344  return LLDB_RECORD_RESULT(value_list);
345 }
346 
347 namespace lldb_private {
348 namespace repro {
349 
350 template <>
355  SBBlock, operator=,(const lldb::SBBlock &));
356  LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsValid, ());
357  LLDB_REGISTER_METHOD_CONST(bool, SBBlock, operator bool, ());
358  LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsInlined, ());
359  LLDB_REGISTER_METHOD_CONST(const char *, SBBlock, GetInlinedName, ());
361  GetInlinedCallSiteFile, ());
362  LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteLine, ());
363  LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteColumn, ());
364  LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetParent, ());
365  LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetContainingInlinedBlock, ());
366  LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetSibling, ());
367  LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetFirstChild, ());
368  LLDB_REGISTER_METHOD(bool, SBBlock, GetDescription, (lldb::SBStream &));
369  LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetNumRanges, ());
370  LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeStartAddress,
371  (uint32_t));
372  LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeEndAddress,
373  (uint32_t));
374  LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetRangeIndexForBlockAddress,
375  (lldb::SBAddress));
377  lldb::SBValueList, SBBlock, GetVariables,
378  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType));
380  (lldb::SBTarget &, bool, bool, bool));
381 }
382 
383 }
384 }
lldb::SBValueList
Definition: SBValueList.h:18
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
Block.h
lldb::SBBlock::IsValid
bool IsValid() const
Definition: SBBlock.cpp:47
lldb::SBBlock::GetNumRanges
uint32_t GetNumRanges()
Definition: SBBlock.cpp:190
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
lldb::eValueTypeVariableGlobal
@ eValueTypeVariableGlobal
globals variable
Definition: lldb-enumerations.h:317
lldb_private::Block::GetBlockVariableList
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:388
lldb::SBFrame::GetFrameSP
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:84
lldb_private::InlineFunctionInfo
Definition: Function.h:127
lldb_private::Function::GetAddressRange
const AddressRange & GetAddressRange()
Definition: Function.h:461
lldb::SBBlock::m_opaque_ptr
lldb_private::Block * m_opaque_ptr
Definition: SBBlock.h:90
lldb::eNoDynamicValues
@ eNoDynamicValues
Definition: lldb-enumerations.h:495
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
lldb_private::Block::GetSibling
Block * GetSibling() const
Get the sibling block for this block.
Definition: Block.cpp:494
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBBlock::GetRangeIndexForBlockAddress
uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr)
Definition: SBBlock.cpp:227
lldb_private::Block::GetParent
Block * GetParent() const
Get the parent block.
Definition: Block.cpp:191
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBValue
Definition: SBValue.h:21
lldb_private::AddressRange::GetByteSize
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:221
lldb::SBBlock::GetRangeEndAddress
lldb::SBAddress GetRangeEndAddress(uint32_t idx)
Definition: SBBlock.cpp:212
lldb_private::InlineFunctionInfo::GetCallSite
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition: Function.cpp:106
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_private::Declaration::GetFile
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:107
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
StackFrame.h
lldb_private::Declaration::GetColumn
uint16_t GetColumn() const
Get accessor for the declaration column number.
Definition: Declaration.h:127
lldb_private::Stream
Definition: Stream.h:28
lldb::SBBlock::SetPtr
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:163
SBReproducerPrivate.h
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb::SBFileSpec::SetFileSpec
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:170
lldb_private::VariableList
Definition: VariableList.h:18
lldb::SBBlock::IsInlined
bool IsInlined() const
Definition: SBBlock.cpp:57
Target.h
lldb::SBTarget
Definition: SBTarget.h:29
lldb::SBBlock::AppendVariables
void AppendVariables(bool can_create, bool get_parent_variables, lldb_private::VariableList *var_list)
Definition: SBBlock.cpp:116
SBStream.h
lldb::SBFrame
Definition: SBFrame.h:17
lldb::SBBlock::GetPtr
lldb_private::Block * GetPtr()
Definition: SBBlock.cpp:161
SBBlock.h
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb::SBBlock::GetVariables
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition: SBBlock.cpp:238
SBFileSpec.h
SBFrame.h
lldb::SBBlock::GetInlinedCallSiteLine
uint32_t GetInlinedCallSiteLine() const
Definition: SBBlock.cpp:92
lldb_private::SymbolContext::function
Function * function
The Function for a given query.
Definition: SymbolContext.h:320
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
lldb::SBBlock::operator=
const lldb::SBBlock & operator=(const lldb::SBBlock &rhs)
Definition: SBBlock.cpp:37
lldb::SBBlock
Definition: SBBlock.h:19
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb::SBValueList::Append
void Append(const lldb::SBValue &val_obj)
Definition: SBValueList.cpp:127
lldb::SBAddress::IsValid
bool IsValid() const
Definition: SBAddress.cpp:76
lldb::SBBlock::GetParent
lldb::SBBlock GetParent()
Definition: SBBlock.cpp:125
lldb_private::InlineFunctionInfo::GetName
ConstString GetName() const
Definition: Function.cpp:94
lldb::DynamicValueType
DynamicValueType
Definition: lldb-enumerations.h:494
lldb::eValueTypeVariableArgument
@ eValueTypeVariableArgument
function argument variables
Definition: lldb-enumerations.h:319
lldb::SBBlock::GetContainingInlinedBlock
lldb::SBBlock GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: SBBlock.cpp:134
VariableList.h
SBAddress.h
lldb::SBBlock::~SBBlock
~SBBlock()
Definition: SBBlock.cpp:45
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
lldb_private::Block::AppendVariables
uint32_t AppendVariables(bool can_create, bool get_parent_variables, bool stop_if_block_is_inlined_function, const std::function< bool(Variable *)> &filter, VariableList *variable_list)
Appends the variables from this block, and optionally from all parent blocks, to variable_list.
Definition: Block.cpp:432
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::Block::GetFirstChild
Block * GetFirstChild() const
Get the first child block.
Definition: Block.h:202
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb_private::Declaration::GetLine
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:120
lldb::eValueTypeVariableLocal
@ eValueTypeVariableLocal
function local variables
Definition: lldb-enumerations.h:320
lldb_private::Block
Definition: Block.h:41
uint32_t
lldb_private::Block::GetRangeAtIndex
bool GetRangeAtIndex(uint32_t range_idx, AddressRange &range)
Definition: Block.cpp:292
lldb_private::repro::RegisterMethods< SBBlock >
void RegisterMethods< SBBlock >(Registry &R)
Definition: SBBlock.cpp:351
lldb::SBValue::SetSP
void SetSP(const lldb::ValueObjectSP &sp)
Definition: SBValue.cpp:1098
lldb::SBBlock::GetInlinedName
const char * GetInlinedName() const
Definition: SBBlock.cpp:65
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
SBValue.h
lldb::SBFileSpec
Definition: SBFileSpec.h:16
Function.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb::SBAddress
Definition: SBAddress.h:17
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
SymbolContext.h
lldb::eValueTypeVariableThreadLocal
@ eValueTypeVariableThreadLocal
thread local storage variable
Definition: lldb-enumerations.h:324
lldb_private::Block::GetRangeIndexContainingAddress
uint32_t GetRangeIndexContainingAddress(const Address &addr)
Definition: Block.cpp:275
lldb::SBAddress::ref
lldb_private::Address & ref()
Definition: SBAddress.cpp:180
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBBlock::GetInlinedCallSiteColumn
uint32_t GetInlinedCallSiteColumn() const
Definition: SBBlock.cpp:104
lldb::SBTarget::GetSP
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:617
ValueObjectVariable.h
lldb::SBBlock::GetSibling
lldb::SBBlock GetSibling()
Definition: SBBlock.cpp:143
lldb::SBBlock::GetFirstChild
lldb::SBBlock GetFirstChild()
Definition: SBBlock.cpp:152
lldb::SBBlock::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBBlock.cpp:165
lldb::eValueTypeVariableStatic
@ eValueTypeVariableStatic
static variable
Definition: lldb-enumerations.h:318
lldb_private::Variable
Definition: Variable.h:25
lldb::SBBlock::SBBlock
SBBlock()
Definition: SBBlock.cpp:28
lldb_private::Block::GetInlinedFunctionInfo
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:276
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBBlock::GetRangeStartAddress
lldb::SBAddress GetRangeStartAddress(uint32_t idx)
Definition: SBBlock.cpp:198
lldb::SBStream
Definition: SBStream.h:18
lldb_private::Block::DumpAddressRanges
void DumpAddressRanges(Stream *s, lldb::addr_t base_addr)
Definition: Block.cpp:158
lldb_private::Block::GetNumRanges
size_t GetNumRanges() const
Definition: Block.h:341
AddressRange.h
lldb::SBBlock::GetInlinedCallSiteFile
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition: SBBlock.cpp:78
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:436
lldb
Definition: SBAddress.h:15
lldb_private::Block::CalculateSymbolContext
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition: Block.cpp:125
lldb_private::Block::GetContainingInlinedBlock
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:197
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93