1 //===-- Block.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/Block.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/SymbolFile.h"
17 #include "lldb/Symbol/SymbolVendor.h"
18 #include "lldb/Symbol/VariableList.h"
21 using namespace lldb_private;
23 Block::Block(lldb::user_id_t uid) :
25 m_parent_scope (nullptr),
29 m_variable_list_sp (),
30 m_parsed_block_info (false),
31 m_parsed_block_variables (false),
32 m_parsed_child_blocks (false)
41 Block::GetDescription(Stream *s, Function *function, lldb::DescriptionLevel level, Target *target) const
43 *s << "id = " << ((const UserID&)*this);
45 size_t num_ranges = m_ranges.GetSize();
49 addr_t base_addr = LLDB_INVALID_ADDRESS;
51 base_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
52 if (base_addr == LLDB_INVALID_ADDRESS)
53 base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
55 s->Printf(", range%s = ", num_ranges > 1 ? "s" : "");
56 for (size_t i=0; i<num_ranges; ++i)
58 const Range &range = m_ranges.GetEntryRef(i);
59 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
63 if (m_inlineInfoSP.get() != nullptr)
65 bool show_fullpaths = (level == eDescriptionLevelVerbose);
66 m_inlineInfoSP->Dump(s, show_fullpaths);
71 Block::Dump(Stream *s, addr_t base_addr, int32_t depth, bool show_context) const
75 Block *parent = GetParent();
78 // We have a depth that is less than zero, print our parent blocks
80 parent->Dump(s, base_addr, depth + 1, show_context);
84 s->Printf("%p: ", static_cast<const void*>(this));
86 *s << "Block" << static_cast<const UserID&>(*this);
87 const Block* parent_block = GetParent();
90 s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID());
92 if (m_inlineInfoSP.get() != nullptr)
94 bool show_fullpaths = false;
95 m_inlineInfoSP->Dump(s, show_fullpaths);
98 if (!m_ranges.IsEmpty())
102 size_t num_ranges = m_ranges.GetSize();
103 for (size_t i=0; i<num_ranges; ++i)
105 const Range &range = m_ranges.GetEntryRef(i);
106 if (parent_block != nullptr && parent_block->Contains(range) == false)
110 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
119 if (m_variable_list_sp.get())
121 m_variable_list_sp->Dump(s, show_context);
124 collection::const_iterator pos, end = m_children.end();
125 for (pos = m_children.begin(); pos != end; ++pos)
126 (*pos)->Dump(s, base_addr, depth - 1, show_context);
135 Block::FindBlockByID (user_id_t block_id)
137 if (block_id == GetID())
140 Block *matching_block = nullptr;
141 collection::const_iterator pos, end = m_children.end();
142 for (pos = m_children.begin(); pos != end; ++pos)
144 matching_block = (*pos)->FindBlockByID (block_id);
148 return matching_block;
152 Block::CalculateSymbolContext (SymbolContext* sc)
155 m_parent_scope->CalculateSymbolContext(sc);
160 Block::CalculateSymbolContextModule ()
163 return m_parent_scope->CalculateSymbolContextModule ();
164 return lldb::ModuleSP();
168 Block::CalculateSymbolContextCompileUnit ()
171 return m_parent_scope->CalculateSymbolContextCompileUnit ();
176 Block::CalculateSymbolContextFunction ()
179 return m_parent_scope->CalculateSymbolContextFunction ();
184 Block::CalculateSymbolContextBlock ()
190 Block::DumpSymbolContext(Stream *s)
192 Function *function = CalculateSymbolContextFunction();
194 function->DumpSymbolContext(s);
195 s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
199 Block::DumpAddressRanges (Stream *s, lldb::addr_t base_addr)
201 if (!m_ranges.IsEmpty())
203 size_t num_ranges = m_ranges.GetSize();
204 for (size_t i=0; i<num_ranges; ++i)
206 const Range &range = m_ranges.GetEntryRef(i);
207 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
213 Block::Contains (addr_t range_offset) const
215 return m_ranges.FindEntryThatContains(range_offset) != nullptr;
219 Block::Contains (const Block *block) const
222 return false; // This block doesn't contain itself...
224 // Walk the parent chain for "block" and see if any if them match this block
225 const Block *block_parent;
226 for (block_parent = block->GetParent();
227 block_parent != nullptr;
228 block_parent = block_parent->GetParent())
230 if (this == block_parent)
231 return true; // One of the parents of "block" is this object!
237 Block::Contains (const Range& range) const
239 return m_ranges.FindEntryThatContains (range) != nullptr;
243 Block::GetParent () const
246 return m_parent_scope->CalculateSymbolContextBlock();
251 Block::GetContainingInlinedBlock ()
253 if (GetInlinedFunctionInfo())
255 return GetInlinedParent ();
259 Block::GetInlinedParent ()
261 Block *parent_block = GetParent ();
264 if (parent_block->GetInlinedFunctionInfo())
267 return parent_block->GetInlinedParent();
274 Block::GetRangeContainingOffset (const addr_t offset, Range &range)
276 const Range *range_ptr = m_ranges.FindEntryThatContains (offset);
288 Block::GetRangeContainingAddress (const Address& addr, AddressRange &range)
290 Function *function = CalculateSymbolContextFunction();
293 const AddressRange &func_range = function->GetAddressRange();
294 if (addr.GetSection() == func_range.GetBaseAddress().GetSection())
296 const addr_t addr_offset = addr.GetOffset();
297 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
298 if (addr_offset >= func_offset && addr_offset < func_offset + func_range.GetByteSize())
300 addr_t offset = addr_offset - func_offset;
302 const Range *range_ptr = m_ranges.FindEntryThatContains (offset);
306 range.GetBaseAddress() = func_range.GetBaseAddress();
307 range.GetBaseAddress().SetOffset(func_offset + range_ptr->GetRangeBase());
308 range.SetByteSize(range_ptr->GetByteSize());
319 Block::GetRangeContainingLoadAddress (lldb::addr_t load_addr, Target &target, AddressRange &range)
321 Address load_address;
322 load_address.SetLoadAddress(load_addr, &target);
323 AddressRange containing_range;
324 return GetRangeContainingAddress(load_address, containing_range);
329 Block::GetRangeIndexContainingAddress (const Address& addr)
331 Function *function = CalculateSymbolContextFunction();
334 const AddressRange &func_range = function->GetAddressRange();
335 if (addr.GetSection() == func_range.GetBaseAddress().GetSection())
337 const addr_t addr_offset = addr.GetOffset();
338 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
339 if (addr_offset >= func_offset && addr_offset < func_offset + func_range.GetByteSize())
341 addr_t offset = addr_offset - func_offset;
342 return m_ranges.FindEntryIndexThatContains (offset);
350 Block::GetRangeAtIndex (uint32_t range_idx, AddressRange &range)
352 if (range_idx < m_ranges.GetSize())
354 Function *function = CalculateSymbolContextFunction();
357 const Range &vm_range = m_ranges.GetEntryRef(range_idx);
358 range.GetBaseAddress() = function->GetAddressRange().GetBaseAddress();
359 range.GetBaseAddress().Slide(vm_range.GetRangeBase ());
360 range.SetByteSize (vm_range.GetByteSize());
368 Block::GetStartAddress (Address &addr)
370 if (m_ranges.IsEmpty())
373 Function *function = CalculateSymbolContextFunction();
376 addr = function->GetAddressRange().GetBaseAddress();
377 addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase ());
384 Block::FinalizeRanges ()
387 m_ranges.CombineConsecutiveRanges ();
391 Block::AddRange (const Range& range)
393 Block *parent_block = GetParent ();
394 if (parent_block && !parent_block->Contains(range))
396 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
399 ModuleSP module_sp (m_parent_scope->CalculateSymbolContextModule());
400 Function *function = m_parent_scope->CalculateSymbolContextFunction();
401 const addr_t function_file_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
402 const addr_t block_start_addr = function_file_addr + range.GetRangeBase ();
403 const addr_t block_end_addr = function_file_addr + range.GetRangeEnd ();
404 Type *func_type = function->GetType();
406 const Declaration &func_decl = func_type->GetDeclaration();
407 if (func_decl.GetLine())
409 log->Printf ("warning: %s:%u block {0x%8.8" PRIx64 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 ") which is not contained in parent block {0x%8.8" PRIx64 "} in function {0x%8.8" PRIx64 "} from %s",
410 func_decl.GetFile().GetPath().c_str(),
413 (uint32_t)m_ranges.GetSize(),
416 parent_block->GetID(),
418 module_sp->GetFileSpec().GetPath().c_str());
422 log->Printf ("warning: block {0x%8.8" PRIx64 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 ") which is not contained in parent block {0x%8.8" PRIx64 "} in function {0x%8.8" PRIx64 "} from %s",
424 (uint32_t)m_ranges.GetSize(),
427 parent_block->GetID(),
429 module_sp->GetFileSpec().GetPath().c_str());
432 parent_block->AddRange (range);
434 m_ranges.Append(range);
437 // Return the current number of bytes that this object occupies in memory
439 Block::MemorySize() const
441 size_t mem_size = sizeof(Block) + m_ranges.GetSize() * sizeof(Range);
442 if (m_inlineInfoSP.get())
443 mem_size += m_inlineInfoSP->MemorySize();
444 if (m_variable_list_sp.get())
445 mem_size += m_variable_list_sp->MemorySize();
451 Block::AddChild(const BlockSP &child_block_sp)
455 child_block_sp->SetParentScope (this);
456 m_children.push_back (child_block_sp);
461 Block::SetInlinedFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
463 m_inlineInfoSP.reset(new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
469 Block::GetBlockVariableList (bool can_create)
471 if (m_parsed_block_variables == false)
473 if (m_variable_list_sp.get() == nullptr && can_create)
475 m_parsed_block_variables = true;
477 CalculateSymbolContext(&sc);
478 assert(sc.module_sp);
479 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
482 return m_variable_list_sp;
486 Block::AppendBlockVariables (bool can_create,
487 bool get_child_block_variables,
488 bool stop_if_child_block_is_inlined_function,
489 VariableList *variable_list)
491 uint32_t num_variables_added = 0;
492 VariableList *block_var_list = GetBlockVariableList (can_create).get();
495 num_variables_added += block_var_list->GetSize();
496 variable_list->AddVariables (block_var_list);
499 if (get_child_block_variables)
501 collection::const_iterator pos, end = m_children.end();
502 for (pos = m_children.begin(); pos != end; ++pos)
504 Block *child_block = pos->get();
505 if (stop_if_child_block_is_inlined_function == false ||
506 child_block->GetInlinedFunctionInfo() == nullptr)
508 num_variables_added += child_block->AppendBlockVariables (can_create,
509 get_child_block_variables,
510 stop_if_child_block_is_inlined_function,
515 return num_variables_added;
519 Block::AppendVariables
522 bool get_parent_variables,
523 bool stop_if_block_is_inlined_function,
524 VariableList *variable_list
527 uint32_t num_variables_added = 0;
528 VariableListSP variable_list_sp(GetBlockVariableList(can_create));
530 bool is_inlined_function = GetInlinedFunctionInfo() != nullptr;
531 if (variable_list_sp.get())
533 num_variables_added = variable_list_sp->GetSize();
534 variable_list->AddVariables(variable_list_sp.get());
537 if (get_parent_variables)
539 if (stop_if_block_is_inlined_function && is_inlined_function)
540 return num_variables_added;
542 Block* parent_block = GetParent();
544 num_variables_added += parent_block->AppendVariables (can_create, get_parent_variables, stop_if_block_is_inlined_function, variable_list);
546 return num_variables_added;
550 Block::GetDeclContext()
552 ModuleSP module_sp = CalculateSymbolContextModule ();
556 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
560 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
563 return sym_file->GetDeclContextForUID (GetID());
566 return CompilerDeclContext();
570 Block::SetBlockInfoHasBeenParsed (bool b, bool set_children)
572 m_parsed_block_info = b;
575 m_parsed_child_blocks = true;
576 collection::const_iterator pos, end = m_children.end();
577 for (pos = m_children.begin(); pos != end; ++pos)
578 (*pos)->SetBlockInfoHasBeenParsed (b, true);
583 Block::SetDidParseVariables (bool b, bool set_children)
585 m_parsed_block_variables = b;
588 collection::const_iterator pos, end = m_children.end();
589 for (pos = m_children.begin(); pos != end; ++pos)
590 (*pos)->SetDidParseVariables (b, true);
596 Block::GetSibling() const
600 Block *parent_block = GetParent();
602 return parent_block->GetSiblingForChild (this);
606 // A parent of child blocks can be asked to find a sibling block given
607 // one of its child blocks
609 Block::GetSiblingForChild (const Block *child_block) const
611 if (!m_children.empty())
613 collection::const_iterator pos, end = m_children.end();
614 for (pos = m_children.begin(); pos != end; ++pos)
616 if (pos->get() == child_block)