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