1 //===-- SymbolContext.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/SymbolContext.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Host/StringConvert.h"
16 #include "lldb/Symbol/Block.h"
17 #include "lldb/Symbol/ClangASTContext.h"
18 #include "lldb/Symbol/CompileUnit.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Symbol/SymbolVendor.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/Log.h"
28 using namespace lldb_private;
30 SymbolContext::SymbolContext()
31 : target_sp(), module_sp(), comp_unit(nullptr), function(nullptr),
32 block(nullptr), line_entry(), symbol(nullptr), variable(nullptr) {}
34 SymbolContext::SymbolContext(const ModuleSP &m, CompileUnit *cu, Function *f,
35 Block *b, LineEntry *le, Symbol *s)
36 : target_sp(), module_sp(m), comp_unit(cu), function(f), block(b),
37 line_entry(), symbol(s), variable(nullptr) {
42 SymbolContext::SymbolContext(const TargetSP &t, const ModuleSP &m,
43 CompileUnit *cu, Function *f, Block *b,
44 LineEntry *le, Symbol *s)
45 : target_sp(t), module_sp(m), comp_unit(cu), function(f), block(b),
46 line_entry(), symbol(s), variable(nullptr) {
51 SymbolContext::SymbolContext(const SymbolContext &rhs)
52 : target_sp(rhs.target_sp), module_sp(rhs.module_sp),
53 comp_unit(rhs.comp_unit), function(rhs.function), block(rhs.block),
54 line_entry(rhs.line_entry), symbol(rhs.symbol), variable(rhs.variable) {}
56 SymbolContext::SymbolContext(SymbolContextScope *sc_scope)
57 : target_sp(), module_sp(), comp_unit(nullptr), function(nullptr),
58 block(nullptr), line_entry(), symbol(nullptr), variable(nullptr) {
59 sc_scope->CalculateSymbolContext(this);
62 SymbolContext::~SymbolContext() {}
64 const SymbolContext &SymbolContext::operator=(const SymbolContext &rhs) {
66 target_sp = rhs.target_sp;
67 module_sp = rhs.module_sp;
68 comp_unit = rhs.comp_unit;
69 function = rhs.function;
71 line_entry = rhs.line_entry;
73 variable = rhs.variable;
78 void SymbolContext::Clear(bool clear_target) {
90 bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
91 const Address &addr, bool show_fullpaths,
92 bool show_module, bool show_inlined_frames,
93 bool show_function_arguments,
94 bool show_function_name) const {
95 bool dumped_something = false;
96 if (show_module && module_sp) {
98 *s << module_sp->GetFileSpec();
100 *s << module_sp->GetFileSpec().GetFilename();
102 dumped_something = true;
105 if (function != nullptr) {
106 SymbolContext inline_parent_sc;
107 Address inline_parent_addr;
108 if (show_function_name == false) {
110 dumped_something = true;
113 if (show_function_arguments == false)
114 name = function->GetNameNoArguments();
116 name = function->GetName();
121 if (addr.IsValid()) {
122 const addr_t function_offset =
124 function->GetAddressRange().GetBaseAddress().GetOffset();
125 if (show_function_name == false) {
126 // Print +offset even if offset is 0
127 dumped_something = true;
128 s->Printf("+%" PRIu64 ">", function_offset);
129 } else if (function_offset) {
130 dumped_something = true;
131 s->Printf(" + %" PRIu64, function_offset);
135 if (GetParentOfInlinedScope(addr, inline_parent_sc, inline_parent_addr)) {
136 dumped_something = true;
137 Block *inlined_block = block->GetContainingInlinedBlock();
138 const InlineFunctionInfo *inlined_block_info =
139 inlined_block->GetInlinedFunctionInfo();
142 inlined_block_info->GetName(function->GetLanguage()).GetCString());
144 lldb_private::AddressRange block_range;
145 if (inlined_block->GetRangeContainingAddress(addr, block_range)) {
146 const addr_t inlined_function_offset =
147 addr.GetOffset() - block_range.GetBaseAddress().GetOffset();
148 if (inlined_function_offset) {
149 s->Printf(" + %" PRIu64, inlined_function_offset);
152 const Declaration &call_site = inlined_block_info->GetCallSite();
153 if (call_site.IsValid()) {
154 s->PutCString(" at ");
155 call_site.DumpStopContext(s, show_fullpaths);
157 if (show_inlined_frames) {
160 const bool show_function_name = true;
161 return inline_parent_sc.DumpStopContext(
162 s, exe_scope, inline_parent_addr, show_fullpaths, show_module,
163 show_inlined_frames, show_function_arguments, show_function_name);
166 if (line_entry.IsValid()) {
167 dumped_something = true;
168 s->PutCString(" at ");
169 if (line_entry.DumpStopContext(s, show_fullpaths))
170 dumped_something = true;
173 } else if (symbol != nullptr) {
174 if (show_function_name == false) {
176 dumped_something = true;
177 } else if (symbol->GetName()) {
178 dumped_something = true;
179 if (symbol->GetType() == eSymbolTypeTrampoline)
180 s->PutCString("symbol stub for: ");
181 symbol->GetName().Dump(s);
184 if (addr.IsValid() && symbol->ValueIsAddress()) {
185 const addr_t symbol_offset =
186 addr.GetOffset() - symbol->GetAddressRef().GetOffset();
187 if (show_function_name == false) {
188 // Print +offset even if offset is 0
189 dumped_something = true;
190 s->Printf("+%" PRIu64 ">", symbol_offset);
191 } else if (symbol_offset) {
192 dumped_something = true;
193 s->Printf(" + %" PRIu64, symbol_offset);
196 } else if (addr.IsValid()) {
197 addr.Dump(s, exe_scope, Address::DumpStyleModuleWithFileAddress);
198 dumped_something = true;
200 return dumped_something;
203 void SymbolContext::GetDescription(Stream *s, lldb::DescriptionLevel level,
204 Target *target) const {
206 s->Indent(" Module: file = \"");
207 module_sp->GetFileSpec().Dump(s);
209 if (module_sp->GetArchitecture().IsValid())
210 s->Printf(", arch = \"%s\"",
211 module_sp->GetArchitecture().GetArchitectureName());
215 if (comp_unit != nullptr) {
216 s->Indent("CompileUnit: ");
217 comp_unit->GetDescription(s, level);
221 if (function != nullptr) {
222 s->Indent(" Function: ");
223 function->GetDescription(s, level, target);
226 Type *func_type = function->GetType();
228 s->Indent(" FuncType: ");
229 func_type->GetDescription(s, level, false);
234 if (block != nullptr) {
235 std::vector<Block *> blocks;
236 blocks.push_back(block);
237 Block *parent_block = block->GetParent();
239 while (parent_block) {
240 blocks.push_back(parent_block);
241 parent_block = parent_block->GetParent();
243 std::vector<Block *>::reverse_iterator pos;
244 std::vector<Block *>::reverse_iterator begin = blocks.rbegin();
245 std::vector<Block *>::reverse_iterator end = blocks.rend();
246 for (pos = begin; pos != end; ++pos) {
248 s->Indent(" Blocks: ");
251 (*pos)->GetDescription(s, function, level, target);
256 if (line_entry.IsValid()) {
257 s->Indent(" LineEntry: ");
258 line_entry.GetDescription(s, level, comp_unit, target, false);
262 if (symbol != nullptr) {
263 s->Indent(" Symbol: ");
264 symbol->GetDescription(s, level, target);
268 if (variable != nullptr) {
269 s->Indent(" Variable: ");
271 s->Printf("id = {0x%8.8" PRIx64 "}, ", variable->GetID());
273 switch (variable->GetScope()) {
274 case eValueTypeVariableGlobal:
275 s->PutCString("kind = global, ");
278 case eValueTypeVariableStatic:
279 s->PutCString("kind = static, ");
282 case eValueTypeVariableArgument:
283 s->PutCString("kind = argument, ");
286 case eValueTypeVariableLocal:
287 s->PutCString("kind = local, ");
290 case eValueTypeVariableThreadLocal:
291 s->PutCString("kind = thread local, ");
298 s->Printf("name = \"%s\"\n", variable->GetName().GetCString());
302 uint32_t SymbolContext::GetResolvedMask() const {
303 uint32_t resolved_mask = 0;
305 resolved_mask |= eSymbolContextTarget;
307 resolved_mask |= eSymbolContextModule;
309 resolved_mask |= eSymbolContextCompUnit;
311 resolved_mask |= eSymbolContextFunction;
313 resolved_mask |= eSymbolContextBlock;
314 if (line_entry.IsValid())
315 resolved_mask |= eSymbolContextLineEntry;
317 resolved_mask |= eSymbolContextSymbol;
319 resolved_mask |= eSymbolContextVariable;
320 return resolved_mask;
323 void SymbolContext::Dump(Stream *s, Target *target) const {
326 s->PutCString("SymbolContext");
331 *s << "Module = " << module_sp.get() << ' ';
333 module_sp->GetFileSpec().Dump(s);
336 *s << "CompileUnit = " << comp_unit;
337 if (comp_unit != nullptr)
338 *s << " {0x" << comp_unit->GetID() << "} "
339 << *(static_cast<FileSpec *>(comp_unit));
342 *s << "Function = " << function;
343 if (function != nullptr) {
344 *s << " {0x" << function->GetID() << "} " << function->GetType()->GetName()
345 << ", address-range = ";
346 function->GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress,
347 Address::DumpStyleModuleWithFileAddress);
350 Type *func_type = function->GetType();
353 func_type->Dump(s, false);
358 *s << "Block = " << block;
359 if (block != nullptr)
360 *s << " {0x" << block->GetID() << '}';
361 // Dump the block and pass it a negative depth to we print all the parent
363 // if (block != NULL)
364 // block->Dump(s, function->GetFileAddress(), INT_MIN);
367 *s << "LineEntry = ";
368 line_entry.Dump(s, target, true, Address::DumpStyleLoadAddress,
369 Address::DumpStyleModuleWithFileAddress, true);
372 *s << "Symbol = " << symbol;
373 if (symbol != nullptr && symbol->GetMangled())
374 *s << ' ' << symbol->GetName().AsCString();
376 *s << "Variable = " << variable;
377 if (variable != nullptr) {
378 *s << " {0x" << variable->GetID() << "} " << variable->GetType()->GetName();
385 bool lldb_private::operator==(const SymbolContext &lhs,
386 const SymbolContext &rhs) {
387 return lhs.function == rhs.function && lhs.symbol == rhs.symbol &&
388 lhs.module_sp.get() == rhs.module_sp.get() &&
389 lhs.comp_unit == rhs.comp_unit &&
390 lhs.target_sp.get() == rhs.target_sp.get() &&
391 LineEntry::Compare(lhs.line_entry, rhs.line_entry) == 0 &&
392 lhs.variable == rhs.variable;
395 bool lldb_private::operator!=(const SymbolContext &lhs,
396 const SymbolContext &rhs) {
397 return lhs.function != rhs.function || lhs.symbol != rhs.symbol ||
398 lhs.module_sp.get() != rhs.module_sp.get() ||
399 lhs.comp_unit != rhs.comp_unit ||
400 lhs.target_sp.get() != rhs.target_sp.get() ||
401 LineEntry::Compare(lhs.line_entry, rhs.line_entry) != 0 ||
402 lhs.variable != rhs.variable;
405 bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
406 bool use_inline_block_range,
407 AddressRange &range) const {
408 if ((scope & eSymbolContextLineEntry) && line_entry.IsValid()) {
409 range = line_entry.range;
413 if ((scope & eSymbolContextBlock) && (block != nullptr)) {
414 if (use_inline_block_range) {
415 Block *inline_block = block->GetContainingInlinedBlock();
417 return inline_block->GetRangeAtIndex(range_idx, range);
419 return block->GetRangeAtIndex(range_idx, range);
423 if ((scope & eSymbolContextFunction) && (function != nullptr)) {
424 if (range_idx == 0) {
425 range = function->GetAddressRange();
430 if ((scope & eSymbolContextSymbol) && (symbol != nullptr)) {
431 if (range_idx == 0) {
432 if (symbol->ValueIsAddress()) {
433 range.GetBaseAddress() = symbol->GetAddressRef();
434 range.SetByteSize(symbol->GetByteSize());
443 LanguageType SymbolContext::GetLanguage() const {
445 if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {
447 } else if (variable &&
448 (lang = variable->GetLanguage()) != eLanguageTypeUnknown) {
450 } else if (symbol && (lang = symbol->GetLanguage()) != eLanguageTypeUnknown) {
452 } else if (comp_unit &&
453 (lang = comp_unit->GetLanguage()) != eLanguageTypeUnknown) {
456 // If all else fails, try to guess the language from the name.
457 return symbol->GetMangled().GuessLanguage();
459 return eLanguageTypeUnknown;
462 bool SymbolContext::GetParentOfInlinedScope(const Address &curr_frame_pc,
463 SymbolContext &next_frame_sc,
464 Address &next_frame_pc) const {
465 next_frame_sc.Clear(false);
466 next_frame_pc.Clear();
469 // const addr_t curr_frame_file_addr = curr_frame_pc.GetFileAddress();
471 // In order to get the parent of an inlined function we first need to
472 // see if we are in an inlined block as "this->block" could be an
473 // inlined block, or a parent of "block" could be. So lets check if
474 // this block or one of this blocks parents is an inlined function.
475 Block *curr_inlined_block = block->GetContainingInlinedBlock();
476 if (curr_inlined_block) {
477 // "this->block" is contained in an inline function block, so to
478 // get the scope above the inlined block, we get the parent of the
479 // inlined block itself
480 Block *next_frame_block = curr_inlined_block->GetParent();
481 // Now calculate the symbol context of the containing block
482 next_frame_block->CalculateSymbolContext(&next_frame_sc);
484 // If we get here we weren't able to find the return line entry using the
485 // nesting of the blocks and
486 // the line table. So just use the call site info from our inlined block.
489 if (curr_inlined_block->GetRangeContainingAddress(curr_frame_pc, range)) {
490 // To see there this new frame block it, we need to look at the
491 // call site information from
492 const InlineFunctionInfo *curr_inlined_block_inlined_info =
493 curr_inlined_block->GetInlinedFunctionInfo();
494 next_frame_pc = range.GetBaseAddress();
495 next_frame_sc.line_entry.range.GetBaseAddress() = next_frame_pc;
496 next_frame_sc.line_entry.file =
497 curr_inlined_block_inlined_info->GetCallSite().GetFile();
498 next_frame_sc.line_entry.original_file =
499 curr_inlined_block_inlined_info->GetCallSite().GetFile();
500 next_frame_sc.line_entry.line =
501 curr_inlined_block_inlined_info->GetCallSite().GetLine();
502 next_frame_sc.line_entry.column =
503 curr_inlined_block_inlined_info->GetCallSite().GetColumn();
506 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
510 "warning: inlined block 0x%8.8" PRIx64
511 " doesn't have a range that contains file address 0x%" PRIx64,
512 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
514 #ifdef LLDB_CONFIGURATION_DEBUG
516 ObjectFile *objfile = NULL;
518 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
520 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
522 objfile = symbol_file->GetObjectFile();
527 Host::eSystemLogWarning,
528 "warning: inlined block 0x%8.8" PRIx64
529 " doesn't have a range that contains file address 0x%" PRIx64
531 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress(),
532 objfile->GetFileSpec().GetPath().c_str());
535 Host::eSystemLogWarning,
536 "warning: inlined block 0x%8.8" PRIx64
537 " doesn't have a range that contains file address 0x%" PRIx64
539 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
550 Block *SymbolContext::GetFunctionBlock() {
553 // If this symbol context has a block, check to see if this block
554 // is itself, or is contained within a block with inlined function
555 // information. If so, then the inlined block is the block that
556 // defines the function.
557 Block *inlined_block = block->GetContainingInlinedBlock();
559 return inlined_block;
561 // The block in this symbol context is not inside an inlined
562 // block, so the block that defines the function is the function's
563 // top level block, which is returned below.
566 // There is no block information in this symbol context, so we must
567 // assume that the block that is desired is the top level block of
568 // the function itself.
569 return &function->GetBlock(true);
574 bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language,
575 bool &is_instance_method,
576 ConstString &language_object_name)
579 Block *function_block = GetFunctionBlock();
580 if (function_block) {
581 CompilerDeclContext decl_ctx = function_block->GetDeclContext();
583 return decl_ctx.IsClassMethod(&language, &is_instance_method,
584 &language_object_name);
589 void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const {
590 Block *curr_block = block;
591 bool isInlinedblock = false;
592 if (curr_block != nullptr &&
593 curr_block->GetContainingInlinedBlock() != nullptr)
594 isInlinedblock = true;
596 //----------------------------------------------------------------------
597 // Find all types that match the current block if we have one and put
598 // them first in the list. Keep iterating up through all blocks.
599 //----------------------------------------------------------------------
600 while (curr_block != nullptr && !isInlinedblock) {
602 [curr_block, &type_list](const lldb::TypeSP &type_sp) -> bool {
603 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
604 if (scs && curr_block == scs->CalculateSymbolContextBlock())
605 type_list.Insert(type_sp);
606 return true; // Keep iterating
609 // Remove any entries that are now in "type_list" from "type_map"
610 // since we can't remove from type_map while iterating
611 type_list.ForEach([&type_map](const lldb::TypeSP &type_sp) -> bool {
612 type_map.Remove(type_sp);
613 return true; // Keep iterating
615 curr_block = curr_block->GetParent();
617 //----------------------------------------------------------------------
618 // Find all types that match the current function, if we have onem, and
619 // put them next in the list.
620 //----------------------------------------------------------------------
621 if (function != nullptr && !type_map.Empty()) {
622 const size_t old_type_list_size = type_list.GetSize();
623 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
624 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
625 if (scs && function == scs->CalculateSymbolContextFunction())
626 type_list.Insert(type_sp);
627 return true; // Keep iterating
630 // Remove any entries that are now in "type_list" from "type_map"
631 // since we can't remove from type_map while iterating
632 const size_t new_type_list_size = type_list.GetSize();
633 if (new_type_list_size > old_type_list_size) {
634 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
635 type_map.Remove(type_list.GetTypeAtIndex(i));
638 //----------------------------------------------------------------------
639 // Find all types that match the current compile unit, if we have one,
640 // and put them next in the list.
641 //----------------------------------------------------------------------
642 if (comp_unit != nullptr && !type_map.Empty()) {
643 const size_t old_type_list_size = type_list.GetSize();
645 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
646 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
647 if (scs && comp_unit == scs->CalculateSymbolContextCompileUnit())
648 type_list.Insert(type_sp);
649 return true; // Keep iterating
652 // Remove any entries that are now in "type_list" from "type_map"
653 // since we 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));
660 //----------------------------------------------------------------------
661 // Find all types that match the current module, if we have one, and put
662 // them next in the list.
663 //----------------------------------------------------------------------
664 if (module_sp && !type_map.Empty()) {
665 const size_t old_type_list_size = type_list.GetSize();
666 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
667 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
668 if (scs && module_sp == scs->CalculateSymbolContextModule())
669 type_list.Insert(type_sp);
670 return true; // Keep iterating
672 // Remove any entries that are now in "type_list" from "type_map"
673 // since we can't remove from type_map while iterating
674 const size_t new_type_list_size = type_list.GetSize();
675 if (new_type_list_size > old_type_list_size) {
676 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
677 type_map.Remove(type_list.GetTypeAtIndex(i));
680 //----------------------------------------------------------------------
681 // Any types that are left get copied into the list an any order.
682 //----------------------------------------------------------------------
683 if (!type_map.Empty()) {
684 type_map.ForEach([&type_list](const lldb::TypeSP &type_sp) -> bool {
685 type_list.Insert(type_sp);
686 return true; // Keep iterating
692 SymbolContext::GetFunctionName(Mangled::NamePreference preference) const {
695 Block *inlined_block = block->GetContainingInlinedBlock();
698 const InlineFunctionInfo *inline_info =
699 inlined_block->GetInlinedFunctionInfo();
701 return inline_info->GetName(function->GetLanguage());
704 return function->GetMangled().GetName(function->GetLanguage(), preference);
705 } else if (symbol && symbol->ValueIsAddress()) {
706 return symbol->GetMangled().GetName(symbol->GetLanguage(), preference);
708 // No function, return an empty string.
709 return ConstString();
713 LineEntry SymbolContext::GetFunctionStartLineEntry() const {
714 LineEntry line_entry;
717 Block *inlined_block = block->GetContainingInlinedBlock();
719 if (inlined_block->GetStartAddress(start_addr)) {
720 if (start_addr.CalculateSymbolContextLineEntry(line_entry))
728 if (function->GetAddressRange()
730 .CalculateSymbolContextLineEntry(line_entry))
736 bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line,
739 if (!line_entry.IsValid()) {
740 error.SetErrorString("Symbol context has no line table.");
744 range = line_entry.range;
745 if (line_entry.line > end_line) {
746 error.SetErrorStringWithFormat(
747 "end line option %d must be after the current line: %d", end_line,
752 uint32_t line_index = 0;
756 line_index = comp_unit->FindLineEntry(line_index, line_entry.line, nullptr,
758 if (line_index == UINT32_MAX)
760 if (LineEntry::Compare(this_line, line_entry) == 0) {
768 // Can't find the index of the SymbolContext's line entry in the
769 // SymbolContext's CompUnit.
770 error.SetErrorString(
771 "Can't find the current line entry in the CompUnit - can't process "
772 "the end-line option");
776 line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false,
778 if (line_index == UINT32_MAX) {
779 error.SetErrorStringWithFormat(
780 "could not find a line table entry corresponding "
781 "to end line number %d",
786 Block *func_block = GetFunctionBlock();
788 func_block->GetRangeIndexContainingAddress(
789 end_entry.range.GetBaseAddress()) == UINT32_MAX) {
790 error.SetErrorStringWithFormat(
791 "end line number %d is not contained within the current function.",
796 lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() -
797 range.GetBaseAddress().GetFileAddress();
798 range.SetByteSize(range_size);
803 SymbolContext::FindBestGlobalDataSymbol(const ConstString &name, Status &error) {
810 Target &target = *target_sp;
811 Module *module = module_sp.get();
813 auto ProcessMatches = [this, &name, &target, module]
814 (SymbolContextList &sc_list, Status &error) -> const Symbol* {
815 llvm::SmallVector<const Symbol *, 1> external_symbols;
816 llvm::SmallVector<const Symbol *, 1> internal_symbols;
817 const uint32_t matches = sc_list.GetSize();
818 for (uint32_t i = 0; i < matches; ++i) {
819 SymbolContext sym_ctx;
820 sc_list.GetContextAtIndex(i, sym_ctx);
821 if (sym_ctx.symbol) {
822 const Symbol *symbol = sym_ctx.symbol;
823 const Address sym_address = symbol->GetAddress();
825 if (sym_address.IsValid()) {
826 switch (symbol->GetType()) {
827 case eSymbolTypeData:
828 case eSymbolTypeRuntime:
829 case eSymbolTypeAbsolute:
830 case eSymbolTypeObjCClass:
831 case eSymbolTypeObjCMetaClass:
832 case eSymbolTypeObjCIVar:
833 if (symbol->GetDemangledNameIsSynthesized()) {
834 // If the demangled name was synthesized, then don't use it
835 // for expressions. Only let the symbol match if the mangled
836 // named matches for these symbols.
837 if (symbol->GetMangled().GetMangledName() != name)
840 if (symbol->IsExternal()) {
841 external_symbols.push_back(symbol);
843 internal_symbols.push_back(symbol);
846 case eSymbolTypeReExported: {
847 ConstString reexport_name = symbol->GetReExportedSymbolName();
849 ModuleSP reexport_module_sp;
850 ModuleSpec reexport_module_spec;
851 reexport_module_spec.GetPlatformFileSpec() =
852 symbol->GetReExportedSymbolSharedLibrary();
853 if (reexport_module_spec.GetPlatformFileSpec()) {
855 target.GetImages().FindFirstModule(reexport_module_spec);
856 if (!reexport_module_sp) {
857 reexport_module_spec.GetPlatformFileSpec()
861 target.GetImages().FindFirstModule(reexport_module_spec);
864 // Don't allow us to try and resolve a re-exported symbol if it is
865 // the same as the current symbol
866 if (name == symbol->GetReExportedSymbolName() &&
867 module == reexport_module_sp.get())
870 return FindBestGlobalDataSymbol(
871 symbol->GetReExportedSymbolName(), error);
875 case eSymbolTypeCode: // We already lookup functions elsewhere
876 case eSymbolTypeVariable:
877 case eSymbolTypeLocal:
878 case eSymbolTypeParam:
879 case eSymbolTypeTrampoline:
880 case eSymbolTypeInvalid:
881 case eSymbolTypeException:
882 case eSymbolTypeSourceFile:
883 case eSymbolTypeHeaderFile:
884 case eSymbolTypeObjectFile:
885 case eSymbolTypeCommonBlock:
886 case eSymbolTypeBlock:
887 case eSymbolTypeVariableType:
888 case eSymbolTypeLineEntry:
889 case eSymbolTypeLineHeader:
890 case eSymbolTypeScopeBegin:
891 case eSymbolTypeScopeEnd:
892 case eSymbolTypeAdditional:
893 case eSymbolTypeCompiler:
894 case eSymbolTypeInstrumentation:
895 case eSymbolTypeUndefined:
896 case eSymbolTypeResolver:
903 if (external_symbols.size() > 1) {
905 ss.Printf("Multiple external symbols found for '%s'\n", name.AsCString());
906 for (const Symbol *symbol : external_symbols) {
907 symbol->GetDescription(&ss, eDescriptionLevelFull, &target);
910 error.SetErrorString(ss.GetData());
912 } else if (external_symbols.size()) {
913 return external_symbols[0];
914 } else if (internal_symbols.size() > 1) {
916 ss.Printf("Multiple internal symbols found for '%s'\n", name.AsCString());
917 for (const Symbol *symbol : internal_symbols) {
918 symbol->GetDescription(&ss, eDescriptionLevelVerbose, &target);
921 error.SetErrorString(ss.GetData());
923 } else if (internal_symbols.size()) {
924 return internal_symbols[0];
931 SymbolContextList sc_list;
932 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
933 const Symbol *const module_symbol = ProcessMatches(sc_list, error);
935 if (!error.Success()) {
937 } else if (module_symbol) {
938 return module_symbol;
943 SymbolContextList sc_list;
944 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny,
946 const Symbol *const target_symbol = ProcessMatches(sc_list, error);
948 if (!error.Success()) {
950 } else if (target_symbol) {
951 return target_symbol;
955 return nullptr; // no error; we just didn't find anything
959 //----------------------------------------------------------------------
961 // SymbolContextSpecifier
963 //----------------------------------------------------------------------
965 SymbolContextSpecifier::SymbolContextSpecifier(const TargetSP &target_sp)
966 : m_target_sp(target_sp), m_module_spec(), m_module_sp(), m_file_spec_ap(),
967 m_start_line(0), m_end_line(0), m_function_spec(), m_class_name(),
968 m_address_range_ap(), m_type(eNothingSpecified) {}
970 SymbolContextSpecifier::~SymbolContextSpecifier() {}
972 bool SymbolContextSpecifier::AddLineSpecification(uint32_t line_no,
973 SpecificationType type) {
974 bool return_value = true;
976 case eNothingSpecified:
979 case eLineStartSpecified:
980 m_start_line = line_no;
981 m_type |= eLineStartSpecified;
983 case eLineEndSpecified:
984 m_end_line = line_no;
985 m_type |= eLineEndSpecified;
988 return_value = false;
994 bool SymbolContextSpecifier::AddSpecification(const char *spec_string,
995 SpecificationType type) {
996 bool return_value = true;
998 case eNothingSpecified:
1001 case eModuleSpecified: {
1002 // See if we can find the Module, if so stick it in the SymbolContext.
1003 FileSpec module_file_spec(spec_string, false);
1004 ModuleSpec module_spec(module_file_spec);
1005 lldb::ModuleSP module_sp(
1006 m_target_sp->GetImages().FindFirstModule(module_spec));
1007 m_type |= eModuleSpecified;
1009 m_module_sp = module_sp;
1011 m_module_spec.assign(spec_string);
1013 case eFileSpecified:
1014 // CompUnits can't necessarily be resolved here, since an inlined function
1016 // a number of CompUnits. Instead we just convert to a FileSpec and store
1018 m_file_spec_ap.reset(new FileSpec(spec_string, false));
1019 m_type |= eFileSpecified;
1021 case eLineStartSpecified:
1022 m_start_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1024 m_type |= eLineStartSpecified;
1026 case eLineEndSpecified:
1027 m_end_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1029 m_type |= eLineEndSpecified;
1031 case eFunctionSpecified:
1032 m_function_spec.assign(spec_string);
1033 m_type |= eFunctionSpecified;
1035 case eClassOrNamespaceSpecified:
1037 m_class_name.assign(spec_string);
1038 m_type = eClassOrNamespaceSpecified;
1040 case eAddressRangeSpecified:
1041 // Not specified yet...
1045 return return_value;
1048 void SymbolContextSpecifier::Clear() {
1049 m_module_spec.clear();
1050 m_file_spec_ap.reset();
1051 m_function_spec.clear();
1052 m_class_name.clear();
1055 m_address_range_ap.reset();
1057 m_type = eNothingSpecified;
1060 bool SymbolContextSpecifier::SymbolContextMatches(SymbolContext &sc) {
1061 if (m_type == eNothingSpecified)
1064 if (m_target_sp.get() != sc.target_sp.get())
1067 if (m_type & eModuleSpecified) {
1069 if (m_module_sp.get() != nullptr) {
1070 if (m_module_sp.get() != sc.module_sp.get())
1073 FileSpec module_file_spec(m_module_spec, false);
1074 if (!FileSpec::Equal(module_file_spec, sc.module_sp->GetFileSpec(),
1080 if (m_type & eFileSpecified) {
1081 if (m_file_spec_ap.get()) {
1082 // If we don't have a block or a comp_unit, then we aren't going to match
1084 if (sc.block == nullptr && sc.comp_unit == nullptr)
1087 // Check if the block is present, and if so is it inlined:
1088 bool was_inlined = false;
1089 if (sc.block != nullptr) {
1090 const InlineFunctionInfo *inline_info =
1091 sc.block->GetInlinedFunctionInfo();
1092 if (inline_info != nullptr) {
1094 if (!FileSpec::Equal(inline_info->GetDeclaration().GetFile(),
1095 *(m_file_spec_ap.get()), false))
1100 // Next check the comp unit, but only if the SymbolContext was not
1102 if (!was_inlined && sc.comp_unit != nullptr) {
1103 if (!FileSpec::Equal(*(sc.comp_unit), *(m_file_spec_ap.get()), false))
1108 if (m_type & eLineStartSpecified || m_type & eLineEndSpecified) {
1109 if (sc.line_entry.line < m_start_line || sc.line_entry.line > m_end_line)
1113 if (m_type & eFunctionSpecified) {
1114 // First check the current block, and if it is inlined, get the inlined
1116 bool was_inlined = false;
1117 ConstString func_name(m_function_spec.c_str());
1119 if (sc.block != nullptr) {
1120 const InlineFunctionInfo *inline_info =
1121 sc.block->GetInlinedFunctionInfo();
1122 if (inline_info != nullptr) {
1124 const Mangled &name = inline_info->GetMangled();
1125 if (!name.NameMatches(func_name, sc.function->GetLanguage()))
1129 // If it wasn't inlined, check the name in the function or symbol:
1131 if (sc.function != nullptr) {
1132 if (!sc.function->GetMangled().NameMatches(func_name,
1133 sc.function->GetLanguage()))
1135 } else if (sc.symbol != nullptr) {
1136 if (!sc.symbol->GetMangled().NameMatches(func_name,
1137 sc.symbol->GetLanguage()))
1146 bool SymbolContextSpecifier::AddressMatches(lldb::addr_t addr) {
1147 if (m_type & eAddressRangeSpecified) {
1150 Address match_address(addr, nullptr);
1152 m_target_sp->GetImages().ResolveSymbolContextForAddress(
1153 match_address, eSymbolContextEverything, sc);
1154 return SymbolContextMatches(sc);
1159 void SymbolContextSpecifier::GetDescription(
1160 Stream *s, lldb::DescriptionLevel level) const {
1161 char path_str[PATH_MAX + 1];
1163 if (m_type == eNothingSpecified) {
1164 s->Printf("Nothing specified.\n");
1167 if (m_type == eModuleSpecified) {
1170 m_module_sp->GetFileSpec().GetPath(path_str, PATH_MAX);
1171 s->Printf("Module: %s\n", path_str);
1173 s->Printf("Module: %s\n", m_module_spec.c_str());
1176 if (m_type == eFileSpecified && m_file_spec_ap.get() != nullptr) {
1177 m_file_spec_ap->GetPath(path_str, PATH_MAX);
1179 s->Printf("File: %s", path_str);
1180 if (m_type == eLineStartSpecified) {
1181 s->Printf(" from line %" PRIu64 "", (uint64_t)m_start_line);
1182 if (m_type == eLineEndSpecified)
1183 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1185 s->Printf("to end");
1186 } else if (m_type == eLineEndSpecified) {
1187 s->Printf(" from start to line %" PRIu64 "", (uint64_t)m_end_line);
1192 if (m_type == eLineStartSpecified) {
1194 s->Printf("From line %" PRIu64 "", (uint64_t)m_start_line);
1195 if (m_type == eLineEndSpecified)
1196 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1198 s->Printf("to end");
1200 } else if (m_type == eLineEndSpecified) {
1201 s->Printf("From start to line %" PRIu64 ".\n", (uint64_t)m_end_line);
1204 if (m_type == eFunctionSpecified) {
1206 s->Printf("Function: %s.\n", m_function_spec.c_str());
1209 if (m_type == eClassOrNamespaceSpecified) {
1211 s->Printf("Class name: %s.\n", m_class_name.c_str());
1214 if (m_type == eAddressRangeSpecified && m_address_range_ap.get() != nullptr) {
1216 s->PutCString("Address range: ");
1217 m_address_range_ap->Dump(s, m_target_sp.get(),
1218 Address::DumpStyleLoadAddress,
1219 Address::DumpStyleFileAddress);
1220 s->PutCString("\n");
1224 //----------------------------------------------------------------------
1226 // SymbolContextList
1228 //----------------------------------------------------------------------
1230 SymbolContextList::SymbolContextList() : m_symbol_contexts() {}
1232 SymbolContextList::~SymbolContextList() {}
1234 void SymbolContextList::Append(const SymbolContext &sc) {
1235 m_symbol_contexts.push_back(sc);
1238 void SymbolContextList::Append(const SymbolContextList &sc_list) {
1239 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1240 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos)
1241 m_symbol_contexts.push_back(*pos);
1244 uint32_t SymbolContextList::AppendIfUnique(const SymbolContextList &sc_list,
1245 bool merge_symbol_into_function) {
1246 uint32_t unique_sc_add_count = 0;
1247 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1248 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) {
1249 if (AppendIfUnique(*pos, merge_symbol_into_function))
1250 ++unique_sc_add_count;
1252 return unique_sc_add_count;
1255 bool SymbolContextList::AppendIfUnique(const SymbolContext &sc,
1256 bool merge_symbol_into_function) {
1257 collection::iterator pos, end = m_symbol_contexts.end();
1258 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1262 if (merge_symbol_into_function && sc.symbol != nullptr &&
1263 sc.comp_unit == nullptr && sc.function == nullptr &&
1264 sc.block == nullptr && sc.line_entry.IsValid() == false) {
1265 if (sc.symbol->ValueIsAddress()) {
1266 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1267 // Don't merge symbols into inlined function symbol contexts
1268 if (pos->block && pos->block->GetContainingInlinedBlock())
1271 if (pos->function) {
1272 if (pos->function->GetAddressRange().GetBaseAddress() ==
1273 sc.symbol->GetAddressRef()) {
1274 // Do we already have a function with this symbol?
1275 if (pos->symbol == sc.symbol)
1277 if (pos->symbol == nullptr) {
1278 pos->symbol = sc.symbol;
1286 m_symbol_contexts.push_back(sc);
1290 bool SymbolContextList::MergeSymbolContextIntoFunctionContext(
1291 const SymbolContext &symbol_sc, uint32_t start_idx, uint32_t stop_idx) {
1292 if (symbol_sc.symbol != nullptr && symbol_sc.comp_unit == nullptr &&
1293 symbol_sc.function == nullptr && symbol_sc.block == nullptr &&
1294 symbol_sc.line_entry.IsValid() == false) {
1295 if (symbol_sc.symbol->ValueIsAddress()) {
1296 const size_t end = std::min<size_t>(m_symbol_contexts.size(), stop_idx);
1297 for (size_t i = start_idx; i < end; ++i) {
1298 const SymbolContext &function_sc = m_symbol_contexts[i];
1299 // Don't merge symbols into inlined function symbol contexts
1300 if (function_sc.block && function_sc.block->GetContainingInlinedBlock())
1303 if (function_sc.function) {
1304 if (function_sc.function->GetAddressRange().GetBaseAddress() ==
1305 symbol_sc.symbol->GetAddressRef()) {
1306 // Do we already have a function with this symbol?
1307 if (function_sc.symbol == symbol_sc.symbol)
1308 return true; // Already have a symbol context with this symbol,
1311 if (function_sc.symbol == nullptr) {
1312 // We successfully merged this symbol into an existing symbol
1314 m_symbol_contexts[i].symbol = symbol_sc.symbol;
1325 void SymbolContextList::Clear() { m_symbol_contexts.clear(); }
1327 void SymbolContextList::Dump(Stream *s, Target *target) const {
1331 s->PutCString("SymbolContextList");
1335 collection::const_iterator pos, end = m_symbol_contexts.end();
1336 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1337 // pos->Dump(s, target);
1338 pos->GetDescription(s, eDescriptionLevelVerbose, target);
1343 bool SymbolContextList::GetContextAtIndex(size_t idx, SymbolContext &sc) const {
1344 if (idx < m_symbol_contexts.size()) {
1345 sc = m_symbol_contexts[idx];
1351 bool SymbolContextList::GetLastContext(SymbolContext &sc) const {
1352 if (!m_symbol_contexts.empty()) {
1353 sc = m_symbol_contexts.back();
1359 bool SymbolContextList::RemoveContextAtIndex(size_t idx) {
1360 if (idx < m_symbol_contexts.size()) {
1361 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx);
1367 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); }
1369 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const {
1370 uint32_t match_count = 0;
1371 const size_t size = m_symbol_contexts.size();
1372 for (size_t idx = 0; idx < size; ++idx) {
1373 if (m_symbol_contexts[idx].line_entry.line == line)
1379 void SymbolContextList::GetDescription(Stream *s, lldb::DescriptionLevel level,
1380 Target *target) const {
1381 const size_t size = m_symbol_contexts.size();
1382 for (size_t idx = 0; idx < size; ++idx)
1383 m_symbol_contexts[idx].GetDescription(s, level, target);
1386 bool lldb_private::operator==(const SymbolContextList &lhs,
1387 const SymbolContextList &rhs) {
1388 const uint32_t size = lhs.GetSize();
1389 if (size != rhs.GetSize())
1392 SymbolContext lhs_sc;
1393 SymbolContext rhs_sc;
1394 for (uint32_t i = 0; i < size; ++i) {
1395 lhs.GetContextAtIndex(i, lhs_sc);
1396 rhs.GetContextAtIndex(i, rhs_sc);
1397 if (lhs_sc != rhs_sc)
1403 bool lldb_private::operator!=(const SymbolContextList &lhs,
1404 const SymbolContextList &rhs) {
1405 return !(lhs == rhs);