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/Core/Log.h" 11 #include "lldb/Host/linux/HostInfoLinux.h" 12 13 #include <limits.h> 14 #include <stdio.h> 15 #include <string.h> 16 #include <sys/utsname.h> 17 18 #include <algorithm> 19 #include <mutex> // std::once 20 21 using namespace lldb_private; 22 23 namespace 24 { 25 struct 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 40 HostInfoLinuxFields *g_fields = nullptr; 41 } 42 43 void 44 HostInfoLinux::Initialize() 45 { 46 HostInfoPosix::Initialize(); 47 48 g_fields = new HostInfoLinuxFields(); 49 } 50 51 uint32_t 52 HostInfoLinux::GetMaxThreadNameLength() 53 { 54 return 16; 55 } 56 57 bool 58 HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update) 59 { 60 static bool success = false; 61 static std::once_flag g_once_flag; 62 std::call_once(g_once_flag, []() { 63 64 struct utsname un; 65 if (uname(&un) == 0) 66 { 67 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 } 79 } 80 }); 81 82 83 major = g_fields->m_os_major; 84 minor = g_fields->m_os_minor; 85 update = g_fields->m_os_update; 86 return success; 87 } 88 89 bool 90 HostInfoLinux::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 103 bool 104 HostInfoLinux::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 118 llvm::StringRef 119 HostInfoLinux::GetDistributionId() 120 { 121 // Try to run 'lbs_release -i', and use that response 122 // for the distribution id. 123 static std::once_flag g_once_flag; 124 std::call_once(g_once_flag, []() { 125 126 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST)); 127 if (log) 128 log->Printf("attempting to determine Linux distribution..."); 129 130 // check if the lsb_release command exists at one of the 131 // following paths 132 const char *const exe_paths[] = {"/bin/lsb_release", "/usr/bin/lsb_release"}; 133 134 for (size_t exe_index = 0; exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) 135 { 136 const char *const get_distribution_info_exe = exe_paths[exe_index]; 137 if (access(get_distribution_info_exe, F_OK)) 138 { 139 // this exe doesn't exist, move on to next exe 140 if (log) 141 log->Printf("executable doesn't exist: %s", get_distribution_info_exe); 142 continue; 143 } 144 145 // execute the distribution-retrieval command, read output 146 std::string get_distribution_id_command(get_distribution_info_exe); 147 get_distribution_id_command += " -i"; 148 149 FILE *file = popen(get_distribution_id_command.c_str(), "r"); 150 if (!file) 151 { 152 if (log) 153 log->Printf("failed to run command: \"%s\", cannot retrieve " 154 "platform information", 155 get_distribution_id_command.c_str()); 156 break; 157 } 158 159 // retrieve the distribution id string. 160 char distribution_id[256] = {'\0'}; 161 if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) 162 { 163 if (log) 164 log->Printf("distribution id command returned \"%s\"", distribution_id); 165 166 const char *const distributor_id_key = "Distributor ID:\t"; 167 if (strstr(distribution_id, distributor_id_key)) 168 { 169 // strip newlines 170 std::string id_string(distribution_id + strlen(distributor_id_key)); 171 id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), id_string.end()); 172 173 // lower case it and convert whitespace to underscores 174 std::transform(id_string.begin(), id_string.end(), id_string.begin(), [](char ch) 175 { 176 return tolower(isspace(ch) ? '_' : ch); 177 }); 178 179 g_fields->m_distribution_id = id_string; 180 if (log) 181 log->Printf("distribution id set to \"%s\"", g_fields->m_distribution_id.c_str()); 182 } 183 else 184 { 185 if (log) 186 log->Printf("failed to find \"%s\" field in \"%s\"", distributor_id_key, distribution_id); 187 } 188 } 189 else 190 { 191 if (log) 192 log->Printf("failed to retrieve distribution id, \"%s\" returned no" 193 " lines", 194 get_distribution_id_command.c_str()); 195 } 196 197 // clean up the file 198 pclose(file); 199 } 200 }); 201 202 return g_fields->m_distribution_id.c_str(); 203 } 204 205 FileSpec 206 HostInfoLinux::GetProgramFileSpec() 207 { 208 static FileSpec g_program_filespec; 209 210 if (!g_program_filespec) 211 { 212 char exe_path[PATH_MAX]; 213 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); 214 if (len > 0) 215 { 216 exe_path[len] = 0; 217 g_program_filespec.SetFile(exe_path, false); 218 } 219 } 220 221 return g_program_filespec; 222 } 223 224 bool 225 HostInfoLinux::ComputeSharedLibraryDirectory(FileSpec &file_spec) 226 { 227 if (HostInfoPosix::ComputeSharedLibraryDirectory(file_spec)) 228 return true; 229 file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory(); 230 return (bool)file_spec.GetDirectory(); 231 } 232 233 bool 234 HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) 235 { 236 FileSpec temp_file("/usr/lib/lldb", true); 237 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str()); 238 return true; 239 } 240 241 bool 242 HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) 243 { 244 // XDG Base Directory Specification 245 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html 246 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. 247 const char *xdg_data_home = getenv("XDG_DATA_HOME"); 248 if (xdg_data_home && xdg_data_home[0]) 249 { 250 std::string user_plugin_dir(xdg_data_home); 251 user_plugin_dir += "/lldb"; 252 file_spec.GetDirectory().SetCString(user_plugin_dir.c_str()); 253 } 254 else 255 file_spec.GetDirectory().SetCString("~/.local/share/lldb"); 256 return true; 257 } 258 259 void 260 HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64) 261 { 262 HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64); 263 264 const char *distribution_id = GetDistributionId().data(); 265 266 // On Linux, "unknown" in the vendor slot isn't what we want for the default 267 // triple. It's probably an artifact of config.guess. 268 if (arch_32.IsValid()) 269 { 270 arch_32.SetDistributionId(distribution_id); 271 if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 272 arch_32.GetTriple().setVendorName(""); 273 } 274 if (arch_64.IsValid()) 275 { 276 arch_64.SetDistributionId(distribution_id); 277 if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 278 arch_64.GetTriple().setVendorName(""); 279 } 280 } 281