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) {
110 dumped_something = true;
113 if (!show_function_arguments)
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) {
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) {
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) {
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
362 // blocks if (block != NULL)
363 // block->Dump(s, function->GetFileAddress(), INT_MIN);
366 *s << "LineEntry = ";
367 line_entry.Dump(s, target, true, Address::DumpStyleLoadAddress,
368 Address::DumpStyleModuleWithFileAddress, true);
371 *s << "Symbol = " << symbol;
372 if (symbol != nullptr && symbol->GetMangled())
373 *s << ' ' << symbol->GetName().AsCString();
375 *s << "Variable = " << variable;
376 if (variable != nullptr) {
377 *s << " {0x" << variable->GetID() << "} " << variable->GetType()->GetName();
384 bool lldb_private::operator==(const SymbolContext &lhs,
385 const SymbolContext &rhs) {
386 return lhs.function == rhs.function && lhs.symbol == rhs.symbol &&
387 lhs.module_sp.get() == rhs.module_sp.get() &&
388 lhs.comp_unit == rhs.comp_unit &&
389 lhs.target_sp.get() == rhs.target_sp.get() &&
390 LineEntry::Compare(lhs.line_entry, rhs.line_entry) == 0 &&
391 lhs.variable == rhs.variable;
394 bool lldb_private::operator!=(const SymbolContext &lhs,
395 const SymbolContext &rhs) {
396 return !(lhs == rhs);
399 bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
400 bool use_inline_block_range,
401 AddressRange &range) const {
402 if ((scope & eSymbolContextLineEntry) && line_entry.IsValid()) {
403 range = line_entry.range;
407 if ((scope & eSymbolContextBlock) && (block != nullptr)) {
408 if (use_inline_block_range) {
409 Block *inline_block = block->GetContainingInlinedBlock();
411 return inline_block->GetRangeAtIndex(range_idx, range);
413 return block->GetRangeAtIndex(range_idx, range);
417 if ((scope & eSymbolContextFunction) && (function != nullptr)) {
418 if (range_idx == 0) {
419 range = function->GetAddressRange();
424 if ((scope & eSymbolContextSymbol) && (symbol != nullptr)) {
425 if (range_idx == 0) {
426 if (symbol->ValueIsAddress()) {
427 range.GetBaseAddress() = symbol->GetAddressRef();
428 range.SetByteSize(symbol->GetByteSize());
437 LanguageType SymbolContext::GetLanguage() const {
439 if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {
441 } else if (variable &&
442 (lang = variable->GetLanguage()) != eLanguageTypeUnknown) {
444 } else if (symbol && (lang = symbol->GetLanguage()) != eLanguageTypeUnknown) {
446 } else if (comp_unit &&
447 (lang = comp_unit->GetLanguage()) != eLanguageTypeUnknown) {
450 // If all else fails, try to guess the language from the name.
451 return symbol->GetMangled().GuessLanguage();
453 return eLanguageTypeUnknown;
456 bool SymbolContext::GetParentOfInlinedScope(const Address &curr_frame_pc,
457 SymbolContext &next_frame_sc,
458 Address &next_frame_pc) const {
459 next_frame_sc.Clear(false);
460 next_frame_pc.Clear();
463 // const addr_t curr_frame_file_addr = curr_frame_pc.GetFileAddress();
465 // In order to get the parent of an inlined function we first need to see
466 // if we are in an inlined block as "this->block" could be an inlined
467 // block, or a parent of "block" could be. So lets check if this block or
468 // one of this blocks parents is an inlined function.
469 Block *curr_inlined_block = block->GetContainingInlinedBlock();
470 if (curr_inlined_block) {
471 // "this->block" is contained in an inline function block, so to get the
472 // scope above the inlined block, we get the parent of the inlined block
474 Block *next_frame_block = curr_inlined_block->GetParent();
475 // Now calculate the symbol context of the containing block
476 next_frame_block->CalculateSymbolContext(&next_frame_sc);
478 // If we get here we weren't able to find the return line entry using the
479 // nesting of the blocks and the line table. So just use the call site
480 // info from our inlined block.
483 if (curr_inlined_block->GetRangeContainingAddress(curr_frame_pc, range)) {
484 // To see there this new frame block it, we need to look at the call
485 // site information from
486 const InlineFunctionInfo *curr_inlined_block_inlined_info =
487 curr_inlined_block->GetInlinedFunctionInfo();
488 next_frame_pc = range.GetBaseAddress();
489 next_frame_sc.line_entry.range.GetBaseAddress() = next_frame_pc;
490 next_frame_sc.line_entry.file =
491 curr_inlined_block_inlined_info->GetCallSite().GetFile();
492 next_frame_sc.line_entry.original_file =
493 curr_inlined_block_inlined_info->GetCallSite().GetFile();
494 next_frame_sc.line_entry.line =
495 curr_inlined_block_inlined_info->GetCallSite().GetLine();
496 next_frame_sc.line_entry.column =
497 curr_inlined_block_inlined_info->GetCallSite().GetColumn();
500 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
504 "warning: inlined block 0x%8.8" PRIx64
505 " doesn't have a range that contains file address 0x%" PRIx64,
506 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
508 #ifdef LLDB_CONFIGURATION_DEBUG
510 ObjectFile *objfile = NULL;
512 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
514 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
516 objfile = symbol_file->GetObjectFile();
521 Host::eSystemLogWarning,
522 "warning: inlined block 0x%8.8" PRIx64
523 " doesn't have a range that contains file address 0x%" PRIx64
525 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress(),
526 objfile->GetFileSpec().GetPath().c_str());
529 Host::eSystemLogWarning,
530 "warning: inlined block 0x%8.8" PRIx64
531 " doesn't have a range that contains file address 0x%" PRIx64
533 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
544 Block *SymbolContext::GetFunctionBlock() {
547 // If this symbol context has a block, check to see if this block is
548 // itself, or is contained within a block with inlined function
549 // information. If so, then the inlined block is the block that defines
551 Block *inlined_block = block->GetContainingInlinedBlock();
553 return inlined_block;
555 // The block in this symbol context is not inside an inlined block, so
556 // the block that defines the function is the function's top level block,
557 // which is returned below.
560 // There is no block information in this symbol context, so we must assume
561 // that the block that is desired is the top level block of the function
563 return &function->GetBlock(true);
568 bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language,
569 bool &is_instance_method,
570 ConstString &language_object_name)
573 Block *function_block = GetFunctionBlock();
574 if (function_block) {
575 CompilerDeclContext decl_ctx = function_block->GetDeclContext();
577 return decl_ctx.IsClassMethod(&language, &is_instance_method,
578 &language_object_name);
583 void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const {
584 Block *curr_block = block;
585 bool isInlinedblock = false;
586 if (curr_block != nullptr &&
587 curr_block->GetContainingInlinedBlock() != nullptr)
588 isInlinedblock = true;
590 //----------------------------------------------------------------------
591 // Find all types that match the current block if we have one and put them
592 // first in the list. Keep iterating up through all blocks.
593 //----------------------------------------------------------------------
594 while (curr_block != nullptr && !isInlinedblock) {
596 [curr_block, &type_list](const lldb::TypeSP &type_sp) -> bool {
597 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
598 if (scs && curr_block == scs->CalculateSymbolContextBlock())
599 type_list.Insert(type_sp);
600 return true; // Keep iterating
603 // Remove any entries that are now in "type_list" from "type_map" since we
604 // can't remove from type_map while iterating
605 type_list.ForEach([&type_map](const lldb::TypeSP &type_sp) -> bool {
606 type_map.Remove(type_sp);
607 return true; // Keep iterating
609 curr_block = curr_block->GetParent();
611 //----------------------------------------------------------------------
612 // Find all types that match the current function, if we have onem, and put
613 // them next in the list.
614 //----------------------------------------------------------------------
615 if (function != nullptr && !type_map.Empty()) {
616 const size_t old_type_list_size = type_list.GetSize();
617 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
618 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
619 if (scs && function == scs->CalculateSymbolContextFunction())
620 type_list.Insert(type_sp);
621 return true; // Keep iterating
624 // Remove any entries that are now in "type_list" from "type_map" since we
625 // can't remove from type_map while iterating
626 const size_t new_type_list_size = type_list.GetSize();
627 if (new_type_list_size > old_type_list_size) {
628 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
629 type_map.Remove(type_list.GetTypeAtIndex(i));
632 //----------------------------------------------------------------------
633 // Find all types that match the current compile unit, if we have one, and
634 // put them next in the list.
635 //----------------------------------------------------------------------
636 if (comp_unit != nullptr && !type_map.Empty()) {
637 const size_t old_type_list_size = type_list.GetSize();
639 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
640 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
641 if (scs && comp_unit == scs->CalculateSymbolContextCompileUnit())
642 type_list.Insert(type_sp);
643 return true; // Keep iterating
646 // Remove any entries that are now in "type_list" from "type_map" since we
647 // can't remove from type_map while iterating
648 const size_t new_type_list_size = type_list.GetSize();
649 if (new_type_list_size > old_type_list_size) {
650 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
651 type_map.Remove(type_list.GetTypeAtIndex(i));
654 //----------------------------------------------------------------------
655 // Find all types that match the current module, if we have one, and put them
657 //----------------------------------------------------------------------
658 if (module_sp && !type_map.Empty()) {
659 const size_t old_type_list_size = type_list.GetSize();
660 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
661 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
662 if (scs && module_sp == scs->CalculateSymbolContextModule())
663 type_list.Insert(type_sp);
664 return true; // Keep iterating
666 // Remove any entries that are now in "type_list" from "type_map" since we
667 // can't remove from type_map while iterating
668 const size_t new_type_list_size = type_list.GetSize();
669 if (new_type_list_size > old_type_list_size) {
670 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
671 type_map.Remove(type_list.GetTypeAtIndex(i));
674 //----------------------------------------------------------------------
675 // Any types that are left get copied into the list an any order.
676 //----------------------------------------------------------------------
677 if (!type_map.Empty()) {
678 type_map.ForEach([&type_list](const lldb::TypeSP &type_sp) -> bool {
679 type_list.Insert(type_sp);
680 return true; // Keep iterating
686 SymbolContext::GetFunctionName(Mangled::NamePreference preference) const {
689 Block *inlined_block = block->GetContainingInlinedBlock();
692 const InlineFunctionInfo *inline_info =
693 inlined_block->GetInlinedFunctionInfo();
695 return inline_info->GetName(function->GetLanguage());
698 return function->GetMangled().GetName(function->GetLanguage(), preference);
699 } else if (symbol && symbol->ValueIsAddress()) {
700 return symbol->GetMangled().GetName(symbol->GetLanguage(), preference);
702 // No function, return an empty string.
703 return ConstString();
707 LineEntry SymbolContext::GetFunctionStartLineEntry() const {
708 LineEntry line_entry;
711 Block *inlined_block = block->GetContainingInlinedBlock();
713 if (inlined_block->GetStartAddress(start_addr)) {
714 if (start_addr.CalculateSymbolContextLineEntry(line_entry))
722 if (function->GetAddressRange()
724 .CalculateSymbolContextLineEntry(line_entry))
730 bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line,
733 if (!line_entry.IsValid()) {
734 error.SetErrorString("Symbol context has no line table.");
738 range = line_entry.range;
739 if (line_entry.line > end_line) {
740 error.SetErrorStringWithFormat(
741 "end line option %d must be after the current line: %d", end_line,
746 uint32_t line_index = 0;
750 line_index = comp_unit->FindLineEntry(line_index, line_entry.line, nullptr,
752 if (line_index == UINT32_MAX)
754 if (LineEntry::Compare(this_line, line_entry) == 0) {
762 // Can't find the index of the SymbolContext's line entry in the
763 // SymbolContext's CompUnit.
764 error.SetErrorString(
765 "Can't find the current line entry in the CompUnit - can't process "
766 "the end-line option");
770 line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false,
772 if (line_index == UINT32_MAX) {
773 error.SetErrorStringWithFormat(
774 "could not find a line table entry corresponding "
775 "to end line number %d",
780 Block *func_block = GetFunctionBlock();
782 func_block->GetRangeIndexContainingAddress(
783 end_entry.range.GetBaseAddress()) == UINT32_MAX) {
784 error.SetErrorStringWithFormat(
785 "end line number %d is not contained within the current function.",
790 lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() -
791 range.GetBaseAddress().GetFileAddress();
792 range.SetByteSize(range_size);
797 SymbolContext::FindBestGlobalDataSymbol(const ConstString &name, Status &error) {
804 Target &target = *target_sp;
805 Module *module = module_sp.get();
807 auto ProcessMatches = [this, &name, &target, module]
808 (SymbolContextList &sc_list, Status &error) -> const Symbol* {
809 llvm::SmallVector<const Symbol *, 1> external_symbols;
810 llvm::SmallVector<const Symbol *, 1> internal_symbols;
811 const uint32_t matches = sc_list.GetSize();
812 for (uint32_t i = 0; i < matches; ++i) {
813 SymbolContext sym_ctx;
814 sc_list.GetContextAtIndex(i, sym_ctx);
815 if (sym_ctx.symbol) {
816 const Symbol *symbol = sym_ctx.symbol;
817 const Address sym_address = symbol->GetAddress();
819 if (sym_address.IsValid()) {
820 switch (symbol->GetType()) {
821 case eSymbolTypeData:
822 case eSymbolTypeRuntime:
823 case eSymbolTypeAbsolute:
824 case eSymbolTypeObjCClass:
825 case eSymbolTypeObjCMetaClass:
826 case eSymbolTypeObjCIVar:
827 if (symbol->GetDemangledNameIsSynthesized()) {
828 // If the demangled name was synthesized, then don't use it for
829 // expressions. Only let the symbol match if the mangled named
830 // matches for these symbols.
831 if (symbol->GetMangled().GetMangledName() != name)
834 if (symbol->IsExternal()) {
835 external_symbols.push_back(symbol);
837 internal_symbols.push_back(symbol);
840 case eSymbolTypeReExported: {
841 ConstString reexport_name = symbol->GetReExportedSymbolName();
843 ModuleSP reexport_module_sp;
844 ModuleSpec reexport_module_spec;
845 reexport_module_spec.GetPlatformFileSpec() =
846 symbol->GetReExportedSymbolSharedLibrary();
847 if (reexport_module_spec.GetPlatformFileSpec()) {
849 target.GetImages().FindFirstModule(reexport_module_spec);
850 if (!reexport_module_sp) {
851 reexport_module_spec.GetPlatformFileSpec()
855 target.GetImages().FindFirstModule(reexport_module_spec);
858 // Don't allow us to try and resolve a re-exported symbol if it
859 // is the same as the current symbol
860 if (name == symbol->GetReExportedSymbolName() &&
861 module == reexport_module_sp.get())
864 return FindBestGlobalDataSymbol(
865 symbol->GetReExportedSymbolName(), error);
869 case eSymbolTypeCode: // We already lookup functions elsewhere
870 case eSymbolTypeVariable:
871 case eSymbolTypeLocal:
872 case eSymbolTypeParam:
873 case eSymbolTypeTrampoline:
874 case eSymbolTypeInvalid:
875 case eSymbolTypeException:
876 case eSymbolTypeSourceFile:
877 case eSymbolTypeHeaderFile:
878 case eSymbolTypeObjectFile:
879 case eSymbolTypeCommonBlock:
880 case eSymbolTypeBlock:
881 case eSymbolTypeVariableType:
882 case eSymbolTypeLineEntry:
883 case eSymbolTypeLineHeader:
884 case eSymbolTypeScopeBegin:
885 case eSymbolTypeScopeEnd:
886 case eSymbolTypeAdditional:
887 case eSymbolTypeCompiler:
888 case eSymbolTypeInstrumentation:
889 case eSymbolTypeUndefined:
890 case eSymbolTypeResolver:
897 if (external_symbols.size() > 1) {
899 ss.Printf("Multiple external symbols found for '%s'\n", name.AsCString());
900 for (const Symbol *symbol : external_symbols) {
901 symbol->GetDescription(&ss, eDescriptionLevelFull, &target);
904 error.SetErrorString(ss.GetData());
906 } else if (external_symbols.size()) {
907 return external_symbols[0];
908 } else if (internal_symbols.size() > 1) {
910 ss.Printf("Multiple internal symbols found for '%s'\n", name.AsCString());
911 for (const Symbol *symbol : internal_symbols) {
912 symbol->GetDescription(&ss, eDescriptionLevelVerbose, &target);
915 error.SetErrorString(ss.GetData());
917 } else if (internal_symbols.size()) {
918 return internal_symbols[0];
925 SymbolContextList sc_list;
926 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
927 const Symbol *const module_symbol = ProcessMatches(sc_list, error);
929 if (!error.Success()) {
931 } else if (module_symbol) {
932 return module_symbol;
937 SymbolContextList sc_list;
938 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny,
940 const Symbol *const target_symbol = ProcessMatches(sc_list, error);
942 if (!error.Success()) {
944 } else if (target_symbol) {
945 return target_symbol;
949 return nullptr; // no error; we just didn't find anything
953 //----------------------------------------------------------------------
955 // SymbolContextSpecifier
957 //----------------------------------------------------------------------
959 SymbolContextSpecifier::SymbolContextSpecifier(const TargetSP &target_sp)
960 : m_target_sp(target_sp), m_module_spec(), m_module_sp(), m_file_spec_ap(),
961 m_start_line(0), m_end_line(0), m_function_spec(), m_class_name(),
962 m_address_range_ap(), m_type(eNothingSpecified) {}
964 SymbolContextSpecifier::~SymbolContextSpecifier() {}
966 bool SymbolContextSpecifier::AddLineSpecification(uint32_t line_no,
967 SpecificationType type) {
968 bool return_value = true;
970 case eNothingSpecified:
973 case eLineStartSpecified:
974 m_start_line = line_no;
975 m_type |= eLineStartSpecified;
977 case eLineEndSpecified:
978 m_end_line = line_no;
979 m_type |= eLineEndSpecified;
982 return_value = false;
988 bool SymbolContextSpecifier::AddSpecification(const char *spec_string,
989 SpecificationType type) {
990 bool return_value = true;
992 case eNothingSpecified:
995 case eModuleSpecified: {
996 // See if we can find the Module, if so stick it in the SymbolContext.
997 FileSpec module_file_spec(spec_string);
998 ModuleSpec module_spec(module_file_spec);
999 lldb::ModuleSP module_sp(
1000 m_target_sp->GetImages().FindFirstModule(module_spec));
1001 m_type |= eModuleSpecified;
1003 m_module_sp = module_sp;
1005 m_module_spec.assign(spec_string);
1007 case eFileSpecified:
1008 // CompUnits can't necessarily be resolved here, since an inlined function
1009 // might show up in a number of CompUnits. Instead we just convert to a
1010 // FileSpec and store it away.
1011 m_file_spec_ap.reset(new FileSpec(spec_string));
1012 m_type |= eFileSpecified;
1014 case eLineStartSpecified:
1015 m_start_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1017 m_type |= eLineStartSpecified;
1019 case eLineEndSpecified:
1020 m_end_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1022 m_type |= eLineEndSpecified;
1024 case eFunctionSpecified:
1025 m_function_spec.assign(spec_string);
1026 m_type |= eFunctionSpecified;
1028 case eClassOrNamespaceSpecified:
1030 m_class_name.assign(spec_string);
1031 m_type = eClassOrNamespaceSpecified;
1033 case eAddressRangeSpecified:
1034 // Not specified yet...
1038 return return_value;
1041 void SymbolContextSpecifier::Clear() {
1042 m_module_spec.clear();
1043 m_file_spec_ap.reset();
1044 m_function_spec.clear();
1045 m_class_name.clear();
1048 m_address_range_ap.reset();
1050 m_type = eNothingSpecified;
1053 bool SymbolContextSpecifier::SymbolContextMatches(SymbolContext &sc) {
1054 if (m_type == eNothingSpecified)
1057 if (m_target_sp.get() != sc.target_sp.get())
1060 if (m_type & eModuleSpecified) {
1062 if (m_module_sp.get() != nullptr) {
1063 if (m_module_sp.get() != sc.module_sp.get())
1066 FileSpec module_file_spec(m_module_spec);
1067 if (!FileSpec::Equal(module_file_spec, sc.module_sp->GetFileSpec(),
1073 if (m_type & eFileSpecified) {
1074 if (m_file_spec_ap.get()) {
1075 // If we don't have a block or a comp_unit, then we aren't going to match
1077 if (sc.block == nullptr && sc.comp_unit == nullptr)
1080 // Check if the block is present, and if so is it inlined:
1081 bool was_inlined = false;
1082 if (sc.block != nullptr) {
1083 const InlineFunctionInfo *inline_info =
1084 sc.block->GetInlinedFunctionInfo();
1085 if (inline_info != nullptr) {
1087 if (!FileSpec::Equal(inline_info->GetDeclaration().GetFile(),
1088 *(m_file_spec_ap.get()), false))
1093 // Next check the comp unit, but only if the SymbolContext was not
1095 if (!was_inlined && sc.comp_unit != nullptr) {
1096 if (!FileSpec::Equal(*(sc.comp_unit), *(m_file_spec_ap.get()), false))
1101 if (m_type & eLineStartSpecified || m_type & eLineEndSpecified) {
1102 if (sc.line_entry.line < m_start_line || sc.line_entry.line > m_end_line)
1106 if (m_type & eFunctionSpecified) {
1107 // First check the current block, and if it is inlined, get the inlined
1109 bool was_inlined = false;
1110 ConstString func_name(m_function_spec.c_str());
1112 if (sc.block != nullptr) {
1113 const InlineFunctionInfo *inline_info =
1114 sc.block->GetInlinedFunctionInfo();
1115 if (inline_info != nullptr) {
1117 const Mangled &name = inline_info->GetMangled();
1118 if (!name.NameMatches(func_name, sc.function->GetLanguage()))
1122 // If it wasn't inlined, check the name in the function or symbol:
1124 if (sc.function != nullptr) {
1125 if (!sc.function->GetMangled().NameMatches(func_name,
1126 sc.function->GetLanguage()))
1128 } else if (sc.symbol != nullptr) {
1129 if (!sc.symbol->GetMangled().NameMatches(func_name,
1130 sc.symbol->GetLanguage()))
1139 bool SymbolContextSpecifier::AddressMatches(lldb::addr_t addr) {
1140 if (m_type & eAddressRangeSpecified) {
1143 Address match_address(addr, nullptr);
1145 m_target_sp->GetImages().ResolveSymbolContextForAddress(
1146 match_address, eSymbolContextEverything, sc);
1147 return SymbolContextMatches(sc);
1152 void SymbolContextSpecifier::GetDescription(
1153 Stream *s, lldb::DescriptionLevel level) const {
1154 char path_str[PATH_MAX + 1];
1156 if (m_type == eNothingSpecified) {
1157 s->Printf("Nothing specified.\n");
1160 if (m_type == eModuleSpecified) {
1163 m_module_sp->GetFileSpec().GetPath(path_str, PATH_MAX);
1164 s->Printf("Module: %s\n", path_str);
1166 s->Printf("Module: %s\n", m_module_spec.c_str());
1169 if (m_type == eFileSpecified && m_file_spec_ap.get() != nullptr) {
1170 m_file_spec_ap->GetPath(path_str, PATH_MAX);
1172 s->Printf("File: %s", path_str);
1173 if (m_type == eLineStartSpecified) {
1174 s->Printf(" from line %" PRIu64 "", (uint64_t)m_start_line);
1175 if (m_type == eLineEndSpecified)
1176 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1178 s->Printf("to end");
1179 } else if (m_type == eLineEndSpecified) {
1180 s->Printf(" from start to line %" PRIu64 "", (uint64_t)m_end_line);
1185 if (m_type == eLineStartSpecified) {
1187 s->Printf("From line %" PRIu64 "", (uint64_t)m_start_line);
1188 if (m_type == eLineEndSpecified)
1189 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1191 s->Printf("to end");
1193 } else if (m_type == eLineEndSpecified) {
1194 s->Printf("From start to line %" PRIu64 ".\n", (uint64_t)m_end_line);
1197 if (m_type == eFunctionSpecified) {
1199 s->Printf("Function: %s.\n", m_function_spec.c_str());
1202 if (m_type == eClassOrNamespaceSpecified) {
1204 s->Printf("Class name: %s.\n", m_class_name.c_str());
1207 if (m_type == eAddressRangeSpecified && m_address_range_ap.get() != nullptr) {
1209 s->PutCString("Address range: ");
1210 m_address_range_ap->Dump(s, m_target_sp.get(),
1211 Address::DumpStyleLoadAddress,
1212 Address::DumpStyleFileAddress);
1213 s->PutCString("\n");
1217 //----------------------------------------------------------------------
1219 // SymbolContextList
1221 //----------------------------------------------------------------------
1223 SymbolContextList::SymbolContextList() : m_symbol_contexts() {}
1225 SymbolContextList::~SymbolContextList() {}
1227 void SymbolContextList::Append(const SymbolContext &sc) {
1228 m_symbol_contexts.push_back(sc);
1231 void SymbolContextList::Append(const SymbolContextList &sc_list) {
1232 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1233 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos)
1234 m_symbol_contexts.push_back(*pos);
1237 uint32_t SymbolContextList::AppendIfUnique(const SymbolContextList &sc_list,
1238 bool merge_symbol_into_function) {
1239 uint32_t unique_sc_add_count = 0;
1240 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1241 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) {
1242 if (AppendIfUnique(*pos, merge_symbol_into_function))
1243 ++unique_sc_add_count;
1245 return unique_sc_add_count;
1248 bool SymbolContextList::AppendIfUnique(const SymbolContext &sc,
1249 bool merge_symbol_into_function) {
1250 collection::iterator pos, end = m_symbol_contexts.end();
1251 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1255 if (merge_symbol_into_function && sc.symbol != nullptr &&
1256 sc.comp_unit == nullptr && sc.function == nullptr &&
1257 sc.block == nullptr && !sc.line_entry.IsValid()) {
1258 if (sc.symbol->ValueIsAddress()) {
1259 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1260 // Don't merge symbols into inlined function symbol contexts
1261 if (pos->block && pos->block->GetContainingInlinedBlock())
1264 if (pos->function) {
1265 if (pos->function->GetAddressRange().GetBaseAddress() ==
1266 sc.symbol->GetAddressRef()) {
1267 // Do we already have a function with this symbol?
1268 if (pos->symbol == sc.symbol)
1270 if (pos->symbol == nullptr) {
1271 pos->symbol = sc.symbol;
1279 m_symbol_contexts.push_back(sc);
1283 void SymbolContextList::Clear() { m_symbol_contexts.clear(); }
1285 void SymbolContextList::Dump(Stream *s, Target *target) const {
1289 s->PutCString("SymbolContextList");
1293 collection::const_iterator pos, end = m_symbol_contexts.end();
1294 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1295 // pos->Dump(s, target);
1296 pos->GetDescription(s, eDescriptionLevelVerbose, target);
1301 bool SymbolContextList::GetContextAtIndex(size_t idx, SymbolContext &sc) const {
1302 if (idx < m_symbol_contexts.size()) {
1303 sc = m_symbol_contexts[idx];
1309 bool SymbolContextList::RemoveContextAtIndex(size_t idx) {
1310 if (idx < m_symbol_contexts.size()) {
1311 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx);
1317 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); }
1319 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const {
1320 uint32_t match_count = 0;
1321 const size_t size = m_symbol_contexts.size();
1322 for (size_t idx = 0; idx < size; ++idx) {
1323 if (m_symbol_contexts[idx].line_entry.line == line)
1329 void SymbolContextList::GetDescription(Stream *s, lldb::DescriptionLevel level,
1330 Target *target) const {
1331 const size_t size = m_symbol_contexts.size();
1332 for (size_t idx = 0; idx < size; ++idx)
1333 m_symbol_contexts[idx].GetDescription(s, level, target);
1336 bool lldb_private::operator==(const SymbolContextList &lhs,
1337 const SymbolContextList &rhs) {
1338 const uint32_t size = lhs.GetSize();
1339 if (size != rhs.GetSize())
1342 SymbolContext lhs_sc;
1343 SymbolContext rhs_sc;
1344 for (uint32_t i = 0; i < size; ++i) {
1345 lhs.GetContextAtIndex(i, lhs_sc);
1346 rhs.GetContextAtIndex(i, rhs_sc);
1347 if (lhs_sc != rhs_sc)
1353 bool lldb_private::operator!=(const SymbolContextList &lhs,
1354 const SymbolContextList &rhs) {
1355 return !(lhs == rhs);