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