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 
35 HostInfoLinuxFields *g_fields = nullptr;
36 }
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 
69  struct utsname un;
70  ::memset(&un, 0, sizeof(utsname));
71  s.clear();
72 
73  if (uname(&un) < 0)
74  return false;
75 
76  s.assign(un.release);
77  return true;
78 }
79 
81  struct utsname un;
82 
83  ::memset(&un, 0, sizeof(utsname));
84  s.clear();
85 
86  if (uname(&un) < 0)
87  return false;
88 
89  s.assign(un.version);
90  return true;
91 }
92 
94  assert(g_fields && "Missing call to Initialize?");
95  // Try to run 'lbs_release -i', and use that response for the distribution
96  // id.
97  llvm::call_once(g_fields->m_distribution_once_flag, []() {
98 
99  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
100  LLDB_LOGF(log, "attempting to determine Linux distribution...");
101 
102  // check if the lsb_release command exists at one of the following paths
103  const char *const exe_paths[] = {"/bin/lsb_release",
104  "/usr/bin/lsb_release"};
105 
106  for (size_t exe_index = 0;
107  exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
108  const char *const get_distribution_info_exe = exe_paths[exe_index];
109  if (access(get_distribution_info_exe, F_OK)) {
110  // this exe doesn't exist, move on to next exe
111  LLDB_LOGF(log, "executable doesn't exist: %s",
112  get_distribution_info_exe);
113  continue;
114  }
115 
116  // execute the distribution-retrieval command, read output
117  std::string get_distribution_id_command(get_distribution_info_exe);
118  get_distribution_id_command += " -i";
119 
120  FILE *file = popen(get_distribution_id_command.c_str(), "r");
121  if (!file) {
122  LLDB_LOGF(log,
123  "failed to run command: \"%s\", cannot retrieve "
124  "platform information",
125  get_distribution_id_command.c_str());
126  break;
127  }
128 
129  // retrieve the distribution id string.
130  char distribution_id[256] = {'\0'};
131  if (fgets(distribution_id, sizeof(distribution_id) - 1, file) !=
132  nullptr) {
133  LLDB_LOGF(log, "distribution id command returned \"%s\"",
134  distribution_id);
135 
136  const char *const distributor_id_key = "Distributor ID:\t";
137  if (strstr(distribution_id, distributor_id_key)) {
138  // strip newlines
139  std::string id_string(distribution_id + strlen(distributor_id_key));
140  id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
141  id_string.end());
142 
143  // lower case it and convert whitespace to underscores
144  std::transform(
145  id_string.begin(), id_string.end(), id_string.begin(),
146  [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
147 
148  g_fields->m_distribution_id = id_string;
149  LLDB_LOGF(log, "distribution id set to \"%s\"",
150  g_fields->m_distribution_id.c_str());
151  } else {
152  LLDB_LOGF(log, "failed to find \"%s\" field in \"%s\"",
153  distributor_id_key, distribution_id);
154  }
155  } else {
156  LLDB_LOGF(log,
157  "failed to retrieve distribution id, \"%s\" returned no"
158  " lines",
159  get_distribution_id_command.c_str());
160  }
161 
162  // clean up the file
163  pclose(file);
164  }
165  });
166 
167  return g_fields->m_distribution_id;
168 }
169 
170 FileSpec HostInfoLinux::GetProgramFileSpec() {
171  static FileSpec g_program_filespec;
172 
173  if (!g_program_filespec) {
174  char exe_path[PATH_MAX];
175  ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
176  if (len > 0) {
177  exe_path[len] = 0;
178  g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
179  }
180  }
181 
182  return g_program_filespec;
183 }
184 
187  file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
188  return true;
189  file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
190  return !file_spec.GetDirectory().IsEmpty();
191 }
192 
194  FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins");
195  FileSystem::Instance().Resolve(temp_file);
196  file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
197  return true;
198 }
199 
201  // XDG Base Directory Specification
202  // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
203  // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
204  const char *xdg_data_home = getenv("XDG_DATA_HOME");
205  if (xdg_data_home && xdg_data_home[0]) {
206  std::string user_plugin_dir(xdg_data_home);
207  user_plugin_dir += "/lldb";
208  file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
209  } else
210  file_spec.GetDirectory().SetCString("~/.local/share/lldb");
211  return true;
212 }
213 
215  ArchSpec &arch_64) {
217 
218  const char *distribution_id = GetDistributionId().data();
219 
220  // On Linux, "unknown" in the vendor slot isn't what we want for the default
221  // triple. It's probably an artifact of config.guess.
222  if (arch_32.IsValid()) {
223  arch_32.SetDistributionId(distribution_id);
224  if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
225  arch_32.GetTriple().setVendorName(llvm::StringRef());
226  }
227  if (arch_64.IsValid()) {
228  arch_64.SetDistributionId(distribution_id);
229  if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
230  arch_64.GetTriple().setVendorName(llvm::StringRef());
231  }
232 }
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:214
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:193
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:93
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::FileSystem::Resolve
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
Definition: common/FileSystem.cpp:255
g_fields
static HostInfoBaseFields * g_fields
Definition: HostInfoBase.cpp:71
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::GetProgramFileSpec
static FileSpec GetProgramFileSpec()
Definition: HostInfoLinux.cpp:170
lldb_private::HostInfoLinux::GetOSBuildString
static bool GetOSBuildString(std::string &s)
Definition: HostInfoLinux.cpp:68
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:200
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:185
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:80
lldb_private::HostInfoBase::Terminate
static void Terminate()
Definition: HostInfoBase.cpp:79