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/CompileUnit.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Symbol/Symbol.h" 19 #include "lldb/Symbol/SymbolFile.h" 20 #include "lldb/Symbol/SymbolVendor.h" 21 #include "lldb/Symbol/Variable.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/StreamString.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 LLDB_LOGF( 498 log, 499 "warning: inlined block 0x%8.8" PRIx64 500 " doesn't have a range that contains file address 0x%" PRIx64, 501 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress()); 502 } 503 #ifdef LLDB_CONFIGURATION_DEBUG 504 else { 505 ObjectFile *objfile = nullptr; 506 if (module_sp) { 507 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); 508 if (symbol_vendor) { 509 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile(); 510 if (symbol_file) 511 objfile = symbol_file->GetObjectFile(); 512 } 513 } 514 if (objfile) { 515 Host::SystemLog( 516 Host::eSystemLogWarning, 517 "warning: inlined block 0x%8.8" PRIx64 518 " doesn't have a range that contains file address 0x%" PRIx64 519 " in %s\n", 520 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress(), 521 objfile->GetFileSpec().GetPath().c_str()); 522 } else { 523 Host::SystemLog( 524 Host::eSystemLogWarning, 525 "warning: inlined block 0x%8.8" PRIx64 526 " doesn't have a range that contains file address 0x%" PRIx64 527 "\n", 528 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress()); 529 } 530 } 531 #endif 532 } 533 } 534 } 535 536 return false; 537 } 538 539 Block *SymbolContext::GetFunctionBlock() { 540 if (function) { 541 if (block) { 542 // If this symbol context has a block, check to see if this block is 543 // itself, or is contained within a block with inlined function 544 // information. If so, then the inlined block is the block that defines 545 // the function. 546 Block *inlined_block = block->GetContainingInlinedBlock(); 547 if (inlined_block) 548 return inlined_block; 549 550 // The block in this symbol context is not inside an inlined block, so 551 // the block that defines the function is the function's top level block, 552 // which is returned below. 553 } 554 555 // There is no block information in this symbol context, so we must assume 556 // that the block that is desired is the top level block of the function 557 // itself. 558 return &function->GetBlock(true); 559 } 560 return nullptr; 561 } 562 563 bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language, 564 bool &is_instance_method, 565 ConstString &language_object_name) 566 567 { 568 Block *function_block = GetFunctionBlock(); 569 if (function_block) { 570 CompilerDeclContext decl_ctx = function_block->GetDeclContext(); 571 if (decl_ctx) 572 return decl_ctx.IsClassMethod(&language, &is_instance_method, 573 &language_object_name); 574 } 575 return false; 576 } 577 578 void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const { 579 Block *curr_block = block; 580 bool isInlinedblock = false; 581 if (curr_block != nullptr && 582 curr_block->GetContainingInlinedBlock() != nullptr) 583 isInlinedblock = true; 584 585 // Find all types that match the current block if we have one and put them 586 // first in the list. Keep iterating up through all blocks. 587 while (curr_block != nullptr && !isInlinedblock) { 588 type_map.ForEach( 589 [curr_block, &type_list](const lldb::TypeSP &type_sp) -> bool { 590 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 591 if (scs && curr_block == scs->CalculateSymbolContextBlock()) 592 type_list.Insert(type_sp); 593 return true; // Keep iterating 594 }); 595 596 // Remove any entries that are now in "type_list" from "type_map" since we 597 // can't remove from type_map while iterating 598 type_list.ForEach([&type_map](const lldb::TypeSP &type_sp) -> bool { 599 type_map.Remove(type_sp); 600 return true; // Keep iterating 601 }); 602 curr_block = curr_block->GetParent(); 603 } 604 // Find all types that match the current function, if we have onem, and put 605 // them next in the list. 606 if (function != nullptr && !type_map.Empty()) { 607 const size_t old_type_list_size = type_list.GetSize(); 608 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 609 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 610 if (scs && function == scs->CalculateSymbolContextFunction()) 611 type_list.Insert(type_sp); 612 return true; // Keep iterating 613 }); 614 615 // Remove any entries that are now in "type_list" from "type_map" since we 616 // can't remove from type_map while iterating 617 const size_t new_type_list_size = type_list.GetSize(); 618 if (new_type_list_size > old_type_list_size) { 619 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 620 type_map.Remove(type_list.GetTypeAtIndex(i)); 621 } 622 } 623 // Find all types that match the current compile unit, if we have one, and 624 // put them next in the list. 625 if (comp_unit != nullptr && !type_map.Empty()) { 626 const size_t old_type_list_size = type_list.GetSize(); 627 628 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 629 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 630 if (scs && comp_unit == scs->CalculateSymbolContextCompileUnit()) 631 type_list.Insert(type_sp); 632 return true; // Keep iterating 633 }); 634 635 // Remove any entries that are now in "type_list" from "type_map" since we 636 // can't remove from type_map while iterating 637 const size_t new_type_list_size = type_list.GetSize(); 638 if (new_type_list_size > old_type_list_size) { 639 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 640 type_map.Remove(type_list.GetTypeAtIndex(i)); 641 } 642 } 643 // Find all types that match the current module, if we have one, and put them 644 // next in the list. 645 if (module_sp && !type_map.Empty()) { 646 const size_t old_type_list_size = type_list.GetSize(); 647 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool { 648 SymbolContextScope *scs = type_sp->GetSymbolContextScope(); 649 if (scs && module_sp == scs->CalculateSymbolContextModule()) 650 type_list.Insert(type_sp); 651 return true; // Keep iterating 652 }); 653 // Remove any entries that are now in "type_list" from "type_map" since we 654 // can't remove from type_map while iterating 655 const size_t new_type_list_size = type_list.GetSize(); 656 if (new_type_list_size > old_type_list_size) { 657 for (size_t i = old_type_list_size; i < new_type_list_size; ++i) 658 type_map.Remove(type_list.GetTypeAtIndex(i)); 659 } 660 } 661 // Any types that are left get copied into the list an any order. 662 if (!type_map.Empty()) { 663 type_map.ForEach([&type_list](const lldb::TypeSP &type_sp) -> bool { 664 type_list.Insert(type_sp); 665 return true; // Keep iterating 666 }); 667 } 668 } 669 670 ConstString 671 SymbolContext::GetFunctionName(Mangled::NamePreference preference) const { 672 if (function) { 673 if (block) { 674 Block *inlined_block = block->GetContainingInlinedBlock(); 675 676 if (inlined_block) { 677 const InlineFunctionInfo *inline_info = 678 inlined_block->GetInlinedFunctionInfo(); 679 if (inline_info) 680 return inline_info->GetName(function->GetLanguage()); 681 } 682 } 683 return function->GetMangled().GetName(function->GetLanguage(), preference); 684 } else if (symbol && symbol->ValueIsAddress()) { 685 return symbol->GetMangled().GetName(symbol->GetLanguage(), preference); 686 } else { 687 // No function, return an empty string. 688 return ConstString(); 689 } 690 } 691 692 LineEntry SymbolContext::GetFunctionStartLineEntry() const { 693 LineEntry line_entry; 694 Address start_addr; 695 if (block) { 696 Block *inlined_block = block->GetContainingInlinedBlock(); 697 if (inlined_block) { 698 if (inlined_block->GetStartAddress(start_addr)) { 699 if (start_addr.CalculateSymbolContextLineEntry(line_entry)) 700 return line_entry; 701 } 702 return LineEntry(); 703 } 704 } 705 706 if (function) { 707 if (function->GetAddressRange() 708 .GetBaseAddress() 709 .CalculateSymbolContextLineEntry(line_entry)) 710 return line_entry; 711 } 712 return LineEntry(); 713 } 714 715 bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, 716 AddressRange &range, 717 Status &error) { 718 if (!line_entry.IsValid()) { 719 error.SetErrorString("Symbol context has no line table."); 720 return false; 721 } 722 723 range = line_entry.range; 724 if (line_entry.line > end_line) { 725 error.SetErrorStringWithFormat( 726 "end line option %d must be after the current line: %d", end_line, 727 line_entry.line); 728 return false; 729 } 730 731 uint32_t line_index = 0; 732 bool found = false; 733 while (true) { 734 LineEntry this_line; 735 line_index = comp_unit->FindLineEntry(line_index, line_entry.line, nullptr, 736 false, &this_line); 737 if (line_index == UINT32_MAX) 738 break; 739 if (LineEntry::Compare(this_line, line_entry) == 0) { 740 found = true; 741 break; 742 } 743 } 744 745 LineEntry end_entry; 746 if (!found) { 747 // Can't find the index of the SymbolContext's line entry in the 748 // SymbolContext's CompUnit. 749 error.SetErrorString( 750 "Can't find the current line entry in the CompUnit - can't process " 751 "the end-line option"); 752 return false; 753 } 754 755 line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false, 756 &end_entry); 757 if (line_index == UINT32_MAX) { 758 error.SetErrorStringWithFormat( 759 "could not find a line table entry corresponding " 760 "to end line number %d", 761 end_line); 762 return false; 763 } 764 765 Block *func_block = GetFunctionBlock(); 766 if (func_block && 767 func_block->GetRangeIndexContainingAddress( 768 end_entry.range.GetBaseAddress()) == UINT32_MAX) { 769 error.SetErrorStringWithFormat( 770 "end line number %d is not contained within the current function.", 771 end_line); 772 return false; 773 } 774 775 lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() - 776 range.GetBaseAddress().GetFileAddress(); 777 range.SetByteSize(range_size); 778 return true; 779 } 780 781 const Symbol * 782 SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) { 783 error.Clear(); 784 785 if (!target_sp) { 786 return nullptr; 787 } 788 789 Target &target = *target_sp; 790 Module *module = module_sp.get(); 791 792 auto ProcessMatches = [this, &name, &target, module] 793 (SymbolContextList &sc_list, Status &error) -> const Symbol* { 794 llvm::SmallVector<const Symbol *, 1> external_symbols; 795 llvm::SmallVector<const Symbol *, 1> internal_symbols; 796 const uint32_t matches = sc_list.GetSize(); 797 for (uint32_t i = 0; i < matches; ++i) { 798 SymbolContext sym_ctx; 799 sc_list.GetContextAtIndex(i, sym_ctx); 800 if (sym_ctx.symbol) { 801 const Symbol *symbol = sym_ctx.symbol; 802 const Address sym_address = symbol->GetAddress(); 803 804 if (sym_address.IsValid()) { 805 switch (symbol->GetType()) { 806 case eSymbolTypeData: 807 case eSymbolTypeRuntime: 808 case eSymbolTypeAbsolute: 809 case eSymbolTypeObjCClass: 810 case eSymbolTypeObjCMetaClass: 811 case eSymbolTypeObjCIVar: 812 if (symbol->GetDemangledNameIsSynthesized()) { 813 // If the demangled name was synthesized, then don't use it for 814 // expressions. Only let the symbol match if the mangled named 815 // matches for these symbols. 816 if (symbol->GetMangled().GetMangledName() != name) 817 break; 818 } 819 if (symbol->IsExternal()) { 820 external_symbols.push_back(symbol); 821 } else { 822 internal_symbols.push_back(symbol); 823 } 824 break; 825 case eSymbolTypeReExported: { 826 ConstString reexport_name = symbol->GetReExportedSymbolName(); 827 if (reexport_name) { 828 ModuleSP reexport_module_sp; 829 ModuleSpec reexport_module_spec; 830 reexport_module_spec.GetPlatformFileSpec() = 831 symbol->GetReExportedSymbolSharedLibrary(); 832 if (reexport_module_spec.GetPlatformFileSpec()) { 833 reexport_module_sp = 834 target.GetImages().FindFirstModule(reexport_module_spec); 835 if (!reexport_module_sp) { 836 reexport_module_spec.GetPlatformFileSpec() 837 .GetDirectory() 838 .Clear(); 839 reexport_module_sp = 840 target.GetImages().FindFirstModule(reexport_module_spec); 841 } 842 } 843 // Don't allow us to try and resolve a re-exported symbol if it 844 // is the same as the current symbol 845 if (name == symbol->GetReExportedSymbolName() && 846 module == reexport_module_sp.get()) 847 return nullptr; 848 849 return FindBestGlobalDataSymbol( 850 symbol->GetReExportedSymbolName(), error); 851 } 852 } break; 853 854 case eSymbolTypeCode: // We already lookup functions elsewhere 855 case eSymbolTypeVariable: 856 case eSymbolTypeLocal: 857 case eSymbolTypeParam: 858 case eSymbolTypeTrampoline: 859 case eSymbolTypeInvalid: 860 case eSymbolTypeException: 861 case eSymbolTypeSourceFile: 862 case eSymbolTypeHeaderFile: 863 case eSymbolTypeObjectFile: 864 case eSymbolTypeCommonBlock: 865 case eSymbolTypeBlock: 866 case eSymbolTypeVariableType: 867 case eSymbolTypeLineEntry: 868 case eSymbolTypeLineHeader: 869 case eSymbolTypeScopeBegin: 870 case eSymbolTypeScopeEnd: 871 case eSymbolTypeAdditional: 872 case eSymbolTypeCompiler: 873 case eSymbolTypeInstrumentation: 874 case eSymbolTypeUndefined: 875 case eSymbolTypeResolver: 876 break; 877 } 878 } 879 } 880 } 881 882 if (external_symbols.size() > 1) { 883 StreamString ss; 884 ss.Printf("Multiple external symbols found for '%s'\n", name.AsCString()); 885 for (const Symbol *symbol : external_symbols) { 886 symbol->GetDescription(&ss, eDescriptionLevelFull, &target); 887 } 888 ss.PutChar('\n'); 889 error.SetErrorString(ss.GetData()); 890 return nullptr; 891 } else if (external_symbols.size()) { 892 return external_symbols[0]; 893 } else if (internal_symbols.size() > 1) { 894 StreamString ss; 895 ss.Printf("Multiple internal symbols found for '%s'\n", name.AsCString()); 896 for (const Symbol *symbol : internal_symbols) { 897 symbol->GetDescription(&ss, eDescriptionLevelVerbose, &target); 898 ss.PutChar('\n'); 899 } 900 error.SetErrorString(ss.GetData()); 901 return nullptr; 902 } else if (internal_symbols.size()) { 903 return internal_symbols[0]; 904 } else { 905 return nullptr; 906 } 907 }; 908 909 if (module) { 910 SymbolContextList sc_list; 911 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list); 912 const Symbol *const module_symbol = ProcessMatches(sc_list, error); 913 914 if (!error.Success()) { 915 return nullptr; 916 } else if (module_symbol) { 917 return module_symbol; 918 } 919 } 920 921 { 922 SymbolContextList sc_list; 923 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, 924 sc_list); 925 const Symbol *const target_symbol = ProcessMatches(sc_list, error); 926 927 if (!error.Success()) { 928 return nullptr; 929 } else if (target_symbol) { 930 return target_symbol; 931 } 932 } 933 934 return nullptr; // no error; we just didn't find anything 935 } 936 937 938 // 939 // SymbolContextSpecifier 940 // 941 942 SymbolContextSpecifier::SymbolContextSpecifier(const TargetSP &target_sp) 943 : m_target_sp(target_sp), m_module_spec(), m_module_sp(), m_file_spec_up(), 944 m_start_line(0), m_end_line(0), m_function_spec(), m_class_name(), 945 m_address_range_up(), m_type(eNothingSpecified) {} 946 947 SymbolContextSpecifier::~SymbolContextSpecifier() {} 948 949 bool SymbolContextSpecifier::AddLineSpecification(uint32_t line_no, 950 SpecificationType type) { 951 bool return_value = true; 952 switch (type) { 953 case eNothingSpecified: 954 Clear(); 955 break; 956 case eLineStartSpecified: 957 m_start_line = line_no; 958 m_type |= eLineStartSpecified; 959 break; 960 case eLineEndSpecified: 961 m_end_line = line_no; 962 m_type |= eLineEndSpecified; 963 break; 964 default: 965 return_value = false; 966 break; 967 } 968 return return_value; 969 } 970 971 bool SymbolContextSpecifier::AddSpecification(const char *spec_string, 972 SpecificationType type) { 973 bool return_value = true; 974 switch (type) { 975 case eNothingSpecified: 976 Clear(); 977 break; 978 case eModuleSpecified: { 979 // See if we can find the Module, if so stick it in the SymbolContext. 980 FileSpec module_file_spec(spec_string); 981 ModuleSpec module_spec(module_file_spec); 982 lldb::ModuleSP module_sp( 983 m_target_sp->GetImages().FindFirstModule(module_spec)); 984 m_type |= eModuleSpecified; 985 if (module_sp) 986 m_module_sp = module_sp; 987 else 988 m_module_spec.assign(spec_string); 989 } break; 990 case eFileSpecified: 991 // CompUnits can't necessarily be resolved here, since an inlined function 992 // might show up in a number of CompUnits. Instead we just convert to a 993 // FileSpec and store it away. 994 m_file_spec_up.reset(new FileSpec(spec_string)); 995 m_type |= eFileSpecified; 996 break; 997 case eLineStartSpecified: 998 m_start_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value); 999 if (return_value) 1000 m_type |= eLineStartSpecified; 1001 break; 1002 case eLineEndSpecified: 1003 m_end_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value); 1004 if (return_value) 1005 m_type |= eLineEndSpecified; 1006 break; 1007 case eFunctionSpecified: 1008 m_function_spec.assign(spec_string); 1009 m_type |= eFunctionSpecified; 1010 break; 1011 case eClassOrNamespaceSpecified: 1012 Clear(); 1013 m_class_name.assign(spec_string); 1014 m_type = eClassOrNamespaceSpecified; 1015 break; 1016 case eAddressRangeSpecified: 1017 // Not specified yet... 1018 break; 1019 } 1020 1021 return return_value; 1022 } 1023 1024 void SymbolContextSpecifier::Clear() { 1025 m_module_spec.clear(); 1026 m_file_spec_up.reset(); 1027 m_function_spec.clear(); 1028 m_class_name.clear(); 1029 m_start_line = 0; 1030 m_end_line = 0; 1031 m_address_range_up.reset(); 1032 1033 m_type = eNothingSpecified; 1034 } 1035 1036 bool SymbolContextSpecifier::SymbolContextMatches(SymbolContext &sc) { 1037 if (m_type == eNothingSpecified) 1038 return true; 1039 1040 if (m_target_sp.get() != sc.target_sp.get()) 1041 return false; 1042 1043 if (m_type & eModuleSpecified) { 1044 if (sc.module_sp) { 1045 if (m_module_sp.get() != nullptr) { 1046 if (m_module_sp.get() != sc.module_sp.get()) 1047 return false; 1048 } else { 1049 FileSpec module_file_spec(m_module_spec); 1050 if (!FileSpec::Equal(module_file_spec, sc.module_sp->GetFileSpec(), 1051 false)) 1052 return false; 1053 } 1054 } 1055 } 1056 if (m_type & eFileSpecified) { 1057 if (m_file_spec_up) { 1058 // If we don't have a block or a comp_unit, then we aren't going to match 1059 // a source file. 1060 if (sc.block == nullptr && sc.comp_unit == nullptr) 1061 return false; 1062 1063 // Check if the block is present, and if so is it inlined: 1064 bool was_inlined = false; 1065 if (sc.block != nullptr) { 1066 const InlineFunctionInfo *inline_info = 1067 sc.block->GetInlinedFunctionInfo(); 1068 if (inline_info != nullptr) { 1069 was_inlined = true; 1070 if (!FileSpec::Equal(inline_info->GetDeclaration().GetFile(), 1071 *(m_file_spec_up.get()), false)) 1072 return false; 1073 } 1074 } 1075 1076 // Next check the comp unit, but only if the SymbolContext was not 1077 // inlined. 1078 if (!was_inlined && sc.comp_unit != nullptr) { 1079 if (!FileSpec::Equal(*(sc.comp_unit), *(m_file_spec_up.get()), false)) 1080 return false; 1081 } 1082 } 1083 } 1084 if (m_type & eLineStartSpecified || m_type & eLineEndSpecified) { 1085 if (sc.line_entry.line < m_start_line || sc.line_entry.line > m_end_line) 1086 return false; 1087 } 1088 1089 if (m_type & eFunctionSpecified) { 1090 // First check the current block, and if it is inlined, get the inlined 1091 // function name: 1092 bool was_inlined = false; 1093 ConstString func_name(m_function_spec.c_str()); 1094 1095 if (sc.block != nullptr) { 1096 const InlineFunctionInfo *inline_info = 1097 sc.block->GetInlinedFunctionInfo(); 1098 if (inline_info != nullptr) { 1099 was_inlined = true; 1100 const Mangled &name = inline_info->GetMangled(); 1101 if (!name.NameMatches(func_name, sc.function->GetLanguage())) 1102 return false; 1103 } 1104 } 1105 // If it wasn't inlined, check the name in the function or symbol: 1106 if (!was_inlined) { 1107 if (sc.function != nullptr) { 1108 if (!sc.function->GetMangled().NameMatches(func_name, 1109 sc.function->GetLanguage())) 1110 return false; 1111 } else if (sc.symbol != nullptr) { 1112 if (!sc.symbol->GetMangled().NameMatches(func_name, 1113 sc.symbol->GetLanguage())) 1114 return false; 1115 } 1116 } 1117 } 1118 1119 return true; 1120 } 1121 1122 bool SymbolContextSpecifier::AddressMatches(lldb::addr_t addr) { 1123 if (m_type & eAddressRangeSpecified) { 1124 1125 } else { 1126 Address match_address(addr, nullptr); 1127 SymbolContext sc; 1128 m_target_sp->GetImages().ResolveSymbolContextForAddress( 1129 match_address, eSymbolContextEverything, sc); 1130 return SymbolContextMatches(sc); 1131 } 1132 return true; 1133 } 1134 1135 void SymbolContextSpecifier::GetDescription( 1136 Stream *s, lldb::DescriptionLevel level) const { 1137 char path_str[PATH_MAX + 1]; 1138 1139 if (m_type == eNothingSpecified) { 1140 s->Printf("Nothing specified.\n"); 1141 } 1142 1143 if (m_type == eModuleSpecified) { 1144 s->Indent(); 1145 if (m_module_sp) { 1146 m_module_sp->GetFileSpec().GetPath(path_str, PATH_MAX); 1147 s->Printf("Module: %s\n", path_str); 1148 } else 1149 s->Printf("Module: %s\n", m_module_spec.c_str()); 1150 } 1151 1152 if (m_type == eFileSpecified && m_file_spec_up != nullptr) { 1153 m_file_spec_up->GetPath(path_str, PATH_MAX); 1154 s->Indent(); 1155 s->Printf("File: %s", path_str); 1156 if (m_type == eLineStartSpecified) { 1157 s->Printf(" from line %" PRIu64 "", (uint64_t)m_start_line); 1158 if (m_type == eLineEndSpecified) 1159 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line); 1160 else 1161 s->Printf("to end"); 1162 } else if (m_type == eLineEndSpecified) { 1163 s->Printf(" from start to line %" PRIu64 "", (uint64_t)m_end_line); 1164 } 1165 s->Printf(".\n"); 1166 } 1167 1168 if (m_type == eLineStartSpecified) { 1169 s->Indent(); 1170 s->Printf("From line %" PRIu64 "", (uint64_t)m_start_line); 1171 if (m_type == eLineEndSpecified) 1172 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line); 1173 else 1174 s->Printf("to end"); 1175 s->Printf(".\n"); 1176 } else if (m_type == eLineEndSpecified) { 1177 s->Printf("From start to line %" PRIu64 ".\n", (uint64_t)m_end_line); 1178 } 1179 1180 if (m_type == eFunctionSpecified) { 1181 s->Indent(); 1182 s->Printf("Function: %s.\n", m_function_spec.c_str()); 1183 } 1184 1185 if (m_type == eClassOrNamespaceSpecified) { 1186 s->Indent(); 1187 s->Printf("Class name: %s.\n", m_class_name.c_str()); 1188 } 1189 1190 if (m_type == eAddressRangeSpecified && m_address_range_up != nullptr) { 1191 s->Indent(); 1192 s->PutCString("Address range: "); 1193 m_address_range_up->Dump(s, m_target_sp.get(), 1194 Address::DumpStyleLoadAddress, 1195 Address::DumpStyleFileAddress); 1196 s->PutCString("\n"); 1197 } 1198 } 1199 1200 // 1201 // SymbolContextList 1202 // 1203 1204 SymbolContextList::SymbolContextList() : m_symbol_contexts() {} 1205 1206 SymbolContextList::~SymbolContextList() {} 1207 1208 void SymbolContextList::Append(const SymbolContext &sc) { 1209 m_symbol_contexts.push_back(sc); 1210 } 1211 1212 void SymbolContextList::Append(const SymbolContextList &sc_list) { 1213 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end(); 1214 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) 1215 m_symbol_contexts.push_back(*pos); 1216 } 1217 1218 uint32_t SymbolContextList::AppendIfUnique(const SymbolContextList &sc_list, 1219 bool merge_symbol_into_function) { 1220 uint32_t unique_sc_add_count = 0; 1221 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end(); 1222 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) { 1223 if (AppendIfUnique(*pos, merge_symbol_into_function)) 1224 ++unique_sc_add_count; 1225 } 1226 return unique_sc_add_count; 1227 } 1228 1229 bool SymbolContextList::AppendIfUnique(const SymbolContext &sc, 1230 bool merge_symbol_into_function) { 1231 collection::iterator pos, end = m_symbol_contexts.end(); 1232 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1233 if (*pos == sc) 1234 return false; 1235 } 1236 if (merge_symbol_into_function && sc.symbol != nullptr && 1237 sc.comp_unit == nullptr && sc.function == nullptr && 1238 sc.block == nullptr && !sc.line_entry.IsValid()) { 1239 if (sc.symbol->ValueIsAddress()) { 1240 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1241 // Don't merge symbols into inlined function symbol contexts 1242 if (pos->block && pos->block->GetContainingInlinedBlock()) 1243 continue; 1244 1245 if (pos->function) { 1246 if (pos->function->GetAddressRange().GetBaseAddress() == 1247 sc.symbol->GetAddressRef()) { 1248 // Do we already have a function with this symbol? 1249 if (pos->symbol == sc.symbol) 1250 return false; 1251 if (pos->symbol == nullptr) { 1252 pos->symbol = sc.symbol; 1253 return false; 1254 } 1255 } 1256 } 1257 } 1258 } 1259 } 1260 m_symbol_contexts.push_back(sc); 1261 return true; 1262 } 1263 1264 void SymbolContextList::Clear() { m_symbol_contexts.clear(); } 1265 1266 void SymbolContextList::Dump(Stream *s, Target *target) const { 1267 1268 *s << this << ": "; 1269 s->Indent(); 1270 s->PutCString("SymbolContextList"); 1271 s->EOL(); 1272 s->IndentMore(); 1273 1274 collection::const_iterator pos, end = m_symbol_contexts.end(); 1275 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) { 1276 // pos->Dump(s, target); 1277 pos->GetDescription(s, eDescriptionLevelVerbose, target); 1278 } 1279 s->IndentLess(); 1280 } 1281 1282 bool SymbolContextList::GetContextAtIndex(size_t idx, SymbolContext &sc) const { 1283 if (idx < m_symbol_contexts.size()) { 1284 sc = m_symbol_contexts[idx]; 1285 return true; 1286 } 1287 return false; 1288 } 1289 1290 bool SymbolContextList::RemoveContextAtIndex(size_t idx) { 1291 if (idx < m_symbol_contexts.size()) { 1292 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx); 1293 return true; 1294 } 1295 return false; 1296 } 1297 1298 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); } 1299 1300 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const { 1301 uint32_t match_count = 0; 1302 const size_t size = m_symbol_contexts.size(); 1303 for (size_t idx = 0; idx < size; ++idx) { 1304 if (m_symbol_contexts[idx].line_entry.line == line) 1305 ++match_count; 1306 } 1307 return match_count; 1308 } 1309 1310 void SymbolContextList::GetDescription(Stream *s, lldb::DescriptionLevel level, 1311 Target *target) const { 1312 const size_t size = m_symbol_contexts.size(); 1313 for (size_t idx = 0; idx < size; ++idx) 1314 m_symbol_contexts[idx].GetDescription(s, level, target); 1315 } 1316 1317 bool lldb_private::operator==(const SymbolContextList &lhs, 1318 const SymbolContextList &rhs) { 1319 const uint32_t size = lhs.GetSize(); 1320 if (size != rhs.GetSize()) 1321 return false; 1322 1323 SymbolContext lhs_sc; 1324 SymbolContext rhs_sc; 1325 for (uint32_t i = 0; i < size; ++i) { 1326 lhs.GetContextAtIndex(i, lhs_sc); 1327 rhs.GetContextAtIndex(i, rhs_sc); 1328 if (lhs_sc != rhs_sc) 1329 return false; 1330 } 1331 return true; 1332 } 1333 1334 bool lldb_private::operator!=(const SymbolContextList &lhs, 1335 const SymbolContextList &rhs) { 1336 return !(lhs == rhs); 1337 } 1338