blob: b8ca473894fc71e342467a05454b77e1bedd6b4a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBBlock.cpp ---------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBBlock.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Greg Clayton8f7180b2011-09-26 07:11:27 +000011#include "lldb/API/SBAddress.h"
Greg Clayton95897c62010-09-07 04:20:48 +000012#include "lldb/API/SBFileSpec.h"
Greg Clayton5569e642012-02-06 01:44:54 +000013#include "lldb/API/SBFrame.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Greg Clayton5569e642012-02-06 01:44:54 +000015#include "lldb/API/SBValue.h"
Greg Clayton8f7180b2011-09-26 07:11:27 +000016#include "lldb/Core/AddressRange.h"
Greg Clayton5569e642012-02-06 01:44:54 +000017#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Symbol/Block.h"
Greg Clayton95897c62010-09-07 04:20:48 +000019#include "lldb/Symbol/Function.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000020#include "lldb/Symbol/SymbolContext.h"
Greg Clayton5569e642012-02-06 01:44:54 +000021#include "lldb/Symbol/VariableList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000022#include "lldb/Target/StackFrame.h"
Greg Clayton5569e642012-02-06 01:44:54 +000023#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25using namespace lldb;
Greg Clayton95897c62010-09-07 04:20:48 +000026using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Jonas Devliegherebaf56642019-03-06 00:06:00 +000028SBBlock::SBBlock() : m_opaque_ptr(NULL) {
29 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBBlock);
30}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032SBBlock::SBBlock(lldb_private::Block *lldb_object_ptr)
33 : m_opaque_ptr(lldb_object_ptr) {}
34
Jonas Devliegherebaf56642019-03-06 00:06:00 +000035SBBlock::SBBlock(const SBBlock &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
36 LLDB_RECORD_CONSTRUCTOR(SBBlock, (const lldb::SBBlock &), rhs);
37}
Kate Stoneb9c1b512016-09-06 20:57:50 +000038
39const SBBlock &SBBlock::operator=(const SBBlock &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000040 LLDB_RECORD_METHOD(const lldb::SBBlock &,
41 SBBlock, operator=,(const lldb::SBBlock &), rhs);
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 m_opaque_ptr = rhs.m_opaque_ptr;
Jonas Devlieghere306809f2019-04-03 21:31:22 +000044 return LLDB_RECORD_RESULT(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047SBBlock::~SBBlock() { m_opaque_ptr = NULL; }
48
Jonas Devliegherebaf56642019-03-06 00:06:00 +000049bool SBBlock::IsValid() const {
50 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBBlock, IsValid);
Pavel Labath7f5237b2019-03-11 13:58:46 +000051 return this->operator bool();
52}
53SBBlock::operator bool() const {
54 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBBlock, operator bool);
Jonas Devliegherebaf56642019-03-06 00:06:00 +000055
56 return m_opaque_ptr != NULL;
57}
Kate Stoneb9c1b512016-09-06 20:57:50 +000058
59bool SBBlock::IsInlined() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000060 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBBlock, IsInlined);
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 if (m_opaque_ptr)
63 return m_opaque_ptr->GetInlinedFunctionInfo() != NULL;
64 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067const char *SBBlock::GetInlinedName() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000068 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBBlock, GetInlinedName);
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 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);
Greg Clayton95897c62010-09-07 04:20:48 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
83 return NULL;
Greg Clayton95897c62010-09-07 04:20:48 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086SBFileSpec SBBlock::GetInlinedCallSiteFile() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000087 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBBlock,
88 GetInlinedCallSiteFile);
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 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 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000097 return LLDB_RECORD_RESULT(sb_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +000098}
99
100uint32_t SBBlock::GetInlinedCallSiteLine() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000101 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBBlock, GetInlinedCallSiteLine);
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 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
112uint32_t SBBlock::GetInlinedCallSiteColumn() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000113 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBBlock, GetInlinedCallSiteColumn);
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 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
124void 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
133SBBlock SBBlock::GetParent() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000134 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetParent);
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 SBBlock sb_block;
137 if (m_opaque_ptr)
138 sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000139 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140}
141
142lldb::SBBlock SBBlock::GetContainingInlinedBlock() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000143 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetContainingInlinedBlock);
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 SBBlock sb_block;
146 if (m_opaque_ptr)
147 sb_block.m_opaque_ptr = m_opaque_ptr->GetContainingInlinedBlock();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000148 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149}
150
151SBBlock SBBlock::GetSibling() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000152 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetSibling);
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 SBBlock sb_block;
155 if (m_opaque_ptr)
156 sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000157 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158}
159
160SBBlock SBBlock::GetFirstChild() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000161 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetFirstChild);
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 SBBlock sb_block;
164 if (m_opaque_ptr)
165 sb_block.m_opaque_ptr = m_opaque_ptr->GetFirstChild();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000166 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167}
168
169lldb_private::Block *SBBlock::GetPtr() { return m_opaque_ptr; }
170
171void SBBlock::SetPtr(lldb_private::Block *block) { m_opaque_ptr = block; }
172
173bool SBBlock::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000174 LLDB_RECORD_METHOD(bool, SBBlock, GetDescription, (lldb::SBStream &),
175 description);
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 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());
Greg Clayton95897c62010-09-07 04:20:48 +0000184 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 lldb_private::SymbolContext sc;
186 m_opaque_ptr->CalculateSymbolContext(&sc);
187 if (sc.function) {
188 m_opaque_ptr->DumpAddressRanges(
189 &strm,
190 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress());
Greg Clayton95897c62010-09-07 04:20:48 +0000191 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 } else
193 strm.PutCString("No value");
194
195 return true;
Greg Clayton95897c62010-09-07 04:20:48 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198uint32_t SBBlock::GetNumRanges() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000199 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBBlock, GetNumRanges);
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (m_opaque_ptr)
202 return m_opaque_ptr->GetNumRanges();
203 return 0;
204}
205
206lldb::SBAddress SBBlock::GetRangeStartAddress(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000207 LLDB_RECORD_METHOD(lldb::SBAddress, SBBlock, GetRangeStartAddress, (uint32_t),
208 idx);
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 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();
Greg Clayton95897c62010-09-07 04:20:48 +0000215 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000217 return LLDB_RECORD_RESULT(sb_addr);
Greg Clayton95897c62010-09-07 04:20:48 +0000218}
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220lldb::SBAddress SBBlock::GetRangeEndAddress(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000221 LLDB_RECORD_METHOD(lldb::SBAddress, SBBlock, GetRangeEndAddress, (uint32_t),
222 idx);
223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 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());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000232 return LLDB_RECORD_RESULT(sb_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235uint32_t SBBlock::GetRangeIndexForBlockAddress(lldb::SBAddress block_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000236 LLDB_RECORD_METHOD(uint32_t, SBBlock, GetRangeIndexForBlockAddress,
237 (lldb::SBAddress), block_addr);
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 if (m_opaque_ptr && block_addr.IsValid()) {
240 return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
241 }
242
243 return UINT32_MAX;
Greg Clayton95897c62010-09-07 04:20:48 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246lldb::SBValueList SBBlock::GetVariables(lldb::SBFrame &frame, bool arguments,
247 bool locals, bool statics,
248 lldb::DynamicValueType use_dynamic) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000249 LLDB_RECORD_METHOD(
250 lldb::SBValueList, SBBlock, GetVariables,
251 (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType), frame,
252 arguments, locals, statics, use_dynamic);
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 Block *block = GetPtr();
255 SBValueList value_list;
256 if (block) {
257 StackFrameSP frame_sp(frame.GetFrameSP());
258 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
Greg Clayton8f7180b2011-09-26 07:11:27 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 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()) {
268 case eValueTypeVariableGlobal:
269 case eValueTypeVariableStatic:
270 case eValueTypeVariableThreadLocal:
271 add_variable = statics;
272 break;
Greg Clayton95897c62010-09-07 04:20:48 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 case eValueTypeVariableArgument:
275 add_variable = arguments;
276 break;
Greg Clayton95897c62010-09-07 04:20:48 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 case eValueTypeVariableLocal:
279 add_variable = locals;
280 break;
Greg Clayton48381312010-10-30 04:51:46 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 default:
283 break;
Greg Clayton5569e642012-02-06 01:44:54 +0000284 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 if (add_variable) {
286 if (frame_sp) {
287 lldb::ValueObjectSP valobj_sp(
288 frame_sp->GetValueObjectForFrameVariable(variable_sp,
289 eNoDynamicValues));
290 SBValue value_sb;
291 value_sb.SetSP(valobj_sp, use_dynamic);
292 value_list.Append(value_sb);
293 }
Greg Clayton5569e642012-02-06 01:44:54 +0000294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 }
Greg Clayton5569e642012-02-06 01:44:54 +0000296 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 }
298 }
299 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000300 return LLDB_RECORD_RESULT(value_list);
Greg Clayton5569e642012-02-06 01:44:54 +0000301}
302
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303lldb::SBValueList SBBlock::GetVariables(lldb::SBTarget &target, bool arguments,
304 bool locals, bool statics) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000305 LLDB_RECORD_METHOD(lldb::SBValueList, SBBlock, GetVariables,
306 (lldb::SBTarget &, bool, bool, bool), target, arguments,
307 locals, statics);
308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 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()) {
325 case eValueTypeVariableGlobal:
326 case eValueTypeVariableStatic:
327 case eValueTypeVariableThreadLocal:
328 add_variable = statics;
329 break;
330
331 case eValueTypeVariableArgument:
332 add_variable = arguments;
333 break;
334
335 case eValueTypeVariableLocal:
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 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000352 return LLDB_RECORD_RESULT(value_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353}
Michal Gornyae211ec2019-03-19 17:13:13 +0000354
355namespace lldb_private {
356namespace repro {
357
358template <>
359void RegisterMethods<SBBlock>(Registry &R) {
360 LLDB_REGISTER_CONSTRUCTOR(SBBlock, ());
361 LLDB_REGISTER_CONSTRUCTOR(SBBlock, (const lldb::SBBlock &));
362 LLDB_REGISTER_METHOD(const lldb::SBBlock &,
363 SBBlock, operator=,(const lldb::SBBlock &));
364 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsValid, ());
365 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, operator bool, ());
366 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsInlined, ());
367 LLDB_REGISTER_METHOD_CONST(const char *, SBBlock, GetInlinedName, ());
368 LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBBlock,
369 GetInlinedCallSiteFile, ());
370 LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteLine, ());
371 LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteColumn, ());
372 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetParent, ());
373 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetContainingInlinedBlock, ());
374 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetSibling, ());
375 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetFirstChild, ());
376 LLDB_REGISTER_METHOD(bool, SBBlock, GetDescription, (lldb::SBStream &));
377 LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetNumRanges, ());
378 LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeStartAddress,
379 (uint32_t));
380 LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeEndAddress,
381 (uint32_t));
382 LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetRangeIndexForBlockAddress,
383 (lldb::SBAddress));
384 LLDB_REGISTER_METHOD(
385 lldb::SBValueList, SBBlock, GetVariables,
386 (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType));
387 LLDB_REGISTER_METHOD(lldb::SBValueList, SBBlock, GetVariables,
388 (lldb::SBTarget &, bool, bool, bool));
389}
390
391}
392}