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;
56 llvm::once_flag m_lldb_support_exe_dir_once;
57 FileSpec m_lldb_support_exe_dir;
58 llvm::once_flag m_lldb_headers_dir_once;
59 FileSpec m_lldb_headers_dir;
60 llvm::once_flag m_lldb_clang_resource_dir_once;
61 FileSpec m_lldb_clang_resource_dir;
62 llvm::once_flag m_lldb_system_plugin_dir_once;
63 FileSpec m_lldb_system_plugin_dir;
64 llvm::once_flag m_lldb_user_home_dir_once;
65 FileSpec m_lldb_user_home_dir;
66 llvm::once_flag m_lldb_user_lldb_dir_once;
67 FileSpec m_lldb_user_lldb_dir;
68 llvm::once_flag m_lldb_user_plugin_dir_once;
69 FileSpec m_lldb_user_plugin_dir;
70 llvm::once_flag m_lldb_process_tmp_dir_once;
71 FileSpec m_lldb_process_tmp_dir;
72 llvm::once_flag m_lldb_global_tmp_dir_once;
73 FileSpec m_lldb_global_tmp_dir;
94 llvm::call_once(
g_fields->m_host_triple_once, []() {
95 g_fields->m_host_triple = HostInfo::GetArchitecture().GetTriple();
101 llvm::call_once(
g_fields->m_host_arch_once, []() {
102 HostInfo::ComputeHostArchitectureSupport(g_fields->m_host_arch_32,
103 g_fields->m_host_arch_64);
117std::optional<HostInfoBase::ArchitectureKind>
119 return llvm::StringSwitch<std::optional<ArchitectureKind>>(kind)
123 .Default(std::nullopt);
127 llvm::call_once(
g_fields->m_lldb_so_dir_once, []() {
128 if (!HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir))
129 g_fields->m_lldb_so_dir = FileSpec();
130 Log *log = GetLog(LLDBLog::Host);
131 LLDB_LOG(log,
"shlib dir -> `{0}`", g_fields->m_lldb_so_dir);
137 llvm::call_once(
g_fields->m_lldb_support_exe_dir_once, []() {
138 if (!HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir))
139 g_fields->m_lldb_support_exe_dir = FileSpec();
140 Log *log = GetLog(LLDBLog::Host);
141 LLDB_LOG(log,
"support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);
143 return g_fields->m_lldb_support_exe_dir;
147 llvm::call_once(
g_fields->m_lldb_headers_dir_once, []() {
148 if (!HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir))
149 g_fields->m_lldb_headers_dir = FileSpec();
150 Log *log = GetLog(LLDBLog::Host);
151 LLDB_LOG(log,
"header dir -> `{0}`", g_fields->m_lldb_headers_dir);
153 return g_fields->m_lldb_headers_dir;
157 llvm::call_once(
g_fields->m_lldb_system_plugin_dir_once, []() {
158 if (!HostInfo::ComputeSystemPluginsDirectory(
159 g_fields->m_lldb_system_plugin_dir))
160 g_fields->m_lldb_system_plugin_dir = FileSpec();
161 Log *log = GetLog(LLDBLog::Host);
162 LLDB_LOG(log,
"system plugin dir -> `{0}`",
163 g_fields->m_lldb_system_plugin_dir);
165 return g_fields->m_lldb_system_plugin_dir;
169 llvm::call_once(
g_fields->m_lldb_user_home_dir_once, []() {
170 if (!HostInfo::ComputeUserHomeDirectory(g_fields->m_lldb_user_home_dir))
171 g_fields->m_lldb_user_home_dir = FileSpec();
172 LLDB_LOG(GetLog(LLDBLog::Host),
"user home dir -> `{0}`",
173 g_fields->m_lldb_user_home_dir);
175 return g_fields->m_lldb_user_home_dir;
179 llvm::call_once(
g_fields->m_lldb_user_lldb_dir_once, []() {
180 if (!HostInfo::ComputeUserLLDBHomeDirectory(g_fields->m_lldb_user_lldb_dir))
181 g_fields->m_lldb_user_lldb_dir = FileSpec();
182 LLDB_LOG(GetLog(LLDBLog::Host),
"user lldb home dir -> `{0}`",
183 g_fields->m_lldb_user_lldb_dir);
185 return g_fields->m_lldb_user_lldb_dir;
189 llvm::call_once(
g_fields->m_lldb_user_plugin_dir_once, []() {
190 if (!HostInfo::ComputeUserPluginsDirectory(
191 g_fields->m_lldb_user_plugin_dir))
192 g_fields->m_lldb_user_plugin_dir = FileSpec();
193 Log *log = GetLog(LLDBLog::Host);
194 LLDB_LOG(log,
"user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);
196 return g_fields->m_lldb_user_plugin_dir;
200 llvm::call_once(
g_fields->m_lldb_process_tmp_dir_once, []() {
201 if (!HostInfo::ComputeProcessTempFileDirectory(
202 g_fields->m_lldb_process_tmp_dir))
203 g_fields->m_lldb_process_tmp_dir = FileSpec();
204 Log *log = GetLog(LLDBLog::Host);
205 LLDB_LOG(log,
"process temp dir -> `{0}`",
206 g_fields->m_lldb_process_tmp_dir);
208 return g_fields->m_lldb_process_tmp_dir;
212 llvm::call_once(
g_fields->m_lldb_global_tmp_dir_once, []() {
213 if (!HostInfo::ComputeGlobalTempFileDirectory(
214 g_fields->m_lldb_global_tmp_dir))
215 g_fields->m_lldb_global_tmp_dir = FileSpec();
217 Log *log = GetLog(LLDBLog::Host);
218 LLDB_LOG(log,
"global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);
220 return g_fields->m_lldb_global_tmp_dir;
226 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
230 if (
auto kind = HostInfo::ParseArchitectureKind(triple))
231 return HostInfo::GetArchitecture(*kind);
233 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
235 if (normalized_triple.getVendorName().empty())
236 normalized_triple.setVendor(host_triple.getVendor());
237 if (normalized_triple.getOSName().empty())
238 normalized_triple.setOS(host_triple.getOS());
239 if (normalized_triple.getEnvironmentName().empty() &&
240 !host_triple.getEnvironmentName().empty())
241 normalized_triple.setEnvironment(host_triple.getEnvironment());
246 llvm::StringRef dir) {
253 std::string raw_path = lldb_file_spec.
GetPath();
256 "Attempting to derive the path {0} relative to liblldb install path: {1}",
260 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
261 if (parent_path.empty()) {
262 LLDB_LOG(log,
"Failed to find liblldb within the shared lib path");
266 raw_path = (parent_path + dir).str();
267 LLDB_LOG(log,
"Derived the path as: {0}", raw_path);
291 return bool(file_spec);
296 if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))
301 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
309 llvm::SmallVector<char, 16> tmpdir;
310 llvm::sys::path::system_temp_directory(
true, tmpdir);
311 file_spec =
FileSpec(std::string(tmpdir.data(), tmpdir.size()));
320 if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))
324 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
365 llvm::Triple triple(llvm::sys::getProcessTriple());
370 switch (triple.getArch()) {
375 case llvm::Triple::aarch64:
376 case llvm::Triple::ppc64:
377 case llvm::Triple::ppc64le:
378 case llvm::Triple::x86_64:
379 case llvm::Triple::riscv64:
380 case llvm::Triple::loongarch64:
382 arch_32.
SetTriple(triple.get32BitArchVariant());
385 case llvm::Triple::mips64:
386 case llvm::Triple::mips64el:
387 case llvm::Triple::sparcv9:
388 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.
constexpr lldb_private::HostInfo::SharedLibraryDirectoryHelper * g_shlib_dir_helper
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.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
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 ComputeSharedLibraryDirectory(FileSpec &file_spec)
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 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 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 void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
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.