diff --git a/lldb/include/lldb/Host/linux/HostInfoLinux.h b/lldb/include/lldb/Host/linux/HostInfoLinux.h index 2964f3f1dc989..904d679e6d953 100644 --- a/lldb/include/lldb/Host/linux/HostInfoLinux.h +++ b/lldb/include/lldb/Host/linux/HostInfoLinux.h @@ -15,7 +15,6 @@ #include "llvm/Support/VersionTuple.h" #include -#include namespace lldb_private { @@ -26,18 +25,13 @@ class HostInfoLinux : public HostInfoPosix { static void Initialize(SharedLibraryDirectoryHelper *helper = nullptr); static void Terminate(); - static llvm::VersionTuple GetOSVersion(); - static std::optional GetOSBuildString(); static llvm::StringRef GetDistributionId(); static FileSpec GetProgramFileSpec(); protected: - static bool ComputeSupportExeDirectory(FileSpec &file_spec); - static bool ComputeSystemPluginsDirectory(FileSpec &file_spec); - static bool ComputeUserPluginsDirectory(FileSpec &file_spec); static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64); }; -} +} // namespace lldb_private #endif diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h index 8d070d3ac1e6f..779b67bd66eda 100644 --- a/lldb/include/lldb/Host/posix/HostInfoPosix.h +++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h @@ -12,6 +12,7 @@ #include "lldb/Host/HostInfoBase.h" #include "lldb/Utility/FileSpec.h" #include +#include namespace lldb_private { @@ -35,11 +36,15 @@ class HostInfoPosix : public HostInfoBase { static bool GetEnvironmentVar(const std::string &var_name, std::string &var); static UserIDResolver &GetUserIDResolver(); + static llvm::VersionTuple GetOSVersion(); + static std::optional GetOSBuildString(); protected: static bool ComputeSupportExeDirectory(FileSpec &file_spec); static bool ComputeHeaderDirectory(FileSpec &file_spec); + static bool ComputeSystemPluginsDirectory(FileSpec &file_spec); + static bool ComputeUserPluginsDirectory(FileSpec &file_spec); }; -} +} // namespace lldb_private #endif diff --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp index 723f0c2fb3fdc..711d2ca6f13d3 100644 --- a/lldb/source/Host/linux/HostInfoLinux.cpp +++ b/lldb/source/Host/linux/HostInfoLinux.cpp @@ -30,8 +30,6 @@ namespace { struct HostInfoLinuxFields { llvm::once_flag m_distribution_once_flag; std::string m_distribution_id; - llvm::once_flag m_os_version_once_flag; - llvm::VersionTuple m_os_version; }; } // namespace @@ -50,33 +48,6 @@ void HostInfoLinux::Terminate() { HostInfoBase::Terminate(); } -llvm::VersionTuple HostInfoLinux::GetOSVersion() { - assert(g_fields && "Missing call to Initialize?"); - llvm::call_once(g_fields->m_os_version_once_flag, []() { - struct utsname un; - if (uname(&un) != 0) - return; - - llvm::StringRef release = un.release; - // The kernel release string can include a lot of stuff (e.g. - // 4.9.0-6-amd64). We're only interested in the numbered prefix. - release = release.substr(0, release.find_first_not_of("0123456789.")); - g_fields->m_os_version.tryParse(release); - }); - - return g_fields->m_os_version; -} - -std::optional HostInfoLinux::GetOSBuildString() { - struct utsname un; - ::memset(&un, 0, sizeof(utsname)); - - if (uname(&un) < 0) - return std::nullopt; - - return std::string(un.release); -} - llvm::StringRef HostInfoLinux::GetDistributionId() { assert(g_fields && "Missing call to Initialize?"); // Try to run 'lbs_release -i', and use that response for the distribution @@ -167,35 +138,6 @@ FileSpec HostInfoLinux::GetProgramFileSpec() { return g_program_filespec; } -bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) { - if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) && - file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec)) - return true; - file_spec.SetDirectory(GetProgramFileSpec().GetDirectory()); - return !file_spec.GetDirectory().IsEmpty(); -} - -bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) { - FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins"); - FileSystem::Instance().Resolve(temp_file); - file_spec.SetDirectory(temp_file.GetPath()); - return true; -} - -bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) { - // XDG Base Directory Specification - // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If - // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. - const char *xdg_data_home = getenv("XDG_DATA_HOME"); - if (xdg_data_home && xdg_data_home[0]) { - std::string user_plugin_dir(xdg_data_home); - user_plugin_dir += "/lldb"; - file_spec.SetDirectory(user_plugin_dir.c_str()); - } else - file_spec.SetDirectory("~/.local/share/lldb"); - return true; -} - void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64) { HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64); diff --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp index 731a7dee2e620..193f584900b63 100644 --- a/lldb/source/Host/posix/HostInfoPosix.cpp +++ b/lldb/source/Host/posix/HostInfoPosix.cpp @@ -7,16 +7,20 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/posix/HostInfoPosix.h" +#include "lldb/Host/Config.h" +#include "lldb/Host/FileSystem.h" +#include "lldb/Host/HostInfo.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/UserIDResolver.h" - #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include +#include #include +#include #include #include #include @@ -27,6 +31,31 @@ using namespace lldb_private; +namespace { +struct HostInfoPosixFields { + llvm::once_flag m_os_version_once_flag; + llvm::VersionTuple m_os_version; +}; +} // namespace + +llvm::VersionTuple HostInfoPosix::GetOSVersion() { + static HostInfoPosixFields *g_fields = new HostInfoPosixFields(); + assert(g_fields && "Missing call to Initialize?"); + llvm::call_once(g_fields->m_os_version_once_flag, []() { + struct utsname un; + if (uname(&un) != 0) + return; + + llvm::StringRef release = un.release; + // The Linux kernel release string can include a lot of stuff (e.g. + // 4.9.0-6-amd64). We're only interested in the numbered prefix. + release = release.substr(0, release.find_first_not_of("0123456789.")); + g_fields->m_os_version.tryParse(release); + }); + + return g_fields->m_os_version; +} + size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); } bool HostInfoPosix::GetHostname(std::string &s) { @@ -47,6 +76,16 @@ std::optional HostInfoPosix::GetOSKernelDescription() { return std::string(un.version); } +std::optional HostInfoPosix::GetOSBuildString() { + struct utsname un; + ::memset(&un, 0, sizeof(utsname)); + + if (uname(&un) < 0) + return std::nullopt; + + return std::string(un.release); +} + #ifdef __ANDROID__ #include #endif @@ -140,7 +179,32 @@ FileSpec HostInfoPosix::GetDefaultShell() { } bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { - return ComputePathRelativeToLibrary(file_spec, "/bin"); + if (ComputePathRelativeToLibrary(file_spec, "/bin") && + file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec)) + return true; + file_spec.SetDirectory(HostInfo::GetProgramFileSpec().GetDirectory()); + return !file_spec.GetDirectory().IsEmpty(); +} + +bool HostInfoPosix::ComputeSystemPluginsDirectory(FileSpec &file_spec) { + FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins"); + FileSystem::Instance().Resolve(temp_file); + file_spec.SetDirectory(temp_file.GetPath()); + return true; +} + +bool HostInfoPosix::ComputeUserPluginsDirectory(FileSpec &file_spec) { + // XDG Base Directory Specification + // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If + // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. + const char *xdg_data_home = getenv("XDG_DATA_HOME"); + if (xdg_data_home && xdg_data_home[0]) { + std::string user_plugin_dir(xdg_data_home); + user_plugin_dir += "/lldb"; + file_spec.SetDirectory(user_plugin_dir.c_str()); + } else + file_spec.SetDirectory("~/.local/share/lldb"); + return true; } bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {