blob: acc284de75e51b291c98894dd0297df9a05a5cfe [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Block.cpp -----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/Block.h"
Greg Clayton6c7f5612011-09-29 23:41:34 +000011
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
Greg Clayton6c7f5612011-09-29 23:41:34 +000014#include "lldb/Symbol/Function.h"
Sean Callanan72e49402011-08-05 23:43:37 +000015#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Symbol/SymbolVendor.h"
17#include "lldb/Symbol/VariableList.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023Block::Block(lldb::user_id_t uid)
24 : UserID(uid), m_parent_scope(nullptr), m_children(), m_ranges(),
25 m_inlineInfoSP(), m_variable_list_sp(), m_parsed_block_info(false),
26 m_parsed_block_variables(false), m_parsed_child_blocks(false) {}
27
28Block::~Block() {}
29
30void Block::GetDescription(Stream *s, Function *function,
31 lldb::DescriptionLevel level, Target *target) const {
32 *s << "id = " << ((const UserID &)*this);
33
34 size_t num_ranges = m_ranges.GetSize();
35 if (num_ranges > 0) {
36
37 addr_t base_addr = LLDB_INVALID_ADDRESS;
38 if (target)
39 base_addr =
40 function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
41 if (base_addr == LLDB_INVALID_ADDRESS)
42 base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
43
44 s->Printf(", range%s = ", num_ranges > 1 ? "s" : "");
45 for (size_t i = 0; i < num_ranges; ++i) {
46 const Range &range = m_ranges.GetEntryRef(i);
47 s->AddressRange(base_addr + range.GetRangeBase(),
48 base_addr + range.GetRangeEnd(), 4);
49 }
50 }
51
52 if (m_inlineInfoSP.get() != nullptr) {
53 bool show_fullpaths = (level == eDescriptionLevelVerbose);
54 m_inlineInfoSP->Dump(s, show_fullpaths);
55 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058void Block::Dump(Stream *s, addr_t base_addr, int32_t depth,
59 bool show_context) const {
60 if (depth < 0) {
61 Block *parent = GetParent();
62 if (parent) {
Adrian Prantl05097242018-04-30 16:49:04 +000063 // We have a depth that is less than zero, print our parent blocks first
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 parent->Dump(s, base_addr, depth + 1, show_context);
65 }
66 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 s->Printf("%p: ", static_cast<const void *>(this));
69 s->Indent();
70 *s << "Block" << static_cast<const UserID &>(*this);
71 const Block *parent_block = GetParent();
72 if (parent_block) {
73 s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID());
74 }
75 if (m_inlineInfoSP.get() != nullptr) {
76 bool show_fullpaths = false;
77 m_inlineInfoSP->Dump(s, show_fullpaths);
78 }
79
80 if (!m_ranges.IsEmpty()) {
81 *s << ", ranges =";
Greg Claytonc9800662010-09-10 01:30:46 +000082
Greg Claytonea3e7d52011-10-08 00:49:15 +000083 size_t num_ranges = m_ranges.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 for (size_t i = 0; i < num_ranges; ++i) {
85 const Range &range = m_ranges.GetEntryRef(i);
86 if (parent_block != nullptr && parent_block->Contains(range) == false)
87 *s << '!';
88 else
89 *s << ' ';
90 s->AddressRange(base_addr + range.GetRangeBase(),
91 base_addr + range.GetRangeEnd(), 4);
92 }
93 }
94 s->EOL();
Greg Clayton0c5cd902010-06-28 21:30:43 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 if (depth > 0) {
97 s->IndentMore();
98
99 if (m_variable_list_sp.get()) {
100 m_variable_list_sp->Dump(s, show_context);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000101 }
Greg Clayton0c5cd902010-06-28 21:30:43 +0000102
Greg Clayton624784a2011-09-29 21:19:25 +0000103 collection::const_iterator pos, end = m_children.end();
104 for (pos = m_children.begin(); pos != end; ++pos)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 (*pos)->Dump(s, base_addr, depth - 1, show_context);
106
107 s->IndentLess();
108 }
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000109}
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111Block *Block::FindBlockByID(user_id_t block_id) {
112 if (block_id == GetID())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000113 return this;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114
115 Block *matching_block = nullptr;
116 collection::const_iterator pos, end = m_children.end();
117 for (pos = m_children.begin(); pos != end; ++pos) {
118 matching_block = (*pos)->FindBlockByID(block_id);
119 if (matching_block)
120 break;
121 }
122 return matching_block;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125void Block::CalculateSymbolContext(SymbolContext *sc) {
126 if (m_parent_scope)
127 m_parent_scope->CalculateSymbolContext(sc);
128 sc->block = this;
129}
130
131lldb::ModuleSP Block::CalculateSymbolContextModule() {
132 if (m_parent_scope)
133 return m_parent_scope->CalculateSymbolContextModule();
134 return lldb::ModuleSP();
135}
136
137CompileUnit *Block::CalculateSymbolContextCompileUnit() {
138 if (m_parent_scope)
139 return m_parent_scope->CalculateSymbolContextCompileUnit();
140 return nullptr;
141}
142
143Function *Block::CalculateSymbolContextFunction() {
144 if (m_parent_scope)
145 return m_parent_scope->CalculateSymbolContextFunction();
146 return nullptr;
147}
148
149Block *Block::CalculateSymbolContextBlock() { return this; }
150
151void Block::DumpSymbolContext(Stream *s) {
152 Function *function = CalculateSymbolContextFunction();
153 if (function)
154 function->DumpSymbolContext(s);
155 s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
156}
157
158void Block::DumpAddressRanges(Stream *s, lldb::addr_t base_addr) {
159 if (!m_ranges.IsEmpty()) {
160 size_t num_ranges = m_ranges.GetSize();
161 for (size_t i = 0; i < num_ranges; ++i) {
162 const Range &range = m_ranges.GetEntryRef(i);
163 s->AddressRange(base_addr + range.GetRangeBase(),
164 base_addr + range.GetRangeEnd(), 4);
165 }
166 }
167}
168
169bool Block::Contains(addr_t range_offset) const {
170 return m_ranges.FindEntryThatContains(range_offset) != nullptr;
171}
172
173bool Block::Contains(const Block *block) const {
174 if (this == block)
175 return false; // This block doesn't contain itself...
176
177 // Walk the parent chain for "block" and see if any if them match this block
178 const Block *block_parent;
179 for (block_parent = block->GetParent(); block_parent != nullptr;
180 block_parent = block_parent->GetParent()) {
181 if (this == block_parent)
182 return true; // One of the parents of "block" is this object!
183 }
184 return false;
185}
186
187bool Block::Contains(const Range &range) const {
188 return m_ranges.FindEntryThatContains(range) != nullptr;
189}
190
191Block *Block::GetParent() const {
192 if (m_parent_scope)
193 return m_parent_scope->CalculateSymbolContextBlock();
194 return nullptr;
195}
196
197Block *Block::GetContainingInlinedBlock() {
198 if (GetInlinedFunctionInfo())
199 return this;
200 return GetInlinedParent();
201}
202
203Block *Block::GetInlinedParent() {
204 Block *parent_block = GetParent();
205 if (parent_block) {
206 if (parent_block->GetInlinedFunctionInfo())
207 return parent_block;
208 else
209 return parent_block->GetInlinedParent();
210 }
211 return nullptr;
212}
213
214bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) {
215 const Range *range_ptr = m_ranges.FindEntryThatContains(offset);
216 if (range_ptr) {
217 range = *range_ptr;
218 return true;
219 }
220 range.Clear();
221 return false;
222}
223
224bool Block::GetRangeContainingAddress(const Address &addr,
225 AddressRange &range) {
226 Function *function = CalculateSymbolContextFunction();
227 if (function) {
228 const AddressRange &func_range = function->GetAddressRange();
229 if (addr.GetSection() == func_range.GetBaseAddress().GetSection()) {
230 const addr_t addr_offset = addr.GetOffset();
231 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
232 if (addr_offset >= func_offset &&
233 addr_offset < func_offset + func_range.GetByteSize()) {
234 addr_t offset = addr_offset - func_offset;
235
236 const Range *range_ptr = m_ranges.FindEntryThatContains(offset);
237
238 if (range_ptr) {
239 range.GetBaseAddress() = func_range.GetBaseAddress();
240 range.GetBaseAddress().SetOffset(func_offset +
241 range_ptr->GetRangeBase());
242 range.SetByteSize(range_ptr->GetByteSize());
243 return true;
244 }
245 }
246 }
247 }
248 range.Clear();
249 return false;
250}
251
252bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr,
253 Target &target, AddressRange &range) {
254 Address load_address;
255 load_address.SetLoadAddress(load_addr, &target);
256 AddressRange containing_range;
257 return GetRangeContainingAddress(load_address, containing_range);
258}
259
260uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
261 Function *function = CalculateSymbolContextFunction();
262 if (function) {
263 const AddressRange &func_range = function->GetAddressRange();
264 if (addr.GetSection() == func_range.GetBaseAddress().GetSection()) {
265 const addr_t addr_offset = addr.GetOffset();
266 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
267 if (addr_offset >= func_offset &&
268 addr_offset < func_offset + func_range.GetByteSize()) {
269 addr_t offset = addr_offset - func_offset;
270 return m_ranges.FindEntryIndexThatContains(offset);
271 }
272 }
273 }
274 return UINT32_MAX;
275}
276
277bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) {
278 if (range_idx < m_ranges.GetSize()) {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000279 Function *function = CalculateSymbolContextFunction();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (function) {
281 const Range &vm_range = m_ranges.GetEntryRef(range_idx);
282 range.GetBaseAddress() = function->GetAddressRange().GetBaseAddress();
283 range.GetBaseAddress().Slide(vm_range.GetRangeBase());
284 range.SetByteSize(vm_range.GetByteSize());
285 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000286 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 }
288 return false;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000289}
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291bool Block::GetStartAddress(Address &addr) {
292 if (m_ranges.IsEmpty())
Greg Clayton6dadd502010-09-02 21:44:10 +0000293 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294
295 Function *function = CalculateSymbolContextFunction();
296 if (function) {
297 addr = function->GetAddressRange().GetBaseAddress();
298 addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
299 return true;
300 }
301 return false;
Greg Clayton6dadd502010-09-02 21:44:10 +0000302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304void Block::FinalizeRanges() {
305 m_ranges.Sort();
306 m_ranges.CombineConsecutiveRanges();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307}
308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309void Block::AddRange(const Range &range) {
310 Block *parent_block = GetParent();
311 if (parent_block && !parent_block->Contains(range)) {
312 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
313 if (log) {
314 ModuleSP module_sp(m_parent_scope->CalculateSymbolContextModule());
315 Function *function = m_parent_scope->CalculateSymbolContextFunction();
316 const addr_t function_file_addr =
317 function->GetAddressRange().GetBaseAddress().GetFileAddress();
318 const addr_t block_start_addr = function_file_addr + range.GetRangeBase();
319 const addr_t block_end_addr = function_file_addr + range.GetRangeEnd();
320 Type *func_type = function->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 const Declaration &func_decl = func_type->GetDeclaration();
323 if (func_decl.GetLine()) {
324 log->Printf("warning: %s:%u block {0x%8.8" PRIx64
325 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64
326 ") which is not contained in parent block {0x%8.8" PRIx64
327 "} in function {0x%8.8" PRIx64 "} from %s",
328 func_decl.GetFile().GetPath().c_str(), func_decl.GetLine(),
329 GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
330 block_end_addr, parent_block->GetID(), function->GetID(),
331 module_sp->GetFileSpec().GetPath().c_str());
332 } else {
333 log->Printf("warning: block {0x%8.8" PRIx64
334 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64
335 ") which is not contained in parent block {0x%8.8" PRIx64
336 "} in function {0x%8.8" PRIx64 "} from %s",
337 GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
338 block_end_addr, parent_block->GetID(), function->GetID(),
339 module_sp->GetFileSpec().GetPath().c_str());
340 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000341 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 parent_block->AddRange(range);
343 }
344 m_ranges.Append(range);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345}
346
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347// Return the current number of bytes that this object occupies in memory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348size_t Block::MemorySize() const {
349 size_t mem_size = sizeof(Block) + m_ranges.GetSize() * sizeof(Range);
350 if (m_inlineInfoSP.get())
351 mem_size += m_inlineInfoSP->MemorySize();
352 if (m_variable_list_sp.get())
353 mem_size += m_variable_list_sp->MemorySize();
354 return mem_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357void Block::AddChild(const BlockSP &child_block_sp) {
358 if (child_block_sp) {
359 child_block_sp->SetParentScope(this);
360 m_children.push_back(child_block_sp);
361 }
362}
363
364void Block::SetInlinedFunctionInfo(const char *name, const char *mangled,
365 const Declaration *decl_ptr,
366 const Declaration *call_decl_ptr) {
367 m_inlineInfoSP.reset(
368 new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
369}
370
371VariableListSP Block::GetBlockVariableList(bool can_create) {
372 if (m_parsed_block_variables == false) {
373 if (m_variable_list_sp.get() == nullptr && can_create) {
374 m_parsed_block_variables = true;
375 SymbolContext sc;
376 CalculateSymbolContext(&sc);
377 assert(sc.module_sp);
378 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000379 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 }
381 return m_variable_list_sp;
Greg Claytonc662ec82011-06-17 22:10:16 +0000382}
Jim Inghame1be14d2010-08-18 19:29:16 +0000383
Greg Claytonc662ec82011-06-17 22:10:16 +0000384uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385Block::AppendBlockVariables(bool can_create, bool get_child_block_variables,
386 bool stop_if_child_block_is_inlined_function,
387 const std::function<bool(Variable *)> &filter,
388 VariableList *variable_list) {
389 uint32_t num_variables_added = 0;
390 VariableList *block_var_list = GetBlockVariableList(can_create).get();
391 if (block_var_list) {
392 for (size_t i = 0; i < block_var_list->GetSize(); ++i) {
393 VariableSP variable = block_var_list->GetVariableAtIndex(i);
394 if (filter(variable.get())) {
395 num_variables_added++;
396 variable_list->AddVariable(variable);
397 }
Greg Claytonc662ec82011-06-17 22:10:16 +0000398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 }
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 if (get_child_block_variables) {
402 collection::const_iterator pos, end = m_children.end();
403 for (pos = m_children.begin(); pos != end; ++pos) {
404 Block *child_block = pos->get();
405 if (stop_if_child_block_is_inlined_function == false ||
406 child_block->GetInlinedFunctionInfo() == nullptr) {
407 num_variables_added += child_block->AppendBlockVariables(
408 can_create, get_child_block_variables,
409 stop_if_child_block_is_inlined_function, filter, variable_list);
410 }
Jim Inghame1be14d2010-08-18 19:29:16 +0000411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 }
413 return num_variables_added;
Jim Inghame1be14d2010-08-18 19:29:16 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416uint32_t Block::AppendVariables(bool can_create, bool get_parent_variables,
417 bool stop_if_block_is_inlined_function,
418 const std::function<bool(Variable *)> &filter,
419 VariableList *variable_list) {
420 uint32_t num_variables_added = 0;
421 VariableListSP variable_list_sp(GetBlockVariableList(can_create));
Jim Inghame1be14d2010-08-18 19:29:16 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 bool is_inlined_function = GetInlinedFunctionInfo() != nullptr;
424 if (variable_list_sp) {
425 for (size_t i = 0; i < variable_list_sp->GetSize(); ++i) {
426 VariableSP variable = variable_list_sp->GetVariableAtIndex(i);
427 if (filter(variable.get())) {
428 num_variables_added++;
429 variable_list->AddVariable(variable);
430 }
Jim Inghame1be14d2010-08-18 19:29:16 +0000431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 }
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 if (get_parent_variables) {
435 if (stop_if_block_is_inlined_function && is_inlined_function)
436 return num_variables_added;
437
438 Block *parent_block = GetParent();
439 if (parent_block)
440 num_variables_added += parent_block->AppendVariables(
441 can_create, get_parent_variables, stop_if_block_is_inlined_function,
442 filter, variable_list);
443 }
444 return num_variables_added;
Jim Inghame1be14d2010-08-18 19:29:16 +0000445}
446
Vedant Kumar4b36f792018-10-05 23:23:15 +0000447SymbolFile *Block::GetSymbolFile() {
448 if (ModuleSP module_sp = CalculateSymbolContextModule())
449 if (SymbolVendor *sym_vendor = module_sp->GetSymbolVendor())
450 return sym_vendor->GetSymbolFile();
451 return nullptr;
452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454CompilerDeclContext Block::GetDeclContext() {
Vedant Kumar4b36f792018-10-05 23:23:15 +0000455 if (SymbolFile *sym_file = GetSymbolFile())
456 return sym_file->GetDeclContextForUID(GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +0000458}
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460void Block::SetBlockInfoHasBeenParsed(bool b, bool set_children) {
461 m_parsed_block_info = b;
462 if (set_children) {
463 m_parsed_child_blocks = true;
464 collection::const_iterator pos, end = m_children.end();
465 for (pos = m_children.begin(); pos != end; ++pos)
466 (*pos)->SetBlockInfoHasBeenParsed(b, true);
467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
Greg Claytonc662ec82011-06-17 22:10:16 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470void Block::SetDidParseVariables(bool b, bool set_children) {
471 m_parsed_block_variables = b;
472 if (set_children) {
473 collection::const_iterator pos, end = m_children.end();
474 for (pos = m_children.begin(); pos != end; ++pos)
475 (*pos)->SetDidParseVariables(b, true);
476 }
Greg Claytonc662ec82011-06-17 22:10:16 +0000477}
478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479Block *Block::GetSibling() const {
480 if (m_parent_scope) {
481 Block *parent_block = GetParent();
482 if (parent_block)
483 return parent_block->GetSiblingForChild(this);
484 }
485 return nullptr;
Greg Clayton624784a2011-09-29 21:19:25 +0000486}
487// A parent of child blocks can be asked to find a sibling block given
488// one of its child blocks
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489Block *Block::GetSiblingForChild(const Block *child_block) const {
490 if (!m_children.empty()) {
491 collection::const_iterator pos, end = m_children.end();
492 for (pos = m_children.begin(); pos != end; ++pos) {
493 if (pos->get() == child_block) {
494 if (++pos != end)
495 return pos->get();
496 break;
497 }
Greg Clayton624784a2011-09-29 21:19:25 +0000498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 }
500 return nullptr;
Greg Clayton624784a2011-09-29 21:19:25 +0000501}