1//===-- HostInfoBase.cpp ----------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Host/Config.h"
10
11#include "lldb/Host/FileSystem.h"
12#include "lldb/Host/Host.h"
13#include "lldb/Host/HostInfo.h"
14#include "lldb/Host/HostInfoBase.h"
15#include "lldb/Utility/ArchSpec.h"
16#include "lldb/Utility/Log.h"
17#include "lldb/Utility/StreamString.h"
18
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/Support/Host.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/ScopedPrinter.h"
24#include "llvm/Support/Threading.h"
25#include "llvm/Support/raw_ostream.h"
26
27#include <mutex>
28#include <thread>
29
30using namespace lldb;
31using namespace lldb_private;
32
33namespace {
34// The HostInfoBaseFields is a work around for windows not supporting static
35// variables correctly in a thread safe way. Really each of the variables in
36// HostInfoBaseFields should live in the functions in which they are used and
37// each one should be static, but the work around is in place to avoid this
38// restriction. Ick.
39
40struct HostInfoBaseFields {
41  ~HostInfoBaseFields() {
42    if (FileSystem::Instance().Exists(m_lldb_process_tmp_dir)) {
43      // Remove the LLDB temporary directory if we have one. Set "recurse" to
44      // true to all files that were created for the LLDB process can be
45      // cleaned up.
46      llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath());
47    }
48  }
49
50  llvm::once_flag m_host_triple_once;
51  llvm::Triple m_host_triple;
52
53  llvm::once_flag m_host_arch_once;
54  ArchSpec m_host_arch_32;
55  ArchSpec m_host_arch_64;
56
57  llvm::once_flag m_lldb_so_dir_once;
58  FileSpec m_lldb_so_dir;
59  llvm::once_flag m_lldb_support_exe_dir_once;
60  FileSpec m_lldb_support_exe_dir;
61  llvm::once_flag m_lldb_headers_dir_once;
62  FileSpec m_lldb_headers_dir;
63  llvm::once_flag m_lldb_clang_resource_dir_once;
64  FileSpec m_lldb_clang_resource_dir;
65  llvm::once_flag m_lldb_system_plugin_dir_once;
66  FileSpec m_lldb_system_plugin_dir;
67  llvm::once_flag m_lldb_user_plugin_dir_once;
68  FileSpec m_lldb_user_plugin_dir;
69  llvm::once_flag m_lldb_process_tmp_dir_once;
70  FileSpec m_lldb_process_tmp_dir;
71  llvm::once_flag m_lldb_global_tmp_dir_once;
72  FileSpec m_lldb_global_tmp_dir;
73};
74
75HostInfoBaseFields *g_fields = nullptr;
76}
77
78void HostInfoBase::Initialize() { g_fields = new HostInfoBaseFields(); }
79
80void HostInfoBase::Terminate() {
81  delete g_fields;
82  g_fields = nullptr;
83}
84
85llvm::Triple HostInfoBase::GetTargetTriple() {
86  llvm::call_once(g_fields->m_host_triple_once, []() {
87    g_fields->m_host_triple =
88        HostInfo::GetArchitecture().GetTriple();
89  });
90  return g_fields->m_host_triple;
91}
92
93const ArchSpec &HostInfoBase::GetArchitecture(ArchitectureKind arch_kind) {
94  llvm::call_once(g_fields->m_host_arch_once, []() {
95    HostInfo::ComputeHostArchitectureSupport(g_fields->m_host_arch_32,
96                                             g_fields->m_host_arch_64);
97  });
98
99  // If an explicit 32 or 64-bit architecture was requested, return that.
100  if (arch_kind == eArchKind32)
101    return g_fields->m_host_arch_32;
102  if (arch_kind == eArchKind64)
103    return g_fields->m_host_arch_64;
104
105  // Otherwise prefer the 64-bit architecture if it is valid.
106  return (g_fields->m_host_arch_64.IsValid()) ? g_fields->m_host_arch_64
107                                              : g_fields->m_host_arch_32;
108}
109
110llvm::Optional<HostInfoBase::ArchitectureKind> HostInfoBase::ParseArchitectureKind(llvm::StringRef kind) {
111  return llvm::StringSwitch<llvm::Optional<ArchitectureKind>>(kind)
112      .Case(LLDB_ARCH_DEFAULT, eArchKindDefault)
113      .Case(LLDB_ARCH_DEFAULT_32BIT, eArchKind32)
114      .Case(LLDB_ARCH_DEFAULT_64BIT, eArchKind64)
115      .Default(llvm::None);
116}
117
118FileSpec HostInfoBase::GetShlibDir() {
119  llvm::call_once(g_fields->m_lldb_so_dir_once, []() {
120    if (!HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir))
121      g_fields->m_lldb_so_dir = FileSpec();
122    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
123    LLDB_LOG(log, "shlib dir -> `{0}`", g_fields->m_lldb_so_dir);
124  });
125  return g_fields->m_lldb_so_dir;
126}
127
128FileSpec HostInfoBase::GetSupportExeDir() {
129  llvm::call_once(g_fields->m_lldb_support_exe_dir_once, []() {
130    if (!HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir))
131      g_fields->m_lldb_support_exe_dir = FileSpec();
132    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
133    LLDB_LOG(log, "support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);
134  });
135  return g_fields->m_lldb_support_exe_dir;
136}
137
138FileSpec HostInfoBase::GetHeaderDir() {
139  llvm::call_once(g_fields->m_lldb_headers_dir_once, []() {
140    if (!HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir))
141      g_fields->m_lldb_headers_dir = FileSpec();
142    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
143    LLDB_LOG(log, "header dir -> `{0}`", g_fields->m_lldb_headers_dir);
144  });
145  return g_fields->m_lldb_headers_dir;
146}
147
148FileSpec HostInfoBase::GetSystemPluginDir() {
149  llvm::call_once(g_fields->m_lldb_system_plugin_dir_once, []() {
150    if (!HostInfo::ComputeSystemPluginsDirectory(g_fields->m_lldb_system_plugin_dir))
151      g_fields->m_lldb_system_plugin_dir = FileSpec();
152    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
153    LLDB_LOG(log, "system plugin dir -> `{0}`",
154             g_fields->m_lldb_system_plugin_dir);
155  });
156  return g_fields->m_lldb_system_plugin_dir;
157}
158
159FileSpec HostInfoBase::GetUserPluginDir() {
160  llvm::call_once(g_fields->m_lldb_user_plugin_dir_once, []() {
161    if (!HostInfo::ComputeUserPluginsDirectory(g_fields->m_lldb_user_plugin_dir))
162      g_fields->m_lldb_user_plugin_dir = FileSpec();
163    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
164    LLDB_LOG(log, "user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);
165  });
166  return g_fields->m_lldb_user_plugin_dir;
167}
168
169FileSpec HostInfoBase::GetProcessTempDir() {
170  llvm::call_once(g_fields->m_lldb_process_tmp_dir_once, []() {
171    if (!HostInfo::ComputeProcessTempFileDirectory( g_fields->m_lldb_process_tmp_dir))
172      g_fields->m_lldb_process_tmp_dir = FileSpec();
173    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
174    LLDB_LOG(log, "process temp dir -> `{0}`",
175             g_fields->m_lldb_process_tmp_dir);
176  });
177  return g_fields->m_lldb_process_tmp_dir;
178}
179
180FileSpec HostInfoBase::GetGlobalTempDir() {
181  llvm::call_once(g_fields->m_lldb_global_tmp_dir_once, []() {
182    if (!HostInfo::ComputeGlobalTempFileDirectory( g_fields->m_lldb_global_tmp_dir))
183      g_fields->m_lldb_global_tmp_dir = FileSpec();
184
185    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
186    LLDB_LOG(log, "global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);
187  });
188  return g_fields->m_lldb_global_tmp_dir;
189}
190
191ArchSpec HostInfoBase::GetAugmentedArchSpec(llvm::StringRef triple) {
192  if (triple.empty())
193    return ArchSpec();
194  llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
195  if (!ArchSpec::ContainsOnlyArch(normalized_triple))
196    return ArchSpec(triple);
197
198  if (auto kind = HostInfo::ParseArchitectureKind(triple))
199    return HostInfo::GetArchitecture(*kind);
200
201  llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
202
203  if (normalized_triple.getVendorName().empty())
204    normalized_triple.setVendor(host_triple.getVendor());
205  if (normalized_triple.getOSName().empty())
206    normalized_triple.setOS(host_triple.getOS());
207  if (normalized_triple.getEnvironmentName().empty())
208    normalized_triple.setEnvironment(host_triple.getEnvironment());
209  return ArchSpec(normalized_triple);
210}
211
212bool HostInfoBase::ComputePathRelativeToLibrary(FileSpec &file_spec,
213                                                llvm::StringRef dir) {
214  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
215
216  FileSpec lldb_file_spec = GetShlibDir();
217  if (!lldb_file_spec)
218    return false;
219
220  std::string raw_path = lldb_file_spec.GetPath();
221  LLDB_LOGF(log,
222            "HostInfo::%s() attempting to "
223            "derive the path %s relative to liblldb install path: %s",
224            __FUNCTION__, dir.data(), raw_path.c_str());
225
226  // Drop bin (windows) or lib
227  llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
228  if (parent_path.empty()) {
229    LLDB_LOGF(log,
230              "HostInfo::%s() failed to find liblldb within the shared "
231              "lib path",
232              __FUNCTION__);
233    return false;
234  }
235
236  raw_path = (parent_path + dir).str();
237  LLDB_LOGF(log, "HostInfo::%s() derived the path as: %s", __FUNCTION__,
238            raw_path.c_str());
239  file_spec.GetDirectory().SetString(raw_path);
240  return (bool)file_spec.GetDirectory();
241}
242
243bool HostInfoBase::ComputeSharedLibraryDirectory(FileSpec &file_spec) {
244  // To get paths related to LLDB we get the path to the executable that
245  // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB".
246  // On other posix systems, we will get .../lib(64|32)?/liblldb.so.
247
248  FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress(
249      reinterpret_cast<void *>(
250          HostInfoBase::ComputeSharedLibraryDirectory)));
251
252  // This is necessary because when running the testsuite the shlib might be a
253  // symbolic link inside the Python resource dir.
254  FileSystem::Instance().ResolveSymbolicLink(lldb_file_spec, lldb_file_spec);
255
256  // Remove the filename so that this FileSpec only represents the directory.
257  file_spec.GetDirectory() = lldb_file_spec.GetDirectory();
258
259  return (bool)file_spec.GetDirectory();
260}
261
262bool HostInfoBase::ComputeSupportExeDirectory(FileSpec &file_spec) {
263  file_spec = GetShlibDir();
264  return bool(file_spec);
265}
266
267bool HostInfoBase::ComputeProcessTempFileDirectory(FileSpec &file_spec) {
268  FileSpec temp_file_spec;
269  if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))
270    return false;
271
272  std::string pid_str{llvm::to_string(Host::GetCurrentProcessID())};
273  temp_file_spec.AppendPathComponent(pid_str);
274  if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))
275    return false;
276
277  file_spec.GetDirectory().SetCString(temp_file_spec.GetCString());
278  return true;
279}
280
281bool HostInfoBase::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
282  llvm::SmallVector<char, 16> tmpdir;
283  llvm::sys::path::system_temp_directory(/*ErasedOnReboot*/ true, tmpdir);
284  file_spec = FileSpec(std::string(tmpdir.data(), tmpdir.size()));
285  FileSystem::Instance().Resolve(file_spec);
286  return true;
287}
288
289bool HostInfoBase::ComputeGlobalTempFileDirectory(FileSpec &file_spec) {
290  file_spec.Clear();
291
292  FileSpec temp_file_spec;
293  if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))
294    return false;
295
296  temp_file_spec.AppendPathComponent("lldb");
297  if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))
298    return false;
299
300  file_spec.GetDirectory().SetCString(temp_file_spec.GetCString());
301  return true;
302}
303
304bool HostInfoBase::ComputeHeaderDirectory(FileSpec &file_spec) {
305  // TODO(zturner): Figure out how to compute the header directory for all
306  // platforms.
307  return false;
308}
309
310bool HostInfoBase::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
311  // TODO(zturner): Figure out how to compute the system plugins directory for
312  // all platforms.
313  return false;
314}
315
316bool HostInfoBase::ComputeUserPluginsDirectory(FileSpec &file_spec) {
317  // TODO(zturner): Figure out how to compute the user plugins directory for
318  // all platforms.
319  return false;
320}
321
322void HostInfoBase::ComputeHostArchitectureSupport(ArchSpec &arch_32,
323                                                  ArchSpec &arch_64) {
324  llvm::Triple triple(llvm::sys::getProcessTriple());
325
326  arch_32.Clear();
327  arch_64.Clear();
328
329  switch (triple.getArch()) {
330  default:
331    arch_32.SetTriple(triple);
332    break;
333
334  case llvm::Triple::aarch64:
335  case llvm::Triple::ppc64:
336  case llvm::Triple::ppc64le:
337  case llvm::Triple::x86_64:
338    arch_64.SetTriple(triple);
339    arch_32.SetTriple(triple.get32BitArchVariant());
340    break;
341
342  case llvm::Triple::mips64:
343  case llvm::Triple::mips64el:
344  case llvm::Triple::sparcv9:
345  case llvm::Triple::systemz:
346    arch_64.SetTriple(triple);
347    break;
348  }
349}
350