diff options
Diffstat (limited to 'source/Host/android')
-rw-r--r-- | source/Host/android/HostInfoAndroid.cpp | 104 | ||||
-rw-r--r-- | source/Host/android/LibcGlue.cpp | 40 | ||||
-rw-r--r-- | source/Host/android/ProcessLauncherAndroid.cpp | 108 |
3 files changed, 252 insertions, 0 deletions
diff --git a/source/Host/android/HostInfoAndroid.cpp b/source/Host/android/HostInfoAndroid.cpp new file mode 100644 index 0000000000000..3fa50ec8ddfd7 --- /dev/null +++ b/source/Host/android/HostInfoAndroid.cpp @@ -0,0 +1,104 @@ +//===-- HostInfoAndroid.cpp -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/android/HostInfoAndroid.h" +#include "lldb/Host/linux/HostInfoLinux.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" + +using namespace lldb_private; +using namespace llvm; + +void +HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64) +{ + HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64); + + if (arch_32.IsValid()) + { + arch_32.GetTriple().setEnvironment(llvm::Triple::Android); + } + if (arch_64.IsValid()) + { + arch_64.GetTriple().setEnvironment(llvm::Triple::Android); + } +} + +FileSpec +HostInfoAndroid::GetDefaultShell() +{ + return FileSpec("/system/bin/sh", false); +} + +FileSpec +HostInfoAndroid::ResolveLibraryPath(const std::string& module_path, const ArchSpec& arch) +{ + static const char* const ld_library_path_separator = ":"; + static const char* const default_lib32_path[] = { + "/vendor/lib", + "/system/lib", + nullptr + }; + static const char* const default_lib64_path[] = { + "/vendor/lib64", + "/system/lib64", + nullptr + }; + + if (module_path.empty() || module_path[0] == '/') + return FileSpec(module_path.c_str(), true); + + SmallVector<StringRef, 4> ld_paths; + + if (const char* ld_library_path = ::getenv("LD_LIBRARY_PATH")) + StringRef(ld_library_path).split(ld_paths, StringRef(ld_library_path_separator), -1, false); + + const char* const* default_lib_path = nullptr; + switch (arch.GetAddressByteSize()) + { + case 4: + default_lib_path = default_lib32_path; + break; + case 8: + default_lib_path = default_lib64_path; + break; + default: + assert(false && "Unknown address byte size"); + return FileSpec(); + } + + for(const char* const* it = default_lib_path; *it; ++it) + ld_paths.push_back(StringRef(*it)); + + for (const StringRef& path : ld_paths) + { + FileSpec file_candidate(path.str().c_str(), true); + file_candidate.AppendPathComponent(module_path.c_str()); + + if (file_candidate.Exists()) + return file_candidate; + } + + return FileSpec(); +} + +bool +HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) +{ + bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec); + + // On Android, there is no path which is guaranteed to be writable. If the user has not + // provided a path via an environment variable, the generic algorithm will deduce /tmp, which + // is plain wrong. In that case we have an invalid directory, we substitute the path with + // /data/local/tmp, which is correct at least in some cases (i.e., when running as shell user). + if (!success || !file_spec.Exists()) + file_spec = FileSpec("/data/local/tmp", false); + + return file_spec.Exists(); +} diff --git a/source/Host/android/LibcGlue.cpp b/source/Host/android/LibcGlue.cpp new file mode 100644 index 0000000000000..3842fb6c2a8ed --- /dev/null +++ b/source/Host/android/LibcGlue.cpp @@ -0,0 +1,40 @@ +//===-- LibcGlue.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// This files adds functions missing from libc on earlier versions of Android + +#include <android/api-level.h> + +#include <sys/syscall.h> + +#if __ANDROID_API__ < 21 + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <signal.h> + +#include "lldb/Host/Time.h" + +time_t timegm(struct tm* t) +{ + return (time_t) timegm64(t); +} + +int signalfd (int fd, const sigset_t *mask, int flags) +{ + return syscall(__NR_signalfd4, fd, mask, _NSIG / 8, flags); +} + +int posix_openpt(int flags) +{ + return open("/dev/ptmx", flags); +} + +#endif diff --git a/source/Host/android/ProcessLauncherAndroid.cpp b/source/Host/android/ProcessLauncherAndroid.cpp new file mode 100644 index 0000000000000..24eebc8c030f0 --- /dev/null +++ b/source/Host/android/ProcessLauncherAndroid.cpp @@ -0,0 +1,108 @@ +//===-- ProcessLauncherAndroid.cpp ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/FileSpec.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/HostProcess.h" +#include "lldb/Host/android/ProcessLauncherAndroid.h" + +#include "lldb/Target/ProcessLaunchInfo.h" + +#include <limits.h> + +using namespace lldb; +using namespace lldb_private; + +static bool +DupDescriptor(const FileSpec &file_spec, int fd, int flags) +{ + int target_fd = ::open(file_spec.GetCString(), flags, 0666); + + if (target_fd == -1) + return false; + + if (::dup2(target_fd, fd) == -1) + return false; + + return (::close(target_fd) == -1) ? false : true; +} + +// If there is no PATH variable specified inside the environment then set the path to /system/bin. +// It is required because the default path used by execve() is wrong on android. +static void +FixupEnvironment(Args& env) +{ + static const char* path = "PATH="; + static const int path_len = ::strlen(path); + for (const char** args = env.GetConstArgumentVector(); *args; ++args) + if (::strncmp(path, *args, path_len) == 0) + return; + env.AppendArgument("PATH=/system/bin"); +} + +HostProcess +ProcessLauncherAndroid::LaunchProcess(const ProcessLaunchInfo &launch_info, Error &error) +{ + // TODO: Handle other launch parameters specified in launc_info + + char exe_path[PATH_MAX]; + launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path)); + + lldb::pid_t pid = ::fork(); + if (pid == static_cast<lldb::pid_t>(-1)) + { + // Fork failed + error.SetErrorStringWithFormat("Fork failed with error message: %s", strerror(errno)); + return HostProcess(LLDB_INVALID_PROCESS_ID); + } + else if (pid == 0) + { + if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDIN_FILENO)) { + FileSpec file_spec = file_action->GetFileSpec(); + if (file_spec) + if (!DupDescriptor(file_spec, STDIN_FILENO, O_RDONLY)) + exit(-1); + } + + if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDOUT_FILENO)) { + FileSpec file_spec = file_action->GetFileSpec(); + if (file_spec) + if (!DupDescriptor(file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) + exit(-1); + } + + if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDERR_FILENO)) { + FileSpec file_spec = file_action->GetFileSpec(); + if (file_spec) + if (!DupDescriptor(file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) + exit(-1); + } + + // Child process + const char **argv = launch_info.GetArguments().GetConstArgumentVector(); + + Args env = launch_info.GetEnvironmentEntries(); + FixupEnvironment(env); + const char **envp = env.GetConstArgumentVector(); + + FileSpec working_dir = launch_info.GetWorkingDirectory(); + if (working_dir) + { + if (::chdir(working_dir.GetCString()) != 0) + exit(-1); + } + + execve(argv[0], + const_cast<char *const *>(argv), + const_cast<char *const *>(envp)); + exit(-1); + } + + return HostProcess(pid); +} |