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