Coverage Report

Created: 2022-01-18 06:27

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