LLDB  mainline
SBBlock.cpp
Go to the documentation of this file.
1 //===-- SBBlock.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/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(NULL) {
30 }
31 
32 SBBlock::SBBlock(lldb_private::Block *lldb_object_ptr)
33  : m_opaque_ptr(lldb_object_ptr) {}
34 
35 SBBlock::SBBlock(const SBBlock &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
37 }
38 
39 const SBBlock &SBBlock::operator=(const SBBlock &rhs) {
41  SBBlock, operator=,(const lldb::SBBlock &), rhs);
42 
43  m_opaque_ptr = rhs.m_opaque_ptr;
44  return LLDB_RECORD_RESULT(*this);
45 }
46 
47 SBBlock::~SBBlock() { m_opaque_ptr = NULL; }
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 != NULL;
57 }
58 
59 bool SBBlock::IsInlined() const {
61 
62  if (m_opaque_ptr)
63  return m_opaque_ptr->GetInlinedFunctionInfo() != NULL;
64  return false;
65 }
66 
67 const char *SBBlock::GetInlinedName() const {
69 
70  if (m_opaque_ptr) {
71  const InlineFunctionInfo *inlined_info =
72  m_opaque_ptr->GetInlinedFunctionInfo();
73  if (inlined_info) {
74  Function *function = m_opaque_ptr->CalculateSymbolContextFunction();
75  LanguageType language;
76  if (function)
77  language = function->GetLanguage();
78  else
79  language = lldb::eLanguageTypeUnknown;
80  return inlined_info->GetName(language).AsCString(NULL);
81  }
82  }
83  return NULL;
84 }
85 
89 
90  SBFileSpec sb_file;
91  if (m_opaque_ptr) {
92  const InlineFunctionInfo *inlined_info =
93  m_opaque_ptr->GetInlinedFunctionInfo();
94  if (inlined_info)
95  sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
96  }
97  return LLDB_RECORD_RESULT(sb_file);
98 }
99 
102 
103  if (m_opaque_ptr) {
104  const InlineFunctionInfo *inlined_info =
105  m_opaque_ptr->GetInlinedFunctionInfo();
106  if (inlined_info)
107  return inlined_info->GetCallSite().GetLine();
108  }
109  return 0;
110 }
111 
114 
115  if (m_opaque_ptr) {
116  const InlineFunctionInfo *inlined_info =
117  m_opaque_ptr->GetInlinedFunctionInfo();
118  if (inlined_info)
119  return inlined_info->GetCallSite().GetColumn();
120  }
121  return 0;
122 }
123 
124 void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
125  lldb_private::VariableList *var_list) {
126  if (IsValid()) {
127  bool show_inline = true;
128  m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
129  [](Variable *) { return true; }, var_list);
130  }
131 }
132 
135 
136  SBBlock sb_block;
137  if (m_opaque_ptr)
138  sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
139  return LLDB_RECORD_RESULT(sb_block);
140 }
141 
144 
145  SBBlock sb_block;
146  if (m_opaque_ptr)
147  sb_block.m_opaque_ptr = m_opaque_ptr->GetContainingInlinedBlock();
148  return LLDB_RECORD_RESULT(sb_block);
149 }
150 
153 
154  SBBlock sb_block;
155  if (m_opaque_ptr)
156  sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
157  return LLDB_RECORD_RESULT(sb_block);
158 }
159 
162 
163  SBBlock sb_block;
164  if (m_opaque_ptr)
165  sb_block.m_opaque_ptr = m_opaque_ptr->GetFirstChild();
166  return LLDB_RECORD_RESULT(sb_block);
167 }
168 
169 lldb_private::Block *SBBlock::GetPtr() { return m_opaque_ptr; }
170 
171 void SBBlock::SetPtr(lldb_private::Block *block) { m_opaque_ptr = block; }
172 
173 bool SBBlock::GetDescription(SBStream &description) {
175  description);
176 
177  Stream &strm = description.ref();
178 
179  if (m_opaque_ptr) {
180  lldb::user_id_t id = m_opaque_ptr->GetID();
181  strm.Printf("Block: {id: %" PRIu64 "} ", id);
182  if (IsInlined()) {
183  strm.Printf(" (inlined, '%s') ", GetInlinedName());
184  }
186  m_opaque_ptr->CalculateSymbolContext(&sc);
187  if (sc.function) {
188  m_opaque_ptr->DumpAddressRanges(
189  &strm,
191  }
192  } else
193  strm.PutCString("No value");
194 
195  return true;
196 }
197 
200 
201  if (m_opaque_ptr)
202  return m_opaque_ptr->GetNumRanges();
203  return 0;
204 }
205 
208  idx);
209 
210  lldb::SBAddress sb_addr;
211  if (m_opaque_ptr) {
212  AddressRange range;
213  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
214  sb_addr.ref() = range.GetBaseAddress();
215  }
216  }
217  return LLDB_RECORD_RESULT(sb_addr);
218 }
219 
222  idx);
223 
224  lldb::SBAddress sb_addr;
225  if (m_opaque_ptr) {
226  AddressRange range;
227  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
228  sb_addr.ref() = range.GetBaseAddress();
229  sb_addr.ref().Slide(range.GetByteSize());
230  }
231  }
232  return LLDB_RECORD_RESULT(sb_addr);
233 }
234 
237  (lldb::SBAddress), block_addr);
238 
239  if (m_opaque_ptr && block_addr.IsValid()) {
240  return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
241  }
242 
243  return UINT32_MAX;
244 }
245 
247  bool locals, bool statics,
248  lldb::DynamicValueType use_dynamic) {
251  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType), frame,
252  arguments, locals, statics, use_dynamic);
253 
254  Block *block = GetPtr();
255  SBValueList value_list;
256  if (block) {
257  StackFrameSP frame_sp(frame.GetFrameSP());
258  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
259 
260  if (variable_list_sp) {
261  const size_t num_variables = variable_list_sp->GetSize();
262  if (num_variables) {
263  for (size_t i = 0; i < num_variables; ++i) {
264  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
265  if (variable_sp) {
266  bool add_variable = false;
267  switch (variable_sp->GetScope()) {
271  add_variable = statics;
272  break;
273 
275  add_variable = arguments;
276  break;
277 
279  add_variable = locals;
280  break;
281 
282  default:
283  break;
284  }
285  if (add_variable) {
286  if (frame_sp) {
287  lldb::ValueObjectSP valobj_sp(
288  frame_sp->GetValueObjectForFrameVariable(variable_sp,
290  SBValue value_sb;
291  value_sb.SetSP(valobj_sp, use_dynamic);
292  value_list.Append(value_sb);
293  }
294  }
295  }
296  }
297  }
298  }
299  }
300  return LLDB_RECORD_RESULT(value_list);
301 }
302 
304  bool locals, bool statics) {
306  (lldb::SBTarget &, bool, bool, bool), target, arguments,
307  locals, statics);
308 
309  Block *block = GetPtr();
310 
311  SBValueList value_list;
312  if (block) {
313  TargetSP target_sp(target.GetSP());
314 
315  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
316 
317  if (variable_list_sp) {
318  const size_t num_variables = variable_list_sp->GetSize();
319  if (num_variables) {
320  for (size_t i = 0; i < num_variables; ++i) {
321  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
322  if (variable_sp) {
323  bool add_variable = false;
324  switch (variable_sp->GetScope()) {
328  add_variable = statics;
329  break;
330 
332  add_variable = arguments;
333  break;
334 
336  add_variable = locals;
337  break;
338 
339  default:
340  break;
341  }
342  if (add_variable) {
343  if (target_sp)
344  value_list.Append(
345  ValueObjectVariable::Create(target_sp.get(), variable_sp));
346  }
347  }
348  }
349  }
350  }
351  }
352  return LLDB_RECORD_RESULT(value_list);
353 }
354 
355 namespace lldb_private {
356 namespace repro {
357 
358 template <>
363  SBBlock, operator=,(const lldb::SBBlock &));
365  LLDB_REGISTER_METHOD_CONST(bool, SBBlock, operator bool, ());
379  (uint32_t));
381  (uint32_t));
383  (lldb::SBAddress));
386  (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType));
388  (lldb::SBTarget &, bool, bool, bool));
389 }
390 
391 }
392 }
uint32_t GetRangeIndexContainingAddress(const Address &addr)
Definition: Block.cpp:276
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
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::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:232
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition: SBBlock.cpp:86
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
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:235
Block * GetParent() const
Get the parent block.
Definition: Block.cpp:192
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
uint32_t GetNumRanges()
Definition: SBBlock.cpp:198
uint32_t GetInlinedCallSiteColumn() const
Definition: SBBlock.cpp:112
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::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:272
void RegisterMethods< SBBlock >(Registry &R)
Definition: SBBlock.cpp:359
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:387
Block * GetSibling() const
Get the sibling block for this block.
Definition: Block.cpp:495
A class that describes a single lexical block.
Definition: Block.h:41
void SetSP(const lldb::ValueObjectSP &sp)
Definition: SBValue.cpp:1117
lldb::LanguageType GetLanguage() const
Definition: Function.cpp:591
Block * GetFirstChild() const
Get the first child block.
Definition: Block.h:216
Function * function
The Function for a given query.
lldb::SBBlock GetSibling()
Definition: SBBlock.cpp:151
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:293
bool Slide(int64_t offset)
Definition: Address.h:430
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:148
lldb::SBAddress GetRangeStartAddress(uint32_t idx)
Definition: SBBlock.cpp:206
#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:600
LanguageType
Programming language type.
const char * GetInlinedName() const
Definition: SBBlock.cpp:67
lldb::SBBlock GetParent()
Definition: SBBlock.cpp:133
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,...)
ConstString GetName(lldb::LanguageType language) const
Definition: Function.cpp:104
lldb::SBAddress GetRangeEndAddress(uint32_t idx)
Definition: SBBlock.cpp:220
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:198
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition: Function.cpp:116
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object&#39;s symbol context into sc.
Definition: Block.cpp:126
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:161
lldb::SBBlock GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: SBBlock.cpp:142
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:296
A class that describes information for an inlined function.
Definition: Function.h:122
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:84
uint32_t GetInlinedCallSiteLine() const
Definition: SBBlock.cpp:100
Unknown or invalid language value.
lldb::SBBlock GetFirstChild()
Definition: SBBlock.cpp:160
size_t GetNumRanges() const
Definition: Block.h:361
Definition: SBAddress.h:15
bool GetDescription(lldb::SBStream &description)
Definition: SBBlock.cpp:173
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)
Function * CalculateSymbolContextFunction() override
Definition: Block.cpp:144
const AddressRange & GetAddressRange()
Definition: Function.h:371
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition: SBBlock.cpp:246
bool IsValid() const
Definition: SBBlock.cpp:49
void DumpAddressRanges(Stream *s, lldb::addr_t base_addr)
Definition: Block.cpp:159
A section + offset based address range class.
Definition: AddressRange.h:32
uint32_t GetSize()
Definition: SBValueList.cpp:33
#define LLDB_RECORD_RESULT(Result)
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:136
void Append(const lldb::SBValue &val_obj)