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