9#include "lldb/Host/Config.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/ScopedPrinter.h"
23#include "llvm/Support/Threading.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/TargetParser/Host.h"
26#include "llvm/TargetParser/Triple.h"
37struct HostInfoBaseFields {
38 ~HostInfoBaseFields() {
43 llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath());
47 llvm::once_flag m_host_triple_once;
48 llvm::Triple m_host_triple;
50 llvm::once_flag m_host_arch_once;
51 ArchSpec m_host_arch_32;
52 ArchSpec m_host_arch_64;
54 llvm::once_flag m_lldb_so_dir_once;
55 FileSpec m_lldb_so_dir;
59 bool m_lldb_so_dir_computed =
false;
63 llvm::once_flag m_lldb_support_exe_dir_once;
64 FileSpec m_lldb_support_exe_dir;
65 llvm::once_flag m_lldb_headers_dir_once;
66 FileSpec m_lldb_headers_dir;
67 llvm::once_flag m_lldb_clang_resource_dir_once;
68 FileSpec m_lldb_clang_resource_dir;
69 llvm::once_flag m_lldb_system_plugin_dir_once;
70 FileSpec m_lldb_system_plugin_dir;
71 llvm::once_flag m_lldb_user_home_dir_once;
72 FileSpec m_lldb_user_home_dir;
73 llvm::once_flag m_lldb_user_lldb_dir_once;
74 FileSpec m_lldb_user_lldb_dir;
75 llvm::once_flag m_lldb_user_plugin_dir_once;
76 FileSpec m_lldb_user_plugin_dir;
77 llvm::once_flag m_lldb_process_tmp_dir_once;
78 FileSpec m_lldb_process_tmp_dir;
79 llvm::once_flag m_lldb_global_tmp_dir_once;
80 FileSpec m_lldb_global_tmp_dir;
98 llvm::call_once(
g_fields->m_host_triple_once, []() {
99 g_fields->m_host_triple = HostInfo::GetArchitecture().GetTriple();
105 llvm::call_once(
g_fields->m_host_arch_once, []() {
106 HostInfo::ComputeHostArchitectureSupport(g_fields->m_host_arch_32,
107 g_fields->m_host_arch_64);
121std::optional<HostInfoBase::ArchitectureKind>
123 return llvm::StringSwitch<std::optional<ArchitectureKind>>(kind)
127 .Default(std::nullopt);
131 llvm::call_once(
g_fields->m_lldb_so_dir_once, []() {
132 if (!HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir,
133 g_fields->g_shlib_dir_helper))
134 g_fields->m_lldb_so_dir = FileSpec();
136 g_fields->m_lldb_so_dir_computed = true;
138 Log *log = GetLog(LLDBLog::Host);
139 LLDB_LOG(log,
"shlib dir -> `{0}`", g_fields->m_lldb_so_dir);
145 llvm::call_once(
g_fields->m_lldb_support_exe_dir_once, []() {
146 if (!HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir))
147 g_fields->m_lldb_support_exe_dir = FileSpec();
148 Log *log = GetLog(LLDBLog::Host);
149 LLDB_LOG(log,
"support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);
151 return g_fields->m_lldb_support_exe_dir;
155 llvm::call_once(
g_fields->m_lldb_headers_dir_once, []() {
156 if (!HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir))
157 g_fields->m_lldb_headers_dir = FileSpec();
158 Log *log = GetLog(LLDBLog::Host);
159 LLDB_LOG(log,
"header dir -> `{0}`", g_fields->m_lldb_headers_dir);
161 return g_fields->m_lldb_headers_dir;
165 llvm::call_once(
g_fields->m_lldb_system_plugin_dir_once, []() {
166 if (!HostInfo::ComputeSystemPluginsDirectory(
167 g_fields->m_lldb_system_plugin_dir))
168 g_fields->m_lldb_system_plugin_dir = FileSpec();
169 Log *log = GetLog(LLDBLog::Host);
170 LLDB_LOG(log,
"system plugin dir -> `{0}`",
171 g_fields->m_lldb_system_plugin_dir);
173 return g_fields->m_lldb_system_plugin_dir;
177 llvm::call_once(
g_fields->m_lldb_user_home_dir_once, []() {
178 if (!HostInfo::ComputeUserHomeDirectory(g_fields->m_lldb_user_home_dir))
179 g_fields->m_lldb_user_home_dir = FileSpec();
180 LLDB_LOG(GetLog(LLDBLog::Host),
"user home dir -> `{0}`",
181 g_fields->m_lldb_user_home_dir);
183 return g_fields->m_lldb_user_home_dir;
187 llvm::call_once(
g_fields->m_lldb_user_lldb_dir_once, []() {
188 if (!HostInfo::ComputeUserLLDBHomeDirectory(g_fields->m_lldb_user_lldb_dir))
189 g_fields->m_lldb_user_lldb_dir = FileSpec();
190 LLDB_LOG(GetLog(LLDBLog::Host),
"user lldb home dir -> `{0}`",
191 g_fields->m_lldb_user_lldb_dir);
193 return g_fields->m_lldb_user_lldb_dir;
197 llvm::call_once(
g_fields->m_lldb_user_plugin_dir_once, []() {
198 if (!HostInfo::ComputeUserPluginsDirectory(
199 g_fields->m_lldb_user_plugin_dir))
200 g_fields->m_lldb_user_plugin_dir = FileSpec();
201 Log *log = GetLog(LLDBLog::Host);
202 LLDB_LOG(log,
"user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);
204 return g_fields->m_lldb_user_plugin_dir;
208 llvm::call_once(
g_fields->m_lldb_process_tmp_dir_once, []() {
209 if (!HostInfo::ComputeProcessTempFileDirectory(
210 g_fields->m_lldb_process_tmp_dir))
211 g_fields->m_lldb_process_tmp_dir = FileSpec();
212 Log *log = GetLog(LLDBLog::Host);
213 LLDB_LOG(log,
"process temp dir -> `{0}`",
214 g_fields->m_lldb_process_tmp_dir);
216 return g_fields->m_lldb_process_tmp_dir;
220 llvm::call_once(
g_fields->m_lldb_global_tmp_dir_once, []() {
221 if (!HostInfo::ComputeGlobalTempFileDirectory(
222 g_fields->m_lldb_global_tmp_dir))
223 g_fields->m_lldb_global_tmp_dir = FileSpec();
225 Log *log = GetLog(LLDBLog::Host);
226 LLDB_LOG(log,
"global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);
228 return g_fields->m_lldb_global_tmp_dir;
234 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
238 if (
auto kind = HostInfo::ParseArchitectureKind(triple))
239 return HostInfo::GetArchitecture(*kind);
241 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
243 if (normalized_triple.getVendorName().empty())
244 normalized_triple.setVendor(host_triple.getVendor());
245 if (normalized_triple.getOSName().empty())
246 normalized_triple.setOS(host_triple.getOS());
247 if (normalized_triple.getEnvironmentName().empty() &&
248 !host_triple.getEnvironmentName().empty())
249 normalized_triple.setEnvironment(host_triple.getEnvironment());
254 llvm::StringRef dir) {
261 std::string raw_path = lldb_file_spec.
GetPath();
264 "Attempting to derive the path {0} relative to liblldb install path: {1}",
268 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
269 if (parent_path.empty()) {
270 LLDB_LOG(log,
"Failed to find liblldb within the shared lib path");
274 raw_path = (parent_path + dir).str();
275 LLDB_LOG(log,
"Derived the path as: {0}", raw_path);
283 "SetSharedLibraryDirectoryHelper called before Initialize");
284 assert(!
g_fields->m_lldb_so_dir_computed &&
285 "SetSharedLibraryDirectoryHelper called after "
286 "ComputeSharedLibraryDirectory");
287 g_fields->g_shlib_dir_helper = helper;
300 helper(lldb_file_spec);
310 return bool(file_spec);
315 if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))
320 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
328 llvm::SmallVector<char, 16> tmpdir;
329 llvm::sys::path::system_temp_directory(
true, tmpdir);
330 file_spec =
FileSpec(std::string(tmpdir.data(), tmpdir.size()));
339 if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))
343 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
384 llvm::Triple triple(llvm::sys::getProcessTriple());
389 switch (triple.getArch()) {
394 case llvm::Triple::aarch64:
395 case llvm::Triple::ppc64:
396 case llvm::Triple::ppc64le:
397 case llvm::Triple::x86_64:
398 case llvm::Triple::riscv64:
399 case llvm::Triple::loongarch64:
401 arch_32.
SetTriple(triple.get32BitArchVariant());
404 case llvm::Triple::mips64:
405 case llvm::Triple::mips64el:
406 case llvm::Triple::sparcv9:
407 case llvm::Triple::systemz:
static HostInfoBaseFields * g_fields
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
An architecture specification class.
void Clear()
Clears the object state.
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
static bool ContainsOnlyArch(const llvm::Triple &normalized_triple)
Returns true if the OS, vendor and environment fields of the triple are unset.
void AppendPathComponent(llvm::StringRef component)
void SetDirectory(ConstString directory)
Directory string set accessor.
const ConstString & GetDirectory() const
Directory string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Clear()
Clears the object state.
ConstString GetPathAsConstString(bool denormalize=true) const
Get the full path as a ConstString.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
static FileSpec GetGlobalTempDir()
Returns the global temporary directory.
static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
void(FileSpec &this_file) SharedLibraryDirectoryHelper
A helper function for determining the liblldb location.
static bool ComputeSupportExeDirectory(FileSpec &file_spec)
static ArchSpec GetAugmentedArchSpec(llvm::StringRef triple)
If the triple does not specify the vendor, os, and environment parts, we "augment" these using inform...
static FileSpec GetHeaderDir()
Returns the directory containing the lldb headers.
static FileSpec GetUserLLDBDir()
Returns the directory containing the users lldb home (e.g.
static bool ComputeUserLLDBHomeDirectory(FileSpec &file_spec)
static bool ComputeSharedLibraryDirectory(FileSpec &file_spec, SharedLibraryDirectoryHelper *helper)
static const ArchSpec & GetArchitecture(ArchitectureKind arch_kind=eArchKindDefault)
static std::optional< ArchitectureKind > ParseArchitectureKind(llvm::StringRef kind)
static llvm::Triple GetTargetTriple()
Gets the host target triple.
static void SetSharedLibraryDirectoryHelper(SharedLibraryDirectoryHelper *helper)
static bool ComputePathRelativeToLibrary(FileSpec &file_spec, llvm::StringRef dir)
static bool ComputeTempFileBaseDirectory(FileSpec &file_spec)
static FileSpec GetSupportExeDir()
Returns the directory containing the support executables (debugserver, ...).
static bool ComputeUserPluginsDirectory(FileSpec &file_spec)
static FileSpec GetProcessTempDir()
Returns the process temporary directory.
static bool ComputeHeaderDirectory(FileSpec &file_spec)
static FileSpec GetShlibDir()
Returns the directory containing the lldb shared library.
static FileSpec GetSystemPluginDir()
Returns the directory containing the system plugins.
static bool ComputeUserHomeDirectory(FileSpec &file_spec)
static bool ComputeGlobalTempFileDirectory(FileSpec &file_spec)
static bool ComputeProcessTempFileDirectory(FileSpec &file_spec)
static FileSpec GetUserHomeDir()
Returns the directory containing the users home (e.g.
static FileSpec GetUserPluginDir()
Returns the directory containing the user plugins.
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec)
static lldb::pid_t GetCurrentProcessID()
Get the process ID for the calling process.
static FileSpec GetModuleFileSpecForHostAddress(const void *host_addr)
Given an address in the current process (the process that is running the LLDB code),...
#define LLDB_ARCH_DEFAULT_64BIT
#define LLDB_ARCH_DEFAULT
CPU Type definitions.
#define LLDB_ARCH_DEFAULT_32BIT
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.