blob: bace2586aad1c3263ef837fb371a3ac23b49804b [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>
19
20using namespace lldb_private;
21
Zachary Turner673b6e42014-08-21 17:57:03 +000022namespace
23{
24struct HostInfoLinuxFields
25{
26 HostInfoLinuxFields()
27 : m_os_major(0)
28 , m_os_minor(0)
29 , m_os_update(0)
30 {
31 }
32
33 std::string m_distribution_id;
34 uint32_t m_os_major;
35 uint32_t m_os_minor;
36 uint32_t m_os_update;
37};
38
39HostInfoLinuxFields *g_fields = nullptr;
40}
41
42void
43HostInfoLinux::Initialize()
44{
45 HostInfoPosix::Initialize();
46
47 g_fields = new HostInfoLinuxFields();
48}
Zachary Turner97a14e62014-08-19 17:18:29 +000049
Zachary Turner39de3112014-09-09 20:54:56 +000050uint32_t
51HostInfoLinux::GetMaxThreadNameLength()
52{
53 return 16;
54}
55
Zachary Turner97a14e62014-08-19 17:18:29 +000056bool
57HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
58{
59 static bool is_initialized = false;
60 static bool success = false;
61
62 if (!is_initialized)
63 {
64 is_initialized = true;
65 struct utsname un;
66
67 if (uname(&un))
68 goto finished;
69
Zachary Turner673b6e42014-08-21 17:57:03 +000070 int status = sscanf(un.release, "%u.%u.%u", &g_fields->m_os_major, &g_fields->m_os_minor, &g_fields->m_os_update);
Zachary Turner97a14e62014-08-19 17:18:29 +000071 if (status == 3)
72 {
73 success = true;
74 goto finished;
75 }
76
77 // Some kernels omit the update version, so try looking for just "X.Y" and
78 // set update to 0.
Zachary Turner673b6e42014-08-21 17:57:03 +000079 g_fields->m_os_update = 0;
80 status = sscanf(un.release, "%u.%u", &g_fields->m_os_major, &g_fields->m_os_minor);
Zachary Turner97a14e62014-08-19 17:18:29 +000081 success = !!(status == 2);
82 }
83
84finished:
Zachary Turner673b6e42014-08-21 17:57:03 +000085 major = g_fields->m_os_major;
86 minor = g_fields->m_os_minor;
87 update = g_fields->m_os_update;
Zachary Turner97a14e62014-08-19 17:18:29 +000088 return success;
89}
90
Oleksiy Vyalov53c038a2014-12-09 02:13:05 +000091bool
92HostInfoLinux::GetOSBuildString(std::string &s)
93{
94 struct utsname un;
95 ::memset(&un, 0, sizeof(utsname));
96 s.clear();
97
98 if (uname(&un) < 0)
99 return false;
100
101 s.assign(un.release);
102 return true;
103}
104
105bool
106HostInfoLinux::GetOSKernelDescription(std::string &s)
107{
108 struct utsname un;
109
110 ::memset(&un, 0, sizeof(utsname));
111 s.clear();
112
113 if (uname(&un) < 0)
114 return false;
115
116 s.assign(un.version);
117 return true;
118}
119
Zachary Turner97a14e62014-08-19 17:18:29 +0000120llvm::StringRef
121HostInfoLinux::GetDistributionId()
122{
123 static bool is_initialized = false;
124 // Try to run 'lbs_release -i', and use that response
125 // for the distribution id.
126
127 if (!is_initialized)
128 {
129 is_initialized = true;
130
131 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
132 if (log)
133 log->Printf("attempting to determine Linux distribution...");
134
135 // check if the lsb_release command exists at one of the
136 // following paths
137 const char *const exe_paths[] = {"/bin/lsb_release", "/usr/bin/lsb_release"};
138
139 for (size_t exe_index = 0; exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index)
140 {
141 const char *const get_distribution_info_exe = exe_paths[exe_index];
142 if (access(get_distribution_info_exe, F_OK))
143 {
144 // this exe doesn't exist, move on to next exe
145 if (log)
146 log->Printf("executable doesn't exist: %s", get_distribution_info_exe);
147 continue;
148 }
149
150 // execute the distribution-retrieval command, read output
151 std::string get_distribution_id_command(get_distribution_info_exe);
152 get_distribution_id_command += " -i";
153
154 FILE *file = popen(get_distribution_id_command.c_str(), "r");
155 if (!file)
156 {
157 if (log)
158 log->Printf("failed to run command: \"%s\", cannot retrieve "
159 "platform information",
160 get_distribution_id_command.c_str());
161 break;
162 }
163
164 // retrieve the distribution id string.
165 char distribution_id[256] = {'\0'};
166 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL)
167 {
168 if (log)
169 log->Printf("distribution id command returned \"%s\"", distribution_id);
170
171 const char *const distributor_id_key = "Distributor ID:\t";
172 if (strstr(distribution_id, distributor_id_key))
173 {
174 // strip newlines
175 std::string id_string(distribution_id + strlen(distributor_id_key));
176 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), id_string.end());
177
178 // lower case it and convert whitespace to underscores
179 std::transform(id_string.begin(), id_string.end(), id_string.begin(), [](char ch)
180 {
181 return tolower(isspace(ch) ? '_' : ch);
182 });
183
Zachary Turner673b6e42014-08-21 17:57:03 +0000184 g_fields->m_distribution_id = id_string;
Zachary Turner97a14e62014-08-19 17:18:29 +0000185 if (log)
Zachary Turner673b6e42014-08-21 17:57:03 +0000186 log->Printf("distribution id set to \"%s\"", g_fields->m_distribution_id.c_str());
Zachary Turner97a14e62014-08-19 17:18:29 +0000187 }
188 else
189 {
190 if (log)
191 log->Printf("failed to find \"%s\" field in \"%s\"", distributor_id_key, distribution_id);
192 }
193 }
194 else
195 {
196 if (log)
197 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
198 " lines",
199 get_distribution_id_command.c_str());
200 }
201
202 // clean up the file
203 pclose(file);
204 }
205 }
206
Zachary Turner673b6e42014-08-21 17:57:03 +0000207 return g_fields->m_distribution_id.c_str();
Zachary Turner97a14e62014-08-19 17:18:29 +0000208}
Zachary Turner13b18262014-08-20 16:42:51 +0000209
Zachary Turnera21fee02014-08-21 21:49:24 +0000210FileSpec
211HostInfoLinux::GetProgramFileSpec()
212{
213 static FileSpec g_program_filespec;
214
215 if (!g_program_filespec)
216 {
217 char exe_path[PATH_MAX];
218 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
219 if (len > 0)
220 {
221 exe_path[len] = 0;
222 g_program_filespec.SetFile(exe_path, false);
223 }
224 }
225
226 return g_program_filespec;
227}
228
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000229bool
230HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec)
231{
Greg Clayton0d8400c2014-08-25 18:21:06 +0000232 FileSpec temp_file("/usr/lib/lldb", true);
233 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000234 return true;
235}
236
237bool
238HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec)
239{
240 // XDG Base Directory Specification
241 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
242 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000243 const char *xdg_data_home = getenv("XDG_DATA_HOME");
244 if (xdg_data_home && xdg_data_home[0])
245 {
246 std::string user_plugin_dir(xdg_data_home);
247 user_plugin_dir += "/lldb";
Greg Clayton0d8400c2014-08-25 18:21:06 +0000248 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000249 }
250 else
Greg Clayton0d8400c2014-08-25 18:21:06 +0000251 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000252 return true;
253}
254
Zachary Turner13b18262014-08-20 16:42:51 +0000255void
256HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
257{
258 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
259
260 const char *distribution_id = GetDistributionId().data();
261
262 // On Linux, "unknown" in the vendor slot isn't what we want for the default
263 // triple. It's probably an artifact of config.guess.
264 if (arch_32.IsValid())
265 {
266 arch_32.SetDistributionId(distribution_id);
267 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
268 arch_32.GetTriple().setVendorName("");
269 }
270 if (arch_64.IsValid())
271 {
272 arch_64.SetDistributionId(distribution_id);
273 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
274 arch_64.GetTriple().setVendorName("");
275 }
276}