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