Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 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 | |
Michal Gorny | c061175 | 2018-01-29 18:25:06 +0000 | [diff] [blame] | 10 | #include "lldb/Host/Config.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 11 | #include "lldb/Host/linux/HostInfoLinux.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/Log.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 13 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Threading.h" |
| 15 | |
Zachary Turner | e47ffc3 | 2014-08-21 21:02:47 +0000 | [diff] [blame] | 16 | #include <limits.h> |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/utsname.h> |
Pavel Labath | b6dbe9a | 2017-07-18 13:14:01 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Greg Clayton | ff48e4b | 2015-02-03 02:05:44 +0000 | [diff] [blame] | 23 | #include <mutex> // std::once |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lldb_private; |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | struct HostInfoLinuxFields { |
| 29 | HostInfoLinuxFields() : m_os_major(0), m_os_minor(0), m_os_update(0) {} |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | std::string m_distribution_id; |
| 32 | uint32_t m_os_major; |
| 33 | uint32_t m_os_minor; |
| 34 | uint32_t m_os_update; |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | HostInfoLinuxFields *g_fields = nullptr; |
| 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | void HostInfoLinux::Initialize() { |
| 41 | HostInfoPosix::Initialize(); |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | g_fields = new HostInfoLinuxFields(); |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 44 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | bool HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor, |
| 47 | uint32_t &update) { |
| 48 | static bool success = false; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 49 | static llvm::once_flag g_once_flag; |
| 50 | llvm::call_once(g_once_flag, []() { |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 51 | |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 52 | struct utsname un; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | if (uname(&un) == 0) { |
| 54 | int status = sscanf(un.release, "%u.%u.%u", &g_fields->m_os_major, |
| 55 | &g_fields->m_os_minor, &g_fields->m_os_update); |
| 56 | if (status == 3) |
| 57 | success = true; |
| 58 | else { |
| 59 | // Some kernels omit the update version, so try looking for just "X.Y" |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 60 | // and set update to 0. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | g_fields->m_os_update = 0; |
| 62 | status = sscanf(un.release, "%u.%u", &g_fields->m_os_major, |
| 63 | &g_fields->m_os_minor); |
| 64 | if (status == 2) |
| 65 | success = true; |
| 66 | } |
| 67 | } |
| 68 | }); |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | major = g_fields->m_os_major; |
| 71 | minor = g_fields->m_os_minor; |
| 72 | update = g_fields->m_os_update; |
| 73 | return success; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | bool HostInfoLinux::GetOSBuildString(std::string &s) { |
| 77 | struct utsname un; |
| 78 | ::memset(&un, 0, sizeof(utsname)); |
| 79 | s.clear(); |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | if (uname(&un) < 0) |
| 82 | return false; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | s.assign(un.release); |
| 85 | return true; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | bool HostInfoLinux::GetOSKernelDescription(std::string &s) { |
| 89 | struct utsname un; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | ::memset(&un, 0, sizeof(utsname)); |
| 92 | s.clear(); |
| 93 | |
| 94 | if (uname(&un) < 0) |
| 95 | return false; |
| 96 | |
| 97 | s.assign(un.version); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | llvm::StringRef HostInfoLinux::GetDistributionId() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 102 | // Try to run 'lbs_release -i', and use that response for the distribution |
| 103 | // id. |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 104 | static llvm::once_flag g_once_flag; |
| 105 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | |
| 107 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST)); |
| 108 | if (log) |
| 109 | log->Printf("attempting to determine Linux distribution..."); |
| 110 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 111 | // check if the lsb_release command exists at one of the following paths |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | const char *const exe_paths[] = {"/bin/lsb_release", |
| 113 | "/usr/bin/lsb_release"}; |
| 114 | |
| 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 |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 120 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | log->Printf("executable doesn't exist: %s", |
| 122 | get_distribution_info_exe); |
| 123 | continue; |
| 124 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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"; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | FILE *file = popen(get_distribution_id_command.c_str(), "r"); |
| 131 | if (!file) { |
| 132 | if (log) |
| 133 | log->Printf("failed to run command: \"%s\", cannot retrieve " |
| 134 | "platform information", |
| 135 | get_distribution_id_command.c_str()); |
| 136 | break; |
| 137 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | // retrieve the distribution id string. |
| 140 | char distribution_id[256] = {'\0'}; |
| 141 | if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) { |
| 142 | if (log) |
| 143 | log->Printf("distribution id command returned \"%s\"", |
| 144 | distribution_id); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | const char *const distributor_id_key = "Distributor ID:\t"; |
| 147 | if (strstr(distribution_id, distributor_id_key)) { |
| 148 | // strip newlines |
| 149 | std::string id_string(distribution_id + strlen(distributor_id_key)); |
| 150 | id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), |
| 151 | id_string.end()); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | // lower case it and convert whitespace to underscores |
| 154 | std::transform( |
| 155 | id_string.begin(), id_string.end(), id_string.begin(), |
| 156 | [](char ch) { return tolower(isspace(ch) ? '_' : ch); }); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | g_fields->m_distribution_id = id_string; |
| 159 | if (log) |
| 160 | log->Printf("distribution id set to \"%s\"", |
| 161 | g_fields->m_distribution_id.c_str()); |
| 162 | } else { |
| 163 | if (log) |
| 164 | log->Printf("failed to find \"%s\" field in \"%s\"", |
| 165 | distributor_id_key, distribution_id); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 166 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | } else { |
| 168 | if (log) |
| 169 | log->Printf("failed to retrieve distribution id, \"%s\" returned no" |
| 170 | " lines", |
| 171 | get_distribution_id_command.c_str()); |
| 172 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | // clean up the file |
| 175 | pclose(file); |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 176 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | }); |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 178 | |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 179 | return g_fields->m_distribution_id; |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | FileSpec HostInfoLinux::GetProgramFileSpec() { |
| 183 | static FileSpec g_program_filespec; |
| 184 | |
| 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); |
| 188 | if (len > 0) { |
| 189 | exe_path[len] = 0; |
Jonas Devlieghere | dd2f78e | 2018-06-13 22:23:48 +0000 | [diff] [blame^] | 190 | g_program_filespec.SetFile(exe_path, false, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | return g_program_filespec; |
Chaoren Lin | c30c49c | 2015-02-10 18:30:34 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) { |
| 198 | if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) && |
| 199 | file_spec.IsAbsolute() && file_spec.Exists()) |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 200 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory(); |
| 202 | return !file_spec.GetDirectory().IsEmpty(); |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) { |
Michal Gorny | c061175 | 2018-01-29 18:25:06 +0000 | [diff] [blame] | 206 | FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins", true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str()); |
| 208 | return true; |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) { |
| 212 | // XDG Base Directory Specification |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 213 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If |
| 214 | // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
| 220 | } else |
| 221 | file_spec.GetDirectory().SetCString("~/.local/share/lldb"); |
| 222 | return true; |
| 223 | } |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32, |
| 226 | ArchSpec &arch_64) { |
| 227 | HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64); |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | const char *distribution_id = GetDistributionId().data(); |
| 230 | |
| 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()); |
| 237 | } |
| 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()); |
| 242 | } |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 243 | } |