Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,127 @@
//===-- DomainSocket.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/posix/DomainSocket.h"
#include "llvm/Support/FileSystem.h"
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
using namespace lldb;
using namespace lldb_private;
#ifdef __ANDROID__
// Android does not have SUN_LEN
#ifndef SUN_LEN
#define SUN_LEN(ptr) \
(offsetof(struct sockaddr_un, sun_path) + strlen((ptr)->sun_path))
#endif
#endif // #ifdef __ANDROID__
namespace {
const int kDomain = AF_UNIX;
const int kType = SOCK_STREAM;
bool SetSockAddr(llvm::StringRef name, const size_t name_offset,
sockaddr_un *saddr_un, socklen_t &saddr_un_len) {
if (name.size() + name_offset > sizeof(saddr_un->sun_path))
return false;
memset(saddr_un, 0, sizeof(*saddr_un));
saddr_un->sun_family = kDomain;
memcpy(saddr_un->sun_path + name_offset, name.data(), name.size());
// For domain sockets we can use SUN_LEN in order to calculate size of
// sockaddr_un, but for abstract sockets we have to calculate size manually
// because of leading null symbol.
if (name_offset == 0)
saddr_un_len = SUN_LEN(saddr_un);
else
saddr_un_len =
offsetof(struct sockaddr_un, sun_path) + name_offset + name.size();
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
saddr_un->sun_len = saddr_un_len;
#endif
return true;
}
} // namespace
DomainSocket::DomainSocket(bool should_close, bool child_processes_inherit)
: Socket(ProtocolUnixDomain, should_close, child_processes_inherit) {}
DomainSocket::DomainSocket(SocketProtocol protocol,
bool child_processes_inherit)
: Socket(protocol, true, child_processes_inherit) {}
DomainSocket::DomainSocket(NativeSocket socket,
const DomainSocket &listen_socket)
: Socket(ProtocolUnixDomain, listen_socket.m_should_close_fd,
listen_socket.m_child_processes_inherit) {
m_socket = socket;
}
Status DomainSocket::Connect(llvm::StringRef name) {
sockaddr_un saddr_un;
socklen_t saddr_un_len;
if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
return Status("Failed to set socket address");
Status error;
m_socket = CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error);
if (error.Fail())
return error;
if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) <
0)
SetLastError(error);
return error;
}
Status DomainSocket::Listen(llvm::StringRef name, int backlog) {
sockaddr_un saddr_un;
socklen_t saddr_un_len;
if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
return Status("Failed to set socket address");
DeleteSocketFile(name);
Status error;
m_socket = CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error);
if (error.Fail())
return error;
if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) ==
0)
if (::listen(GetNativeSocket(), backlog) == 0)
return error;
SetLastError(error);
return error;
}
Status DomainSocket::Accept(Socket *&socket) {
Status error;
auto conn_fd = AcceptSocket(GetNativeSocket(), nullptr, nullptr,
m_child_processes_inherit, error);
if (error.Success())
socket = new DomainSocket(conn_fd, *this);
return error;
}
size_t DomainSocket::GetNameOffset() const { return 0; }
void DomainSocket::DeleteSocketFile(llvm::StringRef name) {
llvm::sys::fs::remove(name);
}

View File

@@ -0,0 +1,80 @@
//===-- FileSystem.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/FileSystem.h"
// C includes
#include <dirent.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef __linux__
#include <linux/magic.h>
#include <sys/mount.h>
#include <sys/statfs.h>
#endif
#if defined(__NetBSD__)
#include <sys/statvfs.h>
#endif
// lldb Includes
#include "lldb/Host/Host.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/Support/FileSystem.h"
using namespace lldb;
using namespace lldb_private;
const char *FileSystem::DEV_NULL = "/dev/null";
Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
Status error;
if (::symlink(dst.GetCString(), src.GetCString()) == -1)
error.SetErrorToErrno();
return error;
}
Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
Status error;
char buf[PATH_MAX];
ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
if (count < 0)
error.SetErrorToErrno();
else {
buf[count] = '\0'; // Success
dst.SetFile(buf, false);
}
return error;
}
Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
char resolved_path[PATH_MAX];
if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
return Status("Couldn't get the canonical path for %s", src.GetCString());
}
char real_path[PATH_MAX + 1];
if (realpath(resolved_path, real_path) == nullptr) {
Status err;
err.SetErrorToErrno();
return err;
}
dst = FileSpec(real_path, false);
return Status();
}
FILE *FileSystem::Fopen(const char *path, const char *mode) {
return ::fopen(path, mode);
}

View File

@@ -0,0 +1,232 @@
//===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#if !defined(LLDB_DISABLE_PYTHON)
#include "Plugins/ScriptInterpreter/Python/lldb-python.h"
#endif
#include "lldb/Host/posix/HostInfoPosix.h"
#include "lldb/Utility/Log.h"
#include "clang/Basic/Version.h"
#include "clang/Config/config.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <grp.h>
#include <limits.h>
#include <mutex>
#include <netdb.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
using namespace lldb_private;
size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
bool HostInfoPosix::GetHostname(std::string &s) {
char hostname[PATH_MAX];
hostname[sizeof(hostname) - 1] = '\0';
if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
struct hostent *h = ::gethostbyname(hostname);
if (h)
s.assign(h->h_name);
else
s.assign(hostname);
return true;
}
return false;
}
#ifdef __ANDROID__
#include <android/api-level.h>
#endif
#if defined(__ANDROID_API__) && __ANDROID_API__ < 21
#define USE_GETPWUID
#endif
#ifdef USE_GETPWUID
static std::mutex s_getpwuid_lock;
#endif
const char *HostInfoPosix::LookupUserName(uint32_t uid,
std::string &user_name) {
#ifdef USE_GETPWUID
// getpwuid_r is missing from android-9
// make getpwuid thread safe with a mutex
std::lock_guard<std::mutex> lock(s_getpwuid_lock);
struct passwd *user_info_ptr = ::getpwuid(uid);
if (user_info_ptr) {
user_name.assign(user_info_ptr->pw_name);
return user_name.c_str();
}
#else
struct passwd user_info;
struct passwd *user_info_ptr = &user_info;
char user_buffer[PATH_MAX];
size_t user_buffer_size = sizeof(user_buffer);
if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
&user_info_ptr) == 0) {
if (user_info_ptr) {
user_name.assign(user_info_ptr->pw_name);
return user_name.c_str();
}
}
#endif
user_name.clear();
return nullptr;
}
const char *HostInfoPosix::LookupGroupName(uint32_t gid,
std::string &group_name) {
#ifndef __ANDROID__
char group_buffer[PATH_MAX];
size_t group_buffer_size = sizeof(group_buffer);
struct group group_info;
struct group *group_info_ptr = &group_info;
// Try the threadsafe version first
if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
&group_info_ptr) == 0) {
if (group_info_ptr) {
group_name.assign(group_info_ptr->gr_name);
return group_name.c_str();
}
} else {
// The threadsafe version isn't currently working for me on darwin, but the
// non-threadsafe version
// is, so I am calling it below.
group_info_ptr = ::getgrgid(gid);
if (group_info_ptr) {
group_name.assign(group_info_ptr->gr_name);
return group_name.c_str();
}
}
group_name.clear();
#else
assert(false && "getgrgid_r() not supported on Android");
#endif
return NULL;
}
uint32_t HostInfoPosix::GetUserID() { return getuid(); }
uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh", false); }
bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec,
llvm::StringRef dir) {
Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
FileSpec lldb_file_spec;
if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
return false;
std::string raw_path = lldb_file_spec.GetPath();
// drop library directory
llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
// Most Posix systems (e.g. Linux/*BSD) will attempt to replace a */lib with
// */bin as the base directory for helper exe programs. This will fail if the
// /lib and /bin directories are rooted in entirely different trees.
if (log)
log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to "
"derive the %s path from this path: %s",
dir.data(), raw_path.c_str());
if (!parent_path.empty()) {
// Now write in bin in place of lib.
raw_path = (parent_path + dir).str();
if (log)
log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__,
raw_path.c_str());
} else {
if (log)
log->Printf("Host::%s() failed to find /lib/liblldb within the shared "
"lib path, bailing on bin path construction",
__FUNCTION__);
}
file_spec.GetDirectory().SetString(raw_path);
return (bool)file_spec.GetDirectory();
}
bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
return ComputePathRelativeToLibrary(file_spec, "/bin");
}
bool HostInfoPosix::ComputeClangDirectory(FileSpec &file_spec) {
return ComputePathRelativeToLibrary(
file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
CLANG_VERSION_STRING)
.str());
}
bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
FileSpec temp_file("/opt/local/include/lldb", false);
file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
return true;
}
bool HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) {
#ifndef LLDB_DISABLE_PYTHON
FileSpec lldb_file_spec;
if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
return false;
char raw_path[PATH_MAX];
lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
#if defined(LLDB_PYTHON_RELATIVE_LIBDIR)
// Build the path by backing out of the lib dir, then building
// with whatever the real python interpreter uses. (e.g. lib
// for most, lib64 on RHEL x86_64).
char python_path[PATH_MAX];
::snprintf(python_path, sizeof(python_path), "%s/../%s", raw_path,
LLDB_PYTHON_RELATIVE_LIBDIR);
char final_path[PATH_MAX];
realpath(python_path, final_path);
file_spec.GetDirectory().SetCString(final_path);
return true;
#else
llvm::SmallString<256> python_version_dir;
llvm::raw_svector_ostream os(python_version_dir);
os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION
<< "/site-packages";
// We may get our string truncated. Should we protect this with an assert?
::strncat(raw_path, python_version_dir.c_str(),
sizeof(raw_path) - strlen(raw_path) - 1);
file_spec.GetDirectory().SetCString(raw_path);
return true;
#endif
#else
return false;
#endif
}
bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
std::string &var) {
if (const char *pvar = ::getenv(var_name.c_str())) {
var = std::string(pvar);
return true;
}
return false;
}

View File

@@ -0,0 +1,95 @@
//===-- HostProcessPosix.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/Host.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/posix/HostProcessPosix.h"
#include "llvm/ADT/STLExtras.h"
#include <csignal>
#include <limits.h>
#include <unistd.h>
using namespace lldb_private;
namespace {
const int kInvalidPosixProcess = 0;
}
HostProcessPosix::HostProcessPosix()
: HostNativeProcessBase(kInvalidPosixProcess) {}
HostProcessPosix::HostProcessPosix(lldb::process_t process)
: HostNativeProcessBase(process) {}
HostProcessPosix::~HostProcessPosix() {}
Status HostProcessPosix::Signal(int signo) const {
if (m_process == kInvalidPosixProcess) {
Status error;
error.SetErrorString("HostProcessPosix refers to an invalid process");
return error;
}
return HostProcessPosix::Signal(m_process, signo);
}
Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
Status error;
if (-1 == ::kill(process, signo))
error.SetErrorToErrno();
return error;
}
Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
Status HostProcessPosix::GetMainModule(FileSpec &file_spec) const {
Status error;
// Use special code here because proc/[pid]/exe is a symbolic link.
char link_path[PATH_MAX];
if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) != 1) {
error.SetErrorString("Unable to build /proc/<pid>/exe string");
return error;
}
error = FileSystem::Readlink(FileSpec{link_path, false}, file_spec);
if (!error.Success())
return error;
// If the binary has been deleted, the link name has " (deleted)" appended.
// Remove if there.
if (file_spec.GetFilename().GetStringRef().endswith(" (deleted)")) {
const char *filename = file_spec.GetFilename().GetCString();
static const size_t deleted_len = strlen(" (deleted)");
const size_t len = file_spec.GetFilename().GetLength();
file_spec.GetFilename().SetCStringWithLength(filename, len - deleted_len);
}
return error;
}
lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
bool HostProcessPosix::IsRunning() const {
if (m_process == kInvalidPosixProcess)
return false;
// Send this process the null signal. If it succeeds the process is running.
Status error = Signal(0);
return error.Success();
}
HostThread HostProcessPosix::StartMonitoring(
const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
return Host::StartMonitoringChildProcess(callback, m_process,
monitor_signals);
}

View File

@@ -0,0 +1,65 @@
//===-- HostThreadPosix.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/posix/HostThreadPosix.h"
#include "lldb/Utility/Status.h"
#include <errno.h>
#include <pthread.h>
using namespace lldb;
using namespace lldb_private;
HostThreadPosix::HostThreadPosix() {}
HostThreadPosix::HostThreadPosix(lldb::thread_t thread)
: HostNativeThreadBase(thread) {}
HostThreadPosix::~HostThreadPosix() {}
Status HostThreadPosix::Join(lldb::thread_result_t *result) {
Status error;
if (IsJoinable()) {
int err = ::pthread_join(m_thread, result);
error.SetError(err, lldb::eErrorTypePOSIX);
} else {
if (result)
*result = NULL;
error.SetError(EINVAL, eErrorTypePOSIX);
}
Reset();
return error;
}
Status HostThreadPosix::Cancel() {
Status error;
if (IsJoinable()) {
#ifndef __ANDROID__
#ifndef __FreeBSD__
llvm_unreachable("someone is calling HostThread::Cancel()");
#endif
int err = ::pthread_cancel(m_thread);
error.SetError(err, eErrorTypePOSIX);
#else
error.SetErrorString("HostThreadPosix::Cancel() not supported on Android");
#endif
}
return error;
}
Status HostThreadPosix::Detach() {
Status error;
if (IsJoinable()) {
int err = ::pthread_detach(m_thread);
error.SetError(err, eErrorTypePOSIX);
}
Reset();
return error;
}

View File

@@ -0,0 +1,61 @@
//===-- LockFilePosix.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/posix/LockFilePosix.h"
#include <fcntl.h>
#include <unistd.h>
using namespace lldb;
using namespace lldb_private;
namespace {
Status fileLock(int fd, int cmd, int lock_type, const uint64_t start,
const uint64_t len) {
struct flock fl;
fl.l_type = lock_type;
fl.l_whence = SEEK_SET;
fl.l_start = start;
fl.l_len = len;
fl.l_pid = ::getpid();
Status error;
if (::fcntl(fd, cmd, &fl) == -1)
error.SetErrorToErrno();
return error;
}
} // namespace
LockFilePosix::LockFilePosix(int fd) : LockFileBase(fd) {}
LockFilePosix::~LockFilePosix() { Unlock(); }
Status LockFilePosix::DoWriteLock(const uint64_t start, const uint64_t len) {
return fileLock(m_fd, F_SETLKW, F_WRLCK, start, len);
}
Status LockFilePosix::DoTryWriteLock(const uint64_t start, const uint64_t len) {
return fileLock(m_fd, F_SETLK, F_WRLCK, start, len);
}
Status LockFilePosix::DoReadLock(const uint64_t start, const uint64_t len) {
return fileLock(m_fd, F_SETLKW, F_RDLCK, start, len);
}
Status LockFilePosix::DoTryReadLock(const uint64_t start, const uint64_t len) {
return fileLock(m_fd, F_SETLK, F_RDLCK, start, len);
}
Status LockFilePosix::DoUnlock() {
return fileLock(m_fd, F_SETLK, F_UNLCK, m_start, m_len);
}

View File

@@ -0,0 +1,318 @@
//===-- PipePosix.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/posix/PipePosix.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Utility/SelectHelper.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
#ifndef _GLIBCXX_USE_NANOSLEEP
#define _GLIBCXX_USE_NANOSLEEP
#endif
#endif
#include <functional>
#include <thread>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace lldb;
using namespace lldb_private;
int PipePosix::kInvalidDescriptor = -1;
enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
// pipe2 is supported by a limited set of platforms
// TODO: Add more platforms that support pipe2.
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10) || \
defined(__NetBSD__)
#define PIPE2_SUPPORTED 1
#else
#define PIPE2_SUPPORTED 0
#endif
namespace {
constexpr auto OPEN_WRITER_SLEEP_TIMEOUT_MSECS = 100;
#if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED
bool SetCloexecFlag(int fd) {
int flags = ::fcntl(fd, F_GETFD);
if (flags == -1)
return false;
return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0);
}
#endif
std::chrono::time_point<std::chrono::steady_clock> Now() {
return std::chrono::steady_clock::now();
}
}
PipePosix::PipePosix()
: m_fds{PipePosix::kInvalidDescriptor, PipePosix::kInvalidDescriptor} {}
PipePosix::PipePosix(int read_fd, int write_fd) : m_fds{read_fd, write_fd} {}
PipePosix::PipePosix(PipePosix &&pipe_posix)
: PipeBase{std::move(pipe_posix)},
m_fds{pipe_posix.ReleaseReadFileDescriptor(),
pipe_posix.ReleaseWriteFileDescriptor()} {}
PipePosix &PipePosix::operator=(PipePosix &&pipe_posix) {
PipeBase::operator=(std::move(pipe_posix));
m_fds[READ] = pipe_posix.ReleaseReadFileDescriptor();
m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptor();
return *this;
}
PipePosix::~PipePosix() { Close(); }
Status PipePosix::CreateNew(bool child_processes_inherit) {
if (CanRead() || CanWrite())
return Status(EINVAL, eErrorTypePOSIX);
Status error;
#if PIPE2_SUPPORTED
if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0)
return error;
#else
if (::pipe(m_fds) == 0) {
#ifdef FD_CLOEXEC
if (!child_processes_inherit) {
if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1])) {
error.SetErrorToErrno();
Close();
return error;
}
}
#endif
return error;
}
#endif
error.SetErrorToErrno();
m_fds[READ] = PipePosix::kInvalidDescriptor;
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
return error;
}
Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) {
if (CanRead() || CanWrite())
return Status("Pipe is already opened");
Status error;
if (::mkfifo(name.data(), 0660) != 0)
error.SetErrorToErrno();
return error;
}
Status PipePosix::CreateWithUniqueName(llvm::StringRef prefix,
bool child_process_inherit,
llvm::SmallVectorImpl<char> &name) {
llvm::SmallString<PATH_MAX> named_pipe_path;
llvm::SmallString<PATH_MAX> pipe_spec((prefix + ".%%%%%%").str());
FileSpec tmpdir_file_spec;
tmpdir_file_spec.Clear();
if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) {
tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str());
} else {
tmpdir_file_spec.AppendPathComponent("/tmp");
tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str());
}
// It's possible that another process creates the target path after we've
// verified it's available but before we create it, in which case we
// should try again.
Status error;
do {
llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(),
named_pipe_path);
error = CreateNew(named_pipe_path, child_process_inherit);
} while (error.GetError() == EEXIST);
if (error.Success())
name = named_pipe_path;
return error;
}
Status PipePosix::OpenAsReader(llvm::StringRef name,
bool child_process_inherit) {
if (CanRead() || CanWrite())
return Status("Pipe is already opened");
int flags = O_RDONLY | O_NONBLOCK;
if (!child_process_inherit)
flags |= O_CLOEXEC;
Status error;
int fd = ::open(name.data(), flags);
if (fd != -1)
m_fds[READ] = fd;
else
error.SetErrorToErrno();
return error;
}
Status
PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name,
bool child_process_inherit,
const std::chrono::microseconds &timeout) {
if (CanRead() || CanWrite())
return Status("Pipe is already opened");
int flags = O_WRONLY | O_NONBLOCK;
if (!child_process_inherit)
flags |= O_CLOEXEC;
using namespace std::chrono;
const auto finish_time = Now() + timeout;
while (!CanWrite()) {
if (timeout != microseconds::zero()) {
const auto dur = duration_cast<microseconds>(finish_time - Now()).count();
if (dur <= 0)
return Status("timeout exceeded - reader hasn't opened so far");
}
errno = 0;
int fd = ::open(name.data(), flags);
if (fd == -1) {
const auto errno_copy = errno;
// We may get ENXIO if a reader side of the pipe hasn't opened yet.
if (errno_copy != ENXIO)
return Status(errno_copy, eErrorTypePOSIX);
std::this_thread::sleep_for(
milliseconds(OPEN_WRITER_SLEEP_TIMEOUT_MSECS));
} else {
m_fds[WRITE] = fd;
}
}
return Status();
}
int PipePosix::GetReadFileDescriptor() const { return m_fds[READ]; }
int PipePosix::GetWriteFileDescriptor() const { return m_fds[WRITE]; }
int PipePosix::ReleaseReadFileDescriptor() {
const int fd = m_fds[READ];
m_fds[READ] = PipePosix::kInvalidDescriptor;
return fd;
}
int PipePosix::ReleaseWriteFileDescriptor() {
const int fd = m_fds[WRITE];
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
return fd;
}
void PipePosix::Close() {
CloseReadFileDescriptor();
CloseWriteFileDescriptor();
}
Status PipePosix::Delete(llvm::StringRef name) {
return llvm::sys::fs::remove(name);
}
bool PipePosix::CanRead() const {
return m_fds[READ] != PipePosix::kInvalidDescriptor;
}
bool PipePosix::CanWrite() const {
return m_fds[WRITE] != PipePosix::kInvalidDescriptor;
}
void PipePosix::CloseReadFileDescriptor() {
if (CanRead()) {
close(m_fds[READ]);
m_fds[READ] = PipePosix::kInvalidDescriptor;
}
}
void PipePosix::CloseWriteFileDescriptor() {
if (CanWrite()) {
close(m_fds[WRITE]);
m_fds[WRITE] = PipePosix::kInvalidDescriptor;
}
}
Status PipePosix::ReadWithTimeout(void *buf, size_t size,
const std::chrono::microseconds &timeout,
size_t &bytes_read) {
bytes_read = 0;
if (!CanRead())
return Status(EINVAL, eErrorTypePOSIX);
const int fd = GetReadFileDescriptor();
SelectHelper select_helper;
select_helper.SetTimeout(timeout);
select_helper.FDSetRead(fd);
Status error;
while (error.Success()) {
error = select_helper.Select();
if (error.Success()) {
auto result = ::read(fd, reinterpret_cast<char *>(buf) + bytes_read,
size - bytes_read);
if (result != -1) {
bytes_read += result;
if (bytes_read == size || result == 0)
break;
} else {
error.SetErrorToErrno();
break;
}
}
}
return error;
}
Status PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) {
bytes_written = 0;
if (!CanWrite())
return Status(EINVAL, eErrorTypePOSIX);
const int fd = GetWriteFileDescriptor();
SelectHelper select_helper;
select_helper.SetTimeout(std::chrono::seconds(0));
select_helper.FDSetWrite(fd);
Status error;
while (error.Success()) {
error = select_helper.Select();
if (error.Success()) {
auto result =
::write(fd, reinterpret_cast<const char *>(buf) + bytes_written,
size - bytes_written);
if (result != -1) {
bytes_written += result;
if (bytes_written == size)
break;
} else {
error.SetErrorToErrno();
}
}
}
return error;
}

View File

@@ -0,0 +1,230 @@
//===-- ProcessLauncherLinux.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/posix/ProcessLauncherPosixFork.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostProcess.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Target/ProcessLaunchInfo.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Log.h"
#include "llvm/Support/Errno.h"
#include <limits.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sstream>
#include <csignal>
#ifdef __ANDROID__
#include <android/api-level.h>
#define PT_TRACE_ME PTRACE_TRACEME
#endif
#if defined(__ANDROID_API__) && __ANDROID_API__ < 15
#include <linux/personality.h>
#elif defined(__linux__)
#include <sys/personality.h>
#endif
using namespace lldb;
using namespace lldb_private;
static void FixupEnvironment(Args &env) {
#ifdef __ANDROID__
// 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 const char *path = "PATH=";
for (auto &entry : env.entries()) {
if (entry.ref.startswith(path))
return;
}
env.AppendArgument(llvm::StringRef("PATH=/system/bin"));
#endif
}
static void LLVM_ATTRIBUTE_NORETURN ExitWithError(int error_fd,
const char *operation) {
int err = errno;
llvm::raw_fd_ostream os(error_fd, true);
os << operation << " failed: " << llvm::sys::StrError(err);
os.flush();
_exit(1);
}
static void DisableASLRIfRequested(int error_fd, const ProcessLaunchInfo &info) {
#if defined(__linux__)
if (info.GetFlags().Test(lldb::eLaunchFlagDisableASLR)) {
const unsigned long personality_get_current = 0xffffffff;
int value = personality(personality_get_current);
if (value == -1)
ExitWithError(error_fd, "personality get");
value = personality(ADDR_NO_RANDOMIZE | value);
if (value == -1)
ExitWithError(error_fd, "personality set");
}
#endif
}
static void DupDescriptor(int error_fd, const FileSpec &file_spec, int fd,
int flags) {
int target_fd = ::open(file_spec.GetCString(), flags, 0666);
if (target_fd == -1)
ExitWithError(error_fd, "DupDescriptor-open");
if (target_fd == fd)
return;
if (::dup2(target_fd, fd) == -1)
ExitWithError(error_fd, "DupDescriptor-dup2");
::close(target_fd);
return;
}
static void LLVM_ATTRIBUTE_NORETURN ChildFunc(int error_fd,
const ProcessLaunchInfo &info) {
// Do not inherit setgid powers.
if (setgid(getgid()) != 0)
ExitWithError(error_fd, "setgid");
if (info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)) {
if (setpgid(0, 0) != 0)
ExitWithError(error_fd, "setpgid");
}
for (size_t i = 0; i < info.GetNumFileActions(); ++i) {
const FileAction &action = *info.GetFileActionAtIndex(i);
switch (action.GetAction()) {
case FileAction::eFileActionClose:
if (close(action.GetFD()) != 0)
ExitWithError(error_fd, "close");
break;
case FileAction::eFileActionDuplicate:
if (dup2(action.GetFD(), action.GetActionArgument()) == -1)
ExitWithError(error_fd, "dup2");
break;
case FileAction::eFileActionOpen:
DupDescriptor(error_fd, action.GetFileSpec(), action.GetFD(),
action.GetActionArgument());
break;
case FileAction::eFileActionNone:
break;
}
}
const char **argv = info.GetArguments().GetConstArgumentVector();
// Change working directory
if (info.GetWorkingDirectory() &&
0 != ::chdir(info.GetWorkingDirectory().GetCString()))
ExitWithError(error_fd, "chdir");
DisableASLRIfRequested(error_fd, info);
Args env = info.GetEnvironmentEntries();
FixupEnvironment(env);
const char **envp = env.GetConstArgumentVector();
// Clear the signal mask to prevent the child from being affected by
// any masking done by the parent.
sigset_t set;
if (sigemptyset(&set) != 0 ||
pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0)
ExitWithError(error_fd, "pthread_sigmask");
if (info.GetFlags().Test(eLaunchFlagDebug)) {
// HACK:
// Close everything besides stdin, stdout, and stderr that has no file
// action to avoid leaking. Only do this when debugging, as elsewhere we
// actually rely on
// passing open descriptors to child processes.
for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
if (!info.GetFileActionForFD(fd) && fd != error_fd)
close(fd);
// Start tracing this child that is about to exec.
if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
ExitWithError(error_fd, "ptrace");
}
// Execute. We should never return...
execve(argv[0], const_cast<char *const *>(argv),
const_cast<char *const *>(envp));
#if defined(__linux__)
if (errno == ETXTBSY) {
// On android M and earlier we can get this error because the adb deamon can
// hold a write
// handle on the executable even after it has finished uploading it. This
// state lasts
// only a short time and happens only when there are many concurrent adb
// commands being
// issued, such as when running the test suite. (The file remains open when
// someone does
// an "adb shell" command in the fork() child before it has had a chance to
// exec.) Since
// this state should clear up quickly, wait a while and then give it one
// more go.
usleep(50000);
execve(argv[0], const_cast<char *const *>(argv),
const_cast<char *const *>(envp));
}
#endif
// ...unless exec fails. In which case we definitely need to end the child
// here.
ExitWithError(error_fd, "execve");
}
HostProcess
ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info,
Status &error) {
char exe_path[PATH_MAX];
launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
// A pipe used by the child process to report errors.
PipePosix pipe;
const bool child_processes_inherit = false;
error = pipe.CreateNew(child_processes_inherit);
if (error.Fail())
return HostProcess();
::pid_t pid = ::fork();
if (pid == -1) {
// Fork failed
error.SetErrorStringWithFormatv("Fork failed with error message: {0}",
llvm::sys::StrError());
return HostProcess(LLDB_INVALID_PROCESS_ID);
}
if (pid == 0) {
// child process
pipe.CloseReadFileDescriptor();
ChildFunc(pipe.ReleaseWriteFileDescriptor(), launch_info);
}
// parent process
pipe.CloseWriteFileDescriptor();
char buf[1000];
int r = read(pipe.GetReadFileDescriptor(), buf, sizeof buf);
if (r == 0)
return HostProcess(pid); // No error. We're done.
error.SetErrorString(buf);
waitpid(pid, nullptr, 0);
return HostProcess();
}