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 
28 SBBlock::SBBlock() : m_opaque_ptr(nullptr) {
30 }
31 
33  : m_opaque_ptr(lldb_object_ptr) {}
34 
37 }
38 
39 const SBBlock &SBBlock::operator=(const SBBlock &rhs) {
41  SBBlock, operator=,(const lldb::SBBlock &), rhs);
42 
44  return LLDB_RECORD_RESULT(*this);
45 }
46 
48 
49 bool SBBlock::IsValid() const {
51  return this->operator bool();
52 }
53 SBBlock::operator bool() const {
54  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBBlock, operator bool);
55 
56  return m_opaque_ptr != nullptr;
57 }
58 
59 bool SBBlock::IsInlined() const {
61 
62  if (m_opaque_ptr)
63  return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
64  return false;
65 }
66 
67 const char *SBBlock::GetInlinedName() const {
69 
70  if (m_opaque_ptr) {
71  const InlineFunctionInfo *inlined_info =
73  if (inlined_info) {
74  return inlined_info->GetName().AsCString(nullptr);
75  }
76  }
77  return nullptr;
78 }
79 
83 
84  SBFileSpec sb_file;
85  if (m_opaque_ptr) {
86  const InlineFunctionInfo *inlined_info =
88  if (inlined_info)
89  sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
90  }
91  return LLDB_RECORD_RESULT(sb_file);
92 }
93 
96 
97  if (m_opaque_ptr) {
98  const InlineFunctionInfo *inlined_info =
100  if (inlined_info)
101  return inlined_info->GetCallSite().GetLine();
102  }
103  return 0;
104 }
105 
108 
109  if (m_opaque_ptr) {
110  const InlineFunctionInfo *inlined_info =
112  if (inlined_info)
113  return inlined_info->GetCallSite().GetColumn();
114  }
115  return 0;
116 }
117 
118 void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
119  lldb_private::VariableList *var_list) {
120  if (IsValid()) {
121  bool show_inline = true;
122  m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
123  [](Variable *) { return true; }, var_list);
124  }
125 }
126 
129 
130  SBBlock sb_block;
131  if (m_opaque_ptr)
132  sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
133  return LLDB_RECORD_RESULT(sb_block);
134 }
135 
138 
139  SBBlock sb_block;
140  if (m_opaque_ptr)
142  return LLDB_RECORD_RESULT(sb_block);
143 }
144 
147 
148  SBBlock sb_block;
149  if (m_opaque_ptr)
150  sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
151  return LLDB_RECORD_RESULT(sb_block);
152 }
153 
156 
157  SBBlock sb_block;
158  if (m_opaque_ptr)
160  return LLDB_RECORD_RESULT(sb_block);
161 }
162 
164 
166 
167 bool SBBlock::GetDescription(SBStream &description) {
169  description);
170 
171  Stream &strm = description.ref();
172 
173  if (m_opaque_ptr) {
175  strm.Printf("Block: {id: %" PRIu64 "} ", id);
176  if (IsInlined()) {
177  strm.Printf(" (inlined, '%s') ", GetInlinedName());
178  }
181  if (sc.function) {
183  &strm,
185  }
186  } else
187  strm.PutCString("No value");
188 
189  return true;
190 }
191 
194 
195  if (m_opaque_ptr)
196  return m_opaque_ptr->GetNumRanges();
197  return 0;
198 }
199 
202  idx);
203 
204  lldb::SBAddress sb_addr;
205  if (m_opaque_ptr) {
206  AddressRange range;
207  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
208  sb_addr.ref() = range.GetBaseAddress();
209  }
210  }
211  return LLDB_RECORD_RESULT(sb_addr);
212 }
213 
216  idx);
217 
218  lldb::SBAddress sb_addr;
219  if (m_opaque_ptr) {
220  AddressRange range;
221  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
222  sb_addr.ref() = range.GetBaseAddress();
223  sb_addr.ref().Slide(range.GetByteSize());
224  }
225  }
226  return LLDB_RECORD_RESULT(sb_addr);
227 }
228 
231  (lldb::SBAddress), block_addr);
232 
233  if (m_opaque_ptr && block_addr.IsValid()) {
234  return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
235  }
236 
237  return UINT32_MAX;
238 }
239 
241  bool locals, bool statics,
242  lldb::DynamicValueType use_dynamic) {
245  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType), frame,
246  arguments, locals, statics, use_dynamic);
247 
248  Block *block = GetPtr();
249  SBValueList value_list;
250  if (block) {
251  StackFrameSP frame_sp(frame.GetFrameSP());
252  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
253 
254  if (variable_list_sp) {
255  const size_t num_variables = variable_list_sp->GetSize();
256  if (num_variables) {
257  for (size_t i = 0; i < num_variables; ++i) {
258  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
259  if (variable_sp) {
260  bool add_variable = false;
261  switch (variable_sp->GetScope()) {
265  add_variable = statics;
266  break;
267 
269  add_variable = arguments;
270  break;
271 
273  add_variable = locals;
274  break;
275 
276  default:
277  break;
278  }
279  if (add_variable) {
280  if (frame_sp) {
281  lldb::ValueObjectSP valobj_sp(
282  frame_sp->GetValueObjectForFrameVariable(variable_sp,
284  SBValue value_sb;
285  value_sb.SetSP(valobj_sp, use_dynamic);
286  value_list.Append(value_sb);
287  }
288  }
289  }
290  }
291  }
292  }
293  }
294  return LLDB_RECORD_RESULT(value_list);
295 }
296 
298  bool locals, bool statics) {
300  (lldb::SBTarget &, bool, bool, bool), target, arguments,
301  locals, statics);
302 
303  Block *block = GetPtr();
304 
305  SBValueList value_list;
306  if (block) {
307  TargetSP target_sp(target.GetSP());
308 
309  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
310 
311  if (variable_list_sp) {
312  const size_t num_variables = variable_list_sp->GetSize();
313  if (num_variables) {
314  for (size_t i = 0; i < num_variables; ++i) {
315  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
316  if (variable_sp) {
317  bool add_variable = false;
318  switch (variable_sp->GetScope()) {
322  add_variable = statics;
323  break;
324 
326  add_variable = arguments;
327  break;
328 
330  add_variable = locals;
331  break;
332 
333  default:
334  break;
335  }
336  if (add_variable) {
337  if (target_sp)
338  value_list.Append(
339  ValueObjectVariable::Create(target_sp.get(), variable_sp));
340  }
341  }
342  }
343  }
344  }
345  }
346  return LLDB_RECORD_RESULT(value_list);
347 }
348 
349 namespace lldb_private {
350 namespace repro {
351 
352 template <>
357  SBBlock, operator=,(const lldb::SBBlock &));
359  LLDB_REGISTER_METHOD_CONST(bool, SBBlock, operator bool, ());
373  (uint32_t));
375  (uint32_t));
377  (lldb::SBAddress));
380  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType));
382  (lldb::SBTarget &, bool, bool, bool));
383 }
384 
385 }
386 }
uint32_t GetRangeIndexContainingAddress(const Address &addr)
Definition: Block.cpp:275
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
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::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:222
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition: SBBlock.cpp:80
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:223
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr)
Definition: SBBlock.cpp:229
Block * GetParent() const
Get the parent block.
Definition: Block.cpp:191
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
uint32_t GetNumRanges()
Definition: SBBlock.cpp:192
uint32_t GetInlinedCallSiteColumn() const
Definition: SBBlock.cpp:106
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
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
void RegisterMethods< SBBlock >(Registry &R)
Definition: SBBlock.cpp:353
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:388
Block * GetSibling() const
Get the sibling block for this block.
Definition: Block.cpp:494
A class that describes a single lexical block.
Definition: Block.h:41
void SetSP(const lldb::ValueObjectSP &sp)
Definition: SBValue.cpp:1098
Block * GetFirstChild() const
Get the first child block.
Definition: Block.h:202
Function * function
The Function for a given query.
lldb::SBBlock GetSibling()
Definition: SBBlock.cpp:145
const lldb::SBBlock & operator=(const lldb::SBBlock &rhs)
Definition: SBBlock.cpp:39
#define UINT32_MAX
Definition: lldb-defines.h:31
bool GetRangeAtIndex(uint32_t range_idx, AddressRange &range)
Definition: Block.cpp:292
bool Slide(int64_t offset)
Definition: Address.h:436
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:138
lldb::SBAddress GetRangeStartAddress(uint32_t idx)
Definition: SBBlock.cpp:200
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
uint64_t user_id_t
Definition: lldb-types.h:84
bool IsInlined() const
Definition: SBBlock.cpp:59
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:608
const char * GetInlinedName() const
Definition: SBBlock.cpp:67
lldb::SBBlock GetParent()
Definition: SBBlock.cpp:127
lldb_private::Block * GetPtr()
Definition: SBBlock.cpp:163
lldb_private::Address & ref()
Definition: SBAddress.cpp:188
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
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
lldb::SBAddress GetRangeEndAddress(uint32_t idx)
Definition: SBBlock.cpp:214
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:197
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition: Function.cpp:106
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:170
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object&#39;s symbol context into sc.
Definition: Block.cpp:125
lldb_private::Block * m_opaque_ptr
Definition: SBBlock.h:90
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:151
function argument variables
lldb::SBBlock GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: SBBlock.cpp:136
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:276
function local variables
A class that describes information for an inlined function.
Definition: Function.h:124
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:84
uint32_t GetInlinedCallSiteLine() const
Definition: SBBlock.cpp:94
lldb::SBBlock GetFirstChild()
Definition: SBBlock.cpp:154
size_t GetNumRanges() const
Definition: Block.h:341
Definition: SBAddress.h:15
thread local storage variable
bool GetDescription(lldb::SBStream &description)
Definition: SBBlock.cpp:167
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:210
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
void AppendVariables(bool can_create, bool get_parent_variables, lldb_private::VariableList *var_list)
Definition: SBBlock.cpp:118
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
const AddressRange & GetAddressRange()
Definition: Function.h:457
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition: SBBlock.cpp:240
bool IsValid() const
Definition: SBBlock.cpp:49
void DumpAddressRanges(Stream *s, lldb::addr_t base_addr)
Definition: Block.cpp:158
A section + offset based address range class.
Definition: AddressRange.h:25
uint32_t GetSize()
Definition: SBValueList.cpp:33
#define LLDB_RECORD_RESULT(Result)
ConstString GetName() const
Definition: Function.cpp:94
bool IsValid() const
Definition: SBAddress.cpp:79
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
uint32_t GetColumn() const
Get accessor for the declaration column number.
Definition: Declaration.h:126
void Append(const lldb::SBValue &val_obj)