1 //===-- SymbolContext.cpp ---------------------------------------*- C++ -*-===// 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/SymbolContext.h" 10 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/ModuleSpec.h" 13 #include "lldb/Host/Host.h" 14 #include "lldb/Host/StringConvert.h" 15 #include "lldb/Symbol/Block.h" 16 #include "lldb/Symbol/ClangASTContext.h" 17 #include "lldb/Symbol/CompileUnit.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Symbol/Symbol.h" 20 #include "lldb/Symbol/SymbolFile.h" 21 #include "lldb/Symbol/SymbolVendor.h" 22 #include "lldb/Symbol/Variable.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Utility/Log.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 SymbolContext::SymbolContext() 30 : target_sp(), module_sp(), comp_unit(nullptr), function(nullptr), 31 block(nullptr), line_entry(), symbol(nullptr), variable(nullptr) {} 32 33 SymbolContext::SymbolContext(const ModuleSP &m, CompileUnit *cu, Function *f, 34 Block *b, LineEntry *le, Symbol *s) 35 : target_sp(), module_sp(m), comp_unit(cu), function(f), block(b), 36 line_entry(), symbol(s), variable(nullptr) { 37 if (le) 38 line_entry = *le; 39 } 40 41 SymbolContext::SymbolContext(const TargetSP &t, const ModuleSP &m, 42 CompileUnit *cu, Function *f, Block *b, 43 LineEntry *le, Symbol *s) 44 : target_sp(t), module_sp(m), comp_unit(cu), function(f), block(b), 45 line_entry(), symbol(s), variable(nullptr) { 46 if (le) 47 line_entry = *le; 48 } 49 50 SymbolContext::SymbolContext(SymbolContextScope *sc_scope) 51 : target_sp(), module_sp(), comp_unit(nullptr), function(nullptr), 52 block(nullptr), line_entry(), symbol(nullptr), variable(nullptr) { 53 sc_scope->CalculateSymbolContext(this); 54 } 55 56 SymbolContext::~SymbolContext() {} 57 58 const SymbolContext &SymbolContext::operator=(const SymbolContext &rhs) { 59 if (this != &rhs) { 60 target_sp = rhs.target_sp; 61 module_sp = rhs.module_sp; 62 comp_unit = rhs.comp_unit; 63 function = rhs.function; 64 block = rhs.block; 65 line_entry = rhs.line_entry; 66 symbol = rhs.symbol; 67 variable = rhs.variable; 68 } 69 return *this; 70 } 71 72 void SymbolContext::Clear(bool clear_target) { 73 if (clear_target) 74 target_sp.reset(); 75 module_sp.reset(); 76 comp_unit = nullptr; 77 function = nullptr; 78 block = nullptr; 79 line_entry.Clear(); 80 symbol = nullptr; 81 variable = nullptr; 82 } 83 84 bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope, 85 const Address &addr, bool show_fullpaths, 86 bool show_module, bool show_inlined_frames, 87 bool show_function_arguments, 88 bool show_function_name) const { 89 bool dumped_something = false; 90 if (show_module && module_sp) { 91 if (show_fullpaths) 92 *s << module_sp->GetFileSpec(); 93 else 94 *s << module_sp->GetFileSpec().GetFilename(); 95 s->PutChar('`'); 96 dumped_something = true; 97 } 98 99 if (function != nullptr) { 100 SymbolContext inline_parent_sc; 101 Address inline_parent_addr; 102 if (!show_function_name) { 103 s->Printf("<"); 104 dumped_something = true; 105 } else { 106 ConstString name; 107 if (!show_function_arguments) 108 name = function->GetNameNoArguments(); 109 if (!name) 110 name = function->GetName(); 111 if (name) 112 name.Dump(s); 113 } 114 115 if (addr.IsValid()) { 116 const addr_t function_offset = 117 addr.GetOffset() - 118 function->GetAddressRange().GetBaseAddress().GetOffset(); 119 if (!show_function_name) { 120 // Print +offset even if offset is 0 121 dumped_something = true; 122 s->Printf("+%" PRIu64 ">", function_offset); 123 } else if (function_offset) { 124 dumped_something = true; 125 s->Printf(" + %" PRIu64, function_offset); 126 } 127 } 128 129 if (GetParentOfInlinedScope(addr, inline_parent_sc, inline_parent_addr)) { 130 dumped_something = true; 131 Block *inlined_block = block->GetContainingInlinedBlock(); 132 const InlineFunctionInfo *inlined_block_info = 133 inlined_block->GetInlinedFunctionInfo(); 134 s->Printf( 135 " [inlined] %s", 136 inlined_block_info->GetName(function->GetLanguage()).GetCString()); 137 138 lldb_private::AddressRange block_range; 139 if (inlined_block->GetRangeContainingAddress(addr, block_range)) { 140 const addr_t inlined_function_offset = 141 addr.GetOffset() - block_range.GetBaseAddress().GetOffset(); 142 if (inlined_function_offset) { 143 s->Printf(" + %" PRIu64, inlined_function_offset); 144 } 145 } 146 const Declaration &call_site = inlined_block_info->GetCallSite(); 147 if (call_site.IsValid()) { 148 s->PutCString(" at "); 149 call_site.DumpStopContext(s, show_fullpaths); 150 } 151 if (show_inlined_frames) { 152 s->EOL(); 153 s->Indent(); 154 const bool show_function_name = true; 155 return inline_parent_sc.DumpStopContext( 156 s, exe_scope, inline_parent_addr, show_fullpaths, show_module, 157 show_inlined_frames, show_function_arguments, show_function_name); 158 } 159 } else { 160 if (line_entry.IsValid()) { 161 dumped_something = true; 162 s->PutCString(" at "); 163 if (line_entry.DumpStopContext(s, show_fullpaths)) 164 dumped_something = true; 165 } 166 } 167 } else if (symbol != nullptr) { 168 if (!show_function_name) { 169 s->Printf("<"); 170 dumped_something = true; 171 } else if (symbol->GetName()) { 172 dumped_something = true; 173 if (symbol->GetType() == eSymbolTypeTrampoline) 174 s->PutCString("symbol stub for: "); 175 symbol->GetName().Dump(s); 176 } 177 178 if (addr.IsValid() && symbol->ValueIsAddress()) { 179 const addr_t symbol_offset = 180 addr.GetOffset() - symbol->GetAddressRef().GetOffset(); 181 if (!show_function_name) { 182 // Print +offset even if offset is 0 183 dumped_something = true; 184 s->Printf("+%" PRIu64 ">", symbol_offset); 185 } else if (symbol_offset) { 186 dumped_something = true; 187 s->Printf(" + %" PRIu64, symbol_offset); 188 } 189 } 190 } else if (addr.IsValid()) { 191 addr.Dump(s, exe_scope, Address::DumpStyleModuleWithFileAddress); 192 dumped_something = true; 193 } 194 return dumped_something; 195 } 196 197 void SymbolContext::GetDescription(Stream *s, lldb::DescriptionLevel level, 198 Target *target) const { 199 if (module_sp) { 200 s->Indent(" Module: file = \""); 201 module_sp->GetFileSpec().Dump(s); 202 *s << '"'; 203 if (module_sp->GetArchitecture().IsValid()) 204 s->Printf(", arch = \"%s\"", 205 module_sp->GetArchitecture().GetArchitectureName()); 206 s->EOL(); 207 } 208 209 if (comp_unit != nullptr) { 210 s->Indent("CompileUnit: "); 211 comp_unit->GetDescription(s, level); 212 s->EOL(); 213 } 214 215 if (function != nullptr) { 216 s->Indent(" Function: "); 217 function->GetDescription(s, level, target); 218 s->EOL(); 219 220 Type *func_type = function->GetType(); 221 if (func_type) { 222 s->Indent(" FuncType: "); 223 func_type->GetDescription(s, level, false); 224 s->EOL(); 225 } 226 } 227 228 if (block != nullptr) { 229 std::vector<Block *> blocks; 230 blocks.push_back(block); 231 Block *parent_block = block->GetParent(); 232 233 while (parent_block) { 234 blocks.push_back(parent_block); 235 parent_block = parent_block->GetParent(); 236 } 237 std::vector<Block *>::reverse_iterator pos; 238 std::vector<Block *>::reverse_iterator begin = blocks.rbegin(); 239 std::vector<Block *>::reverse_iterator end = blocks.rend(); 240 for (pos = begin; pos != end; ++pos) { 241 if (pos == begin) 242 s->Indent(" Blocks: "); 243 else 244 s->Indent(" "); 245 (*pos)->GetDescription(s, function, level, target); 246 s->EOL(); 247 } 248 } 249 250 if (line_entry.IsValid()) { 251 s->Indent(" LineEntry: "); 252 line_entry.GetDescription(s, level, comp_unit, target, false); 253 s->EOL(); 254 } 255 256 if (symbol != nullptr) { 257 s->Indent(" Symbol: "); 258 symbol->GetDescription(s, level, target); 259 s->EOL(); 260 } 261 262 if (variable != nullptr) { 263 s->Indent(" Variable: "); 264 265 s->Printf("id = {0x%8.8" PRIx64 "}, ", variable->GetID()); 266 267 switch (variable->GetScope()) { 268 case eValueTypeVariableGlobal: 269 s->PutCString("kind = global, "); 270 break; 271 272 case eValueTypeVariableStatic: 273 s->PutCString("kind = static, "); 274 break; 275 276 case eValueTypeVariableArgument: 277 s->PutCString("kind = argument, "); 278 break; 279 280 case eValueTypeVariableLocal: 281 s->PutCString("kind = local, "); 282 break; 283 284 case eValueTypeVariableThreadLocal: 285 s->PutCString("kind = thread local, "); 286 break; 287 288 default: 289 break; 290 } 291 292 s->Printf("name = \"%s\"\n", variable->GetName().GetCString()); 293 } 294 } 295 296 uint32_t SymbolContext::GetResolvedMask() const { 297 uint32_t resolved_mask = 0; 298 if (target_sp) 299 resolved_mask |= eSymbolContextTarget; 300 if (module_sp) 301 resolved_mask |= eSymbolContextModule; 302 if (comp_unit) 303 resolved_mask |= eSymbolContextCompUnit; 304 if (function) 305 resolved_mask |= eSymbolContextFunction; 306 if (block) 307 resolved_mask |= eSymbolContextBlock; 308 if (line_entry.IsValid()) 309 resolved_mask |= eSymbolContextLineEntry; 310 if (symbol) 311 resolved_mask |= eSymbolContextSymbol; 312 if (variable) 313 resolved_mask |= eSymbolContextVariable; 314 return resolved_mask; 315 } 316 317 void SymbolContext::Dump(Stream *s, Target *target) const { 318 *s << this << ": "; 319 s->Indent(); 320 s->PutCString("SymbolContext"); 321 s->IndentMore(); 322 s->EOL(); 323 s->IndentMore(); 324 s->Indent(); 325 *s << "Module = " << module_sp.get() << ' '; 326 if (module_sp) 327 module_sp->GetFileSpec().Dump(s); 328 s->EOL(); 329 s->Indent(); 330 *s << "CompileUnit = " << comp_unit; 331 if (comp_unit != nullptr) 332 *s << " {0x" << comp_unit->GetID() << "} " 333 << *(static_cast<FileSpec *>(comp_unit)); 334 s->EOL(); 335 s->Indent(); 336 *s << "Function = " << function; 337 if (function != nullptr) { 338 *s << " {0x" << function->GetID() << "} " << function->GetType()->GetName() 339 << ", address-range = "; 340 function->GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, 341 Address::DumpStyleModuleWithFileAddress); 342 s->EOL(); 343 s->Indent(); 344 Type *func_type = function->GetType(); 345 if (func_type) { 346 *s << " Type = "; 347 func_type->Dump(s, false); 348 } 349 } 350 s->EOL(); 351 s->Indent(); 352 *s << "Block = " << block; 353 if (block != nullptr) 354 *s << " {0x" << block->GetID() << '}'; 355 // Dump the block and pass it a negative depth to we print all the parent 356 // blocks if (block != NULL) 357 // block->Dump(s, function->GetFileAddress(), INT_MIN); 358 s->EOL(); 359 s->Indent(); 360 *s << "LineEntry = "; 361 line_entry.Dump(s, target, true, Address::DumpStyleLoadAddress, 362 Address::DumpStyleModuleWithFileAddress, true); 363 s->EOL(); 364 s->Indent(); 365 *s << "Symbol = " << symbol; 366 if (symbol != nullptr && symbol->GetMangled()) 367 *s << ' ' << symbol->GetName().AsCString(); 368 s->EOL(); 369 *s << "Variable = " << variable; 370 if (variable != nullptr) { 371 *s << " {0x" << variable->GetID() << "} " << variable->GetType()->GetName(); 372 s->EOL(); 373 } 374 s->IndentLess(); 375 s->IndentLess(); 376 } 377 378 bool lldb_private::operator==(const SymbolContext &lhs, 379 const SymbolContext &rhs) { 380 return lhs.function == rhs.function && lhs.symbol == rhs.symbol && 381 lhs.module_sp.get() == rhs.module_sp.get() && 382 lhs.comp_unit == rhs.comp_unit && 383 lhs.target_sp.get() == rhs.target_sp.get() && 384 LineEntry::Compare(lhs.line_entry, rhs.line_entry) == 0 && 385 lhs.variable == rhs.variable; 386 } 387 388 bool lldb_private::operator!=(const SymbolContext &lhs, 389 const SymbolContext &rhs) { 390 return !(lhs == rhs); 391 } 392 393 bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx, 394 bool use_inline_block_range, 395 AddressRange &range) const { 396 if ((scope & eSymbolContextLineEntry) && line_entry.IsValid()) { 397 range = line_entry.range; 398 return true; 399 } 400 401 if ((scope & eSymbolContextBlock) && (block != nullptr)) { 402 if (use_inline_block_range) { 403 Block *inline_block = block->GetContainingInlinedBlock(); 404 if (inline_block) 405 return inline_block->GetRangeAtIndex(range_idx, range); 406 } else { 407 return block->GetRangeAtIndex(range_idx, range); 408 } 409 } 410 411 if ((scope & eSymbolContextFunction) && (function != nullptr)) { 412 if (range_idx == 0) { 413 range = function->GetAddressRange(); 414 return true; 415 } 416 } 417 418 if ((scope & eSymbolContextSymbol) && (symbol != nullptr)) { 419 if (range_idx == 0) { 420 if (symbol->ValueIsAddress()) { 421 range.GetBaseAddress() = symbol->GetAddressRef(); 422 range.SetByteSize(symbol->GetByteSize()); 423 return true; 424 } 425 } 426 } 427 range.Clear(); 428 return false; 429 } 430 431 LanguageType SymbolContext::GetLanguage() const { 432 LanguageType lang; 433 if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) { 434 return lang; 435 } else if (variable && 436 (lang = variable->GetLanguage()) != eLanguageTypeUnknown) { 437 return lang; 438 } else if (symbol && (lang = symbol->GetLanguage()) != eLanguageTypeUnknown) { 439 return lang; 440 } else if (comp_unit && 441 (lang = comp_unit->GetLanguage()) != eLanguageTypeUnknown) { 442 return lang; 443 } else if (symbol) { 444 // If all else fails, try to guess the language from the name. 445 return symbol->GetMangled().GuessLanguage(); 446 } 447 return eLanguageTypeUnknown; 448 } 449 450 bool SymbolContext::GetParentOfInlinedScope(const Address &curr_frame_pc, 451 SymbolContext &next_frame_sc, 452 Address &next_frame_pc) const { 453 next_frame_sc.Clear(false); 454 next_frame_pc.Clear(); 455 456 if (block) { 457 // const addr_t curr_frame_file_addr = curr_frame_pc.GetFileAddress(); 458 459 // In order to get the parent of an inlined function we first need to see 460 // if we are in an inlined block as "this->block" could be an inlined 461 // block, or a parent of "block" could be. So lets check if this block or 462 // one of this blocks parents is an inlined function. 463 Block *curr_inlined_block = block->GetContainingInlinedBlock(); 464 if (curr_inlined_block) { 465 // "this->block" is contained in an inline function block, so to get the 466 // scope above the inlined block, we get the parent of the inlined block 467 // itself 468 Block *next_frame_block = curr_inlined_block->GetParent(); 469 // Now calculate the symbol context of the containing block 470 next_frame_block->CalculateSymbolContext(&next_frame_sc); 471 472 // If we get here we weren't able to find the return line entry using the 473 // nesting of the blocks and the line table. So just use the call site 474 // info from our inlined block. 475 476 AddressRange range; 477 if (curr_inlined_block->GetRangeContainingAddress(curr_frame_pc, range)) { 478 // To see there this new frame block it, we need to look at the call 479 // site information from 480 const InlineFunctionInfo *curr_inlined_block_inlined_info = 481 curr_inlined_block->GetInlinedFunctionInfo(); 482 next_frame_pc = range.GetBaseAddress(); 483 next_frame_sc.line_entry.range.GetBaseAddress() = next_frame_pc; 484 next_frame_sc.line_entry.file = 485 curr_inlined_block_inlined_info->GetCallSite().GetFile(); 486 next_frame_sc.line_entry.original_file = 487 curr_inlined_block_inlined_info->GetCallSite().GetFile(); 488 next_frame_sc.line_entry.line = 489 curr_inlined_block_inlined_info->GetCallSite().GetLine(); 490 next_frame_sc.line_entry.column = 491 curr_inlined_block_inlined_info->GetCallSite().GetColumn(); 492 return true; 493 } else { 494 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); 495 496 if (log) { 497 log->Printf( 498 "warning: inlined block 0x%8.8" PRIx64 499 " doesn't have a range that contains file address 0x%" PRIx64, 500 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress()); 501 } 502 #ifdef LLDB_CONFIGURATION_DEBUG 503 else { 504 ObjectFile *objfile = nullptr; 505 if (module_sp) { 506 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); 507 if (symbol_vendor) { 508 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile(); 509 if (symbol_file) 510 objfile = symbol_file->GetObjectFile(); 511 } 512 } 513 if (objfile) { 514 Host::SystemLog( 515 Host::eSystemLogWarning, 516 "warning: inlined block 0x%8.8" PRIx64 517 " doesn't have a range that contains file address 0x%" PRIx64 518 " in %s\n", 519 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress(), 520 objfile->GetFileSpec().GetPath().c_str()); 521 } else { 522 Host::SystemLog( 523 Host::eSystemLogWarning, 524 "warning: inlined block 0x%8.8" PRIx64 525 " doesn't have a range that contains file address 0x%" PRIx64 526 "\n", 527 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress()); 528 } 529 } 530 #endif 531 } 532 } 533 } 534 535 return false; 536 } 537 538 Block *SymbolContext::GetFunctionBlock() { 539 if (function) { 540 if (block) { 541 // If this symbol context has a block, check to see if this block is 542 // itself, or is contained within a block with inlined function 543 // information. If so, then the inlined block is the block that defines 544 // the function. 545 Block *inlined_block = block->GetContainingInlinedBlock(); 546 if (inlined_block) 547 return inlined_block; 548 549 // The block in this symbol context is not inside an inlined block, so 550 // the block that defines the function is the function's top level block, 551 // which is returned below. 552 } 553 554 // There is no block information in this symbol context, so we must assume 555 // that the block that is desired is the top level block of the function 556 // itself. 557 return &function->GetBlock(true); 558 } 559 return nullptr; 560 } 561 562 bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language, 563 bool &is_instance_method, 564 ConstString &language_object_name) 565 566 { 567 Block *function_block = GetFunctionBlock(); 568 if (function_block) { 569 CompilerDeclContext decl_ctx = function_block->GetDeclContext(); 570 if (decl_ctx) 571 return decl_ctx.IsClassMethod(&language, &is_instance_method, 572 &language_object_name); 573 } 574 return false; 575 } 576 577 void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const { 578 Block *curr_block = block; 579 bool isInlinedblock = false; 580 if (curr_block != nullptr && 581 curr_block->GetContainingInlinedBlock() != nullptr) 582 isInlinedblock = true; 583 584 // Find all types that match the current block if we have one and put them 585 // first in the list. Keep iterating up through all blocks. 586 while (curr_block != nullptr && !isInlinedblock) { 587 type_map.ForEach( 588 [curr_block, &type_list](const lldb::TypeSP &type_sp) -> bool { 589 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 590 if (scs && curr_block == scs->CalculateSymbolContextBlock()) 591 type_list.Insert(type_sp); 592 return true; // Keep iterating 593 }); 594 595 // Remove any entries that are now in "type_list" from "type_map" since we 596 // can't remove from type_map while iterating 597 type_list.ForEach([&type_map](const lldb::TypeSP &type_sp) -> bool { 598 type_map.Remove(type_sp); 599 return true; // Keep iterating 600 }); 601 curr_block = curr_block->GetParent(); 602 } 603 // Find all types that match the current function, if we have onem, and put 604 // them next in the list. 605 if (function != nullptr && !type_map.Empty()) { 606 const size_t old_type_list_size = type_list.GetSize(); 607 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 608 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 609 if (scs && function == scs->CalculateSymbolContextFunction()) 610 type_list.Insert(type_sp); 611 return true; // Keep iterating 612 }); 613 614 // Remove any entries that are now in "type_list" from "type_map" since we 615 // can't remove from type_map while iterating 616 const size_t new_type_list_size = type_list.GetSize(); 617 if (new_type_list_size > old_type_list_size) { 618 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 619 type_map.Remove(type_list.GetTypeAtIndex(i)); 620 } 621 } 622 // Find all types that match the current compile unit, if we have one, and 623 // put them next in the list. 624 if (comp_unit != nullptr && !type_map.Empty()) { 625 const size_t old_type_list_size = type_list.GetSize(); 626 627 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 628 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 629 if (scs && comp_unit == scs->CalculateSymbolContextCompileUnit()) 630 type_list.Insert(type_sp); 631 return true; // Keep iterating 632 }); 633 634 // Remove any entries that are now in "type_list" from "type_map" since we 635 // can't remove from type_map while iterating 636 const size_t new_type_list_size = type_list.GetSize(); 637 if (new_type_list_size > old_type_list_size) { 638 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 639 type_map.Remove(type_list.GetTypeAtIndex(i)); 640 } 641 } 642 // Find all types that match the current module, if we have one, and put them 643 // next in the list. 644 if (module_sp && !type_map.Empty()) { 645 const size_t old_type_list_size = type_list.GetSize(); 646 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 647 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 648 if (scs && module_sp == scs->CalculateSymbolContextModule()) 649 type_list.Insert(type_sp); 650 return true; // Keep iterating 651 }); 652 // Remove any entries that are now in "type_list" from "type_map" since we 653 // can't remove from type_map while iterating 654 const size_t new_type_list_size = type_list.GetSize(); 655 if (new_type_list_size > old_type_list_size) { 656 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 657 type_map.Remove(type_list.GetTypeAtIndex(i)); 658 } 659 } 660 // Any types that are left get copied into the list an any order. 661 if (!type_map.Empty()) { 662 type_map.ForEach([&type_list](const lldb::TypeSP &type_sp) -> bool { 663 type_list.Insert(type_sp); 664 return true; // Keep iterating 665 }); 666 } 667 } 668 669 ConstString 670 SymbolContext::GetFunctionName(Mangled::NamePreference preference) const { 671 if (function) { 672 if (block) { 673 Block *inlined_block = block->GetContainingInlinedBlock(); 674 675 if (inlined_block) { 676 const InlineFunctionInfo *inline_info = 677 inlined_block->GetInlinedFunctionInfo(); 678 if (inline_info) 679 return inline_info->GetName(function->GetLanguage()); 680 } 681 } 682 return function->GetMangled().GetName(function->GetLanguage(), preference); 683 } else if (symbol && symbol->ValueIsAddress()) { 684 return symbol->GetMangled().GetName(symbol->GetLanguage(), preference); 685 } else { 686 // No function, return an empty string. 687 return ConstString(); 688 } 689 } 690 691 LineEntry SymbolContext::GetFunctionStartLineEntry() const { 692 LineEntry line_entry; 693 Address start_addr; 694 if (block) { 695 Block *inlined_block = block->GetContainingInlinedBlock(); 696 if (inlined_block) { 697 if (inlined_block->GetStartAddress(start_addr)) { 698 if (start_addr.CalculateSymbolContextLineEntry(line_entry)) 699 return line_entry; 700 } 701 return LineEntry(); 702 } 703 } 704 705 if (function) { 706 if (function->GetAddressRange() 707 .GetBaseAddress() 708 .CalculateSymbolContextLineEntry(line_entry)) 709 return line_entry; 710 } 711 return LineEntry(); 712 } 713 714 bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, 715 AddressRange &range, 716 Status &error) { 717 if (!line_entry.IsValid()) { 718 error.SetErrorString("Symbol context has no line table."); 719 return false; 720 } 721 722 range = line_entry.range; 723 if (line_entry.line > end_line) { 724 error.SetErrorStringWithFormat( 725 "end line option %d must be after the current line: %d", end_line, 726 line_entry.line); 727 return false; 728 } 729 730 uint32_t line_index = 0; 731 bool found = false; 732 while (true) { 733 LineEntry this_line; 734 line_index = comp_unit->FindLineEntry(line_index, line_entry.line, nullptr, 735 false, &this_line); 736 if (line_index == UINT32_MAX) 737 break; 738 if (LineEntry::Compare(this_line, line_entry) == 0) { 739 found = true; 740 break; 741 } 742 } 743 744 LineEntry end_entry; 745 if (!found) { 746 // Can't find the index of the SymbolContext's line entry in the 747 // SymbolContext's CompUnit. 748 error.SetErrorString( 749 "Can't find the current line entry in the CompUnit - can't process " 750 "the end-line option"); 751 return false; 752 } 753 754 line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false, 755 &end_entry); 756 if (line_index == UINT32_MAX) { 757 error.SetErrorStringWithFormat( 758 "could not find a line table entry corresponding " 759 "to end line number %d", 760 end_line); 761 return false; 762 } 763 764 Block *func_block = GetFunctionBlock(); 765 if (func_block && 766 func_block->GetRangeIndexContainingAddress( 767 end_entry.range.GetBaseAddress()) == UINT32_MAX) { 768 error.SetErrorStringWithFormat( 769 "end line number %d is not contained within the current function.", 770 end_line); 771 return false; 772 } 773 774 lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() - 775 range.GetBaseAddress().GetFileAddress(); 776 range.SetByteSize(range_size); 777 return true; 778 } 779 780 const Symbol * 781 SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) { 782 error.Clear(); 783 784 if (!target_sp) { 785 return nullptr; 786 } 787 788 Target &target = *target_sp; 789 Module *module = module_sp.get(); 790 791 auto ProcessMatches = [this, &name, &target, module] 792 (SymbolContextList &sc_list, Status &error) -> const Symbol* { 793 llvm::SmallVector<const Symbol *, 1> external_symbols; 794 llvm::SmallVector<const Symbol *, 1> internal_symbols; 795 const uint32_t matches = sc_list.GetSize(); 796 for (uint32_t i = 0; i < matches; ++i) { 797 SymbolContext sym_ctx; 798 sc_list.GetContextAtIndex(i, sym_ctx); 799 if (sym_ctx.symbol) { 800 const Symbol *symbol = sym_ctx.symbol; 801 const Address sym_address = symbol->GetAddress(); 802 803 if (sym_address.IsValid()) { 804 switch (symbol->GetType()) { 805 case eSymbolTypeData: 806 case eSymbolTypeRuntime: 807 case eSymbolTypeAbsolute: 808 case eSymbolTypeObjCClass: 809 case eSymbolTypeObjCMetaClass: 810 case eSymbolTypeObjCIVar: 811 if (symbol->GetDemangledNameIsSynthesized()) { 812 // If the demangled name was synthesized, then don't use it for 813 // expressions. Only let the symbol match if the mangled named 814 // matches for these symbols. 815 if (symbol->GetMangled().GetMangledName() != name) 816 break; 817 } 818 if (symbol->IsExternal()) { 819 external_symbols.push_back(symbol); 820 } else { 821 internal_symbols.push_back(symbol); 822 } 823 break; 824 case eSymbolTypeReExported: { 825 ConstString reexport_name = symbol->GetReExportedSymbolName(); 826 if (reexport_name) { 827 ModuleSP reexport_module_sp; 828 ModuleSpec reexport_module_spec; 829 reexport_module_spec.GetPlatformFileSpec() = 830 symbol->GetReExportedSymbolSharedLibrary(); 831 if (reexport_module_spec.GetPlatformFileSpec()) { 832 reexport_module_sp = 833 target.GetImages().FindFirstModule(reexport_module_spec); 834 if (!reexport_module_sp) { 835 reexport_module_spec.GetPlatformFileSpec() 836 .GetDirectory() 837 .Clear(); 838 reexport_module_sp = 839 target.GetImages().FindFirstModule(reexport_module_spec); 840 } 841 } 842 // Don't allow us to try and resolve a re-exported symbol if it 843 // is the same as the current symbol 844 if (name == symbol->GetReExportedSymbolName() && 845 module == reexport_module_sp.get()) 846 return nullptr; 847 848 return FindBestGlobalDataSymbol( 849 symbol->GetReExportedSymbolName(), error); 850 } 851 } break; 852 853 case eSymbolTypeCode: // We already lookup functions elsewhere 854 case eSymbolTypeVariable: 855 case eSymbolTypeLocal: 856 case eSymbolTypeParam: 857 case eSymbolTypeTrampoline: 858 case eSymbolTypeInvalid: 859 case eSymbolTypeException: 860 case eSymbolTypeSourceFile: 861 case eSymbolTypeHeaderFile: 862 case eSymbolTypeObjectFile: 863 case eSymbolTypeCommonBlock: 864 case eSymbolTypeBlock: 865 case eSymbolTypeVariableType: 866 case eSymbolTypeLineEntry: 867 case eSymbolTypeLineHeader: 868 case eSymbolTypeScopeBegin: 869 case eSymbolTypeScopeEnd: 870 case eSymbolTypeAdditional: 871 case eSymbolTypeCompiler: 872 case eSymbolTypeInstrumentation: 873 case eSymbolTypeUndefined: 874 case eSymbolTypeResolver: 875 break; 876 } 877 } 878 } 879 } 880 881 if (external_symbols.size() > 1) { 882 StreamString ss; 883 ss.Printf("Multiple external symbols found for '%s'\n", name.AsCString()); 884 for (const Symbol *symbol : external_symbols) { 885 symbol->GetDescription(&ss, eDescriptionLevelFull, &target); 886 } 887 ss.PutChar('\n'); 888 error.SetErrorString(ss.GetData()); 889 return nullptr; 890 } else if (external_symbols.size()) { 891 return external_symbols[0]; 892 } else if (internal_symbols.size() > 1) { 893 StreamString ss; 894 ss.Printf("Multiple internal symbols found for '%s'\n", name.AsCString()); 895 for (const Symbol *symbol : internal_symbols) { 896 symbol->GetDescription(&ss, eDescriptionLevelVerbose, &target); 897 ss.PutChar('\n'); 898 } 899 error.SetErrorString(ss.GetData()); 900 return nullptr; 901 } else if (internal_symbols.size()) { 902 return internal_symbols[0]; 903 } else { 904 return nullptr; 905 } 906 }; 907 908 if (module) { 909 SymbolContextList sc_list; 910 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list); 911 const Symbol *const module_symbol = ProcessMatches(sc_list, error); 912 913 if (!error.Success()) { 914 return nullptr; 915 } else if (module_symbol) { 916 return module_symbol; 917 } 918 } 919 920 { 921 SymbolContextList sc_list; 922 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, 923 sc_list); 924 const Symbol *const target_symbol = ProcessMatches(sc_list, error); 925 926 if (!error.Success()) { 927 return nullptr; 928 } else if (target_symbol) { 929 return target_symbol; 930 } 931 } 932 933 return nullptr; // no error; we just didn't find anything 934 } 935 936 937 // 938 // SymbolContextSpecifier 939 // 940 941 SymbolContextSpecifier::SymbolContextSpecifier(const TargetSP &target_sp) 942 : m_target_sp(target_sp), m_module_spec(), m_module_sp(), m_file_spec_up(), 943 m_start_line(0), m_end_line(0), m_function_spec(), m_class_name(), 944 m_address_range_up(), m_type(eNothingSpecified) {} 945 946 SymbolContextSpecifier::~SymbolContextSpecifier() {} 947 948 bool SymbolContextSpecifier::AddLineSpecification(uint32_t line_no, 949 SpecificationType type) { 950 bool return_value = true; 951 switch (type) { 952 case eNothingSpecified: 953 Clear(); 954 break; 955 case eLineStartSpecified: 956 m_start_line = line_no; 957 m_type |= eLineStartSpecified; 958 break; 959 case eLineEndSpecified: 960 m_end_line = line_no; 961 m_type |= eLineEndSpecified; 962 break; 963 default: 964 return_value = false; 965 break; 966 } 967 return return_value; 968 } 969 970 bool SymbolContextSpecifier::AddSpecification(const char *spec_string, 971 SpecificationType type) { 972 bool return_value = true; 973 switch (type) { 974 case eNothingSpecified: 975 Clear(); 976 break; 977 case eModuleSpecified: { 978 // See if we can find the Module, if so stick it in the SymbolContext. 979 FileSpec module_file_spec(spec_string); 980 ModuleSpec module_spec(module_file_spec); 981 lldb::ModuleSP module_sp( 982 m_target_sp->GetImages().FindFirstModule(module_spec)); 983 m_type |= eModuleSpecified; 984 if (module_sp) 985 m_module_sp = module_sp; 986 else 987 m_module_spec.assign(spec_string); 988 } break; 989 case eFileSpecified: 990 // CompUnits can't necessarily be resolved here, since an inlined function 991 // might show up in a number of CompUnits. Instead we just convert to a 992 // FileSpec and store it away. 993 m_file_spec_up.reset(new FileSpec(spec_string)); 994 m_type |= eFileSpecified; 995 break; 996 case eLineStartSpecified: 997 m_start_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value); 998 if (return_value) 999 m_type |= eLineStartSpecified; 1000 break; 1001 case eLineEndSpecified: 1002 m_end_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value); 1003 if (return_value) 1004 m_type |= eLineEndSpecified; 1005 break; 1006 case eFunctionSpecified: 1007 m_function_spec.assign(spec_string); 1008 m_type |= eFunctionSpecified; 1009 break; 1010 case eClassOrNamespaceSpecified: 1011 Clear(); 1012 m_class_name.assign(spec_string); 1013 m_type = eClassOrNamespaceSpecified; 1014 break; 1015 case eAddressRangeSpecified: 1016 // Not specified yet... 1017 break; 1018 } 1019 1020 return return_value; 1021 } 1022 1023 void SymbolContextSpecifier::Clear() { 1024 m_module_spec.clear(); 1025 m_file_spec_up.reset(); 1026 m_function_spec.clear(); 1027 m_class_name.clear(); 1028 m_start_line = 0; 1029 m_end_line = 0; 1030 m_address_range_up.reset(); 1031 1032 m_type = eNothingSpecified; 1033 } 1034 1035 bool SymbolContextSpecifier::SymbolContextMatches(SymbolContext &sc) { 1036 if (m_type == eNothingSpecified) 1037 return true; 1038 1039 if (m_target_sp.get() != sc.target_sp.get()) 1040 return false; 1041 1042 if (m_type & eModuleSpecified) { 1043 if (sc.module_sp) { 1044 if (m_module_sp.get() != nullptr) { 1045 if (m_module_sp.get() != sc.module_sp.get()) 1046 return false; 1047 } else { 1048 FileSpec module_file_spec(m_module_spec); 1049 if (!FileSpec::Equal(module_file_spec, sc.module_sp->GetFileSpec(), 1050 false)) 1051 return false; 1052 } 1053 } 1054 } 1055 if (m_type & eFileSpecified) { 1056 if (m_file_spec_up) { 1057 // If we don't have a block or a comp_unit, then we aren't going to match 1058 // a source file. 1059 if (sc.block == nullptr && sc.comp_unit == nullptr) 1060 return false; 1061 1062 // Check if the block is present, and if so is it inlined: 1063 bool was_inlined = false; 1064 if (sc.block != nullptr) { 1065 const InlineFunctionInfo *inline_info = 1066 sc.block->GetInlinedFunctionInfo(); 1067 if (inline_info != nullptr) { 1068 was_inlined = true; 1069 if (!FileSpec::Equal(inline_info->GetDeclaration().GetFile(), 1070 *(m_file_spec_up.get()), false)) 1071 return false; 1072 } 1073 } 1074 1075 // Next check the comp unit, but only if the SymbolContext was not 1076 // inlined. 1077 if (!was_inlined && sc.comp_unit != nullptr) { 1078 if (!FileSpec::Equal(*(sc.comp_unit), *(m_file_spec_up.get()), false)) 1079 return false; 1080 } 1081 } 1082 } 1083 if (m_type & eLineStartSpecified || m_type & eLineEndSpecified) { 1084 if (sc.line_entry.line < m_start_line || sc.line_entry.line > m_end_line) 1085 return false; 1086 } 1087 1088 if (m_type & eFunctionSpecified) { 1089 // First check the current block, and if it is inlined, get the inlined 1090 // function name: 1091 bool was_inlined = false; 1092 ConstString func_name(m_function_spec.c_str()); 1093 1094 if (sc.block != nullptr) { 1095 const InlineFunctionInfo *inline_info = 1096 sc.block->GetInlinedFunctionInfo(); 1097 if (inline_info != nullptr) { 1098 was_inlined = true; 1099 const Mangled &name = inline_info->GetMangled(); 1100 if (!name.NameMatches(func_name, sc.function->GetLanguage())) 1101 return false; 1102 } 1103 } 1104 // If it wasn't inlined, check the name in the function or symbol: 1105 if (!was_inlined) { 1106 if (sc.function != nullptr) { 1107 if (!sc.function->GetMangled().NameMatches(func_name, 1108 sc.function->GetLanguage())) 1109 return false; 1110 } else if (sc.symbol != nullptr) { 1111 if (!sc.symbol->GetMangled().NameMatches(func_name, 1112 sc.symbol->GetLanguage())) 1113 return false; 1114 } 1115 } 1116 } 1117 1118 return true; 1119 } 1120 1121 bool SymbolContextSpecifier::AddressMatches(lldb::addr_t addr) { 1122 if (m_type & eAddressRangeSpecified) { 1123 1124 } else { 1125 Address match_address(addr, nullptr); 1126 SymbolContext sc; 1127 m_target_sp->GetImages().ResolveSymbolContextForAddress( 1128 match_address, eSymbolContextEverything, sc); 1129 return SymbolContextMatches(sc); 1130 } 1131 return true; 1132 } 1133 1134 void SymbolContextSpecifier::GetDescription( 1135 Stream *s, lldb::DescriptionLevel level) const { 1136 char path_str[PATH_MAX + 1]; 1137 1138 if (m_type == eNothingSpecified) { 1139 s->Printf("Nothing specified.\n"); 1140 } 1141 1142 if (m_type == eModuleSpecified) { 1143 s->Indent(); 1144 if (m_module_sp) { 1145 m_module_sp->GetFileSpec().GetPath(path_str, PATH_MAX); 1146 s->Printf("Module: %s\n", path_str); 1147 } else 1148 s->Printf("Module: %s\n", m_module_spec.c_str()); 1149 } 1150 1151 if (m_type == eFileSpecified && m_file_spec_up != nullptr) { 1152 m_file_spec_up->GetPath(path_str, PATH_MAX); 1153 s->Indent(); 1154 s->Printf("File: %s", path_str); 1155 if (m_type == eLineStartSpecified) { 1156 s->Printf(" from line %" PRIu64 "", (uint64_t)m_start_line); 1157 if (m_type == eLineEndSpecified) 1158 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line); 1159 else 1160 s->Printf("to end"); 1161 } else if (m_type == eLineEndSpecified) { 1162 s->Printf(" from start to line %" PRIu64 "", (uint64_t)m_end_line); 1163 } 1164 s->Printf(".\n"); 1165 } 1166 1167 if (m_type == eLineStartSpecified) { 1168 s->Indent(); 1169 s->Printf("From line %" PRIu64 "", (uint64_t)m_start_line); 1170 if (m_type == eLineEndSpecified) 1171 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line); 1172 else 1173 s->Printf("to end"); 1174 s->Printf(".\n"); 1175 } else if (m_type == eLineEndSpecified) { 1176 s->Printf("From start to line %" PRIu64 ".\n", (uint64_t)m_end_line); 1177 } 1178 1179 if (m_type == eFunctionSpecified) { 1180 s->Indent(); 1181 s->Printf("Function: %s.\n", m_function_spec.c_str()); 1182 } 1183 1184 if (m_type == eClassOrNamespaceSpecified) { 1185 s->Indent(); 1186 s->Printf("Class name: %s.\n", m_class_name.c_str()); 1187 } 1188 1189 if (m_type == eAddressRangeSpecified && m_address_range_up != nullptr) { 1190 s->Indent(); 1191 s->PutCString("Address range: "); 1192 m_address_range_up->Dump(s, m_target_sp.get(), 1193 Address::DumpStyleLoadAddress, 1194 Address::DumpStyleFileAddress); 1195 s->PutCString("\n"); 1196 } 1197 } 1198 1199 // 1200 // SymbolContextList 1201 // 1202 1203 SymbolContextList::SymbolContextList() : m_symbol_contexts() {} 1204 1205 SymbolContextList::~SymbolContextList() {} 1206 1207 void SymbolContextList::Append(const SymbolContext &sc) { 1208 m_symbol_contexts.push_back(sc); 1209 } 1210 1211 void SymbolContextList::Append(const SymbolContextList &sc_list) { 1212 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end(); 1213 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) 1214 m_symbol_contexts.push_back(*pos); 1215 } 1216 1217 uint32_t SymbolContextList::AppendIfUnique(const SymbolContextList &sc_list, 1218 bool merge_symbol_into_function) { 1219 uint32_t unique_sc_add_count = 0; 1220 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end(); 1221 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) { 1222 if (AppendIfUnique(*pos, merge_symbol_into_function)) 1223 ++unique_sc_add_count; 1224 } 1225 return unique_sc_add_count; 1226 } 1227 1228 bool SymbolContextList::AppendIfUnique(const SymbolContext &sc, 1229 bool merge_symbol_into_function) { 1230 collection::iterator pos, end = m_symbol_contexts.end(); 1231 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1232 if (*pos == sc) 1233 return false; 1234 } 1235 if (merge_symbol_into_function && sc.symbol != nullptr && 1236 sc.comp_unit == nullptr && sc.function == nullptr && 1237 sc.block == nullptr && !sc.line_entry.IsValid()) { 1238 if (sc.symbol->ValueIsAddress()) { 1239 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1240 // Don't merge symbols into inlined function symbol contexts 1241 if (pos->block && pos->block->GetContainingInlinedBlock()) 1242 continue; 1243 1244 if (pos->function) { 1245 if (pos->function->GetAddressRange().GetBaseAddress() == 1246 sc.symbol->GetAddressRef()) { 1247 // Do we already have a function with this symbol? 1248 if (pos->symbol == sc.symbol) 1249 return false; 1250 if (pos->symbol == nullptr) { 1251 pos->symbol = sc.symbol; 1252 return false; 1253 } 1254 } 1255 } 1256 } 1257 } 1258 } 1259 m_symbol_contexts.push_back(sc); 1260 return true; 1261 } 1262 1263 void SymbolContextList::Clear() { m_symbol_contexts.clear(); } 1264 1265 void SymbolContextList::Dump(Stream *s, Target *target) const { 1266 1267 *s << this << ": "; 1268 s->Indent(); 1269 s->PutCString("SymbolContextList"); 1270 s->EOL(); 1271 s->IndentMore(); 1272 1273 collection::const_iterator pos, end = m_symbol_contexts.end(); 1274 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1275 // pos->Dump(s, target); 1276 pos->GetDescription(s, eDescriptionLevelVerbose, target); 1277 } 1278 s->IndentLess(); 1279 } 1280 1281 bool SymbolContextList::GetContextAtIndex(size_t idx, SymbolContext &sc) const { 1282 if (idx < m_symbol_contexts.size()) { 1283 sc = m_symbol_contexts[idx]; 1284 return true; 1285 } 1286 return false; 1287 } 1288 1289 bool SymbolContextList::RemoveContextAtIndex(size_t idx) { 1290 if (idx < m_symbol_contexts.size()) { 1291 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx); 1292 return true; 1293 } 1294 return false; 1295 } 1296 1297 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); } 1298 1299 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const { 1300 uint32_t match_count = 0; 1301 const size_t size = m_symbol_contexts.size(); 1302 for (size_t idx = 0; idx < size; ++idx) { 1303 if (m_symbol_contexts[idx].line_entry.line == line) 1304 ++match_count; 1305 } 1306 return match_count; 1307 } 1308 1309 void SymbolContextList::GetDescription(Stream *s, lldb::DescriptionLevel level, 1310 Target *target) const { 1311 const size_t size = m_symbol_contexts.size(); 1312 for (size_t idx = 0; idx < size; ++idx) 1313 m_symbol_contexts[idx].GetDescription(s, level, target); 1314 } 1315 1316 bool lldb_private::operator==(const SymbolContextList &lhs, 1317 const SymbolContextList &rhs) { 1318 const uint32_t size = lhs.GetSize(); 1319 if (size != rhs.GetSize()) 1320 return false; 1321 1322 SymbolContext lhs_sc; 1323 SymbolContext rhs_sc; 1324 for (uint32_t i = 0; i < size; ++i) { 1325 lhs.GetContextAtIndex(i, lhs_sc); 1326 rhs.GetContextAtIndex(i, rhs_sc); 1327 if (lhs_sc != rhs_sc) 1328 return false; 1329 } 1330 return true; 1331 } 1332 1333 bool lldb_private::operator!=(const SymbolContextList &lhs, 1334 const SymbolContextList &rhs) { 1335 return !(lhs == rhs); 1336 } 1337