Skip to content

Commit f0b3bdd

Browse files
authored
[lldb] Remove raw access to PluginInstances vector (#132884)
Remove raw access to PluginInstances vector This commit modifies the PluginInstances class to remove direct access to the m_instances vector. Instead, we expose a new `GetSnapshot` method that returns a copy of the current state of the instances vector. All external iteration over the instances is updated to use the new method. The motivation for the change is to allow modifying the way we store instances without having to change all the clients. This is a preliminary change to allow enabling/disabling of plugins in which case we want to iterate over only enabled plugins. We also considered using a custom iterator that wraps the vector iterator and can skip over disabled instances. That works, but the iterator code is a bit messy with all template and typedefs to make a compliant iterator.
1 parent a0e1e68 commit f0b3bdd

File tree

1 file changed

+84
-84
lines changed

1 file changed

+84
-84
lines changed

lldb/source/Core/PluginManager.cpp

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -226,30 +226,26 @@ template <typename Instance> class PluginInstances {
226226
}
227227

228228
typename Instance::CallbackType GetCallbackAtIndex(uint32_t idx) {
229-
if (Instance *instance = GetInstanceAtIndex(idx))
229+
if (const Instance *instance = GetInstanceAtIndex(idx))
230230
return instance->create_callback;
231231
return nullptr;
232232
}
233233

234234
llvm::StringRef GetDescriptionAtIndex(uint32_t idx) {
235-
if (Instance *instance = GetInstanceAtIndex(idx))
235+
if (const Instance *instance = GetInstanceAtIndex(idx))
236236
return instance->description;
237237
return "";
238238
}
239239

240240
llvm::StringRef GetNameAtIndex(uint32_t idx) {
241-
if (Instance *instance = GetInstanceAtIndex(idx))
241+
if (const Instance *instance = GetInstanceAtIndex(idx))
242242
return instance->name;
243243
return "";
244244
}
245245

246246
typename Instance::CallbackType GetCallbackForName(llvm::StringRef name) {
247-
if (name.empty())
248-
return nullptr;
249-
for (auto &instance : m_instances) {
250-
if (name == instance.name)
251-
return instance.create_callback;
252-
}
247+
if (const Instance *instance = GetInstanceForName(name))
248+
return instance->create_callback;
253249
return nullptr;
254250
}
255251

@@ -260,12 +256,33 @@ template <typename Instance> class PluginInstances {
260256
}
261257
}
262258

263-
const std::vector<Instance> &GetInstances() const { return m_instances; }
264-
std::vector<Instance> &GetInstances() { return m_instances; }
259+
// Return a copy of all the enabled instances.
260+
// Note that this is a copy of the internal state so modifications
261+
// to the returned instances will not be reflected back to instances
262+
// stored by the PluginInstances object.
263+
std::vector<Instance> GetSnapshot() { return m_instances; }
264+
265+
const Instance *GetInstanceAtIndex(uint32_t idx) {
266+
uint32_t count = 0;
267+
268+
return FindEnabledInstance(
269+
[&](const Instance &instance) { return count++ == idx; });
270+
}
271+
272+
const Instance *GetInstanceForName(llvm::StringRef name) {
273+
if (name.empty())
274+
return nullptr;
265275

266-
Instance *GetInstanceAtIndex(uint32_t idx) {
267-
if (idx < m_instances.size())
268-
return &m_instances[idx];
276+
return FindEnabledInstance(
277+
[&](const Instance &instance) { return instance.name == name; });
278+
}
279+
280+
const Instance *
281+
FindEnabledInstance(std::function<bool(const Instance &)> predicate) const {
282+
for (const auto &instance : m_instances) {
283+
if (predicate(instance))
284+
return &instance;
285+
}
269286
return nullptr;
270287
}
271288

@@ -571,17 +588,15 @@ PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(uint32_t idx) {
571588

572589
LanguageRuntimeGetCommandObject
573590
PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(uint32_t idx) {
574-
const auto &instances = GetLanguageRuntimeInstances().GetInstances();
575-
if (idx < instances.size())
576-
return instances[idx].command_callback;
591+
if (auto instance = GetLanguageRuntimeInstances().GetInstanceAtIndex(idx))
592+
return instance->command_callback;
577593
return nullptr;
578594
}
579595

580596
LanguageRuntimeGetExceptionPrecondition
581597
PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(uint32_t idx) {
582-
const auto &instances = GetLanguageRuntimeInstances().GetInstances();
583-
if (idx < instances.size())
584-
return instances[idx].precondition_callback;
598+
if (auto instance = GetLanguageRuntimeInstances().GetInstanceAtIndex(idx))
599+
return instance->precondition_callback;
585600
return nullptr;
586601
}
587602

@@ -643,12 +658,7 @@ bool PluginManager::IsRegisteredObjectFilePluginName(llvm::StringRef name) {
643658
if (name.empty())
644659
return false;
645660

646-
const auto &instances = GetObjectFileInstances().GetInstances();
647-
for (auto &instance : instances) {
648-
if (instance.name == name)
649-
return true;
650-
}
651-
return false;
661+
return GetObjectFileInstances().GetInstanceForName(name) != nullptr;
652662
}
653663

654664
bool PluginManager::RegisterPlugin(
@@ -674,29 +684,24 @@ PluginManager::GetObjectFileCreateCallbackAtIndex(uint32_t idx) {
674684

675685
ObjectFileCreateMemoryInstance
676686
PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(uint32_t idx) {
677-
const auto &instances = GetObjectFileInstances().GetInstances();
678-
if (idx < instances.size())
679-
return instances[idx].create_memory_callback;
687+
if (auto instance = GetObjectFileInstances().GetInstanceAtIndex(idx))
688+
return instance->create_memory_callback;
680689
return nullptr;
681690
}
682691

683692
ObjectFileGetModuleSpecifications
684693
PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
685694
uint32_t idx) {
686-
const auto &instances = GetObjectFileInstances().GetInstances();
687-
if (idx < instances.size())
688-
return instances[idx].get_module_specifications;
695+
if (auto instance = GetObjectFileInstances().GetInstanceAtIndex(idx))
696+
return instance->get_module_specifications;
689697
return nullptr;
690698
}
691699

692700
ObjectFileCreateMemoryInstance
693701
PluginManager::GetObjectFileCreateMemoryCallbackForPluginName(
694702
llvm::StringRef name) {
695-
const auto &instances = GetObjectFileInstances().GetInstances();
696-
for (auto &instance : instances) {
697-
if (instance.name == name)
698-
return instance.create_memory_callback;
699-
}
703+
if (auto instance = GetObjectFileInstances().GetInstanceForName(name))
704+
return instance->create_memory_callback;
700705
return nullptr;
701706
}
702707

@@ -729,7 +734,7 @@ Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
729734

730735
// Fall back to object plugins.
731736
const auto &plugin_name = options.GetPluginName().value_or("");
732-
auto &instances = GetObjectFileInstances().GetInstances();
737+
auto instances = GetObjectFileInstances().GetSnapshot();
733738
for (auto &instance : instances) {
734739
if (plugin_name.empty() || instance.name == plugin_name) {
735740
if (instance.save_core && instance.save_core(process_sp, options, error))
@@ -791,18 +796,16 @@ PluginManager::GetObjectContainerCreateCallbackAtIndex(uint32_t idx) {
791796

792797
ObjectContainerCreateMemoryInstance
793798
PluginManager::GetObjectContainerCreateMemoryCallbackAtIndex(uint32_t idx) {
794-
const auto &instances = GetObjectContainerInstances().GetInstances();
795-
if (idx < instances.size())
796-
return instances[idx].create_memory_callback;
799+
if (auto instance = GetObjectContainerInstances().GetInstanceAtIndex(idx))
800+
return instance->create_memory_callback;
797801
return nullptr;
798802
}
799803

800804
ObjectFileGetModuleSpecifications
801805
PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(
802806
uint32_t idx) {
803-
const auto &instances = GetObjectContainerInstances().GetInstances();
804-
if (idx < instances.size())
805-
return instances[idx].get_module_specifications;
807+
if (auto instance = GetObjectContainerInstances().GetInstanceAtIndex(idx))
808+
return instance->get_module_specifications;
806809
return nullptr;
807810
}
808811

@@ -849,7 +852,7 @@ PluginManager::GetPlatformCreateCallbackForPluginName(llvm::StringRef name) {
849852

850853
void PluginManager::AutoCompletePlatformName(llvm::StringRef name,
851854
CompletionRequest &request) {
852-
for (const auto &instance : GetPlatformInstances().GetInstances()) {
855+
for (const auto &instance : GetPlatformInstances().GetSnapshot()) {
853856
if (instance.name.starts_with(name))
854857
request.AddCompletion(instance.name);
855858
}
@@ -897,7 +900,7 @@ PluginManager::GetProcessCreateCallbackForPluginName(llvm::StringRef name) {
897900

898901
void PluginManager::AutoCompleteProcessName(llvm::StringRef name,
899902
CompletionRequest &request) {
900-
for (const auto &instance : GetProcessInstances().GetInstances()) {
903+
for (const auto &instance : GetProcessInstances().GetSnapshot()) {
901904
if (instance.name.starts_with(name))
902905
request.AddCompletion(instance.name, instance.description);
903906
}
@@ -935,11 +938,11 @@ bool PluginManager::UnregisterPlugin(
935938

936939
lldb::RegisterTypeBuilderSP
937940
PluginManager::GetRegisterTypeBuilder(Target &target) {
938-
const auto &instances = GetRegisterTypeBuilderInstances().GetInstances();
939941
// We assume that RegisterTypeBuilderClang is the only instance of this plugin
940942
// type and is always present.
941-
assert(instances.size());
942-
return instances[0].create_callback(target);
943+
auto instance = GetRegisterTypeBuilderInstances().GetInstanceAtIndex(0);
944+
assert(instance);
945+
return instance->create_callback(target);
943946
}
944947

945948
#pragma mark ScriptInterpreter
@@ -984,7 +987,7 @@ PluginManager::GetScriptInterpreterCreateCallbackAtIndex(uint32_t idx) {
984987
lldb::ScriptInterpreterSP
985988
PluginManager::GetScriptInterpreterForLanguage(lldb::ScriptLanguage script_lang,
986989
Debugger &debugger) {
987-
const auto &instances = GetScriptInterpreterInstances().GetInstances();
990+
const auto instances = GetScriptInterpreterInstances().GetSnapshot();
988991
ScriptInterpreterCreateInstance none_instance = nullptr;
989992
for (const auto &instance : instances) {
990993
if (instance.language == lldb::eScriptLanguageNone)
@@ -1046,13 +1049,12 @@ PluginManager::GetStructuredDataPluginCreateCallbackAtIndex(uint32_t idx) {
10461049
StructuredDataFilterLaunchInfo
10471050
PluginManager::GetStructuredDataFilterCallbackAtIndex(
10481051
uint32_t idx, bool &iteration_complete) {
1049-
const auto &instances = GetStructuredDataPluginInstances().GetInstances();
1050-
if (idx < instances.size()) {
1052+
if (auto instance =
1053+
GetStructuredDataPluginInstances().GetInstanceAtIndex(idx)) {
10511054
iteration_complete = false;
1052-
return instances[idx].filter_callback;
1053-
} else {
1054-
iteration_complete = true;
1055+
return instance->filter_callback;
10551056
}
1057+
iteration_complete = true;
10561058
return nullptr;
10571059
}
10581060

@@ -1167,7 +1169,7 @@ PluginManager::GetSymbolLocatorCreateCallbackAtIndex(uint32_t idx) {
11671169

11681170
ModuleSpec
11691171
PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
1170-
auto &instances = GetSymbolLocatorInstances().GetInstances();
1172+
auto instances = GetSymbolLocatorInstances().GetSnapshot();
11711173
for (auto &instance : instances) {
11721174
if (instance.locate_executable_object_file) {
11731175
std::optional<ModuleSpec> result =
@@ -1181,7 +1183,7 @@ PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
11811183

11821184
FileSpec PluginManager::LocateExecutableSymbolFile(
11831185
const ModuleSpec &module_spec, const FileSpecList &default_search_paths) {
1184-
auto &instances = GetSymbolLocatorInstances().GetInstances();
1186+
auto instances = GetSymbolLocatorInstances().GetSnapshot();
11851187
for (auto &instance : instances) {
11861188
if (instance.locate_executable_symbol_file) {
11871189
std::optional<FileSpec> result = instance.locate_executable_symbol_file(
@@ -1197,7 +1199,7 @@ bool PluginManager::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
11971199
Status &error,
11981200
bool force_lookup,
11991201
bool copy_executable) {
1200-
auto &instances = GetSymbolLocatorInstances().GetInstances();
1202+
auto instances = GetSymbolLocatorInstances().GetSnapshot();
12011203
for (auto &instance : instances) {
12021204
if (instance.download_object_symbol_file) {
12031205
if (instance.download_object_symbol_file(module_spec, error, force_lookup,
@@ -1211,7 +1213,7 @@ bool PluginManager::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
12111213
FileSpec PluginManager::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
12121214
const UUID *uuid,
12131215
const ArchSpec *arch) {
1214-
auto &instances = GetSymbolLocatorInstances().GetInstances();
1216+
auto instances = GetSymbolLocatorInstances().GetSnapshot();
12151217
for (auto &instance : instances) {
12161218
if (instance.find_symbol_file_in_bundle) {
12171219
std::optional<FileSpec> result =
@@ -1272,21 +1274,20 @@ PluginManager::GetTraceCreateCallback(llvm::StringRef plugin_name) {
12721274

12731275
TraceCreateInstanceForLiveProcess
12741276
PluginManager::GetTraceCreateCallbackForLiveProcess(llvm::StringRef plugin_name) {
1275-
for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1276-
if (instance.name == plugin_name)
1277-
return instance.create_callback_for_live_process;
1277+
if (auto instance = GetTracePluginInstances().GetInstanceForName(plugin_name))
1278+
return instance->create_callback_for_live_process;
1279+
12781280
return nullptr;
12791281
}
12801282

12811283
llvm::StringRef PluginManager::GetTraceSchema(llvm::StringRef plugin_name) {
1282-
for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1283-
if (instance.name == plugin_name)
1284-
return instance.schema;
1284+
if (auto instance = GetTracePluginInstances().GetInstanceForName(plugin_name))
1285+
return instance->schema;
12851286
return llvm::StringRef();
12861287
}
12871288

12881289
llvm::StringRef PluginManager::GetTraceSchema(size_t index) {
1289-
if (TraceInstance *instance =
1290+
if (const TraceInstance *instance =
12901291
GetTracePluginInstances().GetInstanceAtIndex(index))
12911292
return instance->schema;
12921293
return llvm::StringRef();
@@ -1335,7 +1336,7 @@ bool PluginManager::UnregisterPlugin(
13351336

13361337
ThreadTraceExportCommandCreator
13371338
PluginManager::GetThreadTraceExportCommandCreatorAtIndex(uint32_t index) {
1338-
if (TraceExporterInstance *instance =
1339+
if (const TraceExporterInstance *instance =
13391340
GetTraceExporterInstances().GetInstanceAtIndex(index))
13401341
return instance->create_thread_trace_export_command;
13411342
return nullptr;
@@ -1438,9 +1439,9 @@ bool PluginManager::UnregisterPlugin(
14381439

14391440
InstrumentationRuntimeGetType
14401441
PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(uint32_t idx) {
1441-
const auto &instances = GetInstrumentationRuntimeInstances().GetInstances();
1442-
if (idx < instances.size())
1443-
return instances[idx].get_type_callback;
1442+
if (auto instance =
1443+
GetInstrumentationRuntimeInstances().GetInstanceAtIndex(idx))
1444+
return instance->get_type_callback;
14441445
return nullptr;
14451446
}
14461447

@@ -1493,15 +1494,15 @@ PluginManager::GetTypeSystemCreateCallbackAtIndex(uint32_t idx) {
14931494
}
14941495

14951496
LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForTypes() {
1496-
const auto &instances = GetTypeSystemInstances().GetInstances();
1497+
const auto instances = GetTypeSystemInstances().GetSnapshot();
14971498
LanguageSet all;
14981499
for (unsigned i = 0; i < instances.size(); ++i)
14991500
all.bitvector |= instances[i].supported_languages_for_types.bitvector;
15001501
return all;
15011502
}
15021503

15031504
LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForExpressions() {
1504-
const auto &instances = GetTypeSystemInstances().GetInstances();
1505+
const auto instances = GetTypeSystemInstances().GetSnapshot();
15051506
LanguageSet all;
15061507
for (unsigned i = 0; i < instances.size(); ++i)
15071508
all.bitvector |= instances[i].supported_languages_for_expressions.bitvector;
@@ -1545,7 +1546,7 @@ bool PluginManager::UnregisterPlugin(
15451546
}
15461547

15471548
uint32_t PluginManager::GetNumScriptedInterfaces() {
1548-
return GetScriptedInterfaceInstances().GetInstances().size();
1549+
return GetScriptedInterfaceInstances().GetSnapshot().size();
15491550
}
15501551

15511552
llvm::StringRef PluginManager::GetScriptedInterfaceNameAtIndex(uint32_t index) {
@@ -1559,17 +1560,16 @@ PluginManager::GetScriptedInterfaceDescriptionAtIndex(uint32_t index) {
15591560

15601561
lldb::ScriptLanguage
15611562
PluginManager::GetScriptedInterfaceLanguageAtIndex(uint32_t idx) {
1562-
const auto &instances = GetScriptedInterfaceInstances().GetInstances();
1563-
return idx < instances.size() ? instances[idx].language
1564-
: ScriptLanguage::eScriptLanguageNone;
1563+
if (auto instance = GetScriptedInterfaceInstances().GetInstanceAtIndex(idx))
1564+
return instance->language;
1565+
return ScriptLanguage::eScriptLanguageNone;
15651566
}
15661567

15671568
ScriptedInterfaceUsages
15681569
PluginManager::GetScriptedInterfaceUsagesAtIndex(uint32_t idx) {
1569-
const auto &instances = GetScriptedInterfaceInstances().GetInstances();
1570-
if (idx >= instances.size())
1571-
return {};
1572-
return instances[idx].usages;
1570+
if (auto instance = GetScriptedInterfaceInstances().GetInstanceAtIndex(idx))
1571+
return instance->usages;
1572+
return {};
15731573
}
15741574

15751575
#pragma mark REPL
@@ -1606,13 +1606,13 @@ REPLCreateInstance PluginManager::GetREPLCreateCallbackAtIndex(uint32_t idx) {
16061606
}
16071607

16081608
LanguageSet PluginManager::GetREPLSupportedLanguagesAtIndex(uint32_t idx) {
1609-
const auto &instances = GetREPLInstances().GetInstances();
1610-
return idx < instances.size() ? instances[idx].supported_languages
1611-
: LanguageSet();
1609+
if (auto instance = GetREPLInstances().GetInstanceAtIndex(idx))
1610+
return instance->supported_languages;
1611+
return LanguageSet();
16121612
}
16131613

16141614
LanguageSet PluginManager::GetREPLAllTypeSystemSupportedLanguages() {
1615-
const auto &instances = GetREPLInstances().GetInstances();
1615+
const auto instances = GetREPLInstances().GetSnapshot();
16161616
LanguageSet all;
16171617
for (unsigned i = 0; i < instances.size(); ++i)
16181618
all.bitvector |= instances[i].supported_languages.bitvector;

0 commit comments

Comments
 (0)