Skip to content

Commit b8807f8

Browse files
Moving APIs from HostInfoLinux to HostInfoPosix
1 parent 04313b8 commit b8807f8

File tree

4 files changed

+97
-69
lines changed

4 files changed

+97
-69
lines changed

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

Lines changed: 1 addition & 8 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,12 @@ 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();
32-
static FileSpec GetProgramFileSpec();
3329

3430
protected:
35-
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
36-
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
37-
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
3831
static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
3932
ArchSpec &arch_64);
4033
};
41-
}
34+
} // namespace lldb_private
4235

4336
#endif

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

Lines changed: 7 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

@@ -31,15 +32,20 @@ class HostInfoPosix : public HostInfoBase {
3132
static uint32_t GetEffectiveGroupID();
3233

3334
static FileSpec GetDefaultShell();
35+
static FileSpec GetProgramFileSpec();
3436

3537
static bool GetEnvironmentVar(const std::string &var_name, std::string &var);
3638

3739
static UserIDResolver &GetUserIDResolver();
40+
static llvm::VersionTuple GetOSVersion();
41+
static std::optional<std::string> GetOSBuildString();
3842

3943
protected:
4044
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
4145
static bool ComputeHeaderDirectory(FileSpec &file_spec);
46+
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
47+
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
4248
};
43-
}
49+
} // namespace lldb_private
4450

4551
#endif

lldb/source/Host/linux/HostInfoLinux.cpp

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ 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;
33+
// llvm::once_flag m_os_version_once_flag;
34+
// llvm::VersionTuple m_os_version;
3535
};
3636
} // namespace
3737

@@ -50,7 +50,7 @@ void HostInfoLinux::Terminate() {
5050
HostInfoBase::Terminate();
5151
}
5252

53-
llvm::VersionTuple HostInfoLinux::GetOSVersion() {
53+
/*llvm::VersionTuple HostInfoPosix::GetOSVersion() {
5454
assert(g_fields && "Missing call to Initialize?");
5555
llvm::call_once(g_fields->m_os_version_once_flag, []() {
5656
struct utsname un;
@@ -66,17 +66,7 @@ llvm::VersionTuple HostInfoLinux::GetOSVersion() {
6666
6767
return g_fields->m_os_version;
6868
}
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-
69+
*/
8070
llvm::StringRef HostInfoLinux::GetDistributionId() {
8171
assert(g_fields && "Missing call to Initialize?");
8272
// Try to run 'lbs_release -i', and use that response for the distribution
@@ -152,50 +142,6 @@ llvm::StringRef HostInfoLinux::GetDistributionId() {
152142
return g_fields->m_distribution_id;
153143
}
154144

155-
FileSpec HostInfoLinux::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 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-
199145
void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
200146
ArchSpec &arch_64) {
201147
HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);

lldb/source/Host/posix/HostInfoPosix.cpp

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

99
#include "lldb/Host/posix/HostInfoPosix.h"
10+
#include "lldb/Host/Config.h"
11+
#include "lldb/Host/FileSystem.h"
1012
#include "lldb/Utility/Log.h"
1113
#include "lldb/Utility/UserIDResolver.h"
12-
1314
#include "llvm/ADT/SmallString.h"
1415
#include "llvm/ADT/Twine.h"
1516
#include "llvm/Support/Path.h"
1617
#include "llvm/Support/raw_ostream.h"
1718

1819
#include <climits>
20+
#include <cstdio>
1921
#include <cstdlib>
22+
#include <cstring>
2023
#include <grp.h>
2124
#include <mutex>
2225
#include <optional>
@@ -27,6 +30,31 @@
2730

2831
using namespace lldb_private;
2932

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

3260
bool HostInfoPosix::GetHostname(std::string &s) {
@@ -47,6 +75,16 @@ std::optional<std::string> HostInfoPosix::GetOSKernelDescription() {
4775
return std::string(un.version);
4876
}
4977

78+
std::optional<std::string> HostInfoPosix::GetOSBuildString() {
79+
struct utsname un;
80+
::memset(&un, 0, sizeof(utsname));
81+
82+
if (uname(&un) < 0)
83+
return std::nullopt;
84+
85+
return std::string(un.release);
86+
}
87+
5088
#ifdef __ANDROID__
5189
#include <android/api-level.h>
5290
#endif
@@ -139,8 +177,53 @@ FileSpec HostInfoPosix::GetDefaultShell() {
139177
return FileSpec("/bin/sh");
140178
}
141179

180+
FileSpec HostInfoPosix::GetProgramFileSpec() {
181+
static FileSpec g_program_filespec;
182+
183+
if (!g_program_filespec) {
184+
char exe_path[PATH_MAX];
185+
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
186+
if (len > 0) {
187+
exe_path[len] = 0;
188+
g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
189+
}
190+
}
191+
192+
return g_program_filespec;
193+
}
194+
195+
// Keeping the original one for reference
196+
// bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
197+
// return ComputePathRelativeToLibrary(file_spec, "/bin");
198+
// }
199+
142200
bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
143-
return ComputePathRelativeToLibrary(file_spec, "/bin");
201+
if (ComputePathRelativeToLibrary(file_spec, "/bin") &&
202+
file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
203+
return true;
204+
file_spec.SetDirectory(GetProgramFileSpec().GetDirectory());
205+
return !file_spec.GetDirectory().IsEmpty();
206+
}
207+
208+
bool HostInfoPosix::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
209+
FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins");
210+
FileSystem::Instance().Resolve(temp_file);
211+
file_spec.SetDirectory(temp_file.GetPath());
212+
return true;
213+
}
214+
215+
bool HostInfoPosix::ComputeUserPluginsDirectory(FileSpec &file_spec) {
216+
// XDG Base Directory Specification
217+
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
218+
// XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
219+
const char *xdg_data_home = getenv("XDG_DATA_HOME");
220+
if (xdg_data_home && xdg_data_home[0]) {
221+
std::string user_plugin_dir(xdg_data_home);
222+
user_plugin_dir += "/lldb";
223+
file_spec.SetDirectory(user_plugin_dir.c_str());
224+
} else
225+
file_spec.SetDirectory("~/.local/share/lldb");
226+
return true;
144227
}
145228

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

0 commit comments

Comments
 (0)