1 //===-- Block.cpp ---------------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Symbol/Block.h" 10 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" 18 19 #include <memory> 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 Block::Block(Function &function, user_id_t function_uid) 25 : Block(function_uid, function) {} 26 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) {} 30 31 Block::~Block() = default; 32 33 void Block::GetDescription(Stream *s, Function *function, 34 lldb::DescriptionLevel level, Target *target) const { 35 *s << "id = " << ((const UserID &)*this); 36 37 size_t num_ranges = m_ranges.GetSize(); 38 if (num_ranges > 0) { 39 40 addr_t base_addr = LLDB_INVALID_ADDRESS; 41 if (target) 42 base_addr = function->GetAddress().GetLoadAddress(target); 43 if (base_addr == LLDB_INVALID_ADDRESS) 44 base_addr = function->GetAddress().GetFileAddress(); 45 46 s->Printf(", range%s = ", num_ranges > 1 ? "s" : ""); 47 for (size_t i = 0; i < num_ranges; ++i) { 48 const Range &range = m_ranges.GetEntryRef(i); 49 DumpAddressRange(s->AsRawOstream(), base_addr + range.GetRangeBase(), 50 base_addr + range.GetRangeEnd(), 4); 51 } 52 } 53 54 if (m_inlineInfoSP.get() != nullptr) { 55 bool show_fullpaths = (level == eDescriptionLevelVerbose); 56 m_inlineInfoSP->Dump(s, show_fullpaths); 57 } 58 } 59 60 void Block::Dump(Stream *s, addr_t base_addr, int32_t depth, 61 bool show_context) const { 62 if (depth < 0) { 63 Block *parent = GetParent(); 64 if (parent) { 65 // We have a depth that is less than zero, print our parent blocks first 66 parent->Dump(s, base_addr, depth + 1, show_context); 67 } 68 } 69 70 s->Printf("%p: ", static_cast<const void *>(this)); 71 s->Indent(); 72 *s << "Block" << static_cast<const UserID &>(*this); 73 const Block *parent_block = GetParent(); 74 if (parent_block) { 75 s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID()); 76 } 77 if (m_inlineInfoSP.get() != nullptr) { 78 bool show_fullpaths = false; 79 m_inlineInfoSP->Dump(s, show_fullpaths); 80 } 81 82 if (!m_ranges.IsEmpty()) { 83 *s << ", ranges ="; 84 85 size_t num_ranges = m_ranges.GetSize(); 86 for (size_t i = 0; i < num_ranges; ++i) { 87 const Range &range = m_ranges.GetEntryRef(i); 88 if (parent_block != nullptr && !parent_block->Contains(range)) 89 *s << '!'; 90 else 91 *s << ' '; 92 DumpAddressRange(s->AsRawOstream(), base_addr + range.GetRangeBase(), 93 base_addr + range.GetRangeEnd(), 4); 94 } 95 } 96 s->EOL(); 97 98 if (depth > 0) { 99 s->IndentMore(); 100 101 if (m_variable_list_sp.get()) { 102 m_variable_list_sp->Dump(s, show_context); 103 } 104 105 collection::const_iterator pos, end = m_children.end(); 106 for (pos = m_children.begin(); pos != end; ++pos) 107 (*pos)->Dump(s, base_addr, depth - 1, show_context); 108 109 s->IndentLess(); 110 } 111 } 112 113 Block *Block::FindBlockByID(user_id_t block_id) { 114 if (block_id == GetID()) 115 return this; 116 117 Block *matching_block = nullptr; 118 collection::const_iterator pos, end = m_children.end(); 119 for (pos = m_children.begin(); pos != end; ++pos) { 120 matching_block = (*pos)->FindBlockByID(block_id); 121 if (matching_block) 122 break; 123 } 124 return matching_block; 125 } 126 127 Block *Block::FindInnermostBlockByOffset(const lldb::addr_t offset) { 128 if (!Contains(offset)) 129 return nullptr; 130 for (const BlockSP &block_sp : m_children) { 131 if (Block *block = block_sp->FindInnermostBlockByOffset(offset)) 132 return block; 133 } 134 return this; 135 } 136 137 void Block::CalculateSymbolContext(SymbolContext *sc) { 138 m_parent_scope.CalculateSymbolContext(sc); 139 sc->block = this; 140 } 141 142 lldb::ModuleSP Block::CalculateSymbolContextModule() { 143 return m_parent_scope.CalculateSymbolContextModule(); 144 } 145 146 CompileUnit *Block::CalculateSymbolContextCompileUnit() { 147 return m_parent_scope.CalculateSymbolContextCompileUnit(); 148 } 149 150 Function *Block::CalculateSymbolContextFunction() { 151 return m_parent_scope.CalculateSymbolContextFunction(); 152 } 153 154 Block *Block::CalculateSymbolContextBlock() { return this; } 155 156 void Block::DumpSymbolContext(Stream *s) { 157 Function *function = CalculateSymbolContextFunction(); 158 if (function) 159 function->DumpSymbolContext(s); 160 s->Printf(", Block{0x%8.8" PRIx64 "}", GetID()); 161 } 162 163 void Block::DumpAddressRanges(Stream *s, lldb::addr_t base_addr) { 164 if (!m_ranges.IsEmpty()) { 165 size_t num_ranges = m_ranges.GetSize(); 166 for (size_t i = 0; i < num_ranges; ++i) { 167 const Range &range = m_ranges.GetEntryRef(i); 168 DumpAddressRange(s->AsRawOstream(), base_addr + range.GetRangeBase(), 169 base_addr + range.GetRangeEnd(), 4); 170 } 171 } 172 } 173 174 bool Block::Contains(addr_t range_offset) const { 175 return m_ranges.FindEntryThatContains(range_offset) != nullptr; 176 } 177 178 bool Block::Contains(const Block *block) const { 179 if (this == block) 180 return false; // This block doesn't contain itself... 181 182 // Walk the parent chain for "block" and see if any if them match this block 183 const Block *block_parent; 184 for (block_parent = block->GetParent(); block_parent != nullptr; 185 block_parent = block_parent->GetParent()) { 186 if (this == block_parent) 187 return true; // One of the parents of "block" is this object! 188 } 189 return false; 190 } 191 192 bool Block::Contains(const Range &range) const { 193 return m_ranges.FindEntryThatContains(range) != nullptr; 194 } 195 196 Block *Block::GetParent() const { 197 return m_parent_scope.CalculateSymbolContextBlock(); 198 } 199 200 Block *Block::GetContainingInlinedBlock() { 201 if (GetInlinedFunctionInfo()) 202 return this; 203 return GetInlinedParent(); 204 } 205 206 Block *Block::GetInlinedParent() { 207 Block *parent_block = GetParent(); 208 if (parent_block) { 209 if (parent_block->GetInlinedFunctionInfo()) 210 return parent_block; 211 else 212 return parent_block->GetInlinedParent(); 213 } 214 return nullptr; 215 } 216 217 Block *Block::GetContainingInlinedBlockWithCallSite( 218 const Declaration &find_call_site) { 219 Block *inlined_block = GetContainingInlinedBlock(); 220 221 while (inlined_block) { 222 const auto *function_info = inlined_block->GetInlinedFunctionInfo(); 223 224 if (function_info && 225 function_info->GetCallSite().FileAndLineEqual(find_call_site, true)) 226 return inlined_block; 227 inlined_block = inlined_block->GetInlinedParent(); 228 } 229 return nullptr; 230 } 231 232 bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) { 233 const Range *range_ptr = m_ranges.FindEntryThatContains(offset); 234 if (range_ptr) { 235 range = *range_ptr; 236 return true; 237 } 238 range.Clear(); 239 return false; 240 } 241 242 bool Block::GetRangeContainingAddress(const Address &addr, 243 AddressRange &range) { 244 Function *function = CalculateSymbolContextFunction(); 245 if (function) { 246 const AddressRange &func_range = function->GetAddressRange(); 247 if (addr.GetModule() == func_range.GetBaseAddress().GetModule()) { 248 const addr_t file_addr = addr.GetFileAddress(); 249 const addr_t func_file_addr = 250 func_range.GetBaseAddress().GetFileAddress(); 251 if (file_addr >= func_file_addr && 252 file_addr < func_file_addr + func_range.GetByteSize()) { 253 addr_t offset = file_addr - func_file_addr; 254 255 const Range *range_ptr = m_ranges.FindEntryThatContains(offset); 256 257 if (range_ptr) { 258 range.GetBaseAddress() = 259 Address(func_file_addr + range_ptr->GetRangeBase(), 260 addr.GetModule()->GetSectionList()); 261 range.SetByteSize(range_ptr->GetByteSize()); 262 return true; 263 } 264 } 265 } 266 } 267 range.Clear(); 268 return false; 269 } 270 271 bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr, 272 Target &target, AddressRange &range) { 273 Address load_address; 274 load_address.SetLoadAddress(load_addr, &target); 275 AddressRange containing_range; 276 return GetRangeContainingAddress(load_address, containing_range); 277 } 278 279 uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) { 280 Function *function = CalculateSymbolContextFunction(); 281 if (function) { 282 const AddressRange &func_range = function->GetAddressRange(); 283 if (addr.GetSection() == func_range.GetBaseAddress().GetSection()) { 284 const addr_t addr_offset = addr.GetOffset(); 285 const addr_t func_offset = func_range.GetBaseAddress().GetOffset(); 286 if (addr_offset >= func_offset && 287 addr_offset < func_offset + func_range.GetByteSize()) { 288 addr_t offset = addr_offset - func_offset; 289 return m_ranges.FindEntryIndexThatContains(offset); 290 } 291 } 292 } 293 return UINT32_MAX; 294 } 295 296 bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) { 297 if (range_idx < m_ranges.GetSize()) { 298 Function *function = CalculateSymbolContextFunction(); 299 if (function) { 300 const Range &vm_range = m_ranges.GetEntryRef(range_idx); 301 range.GetBaseAddress() = function->GetAddress(); 302 range.GetBaseAddress().Slide(vm_range.GetRangeBase()); 303 range.SetByteSize(vm_range.GetByteSize()); 304 return true; 305 } 306 } 307 return false; 308 } 309 310 AddressRanges Block::GetRanges() { 311 AddressRanges ranges; 312 Function *function = CalculateSymbolContextFunction(); 313 if (!function) 314 return ranges; 315 for (size_t i = 0, e = m_ranges.GetSize(); i < e; ++i) { 316 ranges.emplace_back(); 317 auto &range = ranges.back(); 318 const Range &vm_range = m_ranges.GetEntryRef(i); 319 range.GetBaseAddress() = function->GetAddress(); 320 range.GetBaseAddress().Slide(vm_range.GetRangeBase()); 321 range.SetByteSize(vm_range.GetByteSize()); 322 } 323 return ranges; 324 } 325 326 bool Block::GetStartAddress(Address &addr) { 327 if (m_ranges.IsEmpty()) 328 return false; 329 330 Function *function = CalculateSymbolContextFunction(); 331 if (function) { 332 addr = function->GetAddress(); 333 addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase()); 334 return true; 335 } 336 return false; 337 } 338 339 void Block::FinalizeRanges() { 340 m_ranges.Sort(); 341 m_ranges.CombineConsecutiveRanges(); 342 } 343 344 void Block::AddRange(const Range &range) { 345 Block *parent_block = GetParent(); 346 if (parent_block && !parent_block->Contains(range)) { 347 Log *log = GetLog(LLDBLog::Symbols); 348 if (log) { 349 ModuleSP module_sp(m_parent_scope.CalculateSymbolContextModule()); 350 Function *function = m_parent_scope.CalculateSymbolContextFunction(); 351 const addr_t function_file_addr = function->GetAddress().GetFileAddress(); 352 const addr_t block_start_addr = function_file_addr + range.GetRangeBase(); 353 const addr_t block_end_addr = function_file_addr + range.GetRangeEnd(); 354 Type *func_type = function->GetType(); 355 356 const Declaration &func_decl = func_type->GetDeclaration(); 357 if (func_decl.GetLine()) { 358 LLDB_LOGF(log, 359 "warning: %s:%u block {0x%8.8" PRIx64 360 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 361 ") which is not contained in parent block {0x%8.8" PRIx64 362 "} in function {0x%8.8" PRIx64 "} from %s", 363 func_decl.GetFile().GetPath().c_str(), func_decl.GetLine(), 364 GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr, 365 block_end_addr, parent_block->GetID(), function->GetID(), 366 module_sp->GetFileSpec().GetPath().c_str()); 367 } else { 368 LLDB_LOGF(log, 369 "warning: block {0x%8.8" PRIx64 "} has range[%u] [0x%" PRIx64 370 " - 0x%" PRIx64 371 ") which is not contained in parent block {0x%8.8" PRIx64 372 "} in function {0x%8.8" PRIx64 "} from %s", 373 GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr, 374 block_end_addr, parent_block->GetID(), function->GetID(), 375 module_sp->GetFileSpec().GetPath().c_str()); 376 } 377 } 378 parent_block->AddRange(range); 379 } 380 m_ranges.Append(range); 381 } 382 383 // Return the current number of bytes that this object occupies in memory 384 size_t Block::MemorySize() const { 385 size_t mem_size = sizeof(Block) + m_ranges.GetSize() * sizeof(Range); 386 if (m_inlineInfoSP.get()) 387 mem_size += m_inlineInfoSP->MemorySize(); 388 if (m_variable_list_sp.get()) 389 mem_size += m_variable_list_sp->MemorySize(); 390 return mem_size; 391 } 392 393 BlockSP Block::CreateChild(user_id_t uid) { 394 m_children.push_back(std::shared_ptr<Block>(new Block(uid, *this))); 395 return m_children.back(); 396 } 397 398 void Block::SetInlinedFunctionInfo(const char *name, const char *mangled, 399 const Declaration *decl_ptr, 400 const Declaration *call_decl_ptr) { 401 m_inlineInfoSP = std::make_shared<InlineFunctionInfo>(name, mangled, decl_ptr, 402 call_decl_ptr); 403 } 404 405 VariableListSP Block::GetBlockVariableList(bool can_create) { 406 if (!m_parsed_block_variables) { 407 if (m_variable_list_sp.get() == nullptr && can_create) { 408 m_parsed_block_variables = true; 409 SymbolContext sc; 410 CalculateSymbolContext(&sc); 411 assert(sc.module_sp); 412 sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc); 413 } 414 } 415 return m_variable_list_sp; 416 } 417 418 uint32_t 419 Block::AppendBlockVariables(bool can_create, bool get_child_block_variables, 420 bool stop_if_child_block_is_inlined_function, 421 const std::function<bool(Variable *)> &filter, 422 VariableList *variable_list) { 423 uint32_t num_variables_added = 0; 424 VariableList *block_var_list = GetBlockVariableList(can_create).get(); 425 if (block_var_list) { 426 for (const VariableSP &var_sp : *block_var_list) { 427 if (filter(var_sp.get())) { 428 num_variables_added++; 429 variable_list->AddVariable(var_sp); 430 } 431 } 432 } 433 434 if (get_child_block_variables) { 435 collection::const_iterator pos, end = m_children.end(); 436 for (pos = m_children.begin(); pos != end; ++pos) { 437 Block *child_block = pos->get(); 438 if (!stop_if_child_block_is_inlined_function || 439 child_block->GetInlinedFunctionInfo() == nullptr) { 440 num_variables_added += child_block->AppendBlockVariables( 441 can_create, get_child_block_variables, 442 stop_if_child_block_is_inlined_function, filter, variable_list); 443 } 444 } 445 } 446 return num_variables_added; 447 } 448 449 uint32_t Block::AppendVariables(bool can_create, bool get_parent_variables, 450 bool stop_if_block_is_inlined_function, 451 const std::function<bool(Variable *)> &filter, 452 VariableList *variable_list) { 453 uint32_t num_variables_added = 0; 454 VariableListSP variable_list_sp(GetBlockVariableList(can_create)); 455 456 bool is_inlined_function = GetInlinedFunctionInfo() != nullptr; 457 if (variable_list_sp) { 458 for (size_t i = 0; i < variable_list_sp->GetSize(); ++i) { 459 VariableSP variable = variable_list_sp->GetVariableAtIndex(i); 460 if (filter(variable.get())) { 461 num_variables_added++; 462 variable_list->AddVariable(variable); 463 } 464 } 465 } 466 467 if (get_parent_variables) { 468 if (stop_if_block_is_inlined_function && is_inlined_function) 469 return num_variables_added; 470 471 Block *parent_block = GetParent(); 472 if (parent_block) 473 num_variables_added += parent_block->AppendVariables( 474 can_create, get_parent_variables, stop_if_block_is_inlined_function, 475 filter, variable_list); 476 } 477 return num_variables_added; 478 } 479 480 SymbolFile *Block::GetSymbolFile() { 481 if (ModuleSP module_sp = CalculateSymbolContextModule()) 482 return module_sp->GetSymbolFile(); 483 return nullptr; 484 } 485 486 CompilerDeclContext Block::GetDeclContext() { 487 if (SymbolFile *sym_file = GetSymbolFile()) 488 return sym_file->GetDeclContextForUID(GetID()); 489 return CompilerDeclContext(); 490 } 491 492 void Block::SetBlockInfoHasBeenParsed(bool b, bool set_children) { 493 m_parsed_block_info = b; 494 if (set_children) { 495 m_parsed_child_blocks = true; 496 collection::const_iterator pos, end = m_children.end(); 497 for (pos = m_children.begin(); pos != end; ++pos) 498 (*pos)->SetBlockInfoHasBeenParsed(b, true); 499 } 500 } 501 502 void Block::SetDidParseVariables(bool b, bool set_children) { 503 m_parsed_block_variables = b; 504 if (set_children) { 505 collection::const_iterator pos, end = m_children.end(); 506 for (pos = m_children.begin(); pos != end; ++pos) 507 (*pos)->SetDidParseVariables(b, true); 508 } 509 } 510 511 Block *Block::GetSibling() const { 512 if (Block *parent_block = GetParent()) 513 return parent_block->GetSiblingForChild(this); 514 return nullptr; 515 } 516 517 // A parent of child blocks can be asked to find a sibling block given 518 // one of its child blocks 519 Block *Block::GetSiblingForChild(const Block *child_block) const { 520 if (!m_children.empty()) { 521 collection::const_iterator pos, end = m_children.end(); 522 for (pos = m_children.begin(); pos != end; ++pos) { 523 if (pos->get() == child_block) { 524 if (++pos != end) 525 return pos->get(); 526 break; 527 } 528 } 529 } 530 return nullptr; 531 } 532