diff options
| author | Greg Clayton <gclayton@apple.com> | 2010-10-17 22:03:32 +0000 |
|---|---|---|
| committer | Greg Clayton <gclayton@apple.com> | 2010-10-17 22:03:32 +0000 |
| commit | dd36defda7a1feb8f60478810adc897e7a15c00b (patch) | |
| tree | 519d79f27a94b37b0580a05a3195c38179cf1038 /lldb/source/Host | |
| parent | 95b6f045f1f104b96d443c404755c2757b6f6cf7 (diff) | |
| download | bcm5719-llvm-dd36defda7a1feb8f60478810adc897e7a15c00b.tar.gz bcm5719-llvm-dd36defda7a1feb8f60478810adc897e7a15c00b.zip | |
Added a new Host call to find LLDB related paths:
static bool
Host::GetLLDBPath (lldb::PathType path_type, FileSpec &file_spec);
This will fill in "file_spec" with an appropriate path that is appropriate
for the current Host OS. MacOSX will return paths within the LLDB.framework,
and other unixes will return the paths they want. The current PathType
enums are:
typedef enum PathType
{
ePathTypeLLDBShlibDir, // The directory where the lldb.so (unix) or LLDB mach-o file in LLDB.framework (MacOSX) exists
ePathTypeSupportExecutableDir, // Find LLDB support executable directory (debugserver, etc)
ePathTypeHeaderDir, // Find LLDB header file directory
ePathTypePythonDir // Find Python modules (PYTHONPATH) directory
} PathType;
All places that were finding executables are and python paths are now updated
to use this Host call.
Added another new host call to launch the inferior in a terminal. This ability
will be very host specific and doesn't need to be supported on all systems.
MacOSX currently will create a new .command file and tell Terminal.app to open
the .command file. It also uses the new "darwin-debug" app which is a small
app that uses posix to exec (no fork) and stop at the entry point of the
program. The GDB remote plug-in is almost able launch a process and attach to
it, it currently will spawn the process, but it won't attach to it just yet.
This will let LLDB not have to share the terminal with another process and a
new terminal window will pop up when you launch. This won't get hooked up
until we work out all of the kinks. The new Host function is:
static lldb::pid_t
Host::LaunchInNewTerminal (
const char **argv, // argv[0] is executable
const char **envp,
const ArchSpec *arch_spec,
bool stop_at_entry,
bool disable_aslr);
Cleaned up FileSpec::GetPath to not use strncpy() as it was always zero
filling the entire path buffer.
Fixed an issue with the dynamic checker function where I missed a '$' prefix
that should have been added.
llvm-svn: 116690
Diffstat (limited to 'lldb/source/Host')
| -rw-r--r-- | lldb/source/Host/common/Host.cpp | 153 | ||||
| -rw-r--r-- | lldb/source/Host/macosx/Host.mm | 156 | ||||
| -rw-r--r-- | lldb/source/Host/macosx/cfcpp/CFCMutableArray.cpp | 43 | ||||
| -rw-r--r-- | lldb/source/Host/macosx/cfcpp/CFCMutableArray.h | 5 |
4 files changed, 339 insertions, 18 deletions
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index 16e1ea9459a..ea96e65f6cb 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -626,12 +626,140 @@ Host::GetModuleFileSpecForHostAddress (const void *host_addr) #if !defined (__APPLE__) // see Host.mm bool -Host::ResolveExecutableInBundle (FileSpec *file) +Host::ResolveExecutableInBundle (FileSpec &file) { - return false; + return false; } #endif + +bool +Host::GetLLDBPath (PathType path_type, FileSpec &file_spec) +{ + // To get paths related to LLDB we get the path to the exectuable that + // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB", + // on linux this is assumed to be the "lldb" main executable. If LLDB on + // linux is actually in a shared library (lldb.so??) then this function will + // need to be modified to "do the right thing". + + switch (path_type) + { + case ePathTypeLLDBShlibDir: + { + static ConstString g_lldb_so_dir; + if (!g_lldb_so_dir) + { + FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath)); + g_lldb_so_dir = lldb_file_spec.GetDirectory(); + } + file_spec.GetDirectory() = g_lldb_so_dir; + return file_spec.GetDirectory(); + } + break; + + case ePathTypeSupportExecutableDir: + { + static ConstString g_lldb_support_exe_dir; + if (!g_lldb_support_exe_dir) + { + FileSpec lldb_file_spec; + if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) + { + char raw_path[PATH_MAX]; + char resolved_path[PATH_MAX]; + lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + +#if defined (__APPLE__) + char *framework_pos = ::strstr (raw_path, "LLDB.framework"); + if (framework_pos) + { + framework_pos += strlen("LLDB.framework"); + ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); + } +#endif + FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); + g_lldb_support_exe_dir.SetCString(resolved_path); + } + } + file_spec.GetDirectory() = g_lldb_support_exe_dir; + return file_spec.GetDirectory(); + } + break; + + case ePathTypeHeaderDir: + { + static ConstString g_lldb_headers_dir; + if (!g_lldb_headers_dir) + { +#if defined (__APPLE__) + FileSpec lldb_file_spec; + if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) + { + char raw_path[PATH_MAX]; + char resolved_path[PATH_MAX]; + lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + + char *framework_pos = ::strstr (raw_path, "LLDB.framework"); + if (framework_pos) + { + framework_pos += strlen("LLDB.framework"); + ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); + } + FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); + g_lldb_headers_dir.SetCString(resolved_path); + } +#else + // TODO: Anyone know how we can determine this for linux?? + g_lldb_headers_dir.SetCString ("/opt/local/include/lldb"); +#endif + } + file_spec.GetDirectory() = g_lldb_headers_dir; + return file_spec.GetDirectory(); + } + break; + + case ePathTypePythonDir: + { + // TODO: Anyone know how we can determine this for linux?? + // For linux we are currently assuming the location of the lldb + // binary that contains this function is the directory that will + // contain lldb.so, lldb.py and embedded_interpreter.py... + + static ConstString g_lldb_python_dir; + if (!g_lldb_python_dir) + { + FileSpec lldb_file_spec; + if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) + { + char raw_path[PATH_MAX]; + char resolved_path[PATH_MAX]; + lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + +#if defined (__APPLE__) + char *framework_pos = ::strstr (raw_path, "LLDB.framework"); + if (framework_pos) + { + framework_pos += strlen("LLDB.framework"); + ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); + } +#endif + FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); + g_lldb_python_dir.SetCString(resolved_path); + } + } + file_spec.GetDirectory() = g_lldb_python_dir; + return file_spec.GetDirectory(); + } + break; + + default: + assert (!"Unhandled PathType"); + break; + } + + return false; +} + uint32_t Host::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) { @@ -727,4 +855,25 @@ Host::OpenFileInExternalEditor (FileSpec &file_spec, uint32_t line_no) { return false; } + + +lldb::pid_t +LaunchApplication (const FileSpec &app_file_spec) +{ + return LLDB_INVALID_PROCESS_ID; +} + +lldb::pid_t +Host::LaunchInNewTerminal +( + const char **argv, + const char **envp, + const ArchSpec *arch_spec, + bool stop_at_entry, + bool disable_aslr +) +{ + return LLDB_INVALID_PROCESS_ID; +} + #endif diff --git a/lldb/source/Host/macosx/Host.mm b/lldb/source/Host/macosx/Host.mm index e3379d07cca..298aec4029c 100644 --- a/lldb/source/Host/macosx/Host.mm +++ b/lldb/source/Host/macosx/Host.mm @@ -8,10 +8,18 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/Host.h" + +#include <sys/types.h> +#include <sys/stat.h> + +#include "lldb/Core/ArchSpec.h" #include "lldb/Core/FileSpec.h" #include "lldb/Core/Log.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/StreamString.h" #include "cfcpp/CFCBundle.h" +#include "cfcpp/CFCMutableArray.h" #include "cfcpp/CFCMutableDictionary.h" #include "cfcpp/CFCReleaser.h" #include "cfcpp/CFCString.h" @@ -90,26 +98,26 @@ Host::ThreadCreated (const char *thread_name) bool -Host::ResolveExecutableInBundle (FileSpec *file) +Host::ResolveExecutableInBundle (FileSpec &file) { #if defined (__APPLE__) - if (file->GetFileType () == FileSpec::eFileTypeDirectory) - { - char path[PATH_MAX]; - if (file->GetPath(path, sizeof(path))) + if (file.GetFileType () == FileSpec::eFileTypeDirectory) { - CFCBundle bundle (path); - CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL ()); - if (url.get()) - { - if (::CFURLGetFileSystemRepresentation (url.get(), YES, (UInt8*)path, sizeof(path))) + char path[PATH_MAX]; + if (file.GetPath(path, sizeof(path))) { - file->SetFile(path); - return true; + CFCBundle bundle (path); + CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL ()); + if (url.get()) + { + if (::CFURLGetFileSystemRepresentation (url.get(), YES, (UInt8*)path, sizeof(path))) + { + file.SetFile(path); + return true; + } + } } - } } - } #endif return false; } @@ -124,8 +132,8 @@ Host::LaunchApplication (const FileSpec &app_file_spec) ::bzero (&app_params, sizeof (app_params)); app_params.flags = kLSLaunchDefaults | kLSLaunchDontAddToRecents | - kLSLaunchDontSwitch | - kLSLaunchNewInstance;// | 0x00001000; + kLSLaunchNewInstance; + FSRef app_fsref; CFCString app_cfstr (app_path, kCFStringEncodingUTF8); @@ -150,6 +158,122 @@ Host::LaunchApplication (const FileSpec &app_file_spec) return pid; } +lldb::pid_t +Host::LaunchInNewTerminal +( + const char **argv, + const char **envp, + const ArchSpec *arch_spec, + bool stop_at_entry, + bool disable_aslr +) +{ + if (!argv || !argv[0]) + return LLDB_INVALID_PROCESS_ID; + + OSStatus error = 0; + + FileSpec program (argv[0]); + + + char temp_file_path[PATH_MAX]; + const char *tmpdir = ::getenv ("TMPDIR"); + if (tmpdir == NULL) + tmpdir = "/tmp/"; + ::snprintf (temp_file_path, sizeof(temp_file_path), "%s%s-XXXXXX", tmpdir, program.GetFilename().AsCString()); + + if (::mktemp (temp_file_path) == NULL) + return LLDB_INVALID_PROCESS_ID; + + ::strncat (temp_file_path, ".command", sizeof (temp_file_path)); + + StreamFile command_file (temp_file_path, "w"); + + FileSpec darwin_debug_file_spec; + if (!Host::GetLLDBPath (ePathTypeSupportExecutableDir, darwin_debug_file_spec)) + return LLDB_INVALID_PROCESS_ID; + darwin_debug_file_spec.GetFilename().SetCString("darwin-debug"); + + if (!darwin_debug_file_spec.Exists()) + return LLDB_INVALID_PROCESS_ID; + + char launcher_path[PATH_MAX]; + darwin_debug_file_spec.GetPath(launcher_path, sizeof(launcher_path)); + command_file.Printf("\"%s\" ", launcher_path); + + if (arch_spec && arch_spec->IsValid()) + { + command_file.Printf("--arch=%s ", arch_spec->AsCString()); + } + + if (disable_aslr) + { + command_file.PutCString("--disable-aslr "); + } + + command_file.PutCString("-- "); + + if (argv) + { + for (size_t i=0; argv[i] != NULL; ++i) + { + command_file.Printf("\"%s\" ", argv[i]); + } + } + command_file.EOL(); + command_file.Close(); + if (::chmod (temp_file_path, S_IRWXU | S_IRWXG) != 0) + return LLDB_INVALID_PROCESS_ID; + + CFCMutableDictionary cf_env_dict; + + const bool can_create = true; + if (envp) + { + for (size_t i=0; envp[i] != NULL; ++i) + { + const char *env_entry = envp[i]; + const char *equal_pos = strchr(env_entry, '='); + if (equal_pos) + { + std::string env_key (env_entry, equal_pos); + std::string env_val (equal_pos + 1); + CFCString cf_env_key (env_key.c_str(), kCFStringEncodingUTF8); + CFCString cf_env_val (env_val.c_str(), kCFStringEncodingUTF8); + cf_env_dict.AddValue (cf_env_key.get(), cf_env_val.get(), can_create); + } + } + } + + LSApplicationParameters app_params; + ::bzero (&app_params, sizeof (app_params)); + app_params.flags = kLSLaunchDontAddToRecents | kLSLaunchAsync; + app_params.argv = NULL; + app_params.environment = (CFDictionaryRef)cf_env_dict.get(); + + CFCReleaser<CFURLRef> command_file_url (::CFURLCreateFromFileSystemRepresentation (NULL, + (const UInt8 *)temp_file_path, + strlen (temp_file_path), + false)); + + CFCMutableArray urls; + + // Terminal.app will open the ".command" file we have created + // and run our process inside it which will wait at the entry point + // for us to attach. + urls.AppendValue(command_file_url.get()); + + ProcessSerialNumber psn; + error = LSOpenURLsWithRole(urls.get(), kLSRolesShell, NULL, &app_params, &psn, 1); + + if (error != noErr) + return LLDB_INVALID_PROCESS_ID; + + ::pid_t pid = LLDB_INVALID_PROCESS_ID; + error = ::GetProcessPID(&psn, &pid); + return pid; +} + bool Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) { diff --git a/lldb/source/Host/macosx/cfcpp/CFCMutableArray.cpp b/lldb/source/Host/macosx/cfcpp/CFCMutableArray.cpp index 3b92f03fa61..c3c0a11193a 100644 --- a/lldb/source/Host/macosx/cfcpp/CFCMutableArray.cpp +++ b/lldb/source/Host/macosx/cfcpp/CFCMutableArray.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "CFCMutableArray.h" +#include "CFCString.h" //---------------------------------------------------------------------- // CFCString constructor @@ -121,3 +122,45 @@ CFCMutableArray::AppendValue(const void *value, bool can_create) } return false; } + + +bool +CFCMutableArray::AppendCStringAsCFString (const char *s, CFStringEncoding encoding, bool can_create) +{ + CFMutableArrayRef array = get(); + if (array == NULL) + { + if (can_create == false) + return false; + array = ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); + reset ( array ); + } + if (array != NULL) + { + CFCString cf_str (s, encoding); + ::CFArrayAppendValue (array, cf_str.get()); + return true; + } + return false; +} + +bool +CFCMutableArray::AppendFileSystemRepresentationAsCFString (const char *s, bool can_create) +{ + CFMutableArrayRef array = get(); + if (array == NULL) + { + if (can_create == false) + return false; + array = ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); + reset ( array ); + } + if (array != NULL) + { + CFCString cf_path; + cf_path.SetFileSystemRepresentation(s); + ::CFArrayAppendValue (array, cf_path.get()); + return true; + } + return false; +} diff --git a/lldb/source/Host/macosx/cfcpp/CFCMutableArray.h b/lldb/source/Host/macosx/cfcpp/CFCMutableArray.h index eaadb8d5590..f78cd92ffab 100644 --- a/lldb/source/Host/macosx/cfcpp/CFCMutableArray.h +++ b/lldb/source/Host/macosx/cfcpp/CFCMutableArray.h @@ -29,6 +29,11 @@ public: const void * GetValueAtIndex(CFIndex idx) const; bool SetValueAtIndex(CFIndex idx, const void *value); bool AppendValue(const void *value, bool can_create = true); // Appends value and optionally creates a CFCMutableArray if this class doesn't contain one + bool AppendCStringAsCFString (const char *cstr, + CFStringEncoding encoding = kCFStringEncodingUTF8, + bool can_create = true); + bool AppendFileSystemRepresentationAsCFString (const char *s, + bool can_create = true); }; #endif // #ifndef CoreFoundationCPP_CFMutableArray_h_ |

