blob: 2085df432070b48cc996fb633b0f47f7dccf9853 [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
10#include "lldb/Core/Log.h"
11#include "lldb/Host/linux/HostInfoLinux.h"
12
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
Zachary Turner673b6e42014-08-21 17:57:03 +000023namespace
24{
25struct HostInfoLinuxFields
26{
27 HostInfoLinuxFields()
28 : m_os_major(0)
29 , m_os_minor(0)
30 , m_os_update(0)
31 {
32 }
33
34 std::string m_distribution_id;
35 uint32_t m_os_major;
36 uint32_t m_os_minor;
37 uint32_t m_os_update;
38};
39
40HostInfoLinuxFields *g_fields = nullptr;
41}
42
43void
44HostInfoLinux::Initialize()
45{
46 HostInfoPosix::Initialize();
47
48 g_fields = new HostInfoLinuxFields();
49}
Zachary Turner97a14e62014-08-19 17:18:29 +000050
Zachary Turner39de3112014-09-09 20:54:56 +000051uint32_t
52HostInfoLinux::GetMaxThreadNameLength()
53{
54 return 16;
55}
56
Zachary Turner97a14e62014-08-19 17:18:29 +000057bool
58HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
59{
Zachary Turner97a14e62014-08-19 17:18:29 +000060 static bool success = false;
Greg Claytonff48e4b2015-02-03 02:05:44 +000061 static std::once_flag g_once_flag;
62 std::call_once(g_once_flag, []() {
Zachary Turner97a14e62014-08-19 17:18:29 +000063
Zachary Turner97a14e62014-08-19 17:18:29 +000064 struct utsname un;
Greg Claytonff48e4b2015-02-03 02:05:44 +000065 if (uname(&un) == 0)
Zachary Turner97a14e62014-08-19 17:18:29 +000066 {
Greg Claytonff48e4b2015-02-03 02:05:44 +000067 int status = sscanf(un.release, "%u.%u.%u", &g_fields->m_os_major, &g_fields->m_os_minor, &g_fields->m_os_update);
68 if (status == 3)
69 success = true;
70 else
71 {
72 // Some kernels omit the update version, so try looking for just "X.Y" and
73 // set update to 0.
74 g_fields->m_os_update = 0;
75 status = sscanf(un.release, "%u.%u", &g_fields->m_os_major, &g_fields->m_os_minor);
76 if (status == 2)
77 success = true;
78 }
Zachary Turner97a14e62014-08-19 17:18:29 +000079 }
Greg Claytonff48e4b2015-02-03 02:05:44 +000080 });
Zachary Turner97a14e62014-08-19 17:18:29 +000081
Zachary Turner97a14e62014-08-19 17:18:29 +000082
Zachary Turner673b6e42014-08-21 17:57:03 +000083 major = g_fields->m_os_major;
84 minor = g_fields->m_os_minor;
85 update = g_fields->m_os_update;
Zachary Turner97a14e62014-08-19 17:18:29 +000086 return success;
87}
88
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000089bool
90HostInfoLinux::GetOSBuildString(std::string &s)
91{
92 struct utsname un;
93 ::memset(&un, 0, sizeof(utsname));
94 s.clear();
95
96 if (uname(&un) < 0)
97 return false;
98
99 s.assign(un.release);
100 return true;
101}
102
103bool
104HostInfoLinux::GetOSKernelDescription(std::string &s)
105{
106 struct utsname un;
107
108 ::memset(&un, 0, sizeof(utsname));
109 s.clear();
110
111 if (uname(&un) < 0)
112 return false;
113
114 s.assign(un.version);
115 return true;
116}
117
Zachary Turner97a14e62014-08-19 17:18:29 +0000118llvm::StringRef
119HostInfoLinux::GetDistributionId()
120{
Zachary Turner97a14e62014-08-19 17:18:29 +0000121 // Try to run 'lbs_release -i', and use that response
122 // for the distribution id.
Greg Claytonff48e4b2015-02-03 02:05:44 +0000123 static bool success = false;
124 static std::once_flag g_once_flag;
125 std::call_once(g_once_flag, []() {
Zachary Turner97a14e62014-08-19 17:18:29 +0000126
127 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
128 if (log)
129 log->Printf("attempting to determine Linux distribution...");
130
131 // check if the lsb_release command exists at one of the
132 // following paths
133 const char *const exe_paths[] = {"/bin/lsb_release", "/usr/bin/lsb_release"};
134
135 for (size_t exe_index = 0; exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index)
136 {
137 const char *const get_distribution_info_exe = exe_paths[exe_index];
138 if (access(get_distribution_info_exe, F_OK))
139 {
140 // this exe doesn't exist, move on to next exe
141 if (log)
142 log->Printf("executable doesn't exist: %s", get_distribution_info_exe);
143 continue;
144 }
145
146 // execute the distribution-retrieval command, read output
147 std::string get_distribution_id_command(get_distribution_info_exe);
148 get_distribution_id_command += " -i";
149
150 FILE *file = popen(get_distribution_id_command.c_str(), "r");
151 if (!file)
152 {
153 if (log)
154 log->Printf("failed to run command: \"%s\", cannot retrieve "
155 "platform information",
156 get_distribution_id_command.c_str());
157 break;
158 }
159
160 // retrieve the distribution id string.
161 char distribution_id[256] = {'\0'};
162 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL)
163 {
164 if (log)
165 log->Printf("distribution id command returned \"%s\"", distribution_id);
166
167 const char *const distributor_id_key = "Distributor ID:\t";
168 if (strstr(distribution_id, distributor_id_key))
169 {
170 // strip newlines
171 std::string id_string(distribution_id + strlen(distributor_id_key));
172 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), id_string.end());
173
174 // lower case it and convert whitespace to underscores
175 std::transform(id_string.begin(), id_string.end(), id_string.begin(), [](char ch)
176 {
177 return tolower(isspace(ch) ? '_' : ch);
178 });
179
Zachary Turner673b6e42014-08-21 17:57:03 +0000180 g_fields->m_distribution_id = id_string;
Zachary Turner97a14e62014-08-19 17:18:29 +0000181 if (log)
Zachary Turner673b6e42014-08-21 17:57:03 +0000182 log->Printf("distribution id set to \"%s\"", g_fields->m_distribution_id.c_str());
Zachary Turner97a14e62014-08-19 17:18:29 +0000183 }
184 else
185 {
186 if (log)
187 log->Printf("failed to find \"%s\" field in \"%s\"", distributor_id_key, distribution_id);
188 }
189 }
190 else
191 {
192 if (log)
193 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
194 " lines",
195 get_distribution_id_command.c_str());
196 }
197
198 // clean up the file
199 pclose(file);
200 }
Greg Claytonff48e4b2015-02-03 02:05:44 +0000201 });
Zachary Turner97a14e62014-08-19 17:18:29 +0000202
Zachary Turner673b6e42014-08-21 17:57:03 +0000203 return g_fields->m_distribution_id.c_str();
Zachary Turner97a14e62014-08-19 17:18:29 +0000204}
Zachary Turner13b18262014-08-20 16:42:51 +0000205
Zachary Turnera21fee02014-08-21 21:49:24 +0000206FileSpec
207HostInfoLinux::GetProgramFileSpec()
208{
209 static FileSpec g_program_filespec;
210
211 if (!g_program_filespec)
212 {
213 char exe_path[PATH_MAX];
214 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
215 if (len > 0)
216 {
217 exe_path[len] = 0;
218 g_program_filespec.SetFile(exe_path, false);
219 }
220 }
221
222 return g_program_filespec;
223}
224
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000225bool
Chaoren Linc30c49c2015-02-10 18:30:34 +0000226HostInfoLinux::ComputeSharedLibraryDirectory(FileSpec &file_spec)
227{
228 if (HostInfoPosix::ComputeSharedLibraryDirectory(file_spec))
229 return true;
230 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
231 return (bool)file_spec.GetDirectory();
232}
233
234bool
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000235HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec)
236{
Greg Clayton0d8400c2014-08-25 18:21:06 +0000237 FileSpec temp_file("/usr/lib/lldb", true);
238 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000239 return true;
240}
241
242bool
243HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec)
244{
245 // XDG Base Directory Specification
246 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
247 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000248 const char *xdg_data_home = getenv("XDG_DATA_HOME");
249 if (xdg_data_home && xdg_data_home[0])
250 {
251 std::string user_plugin_dir(xdg_data_home);
252 user_plugin_dir += "/lldb";
Greg Clayton0d8400c2014-08-25 18:21:06 +0000253 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000254 }
255 else
Greg Clayton0d8400c2014-08-25 18:21:06 +0000256 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000257 return true;
258}
259
Zachary Turner13b18262014-08-20 16:42:51 +0000260void
261HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
262{
263 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
264
265 const char *distribution_id = GetDistributionId().data();
266
267 // On Linux, "unknown" in the vendor slot isn't what we want for the default
268 // triple. It's probably an artifact of config.guess.
269 if (arch_32.IsValid())
270 {
271 arch_32.SetDistributionId(distribution_id);
272 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
273 arch_32.GetTriple().setVendorName("");
274 }
275 if (arch_64.IsValid())
276 {
277 arch_64.SetDistributionId(distribution_id);
278 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
279 arch_64.GetTriple().setVendorName("");
280 }
281}