summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core
diff options
context:
space:
mode:
authorGreg Clayton <gclayton@apple.com>2011-02-02 02:24:04 +0000
committerGreg Clayton <gclayton@apple.com>2011-02-02 02:24:04 +0000
commit4272cc7d4c1e1a8cb39595cfe691e2d6985f7161 (patch)
treefe1ca65674d840c948b5866e10616620258fd04a /lldb/source/Core
parentb93f581948f66b230cbbd467705b97fa88879b44 (diff)
downloadbcm5719-llvm-4272cc7d4c1e1a8cb39595cfe691e2d6985f7161.tar.gz
bcm5719-llvm-4272cc7d4c1e1a8cb39595cfe691e2d6985f7161.zip
Modified the PluginManager to be ready for loading plug-ins from a system
LLDB plugin directory and a user LLDB plugin directory. We currently still need to work out at what layer the plug-ins will be, but at least we are prepared for plug-ins. Plug-ins will attempt to be loaded from the "/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Plugins" folder, and from the "~/Library/Application Support/LLDB/Plugins" folder on MacOSX. Each plugin will be scanned for: extern "C" bool LLDBPluginInitialize(void); extern "C" void LLDBPluginTerminate(void); If at least LLDBPluginInitialize is found, the plug-in will be loaded. The LLDBPluginInitialize function returns a bool that indicates if the plug-in should stay loaded or not (plug-ins might check the current OS, current hardware, or anything else and determine they don't want to run on the current host). The plug-in is uniqued by path and added to a static loaded plug-in map. The plug-in scanning happens during "lldb_private::Initialize()" which calls to the PluginManager::Initialize() function. Likewise with termination lldb_private::Terminate() calls PluginManager::Terminate(). The paths for the plug-in directories is fetched through new Host calls: bool Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec); bool Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec); This way linux and other systems can define their own appropriate locations for plug-ins to be loaded. To allow dynamic shared library loading, the Host layer has also been modified to include shared library open, close and get symbol: static void * Host::DynamicLibraryOpen (const FileSpec &file_spec, Error &error); static Error Host::DynamicLibraryClose (void *dynamic_library_handle); static void * Host::DynamicLibraryGetSymbol (void *dynamic_library_handle, const char *symbol_name, Error &error); lldb_private::FileSpec also has been modified to support directory enumeration in an attempt to abstract the directory enumeration into one spot in the code. The directory enumertion function is static and takes a callback: typedef enum EnumerateDirectoryResult { eEnumerateDirectoryResultNext, // Enumerate next entry in the current directory eEnumerateDirectoryResultEnter, // Recurse into the current entry if it is a directory or symlink, or next if not eEnumerateDirectoryResultExit, // Exit from the current directory at the current level. eEnumerateDirectoryResultQuit // Stop directory enumerations at any level }; typedef FileSpec::EnumerateDirectoryResult (*EnumerateDirectoryCallbackType) (void *baton, FileSpec::FileType file_type, const FileSpec &spec); static FileSpec::EnumerateDirectoryResult FileSpec::EnumerateDirectory (const char *dir_path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton); This allow clients to specify the directory to search, and specifies if only files, directories or other (pipe, symlink, fifo, etc) files will cause the callback to be called. The callback also gets to return with the action that should be performed after this directory entry. eEnumerateDirectoryResultNext specifies to continue enumerating through a directory with the next entry. eEnumerateDirectoryResultEnter specifies to recurse down into a directory entry, or if the file is not a directory or symlink/alias to a directory, then just iterate to the next entry. eEnumerateDirectoryResultExit specifies to exit the current directory and skip any entries that might be remaining, yet continue enumerating to the next entry in the parent directory. And finally eEnumerateDirectoryResultQuit means to abort all directory enumerations at all levels. Modified the Declaration class to not include column information currently since we don't have any compilers that currently support column based declaration information. Columns support can be re-enabled with the additions of a #define. Added the ability to find an EmulateInstruction plug-in given a target triple and optional plug-in name in the plug-in manager. Fixed a few cases where opendir/readdir was being used, but yet not closedir was being used. Soon these will be deprecated in favor of the new directory enumeration call that was added to the FileSpec class. llvm-svn: 124716
Diffstat (limited to 'lldb/source/Core')
-rw-r--r--lldb/source/Core/EmulateInstruction.cpp26
-rw-r--r--lldb/source/Core/FileSpec.cpp103
-rw-r--r--lldb/source/Core/PluginManager.cpp209
3 files changed, 323 insertions, 15 deletions
diff --git a/lldb/source/Core/EmulateInstruction.cpp b/lldb/source/Core/EmulateInstruction.cpp
index 3718c12d320..391fa87a9d8 100644
--- a/lldb/source/Core/EmulateInstruction.cpp
+++ b/lldb/source/Core/EmulateInstruction.cpp
@@ -10,11 +10,37 @@
#include "EmulateInstruction.h"
#include "lldb/Core/DataExtractor.h"
+#include "lldb/Core/PluginManager.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Endian.h"
using namespace lldb;
using namespace lldb_private;
+EmulateInstruction*
+EmulateInstruction::FindPlugin (const ConstString &triple, const char *plugin_name)
+{
+ EmulateInstructionCreateInstance create_callback = NULL;
+ if (plugin_name)
+ {
+ create_callback = PluginManager::GetEmulateInstructionCreateCallbackForPluginName (plugin_name);
+ if (create_callback)
+ {
+ std::auto_ptr<EmulateInstruction> instance_ap(create_callback(triple));
+ if (instance_ap.get())
+ return instance_ap.release();
+ }
+ }
+ else
+ {
+ for (uint32_t idx = 0; (create_callback = PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) != NULL; ++idx)
+ {
+ std::auto_ptr<EmulateInstruction> instance_ap(create_callback(triple));
+ if (instance_ap.get())
+ return instance_ap.release();
+ }
+ }
+ return NULL;
+}
EmulateInstruction::EmulateInstruction
(
diff --git a/lldb/source/Core/FileSpec.cpp b/lldb/source/Core/FileSpec.cpp
index f1850439bc1..d0749bd3554 100644
--- a/lldb/source/Core/FileSpec.cpp
+++ b/lldb/source/Core/FileSpec.cpp
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
+#include <dirent.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdlib.h>
@@ -27,6 +28,7 @@
#include "lldb/Core/DataBufferMemoryMap.h"
#include "lldb/Core/Stream.h"
#include "lldb/Host/Host.h"
+#include "lldb/Utility/CleanUp.h"
using namespace lldb;
using namespace lldb_private;
@@ -572,7 +574,7 @@ FileSpec::GetFileType () const
default:
break;
}
- return eFileTypeUknown;
+ return eFileTypeUnknown;
}
return eFileTypeInvalid;
}
@@ -805,3 +807,102 @@ FileSpec::ReadFileLines (STLStringArray &lines)
}
return lines.size();
}
+
+FileSpec::EnumerateDirectoryResult
+FileSpec::EnumerateDirectory
+(
+ const char *dir_path,
+ bool find_directories,
+ bool find_files,
+ bool find_other,
+ EnumerateDirectoryCallbackType callback,
+ void *callback_baton
+)
+{
+ if (dir_path && dir_path[0])
+ {
+ lldb_utility::CleanUp <DIR *, int> dir_path_dir (opendir(dir_path), NULL, closedir);
+ if (dir_path_dir.is_valid())
+ {
+ struct dirent* dp;
+ while ((dp = readdir(dir_path_dir.get())) != NULL)
+ {
+ // Only search directories
+ if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
+ {
+ if (dp->d_namlen == 1 && dp->d_name[0] == '.')
+ continue;
+
+ if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
+ continue;
+ }
+
+ bool call_callback = false;
+ FileSpec::FileType file_type = eFileTypeUnknown;
+
+ switch (dp->d_type)
+ {
+ default:
+ case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break;
+ case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break;
+ case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break;
+ case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break;
+ case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break;
+ case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break;
+ case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break;
+ case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break;
+ case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break;
+ }
+
+ if (call_callback)
+ {
+ char child_path[PATH_MAX];
+ const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name);
+ if (child_path_len < sizeof(child_path) - 1)
+ {
+ // Don't resolve the file type or path
+ FileSpec child_path_spec (child_path, false);
+
+ EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec);
+
+ switch (result)
+ {
+ default:
+ case eEnumerateDirectoryResultNext:
+ // Enumerate next entry in the current directory. We just
+ // exit this switch and will continue enumerating the
+ // current directory as we currently are...
+ break;
+
+ case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not
+ if (FileSpec::EnumerateDirectory (child_path,
+ find_directories,
+ find_files,
+ find_other,
+ callback,
+ callback_baton) == eEnumerateDirectoryResultQuit)
+ {
+ // The subdirectory returned Quit, which means to
+ // stop all directory enumerations at all levels.
+ return eEnumerateDirectoryResultQuit;
+ }
+ break;
+
+ case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level.
+ // Exit from this directory level and tell parent to
+ // keep enumerating.
+ return eEnumerateDirectoryResultNext;
+
+ case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level
+ return eEnumerateDirectoryResultQuit;
+ }
+ }
+ }
+ }
+ }
+ }
+ // By default when exiting a directory, we tell the parent enumeration
+ // to continue enumerating.
+ return eEnumerateDirectoryResultNext;
+}
+
diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp
index 198beae191a..9117754fa2f 100644
--- a/lldb/source/Core/PluginManager.cpp
+++ b/lldb/source/Core/PluginManager.cpp
@@ -12,6 +12,12 @@
#include <string>
#include <vector>
+#include "lldb/Core/Error.h"
+#include "lldb/Core/FileSpec.h"
+#include "lldb/Host/Host.h"
+#include "lldb/Host/Mutex.h"
+
+using namespace lldb;
using namespace lldb_private;
enum PluginAction
@@ -21,6 +27,181 @@ enum PluginAction
ePluginGetInstanceAtIndex
};
+struct PluginInfo
+{
+ void *plugin_handle;
+ void *plugin_init_callback;
+ void *plugin_term_callback;
+};
+
+typedef std::map<FileSpec, PluginInfo> PluginTerminateMap;
+
+static Mutex &
+GetPluginMapMutex ()
+{
+ static Mutex g_plugin_map_mutex (Mutex::eMutexTypeRecursive);
+ return g_plugin_map_mutex;
+}
+
+static PluginTerminateMap &
+GetPluginMap ()
+{
+ static PluginTerminateMap g_plugin_map;
+ return g_plugin_map;
+}
+
+static bool
+PluginIsLoaded (const FileSpec &plugin_file_spec)
+{
+ Mutex::Locker locker (GetPluginMapMutex ());
+ PluginTerminateMap &plugin_map = GetPluginMap ();
+ return plugin_map.find (plugin_file_spec) != plugin_map.end();
+}
+
+static void
+SetPluginInfo (const FileSpec &plugin_file_spec, const PluginInfo &plugin_info)
+{
+ Mutex::Locker locker (GetPluginMapMutex ());
+ PluginTerminateMap &plugin_map = GetPluginMap ();
+ assert (plugin_map.find (plugin_file_spec) != plugin_map.end());
+ plugin_map[plugin_file_spec] = plugin_info;
+}
+
+
+static FileSpec::EnumerateDirectoryResult
+LoadPluginCallback
+(
+ void *baton,
+ FileSpec::FileType file_type,
+ const FileSpec &file_spec
+)
+{
+// PluginManager *plugin_manager = (PluginManager *)baton;
+ Error error;
+
+ // If we have a regular file, a symbolic link or unknown file type, try
+ // and process the file. We must handle unknown as sometimes the directory
+ // enumeration might be enumerating a file system that doesn't have correct
+ // file type information.
+ if (file_type == FileSpec::eFileTypeRegular ||
+ file_type == FileSpec::eFileTypeSymbolicLink ||
+ file_type == FileSpec::eFileTypeUnknown )
+ {
+ FileSpec plugin_file_spec (file_spec);
+ plugin_file_spec.ResolvePath();
+
+ if (PluginIsLoaded (plugin_file_spec))
+ return FileSpec::eEnumerateDirectoryResultNext;
+ else
+ {
+ PluginInfo plugin_info = { NULL, NULL, NULL };
+ plugin_info.plugin_handle = Host::DynamicLibraryOpen (plugin_file_spec, error);
+ if (plugin_info.plugin_handle)
+ {
+ bool success = false;
+ plugin_info.plugin_init_callback = Host::DynamicLibraryGetSymbol (plugin_info.plugin_handle, "LLDBPluginInitialize", error);
+ if (plugin_info.plugin_init_callback)
+ {
+ // Call the plug-in "bool LLDBPluginInitialize(void)" function
+ success = ((bool (*)(void))plugin_info.plugin_init_callback)();
+ }
+
+ if (success)
+ {
+ // It is ok for the "LLDBPluginTerminate" symbol to be NULL
+ plugin_info.plugin_term_callback = Host::DynamicLibraryGetSymbol (plugin_info.plugin_handle, "LLDBPluginTerminate", error);
+ }
+ else
+ {
+ // The initialize function returned FALSE which means the
+ // plug-in might not be compatible, or might be too new or
+ // too old, or might not want to run on this machine.
+ Host::DynamicLibraryClose (plugin_info.plugin_handle);
+ plugin_info.plugin_handle = NULL;
+ plugin_info.plugin_init_callback = NULL;
+ }
+
+ // Regardless of success or failure, cache the plug-in load
+ // in our plug-in info so we don't try to load it again and
+ // again.
+ SetPluginInfo (plugin_file_spec, plugin_info);
+
+ return FileSpec::eEnumerateDirectoryResultNext;
+ }
+ }
+ }
+
+ if (file_type == FileSpec::eFileTypeUnknown ||
+ file_type == FileSpec::eFileTypeDirectory ||
+ file_type == FileSpec::eFileTypeSymbolicLink )
+ {
+ // Try and recurse into anything that a directory or symbolic link.
+ // We must also do this for unknown as sometimes the directory enumeration
+ // might be enurating a file system that doesn't have correct file type
+ // information.
+ return FileSpec::eEnumerateDirectoryResultEnter;
+ }
+
+ return FileSpec::eEnumerateDirectoryResultNext;
+}
+
+
+void
+PluginManager::Initialize ()
+{
+ FileSpec dir_spec;
+ const bool find_directories = true;
+ const bool find_files = true;
+ const bool find_other = true;
+ char dir_path[PATH_MAX];
+ if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
+ {
+ if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
+ {
+ FileSpec::EnumerateDirectory (dir_path,
+ find_directories,
+ find_files,
+ find_other,
+ LoadPluginCallback,
+ NULL);
+ }
+ }
+
+ if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
+ {
+ if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
+ {
+ FileSpec::EnumerateDirectory (dir_path,
+ find_directories,
+ find_files,
+ find_other,
+ LoadPluginCallback,
+ NULL);
+ }
+ }
+}
+
+void
+PluginManager::Terminate ()
+{
+ Mutex::Locker locker (GetPluginMapMutex ());
+ PluginTerminateMap &plugin_map = GetPluginMap ();
+
+ PluginTerminateMap::const_iterator pos, end = plugin_map.end();
+ for (pos = plugin_map.begin(); pos != end; ++pos)
+ {
+ // Call the plug-in "void LLDBPluginTerminate (void)" function if there
+ // is one (if the symbol was not NULL).
+ if (pos->second.plugin_handle)
+ {
+ if (pos->second.plugin_term_callback)
+ ((void (*)(void))pos->second.plugin_term_callback)();
+ Host::DynamicLibraryClose (pos->second.plugin_handle);
+ }
+ }
+ plugin_map.clear();
+}
+
#pragma mark ABI
@@ -88,11 +269,11 @@ AccessABIInstances (PluginAction action, ABIInstance &instance, uint32_t index)
bool
PluginManager::RegisterPlugin
- (
- const char *name,
- const char *description,
- ABICreateInstance create_callback
- )
+(
+ const char *name,
+ const char *description,
+ ABICreateInstance create_callback
+)
{
if (create_callback)
{
@@ -458,10 +639,10 @@ AccessEmulateInstructionInstances (PluginAction action, EmulateInstructionInstan
bool
PluginManager::RegisterPlugin
(
- const char *name,
- const char *description,
- EmulateInstructionCreateInstance create_callback
- )
+ const char *name,
+ const char *description,
+ EmulateInstructionCreateInstance create_callback
+)
{
if (create_callback)
{
@@ -580,11 +761,11 @@ AccessLanguageRuntimeInstances (PluginAction action, LanguageRuntimeInstance &in
bool
PluginManager::RegisterPlugin
- (
- const char *name,
- const char *description,
- LanguageRuntimeCreateInstance create_callback
- )
+(
+ const char *name,
+ const char *description,
+ LanguageRuntimeCreateInstance create_callback
+)
{
if (create_callback)
{
OpenPOWER on IntegriCloud