blob: 8d59cda249e83cd45c3b971fcfd805a588e6f0a9 [file] [log] [blame]
Zachary Turner97a14e62014-08-19 17:18:29 +00001//===-- HostInfoLinux.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner97a14e62014-08-19 17:18:29 +000010#include "lldb/Host/linux/HostInfoLinux.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000011#include "lldb/Utility/Log.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000012
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000013#include "llvm/Support/Threading.h"
14
Zachary Turnere47ffc32014-08-21 21:02:47 +000015#include <limits.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000016#include <stdio.h>
17#include <string.h>
18#include <sys/utsname.h>
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000019#include <unistd.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000020
21#include <algorithm>
Greg Claytonff48e4b2015-02-03 02:05:44 +000022#include <mutex> // std::once
Zachary Turner97a14e62014-08-19 17:18:29 +000023
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026namespace {
27struct HostInfoLinuxFields {
28 HostInfoLinuxFields() : m_os_major(0), m_os_minor(0), m_os_update(0) {}
Zachary Turner673b6e42014-08-21 17:57:03 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 std::string m_distribution_id;
31 uint32_t m_os_major;
32 uint32_t m_os_minor;
33 uint32_t m_os_update;
Zachary Turner673b6e42014-08-21 17:57:03 +000034};
35
36HostInfoLinuxFields *g_fields = nullptr;
37}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039void HostInfoLinux::Initialize() {
40 HostInfoPosix::Initialize();
Zachary Turner673b6e42014-08-21 17:57:03 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 g_fields = new HostInfoLinuxFields();
Zachary Turner673b6e42014-08-21 17:57:03 +000043}
Zachary Turner97a14e62014-08-19 17:18:29 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor,
46 uint32_t &update) {
47 static bool success = false;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000048 static llvm::once_flag g_once_flag;
49 llvm::call_once(g_once_flag, []() {
Zachary Turner97a14e62014-08-19 17:18:29 +000050
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000051 struct utsname un;
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 if (uname(&un) == 0) {
53 int status = sscanf(un.release, "%u.%u.%u", &g_fields->m_os_major,
54 &g_fields->m_os_minor, &g_fields->m_os_update);
55 if (status == 3)
56 success = true;
57 else {
58 // Some kernels omit the update version, so try looking for just "X.Y"
59 // and
60 // set update to 0.
61 g_fields->m_os_update = 0;
62 status = sscanf(un.release, "%u.%u", &g_fields->m_os_major,
63 &g_fields->m_os_minor);
64 if (status == 2)
65 success = true;
66 }
67 }
68 });
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 major = g_fields->m_os_major;
71 minor = g_fields->m_os_minor;
72 update = g_fields->m_os_update;
73 return success;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076bool HostInfoLinux::GetOSBuildString(std::string &s) {
77 struct utsname un;
78 ::memset(&un, 0, sizeof(utsname));
79 s.clear();
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 if (uname(&un) < 0)
82 return false;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 s.assign(un.release);
85 return true;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
89 struct utsname un;
Zachary Turner97a14e62014-08-19 17:18:29 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 ::memset(&un, 0, sizeof(utsname));
92 s.clear();
93
94 if (uname(&un) < 0)
95 return false;
96
97 s.assign(un.version);
98 return true;
99}
100
101llvm::StringRef HostInfoLinux::GetDistributionId() {
102 // Try to run 'lbs_release -i', and use that response
103 // for the distribution id.
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000104 static llvm::once_flag g_once_flag;
105 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106
107 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
108 if (log)
109 log->Printf("attempting to determine Linux distribution...");
110
111 // check if the lsb_release command exists at one of the
112 // following paths
113 const char *const exe_paths[] = {"/bin/lsb_release",
114 "/usr/bin/lsb_release"};
115
116 for (size_t exe_index = 0;
117 exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
118 const char *const get_distribution_info_exe = exe_paths[exe_index];
119 if (access(get_distribution_info_exe, F_OK)) {
120 // this exe doesn't exist, move on to next exe
Zachary Turner97a14e62014-08-19 17:18:29 +0000121 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 log->Printf("executable doesn't exist: %s",
123 get_distribution_info_exe);
124 continue;
125 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 // execute the distribution-retrieval command, read output
128 std::string get_distribution_id_command(get_distribution_info_exe);
129 get_distribution_id_command += " -i";
Zachary Turner97a14e62014-08-19 17:18:29 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 FILE *file = popen(get_distribution_id_command.c_str(), "r");
132 if (!file) {
133 if (log)
134 log->Printf("failed to run command: \"%s\", cannot retrieve "
135 "platform information",
136 get_distribution_id_command.c_str());
137 break;
138 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 // retrieve the distribution id string.
141 char distribution_id[256] = {'\0'};
142 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) {
143 if (log)
144 log->Printf("distribution id command returned \"%s\"",
145 distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 const char *const distributor_id_key = "Distributor ID:\t";
148 if (strstr(distribution_id, distributor_id_key)) {
149 // strip newlines
150 std::string id_string(distribution_id + strlen(distributor_id_key));
151 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
152 id_string.end());
Zachary Turner97a14e62014-08-19 17:18:29 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 // lower case it and convert whitespace to underscores
155 std::transform(
156 id_string.begin(), id_string.end(), id_string.begin(),
157 [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
Zachary Turner97a14e62014-08-19 17:18:29 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 g_fields->m_distribution_id = id_string;
160 if (log)
161 log->Printf("distribution id set to \"%s\"",
162 g_fields->m_distribution_id.c_str());
163 } else {
164 if (log)
165 log->Printf("failed to find \"%s\" field in \"%s\"",
166 distributor_id_key, distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 } else {
169 if (log)
170 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
171 " lines",
172 get_distribution_id_command.c_str());
173 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 // clean up the file
176 pclose(file);
Zachary Turnera21fee02014-08-21 21:49:24 +0000177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 });
Zachary Turnera21fee02014-08-21 21:49:24 +0000179
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000180 return g_fields->m_distribution_id;
Zachary Turnera21fee02014-08-21 21:49:24 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183FileSpec HostInfoLinux::GetProgramFileSpec() {
184 static FileSpec g_program_filespec;
185
186 if (!g_program_filespec) {
187 char exe_path[PATH_MAX];
188 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
189 if (len > 0) {
190 exe_path[len] = 0;
191 g_program_filespec.SetFile(exe_path, false);
192 }
193 }
194
195 return g_program_filespec;
Chaoren Linc30c49c2015-02-10 18:30:34 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) {
199 if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
200 file_spec.IsAbsolute() && file_spec.Exists())
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000201 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
203 return !file_spec.GetDirectory().IsEmpty();
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000204}
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
207 FileSpec temp_file("/usr/lib/lldb/plugins", true);
208 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
209 return true;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000210}
211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) {
213 // XDG Base Directory Specification
214 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
215 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
216 const char *xdg_data_home = getenv("XDG_DATA_HOME");
217 if (xdg_data_home && xdg_data_home[0]) {
218 std::string user_plugin_dir(xdg_data_home);
219 user_plugin_dir += "/lldb";
220 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
221 } else
222 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
223 return true;
224}
Zachary Turner13b18262014-08-20 16:42:51 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
227 ArchSpec &arch_64) {
228 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
Zachary Turner13b18262014-08-20 16:42:51 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 const char *distribution_id = GetDistributionId().data();
231
232 // On Linux, "unknown" in the vendor slot isn't what we want for the default
233 // triple. It's probably an artifact of config.guess.
234 if (arch_32.IsValid()) {
235 arch_32.SetDistributionId(distribution_id);
236 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
237 arch_32.GetTriple().setVendorName(llvm::StringRef());
238 }
239 if (arch_64.IsValid()) {
240 arch_64.SetDistributionId(distribution_id);
241 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
242 arch_64.GetTriple().setVendorName(llvm::StringRef());
243 }
Zachary Turner13b18262014-08-20 16:42:51 +0000244}