1 //===-- HostInfoLinux.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/linux/HostInfoLinux.h"
11 #include "lldb/Core/Log.h"
16 #include <sys/utsname.h>
19 #include <mutex> // std::once
21 using namespace lldb_private;
24 struct HostInfoLinuxFields {
25 HostInfoLinuxFields() : m_os_major(0), m_os_minor(0), m_os_update(0) {}
27 std::string m_distribution_id;
33 HostInfoLinuxFields *g_fields = nullptr;
36 void HostInfoLinux::Initialize() {
37 HostInfoPosix::Initialize();
39 g_fields = new HostInfoLinuxFields();
42 uint32_t HostInfoLinux::GetMaxThreadNameLength() { return 16; }
44 bool HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor,
46 static bool success = false;
47 static std::once_flag g_once_flag;
48 std::call_once(g_once_flag, []() {
51 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);
57 // Some kernels omit the update version, so try looking for just "X.Y"
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);
69 major = g_fields->m_os_major;
70 minor = g_fields->m_os_minor;
71 update = g_fields->m_os_update;
75 bool HostInfoLinux::GetOSBuildString(std::string &s) {
77 ::memset(&un, 0, sizeof(utsname));
87 bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
90 ::memset(&un, 0, sizeof(utsname));
100 llvm::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, []() {
106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
108 log->Printf("attempting to determine Linux distribution...");
110 // check if the lsb_release command exists at one of the
112 const char *const exe_paths[] = {"/bin/lsb_release",
113 "/usr/bin/lsb_release"};
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
121 log->Printf("executable doesn't exist: %s",
122 get_distribution_info_exe);
126 // execute the distribution-retrieval command, read output
127 std::string get_distribution_id_command(get_distribution_info_exe);
128 get_distribution_id_command += " -i";
130 FILE *file = popen(get_distribution_id_command.c_str(), "r");
133 log->Printf("failed to run command: \"%s\", cannot retrieve "
134 "platform information",
135 get_distribution_id_command.c_str());
139 // retrieve the distribution id string.
140 char distribution_id[256] = {'\0'};
141 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) {
143 log->Printf("distribution id command returned \"%s\"",
146 const char *const distributor_id_key = "Distributor ID:\t";
147 if (strstr(distribution_id, distributor_id_key)) {
149 std::string id_string(distribution_id + strlen(distributor_id_key));
150 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
153 // lower case it and convert whitespace to underscores
155 id_string.begin(), id_string.end(), id_string.begin(),
156 [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
158 g_fields->m_distribution_id = id_string;
160 log->Printf("distribution id set to \"%s\"",
161 g_fields->m_distribution_id.c_str());
164 log->Printf("failed to find \"%s\" field in \"%s\"",
165 distributor_id_key, distribution_id);
169 log->Printf("failed to retrieve distribution id, \"%s\" returned no"
171 get_distribution_id_command.c_str());
179 return g_fields->m_distribution_id;
182 FileSpec HostInfoLinux::GetProgramFileSpec() {
183 static FileSpec g_program_filespec;
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);
190 g_program_filespec.SetFile(exe_path, false);
194 return g_program_filespec;
197 bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) {
198 if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
199 file_spec.IsAbsolute() && file_spec.Exists())
201 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
202 return !file_spec.GetDirectory().IsEmpty();
205 bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
206 FileSpec temp_file("/usr/lib/lldb/plugins", true);
207 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
211 bool 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());
221 file_spec.GetDirectory().SetCString("~/.local/share/lldb");
225 void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
227 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
229 const char *distribution_id = GetDistributionId().data();
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());
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());