blob: 2a01f0676142622afcba363c8061f4e364cc6004 [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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "lldb/Core/Log.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000012
Zachary Turnere47ffc32014-08-21 21:02:47 +000013#include <limits.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000014#include <stdio.h>
15#include <string.h>
16#include <sys/utsname.h>
17
18#include <algorithm>
Greg Claytonff48e4b2015-02-03 02:05:44 +000019#include <mutex> // std::once
Zachary Turner97a14e62014-08-19 17:18:29 +000020
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023namespace {
24struct HostInfoLinuxFields {
25 HostInfoLinuxFields() : m_os_major(0), m_os_minor(0), m_os_update(0) {}
Zachary Turner673b6e42014-08-21 17:57:03 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 std::string m_distribution_id;
28 uint32_t m_os_major;
29 uint32_t m_os_minor;
30 uint32_t m_os_update;
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
Kate Stoneb9c1b512016-09-06 20:57:50 +000042uint32_t HostInfoLinux::GetMaxThreadNameLength() { return 16; }
Zachary Turner39de3112014-09-09 20:54:56 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044bool HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor,
45 uint32_t &update) {
46 static bool success = false;
47 static std::once_flag g_once_flag;
48 std::call_once(g_once_flag, []() {
Zachary Turner97a14e62014-08-19 17:18:29 +000049
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000050 struct utsname un;
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 if (uname(&un) == 0) {
52 int status = sscanf(un.release, "%u.%u.%u", &g_fields->m_os_major,
53 &g_fields->m_os_minor, &g_fields->m_os_update);
54 if (status == 3)
55 success = true;
56 else {
57 // Some kernels omit the update version, so try looking for just "X.Y"
58 // and
59 // set update to 0.
60 g_fields->m_os_update = 0;
61 status = sscanf(un.release, "%u.%u", &g_fields->m_os_major,
62 &g_fields->m_os_minor);
63 if (status == 2)
64 success = true;
65 }
66 }
67 });
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 major = g_fields->m_os_major;
70 minor = g_fields->m_os_minor;
71 update = g_fields->m_os_update;
72 return success;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075bool HostInfoLinux::GetOSBuildString(std::string &s) {
76 struct utsname un;
77 ::memset(&un, 0, sizeof(utsname));
78 s.clear();
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 if (uname(&un) < 0)
81 return false;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 s.assign(un.release);
84 return true;
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
88 struct utsname un;
Zachary Turner97a14e62014-08-19 17:18:29 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 ::memset(&un, 0, sizeof(utsname));
91 s.clear();
92
93 if (uname(&un) < 0)
94 return false;
95
96 s.assign(un.version);
97 return true;
98}
99
100llvm::StringRef HostInfoLinux::GetDistributionId() {
101 // Try to run 'lbs_release -i', and use that response
102 // for the distribution id.
103 static std::once_flag g_once_flag;
104 std::call_once(g_once_flag, []() {
105
106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
107 if (log)
108 log->Printf("attempting to determine Linux distribution...");
109
110 // check if the lsb_release command exists at one of the
111 // following paths
112 const char *const exe_paths[] = {"/bin/lsb_release",
113 "/usr/bin/lsb_release"};
114
115 for (size_t exe_index = 0;
116 exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
117 const char *const get_distribution_info_exe = exe_paths[exe_index];
118 if (access(get_distribution_info_exe, F_OK)) {
119 // this exe doesn't exist, move on to next exe
Zachary Turner97a14e62014-08-19 17:18:29 +0000120 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 log->Printf("executable doesn't exist: %s",
122 get_distribution_info_exe);
123 continue;
124 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 // execute the distribution-retrieval command, read output
127 std::string get_distribution_id_command(get_distribution_info_exe);
128 get_distribution_id_command += " -i";
Zachary Turner97a14e62014-08-19 17:18:29 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 FILE *file = popen(get_distribution_id_command.c_str(), "r");
131 if (!file) {
132 if (log)
133 log->Printf("failed to run command: \"%s\", cannot retrieve "
134 "platform information",
135 get_distribution_id_command.c_str());
136 break;
137 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 // retrieve the distribution id string.
140 char distribution_id[256] = {'\0'};
141 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) {
142 if (log)
143 log->Printf("distribution id command returned \"%s\"",
144 distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 const char *const distributor_id_key = "Distributor ID:\t";
147 if (strstr(distribution_id, distributor_id_key)) {
148 // strip newlines
149 std::string id_string(distribution_id + strlen(distributor_id_key));
150 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
151 id_string.end());
Zachary Turner97a14e62014-08-19 17:18:29 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 // lower case it and convert whitespace to underscores
154 std::transform(
155 id_string.begin(), id_string.end(), id_string.begin(),
156 [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
Zachary Turner97a14e62014-08-19 17:18:29 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 g_fields->m_distribution_id = id_string;
159 if (log)
160 log->Printf("distribution id set to \"%s\"",
161 g_fields->m_distribution_id.c_str());
162 } else {
163 if (log)
164 log->Printf("failed to find \"%s\" field in \"%s\"",
165 distributor_id_key, distribution_id);
Zachary Turner97a14e62014-08-19 17:18:29 +0000166 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 } else {
168 if (log)
169 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
170 " lines",
171 get_distribution_id_command.c_str());
172 }
Zachary Turner97a14e62014-08-19 17:18:29 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 // clean up the file
175 pclose(file);
Zachary Turnera21fee02014-08-21 21:49:24 +0000176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 });
Zachary Turnera21fee02014-08-21 21:49:24 +0000178
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000179 return g_fields->m_distribution_id;
Zachary Turnera21fee02014-08-21 21:49:24 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182FileSpec HostInfoLinux::GetProgramFileSpec() {
183 static FileSpec g_program_filespec;
184
185 if (!g_program_filespec) {
186 char exe_path[PATH_MAX];
187 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
188 if (len > 0) {
189 exe_path[len] = 0;
190 g_program_filespec.SetFile(exe_path, false);
191 }
192 }
193
194 return g_program_filespec;
Chaoren Linc30c49c2015-02-10 18:30:34 +0000195}
196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) {
198 if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
199 file_spec.IsAbsolute() && file_spec.Exists())
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000200 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
202 return !file_spec.GetDirectory().IsEmpty();
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
206 FileSpec temp_file("/usr/lib/lldb/plugins", true);
207 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
208 return true;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) {
212 // XDG Base Directory Specification
213 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
214 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
215 const char *xdg_data_home = getenv("XDG_DATA_HOME");
216 if (xdg_data_home && xdg_data_home[0]) {
217 std::string user_plugin_dir(xdg_data_home);
218 user_plugin_dir += "/lldb";
219 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
220 } else
221 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
222 return true;
223}
Zachary Turner13b18262014-08-20 16:42:51 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
226 ArchSpec &arch_64) {
227 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
Zachary Turner13b18262014-08-20 16:42:51 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 const char *distribution_id = GetDistributionId().data();
230
231 // On Linux, "unknown" in the vendor slot isn't what we want for the default
232 // triple. It's probably an artifact of config.guess.
233 if (arch_32.IsValid()) {
234 arch_32.SetDistributionId(distribution_id);
235 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
236 arch_32.GetTriple().setVendorName(llvm::StringRef());
237 }
238 if (arch_64.IsValid()) {
239 arch_64.SetDistributionId(distribution_id);
240 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
241 arch_64.GetTriple().setVendorName(llvm::StringRef());
242 }
Zachary Turner13b18262014-08-20 16:42:51 +0000243}