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/Module.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/SymbolFile.h"
16 #include "lldb/Symbol/SymbolVendor.h"
17 #include "lldb/Symbol/VariableList.h"
18 #include "lldb/Utility/Log.h"
21 using namespace lldb_private;
23 Block::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) {}
30 void Block::GetDescription(Stream *s, Function *function,
31 lldb::DescriptionLevel level, Target *target) const {
32 *s << "id = " << ((const UserID &)*this);
34 size_t num_ranges = m_ranges.GetSize();
37 addr_t base_addr = LLDB_INVALID_ADDRESS;
40 function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
41 if (base_addr == LLDB_INVALID_ADDRESS)
42 base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
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);
52 if (m_inlineInfoSP.get() != nullptr) {
53 bool show_fullpaths = (level == eDescriptionLevelVerbose);
54 m_inlineInfoSP->Dump(s, show_fullpaths);
58 void Block::Dump(Stream *s, addr_t base_addr, int32_t depth,
59 bool show_context) const {
61 Block *parent = GetParent();
63 // We have a depth that is less than zero, print our parent blocks first
64 parent->Dump(s, base_addr, depth + 1, show_context);
68 s->Printf("%p: ", static_cast<const void *>(this));
70 *s << "Block" << static_cast<const UserID &>(*this);
71 const Block *parent_block = GetParent();
73 s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID());
75 if (m_inlineInfoSP.get() != nullptr) {
76 bool show_fullpaths = false;
77 m_inlineInfoSP->Dump(s, show_fullpaths);
80 if (!m_ranges.IsEmpty()) {
83 size_t num_ranges = m_ranges.GetSize();
84 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)
90 s->AddressRange(base_addr + range.GetRangeBase(),
91 base_addr + range.GetRangeEnd(), 4);
99 if (m_variable_list_sp.get()) {
100 m_variable_list_sp->Dump(s, show_context);
103 collection::const_iterator pos, end = m_children.end();
104 for (pos = m_children.begin(); pos != end; ++pos)
105 (*pos)->Dump(s, base_addr, depth - 1, show_context);
111 Block *Block::FindBlockByID(user_id_t block_id) {
112 if (block_id == GetID())
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);
122 return matching_block;
125 void Block::CalculateSymbolContext(SymbolContext *sc) {
127 m_parent_scope->CalculateSymbolContext(sc);
131 lldb::ModuleSP Block::CalculateSymbolContextModule() {
133 return m_parent_scope->CalculateSymbolContextModule();
134 return lldb::ModuleSP();
137 CompileUnit *Block::CalculateSymbolContextCompileUnit() {
139 return m_parent_scope->CalculateSymbolContextCompileUnit();
143 Function *Block::CalculateSymbolContextFunction() {
145 return m_parent_scope->CalculateSymbolContextFunction();
149 Block *Block::CalculateSymbolContextBlock() { return this; }
151 void Block::DumpSymbolContext(Stream *s) {
152 Function *function = CalculateSymbolContextFunction();
154 function->DumpSymbolContext(s);
155 s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
158 void 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);
169 bool Block::Contains(addr_t range_offset) const {
170 return m_ranges.FindEntryThatContains(range_offset) != nullptr;
173 bool Block::Contains(const Block *block) const {
175 return false; // This block doesn't contain itself...
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!
187 bool Block::Contains(const Range &range) const {
188 return m_ranges.FindEntryThatContains(range) != nullptr;
191 Block *Block::GetParent() const {
193 return m_parent_scope->CalculateSymbolContextBlock();
197 Block *Block::GetContainingInlinedBlock() {
198 if (GetInlinedFunctionInfo())
200 return GetInlinedParent();
203 Block *Block::GetInlinedParent() {
204 Block *parent_block = GetParent();
206 if (parent_block->GetInlinedFunctionInfo())
209 return parent_block->GetInlinedParent();
214 bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) {
215 const Range *range_ptr = m_ranges.FindEntryThatContains(offset);
224 bool Block::GetRangeContainingAddress(const Address &addr,
225 AddressRange &range) {
226 Function *function = CalculateSymbolContextFunction();
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;
236 const Range *range_ptr = m_ranges.FindEntryThatContains(offset);
239 range.GetBaseAddress() = func_range.GetBaseAddress();
240 range.GetBaseAddress().SetOffset(func_offset +
241 range_ptr->GetRangeBase());
242 range.SetByteSize(range_ptr->GetByteSize());
252 bool 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);
260 uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
261 Function *function = CalculateSymbolContextFunction();
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);
277 bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) {
278 if (range_idx < m_ranges.GetSize()) {
279 Function *function = CalculateSymbolContextFunction();
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());
291 bool Block::GetStartAddress(Address &addr) {
292 if (m_ranges.IsEmpty())
295 Function *function = CalculateSymbolContextFunction();
297 addr = function->GetAddressRange().GetBaseAddress();
298 addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
304 void Block::FinalizeRanges() {
306 m_ranges.CombineConsecutiveRanges();
309 void 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));
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();
322 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());
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());
342 parent_block->AddRange(range);
344 m_ranges.Append(range);
347 // Return the current number of bytes that this object occupies in memory
348 size_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();
357 void 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);
364 void 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));
371 VariableListSP 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;
376 CalculateSymbolContext(&sc);
377 assert(sc.module_sp);
378 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
381 return m_variable_list_sp;
385 Block::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);
401 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);
413 return num_variables_added;
416 uint32_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));
423 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);
434 if (get_parent_variables) {
435 if (stop_if_block_is_inlined_function && is_inlined_function)
436 return num_variables_added;
438 Block *parent_block = GetParent();
440 num_variables_added += parent_block->AppendVariables(
441 can_create, get_parent_variables, stop_if_block_is_inlined_function,
442 filter, variable_list);
444 return num_variables_added;
447 CompilerDeclContext Block::GetDeclContext() {
448 ModuleSP module_sp = CalculateSymbolContextModule();
451 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
454 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
457 return sym_file->GetDeclContextForUID(GetID());
460 return CompilerDeclContext();
463 void Block::SetBlockInfoHasBeenParsed(bool b, bool set_children) {
464 m_parsed_block_info = b;
466 m_parsed_child_blocks = true;
467 collection::const_iterator pos, end = m_children.end();
468 for (pos = m_children.begin(); pos != end; ++pos)
469 (*pos)->SetBlockInfoHasBeenParsed(b, true);
473 void Block::SetDidParseVariables(bool b, bool set_children) {
474 m_parsed_block_variables = b;
476 collection::const_iterator pos, end = m_children.end();
477 for (pos = m_children.begin(); pos != end; ++pos)
478 (*pos)->SetDidParseVariables(b, true);
482 Block *Block::GetSibling() const {
483 if (m_parent_scope) {
484 Block *parent_block = GetParent();
486 return parent_block->GetSiblingForChild(this);
490 // A parent of child blocks can be asked to find a sibling block given
491 // one of its child blocks
492 Block *Block::GetSiblingForChild(const Block *child_block) const {
493 if (!m_children.empty()) {
494 collection::const_iterator pos, end = m_children.end();
495 for (pos = m_children.begin(); pos != end; ++pos) {
496 if (pos->get() == child_block) {