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
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.function != rhs.function || lhs.symbol != rhs.symbol ||
397 lhs.module_sp.get() != rhs.module_sp.get() ||
398 lhs.comp_unit != rhs.comp_unit ||
399 lhs.target_sp.get() != rhs.target_sp.get() ||
400 LineEntry::Compare(lhs.line_entry, rhs.line_entry) != 0 ||
401 lhs.variable != rhs.variable;
404 bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
405 bool use_inline_block_range,
406 AddressRange &range) const {
407 if ((scope & eSymbolContextLineEntry) && line_entry.IsValid()) {
408 range = line_entry.range;
412 if ((scope & eSymbolContextBlock) && (block != nullptr)) {
413 if (use_inline_block_range) {
414 Block *inline_block = block->GetContainingInlinedBlock();
416 return inline_block->GetRangeAtIndex(range_idx, range);
418 return block->GetRangeAtIndex(range_idx, range);
422 if ((scope & eSymbolContextFunction) && (function != nullptr)) {
423 if (range_idx == 0) {
424 range = function->GetAddressRange();
429 if ((scope & eSymbolContextSymbol) && (symbol != nullptr)) {
430 if (range_idx == 0) {
431 if (symbol->ValueIsAddress()) {
432 range.GetBaseAddress() = symbol->GetAddressRef();
433 range.SetByteSize(symbol->GetByteSize());
442 LanguageType SymbolContext::GetLanguage() const {
444 if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {
446 } else if (variable &&
447 (lang = variable->GetLanguage()) != eLanguageTypeUnknown) {
449 } else if (symbol && (lang = symbol->GetLanguage()) != eLanguageTypeUnknown) {
451 } else if (comp_unit &&
452 (lang = comp_unit->GetLanguage()) != eLanguageTypeUnknown) {
455 // If all else fails, try to guess the language from the name.
456 return symbol->GetMangled().GuessLanguage();
458 return eLanguageTypeUnknown;
461 bool SymbolContext::GetParentOfInlinedScope(const Address &curr_frame_pc,
462 SymbolContext &next_frame_sc,
463 Address &next_frame_pc) const {
464 next_frame_sc.Clear(false);
465 next_frame_pc.Clear();
468 // const addr_t curr_frame_file_addr = curr_frame_pc.GetFileAddress();
470 // In order to get the parent of an inlined function we first need to see
471 // if we are in an inlined block as "this->block" could be an inlined
472 // block, or a parent of "block" could be. So lets check if this block or
473 // one of this blocks parents is an inlined function.
474 Block *curr_inlined_block = block->GetContainingInlinedBlock();
475 if (curr_inlined_block) {
476 // "this->block" is contained in an inline function block, so to get the
477 // scope above the inlined block, we get the parent of the inlined block
479 Block *next_frame_block = curr_inlined_block->GetParent();
480 // Now calculate the symbol context of the containing block
481 next_frame_block->CalculateSymbolContext(&next_frame_sc);
483 // If we get here we weren't able to find the return line entry using the
484 // nesting of the blocks and the line table. So just use the call site
485 // info from our inlined block.
488 if (curr_inlined_block->GetRangeContainingAddress(curr_frame_pc, range)) {
489 // To see there this new frame block it, we need to look at the call
490 // site information from
491 const InlineFunctionInfo *curr_inlined_block_inlined_info =
492 curr_inlined_block->GetInlinedFunctionInfo();
493 next_frame_pc = range.GetBaseAddress();
494 next_frame_sc.line_entry.range.GetBaseAddress() = next_frame_pc;
495 next_frame_sc.line_entry.file =
496 curr_inlined_block_inlined_info->GetCallSite().GetFile();
497 next_frame_sc.line_entry.original_file =
498 curr_inlined_block_inlined_info->GetCallSite().GetFile();
499 next_frame_sc.line_entry.line =
500 curr_inlined_block_inlined_info->GetCallSite().GetLine();
501 next_frame_sc.line_entry.column =
502 curr_inlined_block_inlined_info->GetCallSite().GetColumn();
505 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
509 "warning: inlined block 0x%8.8" PRIx64
510 " doesn't have a range that contains file address 0x%" PRIx64,
511 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
513 #ifdef LLDB_CONFIGURATION_DEBUG
515 ObjectFile *objfile = NULL;
517 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
519 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
521 objfile = symbol_file->GetObjectFile();
526 Host::eSystemLogWarning,
527 "warning: inlined block 0x%8.8" PRIx64
528 " doesn't have a range that contains file address 0x%" PRIx64
530 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress(),
531 objfile->GetFileSpec().GetPath().c_str());
534 Host::eSystemLogWarning,
535 "warning: inlined block 0x%8.8" PRIx64
536 " doesn't have a range that contains file address 0x%" PRIx64
538 curr_inlined_block->GetID(), curr_frame_pc.GetFileAddress());
549 Block *SymbolContext::GetFunctionBlock() {
552 // If this symbol context has a block, check to see if this block is
553 // itself, or is contained within a block with inlined function
554 // information. If so, then the inlined block is the block that defines
556 Block *inlined_block = block->GetContainingInlinedBlock();
558 return inlined_block;
560 // The block in this symbol context is not inside an inlined block, so
561 // the block that defines the function is the function's top level block,
562 // which is returned below.
565 // There is no block information in this symbol context, so we must assume
566 // that the block that is desired is the top level block of the function
568 return &function->GetBlock(true);
573 bool SymbolContext::GetFunctionMethodInfo(lldb::LanguageType &language,
574 bool &is_instance_method,
575 ConstString &language_object_name)
578 Block *function_block = GetFunctionBlock();
579 if (function_block) {
580 CompilerDeclContext decl_ctx = function_block->GetDeclContext();
582 return decl_ctx.IsClassMethod(&language, &is_instance_method,
583 &language_object_name);
588 void SymbolContext::SortTypeList(TypeMap &type_map, TypeList &type_list) const {
589 Block *curr_block = block;
590 bool isInlinedblock = false;
591 if (curr_block != nullptr &&
592 curr_block->GetContainingInlinedBlock() != nullptr)
593 isInlinedblock = true;
595 //----------------------------------------------------------------------
596 // Find all types that match the current block if we have one and put them
597 // first in the list. Keep iterating up through all blocks.
598 //----------------------------------------------------------------------
599 while (curr_block != nullptr && !isInlinedblock) {
601 [curr_block, &type_list](const lldb::TypeSP &type_sp) -> bool {
602 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
603 if (scs && curr_block == scs->CalculateSymbolContextBlock())
604 type_list.Insert(type_sp);
605 return true; // Keep iterating
608 // Remove any entries that are now in "type_list" from "type_map" since we
609 // can't remove from type_map while iterating
610 type_list.ForEach([&type_map](const lldb::TypeSP &type_sp) -> bool {
611 type_map.Remove(type_sp);
612 return true; // Keep iterating
614 curr_block = curr_block->GetParent();
616 //----------------------------------------------------------------------
617 // Find all types that match the current function, if we have onem, and put
618 // them next in the list.
619 //----------------------------------------------------------------------
620 if (function != nullptr && !type_map.Empty()) {
621 const size_t old_type_list_size = type_list.GetSize();
622 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
623 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
624 if (scs && function == scs->CalculateSymbolContextFunction())
625 type_list.Insert(type_sp);
626 return true; // Keep iterating
629 // Remove any entries that are now in "type_list" from "type_map" since we
630 // can't remove from type_map while iterating
631 const size_t new_type_list_size = type_list.GetSize();
632 if (new_type_list_size > old_type_list_size) {
633 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
634 type_map.Remove(type_list.GetTypeAtIndex(i));
637 //----------------------------------------------------------------------
638 // Find all types that match the current compile unit, if we have one, and
639 // put them next in the list.
640 //----------------------------------------------------------------------
641 if (comp_unit != nullptr && !type_map.Empty()) {
642 const size_t old_type_list_size = type_list.GetSize();
644 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
645 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
646 if (scs && comp_unit == scs->CalculateSymbolContextCompileUnit())
647 type_list.Insert(type_sp);
648 return true; // Keep iterating
651 // Remove any entries that are now in "type_list" from "type_map" since we
652 // can't remove from type_map while iterating
653 const size_t new_type_list_size = type_list.GetSize();
654 if (new_type_list_size > old_type_list_size) {
655 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
656 type_map.Remove(type_list.GetTypeAtIndex(i));
659 //----------------------------------------------------------------------
660 // Find all types that match the current module, if we have one, and put them
662 //----------------------------------------------------------------------
663 if (module_sp && !type_map.Empty()) {
664 const size_t old_type_list_size = type_list.GetSize();
665 type_map.ForEach([this, &type_list](const lldb::TypeSP &type_sp) -> bool {
666 SymbolContextScope *scs = type_sp->GetSymbolContextScope();
667 if (scs && module_sp == scs->CalculateSymbolContextModule())
668 type_list.Insert(type_sp);
669 return true; // Keep iterating
671 // Remove any entries that are now in "type_list" from "type_map" since we
672 // can't remove from type_map while iterating
673 const size_t new_type_list_size = type_list.GetSize();
674 if (new_type_list_size > old_type_list_size) {
675 for (size_t i = old_type_list_size; i < new_type_list_size; ++i)
676 type_map.Remove(type_list.GetTypeAtIndex(i));
679 //----------------------------------------------------------------------
680 // Any types that are left get copied into the list an any order.
681 //----------------------------------------------------------------------
682 if (!type_map.Empty()) {
683 type_map.ForEach([&type_list](const lldb::TypeSP &type_sp) -> bool {
684 type_list.Insert(type_sp);
685 return true; // Keep iterating
691 SymbolContext::GetFunctionName(Mangled::NamePreference preference) const {
694 Block *inlined_block = block->GetContainingInlinedBlock();
697 const InlineFunctionInfo *inline_info =
698 inlined_block->GetInlinedFunctionInfo();
700 return inline_info->GetName(function->GetLanguage());
703 return function->GetMangled().GetName(function->GetLanguage(), preference);
704 } else if (symbol && symbol->ValueIsAddress()) {
705 return symbol->GetMangled().GetName(symbol->GetLanguage(), preference);
707 // No function, return an empty string.
708 return ConstString();
712 LineEntry SymbolContext::GetFunctionStartLineEntry() const {
713 LineEntry line_entry;
716 Block *inlined_block = block->GetContainingInlinedBlock();
718 if (inlined_block->GetStartAddress(start_addr)) {
719 if (start_addr.CalculateSymbolContextLineEntry(line_entry))
727 if (function->GetAddressRange()
729 .CalculateSymbolContextLineEntry(line_entry))
735 bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line,
738 if (!line_entry.IsValid()) {
739 error.SetErrorString("Symbol context has no line table.");
743 range = line_entry.range;
744 if (line_entry.line > end_line) {
745 error.SetErrorStringWithFormat(
746 "end line option %d must be after the current line: %d", end_line,
751 uint32_t line_index = 0;
755 line_index = comp_unit->FindLineEntry(line_index, line_entry.line, nullptr,
757 if (line_index == UINT32_MAX)
759 if (LineEntry::Compare(this_line, line_entry) == 0) {
767 // Can't find the index of the SymbolContext's line entry in the
768 // SymbolContext's CompUnit.
769 error.SetErrorString(
770 "Can't find the current line entry in the CompUnit - can't process "
771 "the end-line option");
775 line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false,
777 if (line_index == UINT32_MAX) {
778 error.SetErrorStringWithFormat(
779 "could not find a line table entry corresponding "
780 "to end line number %d",
785 Block *func_block = GetFunctionBlock();
787 func_block->GetRangeIndexContainingAddress(
788 end_entry.range.GetBaseAddress()) == UINT32_MAX) {
789 error.SetErrorStringWithFormat(
790 "end line number %d is not contained within the current function.",
795 lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() -
796 range.GetBaseAddress().GetFileAddress();
797 range.SetByteSize(range_size);
802 SymbolContext::FindBestGlobalDataSymbol(const ConstString &name, Status &error) {
809 Target &target = *target_sp;
810 Module *module = module_sp.get();
812 auto ProcessMatches = [this, &name, &target, module]
813 (SymbolContextList &sc_list, Status &error) -> const Symbol* {
814 llvm::SmallVector<const Symbol *, 1> external_symbols;
815 llvm::SmallVector<const Symbol *, 1> internal_symbols;
816 const uint32_t matches = sc_list.GetSize();
817 for (uint32_t i = 0; i < matches; ++i) {
818 SymbolContext sym_ctx;
819 sc_list.GetContextAtIndex(i, sym_ctx);
820 if (sym_ctx.symbol) {
821 const Symbol *symbol = sym_ctx.symbol;
822 const Address sym_address = symbol->GetAddress();
824 if (sym_address.IsValid()) {
825 switch (symbol->GetType()) {
826 case eSymbolTypeData:
827 case eSymbolTypeRuntime:
828 case eSymbolTypeAbsolute:
829 case eSymbolTypeObjCClass:
830 case eSymbolTypeObjCMetaClass:
831 case eSymbolTypeObjCIVar:
832 if (symbol->GetDemangledNameIsSynthesized()) {
833 // If the demangled name was synthesized, then don't use it for
834 // expressions. Only let the symbol match if the mangled named
835 // matches for these symbols.
836 if (symbol->GetMangled().GetMangledName() != name)
839 if (symbol->IsExternal()) {
840 external_symbols.push_back(symbol);
842 internal_symbols.push_back(symbol);
845 case eSymbolTypeReExported: {
846 ConstString reexport_name = symbol->GetReExportedSymbolName();
848 ModuleSP reexport_module_sp;
849 ModuleSpec reexport_module_spec;
850 reexport_module_spec.GetPlatformFileSpec() =
851 symbol->GetReExportedSymbolSharedLibrary();
852 if (reexport_module_spec.GetPlatformFileSpec()) {
854 target.GetImages().FindFirstModule(reexport_module_spec);
855 if (!reexport_module_sp) {
856 reexport_module_spec.GetPlatformFileSpec()
860 target.GetImages().FindFirstModule(reexport_module_spec);
863 // Don't allow us to try and resolve a re-exported symbol if it
864 // is the same as the current symbol
865 if (name == symbol->GetReExportedSymbolName() &&
866 module == reexport_module_sp.get())
869 return FindBestGlobalDataSymbol(
870 symbol->GetReExportedSymbolName(), error);
874 case eSymbolTypeCode: // We already lookup functions elsewhere
875 case eSymbolTypeVariable:
876 case eSymbolTypeLocal:
877 case eSymbolTypeParam:
878 case eSymbolTypeTrampoline:
879 case eSymbolTypeInvalid:
880 case eSymbolTypeException:
881 case eSymbolTypeSourceFile:
882 case eSymbolTypeHeaderFile:
883 case eSymbolTypeObjectFile:
884 case eSymbolTypeCommonBlock:
885 case eSymbolTypeBlock:
886 case eSymbolTypeVariableType:
887 case eSymbolTypeLineEntry:
888 case eSymbolTypeLineHeader:
889 case eSymbolTypeScopeBegin:
890 case eSymbolTypeScopeEnd:
891 case eSymbolTypeAdditional:
892 case eSymbolTypeCompiler:
893 case eSymbolTypeInstrumentation:
894 case eSymbolTypeUndefined:
895 case eSymbolTypeResolver:
902 if (external_symbols.size() > 1) {
904 ss.Printf("Multiple external symbols found for '%s'\n", name.AsCString());
905 for (const Symbol *symbol : external_symbols) {
906 symbol->GetDescription(&ss, eDescriptionLevelFull, &target);
909 error.SetErrorString(ss.GetData());
911 } else if (external_symbols.size()) {
912 return external_symbols[0];
913 } else if (internal_symbols.size() > 1) {
915 ss.Printf("Multiple internal symbols found for '%s'\n", name.AsCString());
916 for (const Symbol *symbol : internal_symbols) {
917 symbol->GetDescription(&ss, eDescriptionLevelVerbose, &target);
920 error.SetErrorString(ss.GetData());
922 } else if (internal_symbols.size()) {
923 return internal_symbols[0];
930 SymbolContextList sc_list;
931 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
932 const Symbol *const module_symbol = ProcessMatches(sc_list, error);
934 if (!error.Success()) {
936 } else if (module_symbol) {
937 return module_symbol;
942 SymbolContextList sc_list;
943 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny,
945 const Symbol *const target_symbol = ProcessMatches(sc_list, error);
947 if (!error.Success()) {
949 } else if (target_symbol) {
950 return target_symbol;
954 return nullptr; // no error; we just didn't find anything
958 //----------------------------------------------------------------------
960 // SymbolContextSpecifier
962 //----------------------------------------------------------------------
964 SymbolContextSpecifier::SymbolContextSpecifier(const TargetSP &target_sp)
965 : m_target_sp(target_sp), m_module_spec(), m_module_sp(), m_file_spec_ap(),
966 m_start_line(0), m_end_line(0), m_function_spec(), m_class_name(),
967 m_address_range_ap(), m_type(eNothingSpecified) {}
969 SymbolContextSpecifier::~SymbolContextSpecifier() {}
971 bool SymbolContextSpecifier::AddLineSpecification(uint32_t line_no,
972 SpecificationType type) {
973 bool return_value = true;
975 case eNothingSpecified:
978 case eLineStartSpecified:
979 m_start_line = line_no;
980 m_type |= eLineStartSpecified;
982 case eLineEndSpecified:
983 m_end_line = line_no;
984 m_type |= eLineEndSpecified;
987 return_value = false;
993 bool SymbolContextSpecifier::AddSpecification(const char *spec_string,
994 SpecificationType type) {
995 bool return_value = true;
997 case eNothingSpecified:
1000 case eModuleSpecified: {
1001 // See if we can find the Module, if so stick it in the SymbolContext.
1002 FileSpec module_file_spec(spec_string, false);
1003 ModuleSpec module_spec(module_file_spec);
1004 lldb::ModuleSP module_sp(
1005 m_target_sp->GetImages().FindFirstModule(module_spec));
1006 m_type |= eModuleSpecified;
1008 m_module_sp = module_sp;
1010 m_module_spec.assign(spec_string);
1012 case eFileSpecified:
1013 // CompUnits can't necessarily be resolved here, since an inlined function
1014 // might show up in a number of CompUnits. Instead we just convert to a
1015 // FileSpec and store it away.
1016 m_file_spec_ap.reset(new FileSpec(spec_string, false));
1017 m_type |= eFileSpecified;
1019 case eLineStartSpecified:
1020 m_start_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1022 m_type |= eLineStartSpecified;
1024 case eLineEndSpecified:
1025 m_end_line = StringConvert::ToSInt32(spec_string, 0, 0, &return_value);
1027 m_type |= eLineEndSpecified;
1029 case eFunctionSpecified:
1030 m_function_spec.assign(spec_string);
1031 m_type |= eFunctionSpecified;
1033 case eClassOrNamespaceSpecified:
1035 m_class_name.assign(spec_string);
1036 m_type = eClassOrNamespaceSpecified;
1038 case eAddressRangeSpecified:
1039 // Not specified yet...
1043 return return_value;
1046 void SymbolContextSpecifier::Clear() {
1047 m_module_spec.clear();
1048 m_file_spec_ap.reset();
1049 m_function_spec.clear();
1050 m_class_name.clear();
1053 m_address_range_ap.reset();
1055 m_type = eNothingSpecified;
1058 bool SymbolContextSpecifier::SymbolContextMatches(SymbolContext &sc) {
1059 if (m_type == eNothingSpecified)
1062 if (m_target_sp.get() != sc.target_sp.get())
1065 if (m_type & eModuleSpecified) {
1067 if (m_module_sp.get() != nullptr) {
1068 if (m_module_sp.get() != sc.module_sp.get())
1071 FileSpec module_file_spec(m_module_spec, false);
1072 if (!FileSpec::Equal(module_file_spec, sc.module_sp->GetFileSpec(),
1078 if (m_type & eFileSpecified) {
1079 if (m_file_spec_ap.get()) {
1080 // If we don't have a block or a comp_unit, then we aren't going to match
1082 if (sc.block == nullptr && sc.comp_unit == nullptr)
1085 // Check if the block is present, and if so is it inlined:
1086 bool was_inlined = false;
1087 if (sc.block != nullptr) {
1088 const InlineFunctionInfo *inline_info =
1089 sc.block->GetInlinedFunctionInfo();
1090 if (inline_info != nullptr) {
1092 if (!FileSpec::Equal(inline_info->GetDeclaration().GetFile(),
1093 *(m_file_spec_ap.get()), false))
1098 // Next check the comp unit, but only if the SymbolContext was not
1100 if (!was_inlined && sc.comp_unit != nullptr) {
1101 if (!FileSpec::Equal(*(sc.comp_unit), *(m_file_spec_ap.get()), false))
1106 if (m_type & eLineStartSpecified || m_type & eLineEndSpecified) {
1107 if (sc.line_entry.line < m_start_line || sc.line_entry.line > m_end_line)
1111 if (m_type & eFunctionSpecified) {
1112 // First check the current block, and if it is inlined, get the inlined
1114 bool was_inlined = false;
1115 ConstString func_name(m_function_spec.c_str());
1117 if (sc.block != nullptr) {
1118 const InlineFunctionInfo *inline_info =
1119 sc.block->GetInlinedFunctionInfo();
1120 if (inline_info != nullptr) {
1122 const Mangled &name = inline_info->GetMangled();
1123 if (!name.NameMatches(func_name, sc.function->GetLanguage()))
1127 // If it wasn't inlined, check the name in the function or symbol:
1129 if (sc.function != nullptr) {
1130 if (!sc.function->GetMangled().NameMatches(func_name,
1131 sc.function->GetLanguage()))
1133 } else if (sc.symbol != nullptr) {
1134 if (!sc.symbol->GetMangled().NameMatches(func_name,
1135 sc.symbol->GetLanguage()))
1144 bool SymbolContextSpecifier::AddressMatches(lldb::addr_t addr) {
1145 if (m_type & eAddressRangeSpecified) {
1148 Address match_address(addr, nullptr);
1150 m_target_sp->GetImages().ResolveSymbolContextForAddress(
1151 match_address, eSymbolContextEverything, sc);
1152 return SymbolContextMatches(sc);
1157 void SymbolContextSpecifier::GetDescription(
1158 Stream *s, lldb::DescriptionLevel level) const {
1159 char path_str[PATH_MAX + 1];
1161 if (m_type == eNothingSpecified) {
1162 s->Printf("Nothing specified.\n");
1165 if (m_type == eModuleSpecified) {
1168 m_module_sp->GetFileSpec().GetPath(path_str, PATH_MAX);
1169 s->Printf("Module: %s\n", path_str);
1171 s->Printf("Module: %s\n", m_module_spec.c_str());
1174 if (m_type == eFileSpecified && m_file_spec_ap.get() != nullptr) {
1175 m_file_spec_ap->GetPath(path_str, PATH_MAX);
1177 s->Printf("File: %s", path_str);
1178 if (m_type == eLineStartSpecified) {
1179 s->Printf(" from line %" PRIu64 "", (uint64_t)m_start_line);
1180 if (m_type == eLineEndSpecified)
1181 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1183 s->Printf("to end");
1184 } else if (m_type == eLineEndSpecified) {
1185 s->Printf(" from start to line %" PRIu64 "", (uint64_t)m_end_line);
1190 if (m_type == eLineStartSpecified) {
1192 s->Printf("From line %" PRIu64 "", (uint64_t)m_start_line);
1193 if (m_type == eLineEndSpecified)
1194 s->Printf("to line %" PRIu64 "", (uint64_t)m_end_line);
1196 s->Printf("to end");
1198 } else if (m_type == eLineEndSpecified) {
1199 s->Printf("From start to line %" PRIu64 ".\n", (uint64_t)m_end_line);
1202 if (m_type == eFunctionSpecified) {
1204 s->Printf("Function: %s.\n", m_function_spec.c_str());
1207 if (m_type == eClassOrNamespaceSpecified) {
1209 s->Printf("Class name: %s.\n", m_class_name.c_str());
1212 if (m_type == eAddressRangeSpecified && m_address_range_ap.get() != nullptr) {
1214 s->PutCString("Address range: ");
1215 m_address_range_ap->Dump(s, m_target_sp.get(),
1216 Address::DumpStyleLoadAddress,
1217 Address::DumpStyleFileAddress);
1218 s->PutCString("\n");
1222 //----------------------------------------------------------------------
1224 // SymbolContextList
1226 //----------------------------------------------------------------------
1228 SymbolContextList::SymbolContextList() : m_symbol_contexts() {}
1230 SymbolContextList::~SymbolContextList() {}
1232 void SymbolContextList::Append(const SymbolContext &sc) {
1233 m_symbol_contexts.push_back(sc);
1236 void SymbolContextList::Append(const SymbolContextList &sc_list) {
1237 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1238 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos)
1239 m_symbol_contexts.push_back(*pos);
1242 uint32_t SymbolContextList::AppendIfUnique(const SymbolContextList &sc_list,
1243 bool merge_symbol_into_function) {
1244 uint32_t unique_sc_add_count = 0;
1245 collection::const_iterator pos, end = sc_list.m_symbol_contexts.end();
1246 for (pos = sc_list.m_symbol_contexts.begin(); pos != end; ++pos) {
1247 if (AppendIfUnique(*pos, merge_symbol_into_function))
1248 ++unique_sc_add_count;
1250 return unique_sc_add_count;
1253 bool SymbolContextList::AppendIfUnique(const SymbolContext &sc,
1254 bool merge_symbol_into_function) {
1255 collection::iterator pos, end = m_symbol_contexts.end();
1256 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1260 if (merge_symbol_into_function && sc.symbol != nullptr &&
1261 sc.comp_unit == nullptr && sc.function == nullptr &&
1262 sc.block == nullptr && sc.line_entry.IsValid() == false) {
1263 if (sc.symbol->ValueIsAddress()) {
1264 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1265 // Don't merge symbols into inlined function symbol contexts
1266 if (pos->block && pos->block->GetContainingInlinedBlock())
1269 if (pos->function) {
1270 if (pos->function->GetAddressRange().GetBaseAddress() ==
1271 sc.symbol->GetAddressRef()) {
1272 // Do we already have a function with this symbol?
1273 if (pos->symbol == sc.symbol)
1275 if (pos->symbol == nullptr) {
1276 pos->symbol = sc.symbol;
1284 m_symbol_contexts.push_back(sc);
1288 bool SymbolContextList::MergeSymbolContextIntoFunctionContext(
1289 const SymbolContext &symbol_sc, uint32_t start_idx, uint32_t stop_idx) {
1290 if (symbol_sc.symbol != nullptr && symbol_sc.comp_unit == nullptr &&
1291 symbol_sc.function == nullptr && symbol_sc.block == nullptr &&
1292 symbol_sc.line_entry.IsValid() == false) {
1293 if (symbol_sc.symbol->ValueIsAddress()) {
1294 const size_t end = std::min<size_t>(m_symbol_contexts.size(), stop_idx);
1295 for (size_t i = start_idx; i < end; ++i) {
1296 const SymbolContext &function_sc = m_symbol_contexts[i];
1297 // Don't merge symbols into inlined function symbol contexts
1298 if (function_sc.block && function_sc.block->GetContainingInlinedBlock())
1301 if (function_sc.function) {
1302 if (function_sc.function->GetAddressRange().GetBaseAddress() ==
1303 symbol_sc.symbol->GetAddressRef()) {
1304 // Do we already have a function with this symbol?
1305 if (function_sc.symbol == symbol_sc.symbol)
1306 return true; // Already have a symbol context with this symbol,
1309 if (function_sc.symbol == nullptr) {
1310 // We successfully merged this symbol into an existing symbol
1312 m_symbol_contexts[i].symbol = symbol_sc.symbol;
1323 void SymbolContextList::Clear() { m_symbol_contexts.clear(); }
1325 void SymbolContextList::Dump(Stream *s, Target *target) const {
1329 s->PutCString("SymbolContextList");
1333 collection::const_iterator pos, end = m_symbol_contexts.end();
1334 for (pos = m_symbol_contexts.begin(); pos != end; ++pos) {
1335 // pos->Dump(s, target);
1336 pos->GetDescription(s, eDescriptionLevelVerbose, target);
1341 bool SymbolContextList::GetContextAtIndex(size_t idx, SymbolContext &sc) const {
1342 if (idx < m_symbol_contexts.size()) {
1343 sc = m_symbol_contexts[idx];
1349 bool SymbolContextList::GetLastContext(SymbolContext &sc) const {
1350 if (!m_symbol_contexts.empty()) {
1351 sc = m_symbol_contexts.back();
1357 bool SymbolContextList::RemoveContextAtIndex(size_t idx) {
1358 if (idx < m_symbol_contexts.size()) {
1359 m_symbol_contexts.erase(m_symbol_contexts.begin() + idx);
1365 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); }
1367 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const {
1368 uint32_t match_count = 0;
1369 const size_t size = m_symbol_contexts.size();
1370 for (size_t idx = 0; idx < size; ++idx) {
1371 if (m_symbol_contexts[idx].line_entry.line == line)
1377 void SymbolContextList::GetDescription(Stream *s, lldb::DescriptionLevel level,
1378 Target *target) const {
1379 const size_t size = m_symbol_contexts.size();
1380 for (size_t idx = 0; idx < size; ++idx)
1381 m_symbol_contexts[idx].GetDescription(s, level, target);
1384 bool lldb_private::operator==(const SymbolContextList &lhs,
1385 const SymbolContextList &rhs) {
1386 const uint32_t size = lhs.GetSize();
1387 if (size != rhs.GetSize())
1390 SymbolContext lhs_sc;
1391 SymbolContext rhs_sc;
1392 for (uint32_t i = 0; i < size; ++i) {
1393 lhs.GetContextAtIndex(i, lhs_sc);
1394 rhs.GetContextAtIndex(i, rhs_sc);
1395 if (lhs_sc != rhs_sc)
1401 bool lldb_private::operator!=(const SymbolContextList &lhs,
1402 const SymbolContextList &rhs) {
1403 return !(lhs == rhs);