Skip to content
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

[lldb][AIX] HostInfoAIX Support #117906

Merged
merged 6 commits into from
Jan 6, 2025
Merged

Conversation

DhruvSrivastavaX
Copy link
Contributor

@DhruvSrivastavaX DhruvSrivastavaX commented Nov 27, 2024

This PR is in reference to porting LLDB on AIX.

Link to discussions on llvm discourse and github:

  1. https://discourse.llvm.org/t/port-lldb-to-ibm-aix/80640
  2. Extending LLDB to work on AIX #101657
    The complete changes for porting are present in this draft PR:
    Extending LLDB to work on AIX #102601

Added a HostInfoAIX file for the AIX platform.
Most of the common functionalities are handled by the parent HostInfoPosix now,
So we just have some basic functions implemented here.

Review Request: @labath @DavidSpickett

@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2024

@llvm/pr-subscribers-lldb

Author: Dhruv Srivastava (DhruvSrivastavaX)

Changes

This PR is in reference to porting LLDB on AIX.

Link to discussions on llvm discourse and github:

  1. https://discourse.llvm.org/t/port-lldb-to-ibm-aix/80640
  2. Extending LLDB to work on AIX #101657
    The complete changes for porting are present in this draft PR:
    Extending LLDB to work on AIX #102601

This is how we have currently added the support for HostInfo for AIX.
It is taken directly from its Linux counterpart.
Please let me know your suggestions about how can we plan to merge this and I will plan accordingly, and drop it as required. Can we keep it as a separate entity for AIX or we can combine it with something else? If you, how would like me to proceed in a way that is best for the code base as a whole.

Review Request: @labath @DavidSpickett


Full diff: https://github.com/llvm/llvm-project/pull/117906.diff

3 Files Affected:

  • (added) lldb/include/lldb/Host/aix/HostInfoAIX.h (+43)
  • (modified) lldb/source/Host/CMakeLists.txt (+5)
  • (added) lldb/source/Host/aix/HostInfoAIX.cpp (+213)
diff --git a/lldb/include/lldb/Host/aix/HostInfoAIX.h b/lldb/include/lldb/Host/aix/HostInfoAIX.h
new file mode 100644
index 00000000000000..c797b36f3dcc8d
--- /dev/null
+++ b/lldb/include/lldb/Host/aix/HostInfoAIX.h
@@ -0,0 +1,43 @@
+//===-- HostInfoAIX.h -----------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_HOST_AIX_HOSTINFOAIX_H_
+#define LLDB_HOST_AIX_HOSTINFOAIX_H_
+
+#include "lldb/Host/posix/HostInfoPosix.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/VersionTuple.h"
+
+#include <optional>
+#include <string>
+
+namespace lldb_private {
+
+class HostInfoAIX : public HostInfoPosix {
+  friend class HostInfoBase;
+
+public:
+  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 // LLDB_HOST_AIX_HOSTINFOAIX_H_
diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt
index c2e091ee8555b7..e0cd8569bf9575 100644
--- a/lldb/source/Host/CMakeLists.txt
+++ b/lldb/source/Host/CMakeLists.txt
@@ -133,6 +133,11 @@ else()
       openbsd/Host.cpp
       openbsd/HostInfoOpenBSD.cpp
       )
+
+  elseif (CMAKE_SYSTEM_NAME MATCHES "AIX")
+    add_host_subdirectory(aix
+      aix/HostInfoAIX.cpp
+      )    
   endif()
 endif()
 
diff --git a/lldb/source/Host/aix/HostInfoAIX.cpp b/lldb/source/Host/aix/HostInfoAIX.cpp
new file mode 100644
index 00000000000000..e43ab3afd94657
--- /dev/null
+++ b/lldb/source/Host/aix/HostInfoAIX.cpp
@@ -0,0 +1,213 @@
+//===-- HostInfoAIX.cpp -------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/aix/HostInfoAIX.h"
+#include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+#include "llvm/Support/Threading.h"
+
+#include <climits>
+#include <cstdio>
+#include <cstring>
+#include <sys/utsname.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <mutex>
+#include <optional>
+
+using namespace lldb_private;
+
+namespace {
+struct HostInfoAIXFields {
+  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
+
+static HostInfoAIXFields *g_fields = nullptr;
+
+void HostInfoAIX::Initialize(SharedLibraryDirectoryHelper *helper) {
+  HostInfoPosix::Initialize(helper);
+
+  g_fields = new HostInfoAIXFields();
+}
+
+void HostInfoAIX::Terminate() {
+  assert(g_fields && "Missing call to Initialize?");
+  delete g_fields;
+  g_fields = nullptr;
+  HostInfoBase::Terminate();
+}
+
+llvm::VersionTuple HostInfoAIX::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> HostInfoAIX::GetOSBuildString() {
+  struct utsname un;
+  ::memset(&un, 0, sizeof(utsname));
+
+  if (uname(&un) < 0)
+    return std::nullopt;
+
+  return std::string(un.release);
+}
+
+llvm::StringRef HostInfoAIX::GetDistributionId() {
+  assert(g_fields && "Missing call to Initialize?");
+  // Try to run 'lbs_release -i', and use that response for the distribution
+  // id.
+  llvm::call_once(g_fields->m_distribution_once_flag, []() {
+    Log *log = GetLog(LLDBLog::Host);
+    LLDB_LOGF(log, "attempting to determine AIX distribution...");
+
+    // check if the lsb_release command exists at one of the following paths
+    const char *const exe_paths[] = {"/bin/lsb_release",
+                                     "/usr/bin/lsb_release"};
+
+    for (size_t exe_index = 0;
+         exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
+      const char *const get_distribution_info_exe = exe_paths[exe_index];
+      if (access(get_distribution_info_exe, F_OK)) {
+        // this exe doesn't exist, move on to next exe
+        LLDB_LOGF(log, "executable doesn't exist: %s",
+                  get_distribution_info_exe);
+        continue;
+      }
+
+      // execute the distribution-retrieval command, read output
+      std::string get_distribution_id_command(get_distribution_info_exe);
+      get_distribution_id_command += " -i";
+
+      FILE *file = popen(get_distribution_id_command.c_str(), "r");
+      if (!file) {
+        LLDB_LOGF(log,
+                  "failed to run command: \"%s\", cannot retrieve "
+                  "platform information",
+                  get_distribution_id_command.c_str());
+        break;
+      }
+
+      // retrieve the distribution id string.
+      char distribution_id[256] = {'\0'};
+      if (fgets(distribution_id, sizeof(distribution_id) - 1, file) !=
+          nullptr) {
+        LLDB_LOGF(log, "distribution id command returned \"%s\"",
+                  distribution_id);
+
+        const char *const distributor_id_key = "Distributor ID:\t";
+        if (strstr(distribution_id, distributor_id_key)) {
+          // strip newlines
+          std::string id_string(distribution_id + strlen(distributor_id_key));
+          llvm::erase(id_string, '\n');
+
+          // lower case it and convert whitespace to underscores
+          std::transform(
+              id_string.begin(), id_string.end(), id_string.begin(),
+              [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
+
+          g_fields->m_distribution_id = id_string;
+          LLDB_LOGF(log, "distribution id set to \"%s\"",
+                    g_fields->m_distribution_id.c_str());
+        } else {
+          LLDB_LOGF(log, "failed to find \"%s\" field in \"%s\"",
+                    distributor_id_key, distribution_id);
+        }
+      } else {
+        LLDB_LOGF(log,
+                  "failed to retrieve distribution id, \"%s\" returned no"
+                  " lines",
+                  get_distribution_id_command.c_str());
+      }
+
+      // clean up the file
+      pclose(file);
+    }
+  });
+
+  return g_fields->m_distribution_id;
+}
+
+FileSpec HostInfoAIX::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 HostInfoAIX::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 HostInfoAIX::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 HostInfoAIX::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 HostInfoAIX::ComputeHostArchitectureSupport(ArchSpec &arch_32,
+                                                 ArchSpec &arch_64) {
+  HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
+
+  // On AIX, "unknown" in the vendor slot isn't what we want for the default
+  // triple.  It's probably an artifact of config.guess.
+  if (arch_32.IsValid()) {
+    if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
+      arch_32.GetTriple().setVendorName(llvm::StringRef());
+  }
+  if (arch_64.IsValid()) {
+    if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
+      arch_64.GetTriple().setVendorName(llvm::StringRef());
+  }
+}

Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think most of this file could be deleted or moved into HostInfoPOSIX. If we can do everything in the inline comments, then that will leave us with a couple of small functions that are copied from linux. We can then think about whether we want to merge those somehow, or we might just decide to live with it.

lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
lldb/include/lldb/Host/aix/HostInfoAIX.h Outdated Show resolved Hide resolved
lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
DavidSpickett pushed a commit that referenced this pull request Dec 20, 2024
…#119694)

This change is related merging some of the APIs in HostInfoLinux into
HostInfoPosix.

Here is the reference PR comment:

#117906 (comment),
#117906 (comment)
@DhruvSrivastavaX
Copy link
Contributor Author

DhruvSrivastavaX commented Dec 27, 2024

Since the common changes have been moved to HostInfoPosix via this PR:

I have updated HostInfoAIX accordingly. Still it is a copy of the remaining HostInfoLinux, so please let me know your comments.

lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
lldb/source/Host/aix/HostInfoAIX.cpp Show resolved Hide resolved
lldb/source/Host/aix/HostInfoAIX.cpp Outdated Show resolved Hide resolved
@DavidSpickett
Copy link
Collaborator

These changes get things building I assume. So in that context I'm OK with this. I have a feeling HostInfoAIX might end up essentially blank eventually, but just because of the way we handle the HostInfo type, I think some file has to exist for it to build. So it's OK to add one now.

Please update the PR description to reflect the intent of the change as it is now.

@DhruvSrivastavaX
Copy link
Contributor Author

Okay sure. Done. Please check! 🙂

@DavidSpickett DavidSpickett merged commit 3a7a9c9 into llvm:main Jan 6, 2025
7 checks passed
paulhuggett pushed a commit to paulhuggett/llvm-project that referenced this pull request Jan 7, 2025
This PR is in reference to porting LLDB on AIX.

Link to discussions on llvm discourse and github:

1. https://discourse.llvm.org/t/port-lldb-to-ibm-aix/80640
2. llvm#101657
The complete changes for porting are present in this draft PR:
llvm#102601

Added a HostInfoAIX file for the AIX platform. 
Most of the common functionalities are handled by the parent
HostInfoPosix now,
So we just have some basic functions implemented here.
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Jan 10, 2025
…stInfoPosix (#119694)

This change is related merging some of the APIs in HostInfoLinux into
HostInfoPosix.

Here is the reference PR comment:

llvm/llvm-project#117906 (comment),
llvm/llvm-project#117906 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants