llvm-project
1826 строк · 67.0 Кб
1//===-- PluginManager.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/PluginManager.h"10
11#include "lldb/Core/Debugger.h"12#include "lldb/Host/FileSystem.h"13#include "lldb/Host/HostInfo.h"14#include "lldb/Interpreter/OptionValueProperties.h"15#include "lldb/Target/Process.h"16#include "lldb/Utility/FileSpec.h"17#include "lldb/Utility/Status.h"18#include "lldb/Utility/StringList.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/Support/DynamicLibrary.h"21#include "llvm/Support/FileSystem.h"22#include "llvm/Support/raw_ostream.h"23#include <cassert>24#include <map>25#include <memory>26#include <mutex>27#include <string>28#include <utility>29#include <vector>30#if defined(_WIN32)31#include "lldb/Host/windows/PosixApi.h"32#endif33
34using namespace lldb;35using namespace lldb_private;36
37typedef bool (*PluginInitCallback)();38typedef void (*PluginTermCallback)();39
40struct PluginInfo {41PluginInfo() = default;42
43llvm::sys::DynamicLibrary library;44PluginInitCallback plugin_init_callback = nullptr;45PluginTermCallback plugin_term_callback = nullptr;46};47
48typedef std::map<FileSpec, PluginInfo> PluginTerminateMap;49
50static std::recursive_mutex &GetPluginMapMutex() {51static std::recursive_mutex g_plugin_map_mutex;52return g_plugin_map_mutex;53}
54
55static PluginTerminateMap &GetPluginMap() {56static PluginTerminateMap g_plugin_map;57return g_plugin_map;58}
59
60static bool PluginIsLoaded(const FileSpec &plugin_file_spec) {61std::lock_guard<std::recursive_mutex> guard(GetPluginMapMutex());62PluginTerminateMap &plugin_map = GetPluginMap();63return plugin_map.find(plugin_file_spec) != plugin_map.end();64}
65
66static void SetPluginInfo(const FileSpec &plugin_file_spec,67const PluginInfo &plugin_info) {68std::lock_guard<std::recursive_mutex> guard(GetPluginMapMutex());69PluginTerminateMap &plugin_map = GetPluginMap();70assert(plugin_map.find(plugin_file_spec) == plugin_map.end());71plugin_map[plugin_file_spec] = plugin_info;72}
73
74template <typename FPtrTy> static FPtrTy CastToFPtr(void *VPtr) {75return reinterpret_cast<FPtrTy>(VPtr);76}
77
78static FileSystem::EnumerateDirectoryResult79LoadPluginCallback(void *baton, llvm::sys::fs::file_type ft,80llvm::StringRef path) {81Status error;82
83namespace fs = llvm::sys::fs;84// If we have a regular file, a symbolic link or unknown file type, try and85// process the file. We must handle unknown as sometimes the directory86// enumeration might be enumerating a file system that doesn't have correct87// file type information.88if (ft == fs::file_type::regular_file || ft == fs::file_type::symlink_file ||89ft == fs::file_type::type_unknown) {90FileSpec plugin_file_spec(path);91FileSystem::Instance().Resolve(plugin_file_spec);92
93if (PluginIsLoaded(plugin_file_spec))94return FileSystem::eEnumerateDirectoryResultNext;95else {96PluginInfo plugin_info;97
98std::string pluginLoadError;99plugin_info.library = llvm::sys::DynamicLibrary::getPermanentLibrary(100plugin_file_spec.GetPath().c_str(), &pluginLoadError);101if (plugin_info.library.isValid()) {102bool success = false;103plugin_info.plugin_init_callback = CastToFPtr<PluginInitCallback>(104plugin_info.library.getAddressOfSymbol("LLDBPluginInitialize"));105if (plugin_info.plugin_init_callback) {106// Call the plug-in "bool LLDBPluginInitialize(void)" function107success = plugin_info.plugin_init_callback();108}109
110if (success) {111// It is ok for the "LLDBPluginTerminate" symbol to be nullptr112plugin_info.plugin_term_callback = CastToFPtr<PluginTermCallback>(113plugin_info.library.getAddressOfSymbol("LLDBPluginTerminate"));114} else {115// The initialize function returned FALSE which means the plug-in116// might not be compatible, or might be too new or too old, or might117// not want to run on this machine. Set it to a default-constructed118// instance to invalidate it.119plugin_info = PluginInfo();120}121
122// Regardless of success or failure, cache the plug-in load in our123// plug-in info so we don't try to load it again and again.124SetPluginInfo(plugin_file_spec, plugin_info);125
126return FileSystem::eEnumerateDirectoryResultNext;127}128}129}130
131if (ft == fs::file_type::directory_file ||132ft == fs::file_type::symlink_file || ft == fs::file_type::type_unknown) {133// Try and recurse into anything that a directory or symbolic link. We must134// also do this for unknown as sometimes the directory enumeration might be135// enumerating a file system that doesn't have correct file type136// information.137return FileSystem::eEnumerateDirectoryResultEnter;138}139
140return FileSystem::eEnumerateDirectoryResultNext;141}
142
143void PluginManager::Initialize() {144const bool find_directories = true;145const bool find_files = true;146const bool find_other = true;147char dir_path[PATH_MAX];148if (FileSpec dir_spec = HostInfo::GetSystemPluginDir()) {149if (FileSystem::Instance().Exists(dir_spec) &&150dir_spec.GetPath(dir_path, sizeof(dir_path))) {151FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,152find_files, find_other,153LoadPluginCallback, nullptr);154}155}156
157if (FileSpec dir_spec = HostInfo::GetUserPluginDir()) {158if (FileSystem::Instance().Exists(dir_spec) &&159dir_spec.GetPath(dir_path, sizeof(dir_path))) {160FileSystem::Instance().EnumerateDirectory(dir_path, find_directories,161find_files, find_other,162LoadPluginCallback, nullptr);163}164}165}
166
167void PluginManager::Terminate() {168std::lock_guard<std::recursive_mutex> guard(GetPluginMapMutex());169PluginTerminateMap &plugin_map = GetPluginMap();170
171PluginTerminateMap::const_iterator pos, end = plugin_map.end();172for (pos = plugin_map.begin(); pos != end; ++pos) {173// Call the plug-in "void LLDBPluginTerminate (void)" function if there is174// one (if the symbol was not nullptr).175if (pos->second.library.isValid()) {176if (pos->second.plugin_term_callback)177pos->second.plugin_term_callback();178}179}180plugin_map.clear();181}
182
183template <typename Callback> struct PluginInstance {184typedef Callback CallbackType;185
186PluginInstance() = default;187PluginInstance(llvm::StringRef name, llvm::StringRef description,188Callback create_callback,189DebuggerInitializeCallback debugger_init_callback = nullptr)190: name(name), description(description), create_callback(create_callback),191debugger_init_callback(debugger_init_callback) {}192
193llvm::StringRef name;194llvm::StringRef description;195Callback create_callback;196DebuggerInitializeCallback debugger_init_callback;197};198
199template <typename Instance> class PluginInstances {200public:201template <typename... Args>202bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,203typename Instance::CallbackType callback,204Args &&...args) {205if (!callback)206return false;207assert(!name.empty());208Instance instance =209Instance(name, description, callback, std::forward<Args>(args)...);210m_instances.push_back(instance);211return false;212}213
214bool UnregisterPlugin(typename Instance::CallbackType callback) {215if (!callback)216return false;217auto pos = m_instances.begin();218auto end = m_instances.end();219for (; pos != end; ++pos) {220if (pos->create_callback == callback) {221m_instances.erase(pos);222return true;223}224}225return false;226}227
228typename Instance::CallbackType GetCallbackAtIndex(uint32_t idx) {229if (Instance *instance = GetInstanceAtIndex(idx))230return instance->create_callback;231return nullptr;232}233
234llvm::StringRef GetDescriptionAtIndex(uint32_t idx) {235if (Instance *instance = GetInstanceAtIndex(idx))236return instance->description;237return "";238}239
240llvm::StringRef GetNameAtIndex(uint32_t idx) {241if (Instance *instance = GetInstanceAtIndex(idx))242return instance->name;243return "";244}245
246typename Instance::CallbackType GetCallbackForName(llvm::StringRef name) {247if (name.empty())248return nullptr;249for (auto &instance : m_instances) {250if (name == instance.name)251return instance.create_callback;252}253return nullptr;254}255
256void PerformDebuggerCallback(Debugger &debugger) {257for (auto &instance : m_instances) {258if (instance.debugger_init_callback)259instance.debugger_init_callback(debugger);260}261}262
263const std::vector<Instance> &GetInstances() const { return m_instances; }264std::vector<Instance> &GetInstances() { return m_instances; }265
266Instance *GetInstanceAtIndex(uint32_t idx) {267if (idx < m_instances.size())268return &m_instances[idx];269return nullptr;270}271
272private:273std::vector<Instance> m_instances;274};275
276#pragma mark ABI277
278typedef PluginInstance<ABICreateInstance> ABIInstance;279typedef PluginInstances<ABIInstance> ABIInstances;280
281static ABIInstances &GetABIInstances() {282static ABIInstances g_instances;283return g_instances;284}
285
286bool PluginManager::RegisterPlugin(llvm::StringRef name,287llvm::StringRef description,288ABICreateInstance create_callback) {289return GetABIInstances().RegisterPlugin(name, description, create_callback);290}
291
292bool PluginManager::UnregisterPlugin(ABICreateInstance create_callback) {293return GetABIInstances().UnregisterPlugin(create_callback);294}
295
296ABICreateInstance PluginManager::GetABICreateCallbackAtIndex(uint32_t idx) {297return GetABIInstances().GetCallbackAtIndex(idx);298}
299
300#pragma mark Architecture301
302typedef PluginInstance<ArchitectureCreateInstance> ArchitectureInstance;303typedef std::vector<ArchitectureInstance> ArchitectureInstances;304
305static ArchitectureInstances &GetArchitectureInstances() {306static ArchitectureInstances g_instances;307return g_instances;308}
309
310void PluginManager::RegisterPlugin(llvm::StringRef name,311llvm::StringRef description,312ArchitectureCreateInstance create_callback) {313GetArchitectureInstances().push_back({name, description, create_callback});314}
315
316void PluginManager::UnregisterPlugin(317ArchitectureCreateInstance create_callback) {318auto &instances = GetArchitectureInstances();319
320for (auto pos = instances.begin(), end = instances.end(); pos != end; ++pos) {321if (pos->create_callback == create_callback) {322instances.erase(pos);323return;324}325}326llvm_unreachable("Plugin not found");327}
328
329std::unique_ptr<Architecture>330PluginManager::CreateArchitectureInstance(const ArchSpec &arch) {331for (const auto &instances : GetArchitectureInstances()) {332if (auto plugin_up = instances.create_callback(arch))333return plugin_up;334}335return nullptr;336}
337
338#pragma mark Disassembler339
340typedef PluginInstance<DisassemblerCreateInstance> DisassemblerInstance;341typedef PluginInstances<DisassemblerInstance> DisassemblerInstances;342
343static DisassemblerInstances &GetDisassemblerInstances() {344static DisassemblerInstances g_instances;345return g_instances;346}
347
348bool PluginManager::RegisterPlugin(llvm::StringRef name,349llvm::StringRef description,350DisassemblerCreateInstance create_callback) {351return GetDisassemblerInstances().RegisterPlugin(name, description,352create_callback);353}
354
355bool PluginManager::UnregisterPlugin(356DisassemblerCreateInstance create_callback) {357return GetDisassemblerInstances().UnregisterPlugin(create_callback);358}
359
360DisassemblerCreateInstance
361PluginManager::GetDisassemblerCreateCallbackAtIndex(uint32_t idx) {362return GetDisassemblerInstances().GetCallbackAtIndex(idx);363}
364
365DisassemblerCreateInstance
366PluginManager::GetDisassemblerCreateCallbackForPluginName(367llvm::StringRef name) {368return GetDisassemblerInstances().GetCallbackForName(name);369}
370
371#pragma mark DynamicLoader372
373typedef PluginInstance<DynamicLoaderCreateInstance> DynamicLoaderInstance;374typedef PluginInstances<DynamicLoaderInstance> DynamicLoaderInstances;375
376static DynamicLoaderInstances &GetDynamicLoaderInstances() {377static DynamicLoaderInstances g_instances;378return g_instances;379}
380
381bool PluginManager::RegisterPlugin(382llvm::StringRef name, llvm::StringRef description,383DynamicLoaderCreateInstance create_callback,384DebuggerInitializeCallback debugger_init_callback) {385return GetDynamicLoaderInstances().RegisterPlugin(386name, description, create_callback, debugger_init_callback);387}
388
389bool PluginManager::UnregisterPlugin(390DynamicLoaderCreateInstance create_callback) {391return GetDynamicLoaderInstances().UnregisterPlugin(create_callback);392}
393
394DynamicLoaderCreateInstance
395PluginManager::GetDynamicLoaderCreateCallbackAtIndex(uint32_t idx) {396return GetDynamicLoaderInstances().GetCallbackAtIndex(idx);397}
398
399DynamicLoaderCreateInstance
400PluginManager::GetDynamicLoaderCreateCallbackForPluginName(401llvm::StringRef name) {402return GetDynamicLoaderInstances().GetCallbackForName(name);403}
404
405#pragma mark JITLoader406
407typedef PluginInstance<JITLoaderCreateInstance> JITLoaderInstance;408typedef PluginInstances<JITLoaderInstance> JITLoaderInstances;409
410static JITLoaderInstances &GetJITLoaderInstances() {411static JITLoaderInstances g_instances;412return g_instances;413}
414
415bool PluginManager::RegisterPlugin(416llvm::StringRef name, llvm::StringRef description,417JITLoaderCreateInstance create_callback,418DebuggerInitializeCallback debugger_init_callback) {419return GetJITLoaderInstances().RegisterPlugin(420name, description, create_callback, debugger_init_callback);421}
422
423bool PluginManager::UnregisterPlugin(JITLoaderCreateInstance create_callback) {424return GetJITLoaderInstances().UnregisterPlugin(create_callback);425}
426
427JITLoaderCreateInstance
428PluginManager::GetJITLoaderCreateCallbackAtIndex(uint32_t idx) {429return GetJITLoaderInstances().GetCallbackAtIndex(idx);430}
431
432#pragma mark EmulateInstruction433
434typedef PluginInstance<EmulateInstructionCreateInstance>435EmulateInstructionInstance;436typedef PluginInstances<EmulateInstructionInstance> EmulateInstructionInstances;437
438static EmulateInstructionInstances &GetEmulateInstructionInstances() {439static EmulateInstructionInstances g_instances;440return g_instances;441}
442
443bool PluginManager::RegisterPlugin(444llvm::StringRef name, llvm::StringRef description,445EmulateInstructionCreateInstance create_callback) {446return GetEmulateInstructionInstances().RegisterPlugin(name, description,447create_callback);448}
449
450bool PluginManager::UnregisterPlugin(451EmulateInstructionCreateInstance create_callback) {452return GetEmulateInstructionInstances().UnregisterPlugin(create_callback);453}
454
455EmulateInstructionCreateInstance
456PluginManager::GetEmulateInstructionCreateCallbackAtIndex(uint32_t idx) {457return GetEmulateInstructionInstances().GetCallbackAtIndex(idx);458}
459
460EmulateInstructionCreateInstance
461PluginManager::GetEmulateInstructionCreateCallbackForPluginName(462llvm::StringRef name) {463return GetEmulateInstructionInstances().GetCallbackForName(name);464}
465
466#pragma mark OperatingSystem467
468typedef PluginInstance<OperatingSystemCreateInstance> OperatingSystemInstance;469typedef PluginInstances<OperatingSystemInstance> OperatingSystemInstances;470
471static OperatingSystemInstances &GetOperatingSystemInstances() {472static OperatingSystemInstances g_instances;473return g_instances;474}
475
476bool PluginManager::RegisterPlugin(477llvm::StringRef name, llvm::StringRef description,478OperatingSystemCreateInstance create_callback,479DebuggerInitializeCallback debugger_init_callback) {480return GetOperatingSystemInstances().RegisterPlugin(481name, description, create_callback, debugger_init_callback);482}
483
484bool PluginManager::UnregisterPlugin(485OperatingSystemCreateInstance create_callback) {486return GetOperatingSystemInstances().UnregisterPlugin(create_callback);487}
488
489OperatingSystemCreateInstance
490PluginManager::GetOperatingSystemCreateCallbackAtIndex(uint32_t idx) {491return GetOperatingSystemInstances().GetCallbackAtIndex(idx);492}
493
494OperatingSystemCreateInstance
495PluginManager::GetOperatingSystemCreateCallbackForPluginName(496llvm::StringRef name) {497return GetOperatingSystemInstances().GetCallbackForName(name);498}
499
500#pragma mark Language501
502typedef PluginInstance<LanguageCreateInstance> LanguageInstance;503typedef PluginInstances<LanguageInstance> LanguageInstances;504
505static LanguageInstances &GetLanguageInstances() {506static LanguageInstances g_instances;507return g_instances;508}
509
510bool PluginManager::RegisterPlugin(llvm::StringRef name,511llvm::StringRef description,512LanguageCreateInstance create_callback) {513return GetLanguageInstances().RegisterPlugin(name, description,514create_callback);515}
516
517bool PluginManager::UnregisterPlugin(LanguageCreateInstance create_callback) {518return GetLanguageInstances().UnregisterPlugin(create_callback);519}
520
521LanguageCreateInstance
522PluginManager::GetLanguageCreateCallbackAtIndex(uint32_t idx) {523return GetLanguageInstances().GetCallbackAtIndex(idx);524}
525
526#pragma mark LanguageRuntime527
528struct LanguageRuntimeInstance529: public PluginInstance<LanguageRuntimeCreateInstance> {530LanguageRuntimeInstance(531llvm::StringRef name, llvm::StringRef description,532CallbackType create_callback,533DebuggerInitializeCallback debugger_init_callback,534LanguageRuntimeGetCommandObject command_callback,535LanguageRuntimeGetExceptionPrecondition precondition_callback)536: PluginInstance<LanguageRuntimeCreateInstance>(537name, description, create_callback, debugger_init_callback),538command_callback(command_callback),539precondition_callback(precondition_callback) {}540
541LanguageRuntimeGetCommandObject command_callback;542LanguageRuntimeGetExceptionPrecondition precondition_callback;543};544
545typedef PluginInstances<LanguageRuntimeInstance> LanguageRuntimeInstances;546
547static LanguageRuntimeInstances &GetLanguageRuntimeInstances() {548static LanguageRuntimeInstances g_instances;549return g_instances;550}
551
552bool PluginManager::RegisterPlugin(553llvm::StringRef name, llvm::StringRef description,554LanguageRuntimeCreateInstance create_callback,555LanguageRuntimeGetCommandObject command_callback,556LanguageRuntimeGetExceptionPrecondition precondition_callback) {557return GetLanguageRuntimeInstances().RegisterPlugin(558name, description, create_callback, nullptr, command_callback,559precondition_callback);560}
561
562bool PluginManager::UnregisterPlugin(563LanguageRuntimeCreateInstance create_callback) {564return GetLanguageRuntimeInstances().UnregisterPlugin(create_callback);565}
566
567LanguageRuntimeCreateInstance
568PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(uint32_t idx) {569return GetLanguageRuntimeInstances().GetCallbackAtIndex(idx);570}
571
572LanguageRuntimeGetCommandObject
573PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(uint32_t idx) {574const auto &instances = GetLanguageRuntimeInstances().GetInstances();575if (idx < instances.size())576return instances[idx].command_callback;577return nullptr;578}
579
580LanguageRuntimeGetExceptionPrecondition
581PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(uint32_t idx) {582const auto &instances = GetLanguageRuntimeInstances().GetInstances();583if (idx < instances.size())584return instances[idx].precondition_callback;585return nullptr;586}
587
588#pragma mark SystemRuntime589
590typedef PluginInstance<SystemRuntimeCreateInstance> SystemRuntimeInstance;591typedef PluginInstances<SystemRuntimeInstance> SystemRuntimeInstances;592
593static SystemRuntimeInstances &GetSystemRuntimeInstances() {594static SystemRuntimeInstances g_instances;595return g_instances;596}
597
598bool PluginManager::RegisterPlugin(599llvm::StringRef name, llvm::StringRef description,600SystemRuntimeCreateInstance create_callback) {601return GetSystemRuntimeInstances().RegisterPlugin(name, description,602create_callback);603}
604
605bool PluginManager::UnregisterPlugin(606SystemRuntimeCreateInstance create_callback) {607return GetSystemRuntimeInstances().UnregisterPlugin(create_callback);608}
609
610SystemRuntimeCreateInstance
611PluginManager::GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx) {612return GetSystemRuntimeInstances().GetCallbackAtIndex(idx);613}
614
615#pragma mark ObjectFile616
617struct ObjectFileInstance : public PluginInstance<ObjectFileCreateInstance> {618ObjectFileInstance(619llvm::StringRef name, llvm::StringRef description,620CallbackType create_callback,621ObjectFileCreateMemoryInstance create_memory_callback,622ObjectFileGetModuleSpecifications get_module_specifications,623ObjectFileSaveCore save_core,624DebuggerInitializeCallback debugger_init_callback)625: PluginInstance<ObjectFileCreateInstance>(626name, description, create_callback, debugger_init_callback),627create_memory_callback(create_memory_callback),628get_module_specifications(get_module_specifications),629save_core(save_core) {}630
631ObjectFileCreateMemoryInstance create_memory_callback;632ObjectFileGetModuleSpecifications get_module_specifications;633ObjectFileSaveCore save_core;634};635typedef PluginInstances<ObjectFileInstance> ObjectFileInstances;636
637static ObjectFileInstances &GetObjectFileInstances() {638static ObjectFileInstances g_instances;639return g_instances;640}
641
642bool PluginManager::RegisterPlugin(643llvm::StringRef name, llvm::StringRef description,644ObjectFileCreateInstance create_callback,645ObjectFileCreateMemoryInstance create_memory_callback,646ObjectFileGetModuleSpecifications get_module_specifications,647ObjectFileSaveCore save_core,648DebuggerInitializeCallback debugger_init_callback) {649return GetObjectFileInstances().RegisterPlugin(650name, description, create_callback, create_memory_callback,651get_module_specifications, save_core, debugger_init_callback);652}
653
654bool PluginManager::UnregisterPlugin(ObjectFileCreateInstance create_callback) {655return GetObjectFileInstances().UnregisterPlugin(create_callback);656}
657
658ObjectFileCreateInstance
659PluginManager::GetObjectFileCreateCallbackAtIndex(uint32_t idx) {660return GetObjectFileInstances().GetCallbackAtIndex(idx);661}
662
663ObjectFileCreateMemoryInstance
664PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(uint32_t idx) {665const auto &instances = GetObjectFileInstances().GetInstances();666if (idx < instances.size())667return instances[idx].create_memory_callback;668return nullptr;669}
670
671ObjectFileGetModuleSpecifications
672PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(673uint32_t idx) {674const auto &instances = GetObjectFileInstances().GetInstances();675if (idx < instances.size())676return instances[idx].get_module_specifications;677return nullptr;678}
679
680ObjectFileCreateMemoryInstance
681PluginManager::GetObjectFileCreateMemoryCallbackForPluginName(682llvm::StringRef name) {683const auto &instances = GetObjectFileInstances().GetInstances();684for (auto &instance : instances) {685if (instance.name == name)686return instance.create_memory_callback;687}688return nullptr;689}
690
691Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,692const FileSpec &outfile,693lldb::SaveCoreStyle &core_style,694llvm::StringRef plugin_name) {695if (plugin_name.empty()) {696// Try saving core directly from the process plugin first.697llvm::Expected<bool> ret = process_sp->SaveCore(outfile.GetPath());698if (!ret)699return Status(ret.takeError());700if (ret.get())701return Status();702}703
704// Fall back to object plugins.705Status error;706auto &instances = GetObjectFileInstances().GetInstances();707for (auto &instance : instances) {708if (plugin_name.empty() || instance.name == plugin_name) {709if (instance.save_core &&710instance.save_core(process_sp, outfile, core_style, error))711return error;712}713}714error.SetErrorString(715"no ObjectFile plugins were able to save a core for this process");716return error;717}
718
719#pragma mark ObjectContainer720
721struct ObjectContainerInstance722: public PluginInstance<ObjectContainerCreateInstance> {723ObjectContainerInstance(724llvm::StringRef name, llvm::StringRef description,725CallbackType create_callback,726ObjectContainerCreateMemoryInstance create_memory_callback,727ObjectFileGetModuleSpecifications get_module_specifications)728: PluginInstance<ObjectContainerCreateInstance>(name, description,729create_callback),730create_memory_callback(create_memory_callback),731get_module_specifications(get_module_specifications) {}732
733ObjectContainerCreateMemoryInstance create_memory_callback;734ObjectFileGetModuleSpecifications get_module_specifications;735};736typedef PluginInstances<ObjectContainerInstance> ObjectContainerInstances;737
738static ObjectContainerInstances &GetObjectContainerInstances() {739static ObjectContainerInstances g_instances;740return g_instances;741}
742
743bool PluginManager::RegisterPlugin(744llvm::StringRef name, llvm::StringRef description,745ObjectContainerCreateInstance create_callback,746ObjectFileGetModuleSpecifications get_module_specifications,747ObjectContainerCreateMemoryInstance create_memory_callback) {748return GetObjectContainerInstances().RegisterPlugin(749name, description, create_callback, create_memory_callback,750get_module_specifications);751}
752
753bool PluginManager::UnregisterPlugin(754ObjectContainerCreateInstance create_callback) {755return GetObjectContainerInstances().UnregisterPlugin(create_callback);756}
757
758ObjectContainerCreateInstance
759PluginManager::GetObjectContainerCreateCallbackAtIndex(uint32_t idx) {760return GetObjectContainerInstances().GetCallbackAtIndex(idx);761}
762
763ObjectContainerCreateMemoryInstance
764PluginManager::GetObjectContainerCreateMemoryCallbackAtIndex(uint32_t idx) {765const auto &instances = GetObjectContainerInstances().GetInstances();766if (idx < instances.size())767return instances[idx].create_memory_callback;768return nullptr;769}
770
771ObjectFileGetModuleSpecifications
772PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(773uint32_t idx) {774const auto &instances = GetObjectContainerInstances().GetInstances();775if (idx < instances.size())776return instances[idx].get_module_specifications;777return nullptr;778}
779
780#pragma mark Platform781
782typedef PluginInstance<PlatformCreateInstance> PlatformInstance;783typedef PluginInstances<PlatformInstance> PlatformInstances;784
785static PlatformInstances &GetPlatformInstances() {786static PlatformInstances g_platform_instances;787return g_platform_instances;788}
789
790bool PluginManager::RegisterPlugin(791llvm::StringRef name, llvm::StringRef description,792PlatformCreateInstance create_callback,793DebuggerInitializeCallback debugger_init_callback) {794return GetPlatformInstances().RegisterPlugin(795name, description, create_callback, debugger_init_callback);796}
797
798bool PluginManager::UnregisterPlugin(PlatformCreateInstance create_callback) {799return GetPlatformInstances().UnregisterPlugin(create_callback);800}
801
802llvm::StringRef PluginManager::GetPlatformPluginNameAtIndex(uint32_t idx) {803return GetPlatformInstances().GetNameAtIndex(idx);804}
805
806llvm::StringRef807PluginManager::GetPlatformPluginDescriptionAtIndex(uint32_t idx) {808return GetPlatformInstances().GetDescriptionAtIndex(idx);809}
810
811PlatformCreateInstance
812PluginManager::GetPlatformCreateCallbackAtIndex(uint32_t idx) {813return GetPlatformInstances().GetCallbackAtIndex(idx);814}
815
816PlatformCreateInstance
817PluginManager::GetPlatformCreateCallbackForPluginName(llvm::StringRef name) {818return GetPlatformInstances().GetCallbackForName(name);819}
820
821void PluginManager::AutoCompletePlatformName(llvm::StringRef name,822CompletionRequest &request) {823for (const auto &instance : GetPlatformInstances().GetInstances()) {824if (instance.name.starts_with(name))825request.AddCompletion(instance.name);826}827}
828
829#pragma mark Process830
831typedef PluginInstance<ProcessCreateInstance> ProcessInstance;832typedef PluginInstances<ProcessInstance> ProcessInstances;833
834static ProcessInstances &GetProcessInstances() {835static ProcessInstances g_instances;836return g_instances;837}
838
839bool PluginManager::RegisterPlugin(840llvm::StringRef name, llvm::StringRef description,841ProcessCreateInstance create_callback,842DebuggerInitializeCallback debugger_init_callback) {843return GetProcessInstances().RegisterPlugin(844name, description, create_callback, debugger_init_callback);845}
846
847bool PluginManager::UnregisterPlugin(ProcessCreateInstance create_callback) {848return GetProcessInstances().UnregisterPlugin(create_callback);849}
850
851llvm::StringRef PluginManager::GetProcessPluginNameAtIndex(uint32_t idx) {852return GetProcessInstances().GetNameAtIndex(idx);853}
854
855llvm::StringRef PluginManager::GetProcessPluginDescriptionAtIndex(uint32_t idx) {856return GetProcessInstances().GetDescriptionAtIndex(idx);857}
858
859ProcessCreateInstance
860PluginManager::GetProcessCreateCallbackAtIndex(uint32_t idx) {861return GetProcessInstances().GetCallbackAtIndex(idx);862}
863
864ProcessCreateInstance
865PluginManager::GetProcessCreateCallbackForPluginName(llvm::StringRef name) {866return GetProcessInstances().GetCallbackForName(name);867}
868
869void PluginManager::AutoCompleteProcessName(llvm::StringRef name,870CompletionRequest &request) {871for (const auto &instance : GetProcessInstances().GetInstances()) {872if (instance.name.starts_with(name))873request.AddCompletion(instance.name, instance.description);874}875}
876
877#pragma mark RegisterTypeBuilder878
879struct RegisterTypeBuilderInstance880: public PluginInstance<RegisterTypeBuilderCreateInstance> {881RegisterTypeBuilderInstance(llvm::StringRef name, llvm::StringRef description,882CallbackType create_callback)883: PluginInstance<RegisterTypeBuilderCreateInstance>(name, description,884create_callback) {}885};886
887typedef PluginInstances<RegisterTypeBuilderInstance>888RegisterTypeBuilderInstances;889
890static RegisterTypeBuilderInstances &GetRegisterTypeBuilderInstances() {891static RegisterTypeBuilderInstances g_instances;892return g_instances;893}
894
895bool PluginManager::RegisterPlugin(896llvm::StringRef name, llvm::StringRef description,897RegisterTypeBuilderCreateInstance create_callback) {898return GetRegisterTypeBuilderInstances().RegisterPlugin(name, description,899create_callback);900}
901
902bool PluginManager::UnregisterPlugin(903RegisterTypeBuilderCreateInstance create_callback) {904return GetRegisterTypeBuilderInstances().UnregisterPlugin(create_callback);905}
906
907lldb::RegisterTypeBuilderSP908PluginManager::GetRegisterTypeBuilder(Target &target) {909const auto &instances = GetRegisterTypeBuilderInstances().GetInstances();910// We assume that RegisterTypeBuilderClang is the only instance of this plugin911// type and is always present.912assert(instances.size());913return instances[0].create_callback(target);914}
915
916#pragma mark ScriptInterpreter917
918struct ScriptInterpreterInstance919: public PluginInstance<ScriptInterpreterCreateInstance> {920ScriptInterpreterInstance(llvm::StringRef name, llvm::StringRef description,921CallbackType create_callback,922lldb::ScriptLanguage language)923: PluginInstance<ScriptInterpreterCreateInstance>(name, description,924create_callback),925language(language) {}926
927lldb::ScriptLanguage language = lldb::eScriptLanguageNone;928};929
930typedef PluginInstances<ScriptInterpreterInstance> ScriptInterpreterInstances;931
932static ScriptInterpreterInstances &GetScriptInterpreterInstances() {933static ScriptInterpreterInstances g_instances;934return g_instances;935}
936
937bool PluginManager::RegisterPlugin(938llvm::StringRef name, llvm::StringRef description,939lldb::ScriptLanguage script_language,940ScriptInterpreterCreateInstance create_callback) {941return GetScriptInterpreterInstances().RegisterPlugin(942name, description, create_callback, script_language);943}
944
945bool PluginManager::UnregisterPlugin(946ScriptInterpreterCreateInstance create_callback) {947return GetScriptInterpreterInstances().UnregisterPlugin(create_callback);948}
949
950ScriptInterpreterCreateInstance
951PluginManager::GetScriptInterpreterCreateCallbackAtIndex(uint32_t idx) {952return GetScriptInterpreterInstances().GetCallbackAtIndex(idx);953}
954
955lldb::ScriptInterpreterSP956PluginManager::GetScriptInterpreterForLanguage(lldb::ScriptLanguage script_lang,957Debugger &debugger) {958const auto &instances = GetScriptInterpreterInstances().GetInstances();959ScriptInterpreterCreateInstance none_instance = nullptr;960for (const auto &instance : instances) {961if (instance.language == lldb::eScriptLanguageNone)962none_instance = instance.create_callback;963
964if (script_lang == instance.language)965return instance.create_callback(debugger);966}967
968// If we didn't find one, return the ScriptInterpreter for the null language.969assert(none_instance != nullptr);970return none_instance(debugger);971}
972
973#pragma mark StructuredDataPlugin974
975struct StructuredDataPluginInstance976: public PluginInstance<StructuredDataPluginCreateInstance> {977StructuredDataPluginInstance(978llvm::StringRef name, llvm::StringRef description,979CallbackType create_callback,980DebuggerInitializeCallback debugger_init_callback,981StructuredDataFilterLaunchInfo filter_callback)982: PluginInstance<StructuredDataPluginCreateInstance>(983name, description, create_callback, debugger_init_callback),984filter_callback(filter_callback) {}985
986StructuredDataFilterLaunchInfo filter_callback = nullptr;987};988
989typedef PluginInstances<StructuredDataPluginInstance>990StructuredDataPluginInstances;991
992static StructuredDataPluginInstances &GetStructuredDataPluginInstances() {993static StructuredDataPluginInstances g_instances;994return g_instances;995}
996
997bool PluginManager::RegisterPlugin(998llvm::StringRef name, llvm::StringRef description,999StructuredDataPluginCreateInstance create_callback,1000DebuggerInitializeCallback debugger_init_callback,1001StructuredDataFilterLaunchInfo filter_callback) {1002return GetStructuredDataPluginInstances().RegisterPlugin(1003name, description, create_callback, debugger_init_callback,1004filter_callback);1005}
1006
1007bool PluginManager::UnregisterPlugin(1008StructuredDataPluginCreateInstance create_callback) {1009return GetStructuredDataPluginInstances().UnregisterPlugin(create_callback);1010}
1011
1012StructuredDataPluginCreateInstance
1013PluginManager::GetStructuredDataPluginCreateCallbackAtIndex(uint32_t idx) {1014return GetStructuredDataPluginInstances().GetCallbackAtIndex(idx);1015}
1016
1017StructuredDataFilterLaunchInfo
1018PluginManager::GetStructuredDataFilterCallbackAtIndex(1019uint32_t idx, bool &iteration_complete) {1020const auto &instances = GetStructuredDataPluginInstances().GetInstances();1021if (idx < instances.size()) {1022iteration_complete = false;1023return instances[idx].filter_callback;1024} else {1025iteration_complete = true;1026}1027return nullptr;1028}
1029
1030#pragma mark SymbolFile1031
1032typedef PluginInstance<SymbolFileCreateInstance> SymbolFileInstance;1033typedef PluginInstances<SymbolFileInstance> SymbolFileInstances;1034
1035static SymbolFileInstances &GetSymbolFileInstances() {1036static SymbolFileInstances g_instances;1037return g_instances;1038}
1039
1040bool PluginManager::RegisterPlugin(1041llvm::StringRef name, llvm::StringRef description,1042SymbolFileCreateInstance create_callback,1043DebuggerInitializeCallback debugger_init_callback) {1044return GetSymbolFileInstances().RegisterPlugin(1045name, description, create_callback, debugger_init_callback);1046}
1047
1048bool PluginManager::UnregisterPlugin(SymbolFileCreateInstance create_callback) {1049return GetSymbolFileInstances().UnregisterPlugin(create_callback);1050}
1051
1052SymbolFileCreateInstance
1053PluginManager::GetSymbolFileCreateCallbackAtIndex(uint32_t idx) {1054return GetSymbolFileInstances().GetCallbackAtIndex(idx);1055}
1056
1057#pragma mark SymbolVendor1058
1059typedef PluginInstance<SymbolVendorCreateInstance> SymbolVendorInstance;1060typedef PluginInstances<SymbolVendorInstance> SymbolVendorInstances;1061
1062static SymbolVendorInstances &GetSymbolVendorInstances() {1063static SymbolVendorInstances g_instances;1064return g_instances;1065}
1066
1067bool PluginManager::RegisterPlugin(llvm::StringRef name,1068llvm::StringRef description,1069SymbolVendorCreateInstance create_callback) {1070return GetSymbolVendorInstances().RegisterPlugin(name, description,1071create_callback);1072}
1073
1074bool PluginManager::UnregisterPlugin(1075SymbolVendorCreateInstance create_callback) {1076return GetSymbolVendorInstances().UnregisterPlugin(create_callback);1077}
1078
1079SymbolVendorCreateInstance
1080PluginManager::GetSymbolVendorCreateCallbackAtIndex(uint32_t idx) {1081return GetSymbolVendorInstances().GetCallbackAtIndex(idx);1082}
1083
1084#pragma mark SymbolLocator1085
1086struct SymbolLocatorInstance1087: public PluginInstance<SymbolLocatorCreateInstance> {1088SymbolLocatorInstance(1089llvm::StringRef name, llvm::StringRef description,1090CallbackType create_callback,1091SymbolLocatorLocateExecutableObjectFile locate_executable_object_file,1092SymbolLocatorLocateExecutableSymbolFile locate_executable_symbol_file,1093SymbolLocatorDownloadObjectAndSymbolFile download_object_symbol_file,1094SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle,1095DebuggerInitializeCallback debugger_init_callback)1096: PluginInstance<SymbolLocatorCreateInstance>(1097name, description, create_callback, debugger_init_callback),1098locate_executable_object_file(locate_executable_object_file),1099locate_executable_symbol_file(locate_executable_symbol_file),1100download_object_symbol_file(download_object_symbol_file),1101find_symbol_file_in_bundle(find_symbol_file_in_bundle) {}1102
1103SymbolLocatorLocateExecutableObjectFile locate_executable_object_file;1104SymbolLocatorLocateExecutableSymbolFile locate_executable_symbol_file;1105SymbolLocatorDownloadObjectAndSymbolFile download_object_symbol_file;1106SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle;1107};1108typedef PluginInstances<SymbolLocatorInstance> SymbolLocatorInstances;1109
1110static SymbolLocatorInstances &GetSymbolLocatorInstances() {1111static SymbolLocatorInstances g_instances;1112return g_instances;1113}
1114
1115bool PluginManager::RegisterPlugin(1116llvm::StringRef name, llvm::StringRef description,1117SymbolLocatorCreateInstance create_callback,1118SymbolLocatorLocateExecutableObjectFile locate_executable_object_file,1119SymbolLocatorLocateExecutableSymbolFile locate_executable_symbol_file,1120SymbolLocatorDownloadObjectAndSymbolFile download_object_symbol_file,1121SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle,1122DebuggerInitializeCallback debugger_init_callback) {1123return GetSymbolLocatorInstances().RegisterPlugin(1124name, description, create_callback, locate_executable_object_file,1125locate_executable_symbol_file, download_object_symbol_file,1126find_symbol_file_in_bundle, debugger_init_callback);1127}
1128
1129bool PluginManager::UnregisterPlugin(1130SymbolLocatorCreateInstance create_callback) {1131return GetSymbolLocatorInstances().UnregisterPlugin(create_callback);1132}
1133
1134SymbolLocatorCreateInstance
1135PluginManager::GetSymbolLocatorCreateCallbackAtIndex(uint32_t idx) {1136return GetSymbolLocatorInstances().GetCallbackAtIndex(idx);1137}
1138
1139ModuleSpec
1140PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) {1141auto &instances = GetSymbolLocatorInstances().GetInstances();1142for (auto &instance : instances) {1143if (instance.locate_executable_object_file) {1144std::optional<ModuleSpec> result =1145instance.locate_executable_object_file(module_spec);1146if (result)1147return *result;1148}1149}1150return {};1151}
1152
1153FileSpec PluginManager::LocateExecutableSymbolFile(1154const ModuleSpec &module_spec, const FileSpecList &default_search_paths) {1155auto &instances = GetSymbolLocatorInstances().GetInstances();1156for (auto &instance : instances) {1157if (instance.locate_executable_symbol_file) {1158std::optional<FileSpec> result = instance.locate_executable_symbol_file(1159module_spec, default_search_paths);1160if (result)1161return *result;1162}1163}1164return {};1165}
1166
1167bool PluginManager::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,1168Status &error,1169bool force_lookup,1170bool copy_executable) {1171auto &instances = GetSymbolLocatorInstances().GetInstances();1172for (auto &instance : instances) {1173if (instance.download_object_symbol_file) {1174if (instance.download_object_symbol_file(module_spec, error, force_lookup,1175copy_executable))1176return true;1177}1178}1179return false;1180}
1181
1182FileSpec PluginManager::FindSymbolFileInBundle(const FileSpec &symfile_bundle,1183const UUID *uuid,1184const ArchSpec *arch) {1185auto &instances = GetSymbolLocatorInstances().GetInstances();1186for (auto &instance : instances) {1187if (instance.find_symbol_file_in_bundle) {1188std::optional<FileSpec> result =1189instance.find_symbol_file_in_bundle(symfile_bundle, uuid, arch);1190if (result)1191return *result;1192}1193}1194return {};1195}
1196
1197#pragma mark Trace1198
1199struct TraceInstance1200: public PluginInstance<TraceCreateInstanceFromBundle> {1201TraceInstance(1202llvm::StringRef name, llvm::StringRef description,1203CallbackType create_callback_from_bundle,1204TraceCreateInstanceForLiveProcess create_callback_for_live_process,1205llvm::StringRef schema, DebuggerInitializeCallback debugger_init_callback)1206: PluginInstance<TraceCreateInstanceFromBundle>(1207name, description, create_callback_from_bundle,1208debugger_init_callback),1209schema(schema),1210create_callback_for_live_process(create_callback_for_live_process) {}1211
1212llvm::StringRef schema;1213TraceCreateInstanceForLiveProcess create_callback_for_live_process;1214};1215
1216typedef PluginInstances<TraceInstance> TraceInstances;1217
1218static TraceInstances &GetTracePluginInstances() {1219static TraceInstances g_instances;1220return g_instances;1221}
1222
1223bool PluginManager::RegisterPlugin(1224llvm::StringRef name, llvm::StringRef description,1225TraceCreateInstanceFromBundle create_callback_from_bundle,1226TraceCreateInstanceForLiveProcess create_callback_for_live_process,1227llvm::StringRef schema, DebuggerInitializeCallback debugger_init_callback) {1228return GetTracePluginInstances().RegisterPlugin(1229name, description, create_callback_from_bundle,1230create_callback_for_live_process, schema, debugger_init_callback);1231}
1232
1233bool PluginManager::UnregisterPlugin(1234TraceCreateInstanceFromBundle create_callback_from_bundle) {1235return GetTracePluginInstances().UnregisterPlugin(1236create_callback_from_bundle);1237}
1238
1239TraceCreateInstanceFromBundle
1240PluginManager::GetTraceCreateCallback(llvm::StringRef plugin_name) {1241return GetTracePluginInstances().GetCallbackForName(plugin_name);1242}
1243
1244TraceCreateInstanceForLiveProcess
1245PluginManager::GetTraceCreateCallbackForLiveProcess(llvm::StringRef plugin_name) {1246for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())1247if (instance.name == plugin_name)1248return instance.create_callback_for_live_process;1249return nullptr;1250}
1251
1252llvm::StringRef PluginManager::GetTraceSchema(llvm::StringRef plugin_name) {1253for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())1254if (instance.name == plugin_name)1255return instance.schema;1256return llvm::StringRef();1257}
1258
1259llvm::StringRef PluginManager::GetTraceSchema(size_t index) {1260if (TraceInstance *instance =1261GetTracePluginInstances().GetInstanceAtIndex(index))1262return instance->schema;1263return llvm::StringRef();1264}
1265
1266#pragma mark TraceExporter1267
1268struct TraceExporterInstance1269: public PluginInstance<TraceExporterCreateInstance> {1270TraceExporterInstance(1271llvm::StringRef name, llvm::StringRef description,1272TraceExporterCreateInstance create_instance,1273ThreadTraceExportCommandCreator create_thread_trace_export_command)1274: PluginInstance<TraceExporterCreateInstance>(name, description,1275create_instance),1276create_thread_trace_export_command(create_thread_trace_export_command) {1277}1278
1279ThreadTraceExportCommandCreator create_thread_trace_export_command;1280};1281
1282typedef PluginInstances<TraceExporterInstance> TraceExporterInstances;1283
1284static TraceExporterInstances &GetTraceExporterInstances() {1285static TraceExporterInstances g_instances;1286return g_instances;1287}
1288
1289bool PluginManager::RegisterPlugin(1290llvm::StringRef name, llvm::StringRef description,1291TraceExporterCreateInstance create_callback,1292ThreadTraceExportCommandCreator create_thread_trace_export_command) {1293return GetTraceExporterInstances().RegisterPlugin(1294name, description, create_callback, create_thread_trace_export_command);1295}
1296
1297TraceExporterCreateInstance
1298PluginManager::GetTraceExporterCreateCallback(llvm::StringRef plugin_name) {1299return GetTraceExporterInstances().GetCallbackForName(plugin_name);1300}
1301
1302bool PluginManager::UnregisterPlugin(1303TraceExporterCreateInstance create_callback) {1304return GetTraceExporterInstances().UnregisterPlugin(create_callback);1305}
1306
1307ThreadTraceExportCommandCreator
1308PluginManager::GetThreadTraceExportCommandCreatorAtIndex(uint32_t index) {1309if (TraceExporterInstance *instance =1310GetTraceExporterInstances().GetInstanceAtIndex(index))1311return instance->create_thread_trace_export_command;1312return nullptr;1313}
1314
1315llvm::StringRef1316PluginManager::GetTraceExporterPluginNameAtIndex(uint32_t index) {1317return GetTraceExporterInstances().GetNameAtIndex(index);1318}
1319
1320#pragma mark UnwindAssembly1321
1322typedef PluginInstance<UnwindAssemblyCreateInstance> UnwindAssemblyInstance;1323typedef PluginInstances<UnwindAssemblyInstance> UnwindAssemblyInstances;1324
1325static UnwindAssemblyInstances &GetUnwindAssemblyInstances() {1326static UnwindAssemblyInstances g_instances;1327return g_instances;1328}
1329
1330bool PluginManager::RegisterPlugin(1331llvm::StringRef name, llvm::StringRef description,1332UnwindAssemblyCreateInstance create_callback) {1333return GetUnwindAssemblyInstances().RegisterPlugin(name, description,1334create_callback);1335}
1336
1337bool PluginManager::UnregisterPlugin(1338UnwindAssemblyCreateInstance create_callback) {1339return GetUnwindAssemblyInstances().UnregisterPlugin(create_callback);1340}
1341
1342UnwindAssemblyCreateInstance
1343PluginManager::GetUnwindAssemblyCreateCallbackAtIndex(uint32_t idx) {1344return GetUnwindAssemblyInstances().GetCallbackAtIndex(idx);1345}
1346
1347#pragma mark MemoryHistory1348
1349typedef PluginInstance<MemoryHistoryCreateInstance> MemoryHistoryInstance;1350typedef PluginInstances<MemoryHistoryInstance> MemoryHistoryInstances;1351
1352static MemoryHistoryInstances &GetMemoryHistoryInstances() {1353static MemoryHistoryInstances g_instances;1354return g_instances;1355}
1356
1357bool PluginManager::RegisterPlugin(1358llvm::StringRef name, llvm::StringRef description,1359MemoryHistoryCreateInstance create_callback) {1360return GetMemoryHistoryInstances().RegisterPlugin(name, description,1361create_callback);1362}
1363
1364bool PluginManager::UnregisterPlugin(1365MemoryHistoryCreateInstance create_callback) {1366return GetMemoryHistoryInstances().UnregisterPlugin(create_callback);1367}
1368
1369MemoryHistoryCreateInstance
1370PluginManager::GetMemoryHistoryCreateCallbackAtIndex(uint32_t idx) {1371return GetMemoryHistoryInstances().GetCallbackAtIndex(idx);1372}
1373
1374#pragma mark InstrumentationRuntime1375
1376struct InstrumentationRuntimeInstance1377: public PluginInstance<InstrumentationRuntimeCreateInstance> {1378InstrumentationRuntimeInstance(1379llvm::StringRef name, llvm::StringRef description,1380CallbackType create_callback,1381InstrumentationRuntimeGetType get_type_callback)1382: PluginInstance<InstrumentationRuntimeCreateInstance>(name, description,1383create_callback),1384get_type_callback(get_type_callback) {}1385
1386InstrumentationRuntimeGetType get_type_callback = nullptr;1387};1388
1389typedef PluginInstances<InstrumentationRuntimeInstance>1390InstrumentationRuntimeInstances;1391
1392static InstrumentationRuntimeInstances &GetInstrumentationRuntimeInstances() {1393static InstrumentationRuntimeInstances g_instances;1394return g_instances;1395}
1396
1397bool PluginManager::RegisterPlugin(1398llvm::StringRef name, llvm::StringRef description,1399InstrumentationRuntimeCreateInstance create_callback,1400InstrumentationRuntimeGetType get_type_callback) {1401return GetInstrumentationRuntimeInstances().RegisterPlugin(1402name, description, create_callback, get_type_callback);1403}
1404
1405bool PluginManager::UnregisterPlugin(1406InstrumentationRuntimeCreateInstance create_callback) {1407return GetInstrumentationRuntimeInstances().UnregisterPlugin(create_callback);1408}
1409
1410InstrumentationRuntimeGetType
1411PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(uint32_t idx) {1412const auto &instances = GetInstrumentationRuntimeInstances().GetInstances();1413if (idx < instances.size())1414return instances[idx].get_type_callback;1415return nullptr;1416}
1417
1418InstrumentationRuntimeCreateInstance
1419PluginManager::GetInstrumentationRuntimeCreateCallbackAtIndex(uint32_t idx) {1420return GetInstrumentationRuntimeInstances().GetCallbackAtIndex(idx);1421}
1422
1423#pragma mark TypeSystem1424
1425struct TypeSystemInstance : public PluginInstance<TypeSystemCreateInstance> {1426TypeSystemInstance(llvm::StringRef name, llvm::StringRef description,1427CallbackType create_callback,1428LanguageSet supported_languages_for_types,1429LanguageSet supported_languages_for_expressions)1430: PluginInstance<TypeSystemCreateInstance>(name, description,1431create_callback),1432supported_languages_for_types(supported_languages_for_types),1433supported_languages_for_expressions(1434supported_languages_for_expressions) {}1435
1436LanguageSet supported_languages_for_types;1437LanguageSet supported_languages_for_expressions;1438};1439
1440typedef PluginInstances<TypeSystemInstance> TypeSystemInstances;1441
1442static TypeSystemInstances &GetTypeSystemInstances() {1443static TypeSystemInstances g_instances;1444return g_instances;1445}
1446
1447bool PluginManager::RegisterPlugin(1448llvm::StringRef name, llvm::StringRef description,1449TypeSystemCreateInstance create_callback,1450LanguageSet supported_languages_for_types,1451LanguageSet supported_languages_for_expressions) {1452return GetTypeSystemInstances().RegisterPlugin(1453name, description, create_callback, supported_languages_for_types,1454supported_languages_for_expressions);1455}
1456
1457bool PluginManager::UnregisterPlugin(TypeSystemCreateInstance create_callback) {1458return GetTypeSystemInstances().UnregisterPlugin(create_callback);1459}
1460
1461TypeSystemCreateInstance
1462PluginManager::GetTypeSystemCreateCallbackAtIndex(uint32_t idx) {1463return GetTypeSystemInstances().GetCallbackAtIndex(idx);1464}
1465
1466LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForTypes() {1467const auto &instances = GetTypeSystemInstances().GetInstances();1468LanguageSet all;1469for (unsigned i = 0; i < instances.size(); ++i)1470all.bitvector |= instances[i].supported_languages_for_types.bitvector;1471return all;1472}
1473
1474LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForExpressions() {1475const auto &instances = GetTypeSystemInstances().GetInstances();1476LanguageSet all;1477for (unsigned i = 0; i < instances.size(); ++i)1478all.bitvector |= instances[i].supported_languages_for_expressions.bitvector;1479return all;1480}
1481
1482#pragma mark REPL1483
1484struct REPLInstance : public PluginInstance<REPLCreateInstance> {1485REPLInstance(llvm::StringRef name, llvm::StringRef description,1486CallbackType create_callback, LanguageSet supported_languages)1487: PluginInstance<REPLCreateInstance>(name, description, create_callback),1488supported_languages(supported_languages) {}1489
1490LanguageSet supported_languages;1491};1492
1493typedef PluginInstances<REPLInstance> REPLInstances;1494
1495static REPLInstances &GetREPLInstances() {1496static REPLInstances g_instances;1497return g_instances;1498}
1499
1500bool PluginManager::RegisterPlugin(llvm::StringRef name, llvm::StringRef description,1501REPLCreateInstance create_callback,1502LanguageSet supported_languages) {1503return GetREPLInstances().RegisterPlugin(name, description, create_callback,1504supported_languages);1505}
1506
1507bool PluginManager::UnregisterPlugin(REPLCreateInstance create_callback) {1508return GetREPLInstances().UnregisterPlugin(create_callback);1509}
1510
1511REPLCreateInstance PluginManager::GetREPLCreateCallbackAtIndex(uint32_t idx) {1512return GetREPLInstances().GetCallbackAtIndex(idx);1513}
1514
1515LanguageSet PluginManager::GetREPLSupportedLanguagesAtIndex(uint32_t idx) {1516const auto &instances = GetREPLInstances().GetInstances();1517return idx < instances.size() ? instances[idx].supported_languages1518: LanguageSet();1519}
1520
1521LanguageSet PluginManager::GetREPLAllTypeSystemSupportedLanguages() {1522const auto &instances = GetREPLInstances().GetInstances();1523LanguageSet all;1524for (unsigned i = 0; i < instances.size(); ++i)1525all.bitvector |= instances[i].supported_languages.bitvector;1526return all;1527}
1528
1529#pragma mark PluginManager1530
1531void PluginManager::DebuggerInitialize(Debugger &debugger) {1532GetDynamicLoaderInstances().PerformDebuggerCallback(debugger);1533GetJITLoaderInstances().PerformDebuggerCallback(debugger);1534GetObjectFileInstances().PerformDebuggerCallback(debugger);1535GetPlatformInstances().PerformDebuggerCallback(debugger);1536GetProcessInstances().PerformDebuggerCallback(debugger);1537GetSymbolFileInstances().PerformDebuggerCallback(debugger);1538GetSymbolLocatorInstances().PerformDebuggerCallback(debugger);1539GetOperatingSystemInstances().PerformDebuggerCallback(debugger);1540GetStructuredDataPluginInstances().PerformDebuggerCallback(debugger);1541GetTracePluginInstances().PerformDebuggerCallback(debugger);1542}
1543
1544// This is the preferred new way to register plugin specific settings. e.g.
1545// This will put a plugin's settings under e.g.
1546// "plugin.<plugin_type_name>.<plugin_type_desc>.SETTINGNAME".
1547static lldb::OptionValuePropertiesSP1548GetDebuggerPropertyForPlugins(Debugger &debugger, llvm::StringRef plugin_type_name,1549llvm::StringRef plugin_type_desc,1550bool can_create) {1551lldb::OptionValuePropertiesSP parent_properties_sp(1552debugger.GetValueProperties());1553if (parent_properties_sp) {1554static constexpr llvm::StringLiteral g_property_name("plugin");1555
1556OptionValuePropertiesSP plugin_properties_sp =1557parent_properties_sp->GetSubProperty(nullptr, g_property_name);1558if (!plugin_properties_sp && can_create) {1559plugin_properties_sp =1560std::make_shared<OptionValueProperties>(g_property_name);1561parent_properties_sp->AppendProperty(g_property_name,1562"Settings specify to plugins.", true,1563plugin_properties_sp);1564}1565
1566if (plugin_properties_sp) {1567lldb::OptionValuePropertiesSP plugin_type_properties_sp =1568plugin_properties_sp->GetSubProperty(nullptr, plugin_type_name);1569if (!plugin_type_properties_sp && can_create) {1570plugin_type_properties_sp =1571std::make_shared<OptionValueProperties>(plugin_type_name);1572plugin_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,1573true, plugin_type_properties_sp);1574}1575return plugin_type_properties_sp;1576}1577}1578return lldb::OptionValuePropertiesSP();1579}
1580
1581// This is deprecated way to register plugin specific settings. e.g.
1582// "<plugin_type_name>.plugin.<plugin_type_desc>.SETTINGNAME" and Platform
1583// generic settings would be under "platform.SETTINGNAME".
1584static lldb::OptionValuePropertiesSP GetDebuggerPropertyForPluginsOldStyle(1585Debugger &debugger, llvm::StringRef plugin_type_name,1586llvm::StringRef plugin_type_desc, bool can_create) {1587static constexpr llvm::StringLiteral g_property_name("plugin");1588lldb::OptionValuePropertiesSP parent_properties_sp(1589debugger.GetValueProperties());1590if (parent_properties_sp) {1591OptionValuePropertiesSP plugin_properties_sp =1592parent_properties_sp->GetSubProperty(nullptr, plugin_type_name);1593if (!plugin_properties_sp && can_create) {1594plugin_properties_sp =1595std::make_shared<OptionValueProperties>(plugin_type_name);1596parent_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,1597true, plugin_properties_sp);1598}1599
1600if (plugin_properties_sp) {1601lldb::OptionValuePropertiesSP plugin_type_properties_sp =1602plugin_properties_sp->GetSubProperty(nullptr, g_property_name);1603if (!plugin_type_properties_sp && can_create) {1604plugin_type_properties_sp =1605std::make_shared<OptionValueProperties>(g_property_name);1606plugin_properties_sp->AppendProperty(g_property_name,1607"Settings specific to plugins",1608true, plugin_type_properties_sp);1609}1610return plugin_type_properties_sp;1611}1612}1613return lldb::OptionValuePropertiesSP();1614}
1615
1616namespace {1617
1618typedef lldb::OptionValuePropertiesSP1619GetDebuggerPropertyForPluginsPtr(Debugger &, llvm::StringRef, llvm::StringRef,1620bool can_create);1621}
1622
1623static lldb::OptionValuePropertiesSP1624GetSettingForPlugin(Debugger &debugger, llvm::StringRef setting_name,1625llvm::StringRef plugin_type_name,1626GetDebuggerPropertyForPluginsPtr get_debugger_property =1627GetDebuggerPropertyForPlugins) {1628lldb::OptionValuePropertiesSP properties_sp;1629lldb::OptionValuePropertiesSP plugin_type_properties_sp(get_debugger_property(1630debugger, plugin_type_name,1631"", // not creating to so we don't need the description1632false));1633if (plugin_type_properties_sp)1634properties_sp =1635plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);1636return properties_sp;1637}
1638
1639static bool1640CreateSettingForPlugin(Debugger &debugger, llvm::StringRef plugin_type_name,1641llvm::StringRef plugin_type_desc,1642const lldb::OptionValuePropertiesSP &properties_sp,1643llvm::StringRef description, bool is_global_property,1644GetDebuggerPropertyForPluginsPtr get_debugger_property =1645GetDebuggerPropertyForPlugins) {1646if (properties_sp) {1647lldb::OptionValuePropertiesSP plugin_type_properties_sp(1648get_debugger_property(debugger, plugin_type_name, plugin_type_desc,1649true));1650if (plugin_type_properties_sp) {1651plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),1652description, is_global_property,1653properties_sp);1654return true;1655}1656}1657return false;1658}
1659
1660static constexpr llvm::StringLiteral kDynamicLoaderPluginName("dynamic-loader");1661static constexpr llvm::StringLiteral kPlatformPluginName("platform");1662static constexpr llvm::StringLiteral kProcessPluginName("process");1663static constexpr llvm::StringLiteral kTracePluginName("trace");1664static constexpr llvm::StringLiteral kObjectFilePluginName("object-file");1665static constexpr llvm::StringLiteral kSymbolFilePluginName("symbol-file");1666static constexpr llvm::StringLiteral kSymbolLocatorPluginName("symbol-locator");1667static constexpr llvm::StringLiteral kJITLoaderPluginName("jit-loader");1668static constexpr llvm::StringLiteral1669kStructuredDataPluginName("structured-data");1670
1671lldb::OptionValuePropertiesSP1672PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,1673llvm::StringRef setting_name) {1674return GetSettingForPlugin(debugger, setting_name, kDynamicLoaderPluginName);1675}
1676
1677bool PluginManager::CreateSettingForDynamicLoaderPlugin(1678Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1679llvm::StringRef description, bool is_global_property) {1680return CreateSettingForPlugin(debugger, kDynamicLoaderPluginName,1681"Settings for dynamic loader plug-ins",1682properties_sp, description, is_global_property);1683}
1684
1685lldb::OptionValuePropertiesSP1686PluginManager::GetSettingForPlatformPlugin(Debugger &debugger,1687llvm::StringRef setting_name) {1688return GetSettingForPlugin(debugger, setting_name, kPlatformPluginName,1689GetDebuggerPropertyForPluginsOldStyle);1690}
1691
1692bool PluginManager::CreateSettingForPlatformPlugin(1693Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1694llvm::StringRef description, bool is_global_property) {1695return CreateSettingForPlugin(debugger, kPlatformPluginName,1696"Settings for platform plug-ins", properties_sp,1697description, is_global_property,1698GetDebuggerPropertyForPluginsOldStyle);1699}
1700
1701lldb::OptionValuePropertiesSP1702PluginManager::GetSettingForProcessPlugin(Debugger &debugger,1703llvm::StringRef setting_name) {1704return GetSettingForPlugin(debugger, setting_name, kProcessPluginName);1705}
1706
1707bool PluginManager::CreateSettingForProcessPlugin(1708Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1709llvm::StringRef description, bool is_global_property) {1710return CreateSettingForPlugin(debugger, kProcessPluginName,1711"Settings for process plug-ins", properties_sp,1712description, is_global_property);1713}
1714
1715lldb::OptionValuePropertiesSP1716PluginManager::GetSettingForSymbolLocatorPlugin(Debugger &debugger,1717llvm::StringRef setting_name) {1718return GetSettingForPlugin(debugger, setting_name, kSymbolLocatorPluginName);1719}
1720
1721bool PluginManager::CreateSettingForSymbolLocatorPlugin(1722Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1723llvm::StringRef description, bool is_global_property) {1724return CreateSettingForPlugin(debugger, kSymbolLocatorPluginName,1725"Settings for symbol locator plug-ins",1726properties_sp, description, is_global_property);1727}
1728
1729bool PluginManager::CreateSettingForTracePlugin(1730Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1731llvm::StringRef description, bool is_global_property) {1732return CreateSettingForPlugin(debugger, kTracePluginName,1733"Settings for trace plug-ins", properties_sp,1734description, is_global_property);1735}
1736
1737lldb::OptionValuePropertiesSP1738PluginManager::GetSettingForObjectFilePlugin(Debugger &debugger,1739llvm::StringRef setting_name) {1740return GetSettingForPlugin(debugger, setting_name, kObjectFilePluginName);1741}
1742
1743bool PluginManager::CreateSettingForObjectFilePlugin(1744Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1745llvm::StringRef description, bool is_global_property) {1746return CreateSettingForPlugin(debugger, kObjectFilePluginName,1747"Settings for object file plug-ins",1748properties_sp, description, is_global_property);1749}
1750
1751lldb::OptionValuePropertiesSP1752PluginManager::GetSettingForSymbolFilePlugin(Debugger &debugger,1753llvm::StringRef setting_name) {1754return GetSettingForPlugin(debugger, setting_name, kSymbolFilePluginName);1755}
1756
1757bool PluginManager::CreateSettingForSymbolFilePlugin(1758Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1759llvm::StringRef description, bool is_global_property) {1760return CreateSettingForPlugin(debugger, kSymbolFilePluginName,1761"Settings for symbol file plug-ins",1762properties_sp, description, is_global_property);1763}
1764
1765lldb::OptionValuePropertiesSP1766PluginManager::GetSettingForJITLoaderPlugin(Debugger &debugger,1767llvm::StringRef setting_name) {1768return GetSettingForPlugin(debugger, setting_name, kJITLoaderPluginName);1769}
1770
1771bool PluginManager::CreateSettingForJITLoaderPlugin(1772Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1773llvm::StringRef description, bool is_global_property) {1774return CreateSettingForPlugin(debugger, kJITLoaderPluginName,1775"Settings for JIT loader plug-ins",1776properties_sp, description, is_global_property);1777}
1778
1779static const char *kOperatingSystemPluginName("os");1780
1781lldb::OptionValuePropertiesSP1782PluginManager::GetSettingForOperatingSystemPlugin(Debugger &debugger,1783llvm::StringRef setting_name) {1784lldb::OptionValuePropertiesSP properties_sp;1785lldb::OptionValuePropertiesSP plugin_type_properties_sp(1786GetDebuggerPropertyForPlugins(1787debugger, kOperatingSystemPluginName,1788"", // not creating to so we don't need the description1789false));1790if (plugin_type_properties_sp)1791properties_sp =1792plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);1793return properties_sp;1794}
1795
1796bool PluginManager::CreateSettingForOperatingSystemPlugin(1797Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1798llvm::StringRef description, bool is_global_property) {1799if (properties_sp) {1800lldb::OptionValuePropertiesSP plugin_type_properties_sp(1801GetDebuggerPropertyForPlugins(debugger, kOperatingSystemPluginName,1802"Settings for operating system plug-ins",1803true));1804if (plugin_type_properties_sp) {1805plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),1806description, is_global_property,1807properties_sp);1808return true;1809}1810}1811return false;1812}
1813
1814lldb::OptionValuePropertiesSP1815PluginManager::GetSettingForStructuredDataPlugin(Debugger &debugger,1816llvm::StringRef setting_name) {1817return GetSettingForPlugin(debugger, setting_name, kStructuredDataPluginName);1818}
1819
1820bool PluginManager::CreateSettingForStructuredDataPlugin(1821Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,1822llvm::StringRef description, bool is_global_property) {1823return CreateSettingForPlugin(debugger, kStructuredDataPluginName,1824"Settings for structured data plug-ins",1825properties_sp, description, is_global_property);1826}
1827