summaryrefslogtreecommitdiff
path: root/source/Host/posix
diff options
context:
space:
mode:
authorEd Maste <emaste@FreeBSD.org>2014-11-25 21:00:58 +0000
committerEd Maste <emaste@FreeBSD.org>2014-11-25 21:00:58 +0000
commit0cac4ca3916ac24ab6139d03cbfd18db9e715bfe (patch)
treec94307da318be46e5aeea1a325c1e91749506e4f /source/Host/posix
parent03b99097822ca3ac69252d9afae716a584ed56c4 (diff)
Notes
Diffstat (limited to 'source/Host/posix')
-rw-r--r--source/Host/posix/FileSystem.cpp201
-rw-r--r--source/Host/posix/HostInfoPosix.cpp193
-rw-r--r--source/Host/posix/HostProcessPosix.cpp103
3 files changed, 497 insertions, 0 deletions
diff --git a/source/Host/posix/FileSystem.cpp b/source/Host/posix/FileSystem.cpp
new file mode 100644
index 0000000000000..5713168111424
--- /dev/null
+++ b/source/Host/posix/FileSystem.cpp
@@ -0,0 +1,201 @@
+//===-- 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 <sys/stat.h>
+#include <sys/types.h>
+
+// lldb Includes
+#include "lldb/Core/Error.h"
+#include "lldb/Core/StreamString.h"
+#include "lldb/Host/Host.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+FileSpec::PathSyntax
+FileSystem::GetNativePathSyntax()
+{
+ return FileSpec::ePathSyntaxPosix;
+}
+
+Error
+FileSystem::MakeDirectory(const char *path, uint32_t file_permissions)
+{
+ Error error;
+ if (path && path[0])
+ {
+ if (::mkdir(path, file_permissions) != 0)
+ {
+ error.SetErrorToErrno();
+ switch (error.GetError())
+ {
+ case ENOENT:
+ {
+ // Parent directory doesn't exist, so lets make it if we can
+ FileSpec spec(path, false);
+ if (spec.GetDirectory() && spec.GetFilename())
+ {
+ // Make the parent directory and try again
+ Error error2 = MakeDirectory(spec.GetDirectory().GetCString(), file_permissions);
+ if (error2.Success())
+ {
+ // Try and make the directory again now that the parent directory was made successfully
+ if (::mkdir(path, file_permissions) == 0)
+ error.Clear();
+ else
+ error.SetErrorToErrno();
+ }
+ }
+ }
+ break;
+
+ case EEXIST:
+ {
+ FileSpec path_spec(path, false);
+ if (path_spec.IsDirectory())
+ error.Clear(); // It is a directory and it already exists
+ }
+ break;
+ }
+ }
+ }
+ else
+ {
+ error.SetErrorString("empty path");
+ }
+ return error;
+}
+
+Error
+FileSystem::DeleteDirectory(const char *path, bool recurse)
+{
+ Error error;
+ if (path && path[0])
+ {
+ if (recurse)
+ {
+ StreamString command;
+ command.Printf("rm -rf \"%s\"", path);
+ int status = ::system(command.GetString().c_str());
+ if (status != 0)
+ error.SetError(status, eErrorTypeGeneric);
+ }
+ else
+ {
+ if (::rmdir(path) != 0)
+ error.SetErrorToErrno();
+ }
+ }
+ else
+ {
+ error.SetErrorString("empty path");
+ }
+ return error;
+}
+
+Error
+FileSystem::GetFilePermissions(const char *path, uint32_t &file_permissions)
+{
+ Error error;
+ struct stat file_stats;
+ if (::stat(path, &file_stats) == 0)
+ {
+ // The bits in "st_mode" currently match the definitions
+ // for the file mode bits in unix.
+ file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
+ }
+ else
+ {
+ error.SetErrorToErrno();
+ }
+ return error;
+}
+
+Error
+FileSystem::SetFilePermissions(const char *path, uint32_t file_permissions)
+{
+ Error error;
+ if (::chmod(path, file_permissions) != 0)
+ error.SetErrorToErrno();
+ return error;
+}
+
+lldb::user_id_t
+FileSystem::GetFileSize(const FileSpec &file_spec)
+{
+ return file_spec.GetByteSize();
+}
+
+bool
+FileSystem::GetFileExists(const FileSpec &file_spec)
+{
+ return file_spec.Exists();
+}
+
+Error
+FileSystem::Symlink(const char *src, const char *dst)
+{
+ Error error;
+ if (::symlink(dst, src) == -1)
+ error.SetErrorToErrno();
+ return error;
+}
+
+Error
+FileSystem::Unlink(const char *path)
+{
+ Error error;
+ if (::unlink(path) == -1)
+ error.SetErrorToErrno();
+ return error;
+}
+
+Error
+FileSystem::Readlink(const char *path, char *buf, size_t buf_len)
+{
+ Error error;
+ ssize_t count = ::readlink(path, buf, buf_len);
+ if (count < 0)
+ error.SetErrorToErrno();
+ else if (static_cast<size_t>(count) < (buf_len - 1))
+ buf[count] = '\0'; // Success
+ else
+ error.SetErrorString("'buf' buffer is too small to contain link contents");
+ return error;
+}
+
+bool
+FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high)
+{
+#if defined(__APPLE__)
+ StreamString md5_cmd_line;
+ md5_cmd_line.Printf("md5 -q '%s'", file_spec.GetPath().c_str());
+ std::string hash_string;
+ Error err = Host::RunShellCommand(md5_cmd_line.GetData(), NULL, NULL, NULL, &hash_string, 60);
+ if (err.Fail())
+ return false;
+ // a correctly formed MD5 is 16-bytes, that is 32 hex digits
+ // if the output is any other length it is probably wrong
+ if (hash_string.size() != 32)
+ return false;
+ std::string part1(hash_string, 0, 16);
+ std::string part2(hash_string, 16);
+ const char *part1_cstr = part1.c_str();
+ const char *part2_cstr = part2.c_str();
+ high = ::strtoull(part1_cstr, NULL, 16);
+ low = ::strtoull(part2_cstr, NULL, 16);
+ return true;
+#else
+ // your own MD5 implementation here
+ return false;
+#endif
+}
diff --git a/source/Host/posix/HostInfoPosix.cpp b/source/Host/posix/HostInfoPosix.cpp
new file mode 100644
index 0000000000000..77fdc2b61a316
--- /dev/null
+++ b/source/Host/posix/HostInfoPosix.cpp
@@ -0,0 +1,193 @@
+//===-- HostInfoPosix.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/lldb-python.h"
+
+#include "lldb/Core/Log.h"
+#include "lldb/Host/posix/HostInfoPosix.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <grp.h>
+#include <limits.h>
+#include <netdb.h>
+#include <pwd.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;
+}
+
+const char *
+HostInfoPosix::LookupUserName(uint32_t uid, std::string &user_name)
+{
+ 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();
+ }
+ }
+ user_name.clear();
+ return NULL;
+}
+
+const char *
+HostInfoPosix::LookupGroupName(uint32_t gid, std::string &group_name)
+{
+ 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();
+ 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();
+}
+
+bool
+HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec)
+{
+ Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
+
+ 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));
+
+ // 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::ComputeSupportExeDirectory() attempting to derive the bin path (ePathTypeSupportExecutableDir) from "
+ "this path: %s",
+ raw_path);
+ char *lib_pos = ::strstr(raw_path, "/lib");
+ if (lib_pos != nullptr)
+ {
+ // First terminate the raw path at the start of lib.
+ *lib_pos = '\0';
+
+ // Now write in bin in place of lib.
+ ::strncpy(lib_pos, "/bin", PATH_MAX - (lib_pos - raw_path));
+
+ if (log)
+ log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__, raw_path);
+ }
+ 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().SetCString(raw_path);
+ return (bool)file_spec.GetDirectory();
+}
+
+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)
+{
+ 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));
+
+ llvm::SmallString<256> python_version_dir;
+ llvm::raw_svector_ostream os(python_version_dir);
+ os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages";
+ os.flush();
+
+ // 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;
+}
diff --git a/source/Host/posix/HostProcessPosix.cpp b/source/Host/posix/HostProcessPosix.cpp
new file mode 100644
index 0000000000000..4618de4711de9
--- /dev/null
+++ b/source/Host/posix/HostProcessPosix.cpp
@@ -0,0 +1,103 @@
+//===-- HostProcessWindows.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/HostProcessPosix.h"
+#include "lldb/Host/FileSystem.h"
+
+#include "llvm/ADT/STLExtras.h"
+
+#include <limits.h>
+
+using namespace lldb_private;
+
+const lldb::pid_t HostProcessPosix::kInvalidProcessId = 0;
+
+HostProcessPosix::HostProcessPosix()
+: m_pid(kInvalidProcessId)
+{
+}
+
+HostProcessPosix::~HostProcessPosix()
+{
+}
+
+Error HostProcessPosix::Create(lldb::pid_t pid)
+{
+ Error error;
+ if (pid == kInvalidProcessId)
+ error.SetErrorString("Attempt to create an invalid process");
+
+ m_pid = pid;
+ return error;
+}
+
+Error HostProcessPosix::Signal(int signo) const
+{
+ if (m_pid <= 0)
+ {
+ Error error;
+ error.SetErrorString("HostProcessPosix refers to an invalid process");
+ return error;
+ }
+
+ return HostProcessPosix::Signal(m_pid, signo);
+}
+
+Error HostProcessPosix::Signal(lldb::pid_t pid, int signo)
+{
+ Error error;
+
+ if (-1 == ::kill(pid, signo))
+ error.SetErrorToErrno();
+
+ return error;
+}
+
+Error HostProcessPosix::GetMainModule(FileSpec &file_spec) const
+{
+ Error error;
+
+ // Use special code here because proc/[pid]/exe is a symbolic link.
+ char link_path[PATH_MAX];
+ char exe_path[PATH_MAX] = "";
+ if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_pid) <= 0)
+ {
+ error.SetErrorString("Unable to build /proc/<pid>/exe string");
+ return error;
+ }
+
+ error = FileSystem::Readlink(link_path, exe_path, llvm::array_lengthof(exe_path));
+ if (!error.Success())
+ return error;
+
+ const ssize_t len = strlen(exe_path);
+ // If the binary has been deleted, the link name has " (deleted)" appended.
+ // Remove if there.
+ static const ssize_t deleted_len = strlen(" (deleted)");
+ if (len > deleted_len &&
+ !strcmp(exe_path + len - deleted_len, " (deleted)"))
+ {
+ exe_path[len - deleted_len] = 0;
+ }
+
+ file_spec.SetFile(exe_path, false);
+ return error;
+}
+
+lldb::pid_t HostProcessPosix::GetProcessId() const
+{
+ return m_pid;
+}
+
+bool HostProcessPosix::IsRunning() const
+{
+ // Send this process the null signal. If it succeeds the process is running.
+ Error error = Signal(0);
+ return error.Success();
+}