Skip to content

Commit d05de47

Browse files
HostInfoAIX
1 parent e874c8f commit d05de47

File tree

3 files changed

+261
-0
lines changed

3 files changed

+261
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- HostInfoAIX.h -----------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_HOST_AIX_HOSTINFOAIX_H_
10+
#define LLDB_HOST_AIX_HOSTINFOAIX_H_
11+
12+
#include "lldb/Host/posix/HostInfoPosix.h"
13+
#include "lldb/Utility/FileSpec.h"
14+
#include "llvm/ADT/StringRef.h"
15+
#include "llvm/Support/VersionTuple.h"
16+
17+
#include <optional>
18+
#include <string>
19+
20+
namespace lldb_private {
21+
22+
class HostInfoAIX : public HostInfoPosix {
23+
friend class HostInfoBase;
24+
25+
public:
26+
static void Initialize(SharedLibraryDirectoryHelper *helper = nullptr);
27+
static void Terminate();
28+
29+
static llvm::VersionTuple GetOSVersion();
30+
static std::optional<std::string> GetOSBuildString();
31+
static llvm::StringRef GetDistributionId();
32+
static FileSpec GetProgramFileSpec();
33+
34+
protected:
35+
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
36+
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
37+
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
38+
static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
39+
ArchSpec &arch_64);
40+
};
41+
} // namespace lldb_private
42+
43+
#endif // LLDB_HOST_AIX_HOSTINFOAIX_H_

lldb/source/Host/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ else()
133133
openbsd/Host.cpp
134134
openbsd/HostInfoOpenBSD.cpp
135135
)
136+
137+
elseif (CMAKE_SYSTEM_NAME MATCHES "AIX")
138+
add_host_subdirectory(aix
139+
aix/HostInfoAIX.cpp
140+
)
136141
endif()
137142
endif()
138143

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
//===-- HostInfoAIX.cpp -------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Host/aix/HostInfoAIX.h"
10+
#include "lldb/Host/Config.h"
11+
#include "lldb/Host/FileSystem.h"
12+
#include "lldb/Utility/LLDBLog.h"
13+
#include "lldb/Utility/Log.h"
14+
15+
#include "llvm/Support/Threading.h"
16+
17+
#include <climits>
18+
#include <cstdio>
19+
#include <cstring>
20+
#include <sys/utsname.h>
21+
#include <unistd.h>
22+
23+
#include <algorithm>
24+
#include <mutex>
25+
#include <optional>
26+
27+
using namespace lldb_private;
28+
29+
namespace {
30+
struct HostInfoAIXFields {
31+
llvm::once_flag m_distribution_once_flag;
32+
std::string m_distribution_id;
33+
llvm::once_flag m_os_version_once_flag;
34+
llvm::VersionTuple m_os_version;
35+
};
36+
} // namespace
37+
38+
static HostInfoAIXFields *g_fields = nullptr;
39+
40+
void HostInfoAIX::Initialize(SharedLibraryDirectoryHelper *helper) {
41+
HostInfoPosix::Initialize(helper);
42+
43+
g_fields = new HostInfoAIXFields();
44+
}
45+
46+
void HostInfoAIX::Terminate() {
47+
assert(g_fields && "Missing call to Initialize?");
48+
delete g_fields;
49+
g_fields = nullptr;
50+
HostInfoBase::Terminate();
51+
}
52+
53+
llvm::VersionTuple HostInfoAIX::GetOSVersion() {
54+
assert(g_fields && "Missing call to Initialize?");
55+
llvm::call_once(g_fields->m_os_version_once_flag, []() {
56+
struct utsname un;
57+
if (uname(&un) != 0)
58+
return;
59+
60+
llvm::StringRef release = un.release;
61+
// The kernel release string can include a lot of stuff (e.g.
62+
// 4.9.0-6-amd64). We're only interested in the numbered prefix.
63+
release = release.substr(0, release.find_first_not_of("0123456789."));
64+
g_fields->m_os_version.tryParse(release);
65+
});
66+
67+
return g_fields->m_os_version;
68+
}
69+
70+
std::optional<std::string> HostInfoAIX::GetOSBuildString() {
71+
struct utsname un;
72+
::memset(&un, 0, sizeof(utsname));
73+
74+
if (uname(&un) < 0)
75+
return std::nullopt;
76+
77+
return std::string(un.release);
78+
}
79+
80+
llvm::StringRef HostInfoAIX::GetDistributionId() {
81+
assert(g_fields && "Missing call to Initialize?");
82+
// Try to run 'lbs_release -i', and use that response for the distribution
83+
// id.
84+
llvm::call_once(g_fields->m_distribution_once_flag, []() {
85+
Log *log = GetLog(LLDBLog::Host);
86+
LLDB_LOGF(log, "attempting to determine AIX distribution...");
87+
88+
// check if the lsb_release command exists at one of the following paths
89+
const char *const exe_paths[] = {"/bin/lsb_release",
90+
"/usr/bin/lsb_release"};
91+
92+
for (size_t exe_index = 0;
93+
exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
94+
const char *const get_distribution_info_exe = exe_paths[exe_index];
95+
if (access(get_distribution_info_exe, F_OK)) {
96+
// this exe doesn't exist, move on to next exe
97+
LLDB_LOGF(log, "executable doesn't exist: %s",
98+
get_distribution_info_exe);
99+
continue;
100+
}
101+
102+
// execute the distribution-retrieval command, read output
103+
std::string get_distribution_id_command(get_distribution_info_exe);
104+
get_distribution_id_command += " -i";
105+
106+
FILE *file = popen(get_distribution_id_command.c_str(), "r");
107+
if (!file) {
108+
LLDB_LOGF(log,
109+
"failed to run command: \"%s\", cannot retrieve "
110+
"platform information",
111+
get_distribution_id_command.c_str());
112+
break;
113+
}
114+
115+
// retrieve the distribution id string.
116+
char distribution_id[256] = {'\0'};
117+
if (fgets(distribution_id, sizeof(distribution_id) - 1, file) !=
118+
nullptr) {
119+
LLDB_LOGF(log, "distribution id command returned \"%s\"",
120+
distribution_id);
121+
122+
const char *const distributor_id_key = "Distributor ID:\t";
123+
if (strstr(distribution_id, distributor_id_key)) {
124+
// strip newlines
125+
std::string id_string(distribution_id + strlen(distributor_id_key));
126+
llvm::erase(id_string, '\n');
127+
128+
// lower case it and convert whitespace to underscores
129+
std::transform(
130+
id_string.begin(), id_string.end(), id_string.begin(),
131+
[](char ch) { return tolower(isspace(ch) ? '_' : ch); });
132+
133+
g_fields->m_distribution_id = id_string;
134+
LLDB_LOGF(log, "distribution id set to \"%s\"",
135+
g_fields->m_distribution_id.c_str());
136+
} else {
137+
LLDB_LOGF(log, "failed to find \"%s\" field in \"%s\"",
138+
distributor_id_key, distribution_id);
139+
}
140+
} else {
141+
LLDB_LOGF(log,
142+
"failed to retrieve distribution id, \"%s\" returned no"
143+
" lines",
144+
get_distribution_id_command.c_str());
145+
}
146+
147+
// clean up the file
148+
pclose(file);
149+
}
150+
});
151+
152+
return g_fields->m_distribution_id;
153+
}
154+
155+
FileSpec HostInfoAIX::GetProgramFileSpec() {
156+
static FileSpec g_program_filespec;
157+
158+
if (!g_program_filespec) {
159+
char exe_path[PATH_MAX];
160+
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
161+
if (len > 0) {
162+
exe_path[len] = 0;
163+
g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
164+
}
165+
}
166+
167+
return g_program_filespec;
168+
}
169+
170+
bool HostInfoAIX::ComputeSupportExeDirectory(FileSpec &file_spec) {
171+
if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
172+
file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
173+
return true;
174+
file_spec.SetDirectory(GetProgramFileSpec().GetDirectory());
175+
return !file_spec.GetDirectory().IsEmpty();
176+
}
177+
178+
bool HostInfoAIX::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
179+
FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins");
180+
FileSystem::Instance().Resolve(temp_file);
181+
file_spec.SetDirectory(temp_file.GetPath());
182+
return true;
183+
}
184+
185+
bool HostInfoAIX::ComputeUserPluginsDirectory(FileSpec &file_spec) {
186+
// XDG Base Directory Specification
187+
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
188+
// XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
189+
const char *xdg_data_home = getenv("XDG_DATA_HOME");
190+
if (xdg_data_home && xdg_data_home[0]) {
191+
std::string user_plugin_dir(xdg_data_home);
192+
user_plugin_dir += "/lldb";
193+
file_spec.SetDirectory(user_plugin_dir.c_str());
194+
} else
195+
file_spec.SetDirectory("~/.local/share/lldb");
196+
return true;
197+
}
198+
199+
void HostInfoAIX::ComputeHostArchitectureSupport(ArchSpec &arch_32,
200+
ArchSpec &arch_64) {
201+
HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
202+
203+
// On AIX, "unknown" in the vendor slot isn't what we want for the default
204+
// triple. It's probably an artifact of config.guess.
205+
if (arch_32.IsValid()) {
206+
if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
207+
arch_32.GetTriple().setVendorName(llvm::StringRef());
208+
}
209+
if (arch_64.IsValid()) {
210+
if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
211+
arch_64.GetTriple().setVendorName(llvm::StringRef());
212+
}
213+
}

0 commit comments

Comments
 (0)