180814287SRaphael Isemann //===-- HostInfoLinux.cpp -------------------------------------------------===//
297a14e60SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
697a14e60SZachary Turner //
797a14e60SZachary Turner //===----------------------------------------------------------------------===//
897a14e60SZachary Turner
997a14e60SZachary Turner #include "lldb/Host/linux/HostInfoLinux.h"
1060cf3f82SJonas Devlieghere #include "lldb/Host/Config.h"
1160cf3f82SJonas Devlieghere #include "lldb/Host/FileSystem.h"
12c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
136f9e6901SZachary Turner #include "lldb/Utility/Log.h"
1497a14e60SZachary Turner
15c5f28e2aSKamil Rytarowski #include "llvm/Support/Threading.h"
16c5f28e2aSKamil Rytarowski
1776e47d48SRaphael Isemann #include <climits>
1876e47d48SRaphael Isemann #include <cstdio>
1976e47d48SRaphael Isemann #include <cstring>
2097a14e60SZachary Turner #include <sys/utsname.h>
21b6dbe9a9SPavel Labath #include <unistd.h>
2297a14e60SZachary Turner
2397a14e60SZachary Turner #include <algorithm>
24672d2c12SJonas Devlieghere #include <mutex>
2597a14e60SZachary Turner
2697a14e60SZachary Turner using namespace lldb_private;
2797a14e60SZachary Turner
28b9c1b51eSKate Stone namespace {
29b9c1b51eSKate Stone struct HostInfoLinuxFields {
30632cbcacSRaphael Isemann llvm::once_flag m_distribution_once_flag;
31673b6e4fSZachary Turner std::string m_distribution_id;
32632cbcacSRaphael Isemann llvm::once_flag m_os_version_once_flag;
332272c481SPavel Labath llvm::VersionTuple m_os_version;
34673b6e4fSZachary Turner };
3593c1b3caSPavel Labath } // namespace
36673b6e4fSZachary Turner
3793c1b3caSPavel Labath static HostInfoLinuxFields *g_fields = nullptr;
38673b6e4fSZachary Turner
Initialize(SharedLibraryDirectoryHelper * helper)39004a264fSPavel Labath void HostInfoLinux::Initialize(SharedLibraryDirectoryHelper *helper) {
40004a264fSPavel Labath HostInfoPosix::Initialize(helper);
41673b6e4fSZachary Turner
42673b6e4fSZachary Turner g_fields = new HostInfoLinuxFields();
43673b6e4fSZachary Turner }
4497a14e60SZachary Turner
Terminate()45f3f90456SVitaly Buka void HostInfoLinux::Terminate() {
46f3f90456SVitaly Buka assert(g_fields && "Missing call to Initialize?");
47f3f90456SVitaly Buka delete g_fields;
48f3f90456SVitaly Buka g_fields = nullptr;
49f3f90456SVitaly Buka HostInfoBase::Terminate();
50f3f90456SVitaly Buka }
51f3f90456SVitaly Buka
GetOSVersion()522272c481SPavel Labath llvm::VersionTuple HostInfoLinux::GetOSVersion() {
53632cbcacSRaphael Isemann assert(g_fields && "Missing call to Initialize?");
54632cbcacSRaphael Isemann llvm::call_once(g_fields->m_os_version_once_flag, []() {
5597a14e60SZachary Turner struct utsname un;
562272c481SPavel Labath if (uname(&un) != 0)
572272c481SPavel Labath return;
582272c481SPavel Labath
592272c481SPavel Labath llvm::StringRef release = un.release;
602272c481SPavel Labath // The kernel release string can include a lot of stuff (e.g.
612272c481SPavel Labath // 4.9.0-6-amd64). We're only interested in the numbered prefix.
622272c481SPavel Labath release = release.substr(0, release.find_first_not_of("0123456789."));
632272c481SPavel Labath g_fields->m_os_version.tryParse(release);
64ff48e4beSGreg Clayton });
6597a14e60SZachary Turner
662272c481SPavel Labath return g_fields->m_os_version;
6797a14e60SZachary Turner }
6897a14e60SZachary Turner
GetOSBuildString()698b8070e2SPavel Labath llvm::Optional<std::string> HostInfoLinux::GetOSBuildString() {
7053c038a5SOleksiy Vyalov struct utsname un;
7153c038a5SOleksiy Vyalov ::memset(&un, 0, sizeof(utsname));
7253c038a5SOleksiy Vyalov
7353c038a5SOleksiy Vyalov if (uname(&un) < 0)
748b8070e2SPavel Labath return llvm::None;
7553c038a5SOleksiy Vyalov
768b8070e2SPavel Labath return std::string(un.release);
7753c038a5SOleksiy Vyalov }
7853c038a5SOleksiy Vyalov
GetDistributionId()79b9c1b51eSKate Stone llvm::StringRef HostInfoLinux::GetDistributionId() {
80632cbcacSRaphael Isemann assert(g_fields && "Missing call to Initialize?");
8105097246SAdrian Prantl // Try to run 'lbs_release -i', and use that response for the distribution
8205097246SAdrian Prantl // id.
83632cbcacSRaphael Isemann llvm::call_once(g_fields->m_distribution_once_flag, []() {
84a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Host);
8563e5fb76SJonas Devlieghere LLDB_LOGF(log, "attempting to determine Linux distribution...");
8697a14e60SZachary Turner
8705097246SAdrian Prantl // check if the lsb_release command exists at one of the following paths
88b9c1b51eSKate Stone const char *const exe_paths[] = {"/bin/lsb_release",
89b9c1b51eSKate Stone "/usr/bin/lsb_release"};
9097a14e60SZachary Turner
91b9c1b51eSKate Stone for (size_t exe_index = 0;
92b9c1b51eSKate Stone exe_index < sizeof(exe_paths) / sizeof(exe_paths[0]); ++exe_index) {
9397a14e60SZachary Turner const char *const get_distribution_info_exe = exe_paths[exe_index];
94b9c1b51eSKate Stone if (access(get_distribution_info_exe, F_OK)) {
9597a14e60SZachary Turner // this exe doesn't exist, move on to next exe
9663e5fb76SJonas Devlieghere LLDB_LOGF(log, "executable doesn't exist: %s",
97b9c1b51eSKate Stone get_distribution_info_exe);
9897a14e60SZachary Turner continue;
9997a14e60SZachary Turner }
10097a14e60SZachary Turner
10197a14e60SZachary Turner // execute the distribution-retrieval command, read output
10297a14e60SZachary Turner std::string get_distribution_id_command(get_distribution_info_exe);
10397a14e60SZachary Turner get_distribution_id_command += " -i";
10497a14e60SZachary Turner
10597a14e60SZachary Turner FILE *file = popen(get_distribution_id_command.c_str(), "r");
106b9c1b51eSKate Stone if (!file) {
10763e5fb76SJonas Devlieghere LLDB_LOGF(log,
10863e5fb76SJonas Devlieghere "failed to run command: \"%s\", cannot retrieve "
10997a14e60SZachary Turner "platform information",
11097a14e60SZachary Turner get_distribution_id_command.c_str());
11197a14e60SZachary Turner break;
11297a14e60SZachary Turner }
11397a14e60SZachary Turner
11497a14e60SZachary Turner // retrieve the distribution id string.
11597a14e60SZachary Turner char distribution_id[256] = {'\0'};
116248a1305SKonrad Kleine if (fgets(distribution_id, sizeof(distribution_id) - 1, file) !=
117248a1305SKonrad Kleine nullptr) {
11863e5fb76SJonas Devlieghere LLDB_LOGF(log, "distribution id command returned \"%s\"",
119b9c1b51eSKate Stone distribution_id);
12097a14e60SZachary Turner
12197a14e60SZachary Turner const char *const distributor_id_key = "Distributor ID:\t";
122b9c1b51eSKate Stone if (strstr(distribution_id, distributor_id_key)) {
12397a14e60SZachary Turner // strip newlines
12497a14e60SZachary Turner std::string id_string(distribution_id + strlen(distributor_id_key));
125b9c1b51eSKate Stone id_string.erase(std::remove(id_string.begin(), id_string.end(), '\n'),
126b9c1b51eSKate Stone id_string.end());
12797a14e60SZachary Turner
12897a14e60SZachary Turner // lower case it and convert whitespace to underscores
129b9c1b51eSKate Stone std::transform(
130b9c1b51eSKate Stone id_string.begin(), id_string.end(), id_string.begin(),
131b9c1b51eSKate Stone [](char ch) { return tolower(isspace(ch) ? '_' : ch); });
13297a14e60SZachary Turner
133673b6e4fSZachary Turner g_fields->m_distribution_id = id_string;
13463e5fb76SJonas Devlieghere LLDB_LOGF(log, "distribution id set to \"%s\"",
135b9c1b51eSKate Stone g_fields->m_distribution_id.c_str());
136b9c1b51eSKate Stone } else {
13763e5fb76SJonas Devlieghere LLDB_LOGF(log, "failed to find \"%s\" field in \"%s\"",
138b9c1b51eSKate Stone distributor_id_key, distribution_id);
13997a14e60SZachary Turner }
140b9c1b51eSKate Stone } else {
14163e5fb76SJonas Devlieghere LLDB_LOGF(log,
14263e5fb76SJonas Devlieghere "failed to retrieve distribution id, \"%s\" returned no"
14397a14e60SZachary Turner " lines",
14497a14e60SZachary Turner get_distribution_id_command.c_str());
14597a14e60SZachary Turner }
14697a14e60SZachary Turner
14797a14e60SZachary Turner // clean up the file
14897a14e60SZachary Turner pclose(file);
14997a14e60SZachary Turner }
150ff48e4beSGreg Clayton });
15197a14e60SZachary Turner
152771ef6d4SMalcolm Parsons return g_fields->m_distribution_id;
15397a14e60SZachary Turner }
15413b18261SZachary Turner
GetProgramFileSpec()155b9c1b51eSKate Stone FileSpec HostInfoLinux::GetProgramFileSpec() {
156a21fee0eSZachary Turner static FileSpec g_program_filespec;
157a21fee0eSZachary Turner
158b9c1b51eSKate Stone if (!g_program_filespec) {
159a21fee0eSZachary Turner char exe_path[PATH_MAX];
160a21fee0eSZachary Turner ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
161b9c1b51eSKate Stone if (len > 0) {
162a21fee0eSZachary Turner exe_path[len] = 0;
1638f3be7a3SJonas Devlieghere g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
164a21fee0eSZachary Turner }
165a21fee0eSZachary Turner }
166a21fee0eSZachary Turner
167a21fee0eSZachary Turner return g_program_filespec;
168a21fee0eSZachary Turner }
169a21fee0eSZachary Turner
ComputeSupportExeDirectory(FileSpec & file_spec)170b9c1b51eSKate Stone bool HostInfoLinux::ComputeSupportExeDirectory(FileSpec &file_spec) {
171f9e915aeSChaoren Lin if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
17260cf3f82SJonas Devlieghere file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
17362fa2732SVince Harron return true;
174*1b4b12a3SNico Weber file_spec.GetDirectory() = GetProgramFileSpec().GetDirectory();
175f9e915aeSChaoren Lin return !file_spec.GetDirectory().IsEmpty();
176c30c49c4SChaoren Lin }
177c30c49c4SChaoren Lin
ComputeSystemPluginsDirectory(FileSpec & file_spec)178b9c1b51eSKate Stone bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
1798f3be7a3SJonas Devlieghere FileSpec temp_file("/usr/lib" LLDB_LIBDIR_SUFFIX "/lldb/plugins");
1808f3be7a3SJonas Devlieghere FileSystem::Instance().Resolve(temp_file);
181*1b4b12a3SNico Weber file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
18242ff0ad8SZachary Turner return true;
18342ff0ad8SZachary Turner }
18442ff0ad8SZachary Turner
ComputeUserPluginsDirectory(FileSpec & file_spec)185b9c1b51eSKate Stone bool HostInfoLinux::ComputeUserPluginsDirectory(FileSpec &file_spec) {
18642ff0ad8SZachary Turner // XDG Base Directory Specification
18705097246SAdrian Prantl // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html If
18805097246SAdrian Prantl // XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
18942ff0ad8SZachary Turner const char *xdg_data_home = getenv("XDG_DATA_HOME");
190b9c1b51eSKate Stone if (xdg_data_home && xdg_data_home[0]) {
19142ff0ad8SZachary Turner std::string user_plugin_dir(xdg_data_home);
19242ff0ad8SZachary Turner user_plugin_dir += "/lldb";
193*1b4b12a3SNico Weber file_spec.GetDirectory().SetCString(user_plugin_dir.c_str());
194b9c1b51eSKate Stone } else
195*1b4b12a3SNico Weber file_spec.GetDirectory().SetCString("~/.local/share/lldb");
19642ff0ad8SZachary Turner return true;
19742ff0ad8SZachary Turner }
19842ff0ad8SZachary Turner
ComputeHostArchitectureSupport(ArchSpec & arch_32,ArchSpec & arch_64)199b9c1b51eSKate Stone void HostInfoLinux::ComputeHostArchitectureSupport(ArchSpec &arch_32,
200b9c1b51eSKate Stone ArchSpec &arch_64) {
20113b18261SZachary Turner HostInfoPosix::ComputeHostArchitectureSupport(arch_32, arch_64);
20213b18261SZachary Turner
20313b18261SZachary Turner const char *distribution_id = GetDistributionId().data();
20413b18261SZachary Turner
20513b18261SZachary Turner // On Linux, "unknown" in the vendor slot isn't what we want for the default
20613b18261SZachary Turner // triple. It's probably an artifact of config.guess.
207b9c1b51eSKate Stone if (arch_32.IsValid()) {
20813b18261SZachary Turner arch_32.SetDistributionId(distribution_id);
20913b18261SZachary Turner if (arch_32.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
2107df337f8STodd Fiala arch_32.GetTriple().setVendorName(llvm::StringRef());
21113b18261SZachary Turner }
212b9c1b51eSKate Stone if (arch_64.IsValid()) {
21313b18261SZachary Turner arch_64.SetDistributionId(distribution_id);
21413b18261SZachary Turner if (arch_64.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
2157df337f8STodd Fiala arch_64.GetTriple().setVendorName(llvm::StringRef());
21613b18261SZachary Turner }
21713b18261SZachary Turner }
218