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" |
Jonas Devlieghere | 60cf3f8 | 2018-11-01 17:35:31 +0000 | [diff] [blame^] | 11 | #include "lldb/Host/Config.h" |
| 12 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 13 | #include "lldb/Utility/Log.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 14 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Threading.h" |
| 16 | |
Zachary Turner | e47ffc3 | 2014-08-21 21:02:47 +0000 | [diff] [blame] | 17 | #include <limits.h> |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/utsname.h> |
Pavel Labath | b6dbe9a | 2017-07-18 13:14:01 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
Greg Clayton | ff48e4b | 2015-02-03 02:05:44 +0000 | [diff] [blame] | 24 | #include <mutex> // std::once |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb_private; |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | struct HostInfoLinuxFields { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | std::string m_distribution_id; |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 31 | llvm::VersionTuple m_os_version; |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | HostInfoLinuxFields *g_fields = nullptr; |
| 35 | } |
| 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | void HostInfoLinux::Initialize() { |
| 38 | HostInfoPosix::Initialize(); |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | g_fields = new HostInfoLinuxFields(); |
Zachary Turner | 673b6e4 | 2014-08-21 17:57:03 +0000 | [diff] [blame] | 41 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 42 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 43 | llvm::VersionTuple HostInfoLinux::GetOSVersion() { |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 44 | static llvm::once_flag g_once_flag; |
| 45 | llvm::call_once(g_once_flag, []() { |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 46 | struct utsname un; |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 47 | if (uname(&un) != 0) |
| 48 | return; |
| 49 | |
| 50 | llvm::StringRef release = un.release; |
| 51 | // The kernel release string can include a lot of stuff (e.g. |
| 52 | // 4.9.0-6-amd64). We're only interested in the numbered prefix. |
| 53 | release = release.substr(0, release.find_first_not_of("0123456789.")); |
| 54 | g_fields->m_os_version.tryParse(release); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | }); |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 56 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 57 | return g_fields->m_os_version; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | bool HostInfoLinux::GetOSBuildString(std::string &s) { |
| 61 | struct utsname un; |
| 62 | ::memset(&un, 0, sizeof(utsname)); |
| 63 | s.clear(); |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | if (uname(&un) < 0) |
| 66 | return false; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | s.assign(un.release); |
| 69 | return true; |
Oleksiy Vyalov | 53c038a | 2014-12-09 02:13:05 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | bool HostInfoLinux::GetOSKernelDescription(std::string &s) { |
| 73 | struct utsname un; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | ::memset(&un, 0, sizeof(utsname)); |
| 76 | s.clear(); |
| 77 | |
| 78 | if (uname(&un) < 0) |
| 79 | return false; |
| 80 | |
| 81 | s.assign(un.version); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | llvm::StringRef HostInfoLinux::GetDistributionId() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 86 | // Try to run 'lbs_release -i', and use that response for the distribution |
| 87 | // id. |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 88 | static llvm::once_flag g_once_flag; |
| 89 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | |
| 91 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST)); |
| 92 | if (log) |
| 93 | log->Printf("attempting to determine Linux distribution..."); |
| 94 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 95 | // 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] | 96 | const char *const exe_paths[] = {"/bin/lsb_release", |
| 97 | "/usr/bin/lsb_release"}; |
| 98 | |
| 99 | for (size_t exe_index = 0; |
| 100 | exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) { |
| 101 | const char *const get_distribution_info_exe = exe_paths[exe_index]; |
| 102 | if (access(get_distribution_info_exe, F_OK)) { |
| 103 | // this exe doesn't exist, move on to next exe |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 104 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | log->Printf("executable doesn't exist: %s", |
| 106 | get_distribution_info_exe); |
| 107 | continue; |
| 108 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | // execute the distribution-retrieval command, read output |
| 111 | std::string get_distribution_id_command(get_distribution_info_exe); |
| 112 | get_distribution_id_command += " -i"; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | FILE *file = popen(get_distribution_id_command.c_str(), "r"); |
| 115 | if (!file) { |
| 116 | if (log) |
| 117 | log->Printf("failed to run command: \"%s\", cannot retrieve " |
| 118 | "platform information", |
| 119 | get_distribution_id_command.c_str()); |
| 120 | break; |
| 121 | } |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | // retrieve the distribution id string. |
| 124 | char distribution_id[256] = {'\0'}; |
| 125 | if (fgets(distribution_id, sizeof(distribution_id) - 1, file) != NULL) { |
| 126 | if (log) |
| 127 | log->Printf("distribution id command returned \"%s\"", |
| 128 | distribution_id); |
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 | const char *const distributor_id_key = "Distributor ID:\t"; |
| 131 | if (strstr(distribution_id, distributor_id_key)) { |
| 132 | // strip newlines |
| 133 | std::string id_string(distribution_id + strlen(distributor_id_key)); |
| 134 | id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'), |
| 135 | id_string.end()); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | // lower case it and convert whitespace to underscores |
| 138 | std::transform( |
| 139 | id_string.begin(), id_string.end(), id_string.begin(), |
| 140 | [](char ch) { return tolower(isspace(ch) ? '_' : ch); }); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | g_fields->m_distribution_id = id_string; |
| 143 | if (log) |
| 144 | log->Printf("distribution id set to \"%s\"", |
| 145 | g_fields->m_distribution_id.c_str()); |
| 146 | } else { |
| 147 | if (log) |
| 148 | log->Printf("failed to find \"%s\" field in \"%s\"", |
| 149 | distributor_id_key, distribution_id); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 150 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | } else { |
| 152 | if (log) |
| 153 | log->Printf("failed to retrieve distribution id, \"%s\" returned no" |
| 154 | " lines", |
| 155 | get_distribution_id_command.c_str()); |
| 156 | } |
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 | // clean up the file |
| 159 | pclose(file); |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 160 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | }); |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 162 | |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 163 | return g_fields->m_distribution_id; |
Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | FileSpec HostInfoLinux::GetProgramFileSpec() { |
| 167 | static FileSpec g_program_filespec; |
| 168 | |
| 169 | if (!g_program_filespec) { |
| 170 | char exe_path[PATH_MAX]; |
| 171 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); |
| 172 | if (len > 0) { |
| 173 | exe_path[len] = 0; |
Jonas Devlieghere | dd2f78e | 2018-06-13 22:23:48 +0000 | [diff] [blame] | 174 | g_program_filespec.SetFile(exe_path, false, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | return g_program_filespec; |
Chaoren Lin | c30c49c | 2015-02-10 18:30:34 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) { |
| 182 | if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) && |
Jonas Devlieghere | 60cf3f8 | 2018-11-01 17:35:31 +0000 | [diff] [blame^] | 183 | file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec)) |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 184 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory(); |
| 186 | return !file_spec.GetDirectory().IsEmpty(); |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) { |
Michal Gorny | c061175 | 2018-01-29 18:25:06 +0000 | [diff] [blame] | 190 | FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins", true); |
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 | } |