Skip to content

Commit e20d83c

Browse files
committed
Remove redundant symbol lookups in IRExecutionUnit::FindInSymbols
When we search for a symbol, we first check if it is in the module_sp of the current SymbolContext, and if not, we check in the target's modules. However, the target's ModuleList also includes the already checked module, which leads to a redundant search in it.
1 parent 3c3df1b commit e20d83c

File tree

24 files changed

+120
-114
lines changed

24 files changed

+120
-114
lines changed

lldb/include/lldb/Core/ModuleList.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class ModuleList {
285285
/// \see Module::FindFunctions ()
286286
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask,
287287
const ModuleFunctionSearchOptions &options,
288-
SymbolContextList &sc_list) const;
288+
const SymbolContext &sc, SymbolContextList &sc_list) const;
289289

290290
/// \see Module::FindFunctionSymbols ()
291291
void FindFunctionSymbols(ConstString name,
@@ -295,7 +295,7 @@ class ModuleList {
295295
/// \see Module::FindFunctions ()
296296
void FindFunctions(const RegularExpression &name,
297297
const ModuleFunctionSearchOptions &options,
298-
SymbolContextList &sc_list);
298+
const SymbolContext &sc, SymbolContextList &sc_list);
299299

300300
/// Find global and static variables by name.
301301
///
@@ -355,6 +355,7 @@ class ModuleList {
355355

356356
void FindSymbolsWithNameAndType(ConstString name,
357357
lldb::SymbolType symbol_type,
358+
const SymbolContext &sc,
358359
SymbolContextList &sc_list) const;
359360

360361
void FindSymbolsMatchingRegExAndType(const RegularExpression &regex,

lldb/source/API/SBTarget.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,8 +1764,8 @@ lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
17641764
function_options.include_inlines = true;
17651765

17661766
FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
1767-
target_sp->GetImages().FindFunctions(ConstString(name), mask,
1768-
function_options, *sb_sc_list);
1767+
target_sp->GetImages().FindFunctions(
1768+
ConstString(name), mask, function_options, SymbolContext(), *sb_sc_list);
17691769
return sb_sc_list;
17701770
}
17711771

@@ -1787,22 +1787,24 @@ lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
17871787
switch (matchtype) {
17881788
case eMatchTypeRegex:
17891789
target_sp->GetImages().FindFunctions(RegularExpression(name_ref),
1790-
function_options, *sb_sc_list);
1790+
function_options, SymbolContext(),
1791+
*sb_sc_list);
17911792
break;
17921793
case eMatchTypeRegexInsensitive:
17931794
target_sp->GetImages().FindFunctions(
17941795
RegularExpression(name_ref, llvm::Regex::RegexFlags::IgnoreCase),
1795-
function_options, *sb_sc_list);
1796+
function_options, SymbolContext(), *sb_sc_list);
17961797
break;
17971798
case eMatchTypeStartsWith:
17981799
regexstr = llvm::Regex::escape(name) + ".*";
17991800
target_sp->GetImages().FindFunctions(RegularExpression(regexstr),
1800-
function_options, *sb_sc_list);
1801+
function_options, SymbolContext(),
1802+
*sb_sc_list);
18011803
break;
18021804
default:
1803-
target_sp->GetImages().FindFunctions(ConstString(name),
1804-
eFunctionNameTypeAny,
1805-
function_options, *sb_sc_list);
1805+
target_sp->GetImages().FindFunctions(
1806+
ConstString(name), eFunctionNameTypeAny, function_options,
1807+
SymbolContext(), *sb_sc_list);
18061808
break;
18071809
}
18081810
}
@@ -2274,7 +2276,7 @@ lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
22742276
TargetSP target_sp(GetSP());
22752277
if (target_sp)
22762278
target_sp->GetImages().FindSymbolsWithNameAndType(
2277-
ConstString(name), symbol_type, *sb_sc_list);
2279+
ConstString(name), symbol_type, SymbolContext(), *sb_sc_list);
22782280
}
22792281
return sb_sc_list;
22802282
}

lldb/source/Commands/CommandObjectDisassemble.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ CommandObjectDisassemble::GetNameRanges(CommandReturnObject &result) {
351351

352352
// Find functions matching the given name.
353353
SymbolContextList sc_list;
354-
GetTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
355-
function_options, sc_list);
354+
GetTarget().GetImages().FindFunctions(
355+
name, eFunctionNameTypeAuto, function_options, SymbolContext(), sc_list);
356356

357357
std::vector<AddressRange> ranges;
358358
llvm::Error range_errs = llvm::Error::success();

lldb/source/Commands/CommandObjectSource.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ class CommandObjectSourceInfo : public CommandObjectParsed {
382382
ModuleList module_list =
383383
(m_module_list.GetSize() > 0) ? m_module_list : target.GetImages();
384384
module_list.FindFunctions(name, eFunctionNameTypeAuto, function_options,
385-
sc_list_funcs);
385+
SymbolContext(), sc_list_funcs);
386386
size_t num_matches = sc_list_funcs.GetSize();
387387

388388
if (!num_matches) {
@@ -875,12 +875,14 @@ class CommandObjectSourceList : public CommandObjectParsed {
875875
target.GetImages().FindModules(module_spec, matching_modules);
876876

877877
matching_modules.FindFunctions(name, eFunctionNameTypeAuto,
878-
function_options, sc_list);
878+
function_options, SymbolContext(),
879+
sc_list);
879880
}
880881
}
881882
} else {
882883
target.GetImages().FindFunctions(name, eFunctionNameTypeAuto,
883-
function_options, sc_list);
884+
function_options, SymbolContext(),
885+
sc_list);
884886
}
885887
}
886888

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3541,7 +3541,8 @@ class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
35413541
function_options.include_symbols = true;
35423542
function_options.include_inlines = false;
35433543
target->GetImages().FindFunctions(function_name, eFunctionNameTypeAuto,
3544-
function_options, sc_list);
3544+
function_options, SymbolContext(),
3545+
sc_list);
35453546
} else if (m_options.m_type == eLookupTypeAddress && target) {
35463547
Address addr;
35473548
if (target->GetSectionLoadList().ResolveLoadAddress(m_options.m_addr,

lldb/source/Core/ModuleList.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,20 @@ ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
441441
void ModuleList::FindFunctions(ConstString name,
442442
FunctionNameType name_type_mask,
443443
const ModuleFunctionSearchOptions &options,
444+
const SymbolContext &sc,
444445
SymbolContextList &sc_list) const {
445446
const size_t old_size = sc_list.GetSize();
446447

447448
if (name_type_mask & eFunctionNameTypeAuto) {
448449
Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
449450

450451
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
452+
if (sc.module_sp)
453+
sc.module_sp->FindFunctions(lookup_info, CompilerDeclContext(), options,
454+
sc_list);
451455
for (const ModuleSP &module_sp : m_modules) {
456+
if (module_sp == sc.module_sp)
457+
continue;
452458
module_sp->FindFunctions(lookup_info, CompilerDeclContext(), options,
453459
sc_list);
454460
}
@@ -459,7 +465,12 @@ void ModuleList::FindFunctions(ConstString name,
459465
lookup_info.Prune(sc_list, old_size);
460466
} else {
461467
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
468+
if (sc.module_sp)
469+
sc.module_sp->FindFunctions(name, CompilerDeclContext(), name_type_mask,
470+
options, sc_list);
462471
for (const ModuleSP &module_sp : m_modules) {
472+
if (module_sp == sc.module_sp)
473+
continue;
463474
module_sp->FindFunctions(name, CompilerDeclContext(), name_type_mask,
464475
options, sc_list);
465476
}
@@ -494,10 +505,14 @@ void ModuleList::FindFunctionSymbols(ConstString name,
494505

495506
void ModuleList::FindFunctions(const RegularExpression &name,
496507
const ModuleFunctionSearchOptions &options,
508+
const SymbolContext &sc,
497509
SymbolContextList &sc_list) {
498510
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
511+
if (sc.module_sp)
512+
sc.module_sp->FindFunctions(name, options, sc_list);
499513
for (const ModuleSP &module_sp : m_modules)
500-
module_sp->FindFunctions(name, options, sc_list);
514+
if (module_sp != sc.module_sp)
515+
module_sp->FindFunctions(name, options, sc_list);
501516
}
502517

503518
void ModuleList::FindCompileUnits(const FileSpec &path,
@@ -526,10 +541,14 @@ void ModuleList::FindGlobalVariables(const RegularExpression &regex,
526541

527542
void ModuleList::FindSymbolsWithNameAndType(ConstString name,
528543
SymbolType symbol_type,
544+
const SymbolContext &sc,
529545
SymbolContextList &sc_list) const {
530546
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
547+
if (sc.module_sp)
548+
sc.module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
531549
for (const ModuleSP &module_sp : m_modules)
532-
module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
550+
if (module_sp != sc.module_sp)
551+
module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
533552
}
534553

535554
void ModuleList::FindSymbolsMatchingRegExAndType(

lldb/source/Expression/IRExecutionUnit.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -791,31 +791,17 @@ IRExecutionUnit::FindInSymbols(const std::vector<ConstString> &names,
791791
function_options.include_symbols = true;
792792
function_options.include_inlines = false;
793793

794+
const ModuleList &images = target->GetImages();
794795
for (const ConstString &name : names) {
795-
if (sc.module_sp) {
796-
SymbolContextList sc_list;
797-
sc.module_sp->FindFunctions(name, CompilerDeclContext(),
798-
lldb::eFunctionNameTypeFull, function_options,
799-
sc_list);
800-
if (auto load_addr = resolver.Resolve(sc_list))
801-
return *load_addr;
802-
}
803-
804-
if (sc.target_sp) {
805-
SymbolContextList sc_list;
806-
sc.target_sp->GetImages().FindFunctions(name, lldb::eFunctionNameTypeFull,
807-
function_options, sc_list);
808-
if (auto load_addr = resolver.Resolve(sc_list))
809-
return *load_addr;
810-
}
811-
812-
if (sc.target_sp) {
813-
SymbolContextList sc_list;
814-
sc.target_sp->GetImages().FindSymbolsWithNameAndType(
815-
name, lldb::eSymbolTypeAny, sc_list);
816-
if (auto load_addr = resolver.Resolve(sc_list))
817-
return *load_addr;
818-
}
796+
SymbolContextList sc_list;
797+
images.FindFunctions(name, lldb::eFunctionNameTypeFull, function_options,
798+
sc, sc_list);
799+
if (auto load_addr = resolver.Resolve(sc_list))
800+
return *load_addr;
801+
802+
images.FindSymbolsWithNameAndType(name, lldb::eSymbolTypeAny, sc, sc_list);
803+
if (auto load_addr = resolver.Resolve(sc_list))
804+
return *load_addr;
819805

820806
lldb::addr_t best_internal_load_address =
821807
resolver.GetBestInternalLoadAddress();

lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ DynamicLoaderHexagonDYLD::GetStepThroughTrampolinePlan(Thread &thread,
419419
Target &target = thread.GetProcess()->GetTarget();
420420
const ModuleList &images = target.GetImages();
421421

422-
images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
422+
images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, SymbolContext(),
423+
target_symbols);
423424
if (target_symbols.GetSize() == 0)
424425
return thread_plan_sp;
425426

lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/HexagonDYLDRendezvous.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ bool HexagonDYLDRendezvous::FindMetadata(const char *name, PThreadField field,
292292
Target &target = m_process->GetTarget();
293293

294294
SymbolContextList list;
295-
target.GetImages().FindSymbolsWithNameAndType(ConstString(name),
296-
eSymbolTypeAny, list);
295+
target.GetImages().FindSymbolsWithNameAndType(
296+
ConstString(name), eSymbolTypeAny, SymbolContext(), list);
297297
if (list.IsEmpty())
298298
return false;
299299

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
895895

896896
SymbolContextList code_symbols;
897897
images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
898-
code_symbols);
898+
current_context, code_symbols);
899899
for (const SymbolContext &context : code_symbols) {
900900
AddressRange addr_range;
901901
context.GetAddressRange(eSymbolContextEverything, 0, false,
@@ -911,8 +911,9 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
911911
}
912912

913913
SymbolContextList reexported_symbols;
914-
images.FindSymbolsWithNameAndType(
915-
trampoline_name, eSymbolTypeReExported, reexported_symbols);
914+
images.FindSymbolsWithNameAndType(trampoline_name,
915+
eSymbolTypeReExported,
916+
current_context, reexported_symbols);
916917
for (const SymbolContext &context : reexported_symbols) {
917918
if (context.symbol) {
918919
Symbol *actual_symbol =
@@ -935,7 +936,7 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
935936

936937
SymbolContextList indirect_symbols;
937938
images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver,
938-
indirect_symbols);
939+
current_context, indirect_symbols);
939940

940941
for (const SymbolContext &context : indirect_symbols) {
941942
AddressRange addr_range;

lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,8 @@ bool DYLDRendezvous::FindMetadata(const char *name, PThreadField field,
708708
Target &target = m_process->GetTarget();
709709

710710
SymbolContextList list;
711-
target.GetImages().FindSymbolsWithNameAndType(ConstString(name),
712-
eSymbolTypeAny, list);
711+
target.GetImages().FindSymbolsWithNameAndType(
712+
ConstString(name), eSymbolTypeAny, SymbolContext(), list);
713713
if (list.IsEmpty())
714714
return false;
715715

lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread,
519519
sym_name = ConstString(target_name);
520520
}
521521
}
522-
images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
522+
images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, context,
523+
target_symbols);
523524
if (!target_symbols.GetSize())
524525
return thread_plan_sp;
525526

lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,9 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
937937
ConstString instance_method_name(ms.GetString());
938938

939939
sc_list.Clear();
940-
m_target->GetImages().FindFunctions(instance_method_name,
941-
lldb::eFunctionNameTypeFull,
942-
function_options, sc_list);
940+
m_target->GetImages().FindFunctions(
941+
instance_method_name, lldb::eFunctionNameTypeFull, function_options,
942+
SymbolContext(), sc_list);
943943

944944
if (sc_list.GetSize())
945945
break;
@@ -950,9 +950,9 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
950950
ConstString class_method_name(ms.GetString());
951951

952952
sc_list.Clear();
953-
m_target->GetImages().FindFunctions(class_method_name,
954-
lldb::eFunctionNameTypeFull,
955-
function_options, sc_list);
953+
m_target->GetImages().FindFunctions(
954+
class_method_name, lldb::eFunctionNameTypeFull, function_options,
955+
SymbolContext(), sc_list);
956956

957957
if (sc_list.GetSize())
958958
break;
@@ -963,9 +963,9 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
963963

964964
SymbolContextList candidate_sc_list;
965965

966-
m_target->GetImages().FindFunctions(selector_name,
967-
lldb::eFunctionNameTypeSelector,
968-
function_options, candidate_sc_list);
966+
m_target->GetImages().FindFunctions(
967+
selector_name, lldb::eFunctionNameTypeSelector, function_options,
968+
SymbolContext(), candidate_sc_list);
969969

970970
for (const SymbolContext &candidate_sc : candidate_sc_list) {
971971
if (!candidate_sc.function)

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,8 @@ addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
523523
lldb::SymbolType symbol_type,
524524
lldb_private::Module *module) {
525525
SymbolContextList sc_list;
526-
527-
if (module)
528-
module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
529-
else
530-
target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
526+
const SymbolContext sc{ModuleSP(module)};
527+
target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc, sc_list);
531528

532529
addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
533530

@@ -1251,7 +1248,7 @@ void ClangExpressionDeclMap::LookupFunction(
12511248

12521249
target->GetImages().FindFunctions(
12531250
name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1254-
sc_list);
1251+
SymbolContext(), sc_list);
12551252
}
12561253

12571254
// If we found more than one function, see if we can use the frame's decl

lldb/source/Plugins/JITLoader/GDB/JITLoaderGDB.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ addr_t JITLoaderGDB::GetSymbolAddress(ModuleList &module_list,
446446
SymbolContextList target_symbols;
447447
Target &target = m_process->GetTarget();
448448

449-
module_list.FindSymbolsWithNameAndType(name, symbol_type, target_symbols);
449+
module_list.FindSymbolsWithNameAndType(name, symbol_type, SymbolContext(),
450+
target_symbols);
450451
if (target_symbols.IsEmpty())
451452
return LLDB_INVALID_ADDRESS;
452453

lldb/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ ValueObjectSP ItaniumABILanguageRuntime::GetExceptionObjectForThread(
634634
SymbolContext context;
635635

636636
modules.FindSymbolsWithNameAndType(
637-
ConstString("__cxa_current_exception_type"), eSymbolTypeCode, contexts);
637+
ConstString("__cxa_current_exception_type"), eSymbolTypeCode,
638+
SymbolContext(), contexts);
638639
contexts.GetContextAtIndex(0, context);
639640
if (!context.symbol) {
640641
return {};

0 commit comments

Comments
 (0)