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