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