[Lldb-commits] [lldb] [lldb][Linux] Moving generic APIs from HostInfoLinux to HostInfoPosix (PR #119694)

Dhruv Srivastava via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 19 02:17:47 PST 2024


https://github.com/DhruvSrivastavaX updated https://github.com/llvm/llvm-project/pull/119694

>From b8807f815956ac46e8b518a88cda79d16a4ec72c Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivastava at ibm.com>
Date: Thu, 12 Dec 2024 06:06:47 -0600
Subject: [PATCH 1/3] Moving APIs from HostInfoLinux to HostInfoPosix

---
 lldb/include/lldb/Host/linux/HostInfoLinux.h |  9 +-
 lldb/include/lldb/Host/posix/HostInfoPosix.h |  8 +-
 lldb/source/Host/linux/HostInfoLinux.cpp     | 62 +-------------
 lldb/source/Host/posix/HostInfoPosix.cpp     | 87 +++++++++++++++++++-
 4 files changed, 97 insertions(+), 69 deletions(-)

diff --git a/lldb/include/lldb/Host/linux/HostInfoLinux.h b/lldb/include/lldb/Host/linux/HostInfoLinux.h
index 2964f3f1dc9893..1d6a1c3b0412a0 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 <optional>
-#include <string>
 
 namespace lldb_private {
 
@@ -26,18 +25,12 @@ 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
diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h
index 8d070d3ac1e6fe..30f67515f35e23 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 <optional>
+#include <string>
 
 namespace lldb_private {
 
@@ -31,15 +32,20 @@ class HostInfoPosix : public HostInfoBase {
   static uint32_t GetEffectiveGroupID();
 
   static FileSpec GetDefaultShell();
+  static FileSpec GetProgramFileSpec();
 
   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
diff --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp
index 723f0c2fb3fdc6..ed8ac614ee02b8 100644
--- a/lldb/source/Host/linux/HostInfoLinux.cpp
+++ b/lldb/source/Host/linux/HostInfoLinux.cpp
@@ -30,8 +30,8 @@ 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;
+  // llvm::once_flag m_os_version_once_flag;
+  // llvm::VersionTuple m_os_version;
 };
 } // namespace
 
@@ -50,7 +50,7 @@ void HostInfoLinux::Terminate() {
   HostInfoBase::Terminate();
 }
 
-llvm::VersionTuple HostInfoLinux::GetOSVersion() {
+/*llvm::VersionTuple HostInfoPosix::GetOSVersion() {
   assert(g_fields && "Missing call to Initialize?");
   llvm::call_once(g_fields->m_os_version_once_flag, []() {
     struct utsname un;
@@ -66,17 +66,7 @@ llvm::VersionTuple HostInfoLinux::GetOSVersion() {
 
   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
@@ -152,50 +142,6 @@ llvm::StringRef HostInfoLinux::GetDistributionId() {
   return g_fields->m_distribution_id;
 }
 
-FileSpec HostInfoLinux::GetProgramFileSpec() {
-  static FileSpec g_program_filespec;
-
-  if (!g_program_filespec) {
-    char exe_path[PATH_MAX];
-    ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
-    if (len > 0) {
-      exe_path[len] = 0;
-      g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
-    }
-  }
-
-  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 731a7dee2e6203..8a4a094db7b76f 100644
--- a/lldb/source/Host/posix/HostInfoPosix.cpp
+++ b/lldb/source/Host/posix/HostInfoPosix.cpp
@@ -7,16 +7,19 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/posix/HostInfoPosix.h"
+#include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.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>
@@ -27,6 +30,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 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 +75,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
@@ -139,8 +177,53 @@ FileSpec HostInfoPosix::GetDefaultShell() {
   return FileSpec("/bin/sh");
 }
 
+FileSpec HostInfoPosix::GetProgramFileSpec() {
+  static FileSpec g_program_filespec;
+
+  if (!g_program_filespec) {
+    char exe_path[PATH_MAX];
+    ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
+    if (len > 0) {
+      exe_path[len] = 0;
+      g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
+    }
+  }
+
+  return g_program_filespec;
+}
+
+// Keeping the original one for reference
+// bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
+//  return ComputePathRelativeToLibrary(file_spec, "/bin");
+// }
+
 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(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) {

>From 8c0178bf4850d146d3732b3e34d91a671b5687b4 Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivastava at ibm.com>
Date: Thu, 19 Dec 2024 02:28:40 -0600
Subject: [PATCH 2/3] Removed GetProgramFileSpec

---
 lldb/include/lldb/Host/linux/HostInfoLinux.h |  1 +
 lldb/include/lldb/Host/posix/HostInfoPosix.h |  1 -
 lldb/source/Host/linux/HostInfoLinux.cpp     | 34 +++++++++-----------
 lldb/source/Host/posix/HostInfoPosix.cpp     | 20 ++----------
 4 files changed, 19 insertions(+), 37 deletions(-)

diff --git a/lldb/include/lldb/Host/linux/HostInfoLinux.h b/lldb/include/lldb/Host/linux/HostInfoLinux.h
index 1d6a1c3b0412a0..904d679e6d9536 100644
--- a/lldb/include/lldb/Host/linux/HostInfoLinux.h
+++ b/lldb/include/lldb/Host/linux/HostInfoLinux.h
@@ -26,6 +26,7 @@ class HostInfoLinux : public HostInfoPosix {
   static void Terminate();
 
   static llvm::StringRef GetDistributionId();
+  static FileSpec GetProgramFileSpec();
 
 protected:
   static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h
index 30f67515f35e23..779b67bd66eda5 100644
--- a/lldb/include/lldb/Host/posix/HostInfoPosix.h
+++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h
@@ -32,7 +32,6 @@ class HostInfoPosix : public HostInfoBase {
   static uint32_t GetEffectiveGroupID();
 
   static FileSpec GetDefaultShell();
-  static FileSpec GetProgramFileSpec();
 
   static bool GetEnvironmentVar(const std::string &var_name, std::string &var);
 
diff --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp
index ed8ac614ee02b8..711d2ca6f13d30 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,23 +48,6 @@ void HostInfoLinux::Terminate() {
   HostInfoBase::Terminate();
 }
 
-/*llvm::VersionTuple HostInfoPosix::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;
-}
-*/
 llvm::StringRef HostInfoLinux::GetDistributionId() {
   assert(g_fields && "Missing call to Initialize?");
   // Try to run 'lbs_release -i', and use that response for the distribution
@@ -142,6 +123,21 @@ llvm::StringRef HostInfoLinux::GetDistributionId() {
   return g_fields->m_distribution_id;
 }
 
+FileSpec HostInfoLinux::GetProgramFileSpec() {
+  static FileSpec g_program_filespec;
+
+  if (!g_program_filespec) {
+    char exe_path[PATH_MAX];
+    ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
+    if (len > 0) {
+      exe_path[len] = 0;
+      g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
+    }
+  }
+
+  return g_program_filespec;
+}
+
 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 8a4a094db7b76f..a8613dbde78e1d 100644
--- a/lldb/source/Host/posix/HostInfoPosix.cpp
+++ b/lldb/source/Host/posix/HostInfoPosix.cpp
@@ -9,6 +9,7 @@
 #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"
@@ -46,7 +47,7 @@ llvm::VersionTuple HostInfoPosix::GetOSVersion() {
       return;
 
     llvm::StringRef release = un.release;
-    // The kernel release string can include a lot of stuff (e.g.
+    // 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);
@@ -177,21 +178,6 @@ FileSpec HostInfoPosix::GetDefaultShell() {
   return FileSpec("/bin/sh");
 }
 
-FileSpec HostInfoPosix::GetProgramFileSpec() {
-  static FileSpec g_program_filespec;
-
-  if (!g_program_filespec) {
-    char exe_path[PATH_MAX];
-    ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
-    if (len > 0) {
-      exe_path[len] = 0;
-      g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
-    }
-  }
-
-  return g_program_filespec;
-}
-
 // Keeping the original one for reference
 // bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
 //  return ComputePathRelativeToLibrary(file_spec, "/bin");
@@ -201,7 +187,7 @@ bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
   if (ComputePathRelativeToLibrary(file_spec, "/bin") &&
       file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
     return true;
-  file_spec.SetDirectory(GetProgramFileSpec().GetDirectory());
+  file_spec.SetDirectory(HostInfo::GetProgramFileSpec().GetDirectory());
   return !file_spec.GetDirectory().IsEmpty();
 }
 

>From dae3994d5b8293edcbb5be4c4fbcadd53cfa75d5 Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivastava at ibm.com>
Date: Thu, 19 Dec 2024 04:15:52 -0600
Subject: [PATCH 3/3] Removed comment

---
 lldb/source/Host/posix/HostInfoPosix.cpp | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp
index a8613dbde78e1d..193f584900b632 100644
--- a/lldb/source/Host/posix/HostInfoPosix.cpp
+++ b/lldb/source/Host/posix/HostInfoPosix.cpp
@@ -178,11 +178,6 @@ FileSpec HostInfoPosix::GetDefaultShell() {
   return FileSpec("/bin/sh");
 }
 
-// Keeping the original one for reference
-// bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
-//  return ComputePathRelativeToLibrary(file_spec, "/bin");
-// }
-
 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
   if (ComputePathRelativeToLibrary(file_spec, "/bin") &&
       file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))



More information about the lldb-commits mailing list