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 "lldb/API/SBAddress.h"
11 #include "lldb/API/SBFileSpec.h"
12 #include "lldb/API/SBFrame.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBValue.h"
15 #include "lldb/Core/AddressRange.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/Function.h"
21 #include "lldb/Target/StackFrame.h"
22 #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) {
34  LLDB_INSTRUMENT_VA(this, rhs);
35 }
36 
37 const SBBlock &SBBlock::operator=(const SBBlock &rhs) {
38  LLDB_INSTRUMENT_VA(this, rhs);
39 
41  return *this;
42 }
43 
45 
46 bool SBBlock::IsValid() const {
47  LLDB_INSTRUMENT_VA(this);
48  return this->operator bool();
49 }
50 SBBlock::operator bool() const {
51  LLDB_INSTRUMENT_VA(this);
52 
53  return m_opaque_ptr != nullptr;
54 }
55 
56 bool SBBlock::IsInlined() const {
57  LLDB_INSTRUMENT_VA(this);
58 
59  if (m_opaque_ptr)
60  return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
61  return false;
62 }
63 
64 const char *SBBlock::GetInlinedName() const {
65  LLDB_INSTRUMENT_VA(this);
66 
67  if (m_opaque_ptr) {
68  const InlineFunctionInfo *inlined_info =
70  if (inlined_info) {
71  return inlined_info->GetName().AsCString(nullptr);
72  }
73  }
74  return nullptr;
75 }
76 
78  LLDB_INSTRUMENT_VA(this);
79 
80  SBFileSpec sb_file;
81  if (m_opaque_ptr) {
82  const InlineFunctionInfo *inlined_info =
84  if (inlined_info)
85  sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
86  }
87  return sb_file;
88 }
89 
91  LLDB_INSTRUMENT_VA(this);
92 
93  if (m_opaque_ptr) {
94  const InlineFunctionInfo *inlined_info =
96  if (inlined_info)
97  return inlined_info->GetCallSite().GetLine();
98  }
99  return 0;
100 }
101 
103  LLDB_INSTRUMENT_VA(this);
104 
105  if (m_opaque_ptr) {
106  const InlineFunctionInfo *inlined_info =
108  if (inlined_info)
109  return inlined_info->GetCallSite().GetColumn();
110  }
111  return 0;
112 }
113 
114 void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
115  lldb_private::VariableList *var_list) {
116  if (IsValid()) {
117  bool show_inline = true;
118  m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
119  [](Variable *) { return true; }, var_list);
120  }
121 }
122 
124  LLDB_INSTRUMENT_VA(this);
125 
126  SBBlock sb_block;
127  if (m_opaque_ptr)
128  sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
129  return sb_block;
130 }
131 
133  LLDB_INSTRUMENT_VA(this);
134 
135  SBBlock sb_block;
136  if (m_opaque_ptr)
138  return sb_block;
139 }
140 
142  LLDB_INSTRUMENT_VA(this);
143 
144  SBBlock sb_block;
145  if (m_opaque_ptr)
146  sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
147  return sb_block;
148 }
149 
151  LLDB_INSTRUMENT_VA(this);
152 
153  SBBlock sb_block;
154  if (m_opaque_ptr)
156  return sb_block;
157 }
158 
160 
162 
163 bool SBBlock::GetDescription(SBStream &description) {
164  LLDB_INSTRUMENT_VA(this, description);
165 
166  Stream &strm = description.ref();
167 
168  if (m_opaque_ptr) {
170  strm.Printf("Block: {id: %" PRIu64 "} ", id);
171  if (IsInlined()) {
172  strm.Printf(" (inlined, '%s') ", GetInlinedName());
173  }
176  if (sc.function) {
178  &strm,
180  }
181  } else
182  strm.PutCString("No value");
183 
184  return true;
185 }
186 
188  LLDB_INSTRUMENT_VA(this);
189 
190  if (m_opaque_ptr)
191  return m_opaque_ptr->GetNumRanges();
192  return 0;
193 }
194 
196  LLDB_INSTRUMENT_VA(this, idx);
197 
198  lldb::SBAddress sb_addr;
199  if (m_opaque_ptr) {
200  AddressRange range;
201  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
202  sb_addr.ref() = range.GetBaseAddress();
203  }
204  }
205  return sb_addr;
206 }
207 
209  LLDB_INSTRUMENT_VA(this, idx);
210 
211  lldb::SBAddress sb_addr;
212  if (m_opaque_ptr) {
213  AddressRange range;
214  if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
215  sb_addr.ref() = range.GetBaseAddress();
216  sb_addr.ref().Slide(range.GetByteSize());
217  }
218  }
219  return sb_addr;
220 }
221 
223  LLDB_INSTRUMENT_VA(this, block_addr);
224 
225  if (m_opaque_ptr && block_addr.IsValid()) {
226  return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
227  }
228 
229  return UINT32_MAX;
230 }
231 
233  bool locals, bool statics,
234  lldb::DynamicValueType use_dynamic) {
235  LLDB_INSTRUMENT_VA(this, frame, arguments, locals, statics, use_dynamic);
236 
237  Block *block = GetPtr();
238  SBValueList value_list;
239  if (block) {
240  StackFrameSP frame_sp(frame.GetFrameSP());
241  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
242 
243  if (variable_list_sp) {
244  const size_t num_variables = variable_list_sp->GetSize();
245  if (num_variables) {
246  for (size_t i = 0; i < num_variables; ++i) {
247  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
248  if (variable_sp) {
249  bool add_variable = false;
250  switch (variable_sp->GetScope()) {
254  add_variable = statics;
255  break;
256 
258  add_variable = arguments;
259  break;
260 
262  add_variable = locals;
263  break;
264 
265  default:
266  break;
267  }
268  if (add_variable) {
269  if (frame_sp) {
270  lldb::ValueObjectSP valobj_sp(
271  frame_sp->GetValueObjectForFrameVariable(variable_sp,
273  SBValue value_sb;
274  value_sb.SetSP(valobj_sp, use_dynamic);
275  value_list.Append(value_sb);
276  }
277  }
278  }
279  }
280  }
281  }
282  }
283  return value_list;
284 }
285 
287  bool locals, bool statics) {
288  LLDB_INSTRUMENT_VA(this, target, arguments, locals, statics);
289 
290  Block *block = GetPtr();
291 
292  SBValueList value_list;
293  if (block) {
294  TargetSP target_sp(target.GetSP());
295 
296  VariableListSP variable_list_sp(block->GetBlockVariableList(true));
297 
298  if (variable_list_sp) {
299  const size_t num_variables = variable_list_sp->GetSize();
300  if (num_variables) {
301  for (size_t i = 0; i < num_variables; ++i) {
302  VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
303  if (variable_sp) {
304  bool add_variable = false;
305  switch (variable_sp->GetScope()) {
309  add_variable = statics;
310  break;
311 
313  add_variable = arguments;
314  break;
315 
317  add_variable = locals;
318  break;
319 
320  default:
321  break;
322  }
323  if (add_variable) {
324  if (target_sp)
325  value_list.Append(
326  ValueObjectVariable::Create(target_sp.get(), variable_sp));
327  }
328  }
329  }
330  }
331  }
332  }
333  return value_list;
334 }
lldb::SBValueList
Definition: SBValueList.h:18
Instrumentation.h
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:46
lldb::SBBlock::GetNumRanges
uint32_t GetNumRanges()
Definition: SBBlock.cpp:187
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
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:399
lldb::SBFrame::GetFrameSP
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:83
lldb_private::InlineFunctionInfo
Definition: Function.h:125
lldb_private::Function::GetAddressRange
const AddressRange & GetAddressRange()
Definition: Function.h:457
lldb::SBBlock::m_opaque_ptr
lldb_private::Block * m_opaque_ptr
Definition: SBBlock.h:90
lldb::eNoDynamicValues
@ eNoDynamicValues
Definition: lldb-enumerations.h:495
lldb_private::Block::GetSibling
Block * GetSibling() const
Get the sibling block for this block.
Definition: Block.cpp:505
lldb::SBBlock::GetRangeIndexForBlockAddress
uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr)
Definition: SBBlock.cpp:222
lldb_private::Block::GetParent
Block * GetParent() const
Get the parent block.
Definition: Block.cpp:202
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:208
lldb_private::InlineFunctionInfo::GetCallSite
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition: Function.cpp:107
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
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:161
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb::SBFileSpec::SetFileSpec
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:164
lldb_private::VariableList
Definition: VariableList.h:18
lldb::SBBlock::IsInlined
bool IsInlined() const
Definition: SBBlock.cpp:56
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:114
SBStream.h
lldb::SBFrame
Definition: SBFrame.h:17
lldb::SBBlock::GetPtr
lldb_private::Block * GetPtr()
Definition: SBBlock.cpp:159
SBBlock.h
lldb::SBBlock::GetVariables
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition: SBBlock.cpp:232
SBFileSpec.h
SBFrame.h
lldb::SBBlock::GetInlinedCallSiteLine
uint32_t GetInlinedCallSiteLine() const
Definition: SBBlock.cpp:90
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:291
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:124
lldb::SBAddress::IsValid
bool IsValid() const
Definition: SBAddress.cpp:72
lldb::SBBlock::GetParent
lldb::SBBlock GetParent()
Definition: SBBlock.cpp:123
lldb_private::InlineFunctionInfo::GetName
ConstString GetName() const
Definition: Function.cpp:95
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:132
VariableList.h
SBAddress.h
lldb::SBBlock::~SBBlock
~SBBlock()
Definition: SBBlock.cpp:44
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:443
lldb_private::Block::GetFirstChild
Block * GetFirstChild() const
Get the first child block.
Definition: Block.h:202
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:303
lldb::SBValue::SetSP
void SetSP(const lldb::ValueObjectSP &sp)
Definition: SBValue.cpp:1066
lldb::SBBlock::GetInlinedName
const char * GetInlinedName() const
Definition: SBBlock.cpp:64
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
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:286
lldb::SBAddress::ref
lldb_private::Address & ref()
Definition: SBAddress.cpp:173
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:102
lldb::SBTarget::GetSP
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:576
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
ValueObjectVariable.h
lldb::SBBlock::GetSibling
lldb::SBBlock GetSibling()
Definition: SBBlock.cpp:141
lldb::SBBlock::GetFirstChild
lldb::SBBlock GetFirstChild()
Definition: SBBlock.cpp:150
lldb::SBBlock::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBBlock.cpp:163
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:195
lldb::SBStream
Definition: SBStream.h:18
lldb_private::Block::DumpAddressRanges
void DumpAddressRanges(Stream *s, lldb::addr_t base_addr)
Definition: Block.cpp:169
lldb_private::Block::GetNumRanges
size_t GetNumRanges() const
Definition: Block.h:343
AddressRange.h
lldb::SBBlock::GetInlinedCallSiteFile
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition: SBBlock.cpp:77
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:449
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:136
lldb_private::Block::GetContainingInlinedBlock
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:208