1 //===-- Block.cpp ---------------------------------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/Block.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/Section.h"
13 #include "lldb/Symbol/Function.h"
14 #include "lldb/Symbol/SymbolFile.h"
15 #include "lldb/Symbol/VariableList.h"
16 #include "lldb/Utility/LLDBLog.h"
17 #include "lldb/Utility/Log.h"
22 using namespace lldb_private
;
24 Block::Block(Function
&function
, user_id_t function_uid
)
25 : Block(function_uid
, function
) {}
27 Block::Block(lldb::user_id_t uid
, SymbolContextScope
&parent_scope
)
28 : UserID(uid
), m_parent_scope(parent_scope
), m_parsed_block_info(false),
29 m_parsed_block_variables(false), m_parsed_child_blocks(false) {}
31 Block::~Block() = default;
33 void Block::GetDescription(Stream
*s
, Function
*function
,
34 lldb::DescriptionLevel level
, Target
*target
) const {
35 *s
<< "id = " << ((const UserID
&)*this);
37 size_t num_ranges
= m_ranges
.GetSize();
40 addr_t base_addr
= LLDB_INVALID_ADDRESS
;
43 function
->GetAddressRange().GetBaseAddress().GetLoadAddress(target
);
44 if (base_addr
== LLDB_INVALID_ADDRESS
)
45 base_addr
= function
->GetAddressRange().GetBaseAddress().GetFileAddress();
47 s
->Printf(", range%s = ", num_ranges
> 1 ? "s" : "");
48 for (size_t i
= 0; i
< num_ranges
; ++i
) {
49 const Range
&range
= m_ranges
.GetEntryRef(i
);
50 DumpAddressRange(s
->AsRawOstream(), base_addr
+ range
.GetRangeBase(),
51 base_addr
+ range
.GetRangeEnd(), 4);
55 if (m_inlineInfoSP
.get() != nullptr) {
56 bool show_fullpaths
= (level
== eDescriptionLevelVerbose
);
57 m_inlineInfoSP
->Dump(s
, show_fullpaths
);
61 void Block::Dump(Stream
*s
, addr_t base_addr
, int32_t depth
,
62 bool show_context
) const {
64 Block
*parent
= GetParent();
66 // We have a depth that is less than zero, print our parent blocks first
67 parent
->Dump(s
, base_addr
, depth
+ 1, show_context
);
71 s
->Printf("%p: ", static_cast<const void *>(this));
73 *s
<< "Block" << static_cast<const UserID
&>(*this);
74 const Block
*parent_block
= GetParent();
76 s
->Printf(", parent = {0x%8.8" PRIx64
"}", parent_block
->GetID());
78 if (m_inlineInfoSP
.get() != nullptr) {
79 bool show_fullpaths
= false;
80 m_inlineInfoSP
->Dump(s
, show_fullpaths
);
83 if (!m_ranges
.IsEmpty()) {
86 size_t num_ranges
= m_ranges
.GetSize();
87 for (size_t i
= 0; i
< num_ranges
; ++i
) {
88 const Range
&range
= m_ranges
.GetEntryRef(i
);
89 if (parent_block
!= nullptr && !parent_block
->Contains(range
))
93 DumpAddressRange(s
->AsRawOstream(), base_addr
+ range
.GetRangeBase(),
94 base_addr
+ range
.GetRangeEnd(), 4);
102 if (m_variable_list_sp
.get()) {
103 m_variable_list_sp
->Dump(s
, show_context
);
106 collection::const_iterator pos
, end
= m_children
.end();
107 for (pos
= m_children
.begin(); pos
!= end
; ++pos
)
108 (*pos
)->Dump(s
, base_addr
, depth
- 1, show_context
);
114 Block
*Block::FindBlockByID(user_id_t block_id
) {
115 if (block_id
== GetID())
118 Block
*matching_block
= nullptr;
119 collection::const_iterator pos
, end
= m_children
.end();
120 for (pos
= m_children
.begin(); pos
!= end
; ++pos
) {
121 matching_block
= (*pos
)->FindBlockByID(block_id
);
125 return matching_block
;
128 Block
*Block::FindInnermostBlockByOffset(const lldb::addr_t offset
) {
129 if (!Contains(offset
))
131 for (const BlockSP
&block_sp
: m_children
) {
132 if (Block
*block
= block_sp
->FindInnermostBlockByOffset(offset
))
138 void Block::CalculateSymbolContext(SymbolContext
*sc
) {
139 m_parent_scope
.CalculateSymbolContext(sc
);
143 lldb::ModuleSP
Block::CalculateSymbolContextModule() {
144 return m_parent_scope
.CalculateSymbolContextModule();
147 CompileUnit
*Block::CalculateSymbolContextCompileUnit() {
148 return m_parent_scope
.CalculateSymbolContextCompileUnit();
151 Function
*Block::CalculateSymbolContextFunction() {
152 return m_parent_scope
.CalculateSymbolContextFunction();
155 Block
*Block::CalculateSymbolContextBlock() { return this; }
157 void Block::DumpSymbolContext(Stream
*s
) {
158 Function
*function
= CalculateSymbolContextFunction();
160 function
->DumpSymbolContext(s
);
161 s
->Printf(", Block{0x%8.8" PRIx64
"}", GetID());
164 void Block::DumpAddressRanges(Stream
*s
, lldb::addr_t base_addr
) {
165 if (!m_ranges
.IsEmpty()) {
166 size_t num_ranges
= m_ranges
.GetSize();
167 for (size_t i
= 0; i
< num_ranges
; ++i
) {
168 const Range
&range
= m_ranges
.GetEntryRef(i
);
169 DumpAddressRange(s
->AsRawOstream(), base_addr
+ range
.GetRangeBase(),
170 base_addr
+ range
.GetRangeEnd(), 4);
175 bool Block::Contains(addr_t range_offset
) const {
176 return m_ranges
.FindEntryThatContains(range_offset
) != nullptr;
179 bool Block::Contains(const Block
*block
) const {
181 return false; // This block doesn't contain itself...
183 // Walk the parent chain for "block" and see if any if them match this block
184 const Block
*block_parent
;
185 for (block_parent
= block
->GetParent(); block_parent
!= nullptr;
186 block_parent
= block_parent
->GetParent()) {
187 if (this == block_parent
)
188 return true; // One of the parents of "block" is this object!
193 bool Block::Contains(const Range
&range
) const {
194 return m_ranges
.FindEntryThatContains(range
) != nullptr;
197 Block
*Block::GetParent() const {
198 return m_parent_scope
.CalculateSymbolContextBlock();
201 Block
*Block::GetContainingInlinedBlock() {
202 if (GetInlinedFunctionInfo())
204 return GetInlinedParent();
207 Block
*Block::GetInlinedParent() {
208 Block
*parent_block
= GetParent();
210 if (parent_block
->GetInlinedFunctionInfo())
213 return parent_block
->GetInlinedParent();
218 Block
*Block::GetContainingInlinedBlockWithCallSite(
219 const Declaration
&find_call_site
) {
220 Block
*inlined_block
= GetContainingInlinedBlock();
222 while (inlined_block
) {
223 const auto *function_info
= inlined_block
->GetInlinedFunctionInfo();
226 function_info
->GetCallSite().FileAndLineEqual(find_call_site
, true))
227 return inlined_block
;
228 inlined_block
= inlined_block
->GetInlinedParent();
233 bool Block::GetRangeContainingOffset(const addr_t offset
, Range
&range
) {
234 const Range
*range_ptr
= m_ranges
.FindEntryThatContains(offset
);
243 bool Block::GetRangeContainingAddress(const Address
&addr
,
244 AddressRange
&range
) {
245 Function
*function
= CalculateSymbolContextFunction();
247 const AddressRange
&func_range
= function
->GetAddressRange();
248 if (addr
.GetModule() == func_range
.GetBaseAddress().GetModule()) {
249 const addr_t file_addr
= addr
.GetFileAddress();
250 const addr_t func_file_addr
=
251 func_range
.GetBaseAddress().GetFileAddress();
252 if (file_addr
>= func_file_addr
&&
253 file_addr
< func_file_addr
+ func_range
.GetByteSize()) {
254 addr_t offset
= file_addr
- func_file_addr
;
256 const Range
*range_ptr
= m_ranges
.FindEntryThatContains(offset
);
259 range
.GetBaseAddress() =
260 Address(func_file_addr
+ range_ptr
->GetRangeBase(),
261 addr
.GetModule()->GetSectionList());
262 range
.SetByteSize(range_ptr
->GetByteSize());
272 bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr
,
273 Target
&target
, AddressRange
&range
) {
274 Address load_address
;
275 load_address
.SetLoadAddress(load_addr
, &target
);
276 AddressRange containing_range
;
277 return GetRangeContainingAddress(load_address
, containing_range
);
280 uint32_t Block::GetRangeIndexContainingAddress(const Address
&addr
) {
281 Function
*function
= CalculateSymbolContextFunction();
283 const AddressRange
&func_range
= function
->GetAddressRange();
284 if (addr
.GetSection() == func_range
.GetBaseAddress().GetSection()) {
285 const addr_t addr_offset
= addr
.GetOffset();
286 const addr_t func_offset
= func_range
.GetBaseAddress().GetOffset();
287 if (addr_offset
>= func_offset
&&
288 addr_offset
< func_offset
+ func_range
.GetByteSize()) {
289 addr_t offset
= addr_offset
- func_offset
;
290 return m_ranges
.FindEntryIndexThatContains(offset
);
297 bool Block::GetRangeAtIndex(uint32_t range_idx
, AddressRange
&range
) {
298 if (range_idx
< m_ranges
.GetSize()) {
299 Function
*function
= CalculateSymbolContextFunction();
301 const Range
&vm_range
= m_ranges
.GetEntryRef(range_idx
);
302 range
.GetBaseAddress() = function
->GetAddressRange().GetBaseAddress();
303 range
.GetBaseAddress().Slide(vm_range
.GetRangeBase());
304 range
.SetByteSize(vm_range
.GetByteSize());
311 AddressRanges
Block::GetRanges() {
312 AddressRanges ranges
;
313 Function
*function
= CalculateSymbolContextFunction();
316 for (size_t i
= 0, e
= m_ranges
.GetSize(); i
< e
; ++i
) {
317 ranges
.emplace_back();
318 auto &range
= ranges
.back();
319 const Range
&vm_range
= m_ranges
.GetEntryRef(i
);
320 range
.GetBaseAddress() = function
->GetAddressRange().GetBaseAddress();
321 range
.GetBaseAddress().Slide(vm_range
.GetRangeBase());
322 range
.SetByteSize(vm_range
.GetByteSize());
327 bool Block::GetStartAddress(Address
&addr
) {
328 if (m_ranges
.IsEmpty())
331 Function
*function
= CalculateSymbolContextFunction();
333 addr
= function
->GetAddressRange().GetBaseAddress();
334 addr
.Slide(m_ranges
.GetEntryRef(0).GetRangeBase());
340 void Block::FinalizeRanges() {
342 m_ranges
.CombineConsecutiveRanges();
345 void Block::AddRange(const Range
&range
) {
346 Block
*parent_block
= GetParent();
347 if (parent_block
&& !parent_block
->Contains(range
)) {
348 Log
*log
= GetLog(LLDBLog::Symbols
);
350 ModuleSP
module_sp(m_parent_scope
.CalculateSymbolContextModule());
351 Function
*function
= m_parent_scope
.CalculateSymbolContextFunction();
352 const addr_t function_file_addr
=
353 function
->GetAddressRange().GetBaseAddress().GetFileAddress();
354 const addr_t block_start_addr
= function_file_addr
+ range
.GetRangeBase();
355 const addr_t block_end_addr
= function_file_addr
+ range
.GetRangeEnd();
356 Type
*func_type
= function
->GetType();
358 const Declaration
&func_decl
= func_type
->GetDeclaration();
359 if (func_decl
.GetLine()) {
361 "warning: %s:%u block {0x%8.8" PRIx64
362 "} has range[%u] [0x%" PRIx64
" - 0x%" PRIx64
363 ") which is not contained in parent block {0x%8.8" PRIx64
364 "} in function {0x%8.8" PRIx64
"} from %s",
365 func_decl
.GetFile().GetPath().c_str(), func_decl
.GetLine(),
366 GetID(), (uint32_t)m_ranges
.GetSize(), block_start_addr
,
367 block_end_addr
, parent_block
->GetID(), function
->GetID(),
368 module_sp
->GetFileSpec().GetPath().c_str());
371 "warning: block {0x%8.8" PRIx64
"} has range[%u] [0x%" PRIx64
373 ") which is not contained in parent block {0x%8.8" PRIx64
374 "} in function {0x%8.8" PRIx64
"} from %s",
375 GetID(), (uint32_t)m_ranges
.GetSize(), block_start_addr
,
376 block_end_addr
, parent_block
->GetID(), function
->GetID(),
377 module_sp
->GetFileSpec().GetPath().c_str());
380 parent_block
->AddRange(range
);
382 m_ranges
.Append(range
);
385 // Return the current number of bytes that this object occupies in memory
386 size_t Block::MemorySize() const {
387 size_t mem_size
= sizeof(Block
) + m_ranges
.GetSize() * sizeof(Range
);
388 if (m_inlineInfoSP
.get())
389 mem_size
+= m_inlineInfoSP
->MemorySize();
390 if (m_variable_list_sp
.get())
391 mem_size
+= m_variable_list_sp
->MemorySize();
395 BlockSP
Block::CreateChild(user_id_t uid
) {
396 m_children
.push_back(std::shared_ptr
<Block
>(new Block(uid
, *this)));
397 return m_children
.back();
400 void Block::SetInlinedFunctionInfo(const char *name
, const char *mangled
,
401 const Declaration
*decl_ptr
,
402 const Declaration
*call_decl_ptr
) {
403 m_inlineInfoSP
= std::make_shared
<InlineFunctionInfo
>(name
, mangled
, decl_ptr
,
407 VariableListSP
Block::GetBlockVariableList(bool can_create
) {
408 if (!m_parsed_block_variables
) {
409 if (m_variable_list_sp
.get() == nullptr && can_create
) {
410 m_parsed_block_variables
= true;
412 CalculateSymbolContext(&sc
);
413 assert(sc
.module_sp
);
414 sc
.module_sp
->GetSymbolFile()->ParseVariablesForContext(sc
);
417 return m_variable_list_sp
;
421 Block::AppendBlockVariables(bool can_create
, bool get_child_block_variables
,
422 bool stop_if_child_block_is_inlined_function
,
423 const std::function
<bool(Variable
*)> &filter
,
424 VariableList
*variable_list
) {
425 uint32_t num_variables_added
= 0;
426 VariableList
*block_var_list
= GetBlockVariableList(can_create
).get();
427 if (block_var_list
) {
428 for (const VariableSP
&var_sp
: *block_var_list
) {
429 if (filter(var_sp
.get())) {
430 num_variables_added
++;
431 variable_list
->AddVariable(var_sp
);
436 if (get_child_block_variables
) {
437 collection::const_iterator pos
, end
= m_children
.end();
438 for (pos
= m_children
.begin(); pos
!= end
; ++pos
) {
439 Block
*child_block
= pos
->get();
440 if (!stop_if_child_block_is_inlined_function
||
441 child_block
->GetInlinedFunctionInfo() == nullptr) {
442 num_variables_added
+= child_block
->AppendBlockVariables(
443 can_create
, get_child_block_variables
,
444 stop_if_child_block_is_inlined_function
, filter
, variable_list
);
448 return num_variables_added
;
451 uint32_t Block::AppendVariables(bool can_create
, bool get_parent_variables
,
452 bool stop_if_block_is_inlined_function
,
453 const std::function
<bool(Variable
*)> &filter
,
454 VariableList
*variable_list
) {
455 uint32_t num_variables_added
= 0;
456 VariableListSP
variable_list_sp(GetBlockVariableList(can_create
));
458 bool is_inlined_function
= GetInlinedFunctionInfo() != nullptr;
459 if (variable_list_sp
) {
460 for (size_t i
= 0; i
< variable_list_sp
->GetSize(); ++i
) {
461 VariableSP variable
= variable_list_sp
->GetVariableAtIndex(i
);
462 if (filter(variable
.get())) {
463 num_variables_added
++;
464 variable_list
->AddVariable(variable
);
469 if (get_parent_variables
) {
470 if (stop_if_block_is_inlined_function
&& is_inlined_function
)
471 return num_variables_added
;
473 Block
*parent_block
= GetParent();
475 num_variables_added
+= parent_block
->AppendVariables(
476 can_create
, get_parent_variables
, stop_if_block_is_inlined_function
,
477 filter
, variable_list
);
479 return num_variables_added
;
482 SymbolFile
*Block::GetSymbolFile() {
483 if (ModuleSP module_sp
= CalculateSymbolContextModule())
484 return module_sp
->GetSymbolFile();
488 CompilerDeclContext
Block::GetDeclContext() {
489 if (SymbolFile
*sym_file
= GetSymbolFile())
490 return sym_file
->GetDeclContextForUID(GetID());
491 return CompilerDeclContext();
494 void Block::SetBlockInfoHasBeenParsed(bool b
, bool set_children
) {
495 m_parsed_block_info
= b
;
497 m_parsed_child_blocks
= true;
498 collection::const_iterator pos
, end
= m_children
.end();
499 for (pos
= m_children
.begin(); pos
!= end
; ++pos
)
500 (*pos
)->SetBlockInfoHasBeenParsed(b
, true);
504 void Block::SetDidParseVariables(bool b
, bool set_children
) {
505 m_parsed_block_variables
= b
;
507 collection::const_iterator pos
, end
= m_children
.end();
508 for (pos
= m_children
.begin(); pos
!= end
; ++pos
)
509 (*pos
)->SetDidParseVariables(b
, true);
513 Block
*Block::GetSibling() const {
514 if (Block
*parent_block
= GetParent())
515 return parent_block
->GetSiblingForChild(this);
519 // A parent of child blocks can be asked to find a sibling block given
520 // one of its child blocks
521 Block
*Block::GetSiblingForChild(const Block
*child_block
) const {
522 if (!m_children
.empty()) {
523 collection::const_iterator pos
, end
= m_children
.end();
524 for (pos
= m_children
.begin(); pos
!= end
; ++pos
) {
525 if (pos
->get() == child_block
) {