1 //===-- 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/Host/Config.h" 11 #include "lldb/Host/linux/HostInfoLinux.h" 12 #include "lldb/Utility/Log.h" 13 14 #include "llvm/Support/Threading.h" 15 16 #include <limits.h> 17 #include <stdio.h> 18 #include <string.h> 19 #include <sys/utsname.h> 20 #include <unistd.h> 21 22 #include <algorithm> 23 #include <mutex> // std::once 24 25 using namespace lldb_private; 26 27 namespace { 28 struct HostInfoLinuxFields { 29 std::string m_distribution_id; 30 llvm::VersionTuple m_os_version; 31 }; 32 33 HostInfoLinuxFields *g_fields = nullptr; 34 } 35 36 void HostInfoLinux::Initialize() { 37 HostInfoPosix::Initialize(); 38 39 g_fields = new HostInfoLinuxFields(); 40 } 41 42 llvm::VersionTuple HostInfoLinux::GetOSVersion() { 43 static llvm::once_flag g_once_flag; 44 llvm::call_once(g_once_flag, []() { 45 struct utsname un; 46 if (uname(&un) != 0) 47 return; 48 49 llvm::StringRef release = un.release; 50 // The kernel release string can include a lot of stuff (e.g. 51 // 4.9.0-6-amd64). We're only interested in the numbered prefix. 52 release = release.substr(0, release.find_first_not_of("0123456789.")); 53 g_fields->m_os_version.tryParse(release); 54 }); 55 56 return g_fields->m_os_version; 57 } 58 59 bool HostInfoLinux::GetOSBuildString(std::string &s) { 60 struct utsname un; 61 ::memset(&un, 0, sizeof(utsname)); 62 s.clear(); 63 64 if (uname(&un) < 0) 65 return false; 66 67 s.assign(un.release); 68 return true; 69 } 70 71 bool HostInfoLinux::GetOSKernelDescription(std::string &s) { 72 struct utsname un; 73 74 ::memset(&un, 0, sizeof(utsname)); 75 s.clear(); 76 77 if (uname(&un) < 0) 78 return false; 79 80 s.assign(un.version); 81 return true; 82 } 83 84 llvm::StringRef HostInfoLinux::GetDistributionId() { 85 // Try to run 'lbs_release -i', and use that response for the distribution 86 // id. 87 static llvm::once_flag g_once_flag; 88 llvm::call_once(g_once_flag, []() { 89 90 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST)); 91 if (log) 92 log->Printf("attempting to determine Linux distribution..."); 93 94 // check if the lsb_release command exists at one of the following paths 95 const char *const exe_paths[] = {"/bin/lsb_release", 96 "/usr/bin/lsb_release"}; 97 98 for (size_t exe_index = 0; 99 exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) { 100 const char *const get_distribution_info_exe = exe_paths[exe_index]; 101 if (access(get_distribution_info_exe, F_OK)) { 102 // this exe doesn't exist, move on to next exe 103 if (log) 104 log->Printf("executable doesn't exist: %s", 105 get_distribution_info_exe); 106 continue; 107 } 108 109 // execute the distribution-retrieval command, read output 110 std::string get_distribution_id_command(get_distribution_info_exe); 111 get_distribution_id_command += " -i"; 112 113 FILE *file = popen(get_distribution_id_command.c_str(), "r"); 114 if (!file) { 115 if (log) 116 log->Printf("failed to run command: \"%s\", cannot retrieve " 117 "platform information", 118 get_distribution_id_command.c_str()); 119 break; 120 } 121 122 // retrieve the distribution id string. 123 char distribution_id[256] = {'\0'}; 124 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) { 125 if (log) 126 log->Printf("distribution id command returned \"%s\"", 127 distribution_id); 128 129 const char *const distributor_id_key = "Distributor ID:\t"; 130 if (strstr(distribution_id, distributor_id_key)) { 131 // strip newlines 132 std::string id_string(distribution_id + strlen(distributor_id_key)); 133 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), 134 id_string.end()); 135 136 // lower case it and convert whitespace to underscores 137 std::transform( 138 id_string.begin(), id_string.end(), id_string.begin(), 139 [](char ch) { return tolower(isspace(ch) ? '_' : ch); }); 140 141 g_fields->m_distribution_id = id_string; 142 if (log) 143 log->Printf("distribution id set to \"%s\"", 144 g_fields->m_distribution_id.c_str()); 145 } else { 146 if (log) 147 log->Printf("failed to find \"%s\" field in \"%s\"", 148 distributor_id_key, distribution_id); 149 } 150 } else { 151 if (log) 152 log->Printf("failed to retrieve distribution id, \"%s\" returned no" 153 " lines", 154 get_distribution_id_command.c_str()); 155 } 156 157 // clean up the file 158 pclose(file); 159 } 160 }); 161 162 return g_fields->m_distribution_id; 163 } 164 165 FileSpec HostInfoLinux::GetProgramFileSpec() { 166 static FileSpec g_program_filespec; 167 168 if (!g_program_filespec) { 169 char exe_path[PATH_MAX]; 170 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); 171 if (len > 0) { 172 exe_path[len] = 0; 173 g_program_filespec.SetFile(exe_path, false, FileSpec::Style::native); 174 } 175 } 176 177 return g_program_filespec; 178 } 179 180 bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) { 181 if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) && 182 file_spec.IsAbsolute() && file_spec.Exists()) 183 return true; 184 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory(); 185 return !file_spec.GetDirectory().IsEmpty(); 186 } 187 188 bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) { 189 FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins", true); 190 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str()); 191 return true; 192 } 193 194 bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) { 195 // XDG Base Directory Specification 196 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If 197 // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. 198 const char *xdg_data_home = getenv("XDG_DATA_HOME"); 199 if (xdg_data_home && xdg_data_home[0]) { 200 std::string user_plugin_dir(xdg_data_home); 201 user_plugin_dir += "/lldb"; 202 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str()); 203 } else 204 file_spec.GetDirectory().SetCString("~/.local/share/lldb"); 205 return true; 206 } 207 208 void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, 209 ArchSpec &arch_64) { 210 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64); 211 212 const char *distribution_id = GetDistributionId().data(); 213 214 // On Linux, "unknown" in the vendor slot isn't what we want for the default 215 // triple. It's probably an artifact of config.guess. 216 if (arch_32.IsValid()) { 217 arch_32.SetDistributionId(distribution_id); 218 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 219 arch_32.GetTriple().setVendorName(llvm::StringRef()); 220 } 221 if (arch_64.IsValid()) { 222 arch_64.SetDistributionId(distribution_id); 223 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 224 arch_64.GetTriple().setVendorName(llvm::StringRef()); 225 } 226 } 227