Skip to content

[lldb][Linux] Moving generic APIs from HostInfoLinux to HostInfoPosix #119694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lldb/include/lldb/Host/linux/HostInfoLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "llvm/Support/VersionTuple.h"

#include <optional>
#include <string>

namespace lldb_private {

Expand All @@ -26,18 +25,13 @@ class HostInfoLinux : public HostInfoPosix {
static void Initialize(SharedLibraryDirectoryHelper *helper = nullptr);
static void Terminate();

static llvm::VersionTuple GetOSVersion();
static std::optional<std::string> 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
7 changes: 6 additions & 1 deletion lldb/include/lldb/Host/posix/HostInfoPosix.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "lldb/Host/HostInfoBase.h"
#include "lldb/Utility/FileSpec.h"
#include <optional>
#include <string>

namespace lldb_private {

Expand All @@ -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<std::string> 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
58 changes: 0 additions & 58 deletions lldb/source/Host/linux/HostInfoLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<std::string> 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
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 66 additions & 2 deletions lldb/source/Host/posix/HostInfoPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <grp.h>
#include <mutex>
#include <optional>
Expand All @@ -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) {
Expand All @@ -47,6 +76,16 @@ std::optional<std::string> HostInfoPosix::GetOSKernelDescription() {
return std::string(un.version);
}

std::optional<std::string> 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 <android/api-level.h>
#endif
Expand Down Expand Up @@ -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) {
Expand Down
Loading