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