blob: a5fee445d5c6a003fa100055ed5b5928315b78c3 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- SBBlock.cpp -------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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
Konrad Kleine248a1302019-05-23 11:14:47 +000028SBBlock::SBBlock() : m_opaque_ptr(nullptr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000029 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
Konrad Kleine248a1302019-05-23 11:14:47 +000047SBBlock::~SBBlock() { m_opaque_ptr = nullptr; }
Kate Stoneb9c1b512016-09-06 20:57:50 +000048
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
Konrad Kleine248a1302019-05-23 11:14:47 +000056 return m_opaque_ptr != nullptr;
Jonas Devliegherebaf56642019-03-06 00:06:00 +000057}
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)
Konrad Kleine248a1302019-05-23 11:14:47 +000063 return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 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) {
Alex Langford22b04482020-01-30 21:55:18 -080074 return inlined_info->GetName().AsCString(nullptr);
Greg Clayton95897c62010-09-07 04:20:48 +000075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 }
Konrad Kleine248a1302019-05-23 11:14:47 +000077 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080SBFileSpec SBBlock::GetInlinedCallSiteFile() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000081 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBBlock,
82 GetInlinedCallSiteFile);
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 SBFileSpec sb_file;
85 if (m_opaque_ptr) {
86 const InlineFunctionInfo *inlined_info =
87 m_opaque_ptr->GetInlinedFunctionInfo();
88 if (inlined_info)
89 sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());
90 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000091 return LLDB_RECORD_RESULT(sb_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +000092}
93
94uint32_t SBBlock::GetInlinedCallSiteLine() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000095 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBBlock, GetInlinedCallSiteLine);
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 if (m_opaque_ptr) {
98 const InlineFunctionInfo *inlined_info =
99 m_opaque_ptr->GetInlinedFunctionInfo();
100 if (inlined_info)
101 return inlined_info->GetCallSite().GetLine();
102 }
103 return 0;
104}
105
106uint32_t SBBlock::GetInlinedCallSiteColumn() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000107 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBBlock, GetInlinedCallSiteColumn);
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 if (m_opaque_ptr) {
110 const InlineFunctionInfo *inlined_info =
111 m_opaque_ptr->GetInlinedFunctionInfo();
112 if (inlined_info)
113 return inlined_info->GetCallSite().GetColumn();
114 }
115 return 0;
116}
117
118void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,
119 lldb_private::VariableList *var_list) {
120 if (IsValid()) {
121 bool show_inline = true;
122 m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,
123 [](Variable *) { return true; }, var_list);
124 }
125}
126
127SBBlock SBBlock::GetParent() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000128 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetParent);
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 SBBlock sb_block;
131 if (m_opaque_ptr)
132 sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000133 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134}
135
136lldb::SBBlock SBBlock::GetContainingInlinedBlock() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000137 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetContainingInlinedBlock);
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 SBBlock sb_block;
140 if (m_opaque_ptr)
141 sb_block.m_opaque_ptr = m_opaque_ptr->GetContainingInlinedBlock();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000142 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143}
144
145SBBlock SBBlock::GetSibling() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000146 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetSibling);
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 SBBlock sb_block;
149 if (m_opaque_ptr)
150 sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000151 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152}
153
154SBBlock SBBlock::GetFirstChild() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000155 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBBlock, GetFirstChild);
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 SBBlock sb_block;
158 if (m_opaque_ptr)
159 sb_block.m_opaque_ptr = m_opaque_ptr->GetFirstChild();
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000160 return LLDB_RECORD_RESULT(sb_block);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161}
162
163lldb_private::Block *SBBlock::GetPtr() { return m_opaque_ptr; }
164
165void SBBlock::SetPtr(lldb_private::Block *block) { m_opaque_ptr = block; }
166
167bool SBBlock::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000168 LLDB_RECORD_METHOD(bool, SBBlock, GetDescription, (lldb::SBStream &),
169 description);
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 Stream &strm = description.ref();
172
173 if (m_opaque_ptr) {
174 lldb::user_id_t id = m_opaque_ptr->GetID();
175 strm.Printf("Block: {id: %" PRIu64 "} ", id);
176 if (IsInlined()) {
177 strm.Printf(" (inlined, '%s') ", GetInlinedName());
Greg Clayton95897c62010-09-07 04:20:48 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 lldb_private::SymbolContext sc;
180 m_opaque_ptr->CalculateSymbolContext(&sc);
181 if (sc.function) {
182 m_opaque_ptr->DumpAddressRanges(
183 &strm,
184 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress());
Greg Clayton95897c62010-09-07 04:20:48 +0000185 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 } else
187 strm.PutCString("No value");
188
189 return true;
Greg Clayton95897c62010-09-07 04:20:48 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192uint32_t SBBlock::GetNumRanges() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000193 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBBlock, GetNumRanges);
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 if (m_opaque_ptr)
196 return m_opaque_ptr->GetNumRanges();
197 return 0;
198}
199
200lldb::SBAddress SBBlock::GetRangeStartAddress(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000201 LLDB_RECORD_METHOD(lldb::SBAddress, SBBlock, GetRangeStartAddress, (uint32_t),
202 idx);
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 lldb::SBAddress sb_addr;
205 if (m_opaque_ptr) {
206 AddressRange range;
207 if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
208 sb_addr.ref() = range.GetBaseAddress();
Greg Clayton95897c62010-09-07 04:20:48 +0000209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000211 return LLDB_RECORD_RESULT(sb_addr);
Greg Clayton95897c62010-09-07 04:20:48 +0000212}
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214lldb::SBAddress SBBlock::GetRangeEndAddress(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000215 LLDB_RECORD_METHOD(lldb::SBAddress, SBBlock, GetRangeEndAddress, (uint32_t),
216 idx);
217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 lldb::SBAddress sb_addr;
219 if (m_opaque_ptr) {
220 AddressRange range;
221 if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {
222 sb_addr.ref() = range.GetBaseAddress();
223 sb_addr.ref().Slide(range.GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000226 return LLDB_RECORD_RESULT(sb_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227}
228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229uint32_t SBBlock::GetRangeIndexForBlockAddress(lldb::SBAddress block_addr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000230 LLDB_RECORD_METHOD(uint32_t, SBBlock, GetRangeIndexForBlockAddress,
231 (lldb::SBAddress), block_addr);
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 if (m_opaque_ptr && block_addr.IsValid()) {
234 return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());
235 }
236
237 return UINT32_MAX;
Greg Clayton95897c62010-09-07 04:20:48 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240lldb::SBValueList SBBlock::GetVariables(lldb::SBFrame &frame, bool arguments,
241 bool locals, bool statics,
242 lldb::DynamicValueType use_dynamic) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000243 LLDB_RECORD_METHOD(
244 lldb::SBValueList, SBBlock, GetVariables,
245 (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType), frame,
246 arguments, locals, statics, use_dynamic);
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 Block *block = GetPtr();
249 SBValueList value_list;
250 if (block) {
251 StackFrameSP frame_sp(frame.GetFrameSP());
252 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
Greg Clayton8f7180b2011-09-26 07:11:27 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 if (variable_list_sp) {
255 const size_t num_variables = variable_list_sp->GetSize();
256 if (num_variables) {
257 for (size_t i = 0; i < num_variables; ++i) {
258 VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
259 if (variable_sp) {
260 bool add_variable = false;
261 switch (variable_sp->GetScope()) {
262 case eValueTypeVariableGlobal:
263 case eValueTypeVariableStatic:
264 case eValueTypeVariableThreadLocal:
265 add_variable = statics;
266 break;
Greg Clayton95897c62010-09-07 04:20:48 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 case eValueTypeVariableArgument:
269 add_variable = arguments;
270 break;
Greg Clayton95897c62010-09-07 04:20:48 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 case eValueTypeVariableLocal:
273 add_variable = locals;
274 break;
Greg Clayton48381312010-10-30 04:51:46 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 default:
277 break;
Greg Clayton5569e642012-02-06 01:44:54 +0000278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (add_variable) {
280 if (frame_sp) {
281 lldb::ValueObjectSP valobj_sp(
282 frame_sp->GetValueObjectForFrameVariable(variable_sp,
283 eNoDynamicValues));
284 SBValue value_sb;
285 value_sb.SetSP(valobj_sp, use_dynamic);
286 value_list.Append(value_sb);
287 }
Greg Clayton5569e642012-02-06 01:44:54 +0000288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 }
Greg Clayton5569e642012-02-06 01:44:54 +0000290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 }
292 }
293 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000294 return LLDB_RECORD_RESULT(value_list);
Greg Clayton5569e642012-02-06 01:44:54 +0000295}
296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297lldb::SBValueList SBBlock::GetVariables(lldb::SBTarget &target, bool arguments,
298 bool locals, bool statics) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000299 LLDB_RECORD_METHOD(lldb::SBValueList, SBBlock, GetVariables,
300 (lldb::SBTarget &, bool, bool, bool), target, arguments,
301 locals, statics);
302
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 Block *block = GetPtr();
304
305 SBValueList value_list;
306 if (block) {
307 TargetSP target_sp(target.GetSP());
308
309 VariableListSP variable_list_sp(block->GetBlockVariableList(true));
310
311 if (variable_list_sp) {
312 const size_t num_variables = variable_list_sp->GetSize();
313 if (num_variables) {
314 for (size_t i = 0; i < num_variables; ++i) {
315 VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));
316 if (variable_sp) {
317 bool add_variable = false;
318 switch (variable_sp->GetScope()) {
319 case eValueTypeVariableGlobal:
320 case eValueTypeVariableStatic:
321 case eValueTypeVariableThreadLocal:
322 add_variable = statics;
323 break;
324
325 case eValueTypeVariableArgument:
326 add_variable = arguments;
327 break;
328
329 case eValueTypeVariableLocal:
330 add_variable = locals;
331 break;
332
333 default:
334 break;
335 }
336 if (add_variable) {
337 if (target_sp)
338 value_list.Append(
339 ValueObjectVariable::Create(target_sp.get(), variable_sp));
340 }
341 }
342 }
343 }
344 }
345 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000346 return LLDB_RECORD_RESULT(value_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347}
Michal Gornyae211ec2019-03-19 17:13:13 +0000348
349namespace lldb_private {
350namespace repro {
351
352template <>
353void RegisterMethods<SBBlock>(Registry &R) {
354 LLDB_REGISTER_CONSTRUCTOR(SBBlock, ());
355 LLDB_REGISTER_CONSTRUCTOR(SBBlock, (const lldb::SBBlock &));
356 LLDB_REGISTER_METHOD(const lldb::SBBlock &,
357 SBBlock, operator=,(const lldb::SBBlock &));
358 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsValid, ());
359 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, operator bool, ());
360 LLDB_REGISTER_METHOD_CONST(bool, SBBlock, IsInlined, ());
361 LLDB_REGISTER_METHOD_CONST(const char *, SBBlock, GetInlinedName, ());
362 LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBBlock,
363 GetInlinedCallSiteFile, ());
364 LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteLine, ());
365 LLDB_REGISTER_METHOD_CONST(uint32_t, SBBlock, GetInlinedCallSiteColumn, ());
366 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetParent, ());
367 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetContainingInlinedBlock, ());
368 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetSibling, ());
369 LLDB_REGISTER_METHOD(lldb::SBBlock, SBBlock, GetFirstChild, ());
370 LLDB_REGISTER_METHOD(bool, SBBlock, GetDescription, (lldb::SBStream &));
371 LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetNumRanges, ());
372 LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeStartAddress,
373 (uint32_t));
374 LLDB_REGISTER_METHOD(lldb::SBAddress, SBBlock, GetRangeEndAddress,
375 (uint32_t));
376 LLDB_REGISTER_METHOD(uint32_t, SBBlock, GetRangeIndexForBlockAddress,
377 (lldb::SBAddress));
378 LLDB_REGISTER_METHOD(
379 lldb::SBValueList, SBBlock, GetVariables,
380 (lldb::SBFrame &, bool, bool, bool, lldb::DynamicValueType));
381 LLDB_REGISTER_METHOD(lldb::SBValueList, SBBlock, GetVariables,
382 (lldb::SBTarget &, bool, bool, bool));
383}
384
385}
386}