diff options
Diffstat (limited to 'source/Host/posix/HostInfoPosix.cpp')
-rw-r--r-- | source/Host/posix/HostInfoPosix.cpp | 323 |
1 files changed, 150 insertions, 173 deletions
diff --git a/source/Host/posix/HostInfoPosix.cpp b/source/Host/posix/HostInfoPosix.cpp index d0ef2ca37061..38aac59ecc40 100644 --- a/source/Host/posix/HostInfoPosix.cpp +++ b/source/Host/posix/HostInfoPosix.cpp @@ -14,7 +14,11 @@ #include "lldb/Core/Log.h" #include "lldb/Host/posix/HostInfoPosix.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> @@ -28,30 +32,23 @@ using namespace lldb_private; -size_t -HostInfoPosix::GetPageSize() -{ - return ::getpagesize(); -} +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; +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_NDK__ +#ifdef __ANDROID__ #include <android/api-level.h> #endif #if defined(__ANDROID_API__) && __ANDROID_API__ < 21 @@ -62,194 +59,174 @@ HostInfoPosix::GetHostname(std::string &s) static std::mutex s_getpwuid_lock; #endif -const char * -HostInfoPosix::LookupUserName(uint32_t uid, std::string &user_name) -{ +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(); - } + // 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(); - } + 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; + user_name.clear(); + return nullptr; } -const char * -HostInfoPosix::LookupGroupName(uint32_t gid, std::string &group_name) -{ +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(); - } + 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(); - } + } 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(); + } + group_name.clear(); #else - assert(false && "getgrgid_r() not supported on Android"); + assert(false && "getgrgid_r() not supported on Android"); #endif - return NULL; + return NULL; } -uint32_t -HostInfoPosix::GetUserID() -{ - return getuid(); -} +uint32_t HostInfoPosix::GetUserID() { return getuid(); } -uint32_t -HostInfoPosix::GetGroupID() -{ - return getgid(); -} +uint32_t HostInfoPosix::GetGroupID() { return getgid(); } -uint32_t -HostInfoPosix::GetEffectiveUserID() -{ - return geteuid(); -} +uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); } -uint32_t -HostInfoPosix::GetEffectiveGroupID() -{ - return getegid(); -} +uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } -FileSpec -HostInfoPosix::GetDefaultShell() -{ - return FileSpec("/bin/sh", false); -} +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); -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; + + std::string raw_path = lldb_file_spec.GetPath(); + // drop library directory + llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path); - FileSpec lldb_file_spec; - if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) - return false; + // 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()); - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + if (!parent_path.empty()) { + // Now write in bin in place of lib. + raw_path = (parent_path + dir).str(); - // 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) - { - // Now write in bin in place of lib. - ::snprintf(lib_pos, PATH_MAX - (lib_pos - raw_path), "/bin"); - - 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(); + 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::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::ComputeSupportExeDirectory(FileSpec &file_spec) { + return ComputePathRelativeToLibrary(file_spec, "/bin"); } -bool -HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) -{ -#ifndef LLDB_DISABLE_PYTHON - FileSpec lldb_file_spec; - if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) - return false; +bool HostInfoPosix::ComputeClangDirectory(FileSpec &file_spec) { + return ComputePathRelativeToLibrary( + file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" + + CLANG_VERSION_STRING) + .str()); +} - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); +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; +} -#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); +bool HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) { +#ifndef LLDB_DISABLE_PYTHON + FileSpec lldb_file_spec; + if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) + return false; - char final_path[PATH_MAX]; - realpath(python_path, final_path); - file_spec.GetDirectory().SetCString(final_path); + char raw_path[PATH_MAX]; + lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); - return true; +#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"; + 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); + // 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; + file_spec.GetDirectory().SetCString(raw_path); + return true; #endif #else - return false; + 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; +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; } |