Skip to content

Commit 385b144

Browse files
[lldb][Linux] Moving generic APIs from HostInfoLinux to HostInfoPosix (#119694)
This change is related merging some of the APIs in HostInfoLinux into HostInfoPosix. Here is the reference PR comment: #117906 (comment), #117906 (comment)
1 parent 9a1837f commit 385b144

File tree

4 files changed

+73
-68
lines changed

4 files changed

+73
-68
lines changed

lldb/include/lldb/Host/linux/HostInfoLinux.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvm/Support/VersionTuple.h"
1616

1717
#include <optional>
18-
#include <string>
1918

2019
namespace lldb_private {
2120

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

29-
static llvm::VersionTuple GetOSVersion();
30-
static std::optional<std::string> GetOSBuildString();
3128
static llvm::StringRef GetDistributionId();
3229
static FileSpec GetProgramFileSpec();
3330

3431
protected:
35-
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
36-
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
37-
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
3832
static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
3933
ArchSpec &arch_64);
4034
};
41-
}
35+
} // namespace lldb_private
4236

4337
#endif

lldb/include/lldb/Host/posix/HostInfoPosix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/Host/HostInfoBase.h"
1313
#include "lldb/Utility/FileSpec.h"
1414
#include <optional>
15+
#include <string>
1516

1617
namespace lldb_private {
1718

@@ -35,11 +36,15 @@ class HostInfoPosix : public HostInfoBase {
3536
static bool GetEnvironmentVar(const std::string &var_name, std::string &var);
3637

3738
static UserIDResolver &GetUserIDResolver();
39+
static llvm::VersionTuple GetOSVersion();
40+
static std::optional<std::string> GetOSBuildString();
3841

3942
protected:
4043
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
4144
static bool ComputeHeaderDirectory(FileSpec &file_spec);
45+
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
46+
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
4247
};
43-
}
48+
} // namespace lldb_private
4449

4550
#endif

lldb/source/Host/linux/HostInfoLinux.cpp

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ namespace {
3030
struct HostInfoLinuxFields {
3131
llvm::once_flag m_distribution_once_flag;
3232
std::string m_distribution_id;
33-
llvm::once_flag m_os_version_once_flag;
34-
llvm::VersionTuple m_os_version;
3533
};
3634
} // namespace
3735

@@ -50,33 +48,6 @@ void HostInfoLinux::Terminate() {
5048
HostInfoBase::Terminate();
5149
}
5250

53-
llvm::VersionTuple HostInfoLinux::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> HostInfoLinux::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-
8051
llvm::StringRef HostInfoLinux::GetDistributionId() {
8152
assert(g_fields && "Missing call to Initialize?");
8253
// Try to run 'lbs_release -i', and use that response for the distribution
@@ -167,35 +138,6 @@ FileSpec HostInfoLinux::GetProgramFileSpec() {
167138
return g_program_filespec;
168139
}
169140

170-
bool HostInfoLinux::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 HostInfoLinux::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 HostInfoLinux::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-
199141
void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
200142
ArchSpec &arch_64) {
201143
HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);

lldb/source/Host/posix/HostInfoPosix.cpp

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Host/posix/HostInfoPosix.h"
10+
#include "lldb/Host/Config.h"
11+
#include "lldb/Host/FileSystem.h"
12+
#include "lldb/Host/HostInfo.h"
1013
#include "lldb/Utility/Log.h"
1114
#include "lldb/Utility/UserIDResolver.h"
12-
1315
#include "llvm/ADT/SmallString.h"
1416
#include "llvm/ADT/Twine.h"
1517
#include "llvm/Support/Path.h"
1618
#include "llvm/Support/raw_ostream.h"
1719

1820
#include <climits>
21+
#include <cstdio>
1922
#include <cstdlib>
23+
#include <cstring>
2024
#include <grp.h>
2125
#include <mutex>
2226
#include <optional>
@@ -27,6 +31,31 @@
2731

2832
using namespace lldb_private;
2933

34+
namespace {
35+
struct HostInfoPosixFields {
36+
llvm::once_flag m_os_version_once_flag;
37+
llvm::VersionTuple m_os_version;
38+
};
39+
} // namespace
40+
41+
llvm::VersionTuple HostInfoPosix::GetOSVersion() {
42+
static HostInfoPosixFields *g_fields = new HostInfoPosixFields();
43+
assert(g_fields && "Missing call to Initialize?");
44+
llvm::call_once(g_fields->m_os_version_once_flag, []() {
45+
struct utsname un;
46+
if (uname(&un) != 0)
47+
return;
48+
49+
llvm::StringRef release = un.release;
50+
// The Linux kernel release string can include a lot of stuff (e.g.
51+
// 4.9.0-6-amd64). We're only interested in the numbered prefix.
52+
release = release.substr(0, release.find_first_not_of("0123456789."));
53+
g_fields->m_os_version.tryParse(release);
54+
});
55+
56+
return g_fields->m_os_version;
57+
}
58+
3059
size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
3160

3261
bool HostInfoPosix::GetHostname(std::string &s) {
@@ -47,6 +76,16 @@ std::optional<std::string> HostInfoPosix::GetOSKernelDescription() {
4776
return std::string(un.version);
4877
}
4978

79+
std::optional<std::string> HostInfoPosix::GetOSBuildString() {
80+
struct utsname un;
81+
::memset(&un, 0, sizeof(utsname));
82+
83+
if (uname(&un) < 0)
84+
return std::nullopt;
85+
86+
return std::string(un.release);
87+
}
88+
5089
#ifdef __ANDROID__
5190
#include <android/api-level.h>
5291
#endif
@@ -140,7 +179,32 @@ FileSpec HostInfoPosix::GetDefaultShell() {
140179
}
141180

142181
bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
143-
return ComputePathRelativeToLibrary(file_spec, "/bin");
182+
if (ComputePathRelativeToLibrary(file_spec, "/bin") &&
183+
file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
184+
return true;
185+
file_spec.SetDirectory(HostInfo::GetProgramFileSpec().GetDirectory());
186+
return !file_spec.GetDirectory().IsEmpty();
187+
}
188+
189+
bool HostInfoPosix::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
190+
FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins");
191+
FileSystem::Instance().Resolve(temp_file);
192+
file_spec.SetDirectory(temp_file.GetPath());
193+
return true;
194+
}
195+
196+
bool HostInfoPosix::ComputeUserPluginsDirectory(FileSpec &file_spec) {
197+
// XDG Base Directory Specification
198+
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
199+
// XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
200+
const char *xdg_data_home = getenv("XDG_DATA_HOME");
201+
if (xdg_data_home && xdg_data_home[0]) {
202+
std::string user_plugin_dir(xdg_data_home);
203+
user_plugin_dir += "/lldb";
204+
file_spec.SetDirectory(user_plugin_dir.c_str());
205+
} else
206+
file_spec.SetDirectory("~/.local/share/lldb");
207+
return true;
144208
}
145209

146210
bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {

0 commit comments

Comments
 (0)