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     ConstString name, const char *description,
834     ProcessCreateInstance create_callback,
835     DebuggerInitializeCallback debugger_init_callback) {
836   return GetProcessInstances().RegisterPlugin(
837       name, description, create_callback, debugger_init_callback);
838 }
839 
840 bool PluginManager::UnregisterPlugin(ProcessCreateInstance create_callback) {
841   return GetProcessInstances().UnregisterPlugin(create_callback);
842 }
843 
844 const char *PluginManager::GetProcessPluginNameAtIndex(uint32_t idx) {
845   return GetProcessInstances().GetNameAtIndex(idx);
846 }
847 
848 const char *PluginManager::GetProcessPluginDescriptionAtIndex(uint32_t idx) {
849   return GetProcessInstances().GetDescriptionAtIndex(idx);
850 }
851 
852 ProcessCreateInstance
853 PluginManager::GetProcessCreateCallbackAtIndex(uint32_t idx) {
854   return GetProcessInstances().GetCallbackAtIndex(idx);
855 }
856 
857 ProcessCreateInstance
858 PluginManager::GetProcessCreateCallbackForPluginName(ConstString name) {
859   return GetProcessInstances().GetCallbackForName(name);
860 }
861 
862 void PluginManager::AutoCompleteProcessName(llvm::StringRef name,
863                                             CompletionRequest &request) {
864   for (const auto &instance : GetProcessInstances().GetInstances()) {
865     if (instance.name.GetStringRef().startswith(name))
866       request.AddCompletion(instance.name.GetCString(), instance.description);
867   }
868 }
869 
870 #pragma mark ScriptInterpreter
871 
872 struct ScriptInterpreterInstance
873     : public PluginInstance<ScriptInterpreterCreateInstance> {
874   ScriptInterpreterInstance(ConstString name, std::string description,
875                             CallbackType create_callback,
876                             lldb::ScriptLanguage language)
877       : PluginInstance<ScriptInterpreterCreateInstance>(
878             name, std::move(description), create_callback),
879         language(language) {}
880 
881   lldb::ScriptLanguage language = lldb::eScriptLanguageNone;
882 };
883 
884 typedef PluginInstances<ScriptInterpreterInstance> ScriptInterpreterInstances;
885 
886 static ScriptInterpreterInstances &GetScriptInterpreterInstances() {
887   static ScriptInterpreterInstances g_instances;
888   return g_instances;
889 }
890 
891 bool PluginManager::RegisterPlugin(
892     ConstString name, const char *description,
893     lldb::ScriptLanguage script_language,
894     ScriptInterpreterCreateInstance create_callback) {
895   return GetScriptInterpreterInstances().RegisterPlugin(
896       name, description, create_callback, script_language);
897 }
898 
899 bool PluginManager::UnregisterPlugin(
900     ScriptInterpreterCreateInstance create_callback) {
901   return GetScriptInterpreterInstances().UnregisterPlugin(create_callback);
902 }
903 
904 ScriptInterpreterCreateInstance
905 PluginManager::GetScriptInterpreterCreateCallbackAtIndex(uint32_t idx) {
906   return GetScriptInterpreterInstances().GetCallbackAtIndex(idx);
907 }
908 
909 lldb::ScriptInterpreterSP
910 PluginManager::GetScriptInterpreterForLanguage(lldb::ScriptLanguage script_lang,
911                                                Debugger &debugger) {
912   const auto &instances = GetScriptInterpreterInstances().GetInstances();
913   ScriptInterpreterCreateInstance none_instance = nullptr;
914   for (const auto &instance : instances) {
915     if (instance.language == lldb::eScriptLanguageNone)
916       none_instance = instance.create_callback;
917 
918     if (script_lang == instance.language)
919       return instance.create_callback(debugger);
920   }
921 
922   // If we didn't find one, return the ScriptInterpreter for the null language.
923   assert(none_instance != nullptr);
924   return none_instance(debugger);
925 }
926 
927 #pragma mark StructuredDataPlugin
928 
929 struct StructuredDataPluginInstance
930     : public PluginInstance<StructuredDataPluginCreateInstance> {
931   StructuredDataPluginInstance(
932       ConstString name, std::string description, CallbackType create_callback,
933       DebuggerInitializeCallback debugger_init_callback,
934       StructuredDataFilterLaunchInfo filter_callback)
935       : PluginInstance<StructuredDataPluginCreateInstance>(
936             name, std::move(description), create_callback,
937             debugger_init_callback),
938         filter_callback(filter_callback) {}
939 
940   StructuredDataFilterLaunchInfo filter_callback = nullptr;
941 };
942 
943 typedef PluginInstances<StructuredDataPluginInstance>
944     StructuredDataPluginInstances;
945 
946 static StructuredDataPluginInstances &GetStructuredDataPluginInstances() {
947   static StructuredDataPluginInstances g_instances;
948   return g_instances;
949 }
950 
951 bool PluginManager::RegisterPlugin(
952     ConstString name, const char *description,
953     StructuredDataPluginCreateInstance create_callback,
954     DebuggerInitializeCallback debugger_init_callback,
955     StructuredDataFilterLaunchInfo filter_callback) {
956   return GetStructuredDataPluginInstances().RegisterPlugin(
957       name, description, create_callback, debugger_init_callback,
958       filter_callback);
959 }
960 
961 bool PluginManager::UnregisterPlugin(
962     StructuredDataPluginCreateInstance create_callback) {
963   return GetStructuredDataPluginInstances().UnregisterPlugin(create_callback);
964 }
965 
966 StructuredDataPluginCreateInstance
967 PluginManager::GetStructuredDataPluginCreateCallbackAtIndex(uint32_t idx) {
968   return GetStructuredDataPluginInstances().GetCallbackAtIndex(idx);
969 }
970 
971 StructuredDataFilterLaunchInfo
972 PluginManager::GetStructuredDataFilterCallbackAtIndex(
973     uint32_t idx, bool &iteration_complete) {
974   const auto &instances = GetStructuredDataPluginInstances().GetInstances();
975   if (idx < instances.size()) {
976     iteration_complete = false;
977     return instances[idx].filter_callback;
978   } else {
979     iteration_complete = true;
980   }
981   return nullptr;
982 }
983 
984 #pragma mark SymbolFile
985 
986 typedef PluginInstance<SymbolFileCreateInstance> SymbolFileInstance;
987 typedef PluginInstances<SymbolFileInstance> SymbolFileInstances;
988 
989 static SymbolFileInstances &GetSymbolFileInstances() {
990   static SymbolFileInstances g_instances;
991   return g_instances;
992 }
993 
994 bool PluginManager::RegisterPlugin(
995     llvm::StringRef name, llvm::StringRef description,
996     SymbolFileCreateInstance create_callback,
997     DebuggerInitializeCallback debugger_init_callback) {
998   return GetSymbolFileInstances().RegisterPlugin(
999       ConstString(name), description.str().c_str(), create_callback,
1000       debugger_init_callback);
1001 }
1002 
1003 bool PluginManager::UnregisterPlugin(SymbolFileCreateInstance create_callback) {
1004   return GetSymbolFileInstances().UnregisterPlugin(create_callback);
1005 }
1006 
1007 SymbolFileCreateInstance
1008 PluginManager::GetSymbolFileCreateCallbackAtIndex(uint32_t idx) {
1009   return GetSymbolFileInstances().GetCallbackAtIndex(idx);
1010 }
1011 
1012 #pragma mark SymbolVendor
1013 
1014 typedef PluginInstance<SymbolVendorCreateInstance> SymbolVendorInstance;
1015 typedef PluginInstances<SymbolVendorInstance> SymbolVendorInstances;
1016 
1017 static SymbolVendorInstances &GetSymbolVendorInstances() {
1018   static SymbolVendorInstances g_instances;
1019   return g_instances;
1020 }
1021 
1022 bool PluginManager::RegisterPlugin(ConstString name, const char *description,
1023                                    SymbolVendorCreateInstance create_callback) {
1024   return GetSymbolVendorInstances().RegisterPlugin(name, description,
1025                                                    create_callback);
1026 }
1027 
1028 bool PluginManager::UnregisterPlugin(
1029     SymbolVendorCreateInstance create_callback) {
1030   return GetSymbolVendorInstances().UnregisterPlugin(create_callback);
1031 }
1032 
1033 SymbolVendorCreateInstance
1034 PluginManager::GetSymbolVendorCreateCallbackAtIndex(uint32_t idx) {
1035   return GetSymbolVendorInstances().GetCallbackAtIndex(idx);
1036 }
1037 
1038 #pragma mark Trace
1039 
1040 struct TraceInstance
1041     : public PluginInstance<TraceCreateInstanceForSessionFile> {
1042   TraceInstance(
1043       ConstString name, std::string description,
1044       CallbackType create_callback_for_session_file,
1045       TraceCreateInstanceForLiveProcess create_callback_for_live_process,
1046       llvm::StringRef schema)
1047       : PluginInstance<TraceCreateInstanceForSessionFile>(
1048             name, std::move(description), create_callback_for_session_file),
1049         schema(schema),
1050         create_callback_for_live_process(create_callback_for_live_process) {}
1051 
1052   llvm::StringRef schema;
1053   TraceCreateInstanceForLiveProcess create_callback_for_live_process;
1054 };
1055 
1056 typedef PluginInstances<TraceInstance> TraceInstances;
1057 
1058 static TraceInstances &GetTracePluginInstances() {
1059   static TraceInstances g_instances;
1060   return g_instances;
1061 }
1062 
1063 bool PluginManager::RegisterPlugin(
1064     ConstString name, const char *description,
1065     TraceCreateInstanceForSessionFile create_callback_for_session_file,
1066     TraceCreateInstanceForLiveProcess create_callback_for_live_process,
1067     llvm::StringRef schema) {
1068   return GetTracePluginInstances().RegisterPlugin(
1069       name, description, create_callback_for_session_file,
1070       create_callback_for_live_process, schema);
1071 }
1072 
1073 bool PluginManager::UnregisterPlugin(
1074     TraceCreateInstanceForSessionFile create_callback_for_session_file) {
1075   return GetTracePluginInstances().UnregisterPlugin(
1076       create_callback_for_session_file);
1077 }
1078 
1079 TraceCreateInstanceForSessionFile
1080 PluginManager::GetTraceCreateCallback(ConstString plugin_name) {
1081   return GetTracePluginInstances().GetCallbackForName(plugin_name);
1082 }
1083 
1084 TraceCreateInstanceForLiveProcess
1085 PluginManager::GetTraceCreateCallbackForLiveProcess(ConstString plugin_name) {
1086   for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1087     if (instance.name == plugin_name)
1088       return instance.create_callback_for_live_process;
1089   return nullptr;
1090 }
1091 
1092 llvm::StringRef PluginManager::GetTraceSchema(ConstString plugin_name) {
1093   for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1094     if (instance.name == plugin_name)
1095       return instance.schema;
1096   return llvm::StringRef();
1097 }
1098 
1099 llvm::StringRef PluginManager::GetTraceSchema(size_t index) {
1100   if (TraceInstance *instance =
1101           GetTracePluginInstances().GetInstanceAtIndex(index))
1102     return instance->schema;
1103   return llvm::StringRef();
1104 }
1105 
1106 #pragma mark TraceExporter
1107 
1108 struct TraceExporterInstance
1109     : public PluginInstance<TraceExporterCreateInstance> {
1110   TraceExporterInstance(
1111       ConstString name, std::string description,
1112       TraceExporterCreateInstance create_instance,
1113       ThreadTraceExportCommandCreator create_thread_trace_export_command)
1114       : PluginInstance<TraceExporterCreateInstance>(
1115             name, std::move(description), create_instance),
1116         create_thread_trace_export_command(create_thread_trace_export_command) {
1117   }
1118 
1119   ThreadTraceExportCommandCreator create_thread_trace_export_command;
1120 };
1121 
1122 typedef PluginInstances<TraceExporterInstance> TraceExporterInstances;
1123 
1124 static TraceExporterInstances &GetTraceExporterInstances() {
1125   static TraceExporterInstances g_instances;
1126   return g_instances;
1127 }
1128 
1129 bool PluginManager::RegisterPlugin(
1130     ConstString name, const char *description,
1131     TraceExporterCreateInstance create_callback,
1132     ThreadTraceExportCommandCreator create_thread_trace_export_command) {
1133   return GetTraceExporterInstances().RegisterPlugin(
1134       name, description, create_callback, create_thread_trace_export_command);
1135 }
1136 
1137 TraceExporterCreateInstance
1138 PluginManager::GetTraceExporterCreateCallback(ConstString plugin_name) {
1139   return GetTraceExporterInstances().GetCallbackForName(plugin_name);
1140 }
1141 
1142 bool PluginManager::UnregisterPlugin(
1143     TraceExporterCreateInstance create_callback) {
1144   return GetTraceExporterInstances().UnregisterPlugin(create_callback);
1145 }
1146 
1147 ThreadTraceExportCommandCreator
1148 PluginManager::GetThreadTraceExportCommandCreatorAtIndex(uint32_t index) {
1149   if (TraceExporterInstance *instance =
1150           GetTraceExporterInstances().GetInstanceAtIndex(index))
1151     return instance->create_thread_trace_export_command;
1152   return nullptr;
1153 }
1154 
1155 const char *PluginManager::GetTraceExporterPluginNameAtIndex(uint32_t index) {
1156   return GetTraceExporterInstances().GetNameAtIndex(index);
1157 }
1158 
1159 #pragma mark UnwindAssembly
1160 
1161 typedef PluginInstance<UnwindAssemblyCreateInstance> UnwindAssemblyInstance;
1162 typedef PluginInstances<UnwindAssemblyInstance> UnwindAssemblyInstances;
1163 
1164 static UnwindAssemblyInstances &GetUnwindAssemblyInstances() {
1165   static UnwindAssemblyInstances g_instances;
1166   return g_instances;
1167 }
1168 
1169 bool PluginManager::RegisterPlugin(
1170     ConstString name, const char *description,
1171     UnwindAssemblyCreateInstance create_callback) {
1172   return GetUnwindAssemblyInstances().RegisterPlugin(name, description,
1173                                                      create_callback);
1174 }
1175 
1176 bool PluginManager::UnregisterPlugin(
1177     UnwindAssemblyCreateInstance create_callback) {
1178   return GetUnwindAssemblyInstances().UnregisterPlugin(create_callback);
1179 }
1180 
1181 UnwindAssemblyCreateInstance
1182 PluginManager::GetUnwindAssemblyCreateCallbackAtIndex(uint32_t idx) {
1183   return GetUnwindAssemblyInstances().GetCallbackAtIndex(idx);
1184 }
1185 
1186 #pragma mark MemoryHistory
1187 
1188 typedef PluginInstance<MemoryHistoryCreateInstance> MemoryHistoryInstance;
1189 typedef PluginInstances<MemoryHistoryInstance> MemoryHistoryInstances;
1190 
1191 static MemoryHistoryInstances &GetMemoryHistoryInstances() {
1192   static MemoryHistoryInstances g_instances;
1193   return g_instances;
1194 }
1195 
1196 bool PluginManager::RegisterPlugin(
1197     ConstString name, const char *description,
1198     MemoryHistoryCreateInstance create_callback) {
1199   return GetMemoryHistoryInstances().RegisterPlugin(name, description,
1200                                                     create_callback);
1201 }
1202 
1203 bool PluginManager::UnregisterPlugin(
1204     MemoryHistoryCreateInstance create_callback) {
1205   return GetMemoryHistoryInstances().UnregisterPlugin(create_callback);
1206 }
1207 
1208 MemoryHistoryCreateInstance
1209 PluginManager::GetMemoryHistoryCreateCallbackAtIndex(uint32_t idx) {
1210   return GetMemoryHistoryInstances().GetCallbackAtIndex(idx);
1211 }
1212 
1213 #pragma mark InstrumentationRuntime
1214 
1215 struct InstrumentationRuntimeInstance
1216     : public PluginInstance<InstrumentationRuntimeCreateInstance> {
1217   InstrumentationRuntimeInstance(
1218       ConstString name, std::string description, CallbackType create_callback,
1219       InstrumentationRuntimeGetType get_type_callback)
1220       : PluginInstance<InstrumentationRuntimeCreateInstance>(
1221             name, std::move(description), create_callback),
1222         get_type_callback(get_type_callback) {}
1223 
1224   InstrumentationRuntimeGetType get_type_callback = nullptr;
1225 };
1226 
1227 typedef PluginInstances<InstrumentationRuntimeInstance>
1228     InstrumentationRuntimeInstances;
1229 
1230 static InstrumentationRuntimeInstances &GetInstrumentationRuntimeInstances() {
1231   static InstrumentationRuntimeInstances g_instances;
1232   return g_instances;
1233 }
1234 
1235 bool PluginManager::RegisterPlugin(
1236     ConstString name, const char *description,
1237     InstrumentationRuntimeCreateInstance create_callback,
1238     InstrumentationRuntimeGetType get_type_callback) {
1239   return GetInstrumentationRuntimeInstances().RegisterPlugin(
1240       name, description, create_callback, get_type_callback);
1241 }
1242 
1243 bool PluginManager::UnregisterPlugin(
1244     InstrumentationRuntimeCreateInstance create_callback) {
1245   return GetInstrumentationRuntimeInstances().UnregisterPlugin(create_callback);
1246 }
1247 
1248 InstrumentationRuntimeGetType
1249 PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(uint32_t idx) {
1250   const auto &instances = GetInstrumentationRuntimeInstances().GetInstances();
1251   if (idx < instances.size())
1252     return instances[idx].get_type_callback;
1253   return nullptr;
1254 }
1255 
1256 InstrumentationRuntimeCreateInstance
1257 PluginManager::GetInstrumentationRuntimeCreateCallbackAtIndex(uint32_t idx) {
1258   return GetInstrumentationRuntimeInstances().GetCallbackAtIndex(idx);
1259 }
1260 
1261 #pragma mark TypeSystem
1262 
1263 struct TypeSystemInstance : public PluginInstance<TypeSystemCreateInstance> {
1264   TypeSystemInstance(ConstString name, std::string description,
1265                      CallbackType create_callback,
1266                      LanguageSet supported_languages_for_types,
1267                      LanguageSet supported_languages_for_expressions)
1268       : PluginInstance<TypeSystemCreateInstance>(name, std::move(description),
1269                                                  create_callback),
1270         supported_languages_for_types(supported_languages_for_types),
1271         supported_languages_for_expressions(
1272             supported_languages_for_expressions) {}
1273 
1274   LanguageSet supported_languages_for_types;
1275   LanguageSet supported_languages_for_expressions;
1276 };
1277 
1278 typedef PluginInstances<TypeSystemInstance> TypeSystemInstances;
1279 
1280 static TypeSystemInstances &GetTypeSystemInstances() {
1281   static TypeSystemInstances g_instances;
1282   return g_instances;
1283 }
1284 
1285 bool PluginManager::RegisterPlugin(
1286     ConstString name, const char *description,
1287     TypeSystemCreateInstance create_callback,
1288     LanguageSet supported_languages_for_types,
1289     LanguageSet supported_languages_for_expressions) {
1290   return GetTypeSystemInstances().RegisterPlugin(
1291       name, description, create_callback, supported_languages_for_types,
1292       supported_languages_for_expressions);
1293 }
1294 
1295 bool PluginManager::UnregisterPlugin(TypeSystemCreateInstance create_callback) {
1296   return GetTypeSystemInstances().UnregisterPlugin(create_callback);
1297 }
1298 
1299 TypeSystemCreateInstance
1300 PluginManager::GetTypeSystemCreateCallbackAtIndex(uint32_t idx) {
1301   return GetTypeSystemInstances().GetCallbackAtIndex(idx);
1302 }
1303 
1304 LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForTypes() {
1305   const auto &instances = GetTypeSystemInstances().GetInstances();
1306   LanguageSet all;
1307   for (unsigned i = 0; i < instances.size(); ++i)
1308     all.bitvector |= instances[i].supported_languages_for_types.bitvector;
1309   return all;
1310 }
1311 
1312 LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForExpressions() {
1313   const auto &instances = GetTypeSystemInstances().GetInstances();
1314   LanguageSet all;
1315   for (unsigned i = 0; i < instances.size(); ++i)
1316     all.bitvector |= instances[i].supported_languages_for_expressions.bitvector;
1317   return all;
1318 }
1319 
1320 #pragma mark REPL
1321 
1322 struct REPLInstance : public PluginInstance<REPLCreateInstance> {
1323   REPLInstance(ConstString name, std::string description,
1324                CallbackType create_callback, LanguageSet supported_languages)
1325       : PluginInstance<REPLCreateInstance>(name, std::move(description),
1326                                            create_callback),
1327         supported_languages(supported_languages) {}
1328 
1329   LanguageSet supported_languages;
1330 };
1331 
1332 typedef PluginInstances<REPLInstance> REPLInstances;
1333 
1334 static REPLInstances &GetREPLInstances() {
1335   static REPLInstances g_instances;
1336   return g_instances;
1337 }
1338 
1339 bool PluginManager::RegisterPlugin(ConstString name, const char *description,
1340                                    REPLCreateInstance create_callback,
1341                                    LanguageSet supported_languages) {
1342   return GetREPLInstances().RegisterPlugin(name, description, create_callback,
1343                                            supported_languages);
1344 }
1345 
1346 bool PluginManager::UnregisterPlugin(REPLCreateInstance create_callback) {
1347   return GetREPLInstances().UnregisterPlugin(create_callback);
1348 }
1349 
1350 REPLCreateInstance PluginManager::GetREPLCreateCallbackAtIndex(uint32_t idx) {
1351   return GetREPLInstances().GetCallbackAtIndex(idx);
1352 }
1353 
1354 LanguageSet PluginManager::GetREPLAllTypeSystemSupportedLanguages() {
1355   const auto &instances = GetREPLInstances().GetInstances();
1356   LanguageSet all;
1357   for (unsigned i = 0; i < instances.size(); ++i)
1358     all.bitvector |= instances[i].supported_languages.bitvector;
1359   return all;
1360 }
1361 
1362 #pragma mark PluginManager
1363 
1364 void PluginManager::DebuggerInitialize(Debugger &debugger) {
1365   GetDynamicLoaderInstances().PerformDebuggerCallback(debugger);
1366   GetJITLoaderInstances().PerformDebuggerCallback(debugger);
1367   GetPlatformInstances().PerformDebuggerCallback(debugger);
1368   GetProcessInstances().PerformDebuggerCallback(debugger);
1369   GetSymbolFileInstances().PerformDebuggerCallback(debugger);
1370   GetOperatingSystemInstances().PerformDebuggerCallback(debugger);
1371   GetStructuredDataPluginInstances().PerformDebuggerCallback(debugger);
1372   GetTracePluginInstances().PerformDebuggerCallback(debugger);
1373 }
1374 
1375 // This is the preferred new way to register plugin specific settings.  e.g.
1376 // This will put a plugin's settings under e.g.
1377 // "plugin.<plugin_type_name>.<plugin_type_desc>.SETTINGNAME".
1378 static lldb::OptionValuePropertiesSP
1379 GetDebuggerPropertyForPlugins(Debugger &debugger, ConstString plugin_type_name,
1380                               ConstString plugin_type_desc, bool can_create) {
1381   lldb::OptionValuePropertiesSP parent_properties_sp(
1382       debugger.GetValueProperties());
1383   if (parent_properties_sp) {
1384     static ConstString g_property_name("plugin");
1385 
1386     OptionValuePropertiesSP plugin_properties_sp =
1387         parent_properties_sp->GetSubProperty(nullptr, g_property_name);
1388     if (!plugin_properties_sp && can_create) {
1389       plugin_properties_sp =
1390           std::make_shared<OptionValueProperties>(g_property_name);
1391       parent_properties_sp->AppendProperty(
1392           g_property_name, ConstString("Settings specify to plugins."), true,
1393           plugin_properties_sp);
1394     }
1395 
1396     if (plugin_properties_sp) {
1397       lldb::OptionValuePropertiesSP plugin_type_properties_sp =
1398           plugin_properties_sp->GetSubProperty(nullptr, plugin_type_name);
1399       if (!plugin_type_properties_sp && can_create) {
1400         plugin_type_properties_sp =
1401             std::make_shared<OptionValueProperties>(plugin_type_name);
1402         plugin_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,
1403                                              true, plugin_type_properties_sp);
1404       }
1405       return plugin_type_properties_sp;
1406     }
1407   }
1408   return lldb::OptionValuePropertiesSP();
1409 }
1410 
1411 // This is deprecated way to register plugin specific settings.  e.g.
1412 // "<plugin_type_name>.plugin.<plugin_type_desc>.SETTINGNAME" and Platform
1413 // generic settings would be under "platform.SETTINGNAME".
1414 static lldb::OptionValuePropertiesSP GetDebuggerPropertyForPluginsOldStyle(
1415     Debugger &debugger, ConstString plugin_type_name,
1416     ConstString plugin_type_desc, bool can_create) {
1417   static ConstString g_property_name("plugin");
1418   lldb::OptionValuePropertiesSP parent_properties_sp(
1419       debugger.GetValueProperties());
1420   if (parent_properties_sp) {
1421     OptionValuePropertiesSP plugin_properties_sp =
1422         parent_properties_sp->GetSubProperty(nullptr, plugin_type_name);
1423     if (!plugin_properties_sp && can_create) {
1424       plugin_properties_sp =
1425           std::make_shared<OptionValueProperties>(plugin_type_name);
1426       parent_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,
1427                                            true, plugin_properties_sp);
1428     }
1429 
1430     if (plugin_properties_sp) {
1431       lldb::OptionValuePropertiesSP plugin_type_properties_sp =
1432           plugin_properties_sp->GetSubProperty(nullptr, g_property_name);
1433       if (!plugin_type_properties_sp && can_create) {
1434         plugin_type_properties_sp =
1435             std::make_shared<OptionValueProperties>(g_property_name);
1436         plugin_properties_sp->AppendProperty(
1437             g_property_name, ConstString("Settings specific to plugins"), true,
1438             plugin_type_properties_sp);
1439       }
1440       return plugin_type_properties_sp;
1441     }
1442   }
1443   return lldb::OptionValuePropertiesSP();
1444 }
1445 
1446 namespace {
1447 
1448 typedef lldb::OptionValuePropertiesSP
1449 GetDebuggerPropertyForPluginsPtr(Debugger &, ConstString, ConstString,
1450                                  bool can_create);
1451 }
1452 
1453 static lldb::OptionValuePropertiesSP
1454 GetSettingForPlugin(Debugger &debugger, ConstString setting_name,
1455                     ConstString plugin_type_name,
1456                     GetDebuggerPropertyForPluginsPtr get_debugger_property =
1457                         GetDebuggerPropertyForPlugins) {
1458   lldb::OptionValuePropertiesSP properties_sp;
1459   lldb::OptionValuePropertiesSP plugin_type_properties_sp(get_debugger_property(
1460       debugger, plugin_type_name,
1461       ConstString(), // not creating to so we don't need the description
1462       false));
1463   if (plugin_type_properties_sp)
1464     properties_sp =
1465         plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);
1466   return properties_sp;
1467 }
1468 
1469 static bool
1470 CreateSettingForPlugin(Debugger &debugger, ConstString plugin_type_name,
1471                        ConstString plugin_type_desc,
1472                        const lldb::OptionValuePropertiesSP &properties_sp,
1473                        ConstString description, bool is_global_property,
1474                        GetDebuggerPropertyForPluginsPtr get_debugger_property =
1475                            GetDebuggerPropertyForPlugins) {
1476   if (properties_sp) {
1477     lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1478         get_debugger_property(debugger, plugin_type_name, plugin_type_desc,
1479                               true));
1480     if (plugin_type_properties_sp) {
1481       plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),
1482                                                 description, is_global_property,
1483                                                 properties_sp);
1484       return true;
1485     }
1486   }
1487   return false;
1488 }
1489 
1490 static const char *kDynamicLoaderPluginName("dynamic-loader");
1491 static const char *kPlatformPluginName("platform");
1492 static const char *kProcessPluginName("process");
1493 static const char *kSymbolFilePluginName("symbol-file");
1494 static const char *kJITLoaderPluginName("jit-loader");
1495 static const char *kStructuredDataPluginName("structured-data");
1496 
1497 lldb::OptionValuePropertiesSP
1498 PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,
1499                                                 ConstString setting_name) {
1500   return GetSettingForPlugin(debugger, setting_name,
1501                              ConstString(kDynamicLoaderPluginName));
1502 }
1503 
1504 bool PluginManager::CreateSettingForDynamicLoaderPlugin(
1505     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1506     ConstString description, bool is_global_property) {
1507   return CreateSettingForPlugin(
1508       debugger, ConstString(kDynamicLoaderPluginName),
1509       ConstString("Settings for dynamic loader plug-ins"), properties_sp,
1510       description, is_global_property);
1511 }
1512 
1513 lldb::OptionValuePropertiesSP
1514 PluginManager::GetSettingForPlatformPlugin(Debugger &debugger,
1515                                            ConstString setting_name) {
1516   return GetSettingForPlugin(debugger, setting_name,
1517                              ConstString(kPlatformPluginName),
1518                              GetDebuggerPropertyForPluginsOldStyle);
1519 }
1520 
1521 bool PluginManager::CreateSettingForPlatformPlugin(
1522     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1523     ConstString description, bool is_global_property) {
1524   return CreateSettingForPlugin(debugger, ConstString(kPlatformPluginName),
1525                                 ConstString("Settings for platform plug-ins"),
1526                                 properties_sp, description, is_global_property,
1527                                 GetDebuggerPropertyForPluginsOldStyle);
1528 }
1529 
1530 lldb::OptionValuePropertiesSP
1531 PluginManager::GetSettingForProcessPlugin(Debugger &debugger,
1532                                           ConstString setting_name) {
1533   return GetSettingForPlugin(debugger, setting_name,
1534                              ConstString(kProcessPluginName));
1535 }
1536 
1537 bool PluginManager::CreateSettingForProcessPlugin(
1538     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1539     ConstString description, bool is_global_property) {
1540   return CreateSettingForPlugin(debugger, ConstString(kProcessPluginName),
1541                                 ConstString("Settings for process plug-ins"),
1542                                 properties_sp, description, is_global_property);
1543 }
1544 
1545 lldb::OptionValuePropertiesSP
1546 PluginManager::GetSettingForSymbolFilePlugin(Debugger &debugger,
1547                                              ConstString setting_name) {
1548   return GetSettingForPlugin(debugger, setting_name,
1549                              ConstString(kSymbolFilePluginName));
1550 }
1551 
1552 bool PluginManager::CreateSettingForSymbolFilePlugin(
1553     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1554     ConstString description, bool is_global_property) {
1555   return CreateSettingForPlugin(
1556       debugger, ConstString(kSymbolFilePluginName),
1557       ConstString("Settings for symbol file plug-ins"), properties_sp,
1558       description, is_global_property);
1559 }
1560 
1561 lldb::OptionValuePropertiesSP
1562 PluginManager::GetSettingForJITLoaderPlugin(Debugger &debugger,
1563                                             ConstString setting_name) {
1564   return GetSettingForPlugin(debugger, setting_name,
1565                              ConstString(kJITLoaderPluginName));
1566 }
1567 
1568 bool PluginManager::CreateSettingForJITLoaderPlugin(
1569     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1570     ConstString description, bool is_global_property) {
1571   return CreateSettingForPlugin(debugger, ConstString(kJITLoaderPluginName),
1572                                 ConstString("Settings for JIT loader plug-ins"),
1573                                 properties_sp, description, is_global_property);
1574 }
1575 
1576 static const char *kOperatingSystemPluginName("os");
1577 
1578 lldb::OptionValuePropertiesSP
1579 PluginManager::GetSettingForOperatingSystemPlugin(Debugger &debugger,
1580                                                   ConstString setting_name) {
1581   lldb::OptionValuePropertiesSP properties_sp;
1582   lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1583       GetDebuggerPropertyForPlugins(
1584           debugger, ConstString(kOperatingSystemPluginName),
1585           ConstString(), // not creating to so we don't need the description
1586           false));
1587   if (plugin_type_properties_sp)
1588     properties_sp =
1589         plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);
1590   return properties_sp;
1591 }
1592 
1593 bool PluginManager::CreateSettingForOperatingSystemPlugin(
1594     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1595     ConstString description, bool is_global_property) {
1596   if (properties_sp) {
1597     lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1598         GetDebuggerPropertyForPlugins(
1599             debugger, ConstString(kOperatingSystemPluginName),
1600             ConstString("Settings for operating system plug-ins"), true));
1601     if (plugin_type_properties_sp) {
1602       plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),
1603                                                 description, is_global_property,
1604                                                 properties_sp);
1605       return true;
1606     }
1607   }
1608   return false;
1609 }
1610 
1611 lldb::OptionValuePropertiesSP
1612 PluginManager::GetSettingForStructuredDataPlugin(Debugger &debugger,
1613                                                  ConstString setting_name) {
1614   return GetSettingForPlugin(debugger, setting_name,
1615                              ConstString(kStructuredDataPluginName));
1616 }
1617 
1618 bool PluginManager::CreateSettingForStructuredDataPlugin(
1619     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1620     ConstString description, bool is_global_property) {
1621   return CreateSettingForPlugin(
1622       debugger, ConstString(kStructuredDataPluginName),
1623       ConstString("Settings for structured data plug-ins"), properties_sp,
1624       description, is_global_property);
1625 }
1626