blob: 2b2207e7afa9ceb98deea539b1b380a2fc92bd4b [file] [log] [blame]
Zachary Turner97a14e62014-08-19 17:18:29 +00001//===-- HostInfoLinux.cpp ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner97a14e62014-08-19 17:18:29 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner97a14e62014-08-19 17:18:29 +00009#include "lldb/Host/linux/HostInfoLinux.h"
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +000010#include "lldb/Host/Config.h"
11#include "lldb/Host/FileSystem.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000012#include "lldb/Utility/Log.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000013
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include "llvm/Support/Threading.h"
15
Zachary Turnere47ffc32014-08-21 21:02:47 +000016#include <limits.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000017#include <stdio.h>
18#include <string.h>
19#include <sys/utsname.h>
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000020#include <unistd.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000021
22#include <algorithm>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000023#include <mutex>
Zachary Turner97a14e62014-08-19 17:18:29 +000024
25using namespace lldb_private;
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027namespace {
28struct HostInfoLinuxFields {
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 std::string m_distribution_id;
Pavel Labath2272c482018-06-18 15:02:23 +000030 llvm::VersionTuple m_os_version;
Zachary Turner673b6e42014-08-21 17:57:03 +000031};
32
33HostInfoLinuxFields *g_fields = nullptr;
34}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036void HostInfoLinux::Initialize() {
37 HostInfoPosix::Initialize();
Zachary Turner673b6e42014-08-21 17:57:03 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 g_fields = new HostInfoLinuxFields();
Zachary Turner673b6e42014-08-21 17:57:03 +000040}
Zachary Turner97a14e62014-08-19 17:18:29 +000041
Pavel Labath2272c482018-06-18 15:02:23 +000042llvm::VersionTuple HostInfoLinux::GetOSVersion() {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000043 static llvm::once_flag g_once_flag;
44 llvm::call_once(g_once_flag, []() {
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000045 struct utsname un;
Pavel Labath2272c482018-06-18 15:02:23 +000046 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);
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 });
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000055
Pavel Labath2272c482018-06-18 15:02:23 +000056 return g_fields->m_os_version;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000057}
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059bool HostInfoLinux::GetOSBuildString(std::string &s) {
60 struct utsname un;
61 ::memset(&un, 0, sizeof(utsname));
62 s.clear();
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 if (uname(&un) < 0)
65 return false;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 s.assign(un.release);
68 return true;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000069}
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
72 struct utsname un;
Zachary Turner97a14e62014-08-19 17:18:29 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 ::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
84llvm::StringRef HostInfoLinux::GetDistributionId() {
Adrian Prantl05097242018-04-30 16:49:04 +000085 // Try to run 'lbs_release -i', and use that response for the distribution
86 // id.
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000087 static llvm::once_flag g_once_flag;
88 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000089
90 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
91 if (log)
92 log->Printf("attempting to determine Linux distribution...");
93
Adrian Prantl05097242018-04-30 16:49:04 +000094 // check if the lsb_release command exists at one of the following paths
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 const char *const exe_paths[] = {"/bin/lsb_release",
96 "/usr/bin/lsb_release"};
97
98 for (size_t exe_index = 0;
99 exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
100 const char *const get_distribution_info_exe = exe_paths[exe_index];
101 if (access(get_distribution_info_exe, F_OK)) {
102 // this exe doesn't exist, move on to next exe
Zachary Turner97a14e62014-08-19 17:18:29 +0000103 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 log->Printf("executable doesn't exist: %s",
105 get_distribution_info_exe);
106 continue;
107 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 // execute the distribution-retrieval command, read output
110 std::string get_distribution_id_command(get_distribution_info_exe);
111 get_distribution_id_command += " -i";
Zachary Turner97a14e62014-08-19 17:18:29 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 FILE *file = popen(get_distribution_id_command.c_str(), "r");
114 if (!file) {
115 if (log)
116 log->Printf("failed to run command: \"%s\", cannot retrieve "
117 "platform information",
118 get_distribution_id_command.c_str());
119 break;
120 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 // retrieve the distribution id string.
123 char distribution_id[256] = {'\0'};
124 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) {
125 if (log)
126 log->Printf("distribution id command returned \"%s\"",
127 distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 const char *const distributor_id_key = "Distributor ID:\t";
130 if (strstr(distribution_id, distributor_id_key)) {
131 // strip newlines
132 std::string id_string(distribution_id + strlen(distributor_id_key));
133 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
134 id_string.end());
Zachary Turner97a14e62014-08-19 17:18:29 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 // lower case it and convert whitespace to underscores
137 std::transform(
138 id_string.begin(), id_string.end(), id_string.begin(),
139 [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
Zachary Turner97a14e62014-08-19 17:18:29 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 g_fields->m_distribution_id = id_string;
142 if (log)
143 log->Printf("distribution id set to \"%s\"",
144 g_fields->m_distribution_id.c_str());
145 } else {
146 if (log)
147 log->Printf("failed to find \"%s\" field in \"%s\"",
148 distributor_id_key, distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 } else {
151 if (log)
152 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
153 " lines",
154 get_distribution_id_command.c_str());
155 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 // clean up the file
158 pclose(file);
Zachary Turnera21fee02014-08-21 21:49:24 +0000159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 });
Zachary Turnera21fee02014-08-21 21:49:24 +0000161
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000162 return g_fields->m_distribution_id;
Zachary Turnera21fee02014-08-21 21:49:24 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165FileSpec HostInfoLinux::GetProgramFileSpec() {
166 static FileSpec g_program_filespec;
167
168 if (!g_program_filespec) {
169 char exe_path[PATH_MAX];
170 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
171 if (len > 0) {
172 exe_path[len] = 0;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000173 g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 }
175 }
176
177 return g_program_filespec;
Chaoren Linc30c49c2015-02-10 18:30:34 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) {
181 if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +0000182 file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000183 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
185 return !file_spec.GetDirectory().IsEmpty();
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000189 FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins");
190 FileSystem::Instance().Resolve(temp_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
192 return true;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000193}
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) {
196 // XDG Base Directory Specification
Adrian Prantl05097242018-04-30 16:49:04 +0000197 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
198 // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 const char *xdg_data_home = getenv("XDG_DATA_HOME");
200 if (xdg_data_home && xdg_data_home[0]) {
201 std::string user_plugin_dir(xdg_data_home);
202 user_plugin_dir += "/lldb";
203 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
204 } else
205 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
206 return true;
207}
Zachary Turner13b18262014-08-20 16:42:51 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
210 ArchSpec &arch_64) {
211 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
Zachary Turner13b18262014-08-20 16:42:51 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 const char *distribution_id = GetDistributionId().data();
214
215 // On Linux, "unknown" in the vendor slot isn't what we want for the default
216 // triple. It's probably an artifact of config.guess.
217 if (arch_32.IsValid()) {
218 arch_32.SetDistributionId(distribution_id);
219 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
220 arch_32.GetTriple().setVendorName(llvm::StringRef());
221 }
222 if (arch_64.IsValid()) {
223 arch_64.SetDistributionId(distribution_id);
224 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
225 arch_64.GetTriple().setVendorName(llvm::StringRef());
226 }
Zachary Turner13b18262014-08-20 16:42:51 +0000227}