197a14e60SZachary Turner //===-- HostInfoBase.cpp ----------------------------------------*- C++ -*-===//
297a14e60SZachary Turner //
397a14e60SZachary Turner //                     The LLVM Compiler Infrastructure
497a14e60SZachary Turner //
597a14e60SZachary Turner // This file is distributed under the University of Illinois Open Source
697a14e60SZachary Turner // License. See LICENSE.TXT for details.
797a14e60SZachary Turner //
897a14e60SZachary Turner //===----------------------------------------------------------------------===//
997a14e60SZachary Turner 
1097a14e60SZachary Turner #include "lldb/Host/Config.h"
1197a14e60SZachary Turner 
1242ff0ad8SZachary Turner #include "lldb/Host/FileSystem.h"
1397a14e60SZachary Turner #include "lldb/Host/Host.h"
1413b18261SZachary Turner #include "lldb/Host/HostInfo.h"
1513b18261SZachary Turner #include "lldb/Host/HostInfoBase.h"
165f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
176f9e6901SZachary Turner #include "lldb/Utility/Log.h"
18bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
1913b18261SZachary Turner 
20270e99abSZachary Turner #include "llvm/ADT/StringExtras.h"
211eb0d42aSPavel Labath #include "llvm/ADT/Triple.h"
2213b18261SZachary Turner #include "llvm/Support/Host.h"
2333183147SPavel Labath #include "llvm/Support/Path.h"
241eb0d42aSPavel Labath #include "llvm/Support/ScopedPrinter.h"
25c5f28e2aSKamil Rytarowski #include "llvm/Support/Threading.h"
26270e99abSZachary Turner #include "llvm/Support/raw_ostream.h"
2797a14e60SZachary Turner 
28c5f28e2aSKamil Rytarowski #include <mutex>
29b9c1b51eSKate Stone #include <thread>
3097a14e60SZachary Turner 
3197a14e60SZachary Turner using namespace lldb;
3297a14e60SZachary Turner using namespace lldb_private;
3397a14e60SZachary Turner 
34b9c1b51eSKate Stone namespace {
35ff48e4beSGreg Clayton //----------------------------------------------------------------------
3605097246SAdrian Prantl // The HostInfoBaseFields is a work around for windows not supporting static
3705097246SAdrian Prantl // variables correctly in a thread safe way. Really each of the variables in
3805097246SAdrian Prantl // HostInfoBaseFields should live in the functions in which they are used and
3905097246SAdrian Prantl // each one should be static, but the work around is in place to avoid this
4005097246SAdrian Prantl // restriction. Ick.
41ff48e4beSGreg Clayton //----------------------------------------------------------------------
42ff48e4beSGreg Clayton 
43b9c1b51eSKate Stone struct HostInfoBaseFields {
44b9c1b51eSKate Stone   ~HostInfoBaseFields() {
45b9c1b51eSKate Stone     if (m_lldb_process_tmp_dir.Exists()) {
4641331afaSZachary Turner       // Remove the LLDB temporary directory if we have one. Set "recurse" to
4705097246SAdrian Prantl       // true to all files that were created for the LLDB process can be
4805097246SAdrian Prantl       // cleaned up.
49d82067f8SZachary Turner       llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath());
5041331afaSZachary Turner     }
5141331afaSZachary Turner   }
5241331afaSZachary Turner 
53673b6e4fSZachary Turner   std::string m_host_triple;
54673b6e4fSZachary Turner 
55673b6e4fSZachary Turner   ArchSpec m_host_arch_32;
56673b6e4fSZachary Turner   ArchSpec m_host_arch_64;
57673b6e4fSZachary Turner 
58673b6e4fSZachary Turner   FileSpec m_lldb_so_dir;
59673b6e4fSZachary Turner   FileSpec m_lldb_support_exe_dir;
60673b6e4fSZachary Turner   FileSpec m_lldb_headers_dir;
61b1e1c62fSSean Callanan   FileSpec m_lldb_clang_resource_dir;
62673b6e4fSZachary Turner   FileSpec m_lldb_system_plugin_dir;
63673b6e4fSZachary Turner   FileSpec m_lldb_user_plugin_dir;
6463acdfdeSOleksiy Vyalov   FileSpec m_lldb_process_tmp_dir;
6563acdfdeSOleksiy Vyalov   FileSpec m_lldb_global_tmp_dir;
66673b6e4fSZachary Turner };
67673b6e4fSZachary Turner 
68673b6e4fSZachary Turner HostInfoBaseFields *g_fields = nullptr;
6942ff0ad8SZachary Turner }
7042ff0ad8SZachary Turner 
71b9c1b51eSKate Stone void HostInfoBase::Initialize() { g_fields = new HostInfoBaseFields(); }
7242ff0ad8SZachary Turner 
73b9c1b51eSKate Stone void HostInfoBase::Terminate() {
7441331afaSZachary Turner   delete g_fields;
7541331afaSZachary Turner   g_fields = nullptr;
7641331afaSZachary Turner }
7741331afaSZachary Turner 
78b9c1b51eSKate Stone llvm::StringRef HostInfoBase::GetTargetTriple() {
79c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
80c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
81b9c1b51eSKate Stone     g_fields->m_host_triple =
82b9c1b51eSKate Stone         HostInfo::GetArchitecture().GetTriple().getTriple();
83ff48e4beSGreg Clayton   });
84673b6e4fSZachary Turner   return g_fields->m_host_triple;
8597a14e60SZachary Turner }
8613b18261SZachary Turner 
87b9c1b51eSKate Stone const ArchSpec &HostInfoBase::GetArchitecture(ArchitectureKind arch_kind) {
88c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
89c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
90b9c1b51eSKate Stone     HostInfo::ComputeHostArchitectureSupport(g_fields->m_host_arch_32,
91b9c1b51eSKate Stone                                              g_fields->m_host_arch_64);
92ff48e4beSGreg Clayton   });
9313b18261SZachary Turner 
9413b18261SZachary Turner   // If an explicit 32 or 64-bit architecture was requested, return that.
9513b18261SZachary Turner   if (arch_kind == eArchKind32)
96673b6e4fSZachary Turner     return g_fields->m_host_arch_32;
9713b18261SZachary Turner   if (arch_kind == eArchKind64)
98673b6e4fSZachary Turner     return g_fields->m_host_arch_64;
9913b18261SZachary Turner 
10013b18261SZachary Turner   // Otherwise prefer the 64-bit architecture if it is valid.
101b9c1b51eSKate Stone   return (g_fields->m_host_arch_64.IsValid()) ? g_fields->m_host_arch_64
102b9c1b51eSKate Stone                                               : g_fields->m_host_arch_32;
10313b18261SZachary Turner }
10413b18261SZachary Turner 
1054ebb64b9SPavel Labath llvm::Optional<HostInfoBase::ArchitectureKind> HostInfoBase::ParseArchitectureKind(llvm::StringRef kind) {
1064ebb64b9SPavel Labath   return llvm::StringSwitch<llvm::Optional<ArchitectureKind>>(kind)
1074ebb64b9SPavel Labath       .Case(LLDB_ARCH_DEFAULT, eArchKindDefault)
1084ebb64b9SPavel Labath       .Case(LLDB_ARCH_DEFAULT_32BIT, eArchKind32)
1094ebb64b9SPavel Labath       .Case(LLDB_ARCH_DEFAULT_64BIT, eArchKind64)
1104ebb64b9SPavel Labath       .Default(llvm::None);
1114ebb64b9SPavel Labath }
1124ebb64b9SPavel Labath 
11360f028ffSPavel Labath FileSpec HostInfoBase::GetShlibDir() {
11460f028ffSPavel Labath   static llvm::once_flag g_once_flag;
11560f028ffSPavel Labath   static bool success = false;
11660f028ffSPavel Labath   llvm::call_once(g_once_flag, []() {
11760f028ffSPavel Labath     success = HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir);
11860f028ffSPavel Labath     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
11960f028ffSPavel Labath     LLDB_LOG(log, "shlib dir -> `{0}`", g_fields->m_lldb_so_dir);
12060f028ffSPavel Labath   });
12160f028ffSPavel Labath   return success ? g_fields->m_lldb_so_dir : FileSpec();
12260f028ffSPavel Labath }
12342ff0ad8SZachary Turner 
12460f028ffSPavel Labath FileSpec HostInfoBase::GetSupportExeDir() {
125c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
126ff48e4beSGreg Clayton   static bool success = false;
127c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
128b9c1b51eSKate Stone     success =
12960f028ffSPavel Labath         HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir);
130ff48e4beSGreg Clayton     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
13160f028ffSPavel Labath     LLDB_LOG(log, "support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);
132ff48e4beSGreg Clayton   });
13360f028ffSPavel Labath   return success ? g_fields->m_lldb_support_exe_dir : FileSpec();
13460f028ffSPavel Labath }
13560f028ffSPavel Labath 
13660f028ffSPavel Labath FileSpec HostInfoBase::GetHeaderDir() {
137c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
138ff48e4beSGreg Clayton   static bool success = false;
139c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
140ff48e4beSGreg Clayton     success = HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir);
141ff48e4beSGreg Clayton     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
14260f028ffSPavel Labath     LLDB_LOG(log, "header dir -> `{0}`", g_fields->m_lldb_headers_dir);
143ff48e4beSGreg Clayton   });
14460f028ffSPavel Labath   return success ? g_fields->m_lldb_headers_dir : FileSpec();
14560f028ffSPavel Labath }
14660f028ffSPavel Labath 
14760f028ffSPavel Labath FileSpec HostInfoBase::GetSystemPluginDir() {
148c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
149ff48e4beSGreg Clayton   static bool success = false;
150c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
151b9c1b51eSKate Stone     success = HostInfo::ComputeSystemPluginsDirectory(
152b9c1b51eSKate Stone         g_fields->m_lldb_system_plugin_dir);
153ff48e4beSGreg Clayton     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
15460f028ffSPavel Labath     LLDB_LOG(log, "system plugin dir -> `{0}`",
15560f028ffSPavel Labath              g_fields->m_lldb_system_plugin_dir);
156ff48e4beSGreg Clayton   });
15760f028ffSPavel Labath   return success ? g_fields->m_lldb_system_plugin_dir : FileSpec();
15860f028ffSPavel Labath }
15960f028ffSPavel Labath 
16060f028ffSPavel Labath FileSpec HostInfoBase::GetUserPluginDir() {
161c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
162ff48e4beSGreg Clayton   static bool success = false;
163c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
16460f028ffSPavel Labath     success =
16560f028ffSPavel Labath         HostInfo::ComputeUserPluginsDirectory(g_fields->m_lldb_user_plugin_dir);
166ff48e4beSGreg Clayton     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
16760f028ffSPavel Labath     LLDB_LOG(log, "user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);
168ff48e4beSGreg Clayton   });
16960f028ffSPavel Labath   return success ? g_fields->m_lldb_user_plugin_dir : FileSpec();
17060f028ffSPavel Labath }
17160f028ffSPavel Labath 
17260f028ffSPavel Labath FileSpec HostInfoBase::GetProcessTempDir() {
173c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
174ff48e4beSGreg Clayton   static bool success = false;
175c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
176b9c1b51eSKate Stone     success = HostInfo::ComputeProcessTempFileDirectory(
177b9c1b51eSKate Stone         g_fields->m_lldb_process_tmp_dir);
178ff48e4beSGreg Clayton     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
17960f028ffSPavel Labath     LLDB_LOG(log, "process temp dir -> `{0}`",
18060f028ffSPavel Labath              g_fields->m_lldb_process_tmp_dir);
181ff48e4beSGreg Clayton   });
18260f028ffSPavel Labath   return success ? g_fields->m_lldb_process_tmp_dir : FileSpec();
18360f028ffSPavel Labath }
18460f028ffSPavel Labath 
18560f028ffSPavel Labath FileSpec HostInfoBase::GetGlobalTempDir() {
186c5f28e2aSKamil Rytarowski   static llvm::once_flag g_once_flag;
18763acdfdeSOleksiy Vyalov   static bool success = false;
188c5f28e2aSKamil Rytarowski   llvm::call_once(g_once_flag, []() {
189b9c1b51eSKate Stone     success = HostInfo::ComputeGlobalTempFileDirectory(
190b9c1b51eSKate Stone         g_fields->m_lldb_global_tmp_dir);
19163acdfdeSOleksiy Vyalov     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
19260f028ffSPavel Labath     LLDB_LOG(log, "global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);
19363acdfdeSOleksiy Vyalov   });
19460f028ffSPavel Labath   return success ? g_fields->m_lldb_global_tmp_dir : FileSpec();
19542ff0ad8SZachary Turner }
19642ff0ad8SZachary Turner 
1977263f1bdSPavel Labath ArchSpec HostInfoBase::GetAugmentedArchSpec(llvm::StringRef triple) {
1987263f1bdSPavel Labath   if (triple.empty())
1997263f1bdSPavel Labath     return ArchSpec();
2007263f1bdSPavel Labath   llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
2017263f1bdSPavel Labath   if (!ArchSpec::ContainsOnlyArch(normalized_triple))
2027263f1bdSPavel Labath     return ArchSpec(triple);
2037263f1bdSPavel Labath 
2044ebb64b9SPavel Labath   if (auto kind = HostInfo::ParseArchitectureKind(triple))
2054ebb64b9SPavel Labath     return HostInfo::GetArchitecture(*kind);
2064ebb64b9SPavel Labath 
2077263f1bdSPavel Labath   llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
2087263f1bdSPavel Labath 
2097263f1bdSPavel Labath   if (normalized_triple.getVendorName().empty())
2107263f1bdSPavel Labath     normalized_triple.setVendor(host_triple.getVendor());
2117263f1bdSPavel Labath   if (normalized_triple.getOSName().empty())
2127263f1bdSPavel Labath     normalized_triple.setOS(host_triple.getOS());
2137263f1bdSPavel Labath   if (normalized_triple.getEnvironmentName().empty())
2147263f1bdSPavel Labath     normalized_triple.setEnvironment(host_triple.getEnvironment());
2157263f1bdSPavel Labath   return ArchSpec(normalized_triple);
2167263f1bdSPavel Labath }
2177263f1bdSPavel Labath 
218b9c1b51eSKate Stone bool HostInfoBase::ComputeSharedLibraryDirectory(FileSpec &file_spec) {
21942ff0ad8SZachary Turner   // To get paths related to LLDB we get the path to the executable that
220cc2ba028SPavel Labath   // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB".
221cc2ba028SPavel Labath   // On other posix systems, we will get .../lib(64|32)?/liblldb.so.
22242ff0ad8SZachary Turner 
22360f028ffSPavel Labath   FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress(
22460f028ffSPavel Labath       reinterpret_cast<void *>(reinterpret_cast<intptr_t>(
22560f028ffSPavel Labath           HostInfoBase::ComputeSharedLibraryDirectory))));
22642ff0ad8SZachary Turner 
227b9c1b51eSKate Stone   // This is necessary because when running the testsuite the shlib might be a
228b9c1b51eSKate Stone   // symbolic link inside the Python resource dir.
229*46376966SJonas Devlieghere   FileSystem::Instance().ResolveSymbolicLink(lldb_file_spec, lldb_file_spec);
230db7d8083SSean Callanan 
23142ff0ad8SZachary Turner   // Remove the filename so that this FileSpec only represents the directory.
2320d8400c8SGreg Clayton   file_spec.GetDirectory() = lldb_file_spec.GetDirectory();
23342ff0ad8SZachary Turner 
23442ff0ad8SZachary Turner   return (bool)file_spec.GetDirectory();
23542ff0ad8SZachary Turner }
23642ff0ad8SZachary Turner 
237b9c1b51eSKate Stone bool HostInfoBase::ComputeSupportExeDirectory(FileSpec &file_spec) {
23860f028ffSPavel Labath   file_spec = GetShlibDir();
23960f028ffSPavel Labath   return bool(file_spec);
24042ff0ad8SZachary Turner }
24142ff0ad8SZachary Turner 
242b9c1b51eSKate Stone bool HostInfoBase::ComputeProcessTempFileDirectory(FileSpec &file_spec) {
24363acdfdeSOleksiy Vyalov   FileSpec temp_file_spec;
244f3a24329STamas Berghammer   if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))
24563acdfdeSOleksiy Vyalov     return false;
24663acdfdeSOleksiy Vyalov 
2471eb0d42aSPavel Labath   std::string pid_str{llvm::to_string(Host::GetCurrentProcessID())};
248d3173f34SChaoren Lin   temp_file_spec.AppendPathComponent(pid_str);
249d3d95fd6SZachary Turner   if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))
25063acdfdeSOleksiy Vyalov     return false;
25163acdfdeSOleksiy Vyalov 
252b79863c2SChaoren Lin   file_spec.GetDirectory().SetCString(temp_file_spec.GetCString());
25363acdfdeSOleksiy Vyalov   return true;
25463acdfdeSOleksiy Vyalov }
25563acdfdeSOleksiy Vyalov 
256b9c1b51eSKate Stone bool HostInfoBase::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
25733183147SPavel Labath   llvm::SmallVector<char, 16> tmpdir;
25833183147SPavel Labath   llvm::sys::path::system_temp_directory(/*ErasedOnReboot*/ true, tmpdir);
25933183147SPavel Labath   file_spec = FileSpec(std::string(tmpdir.data(), tmpdir.size()), true);
260f3a24329STamas Berghammer   return true;
261f3a24329STamas Berghammer }
262f3a24329STamas Berghammer 
263b9c1b51eSKate Stone bool HostInfoBase::ComputeGlobalTempFileDirectory(FileSpec &file_spec) {
264f3a24329STamas Berghammer   file_spec.Clear();
265f3a24329STamas Berghammer 
266f3a24329STamas Berghammer   FileSpec temp_file_spec;
267f3a24329STamas Berghammer   if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))
268f3a24329STamas Berghammer     return false;
269f3a24329STamas Berghammer 
270270e99abSZachary Turner   temp_file_spec.AppendPathComponent("lldb");
271d3d95fd6SZachary Turner   if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))
27242ff0ad8SZachary Turner     return false;
27342ff0ad8SZachary Turner 
274b79863c2SChaoren Lin   file_spec.GetDirectory().SetCString(temp_file_spec.GetCString());
27542ff0ad8SZachary Turner   return true;
27642ff0ad8SZachary Turner }
27742ff0ad8SZachary Turner 
278b9c1b51eSKate Stone bool HostInfoBase::ComputeHeaderDirectory(FileSpec &file_spec) {
279b9c1b51eSKate Stone   // TODO(zturner): Figure out how to compute the header directory for all
280b9c1b51eSKate Stone   // platforms.
28142ff0ad8SZachary Turner   return false;
28242ff0ad8SZachary Turner }
28342ff0ad8SZachary Turner 
284b9c1b51eSKate Stone bool HostInfoBase::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
285b9c1b51eSKate Stone   // TODO(zturner): Figure out how to compute the system plugins directory for
286b9c1b51eSKate Stone   // all platforms.
28742ff0ad8SZachary Turner   return false;
28842ff0ad8SZachary Turner }
28942ff0ad8SZachary Turner 
290b9c1b51eSKate Stone bool HostInfoBase::ComputeUserPluginsDirectory(FileSpec &file_spec) {
29105097246SAdrian Prantl   // TODO(zturner): Figure out how to compute the user plugins directory for
29205097246SAdrian Prantl   // all platforms.
293b1e1c62fSSean Callanan   return false;
294b1e1c62fSSean Callanan }
295b1e1c62fSSean Callanan 
296b9c1b51eSKate Stone void HostInfoBase::ComputeHostArchitectureSupport(ArchSpec &arch_32,
297b9c1b51eSKate Stone                                                   ArchSpec &arch_64) {
298b071bee2SOleksiy Vyalov   llvm::Triple triple(llvm::sys::getProcessTriple());
29913b18261SZachary Turner 
30013b18261SZachary Turner   arch_32.Clear();
30113b18261SZachary Turner   arch_64.Clear();
30213b18261SZachary Turner 
303b9c1b51eSKate Stone   switch (triple.getArch()) {
30413b18261SZachary Turner   default:
30513b18261SZachary Turner     arch_32.SetTriple(triple);
30613b18261SZachary Turner     break;
30713b18261SZachary Turner 
308e85e6021STamas Berghammer   case llvm::Triple::aarch64:
3092559abdaSJustin Hibbits   case llvm::Triple::ppc64:
310686b27b5SPavel Labath   case llvm::Triple::ppc64le:
311f9ee7e4cSJustin Hibbits   case llvm::Triple::x86_64:
31213b18261SZachary Turner     arch_64.SetTriple(triple);
31313b18261SZachary Turner     arch_32.SetTriple(triple.get32BitArchVariant());
31413b18261SZachary Turner     break;
31513b18261SZachary Turner 
31613b18261SZachary Turner   case llvm::Triple::mips64:
3173df471c3SMohit K. Bhakkad   case llvm::Triple::mips64el:
31813b18261SZachary Turner   case llvm::Triple::sparcv9:
319bb00d0b6SUlrich Weigand   case llvm::Triple::systemz:
32013b18261SZachary Turner     arch_64.SetTriple(triple);
32113b18261SZachary Turner     break;
32213b18261SZachary Turner   }
32313b18261SZachary Turner }
324