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(ConstString name, const char *description,
518                                    LanguageCreateInstance create_callback) {
519   return GetLanguageInstances().RegisterPlugin(name, description,
520                                                create_callback);
521 }
522 
523 bool PluginManager::UnregisterPlugin(LanguageCreateInstance create_callback) {
524   return GetLanguageInstances().UnregisterPlugin(create_callback);
525 }
526 
527 LanguageCreateInstance
528 PluginManager::GetLanguageCreateCallbackAtIndex(uint32_t idx) {
529   return GetLanguageInstances().GetCallbackAtIndex(idx);
530 }
531 
532 #pragma mark LanguageRuntime
533 
534 struct LanguageRuntimeInstance
535     : public PluginInstance<LanguageRuntimeCreateInstance> {
536   LanguageRuntimeInstance(
537       ConstString name, std::string description, CallbackType create_callback,
538       DebuggerInitializeCallback debugger_init_callback,
539       LanguageRuntimeGetCommandObject command_callback,
540       LanguageRuntimeGetExceptionPrecondition precondition_callback)
541       : PluginInstance<LanguageRuntimeCreateInstance>(
542             name, std::move(description), create_callback,
543             debugger_init_callback),
544         command_callback(command_callback),
545         precondition_callback(precondition_callback) {}
546 
547   LanguageRuntimeGetCommandObject command_callback;
548   LanguageRuntimeGetExceptionPrecondition precondition_callback;
549 };
550 
551 typedef PluginInstances<LanguageRuntimeInstance> LanguageRuntimeInstances;
552 
553 static LanguageRuntimeInstances &GetLanguageRuntimeInstances() {
554   static LanguageRuntimeInstances g_instances;
555   return g_instances;
556 }
557 
558 bool PluginManager::RegisterPlugin(
559     ConstString name, const char *description,
560     LanguageRuntimeCreateInstance create_callback,
561     LanguageRuntimeGetCommandObject command_callback,
562     LanguageRuntimeGetExceptionPrecondition precondition_callback) {
563   return GetLanguageRuntimeInstances().RegisterPlugin(
564       name, description, create_callback, nullptr, command_callback,
565       precondition_callback);
566 }
567 
568 bool PluginManager::UnregisterPlugin(
569     LanguageRuntimeCreateInstance create_callback) {
570   return GetLanguageRuntimeInstances().UnregisterPlugin(create_callback);
571 }
572 
573 LanguageRuntimeCreateInstance
574 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(uint32_t idx) {
575   return GetLanguageRuntimeInstances().GetCallbackAtIndex(idx);
576 }
577 
578 LanguageRuntimeGetCommandObject
579 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(uint32_t idx) {
580   const auto &instances = GetLanguageRuntimeInstances().GetInstances();
581   if (idx < instances.size())
582     return instances[idx].command_callback;
583   return nullptr;
584 }
585 
586 LanguageRuntimeGetExceptionPrecondition
587 PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(uint32_t idx) {
588   const auto &instances = GetLanguageRuntimeInstances().GetInstances();
589   if (idx < instances.size())
590     return instances[idx].precondition_callback;
591   return nullptr;
592 }
593 
594 #pragma mark SystemRuntime
595 
596 typedef PluginInstance<SystemRuntimeCreateInstance> SystemRuntimeInstance;
597 typedef PluginInstances<SystemRuntimeInstance> SystemRuntimeInstances;
598 
599 static SystemRuntimeInstances &GetSystemRuntimeInstances() {
600   static SystemRuntimeInstances g_instances;
601   return g_instances;
602 }
603 
604 bool PluginManager::RegisterPlugin(
605     ConstString name, const char *description,
606     SystemRuntimeCreateInstance create_callback) {
607   return GetSystemRuntimeInstances().RegisterPlugin(name, description,
608                                                     create_callback);
609 }
610 
611 bool PluginManager::UnregisterPlugin(
612     SystemRuntimeCreateInstance create_callback) {
613   return GetSystemRuntimeInstances().UnregisterPlugin(create_callback);
614 }
615 
616 SystemRuntimeCreateInstance
617 PluginManager::GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx) {
618   return GetSystemRuntimeInstances().GetCallbackAtIndex(idx);
619 }
620 
621 #pragma mark ObjectFile
622 
623 struct ObjectFileInstance : public PluginInstance<ObjectFileCreateInstance> {
624   ObjectFileInstance(
625       ConstString name, std::string description, CallbackType create_callback,
626       ObjectFileCreateMemoryInstance create_memory_callback,
627       ObjectFileGetModuleSpecifications get_module_specifications,
628       ObjectFileSaveCore save_core)
629       : PluginInstance<ObjectFileCreateInstance>(name, std::move(description),
630                                                  create_callback),
631         create_memory_callback(create_memory_callback),
632         get_module_specifications(get_module_specifications),
633         save_core(save_core) {}
634 
635   ObjectFileCreateMemoryInstance create_memory_callback;
636   ObjectFileGetModuleSpecifications get_module_specifications;
637   ObjectFileSaveCore save_core;
638 };
639 typedef PluginInstances<ObjectFileInstance> ObjectFileInstances;
640 
641 static ObjectFileInstances &GetObjectFileInstances() {
642   static ObjectFileInstances g_instances;
643   return g_instances;
644 }
645 
646 bool PluginManager::RegisterPlugin(
647     llvm::StringRef name, llvm::StringRef description,
648     ObjectFileCreateInstance create_callback,
649     ObjectFileCreateMemoryInstance create_memory_callback,
650     ObjectFileGetModuleSpecifications get_module_specifications,
651     ObjectFileSaveCore save_core) {
652   return GetObjectFileInstances().RegisterPlugin(
653       ConstString(name), description.str().c_str(), create_callback,
654       create_memory_callback, get_module_specifications, save_core);
655 }
656 
657 bool PluginManager::UnregisterPlugin(ObjectFileCreateInstance create_callback) {
658   return GetObjectFileInstances().UnregisterPlugin(create_callback);
659 }
660 
661 ObjectFileCreateInstance
662 PluginManager::GetObjectFileCreateCallbackAtIndex(uint32_t idx) {
663   return GetObjectFileInstances().GetCallbackAtIndex(idx);
664 }
665 
666 ObjectFileCreateMemoryInstance
667 PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(uint32_t idx) {
668   const auto &instances = GetObjectFileInstances().GetInstances();
669   if (idx < instances.size())
670     return instances[idx].create_memory_callback;
671   return nullptr;
672 }
673 
674 ObjectFileGetModuleSpecifications
675 PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
676     uint32_t idx) {
677   const auto &instances = GetObjectFileInstances().GetInstances();
678   if (idx < instances.size())
679     return instances[idx].get_module_specifications;
680   return nullptr;
681 }
682 
683 ObjectFileCreateMemoryInstance
684 PluginManager::GetObjectFileCreateMemoryCallbackForPluginName(
685     llvm::StringRef name) {
686   const auto &instances = GetObjectFileInstances().GetInstances();
687   for (auto &instance : instances) {
688     if (instance.name.GetStringRef() == name)
689       return instance.create_memory_callback;
690   }
691   return nullptr;
692 }
693 
694 Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
695                                const FileSpec &outfile,
696                                lldb::SaveCoreStyle &core_style,
697                                llvm::StringRef plugin_name) {
698   if (plugin_name.empty()) {
699     // Try saving core directly from the process plugin first.
700     llvm::Expected<bool> ret = process_sp->SaveCore(outfile.GetPath());
701     if (!ret)
702       return Status(ret.takeError());
703     if (ret.get())
704       return Status();
705   }
706 
707   // Fall back to object plugins.
708   Status error;
709   auto &instances = GetObjectFileInstances().GetInstances();
710   for (auto &instance : instances) {
711     if (plugin_name.empty() || instance.name.GetStringRef() == plugin_name) {
712       if (instance.save_core &&
713           instance.save_core(process_sp, outfile, core_style, error))
714         return error;
715     }
716   }
717   error.SetErrorString(
718       "no ObjectFile plugins were able to save a core for this process");
719   return error;
720 }
721 
722 #pragma mark ObjectContainer
723 
724 struct ObjectContainerInstance
725     : public PluginInstance<ObjectContainerCreateInstance> {
726   ObjectContainerInstance(
727       ConstString name, std::string description, CallbackType create_callback,
728       ObjectFileGetModuleSpecifications get_module_specifications)
729       : PluginInstance<ObjectContainerCreateInstance>(
730             name, std::move(description), create_callback),
731         get_module_specifications(get_module_specifications) {}
732 
733   ObjectFileGetModuleSpecifications get_module_specifications;
734 };
735 typedef PluginInstances<ObjectContainerInstance> ObjectContainerInstances;
736 
737 static ObjectContainerInstances &GetObjectContainerInstances() {
738   static ObjectContainerInstances g_instances;
739   return g_instances;
740 }
741 
742 bool PluginManager::RegisterPlugin(
743     llvm::StringRef name, llvm::StringRef description,
744     ObjectContainerCreateInstance create_callback,
745     ObjectFileGetModuleSpecifications get_module_specifications) {
746   return GetObjectContainerInstances().RegisterPlugin(
747       ConstString(name), description.str().c_str(), create_callback,
748       get_module_specifications);
749 }
750 
751 bool PluginManager::UnregisterPlugin(
752     ObjectContainerCreateInstance create_callback) {
753   return GetObjectContainerInstances().UnregisterPlugin(create_callback);
754 }
755 
756 ObjectContainerCreateInstance
757 PluginManager::GetObjectContainerCreateCallbackAtIndex(uint32_t idx) {
758   return GetObjectContainerInstances().GetCallbackAtIndex(idx);
759 }
760 
761 ObjectFileGetModuleSpecifications
762 PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(
763     uint32_t idx) {
764   const auto &instances = GetObjectContainerInstances().GetInstances();
765   if (idx < instances.size())
766     return instances[idx].get_module_specifications;
767   return nullptr;
768 }
769 
770 #pragma mark Platform
771 
772 typedef PluginInstance<PlatformCreateInstance> PlatformInstance;
773 typedef PluginInstances<PlatformInstance> PlatformInstances;
774 
775 static PlatformInstances &GetPlatformInstances() {
776   static PlatformInstances g_platform_instances;
777   return g_platform_instances;
778 }
779 
780 bool PluginManager::RegisterPlugin(
781     ConstString name, const char *description,
782     PlatformCreateInstance create_callback,
783     DebuggerInitializeCallback debugger_init_callback) {
784   return GetPlatformInstances().RegisterPlugin(
785       name, description, create_callback, debugger_init_callback);
786 }
787 
788 bool PluginManager::UnregisterPlugin(PlatformCreateInstance create_callback) {
789   return GetPlatformInstances().UnregisterPlugin(create_callback);
790 }
791 
792 const char *PluginManager::GetPlatformPluginNameAtIndex(uint32_t idx) {
793   return GetPlatformInstances().GetNameAtIndex(idx);
794 }
795 
796 const char *PluginManager::GetPlatformPluginDescriptionAtIndex(uint32_t idx) {
797   return GetPlatformInstances().GetDescriptionAtIndex(idx);
798 }
799 
800 PlatformCreateInstance
801 PluginManager::GetPlatformCreateCallbackAtIndex(uint32_t idx) {
802   return GetPlatformInstances().GetCallbackAtIndex(idx);
803 }
804 
805 PlatformCreateInstance
806 PluginManager::GetPlatformCreateCallbackForPluginName(ConstString name) {
807   return GetPlatformInstances().GetCallbackForName(name);
808 }
809 
810 void PluginManager::AutoCompletePlatformName(llvm::StringRef name,
811                                              CompletionRequest &request) {
812   for (const auto &instance : GetPlatformInstances().GetInstances()) {
813     if (instance.name.GetStringRef().startswith(name))
814       request.AddCompletion(instance.name.GetCString());
815   }
816 }
817 
818 #pragma mark Process
819 
820 typedef PluginInstance<ProcessCreateInstance> ProcessInstance;
821 typedef PluginInstances<ProcessInstance> ProcessInstances;
822 
823 static ProcessInstances &GetProcessInstances() {
824   static ProcessInstances g_instances;
825   return g_instances;
826 }
827 
828 bool PluginManager::RegisterPlugin(
829     ConstString name, const char *description,
830     ProcessCreateInstance create_callback,
831     DebuggerInitializeCallback debugger_init_callback) {
832   return GetProcessInstances().RegisterPlugin(
833       name, description, create_callback, debugger_init_callback);
834 }
835 
836 bool PluginManager::UnregisterPlugin(ProcessCreateInstance create_callback) {
837   return GetProcessInstances().UnregisterPlugin(create_callback);
838 }
839 
840 const char *PluginManager::GetProcessPluginNameAtIndex(uint32_t idx) {
841   return GetProcessInstances().GetNameAtIndex(idx);
842 }
843 
844 const char *PluginManager::GetProcessPluginDescriptionAtIndex(uint32_t idx) {
845   return GetProcessInstances().GetDescriptionAtIndex(idx);
846 }
847 
848 ProcessCreateInstance
849 PluginManager::GetProcessCreateCallbackAtIndex(uint32_t idx) {
850   return GetProcessInstances().GetCallbackAtIndex(idx);
851 }
852 
853 ProcessCreateInstance
854 PluginManager::GetProcessCreateCallbackForPluginName(ConstString name) {
855   return GetProcessInstances().GetCallbackForName(name);
856 }
857 
858 void PluginManager::AutoCompleteProcessName(llvm::StringRef name,
859                                             CompletionRequest &request) {
860   for (const auto &instance : GetProcessInstances().GetInstances()) {
861     if (instance.name.GetStringRef().startswith(name))
862       request.AddCompletion(instance.name.GetCString(), instance.description);
863   }
864 }
865 
866 #pragma mark ScriptInterpreter
867 
868 struct ScriptInterpreterInstance
869     : public PluginInstance<ScriptInterpreterCreateInstance> {
870   ScriptInterpreterInstance(ConstString name, std::string description,
871                             CallbackType create_callback,
872                             lldb::ScriptLanguage language)
873       : PluginInstance<ScriptInterpreterCreateInstance>(
874             name, std::move(description), create_callback),
875         language(language) {}
876 
877   lldb::ScriptLanguage language = lldb::eScriptLanguageNone;
878 };
879 
880 typedef PluginInstances<ScriptInterpreterInstance> ScriptInterpreterInstances;
881 
882 static ScriptInterpreterInstances &GetScriptInterpreterInstances() {
883   static ScriptInterpreterInstances g_instances;
884   return g_instances;
885 }
886 
887 bool PluginManager::RegisterPlugin(
888     ConstString name, const char *description,
889     lldb::ScriptLanguage script_language,
890     ScriptInterpreterCreateInstance create_callback) {
891   return GetScriptInterpreterInstances().RegisterPlugin(
892       name, description, create_callback, script_language);
893 }
894 
895 bool PluginManager::UnregisterPlugin(
896     ScriptInterpreterCreateInstance create_callback) {
897   return GetScriptInterpreterInstances().UnregisterPlugin(create_callback);
898 }
899 
900 ScriptInterpreterCreateInstance
901 PluginManager::GetScriptInterpreterCreateCallbackAtIndex(uint32_t idx) {
902   return GetScriptInterpreterInstances().GetCallbackAtIndex(idx);
903 }
904 
905 lldb::ScriptInterpreterSP
906 PluginManager::GetScriptInterpreterForLanguage(lldb::ScriptLanguage script_lang,
907                                                Debugger &debugger) {
908   const auto &instances = GetScriptInterpreterInstances().GetInstances();
909   ScriptInterpreterCreateInstance none_instance = nullptr;
910   for (const auto &instance : instances) {
911     if (instance.language == lldb::eScriptLanguageNone)
912       none_instance = instance.create_callback;
913 
914     if (script_lang == instance.language)
915       return instance.create_callback(debugger);
916   }
917 
918   // If we didn't find one, return the ScriptInterpreter for the null language.
919   assert(none_instance != nullptr);
920   return none_instance(debugger);
921 }
922 
923 #pragma mark StructuredDataPlugin
924 
925 struct StructuredDataPluginInstance
926     : public PluginInstance<StructuredDataPluginCreateInstance> {
927   StructuredDataPluginInstance(
928       ConstString name, std::string description, CallbackType create_callback,
929       DebuggerInitializeCallback debugger_init_callback,
930       StructuredDataFilterLaunchInfo filter_callback)
931       : PluginInstance<StructuredDataPluginCreateInstance>(
932             name, std::move(description), create_callback,
933             debugger_init_callback),
934         filter_callback(filter_callback) {}
935 
936   StructuredDataFilterLaunchInfo filter_callback = nullptr;
937 };
938 
939 typedef PluginInstances<StructuredDataPluginInstance>
940     StructuredDataPluginInstances;
941 
942 static StructuredDataPluginInstances &GetStructuredDataPluginInstances() {
943   static StructuredDataPluginInstances g_instances;
944   return g_instances;
945 }
946 
947 bool PluginManager::RegisterPlugin(
948     ConstString name, const char *description,
949     StructuredDataPluginCreateInstance create_callback,
950     DebuggerInitializeCallback debugger_init_callback,
951     StructuredDataFilterLaunchInfo filter_callback) {
952   return GetStructuredDataPluginInstances().RegisterPlugin(
953       name, description, create_callback, debugger_init_callback,
954       filter_callback);
955 }
956 
957 bool PluginManager::UnregisterPlugin(
958     StructuredDataPluginCreateInstance create_callback) {
959   return GetStructuredDataPluginInstances().UnregisterPlugin(create_callback);
960 }
961 
962 StructuredDataPluginCreateInstance
963 PluginManager::GetStructuredDataPluginCreateCallbackAtIndex(uint32_t idx) {
964   return GetStructuredDataPluginInstances().GetCallbackAtIndex(idx);
965 }
966 
967 StructuredDataFilterLaunchInfo
968 PluginManager::GetStructuredDataFilterCallbackAtIndex(
969     uint32_t idx, bool &iteration_complete) {
970   const auto &instances = GetStructuredDataPluginInstances().GetInstances();
971   if (idx < instances.size()) {
972     iteration_complete = false;
973     return instances[idx].filter_callback;
974   } else {
975     iteration_complete = true;
976   }
977   return nullptr;
978 }
979 
980 #pragma mark SymbolFile
981 
982 typedef PluginInstance<SymbolFileCreateInstance> SymbolFileInstance;
983 typedef PluginInstances<SymbolFileInstance> SymbolFileInstances;
984 
985 static SymbolFileInstances &GetSymbolFileInstances() {
986   static SymbolFileInstances g_instances;
987   return g_instances;
988 }
989 
990 bool PluginManager::RegisterPlugin(
991     ConstString name, const char *description,
992     SymbolFileCreateInstance create_callback,
993     DebuggerInitializeCallback debugger_init_callback) {
994   return GetSymbolFileInstances().RegisterPlugin(
995       name, description, create_callback, debugger_init_callback);
996 }
997 
998 bool PluginManager::UnregisterPlugin(SymbolFileCreateInstance create_callback) {
999   return GetSymbolFileInstances().UnregisterPlugin(create_callback);
1000 }
1001 
1002 SymbolFileCreateInstance
1003 PluginManager::GetSymbolFileCreateCallbackAtIndex(uint32_t idx) {
1004   return GetSymbolFileInstances().GetCallbackAtIndex(idx);
1005 }
1006 
1007 #pragma mark SymbolVendor
1008 
1009 typedef PluginInstance<SymbolVendorCreateInstance> SymbolVendorInstance;
1010 typedef PluginInstances<SymbolVendorInstance> SymbolVendorInstances;
1011 
1012 static SymbolVendorInstances &GetSymbolVendorInstances() {
1013   static SymbolVendorInstances g_instances;
1014   return g_instances;
1015 }
1016 
1017 bool PluginManager::RegisterPlugin(ConstString name, const char *description,
1018                                    SymbolVendorCreateInstance create_callback) {
1019   return GetSymbolVendorInstances().RegisterPlugin(name, description,
1020                                                    create_callback);
1021 }
1022 
1023 bool PluginManager::UnregisterPlugin(
1024     SymbolVendorCreateInstance create_callback) {
1025   return GetSymbolVendorInstances().UnregisterPlugin(create_callback);
1026 }
1027 
1028 SymbolVendorCreateInstance
1029 PluginManager::GetSymbolVendorCreateCallbackAtIndex(uint32_t idx) {
1030   return GetSymbolVendorInstances().GetCallbackAtIndex(idx);
1031 }
1032 
1033 #pragma mark Trace
1034 
1035 struct TraceInstance
1036     : public PluginInstance<TraceCreateInstanceForSessionFile> {
1037   TraceInstance(
1038       ConstString name, std::string description,
1039       CallbackType create_callback_for_session_file,
1040       TraceCreateInstanceForLiveProcess create_callback_for_live_process,
1041       llvm::StringRef schema)
1042       : PluginInstance<TraceCreateInstanceForSessionFile>(
1043             name, std::move(description), create_callback_for_session_file),
1044         schema(schema),
1045         create_callback_for_live_process(create_callback_for_live_process) {}
1046 
1047   llvm::StringRef schema;
1048   TraceCreateInstanceForLiveProcess create_callback_for_live_process;
1049 };
1050 
1051 typedef PluginInstances<TraceInstance> TraceInstances;
1052 
1053 static TraceInstances &GetTracePluginInstances() {
1054   static TraceInstances g_instances;
1055   return g_instances;
1056 }
1057 
1058 bool PluginManager::RegisterPlugin(
1059     ConstString name, const char *description,
1060     TraceCreateInstanceForSessionFile create_callback_for_session_file,
1061     TraceCreateInstanceForLiveProcess create_callback_for_live_process,
1062     llvm::StringRef schema) {
1063   return GetTracePluginInstances().RegisterPlugin(
1064       name, description, create_callback_for_session_file,
1065       create_callback_for_live_process, schema);
1066 }
1067 
1068 bool PluginManager::UnregisterPlugin(
1069     TraceCreateInstanceForSessionFile create_callback_for_session_file) {
1070   return GetTracePluginInstances().UnregisterPlugin(
1071       create_callback_for_session_file);
1072 }
1073 
1074 TraceCreateInstanceForSessionFile
1075 PluginManager::GetTraceCreateCallback(ConstString plugin_name) {
1076   return GetTracePluginInstances().GetCallbackForName(plugin_name);
1077 }
1078 
1079 TraceCreateInstanceForLiveProcess
1080 PluginManager::GetTraceCreateCallbackForLiveProcess(ConstString plugin_name) {
1081   for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1082     if (instance.name == plugin_name)
1083       return instance.create_callback_for_live_process;
1084   return nullptr;
1085 }
1086 
1087 llvm::StringRef PluginManager::GetTraceSchema(ConstString plugin_name) {
1088   for (const TraceInstance &instance : GetTracePluginInstances().GetInstances())
1089     if (instance.name == plugin_name)
1090       return instance.schema;
1091   return llvm::StringRef();
1092 }
1093 
1094 llvm::StringRef PluginManager::GetTraceSchema(size_t index) {
1095   if (TraceInstance *instance =
1096           GetTracePluginInstances().GetInstanceAtIndex(index))
1097     return instance->schema;
1098   return llvm::StringRef();
1099 }
1100 
1101 #pragma mark TraceExporter
1102 
1103 struct TraceExporterInstance
1104     : public PluginInstance<TraceExporterCreateInstance> {
1105   TraceExporterInstance(
1106       ConstString name, std::string description,
1107       TraceExporterCreateInstance create_instance,
1108       ThreadTraceExportCommandCreator create_thread_trace_export_command)
1109       : PluginInstance<TraceExporterCreateInstance>(
1110             name, std::move(description), create_instance),
1111         create_thread_trace_export_command(create_thread_trace_export_command) {
1112   }
1113 
1114   ThreadTraceExportCommandCreator create_thread_trace_export_command;
1115 };
1116 
1117 typedef PluginInstances<TraceExporterInstance> TraceExporterInstances;
1118 
1119 static TraceExporterInstances &GetTraceExporterInstances() {
1120   static TraceExporterInstances g_instances;
1121   return g_instances;
1122 }
1123 
1124 bool PluginManager::RegisterPlugin(
1125     ConstString name, const char *description,
1126     TraceExporterCreateInstance create_callback,
1127     ThreadTraceExportCommandCreator create_thread_trace_export_command) {
1128   return GetTraceExporterInstances().RegisterPlugin(
1129       name, description, create_callback, create_thread_trace_export_command);
1130 }
1131 
1132 TraceExporterCreateInstance
1133 PluginManager::GetTraceExporterCreateCallback(ConstString plugin_name) {
1134   return GetTraceExporterInstances().GetCallbackForName(plugin_name);
1135 }
1136 
1137 bool PluginManager::UnregisterPlugin(
1138     TraceExporterCreateInstance create_callback) {
1139   return GetTraceExporterInstances().UnregisterPlugin(create_callback);
1140 }
1141 
1142 ThreadTraceExportCommandCreator
1143 PluginManager::GetThreadTraceExportCommandCreatorAtIndex(uint32_t index) {
1144   if (TraceExporterInstance *instance =
1145           GetTraceExporterInstances().GetInstanceAtIndex(index))
1146     return instance->create_thread_trace_export_command;
1147   return nullptr;
1148 }
1149 
1150 const char *PluginManager::GetTraceExporterPluginNameAtIndex(uint32_t index) {
1151   return GetTraceExporterInstances().GetNameAtIndex(index);
1152 }
1153 
1154 #pragma mark UnwindAssembly
1155 
1156 typedef PluginInstance<UnwindAssemblyCreateInstance> UnwindAssemblyInstance;
1157 typedef PluginInstances<UnwindAssemblyInstance> UnwindAssemblyInstances;
1158 
1159 static UnwindAssemblyInstances &GetUnwindAssemblyInstances() {
1160   static UnwindAssemblyInstances g_instances;
1161   return g_instances;
1162 }
1163 
1164 bool PluginManager::RegisterPlugin(
1165     ConstString name, const char *description,
1166     UnwindAssemblyCreateInstance create_callback) {
1167   return GetUnwindAssemblyInstances().RegisterPlugin(name, description,
1168                                                      create_callback);
1169 }
1170 
1171 bool PluginManager::UnregisterPlugin(
1172     UnwindAssemblyCreateInstance create_callback) {
1173   return GetUnwindAssemblyInstances().UnregisterPlugin(create_callback);
1174 }
1175 
1176 UnwindAssemblyCreateInstance
1177 PluginManager::GetUnwindAssemblyCreateCallbackAtIndex(uint32_t idx) {
1178   return GetUnwindAssemblyInstances().GetCallbackAtIndex(idx);
1179 }
1180 
1181 #pragma mark MemoryHistory
1182 
1183 typedef PluginInstance<MemoryHistoryCreateInstance> MemoryHistoryInstance;
1184 typedef PluginInstances<MemoryHistoryInstance> MemoryHistoryInstances;
1185 
1186 static MemoryHistoryInstances &GetMemoryHistoryInstances() {
1187   static MemoryHistoryInstances g_instances;
1188   return g_instances;
1189 }
1190 
1191 bool PluginManager::RegisterPlugin(
1192     ConstString name, const char *description,
1193     MemoryHistoryCreateInstance create_callback) {
1194   return GetMemoryHistoryInstances().RegisterPlugin(name, description,
1195                                                     create_callback);
1196 }
1197 
1198 bool PluginManager::UnregisterPlugin(
1199     MemoryHistoryCreateInstance create_callback) {
1200   return GetMemoryHistoryInstances().UnregisterPlugin(create_callback);
1201 }
1202 
1203 MemoryHistoryCreateInstance
1204 PluginManager::GetMemoryHistoryCreateCallbackAtIndex(uint32_t idx) {
1205   return GetMemoryHistoryInstances().GetCallbackAtIndex(idx);
1206 }
1207 
1208 #pragma mark InstrumentationRuntime
1209 
1210 struct InstrumentationRuntimeInstance
1211     : public PluginInstance<InstrumentationRuntimeCreateInstance> {
1212   InstrumentationRuntimeInstance(
1213       ConstString name, std::string description, CallbackType create_callback,
1214       InstrumentationRuntimeGetType get_type_callback)
1215       : PluginInstance<InstrumentationRuntimeCreateInstance>(
1216             name, std::move(description), create_callback),
1217         get_type_callback(get_type_callback) {}
1218 
1219   InstrumentationRuntimeGetType get_type_callback = nullptr;
1220 };
1221 
1222 typedef PluginInstances<InstrumentationRuntimeInstance>
1223     InstrumentationRuntimeInstances;
1224 
1225 static InstrumentationRuntimeInstances &GetInstrumentationRuntimeInstances() {
1226   static InstrumentationRuntimeInstances g_instances;
1227   return g_instances;
1228 }
1229 
1230 bool PluginManager::RegisterPlugin(
1231     ConstString name, const char *description,
1232     InstrumentationRuntimeCreateInstance create_callback,
1233     InstrumentationRuntimeGetType get_type_callback) {
1234   return GetInstrumentationRuntimeInstances().RegisterPlugin(
1235       name, description, create_callback, get_type_callback);
1236 }
1237 
1238 bool PluginManager::UnregisterPlugin(
1239     InstrumentationRuntimeCreateInstance create_callback) {
1240   return GetInstrumentationRuntimeInstances().UnregisterPlugin(create_callback);
1241 }
1242 
1243 InstrumentationRuntimeGetType
1244 PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(uint32_t idx) {
1245   const auto &instances = GetInstrumentationRuntimeInstances().GetInstances();
1246   if (idx < instances.size())
1247     return instances[idx].get_type_callback;
1248   return nullptr;
1249 }
1250 
1251 InstrumentationRuntimeCreateInstance
1252 PluginManager::GetInstrumentationRuntimeCreateCallbackAtIndex(uint32_t idx) {
1253   return GetInstrumentationRuntimeInstances().GetCallbackAtIndex(idx);
1254 }
1255 
1256 #pragma mark TypeSystem
1257 
1258 struct TypeSystemInstance : public PluginInstance<TypeSystemCreateInstance> {
1259   TypeSystemInstance(ConstString name, std::string description,
1260                      CallbackType create_callback,
1261                      LanguageSet supported_languages_for_types,
1262                      LanguageSet supported_languages_for_expressions)
1263       : PluginInstance<TypeSystemCreateInstance>(name, std::move(description),
1264                                                  create_callback),
1265         supported_languages_for_types(supported_languages_for_types),
1266         supported_languages_for_expressions(
1267             supported_languages_for_expressions) {}
1268 
1269   LanguageSet supported_languages_for_types;
1270   LanguageSet supported_languages_for_expressions;
1271 };
1272 
1273 typedef PluginInstances<TypeSystemInstance> TypeSystemInstances;
1274 
1275 static TypeSystemInstances &GetTypeSystemInstances() {
1276   static TypeSystemInstances g_instances;
1277   return g_instances;
1278 }
1279 
1280 bool PluginManager::RegisterPlugin(
1281     ConstString name, const char *description,
1282     TypeSystemCreateInstance create_callback,
1283     LanguageSet supported_languages_for_types,
1284     LanguageSet supported_languages_for_expressions) {
1285   return GetTypeSystemInstances().RegisterPlugin(
1286       name, description, create_callback, supported_languages_for_types,
1287       supported_languages_for_expressions);
1288 }
1289 
1290 bool PluginManager::UnregisterPlugin(TypeSystemCreateInstance create_callback) {
1291   return GetTypeSystemInstances().UnregisterPlugin(create_callback);
1292 }
1293 
1294 TypeSystemCreateInstance
1295 PluginManager::GetTypeSystemCreateCallbackAtIndex(uint32_t idx) {
1296   return GetTypeSystemInstances().GetCallbackAtIndex(idx);
1297 }
1298 
1299 LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForTypes() {
1300   const auto &instances = GetTypeSystemInstances().GetInstances();
1301   LanguageSet all;
1302   for (unsigned i = 0; i < instances.size(); ++i)
1303     all.bitvector |= instances[i].supported_languages_for_types.bitvector;
1304   return all;
1305 }
1306 
1307 LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForExpressions() {
1308   const auto &instances = GetTypeSystemInstances().GetInstances();
1309   LanguageSet all;
1310   for (unsigned i = 0; i < instances.size(); ++i)
1311     all.bitvector |= instances[i].supported_languages_for_expressions.bitvector;
1312   return all;
1313 }
1314 
1315 #pragma mark REPL
1316 
1317 struct REPLInstance : public PluginInstance<REPLCreateInstance> {
1318   REPLInstance(ConstString name, std::string description,
1319                CallbackType create_callback, LanguageSet supported_languages)
1320       : PluginInstance<REPLCreateInstance>(name, std::move(description),
1321                                            create_callback),
1322         supported_languages(supported_languages) {}
1323 
1324   LanguageSet supported_languages;
1325 };
1326 
1327 typedef PluginInstances<REPLInstance> REPLInstances;
1328 
1329 static REPLInstances &GetREPLInstances() {
1330   static REPLInstances g_instances;
1331   return g_instances;
1332 }
1333 
1334 bool PluginManager::RegisterPlugin(ConstString name, const char *description,
1335                                    REPLCreateInstance create_callback,
1336                                    LanguageSet supported_languages) {
1337   return GetREPLInstances().RegisterPlugin(name, description, create_callback,
1338                                            supported_languages);
1339 }
1340 
1341 bool PluginManager::UnregisterPlugin(REPLCreateInstance create_callback) {
1342   return GetREPLInstances().UnregisterPlugin(create_callback);
1343 }
1344 
1345 REPLCreateInstance PluginManager::GetREPLCreateCallbackAtIndex(uint32_t idx) {
1346   return GetREPLInstances().GetCallbackAtIndex(idx);
1347 }
1348 
1349 LanguageSet PluginManager::GetREPLAllTypeSystemSupportedLanguages() {
1350   const auto &instances = GetREPLInstances().GetInstances();
1351   LanguageSet all;
1352   for (unsigned i = 0; i < instances.size(); ++i)
1353     all.bitvector |= instances[i].supported_languages.bitvector;
1354   return all;
1355 }
1356 
1357 #pragma mark PluginManager
1358 
1359 void PluginManager::DebuggerInitialize(Debugger &debugger) {
1360   GetDynamicLoaderInstances().PerformDebuggerCallback(debugger);
1361   GetJITLoaderInstances().PerformDebuggerCallback(debugger);
1362   GetPlatformInstances().PerformDebuggerCallback(debugger);
1363   GetProcessInstances().PerformDebuggerCallback(debugger);
1364   GetSymbolFileInstances().PerformDebuggerCallback(debugger);
1365   GetOperatingSystemInstances().PerformDebuggerCallback(debugger);
1366   GetStructuredDataPluginInstances().PerformDebuggerCallback(debugger);
1367   GetTracePluginInstances().PerformDebuggerCallback(debugger);
1368 }
1369 
1370 // This is the preferred new way to register plugin specific settings.  e.g.
1371 // This will put a plugin's settings under e.g.
1372 // "plugin.<plugin_type_name>.<plugin_type_desc>.SETTINGNAME".
1373 static lldb::OptionValuePropertiesSP
1374 GetDebuggerPropertyForPlugins(Debugger &debugger, ConstString plugin_type_name,
1375                               ConstString plugin_type_desc, bool can_create) {
1376   lldb::OptionValuePropertiesSP parent_properties_sp(
1377       debugger.GetValueProperties());
1378   if (parent_properties_sp) {
1379     static ConstString g_property_name("plugin");
1380 
1381     OptionValuePropertiesSP plugin_properties_sp =
1382         parent_properties_sp->GetSubProperty(nullptr, g_property_name);
1383     if (!plugin_properties_sp && can_create) {
1384       plugin_properties_sp =
1385           std::make_shared<OptionValueProperties>(g_property_name);
1386       parent_properties_sp->AppendProperty(
1387           g_property_name, ConstString("Settings specify to plugins."), true,
1388           plugin_properties_sp);
1389     }
1390 
1391     if (plugin_properties_sp) {
1392       lldb::OptionValuePropertiesSP plugin_type_properties_sp =
1393           plugin_properties_sp->GetSubProperty(nullptr, plugin_type_name);
1394       if (!plugin_type_properties_sp && can_create) {
1395         plugin_type_properties_sp =
1396             std::make_shared<OptionValueProperties>(plugin_type_name);
1397         plugin_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,
1398                                              true, plugin_type_properties_sp);
1399       }
1400       return plugin_type_properties_sp;
1401     }
1402   }
1403   return lldb::OptionValuePropertiesSP();
1404 }
1405 
1406 // This is deprecated way to register plugin specific settings.  e.g.
1407 // "<plugin_type_name>.plugin.<plugin_type_desc>.SETTINGNAME" and Platform
1408 // generic settings would be under "platform.SETTINGNAME".
1409 static lldb::OptionValuePropertiesSP GetDebuggerPropertyForPluginsOldStyle(
1410     Debugger &debugger, ConstString plugin_type_name,
1411     ConstString plugin_type_desc, bool can_create) {
1412   static ConstString g_property_name("plugin");
1413   lldb::OptionValuePropertiesSP parent_properties_sp(
1414       debugger.GetValueProperties());
1415   if (parent_properties_sp) {
1416     OptionValuePropertiesSP plugin_properties_sp =
1417         parent_properties_sp->GetSubProperty(nullptr, plugin_type_name);
1418     if (!plugin_properties_sp && can_create) {
1419       plugin_properties_sp =
1420           std::make_shared<OptionValueProperties>(plugin_type_name);
1421       parent_properties_sp->AppendProperty(plugin_type_name, plugin_type_desc,
1422                                            true, plugin_properties_sp);
1423     }
1424 
1425     if (plugin_properties_sp) {
1426       lldb::OptionValuePropertiesSP plugin_type_properties_sp =
1427           plugin_properties_sp->GetSubProperty(nullptr, g_property_name);
1428       if (!plugin_type_properties_sp && can_create) {
1429         plugin_type_properties_sp =
1430             std::make_shared<OptionValueProperties>(g_property_name);
1431         plugin_properties_sp->AppendProperty(
1432             g_property_name, ConstString("Settings specific to plugins"), true,
1433             plugin_type_properties_sp);
1434       }
1435       return plugin_type_properties_sp;
1436     }
1437   }
1438   return lldb::OptionValuePropertiesSP();
1439 }
1440 
1441 namespace {
1442 
1443 typedef lldb::OptionValuePropertiesSP
1444 GetDebuggerPropertyForPluginsPtr(Debugger &, ConstString, ConstString,
1445                                  bool can_create);
1446 }
1447 
1448 static lldb::OptionValuePropertiesSP
1449 GetSettingForPlugin(Debugger &debugger, ConstString setting_name,
1450                     ConstString plugin_type_name,
1451                     GetDebuggerPropertyForPluginsPtr get_debugger_property =
1452                         GetDebuggerPropertyForPlugins) {
1453   lldb::OptionValuePropertiesSP properties_sp;
1454   lldb::OptionValuePropertiesSP plugin_type_properties_sp(get_debugger_property(
1455       debugger, plugin_type_name,
1456       ConstString(), // not creating to so we don't need the description
1457       false));
1458   if (plugin_type_properties_sp)
1459     properties_sp =
1460         plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);
1461   return properties_sp;
1462 }
1463 
1464 static bool
1465 CreateSettingForPlugin(Debugger &debugger, ConstString plugin_type_name,
1466                        ConstString plugin_type_desc,
1467                        const lldb::OptionValuePropertiesSP &properties_sp,
1468                        ConstString description, bool is_global_property,
1469                        GetDebuggerPropertyForPluginsPtr get_debugger_property =
1470                            GetDebuggerPropertyForPlugins) {
1471   if (properties_sp) {
1472     lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1473         get_debugger_property(debugger, plugin_type_name, plugin_type_desc,
1474                               true));
1475     if (plugin_type_properties_sp) {
1476       plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),
1477                                                 description, is_global_property,
1478                                                 properties_sp);
1479       return true;
1480     }
1481   }
1482   return false;
1483 }
1484 
1485 static const char *kDynamicLoaderPluginName("dynamic-loader");
1486 static const char *kPlatformPluginName("platform");
1487 static const char *kProcessPluginName("process");
1488 static const char *kSymbolFilePluginName("symbol-file");
1489 static const char *kJITLoaderPluginName("jit-loader");
1490 static const char *kStructuredDataPluginName("structured-data");
1491 
1492 lldb::OptionValuePropertiesSP
1493 PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,
1494                                                 ConstString setting_name) {
1495   return GetSettingForPlugin(debugger, setting_name,
1496                              ConstString(kDynamicLoaderPluginName));
1497 }
1498 
1499 bool PluginManager::CreateSettingForDynamicLoaderPlugin(
1500     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1501     ConstString description, bool is_global_property) {
1502   return CreateSettingForPlugin(
1503       debugger, ConstString(kDynamicLoaderPluginName),
1504       ConstString("Settings for dynamic loader plug-ins"), properties_sp,
1505       description, is_global_property);
1506 }
1507 
1508 lldb::OptionValuePropertiesSP
1509 PluginManager::GetSettingForPlatformPlugin(Debugger &debugger,
1510                                            ConstString setting_name) {
1511   return GetSettingForPlugin(debugger, setting_name,
1512                              ConstString(kPlatformPluginName),
1513                              GetDebuggerPropertyForPluginsOldStyle);
1514 }
1515 
1516 bool PluginManager::CreateSettingForPlatformPlugin(
1517     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1518     ConstString description, bool is_global_property) {
1519   return CreateSettingForPlugin(debugger, ConstString(kPlatformPluginName),
1520                                 ConstString("Settings for platform plug-ins"),
1521                                 properties_sp, description, is_global_property,
1522                                 GetDebuggerPropertyForPluginsOldStyle);
1523 }
1524 
1525 lldb::OptionValuePropertiesSP
1526 PluginManager::GetSettingForProcessPlugin(Debugger &debugger,
1527                                           ConstString setting_name) {
1528   return GetSettingForPlugin(debugger, setting_name,
1529                              ConstString(kProcessPluginName));
1530 }
1531 
1532 bool PluginManager::CreateSettingForProcessPlugin(
1533     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1534     ConstString description, bool is_global_property) {
1535   return CreateSettingForPlugin(debugger, ConstString(kProcessPluginName),
1536                                 ConstString("Settings for process plug-ins"),
1537                                 properties_sp, description, is_global_property);
1538 }
1539 
1540 lldb::OptionValuePropertiesSP
1541 PluginManager::GetSettingForSymbolFilePlugin(Debugger &debugger,
1542                                              ConstString setting_name) {
1543   return GetSettingForPlugin(debugger, setting_name,
1544                              ConstString(kSymbolFilePluginName));
1545 }
1546 
1547 bool PluginManager::CreateSettingForSymbolFilePlugin(
1548     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1549     ConstString description, bool is_global_property) {
1550   return CreateSettingForPlugin(
1551       debugger, ConstString(kSymbolFilePluginName),
1552       ConstString("Settings for symbol file plug-ins"), properties_sp,
1553       description, is_global_property);
1554 }
1555 
1556 lldb::OptionValuePropertiesSP
1557 PluginManager::GetSettingForJITLoaderPlugin(Debugger &debugger,
1558                                             ConstString setting_name) {
1559   return GetSettingForPlugin(debugger, setting_name,
1560                              ConstString(kJITLoaderPluginName));
1561 }
1562 
1563 bool PluginManager::CreateSettingForJITLoaderPlugin(
1564     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1565     ConstString description, bool is_global_property) {
1566   return CreateSettingForPlugin(debugger, ConstString(kJITLoaderPluginName),
1567                                 ConstString("Settings for JIT loader plug-ins"),
1568                                 properties_sp, description, is_global_property);
1569 }
1570 
1571 static const char *kOperatingSystemPluginName("os");
1572 
1573 lldb::OptionValuePropertiesSP
1574 PluginManager::GetSettingForOperatingSystemPlugin(Debugger &debugger,
1575                                                   ConstString setting_name) {
1576   lldb::OptionValuePropertiesSP properties_sp;
1577   lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1578       GetDebuggerPropertyForPlugins(
1579           debugger, ConstString(kOperatingSystemPluginName),
1580           ConstString(), // not creating to so we don't need the description
1581           false));
1582   if (plugin_type_properties_sp)
1583     properties_sp =
1584         plugin_type_properties_sp->GetSubProperty(nullptr, setting_name);
1585   return properties_sp;
1586 }
1587 
1588 bool PluginManager::CreateSettingForOperatingSystemPlugin(
1589     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1590     ConstString description, bool is_global_property) {
1591   if (properties_sp) {
1592     lldb::OptionValuePropertiesSP plugin_type_properties_sp(
1593         GetDebuggerPropertyForPlugins(
1594             debugger, ConstString(kOperatingSystemPluginName),
1595             ConstString("Settings for operating system plug-ins"), true));
1596     if (plugin_type_properties_sp) {
1597       plugin_type_properties_sp->AppendProperty(properties_sp->GetName(),
1598                                                 description, is_global_property,
1599                                                 properties_sp);
1600       return true;
1601     }
1602   }
1603   return false;
1604 }
1605 
1606 lldb::OptionValuePropertiesSP
1607 PluginManager::GetSettingForStructuredDataPlugin(Debugger &debugger,
1608                                                  ConstString setting_name) {
1609   return GetSettingForPlugin(debugger, setting_name,
1610                              ConstString(kStructuredDataPluginName));
1611 }
1612 
1613 bool PluginManager::CreateSettingForStructuredDataPlugin(
1614     Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
1615     ConstString description, bool is_global_property) {
1616   return CreateSettingForPlugin(
1617       debugger, ConstString(kStructuredDataPluginName),
1618       ConstString("Settings for structured data plug-ins"), properties_sp,
1619       description, is_global_property);
1620 }
1621