LLDB  mainline
HostInfoLinux.cpp
Go to the documentation of this file.
1 //===-- HostInfoLinux.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 
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 
26 using namespace lldb_private;
27 
28 namespace {
29 struct HostInfoLinuxFields {
30  llvm::once_flag m_distribution_once_flag;
31  std::string m_distribution_id;
32  llvm::once_flag m_os_version_once_flag;
33  llvm::VersionTuple m_os_version;
34 };
35 } // namespace
36 
37 static HostInfoLinuxFields *g_fields = nullptr;
38 
41 
42  g_fields = new HostInfoLinuxFields();
43 }
44 
46  assert(g_fields && "Missing call to Initialize?");
47  delete g_fields;
48  g_fields = nullptr;
50 }
51 
52 llvm::VersionTuple HostInfoLinux::GetOSVersion() {
53  assert(g_fields && "Missing call to Initialize?");
54  llvm::call_once(g_fields->m_os_version_once_flag, []() {
55  struct utsname un;
56  if (uname(&un) != 0)
57  return;
58 
59  llvm::StringRef release = un.release;
60  // The kernel release string can include a lot of stuff (e.g.
61  // 4.9.0-6-amd64). We're only interested in the numbered prefix.
62  release = release.substr(0, release.find_first_not_of("0123456789."));
63  g_fields->m_os_version.tryParse(release);
64  });
65 
66  return g_fields->m_os_version;
67 }
68 
69 llvm::Optional<std::string> HostInfoLinux::GetOSBuildString() {
70  struct utsname un;
71  ::memset(&un, 0, sizeof(utsname));
72 
73  if (uname(&un) < 0)
74  return llvm::None;
75 
76  return std::string(un.release);
77 }
78 
80  assert(g_fields && "Missing call to Initialize?");
81  // Try to run 'lbs_release -i', and use that response for the distribution
82  // id.
83  llvm::call_once(g_fields->m_distribution_once_flag, []() {
84  Log *log = GetLog(LLDBLog::Host);
85  LLDB_LOGF(log, "attempting to determine Linux distribution...");
86 
87  // check if the lsb_release command exists at one of the following paths
88  const char *const exe_paths[] = {"/bin/lsb_release",
89  "/usr/bin/lsb_release"};
90 
91  for (size_t exe_index = 0;
92  exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
93  const char *const get_distribution_info_exe = exe_paths[exe_index];
94  if (access(get_distribution_info_exe, F_OK)) {
95  // this exe doesn't exist, move on to next exe
96  LLDB_LOGF(log, "executable doesn't exist: %s",
97  get_distribution_info_exe);
98  continue;
99  }
100 
101  // execute the distribution-retrieval command, read output
102  std::string get_distribution_id_command(get_distribution_info_exe);
103  get_distribution_id_command += " -i";
104 
105  FILE *file = popen(get_distribution_id_command.c_str(), "r");
106  if (!file) {
107  LLDB_LOGF(log,
108  "failed to run command: \"%s\", cannot retrieve "
109  "platform information",
110  get_distribution_id_command.c_str());
111  break;
112  }
113 
114  // retrieve the distribution id string.
115  char distribution_id[256] = {'\0'};
116  if (fgets(distribution_id, sizeof(distribution_id) - 1, file) !=
117  nullptr) {
118  LLDB_LOGF(log, "distribution id command returned \"%s\"",
119  distribution_id);
120 
121  const char *const distributor_id_key = "Distributor ID:\t";
122  if (strstr(distribution_id, distributor_id_key)) {
123  // strip newlines
124  std::string id_string(distribution_id + strlen(distributor_id_key));
125  llvm::erase_value(id_string, '\n');
126 
127  // lower case it and convert whitespace to underscores
128  std::transform(
129  id_string.begin(), id_string.end(), id_string.begin(),
130  [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
131 
132  g_fields->m_distribution_id = id_string;
133  LLDB_LOGF(log, "distribution id set to \"%s\"",
134  g_fields->m_distribution_id.c_str());
135  } else {
136  LLDB_LOGF(log, "failed to find \"%s\" field in \"%s\"",
137  distributor_id_key, distribution_id);
138  }
139  } else {
140  LLDB_LOGF(log,
141  "failed to retrieve distribution id, \"%s\" returned no"
142  " lines",
143  get_distribution_id_command.c_str());
144  }
145 
146  // clean up the file
147  pclose(file);
148  }
149  });
150 
151  return g_fields->m_distribution_id;
152 }
153 
154 FileSpec HostInfoLinux::GetProgramFileSpec() {
155  static FileSpec g_program_filespec;
156 
157  if (!g_program_filespec) {
158  char exe_path[PATH_MAX];
159  ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
160  if (len > 0) {
161  exe_path[len] = 0;
162  g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
163  }
164  }
165 
166  return g_program_filespec;
167 }
168 
171  file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
172  return true;
173  file_spec.SetDirectory(GetProgramFileSpec().GetDirectory());
174  return !file_spec.GetDirectory().IsEmpty();
175 }
176 
178  FileSpec temp_file("/usr/" LLDB_INSTALL_LIBDIR_BASENAME "/lldb/plugins");
179  FileSystem::Instance().Resolve(temp_file);
180  file_spec.SetDirectory(temp_file.GetPath());
181  return true;
182 }
183 
185  // XDG Base Directory Specification
186  // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
187  // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
188  const char *xdg_data_home = getenv("XDG_DATA_HOME");
189  if (xdg_data_home && xdg_data_home[0]) {
190  std::string user_plugin_dir(xdg_data_home);
191  user_plugin_dir += "/lldb";
192  file_spec.SetDirectory(user_plugin_dir.c_str());
193  } else
194  file_spec.SetDirectory("~/.local/share/lldb");
195  return true;
196 }
197 
199  ArchSpec &arch_64) {
201 
202  const char *distribution_id = GetDistributionId().data();
203 
204  // On Linux, "unknown" in the vendor slot isn't what we want for the default
205  // triple. It's probably an artifact of config.guess.
206  if (arch_32.IsValid()) {
207  arch_32.SetDistributionId(distribution_id);
208  if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
209  arch_32.GetTriple().setVendorName(llvm::StringRef());
210  }
211  if (arch_64.IsValid()) {
212  arch_64.SetDistributionId(distribution_id);
213  if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
214  arch_64.GetTriple().setVendorName(llvm::StringRef());
215  }
216 }
g_fields
static HostInfoLinuxFields * g_fields
Definition: HostInfoLinux.cpp:37
lldb_private::ArchSpec
Definition: ArchSpec.h:32
FileSystem.h
lldb_private::HostInfoLinux::ComputeHostArchitectureSupport
static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
Definition: HostInfoLinux.cpp:198
lldb_private::HostInfoPosix::ComputeSupportExeDirectory
static bool ComputeSupportExeDirectory(FileSpec &file_spec)
Definition: HostInfoPosix.cpp:141
lldb_private::HostInfoLinux::Terminate
static void Terminate()
Definition: HostInfoLinux.cpp:45
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:344
lldb_private::HostInfoBase::Initialize
static void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
Definition: HostInfoBase.cpp:75
lldb_private::HostInfoLinux::ComputeSystemPluginsDirectory
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec)
Definition: HostInfoLinux.cpp:177
lldb_private::FileSystem::Exists
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
Definition: common/FileSystem.cpp:150
lldb_private::HostInfoBase::SharedLibraryDirectoryHelper
void(FileSpec &this_file) SharedLibraryDirectoryHelper
A helper function for determining the liblldb location.
Definition: HostInfoBase.h:44
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:454
lldb_private::FileSpec
Definition: FileSpec.h:55
HostInfoLinux.h
Log.h
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:303
lldb_private::ArchSpec::SetDistributionId
void SetDistributionId(const char *distribution_id)
Set the distribution id of the architecture.
Definition: ArchSpec.cpp:678
lldb_private::HostInfoLinux::Initialize
static void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
Definition: HostInfoLinux.cpp:39
lldb_private::HostInfoLinux::GetDistributionId
static llvm::StringRef GetDistributionId()
Definition: HostInfoLinux.cpp:79
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::FileSystem::Resolve
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
Definition: common/FileSystem.cpp:235
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:352
lldb_private::FileSpec::IsAbsolute
bool IsAbsolute() const
Returns true if the filespec represents an absolute path.
Definition: FileSpec.cpp:495
lldb_private::HostInfoBase::ComputeHostArchitectureSupport
static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
Definition: HostInfoBase.cpp:326
lldb_private::HostInfoLinux::GetOSBuildString
static llvm::Optional< std::string > GetOSBuildString()
Definition: HostInfoLinux.cpp:69
lldb_private::FileSpec::GetDirectory
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition: FileSpec.h:222
lldb_private::FileSpec::SetDirectory
void SetDirectory(ConstString directory)
Directory string set accessor.
Definition: FileSpec.cpp:332
lldb_private::HostInfoLinux::GetProgramFileSpec
static FileSpec GetProgramFileSpec()
Definition: HostInfoLinux.cpp:154
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:46
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::HostInfoLinux::GetOSVersion
static llvm::VersionTuple GetOSVersion()
Definition: HostInfoLinux.cpp:52
lldb_private::HostInfoLinux::ComputeUserPluginsDirectory
static bool ComputeUserPluginsDirectory(FileSpec &file_spec)
Definition: HostInfoLinux.cpp:184
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:172
lldb_private::HostInfoLinux::ComputeSupportExeDirectory
static bool ComputeSupportExeDirectory(FileSpec &file_spec)
Definition: HostInfoLinux.cpp:169
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:364
LLDBLog.h
lldb_private::HostInfoBase::Terminate
static void Terminate()
Definition: HostInfoBase.cpp:80