LLDB mainline
SBBlock.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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"
23#include "lldb/lldb-types.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
29
31 : m_opaque_ptr(lldb_object_ptr) {}
32
36
38 LLDB_INSTRUMENT_VA(this, rhs);
39
41 return *this;
42}
43
45
46bool SBBlock::IsValid() const {
48 return this->operator bool();
49}
50SBBlock::operator bool() const {
52
53 return m_opaque_ptr != nullptr;
54}
55
56bool SBBlock::operator==(const SBBlock &rhs) const {
57 LLDB_INSTRUMENT_VA(this, rhs);
58
59 return m_opaque_ptr != nullptr && rhs.m_opaque_ptr != nullptr &&
60 m_opaque_ptr->GetFunction() == rhs.m_opaque_ptr->GetFunction() &&
61 m_opaque_ptr->GetFunction().GetCompileUnit() ==
64}
65
66bool SBBlock::operator!=(const SBBlock &rhs) const {
67 LLDB_INSTRUMENT_VA(this, rhs);
68
69 return !(*this == rhs);
70}
71
72bool SBBlock::IsInlined() const {
74
75 if (m_opaque_ptr)
76 return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
77 return false;
78}
79
80const char *SBBlock::GetInlinedName() const {
82
83 if (m_opaque_ptr) {
84 const InlineFunctionInfo *inlined_info =
85 m_opaque_ptr->GetInlinedFunctionInfo();
86 if (inlined_info) {
87 return inlined_info->GetName().AsCString(nullptr);
88 }
89 }
90 return nullptr;
91}
92
95
96 SBFileSpec sb_file;
97 if (m_opaque_ptr) {
98 const InlineFunctionInfo *inlined_info =
99 m_opaque_ptr->GetInlinedFunctionInfo();
100 if (inlined_info)
101 sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
102 }
103 return sb_file;
104}
105
107 LLDB_INSTRUMENT_VA(this);
108
109 if (m_opaque_ptr) {
110 const InlineFunctionInfo *inlined_info =
111 m_opaque_ptr->GetInlinedFunctionInfo();
112 if (inlined_info)
113 return inlined_info->GetCallSite().GetLine();
114 }
115 return 0;
116}
117
119 LLDB_INSTRUMENT_VA(this);
120
121 if (m_opaque_ptr) {
122 const InlineFunctionInfo *inlined_info =
123 m_opaque_ptr->GetInlinedFunctionInfo();
124 if (inlined_info)
125 return inlined_info->GetCallSite().GetColumn();
126 }
127 return 0;
128}
129
130void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
131 lldb_private::VariableList *var_list) {
132 if (IsValid()) {
133 bool show_inline = true;
134 m_opaque_ptr->AppendVariables(
135 can_create, get_parent_variables, show_inline,
136 [](Variable *) { return true; }, var_list);
137 }
138}
139
141 LLDB_INSTRUMENT_VA(this);
142
143 SBBlock sb_block;
144 if (m_opaque_ptr)
145 sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
146 return sb_block;
147}
148
150 LLDB_INSTRUMENT_VA(this);
151
152 SBBlock sb_block;
153 if (m_opaque_ptr)
154 sb_block.m_opaque_ptr = m_opaque_ptr->GetContainingInlinedBlock();
155 return sb_block;
156}
157
159 LLDB_INSTRUMENT_VA(this);
160
161 SBBlock sb_block;
162 if (m_opaque_ptr)
163 sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
164 return sb_block;
165}
166
168 LLDB_INSTRUMENT_VA(this);
169
170 SBBlock sb_block;
171 if (m_opaque_ptr)
172 sb_block.m_opaque_ptr = m_opaque_ptr->GetFirstChild();
173 return sb_block;
174}
175
177
179
181 LLDB_INSTRUMENT_VA(this, description);
182
183 Stream &strm = description.ref();
184
185 if (m_opaque_ptr) {
186 lldb::user_id_t id = m_opaque_ptr->GetID();
187 strm.Printf("Block: {id: %" PRIu64 "} ", id);
188 if (IsInlined()) {
189 strm.Printf(" (inlined, '%s') ", GetInlinedName());
190 }
192 m_opaque_ptr->CalculateSymbolContext(&sc);
193 if (sc.function) {
194 m_opaque_ptr->DumpAddressRanges(
195 &strm, sc.function->GetAddress().GetFileAddress());
196 }
197 } else
198 strm.PutCString("No value");
199
200 return true;
201}
202
204 LLDB_INSTRUMENT_VA(this);
205
206 if (m_opaque_ptr)
207 return m_opaque_ptr->GetNumRanges();
208 return 0;
209}
210
212 LLDB_INSTRUMENT_VA(this, idx);
213
214 lldb::SBAddress sb_addr;
215 if (m_opaque_ptr) {
216 AddressRange range;
217 if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
218 sb_addr.ref() = range.GetBaseAddress();
219 }
220 }
221 return sb_addr;
222}
223
225 LLDB_INSTRUMENT_VA(this, idx);
226
227 lldb::SBAddress sb_addr;
228 if (m_opaque_ptr) {
229 AddressRange range;
230 if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
231 sb_addr.ref() = range.GetBaseAddress();
232 sb_addr.ref().Slide(range.GetByteSize());
233 }
234 }
235 return sb_addr;
236}
237
239 LLDB_INSTRUMENT_VA(this);
240
241 lldb::SBAddressRangeList sb_ranges;
242 if (m_opaque_ptr)
243 sb_ranges.m_opaque_up->ref() = m_opaque_ptr->GetRanges();
244 return sb_ranges;
245}
246
248 LLDB_INSTRUMENT_VA(this, block_addr);
249
250 if (m_opaque_ptr && block_addr.IsValid()) {
251 return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
252 }
253
254 return UINT32_MAX;
255}
256
258 bool locals, bool statics,
259 lldb::DynamicValueType use_dynamic) {
260 LLDB_INSTRUMENT_VA(this, frame, arguments, locals, statics, use_dynamic);
261
262 Block *block = GetPtr();
263 SBValueList value_list;
264 if (block) {
265 StackFrameSP frame_sp(frame.GetFrameSP());
266 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
267
268 if (variable_list_sp) {
269 const size_t num_variables = variable_list_sp->GetSize();
270 if (num_variables) {
271 for (size_t i = 0; i < num_variables; ++i) {
272 VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
273 if (variable_sp) {
274 bool add_variable = false;
275 switch (variable_sp->GetScope()) {
279 add_variable = statics;
280 break;
281
283 add_variable = arguments;
284 break;
285
287 add_variable = locals;
288 break;
289
290 default:
291 break;
292 }
293 if (add_variable) {
294 if (frame_sp) {
295 lldb::ValueObjectSP valobj_sp(
296 frame_sp->GetValueObjectForFrameVariable(variable_sp,
298 SBValue value_sb;
299 value_sb.SetSP(valobj_sp, use_dynamic);
300 value_list.Append(value_sb);
301 }
302 }
303 }
304 }
305 }
306 }
307 }
308 return value_list;
309}
310
312 bool locals, bool statics) {
313 LLDB_INSTRUMENT_VA(this, target, arguments, locals, statics);
314
315 Block *block = GetPtr();
316
317 SBValueList value_list;
318 if (block) {
319 TargetSP target_sp(target.GetSP());
320
321 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
322
323 if (variable_list_sp) {
324 const size_t num_variables = variable_list_sp->GetSize();
325 if (num_variables) {
326 for (size_t i = 0; i < num_variables; ++i) {
327 VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
328 if (variable_sp) {
329 bool add_variable = false;
330 switch (variable_sp->GetScope()) {
334 add_variable = statics;
335 break;
336
338 add_variable = arguments;
339 break;
340
342 add_variable = locals;
343 break;
344
345 default:
346 break;
347 }
348 if (add_variable) {
349 if (target_sp)
350 value_list.Append(
351 ValueObjectVariable::Create(target_sp.get(), variable_sp));
352 }
353 }
354 }
355 }
356 }
357 }
358 return value_list;
359}
#define LLDB_INSTRUMENT_VA(...)
std::unique_ptr< lldb_private::AddressRangeListImpl > m_opaque_up
lldb_private::Address & ref()
bool IsValid() const
Definition SBAddress.cpp:72
void AppendVariables(bool can_create, bool get_parent_variables, lldb_private::VariableList *var_list)
Definition SBBlock.cpp:130
lldb::SBAddress GetRangeEndAddress(uint32_t idx)
Definition SBBlock.cpp:224
lldb::SBAddressRangeList GetRanges()
Definition SBBlock.cpp:238
uint32_t GetNumRanges()
Definition SBBlock.cpp:203
const char * GetInlinedName() const
Definition SBBlock.cpp:80
uint32_t GetInlinedCallSiteLine() const
Definition SBBlock.cpp:106
lldb::SBAddress GetRangeStartAddress(uint32_t idx)
Definition SBBlock.cpp:211
bool IsValid() const
Definition SBBlock.cpp:46
const lldb::SBBlock & operator=(const lldb::SBBlock &rhs)
Definition SBBlock.cpp:37
lldb::SBBlock GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition SBBlock.cpp:149
uint32_t GetRangeIndexForBlockAddress(lldb::SBAddress block_addr)
Definition SBBlock.cpp:247
lldb::SBValueList GetVariables(lldb::SBFrame &frame, bool arguments, bool locals, bool statics, lldb::DynamicValueType use_dynamic)
Definition SBBlock.cpp:257
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition SBBlock.cpp:178
lldb::SBBlock GetParent()
Definition SBBlock.cpp:140
lldb::SBBlock GetFirstChild()
Definition SBBlock.cpp:167
bool IsInlined() const
Definition SBBlock.cpp:72
uint32_t GetInlinedCallSiteColumn() const
Definition SBBlock.cpp:118
bool operator==(const lldb::SBBlock &rhs) const
Definition SBBlock.cpp:56
lldb::SBBlock GetSibling()
Definition SBBlock.cpp:158
lldb::SBFileSpec GetInlinedCallSiteFile() const
Definition SBBlock.cpp:93
lldb_private::Block * m_opaque_ptr
Definition SBBlock.h:99
lldb_private::Block * GetPtr()
Definition SBBlock.cpp:176
bool operator!=(const lldb::SBBlock &rhs) const
Definition SBBlock.cpp:66
bool GetDescription(lldb::SBStream &description)
Definition SBBlock.cpp:180
void SetFileSpec(const lldb_private::FileSpec &fspec)
lldb::StackFrameSP GetFrameSP() const
Definition SBFrame.cpp:86
lldb_private::Stream & ref()
Definition SBStream.cpp:178
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:589
void Append(const lldb::SBValue &val_obj)
void SetSP(const lldb::ValueObjectSP &sp)
Definition SBValue.cpp:951
A section + offset based address range class.
Address & GetBaseAddress()
Get accessor for the base address of the range.
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
bool Slide(int64_t offset)
Definition Address.h:452
lldb::addr_t GetFileAddress() const
Get the file address.
Definition Address.cpp:281
A class that describes a single lexical block.
Definition Block.h:41
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition Block.cpp:392
Function & GetFunction()
Definition Block.cpp:156
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
uint32_t GetLine() const
Get accessor for the declaration line number.
uint16_t GetColumn() const
Get accessor for the declaration column number.
FileSpec & GetFile()
Get accessor for file specification.
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:453
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition Function.cpp:398
A class that describes information for an inlined function.
Definition Function.h:126
Declaration & GetCallSite()
Get accessor for the call site declaration information.
Definition Function.cpp:109
ConstString GetName() const
Definition Function.cpp:97
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.
Function * function
The Function for a given query.
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
#define UINT32_MAX
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::Target > TargetSP
@ eValueTypeVariableGlobal
globals variable
@ eValueTypeVariableLocal
function local variables
@ eValueTypeVariableArgument
function argument variables
@ eValueTypeVariableStatic
static variable
@ eValueTypeVariableThreadLocal
thread local storage variable