1 //===-- SBBlock.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
16 #include "lldb/Core/ValueObjectVariable.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/VariableList.h"
21 #include "lldb/Target/StackFrame.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/Instrumentation.h"
26 using namespace lldb_private
;
28 SBBlock::SBBlock() { LLDB_INSTRUMENT_VA(this); }
30 SBBlock::SBBlock(lldb_private::Block
*lldb_object_ptr
)
31 : m_opaque_ptr(lldb_object_ptr
) {}
33 SBBlock::SBBlock(const SBBlock
&rhs
) : m_opaque_ptr(rhs
.m_opaque_ptr
) {
34 LLDB_INSTRUMENT_VA(this, rhs
);
37 const SBBlock
&SBBlock::operator=(const SBBlock
&rhs
) {
38 LLDB_INSTRUMENT_VA(this, rhs
);
40 m_opaque_ptr
= rhs
.m_opaque_ptr
;
44 SBBlock::~SBBlock() { m_opaque_ptr
= nullptr; }
46 bool SBBlock::IsValid() const {
47 LLDB_INSTRUMENT_VA(this);
48 return this->operator bool();
50 SBBlock::operator bool() const {
51 LLDB_INSTRUMENT_VA(this);
53 return m_opaque_ptr
!= nullptr;
56 bool SBBlock::IsInlined() const {
57 LLDB_INSTRUMENT_VA(this);
60 return m_opaque_ptr
->GetInlinedFunctionInfo() != nullptr;
64 const char *SBBlock::GetInlinedName() const {
65 LLDB_INSTRUMENT_VA(this);
68 const InlineFunctionInfo
*inlined_info
=
69 m_opaque_ptr
->GetInlinedFunctionInfo();
71 return inlined_info
->GetName().AsCString(nullptr);
77 SBFileSpec
SBBlock::GetInlinedCallSiteFile() const {
78 LLDB_INSTRUMENT_VA(this);
82 const InlineFunctionInfo
*inlined_info
=
83 m_opaque_ptr
->GetInlinedFunctionInfo();
85 sb_file
.SetFileSpec(inlined_info
->GetCallSite().GetFile());
90 uint32_t SBBlock::GetInlinedCallSiteLine() const {
91 LLDB_INSTRUMENT_VA(this);
94 const InlineFunctionInfo
*inlined_info
=
95 m_opaque_ptr
->GetInlinedFunctionInfo();
97 return inlined_info
->GetCallSite().GetLine();
102 uint32_t SBBlock::GetInlinedCallSiteColumn() const {
103 LLDB_INSTRUMENT_VA(this);
106 const InlineFunctionInfo
*inlined_info
=
107 m_opaque_ptr
->GetInlinedFunctionInfo();
109 return inlined_info
->GetCallSite().GetColumn();
114 void SBBlock::AppendVariables(bool can_create
, bool get_parent_variables
,
115 lldb_private::VariableList
*var_list
) {
117 bool show_inline
= true;
118 m_opaque_ptr
->AppendVariables(can_create
, get_parent_variables
, show_inline
,
119 [](Variable
*) { return true; }, var_list
);
123 SBBlock
SBBlock::GetParent() {
124 LLDB_INSTRUMENT_VA(this);
128 sb_block
.m_opaque_ptr
= m_opaque_ptr
->GetParent();
132 lldb::SBBlock
SBBlock::GetContainingInlinedBlock() {
133 LLDB_INSTRUMENT_VA(this);
137 sb_block
.m_opaque_ptr
= m_opaque_ptr
->GetContainingInlinedBlock();
141 SBBlock
SBBlock::GetSibling() {
142 LLDB_INSTRUMENT_VA(this);
146 sb_block
.m_opaque_ptr
= m_opaque_ptr
->GetSibling();
150 SBBlock
SBBlock::GetFirstChild() {
151 LLDB_INSTRUMENT_VA(this);
155 sb_block
.m_opaque_ptr
= m_opaque_ptr
->GetFirstChild();
159 lldb_private::Block
*SBBlock::GetPtr() { return m_opaque_ptr
; }
161 void SBBlock::SetPtr(lldb_private::Block
*block
) { m_opaque_ptr
= block
; }
163 bool SBBlock::GetDescription(SBStream
&description
) {
164 LLDB_INSTRUMENT_VA(this, description
);
166 Stream
&strm
= description
.ref();
169 lldb::user_id_t id
= m_opaque_ptr
->GetID();
170 strm
.Printf("Block: {id: %" PRIu64
"} ", id
);
172 strm
.Printf(" (inlined, '%s') ", GetInlinedName());
174 lldb_private::SymbolContext sc
;
175 m_opaque_ptr
->CalculateSymbolContext(&sc
);
177 m_opaque_ptr
->DumpAddressRanges(
179 sc
.function
->GetAddressRange().GetBaseAddress().GetFileAddress());
182 strm
.PutCString("No value");
187 uint32_t SBBlock::GetNumRanges() {
188 LLDB_INSTRUMENT_VA(this);
191 return m_opaque_ptr
->GetNumRanges();
195 lldb::SBAddress
SBBlock::GetRangeStartAddress(uint32_t idx
) {
196 LLDB_INSTRUMENT_VA(this, idx
);
198 lldb::SBAddress sb_addr
;
201 if (m_opaque_ptr
->GetRangeAtIndex(idx
, range
)) {
202 sb_addr
.ref() = range
.GetBaseAddress();
208 lldb::SBAddress
SBBlock::GetRangeEndAddress(uint32_t idx
) {
209 LLDB_INSTRUMENT_VA(this, idx
);
211 lldb::SBAddress sb_addr
;
214 if (m_opaque_ptr
->GetRangeAtIndex(idx
, range
)) {
215 sb_addr
.ref() = range
.GetBaseAddress();
216 sb_addr
.ref().Slide(range
.GetByteSize());
222 uint32_t SBBlock::GetRangeIndexForBlockAddress(lldb::SBAddress block_addr
) {
223 LLDB_INSTRUMENT_VA(this, block_addr
);
225 if (m_opaque_ptr
&& block_addr
.IsValid()) {
226 return m_opaque_ptr
->GetRangeIndexContainingAddress(block_addr
.ref());
232 lldb::SBValueList
SBBlock::GetVariables(lldb::SBFrame
&frame
, bool arguments
,
233 bool locals
, bool statics
,
234 lldb::DynamicValueType use_dynamic
) {
235 LLDB_INSTRUMENT_VA(this, frame
, arguments
, locals
, statics
, use_dynamic
);
237 Block
*block
= GetPtr();
238 SBValueList value_list
;
240 StackFrameSP
frame_sp(frame
.GetFrameSP());
241 VariableListSP
variable_list_sp(block
->GetBlockVariableList(true));
243 if (variable_list_sp
) {
244 const size_t num_variables
= variable_list_sp
->GetSize();
246 for (size_t i
= 0; i
< num_variables
; ++i
) {
247 VariableSP
variable_sp(variable_list_sp
->GetVariableAtIndex(i
));
249 bool add_variable
= false;
250 switch (variable_sp
->GetScope()) {
251 case eValueTypeVariableGlobal
:
252 case eValueTypeVariableStatic
:
253 case eValueTypeVariableThreadLocal
:
254 add_variable
= statics
;
257 case eValueTypeVariableArgument
:
258 add_variable
= arguments
;
261 case eValueTypeVariableLocal
:
262 add_variable
= locals
;
270 lldb::ValueObjectSP
valobj_sp(
271 frame_sp
->GetValueObjectForFrameVariable(variable_sp
,
274 value_sb
.SetSP(valobj_sp
, use_dynamic
);
275 value_list
.Append(value_sb
);
286 lldb::SBValueList
SBBlock::GetVariables(lldb::SBTarget
&target
, bool arguments
,
287 bool locals
, bool statics
) {
288 LLDB_INSTRUMENT_VA(this, target
, arguments
, locals
, statics
);
290 Block
*block
= GetPtr();
292 SBValueList value_list
;
294 TargetSP
target_sp(target
.GetSP());
296 VariableListSP
variable_list_sp(block
->GetBlockVariableList(true));
298 if (variable_list_sp
) {
299 const size_t num_variables
= variable_list_sp
->GetSize();
301 for (size_t i
= 0; i
< num_variables
; ++i
) {
302 VariableSP
variable_sp(variable_list_sp
->GetVariableAtIndex(i
));
304 bool add_variable
= false;
305 switch (variable_sp
->GetScope()) {
306 case eValueTypeVariableGlobal
:
307 case eValueTypeVariableStatic
:
308 case eValueTypeVariableThreadLocal
:
309 add_variable
= statics
;
312 case eValueTypeVariableArgument
:
313 add_variable
= arguments
;
316 case eValueTypeVariableLocal
:
317 add_variable
= locals
;
326 ValueObjectVariable::Create(target_sp
.get(), variable_sp
));