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"
17#include "lldb/Symbol/Block.h"
22#include "lldb/Target/Target.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
30
32 : m_opaque_ptr(lldb_object_ptr) {}
33
34SBBlock::SBBlock(const SBBlock &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
35 LLDB_INSTRUMENT_VA(this, rhs);
36}
37
39 LLDB_INSTRUMENT_VA(this, rhs);
40
42 return *this;
43}
44
46
47bool SBBlock::IsValid() const {
49 return this->operator bool();
50}
51SBBlock::operator bool() const {
53
54 return m_opaque_ptr != nullptr;
55}
56
57bool SBBlock::IsInlined() const {
59
60 if (m_opaque_ptr)
61 return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
62 return false;
63}
64
65const 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
80
81 SBFileSpec sb_file;
82 if (m_opaque_ptr) {
83 const InlineFunctionInfo *inlined_info =
85 if (inlined_info)
86 sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
87 }
88 return sb_file;
89}
90
93
94 if (m_opaque_ptr) {
95 const InlineFunctionInfo *inlined_info =
97 if (inlined_info)
98 return inlined_info->GetCallSite().GetLine();
99 }
100 return 0;
101}
102
104 LLDB_INSTRUMENT_VA(this);
105
106 if (m_opaque_ptr) {
107 const InlineFunctionInfo *inlined_info =
109 if (inlined_info)
110 return inlined_info->GetCallSite().GetColumn();
111 }
112 return 0;
113}
114
115void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
116 lldb_private::VariableList *var_list) {
117 if (IsValid()) {
118 bool show_inline = true;
119 m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
120 [](Variable *) { return true; }, var_list);
121 }
122}
123
125 LLDB_INSTRUMENT_VA(this);
126
127 SBBlock sb_block;
128 if (m_opaque_ptr)
129 sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
130 return sb_block;
131}
132
134 LLDB_INSTRUMENT_VA(this);
135
136 SBBlock sb_block;
137 if (m_opaque_ptr)
139 return sb_block;
140}
141
143 LLDB_INSTRUMENT_VA(this);
144
145 SBBlock sb_block;
146 if (m_opaque_ptr)
148 return sb_block;
149}
150
152 LLDB_INSTRUMENT_VA(this);
153
154 SBBlock sb_block;
155 if (m_opaque_ptr)
157 return sb_block;
158}
159
161
163
165 LLDB_INSTRUMENT_VA(this, description);
166
167 Stream &strm = description.ref();
168
169 if (m_opaque_ptr) {
171 strm.Printf("Block: {id: %" PRIu64 "} ", id);
172 if (IsInlined()) {
173 strm.Printf(" (inlined, '%s') ", GetInlinedName());
174 }
177 if (sc.function) {
179 &strm, sc.function->GetAddress().GetFileAddress());
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);
224
225 lldb::SBAddressRangeList sb_ranges;
226 if (m_opaque_ptr)
227 sb_ranges.m_opaque_up->ref() = m_opaque_ptr->GetRanges();
228 return sb_ranges;
229}
230
232 LLDB_INSTRUMENT_VA(this, block_addr);
233
234 if (m_opaque_ptr && block_addr.IsValid()) {
235 return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
236 }
237
238 return UINT32_MAX;
239}
240
242 bool locals, bool statics,
243 lldb::DynamicValueType use_dynamic) {
244 LLDB_INSTRUMENT_VA(this, frame, 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 value_list;
293}
294
296 bool locals, bool statics) {
297 LLDB_INSTRUMENT_VA(this, target, arguments, locals, statics);
298
299 Block *block = GetPtr();
300
301 SBValueList value_list;
302 if (block) {
303 TargetSP target_sp(target.GetSP());
304
305 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
306
307 if (variable_list_sp) {
308 const size_t num_variables = variable_list_sp->GetSize();
309 if (num_variables) {
310 for (size_t i = 0; i < num_variables; ++i) {
311 VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
312 if (variable_sp) {
313 bool add_variable = false;
314 switch (variable_sp->GetScope()) {
318 add_variable = statics;
319 break;
320
322 add_variable = arguments;
323 break;
324
326 add_variable = locals;
327 break;
328
329 default:
330 break;
331 }
332 if (add_variable) {
333 if (target_sp)
334 value_list.Append(
335 ValueObjectVariable::Create(target_sp.get(), variable_sp));
336 }
337 }
338 }
339 }
340 }
341 }
342 return value_list;
343}
#define LLDB_INSTRUMENT_VA(...)
std::unique_ptr< lldb_private::AddressRangeListImpl > m_opaque_up
lldb_private::Address & ref()
Definition: SBAddress.cpp:173
bool IsValid() const
Definition: SBAddress.cpp:72
void AppendVariables(bool can_create, bool get_parent_variables, lldb_private::VariableList *var_list)
Definition: SBBlock.cpp:115
lldb::SBAddress GetRangeEndAddress(uint32_t idx)
Definition: SBBlock.cpp:208
lldb::SBAddressRangeList GetRanges()
Definition: SBBlock.cpp:222
uint32_t GetNumRanges()
Definition: SBBlock.cpp:187
const char * GetInlinedName() const
Definition: SBBlock.cpp:65
uint32_t GetInlinedCallSiteLine() const
Definition: SBBlock.cpp:91
lldb::SBAddress GetRangeStartAddress(uint32_t idx)
Definition: SBBlock.cpp:195
bool IsValid() const
Definition: SBBlock.cpp:47
const lldb::SBBlock & operator=(const lldb::SBBlock &rhs)
Definition: SBBlock.cpp:38
lldb::SBBlock GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: SBBlock.cpp:133
uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr)
Definition: SBBlock.cpp:231
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition: SBBlock.cpp:241
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:162
lldb::SBBlock GetParent()
Definition: SBBlock.cpp:124
lldb::SBBlock GetFirstChild()
Definition: SBBlock.cpp:151
bool IsInlined() const
Definition: SBBlock.cpp:57
uint32_t GetInlinedCallSiteColumn() const
Definition: SBBlock.cpp:103
lldb::SBBlock GetSibling()
Definition: SBBlock.cpp:142
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition: SBBlock.cpp:78
lldb_private::Block * m_opaque_ptr
Definition: SBBlock.h:94
lldb_private::Block * GetPtr()
Definition: SBBlock.cpp:160
bool GetDescription(lldb::SBStream &description)
Definition: SBBlock.cpp:164
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:164
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:86
lldb_private::Stream & ref()
Definition: SBStream.cpp:177
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:593
void Append(const lldb::SBValue &val_obj)
void SetSP(const lldb::ValueObjectSP &sp)
Definition: SBValue.cpp:1115
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:223
bool Slide(int64_t offset)
Definition: Address.h:459
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:292
A class that describes a single lexical block.
Definition: Block.h:41
Block * GetFirstChild() const
Get the first child block.
Definition: Block.h:192
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:405
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition: Block.cpp:137
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:200
bool GetRangeAtIndex(uint32_t range_idx, AddressRange &range)
Definition: Block.cpp:296
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:266
AddressRanges GetRanges()
Definition: Block.cpp:310
Block * GetSibling() const
Get the sibling block for this block.
Definition: Block.cpp:511
Block * GetParent() const
Get the parent block.
Definition: Block.cpp:196
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:449
size_t GetNumRanges() const
Definition: Block.h:329
void DumpAddressRanges(Stream *s, lldb::addr_t base_addr)
Definition: Block.cpp:163
uint32_t GetRangeIndexContainingAddress(const Address &addr)
Definition: Block.cpp:279
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:124
uint16_t GetColumn() const
Get accessor for the declaration column number.
Definition: Declaration.h:131
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:111
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition: Function.h:455
A class that describes information for an inlined function.
Definition: Function.h:125
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition: Function.cpp:108
ConstString GetName() const
Definition: Function.cpp:96
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
Function * function
The Function for a given query.
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:424
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
std::shared_ptr< lldb_private::VariableList > VariableListSP
Definition: lldb-forward.h:487
std::shared_ptr< lldb_private::Variable > VariableSP
Definition: lldb-forward.h:486
uint64_t user_id_t
Definition: lldb-types.h:82
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:448
@ eNoDynamicValues
@ eValueTypeVariableGlobal
globals variable
@ eValueTypeVariableLocal
function local variables
@ eValueTypeVariableArgument
function argument variables
@ eValueTypeVariableStatic
static variable
@ eValueTypeVariableThreadLocal
thread local storage variable
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47