Cleaned up the dynamic library open/getsymbol/close code to use abstracted

flags such that symbols can be searched for within a shared library if desired.
Platforms that support the RTLD_FIRST flag can still take advantage of their
quicker lookups, and other platforms can still get the same fucntionality
with a little extra work.

Also changed LLDB_CONFIG flags over to either being defined, or not being
defined to stay in line with current open source practices and to prepare for
using autoconf or cmake to configure LLDB builds.

llvm-svn: 125064
This commit is contained in:
Greg Clayton 2011-02-08 00:35:34 +00:00
parent 14889fc671
commit 453194664d
12 changed files with 135 additions and 47 deletions

View File

@ -521,7 +521,7 @@ public:
static size_t static size_t
Resolve (const char *src_path, char *dst_path, size_t dst_len); Resolve (const char *src_path, char *dst_path, size_t dst_len);
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
//------------------------------------------------------------------ //------------------------------------------------------------------

View File

@ -346,8 +346,15 @@ public:
static size_t static size_t
GetEnvironment (StringList &env); GetEnvironment (StringList &env);
enum DynamicLibraryOpenOptions
{
eDynamicLibraryOpenOptionLazy = (1u << 0), // Lazily resolve symbols in this dynamic library
eDynamicLibraryOpenOptionLocal = (1u << 1), // Only open a shared library with local access (hide it from the global symbol namespace)
eDynamicLibraryOpenOptionLimitGetSymbol = (1u << 2) // DynamicLibraryGetSymbol calls on this handle will only return matches from this shared library
};
static void * static void *
DynamicLibraryOpen (const FileSpec &file_spec, DynamicLibraryOpen (const FileSpec &file_spec,
uint32_t options,
Error &error); Error &error);
static Error static Error

View File

@ -19,4 +19,10 @@
#define LLDB_CONFIG_TERMIOS_SUPPORTED 1 #define LLDB_CONFIG_TERMIOS_SUPPORTED 1
#define LLDB_CONFIG_TERMIOS_SUPPORTED 1
#define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1
//#define LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED 1
#endif // #ifndef liblldb_Platform_Config_h_ #endif // #ifndef liblldb_Platform_Config_h_

View File

@ -23,4 +23,6 @@
#define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1 #define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1
//#define LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED 1
#endif // #ifndef liblldb_Platform_Config_h_ #endif // #ifndef liblldb_Platform_Config_h_

View File

@ -23,4 +23,6 @@
#define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1 #define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1
#define LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED 1
#endif // #ifndef liblldb_Platform_Config_h_ #endif // #ifndef liblldb_Platform_Config_h_

View File

@ -19,8 +19,10 @@
#define LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 1 #define LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 1
#define LLDB_CONFIG_TERMIOS_SUPPORTED 0 //#define LLDB_CONFIG_TERMIOS_SUPPORTED 1
#define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 0 //#define LLDB_CONFIG_TILDE_RESOLVES_TO_USER 1
//#define LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED 1
#endif // #ifndef liblldb_Platform_Config_h_ #endif // #ifndef liblldb_Platform_Config_h_

View File

@ -153,7 +153,7 @@ DiskFilesOrDirectories
if (end_ptr == NULL) if (end_ptr == NULL)
{ {
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
// There's no directory. If the thing begins with a "~" then this is a bare // There's no directory. If the thing begins with a "~" then this is a bare
// user name. // user name.
@ -207,7 +207,7 @@ DiskFilesOrDirectories
return matches.GetSize(); return matches.GetSize();
} }
else else
#endif // LLDB_CONFIG_TILDE_RESOLVES_TO_USER #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
{ {
// The containing part is the CWD, and the whole string is the remainder. // The containing part is the CWD, and the whole string is the remainder.
@ -239,7 +239,7 @@ DiskFilesOrDirectories
// Look for a user name in the containing part, and if it's there, resolve it and stick the // Look for a user name in the containing part, and if it's there, resolve it and stick the
// result back into the containing_part: // result back into the containing_part:
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
if (*partial_name_copy == '~') if (*partial_name_copy == '~')
{ {
size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, containing_part, sizeof (containing_part)); size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, containing_part, sizeof (containing_part));
@ -247,7 +247,7 @@ DiskFilesOrDirectories
if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part)) if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part))
return matches.GetSize(); return matches.GetSize();
} }
#endif // #if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
// Okay, containing_part is now the directory we want to open and look for files: // Okay, containing_part is now the directory we want to open and look for files:

View File

@ -16,7 +16,7 @@
#include <fstream> #include <fstream>
#include "lldb/Host/Config.h" // Have to include this before we test the define... #include "lldb/Host/Config.h" // Have to include this before we test the define...
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
#include <pwd.h> #include <pwd.h>
#endif #endif
@ -44,7 +44,7 @@ GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr)
return false; return false;
} }
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
static const char* static const char*
GetCachedGlobTildeSlash() GetCachedGlobTildeSlash()
@ -134,7 +134,7 @@ FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len)
else else
return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder); return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder);
} }
#endif // #if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
size_t size_t
FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len) FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len)
@ -144,7 +144,7 @@ FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len)
// Glob if needed for ~/, otherwise copy in case src_path is same as dst_path... // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path...
char unglobbed_path[PATH_MAX]; char unglobbed_path[PATH_MAX];
#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
if (src_path[0] == '~') if (src_path[0] == '~')
{ {
size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path)); size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path));
@ -155,7 +155,7 @@ FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len)
::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path); ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
} }
else else
#endif // LLDB_CONFIG_TILDE_RESOLVES_TO_USER #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
{ {
::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path); ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
} }

View File

@ -95,7 +95,11 @@ LoadPluginCallback
else else
{ {
PluginInfo plugin_info = { NULL, NULL, NULL }; PluginInfo plugin_info = { NULL, NULL, NULL };
plugin_info.plugin_handle = Host::DynamicLibraryOpen (plugin_file_spec, error); uint32_t flags = Host::eDynamicLibraryOpenOptionLazy |
Host::eDynamicLibraryOpenOptionLocal |
Host::eDynamicLibraryOpenOptionLimitGetSymbol;
plugin_info.plugin_handle = Host::DynamicLibraryOpen (plugin_file_spec, flags, error);
if (plugin_info.plugin_handle) if (plugin_info.plugin_handle)
{ {
bool success = false; bool success = false;

View File

@ -98,10 +98,10 @@ StreamFile::Open (const char *path, const char *permissions)
void void
StreamFile::SetLineBuffered () StreamFile::SetLineBuffered ()
{ {
#if LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED
if (m_file != NULL) if (m_file != NULL)
setlinebuf (m_file); setlinebuf (m_file);
#endif // #if LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED #endif // #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED
} }
void void

View File

@ -14,6 +14,7 @@
#include "lldb/Core/FileSpec.h" #include "lldb/Core/FileSpec.h"
#include "lldb/Core/Log.h" #include "lldb/Core/Log.h"
#include "lldb/Core/StreamString.h" #include "lldb/Core/StreamString.h"
#include "lldb/Host/Config.h"
#include "lldb/Host/Endian.h" #include "lldb/Host/Endian.h"
#include "lldb/Host/Mutex.h" #include "lldb/Host/Mutex.h"
@ -21,12 +22,16 @@
#include <errno.h> #include <errno.h>
#if defined (__APPLE__) #if defined (__APPLE__)
#include <dispatch/dispatch.h> #include <dispatch/dispatch.h>
#include <libproc.h> #include <libproc.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#elif defined (__linux__) #elif defined (__linux__)
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
using namespace lldb; using namespace lldb;
@ -643,21 +648,48 @@ Host::ResolveExecutableInBundle (FileSpec &file)
} }
#endif #endif
void * // Opaque info that tracks a dynamic library that was loaded
Host::DynamicLibraryOpen (const FileSpec &file_spec, Error &error) struct DynamicLibraryInfo
{
DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
file_spec (fs),
open_options (o),
handle (h)
{
}
const FileSpec file_spec;
uint32_t open_options;
void * handle;
};
void *
Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
{ {
void *dynamic_library_handle = NULL;
char path[PATH_MAX]; char path[PATH_MAX];
if (file_spec.GetPath(path, sizeof(path))) if (file_spec.GetPath(path, sizeof(path)))
{ {
#if defined (__linux__) int mode = 0;
dynamic_library_handle = ::dlopen (path, RTLD_LAZY | RTLD_GLOBAL);
#else if (options & eDynamicLibraryOpenOptionLazy)
dynamic_library_handle = ::dlopen (path, RTLD_LAZY | RTLD_GLOBAL | RTLD_FIRST); mode |= RTLD_LAZY;
if (options & eDynamicLibraryOpenOptionLocal)
mode |= RTLD_LOCAL;
else
mode |= RTLD_GLOBAL;
#ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
mode |= RTLD_FIRST;
#endif #endif
if (dynamic_library_handle)
void * opaque = ::dlopen (path, mode);
if (opaque)
{ {
error.Clear(); error.Clear();
return new DynamicLibraryInfo (file_spec, options, opaque);
} }
else else
{ {
@ -668,40 +700,73 @@ Host::DynamicLibraryOpen (const FileSpec &file_spec, Error &error)
{ {
error.SetErrorString("failed to extract path"); error.SetErrorString("failed to extract path");
} }
return NULL;
return dynamic_library_handle;
} }
Error Error
Host::DynamicLibraryClose (void *dynamic_library_handle) Host::DynamicLibraryClose (void *opaque)
{ {
Error error; Error error;
if (dynamic_library_handle == NULL) if (opaque == NULL)
{ {
error.SetErrorString ("invalid dynamic library handle"); error.SetErrorString ("invalid dynamic library handle");
} }
else if (::dlclose(dynamic_library_handle) != 0) else
{
DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
if (::dlclose (dylib_info->handle) != 0)
{ {
error.SetErrorString(::dlerror()); error.SetErrorString(::dlerror());
} }
dylib_info->open_options = 0;
dylib_info->handle = 0;
delete dylib_info;
}
return error; return error;
} }
void * void *
Host::DynamicLibraryGetSymbol (void *dynamic_library_handle, const char *symbol_name, Error &error) Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
{ {
if (dynamic_library_handle == NULL) if (opaque == NULL)
{ {
error.SetErrorString ("invalid dynamic library handle"); error.SetErrorString ("invalid dynamic library handle");
}
else
{
DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
if (symbol_addr)
{
#ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
// This host doesn't support limiting searches to this shared library
// so we need to verify that the match came from this shared library
// if it was requested in the Host::DynamicLibraryOpen() function.
if (dylib_info->options & eDynamicLibraryOpenOptionLimitGetSymbol)
{
FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
if (match_dylib_spec != dylib_info->file_spec)
{
char dylib_path[PATH_MAX];
if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
else
error.SetErrorString ("symbol not found");
return NULL; return NULL;
} }
}
void *symbol_addr = ::dlsym (dynamic_library_handle, symbol_name); #endif
if (symbol_addr == NULL)
error.SetErrorString(::dlerror());
else
error.Clear(); error.Clear();
return symbol_addr; return symbol_addr;
}
else
{
error.SetErrorString(::dlerror());
}
}
return NULL;
} }
bool bool

View File

@ -14,7 +14,7 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#if LLDB_CONFIG_TERMIOS_SUPPORTED #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
#include <termios.h> #include <termios.h>
#endif #endif
@ -33,7 +33,7 @@ Terminal::SetEcho (bool enabled)
{ {
if (FileDescriptorIsValid()) if (FileDescriptorIsValid())
{ {
#if LLDB_CONFIG_TERMIOS_SUPPORTED #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (IsATerminal ()) if (IsATerminal ())
{ {
struct termios fd_termios; struct termios fd_termios;
@ -60,7 +60,7 @@ Terminal::SetEcho (bool enabled)
return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0; return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
} }
} }
#endif #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
} }
return false; return false;
} }
@ -70,7 +70,7 @@ Terminal::SetCanonical (bool enabled)
{ {
if (FileDescriptorIsValid()) if (FileDescriptorIsValid())
{ {
#if LLDB_CONFIG_TERMIOS_SUPPORTED #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (IsATerminal ()) if (IsATerminal ())
{ {
struct termios fd_termios; struct termios fd_termios;
@ -97,7 +97,7 @@ Terminal::SetCanonical (bool enabled)
return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0; return ::tcsetattr (m_fd, TCSANOW, &fd_termios) == 0;
} }
} }
#endif #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
} }
return false; return false;
} }
@ -132,13 +132,13 @@ TerminalState::Save (int fd, bool save_process_group)
if (m_tty.IsATerminal()) if (m_tty.IsATerminal())
{ {
m_tflags = ::fcntl (fd, F_GETFL, 0); m_tflags = ::fcntl (fd, F_GETFL, 0);
#if LLDB_CONFIG_TERMIOS_SUPPORTED #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (m_termios_ap.get() == NULL) if (m_termios_ap.get() == NULL)
m_termios_ap.reset (new struct termios); m_termios_ap.reset (new struct termios);
int err = ::tcgetattr (fd, m_termios_ap.get()); int err = ::tcgetattr (fd, m_termios_ap.get());
if (err != 0) if (err != 0)
m_termios_ap.reset(); m_termios_ap.reset();
#endif // #if LLDB_CONFIG_TERMIOS_SUPPORTED #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (save_process_group) if (save_process_group)
m_process_group = ::tcgetpgrp (0); m_process_group = ::tcgetpgrp (0);
else else
@ -168,10 +168,10 @@ TerminalState::Restore () const
if (TFlagsIsValid()) if (TFlagsIsValid())
result = fcntl (fd, F_SETFL, m_tflags); result = fcntl (fd, F_SETFL, m_tflags);
#if LLDB_CONFIG_TERMIOS_SUPPORTED #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (TTYStateIsValid()) if (TTYStateIsValid())
result = tcsetattr (fd, TCSANOW, m_termios_ap.get()); result = tcsetattr (fd, TCSANOW, m_termios_ap.get());
#endif // #if LLDB_CONFIG_TERMIOS_SUPPORTED #endif // #ifdef LLDB_CONFIG_TERMIOS_SUPPORTED
if (ProcessGroupIsValid()) if (ProcessGroupIsValid())
{ {