9 #include "lldb/Host/Config.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/ScopedPrinter.h"
25 #include "llvm/Support/Threading.h"
26 #include "llvm/Support/raw_ostream.h"
36 struct HostInfoBaseFields {
37 ~HostInfoBaseFields() {
38 if (FileSystem::Instance().Exists(m_lldb_process_tmp_dir)) {
42 llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath());
46 llvm::once_flag m_host_triple_once;
47 llvm::Triple m_host_triple;
49 llvm::once_flag m_host_arch_once;
53 llvm::once_flag m_lldb_so_dir_once;
55 llvm::once_flag m_lldb_support_exe_dir_once;
57 llvm::once_flag m_lldb_headers_dir_once;
59 llvm::once_flag m_lldb_clang_resource_dir_once;
61 llvm::once_flag m_lldb_system_plugin_dir_once;
63 llvm::once_flag m_lldb_user_plugin_dir_once;
65 llvm::once_flag m_lldb_process_tmp_dir_once;
67 llvm::once_flag m_lldb_global_tmp_dir_once;
80 void HostInfoBase::Terminate() {
86 llvm::Triple HostInfoBase::GetTargetTriple() {
87 llvm::call_once(
g_fields->m_host_triple_once, []() {
88 g_fields->m_host_triple = HostInfo::GetArchitecture().GetTriple();
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);
100 if (arch_kind == eArchKind32)
102 if (arch_kind == eArchKind64)
110 llvm::Optional<HostInfoBase::ArchitectureKind>
111 HostInfoBase::ParseArchitectureKind(llvm::StringRef kind) {
112 return llvm::StringSwitch<llvm::Optional<ArchitectureKind>>(kind)
116 .Default(llvm::None);
120 llvm::call_once(
g_fields->m_lldb_so_dir_once, []() {
121 if (!HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir))
122 g_fields->m_lldb_so_dir = FileSpec();
123 Log *log = GetLog(LLDBLog::Host);
124 LLDB_LOG(log,
"shlib dir -> `{0}`", g_fields->m_lldb_so_dir);
130 llvm::call_once(
g_fields->m_lldb_support_exe_dir_once, []() {
131 if (!HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir))
132 g_fields->m_lldb_support_exe_dir = FileSpec();
133 Log *log = GetLog(LLDBLog::Host);
134 LLDB_LOG(log,
"support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);
136 return g_fields->m_lldb_support_exe_dir;
140 llvm::call_once(
g_fields->m_lldb_headers_dir_once, []() {
141 if (!HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir))
142 g_fields->m_lldb_headers_dir = FileSpec();
143 Log *log = GetLog(LLDBLog::Host);
144 LLDB_LOG(log,
"header dir -> `{0}`", g_fields->m_lldb_headers_dir);
146 return g_fields->m_lldb_headers_dir;
150 llvm::call_once(
g_fields->m_lldb_system_plugin_dir_once, []() {
151 if (!HostInfo::ComputeSystemPluginsDirectory(
152 g_fields->m_lldb_system_plugin_dir))
153 g_fields->m_lldb_system_plugin_dir = FileSpec();
154 Log *log = GetLog(LLDBLog::Host);
155 LLDB_LOG(log,
"system plugin dir -> `{0}`",
156 g_fields->m_lldb_system_plugin_dir);
158 return g_fields->m_lldb_system_plugin_dir;
162 llvm::call_once(
g_fields->m_lldb_user_plugin_dir_once, []() {
163 if (!HostInfo::ComputeUserPluginsDirectory(
164 g_fields->m_lldb_user_plugin_dir))
165 g_fields->m_lldb_user_plugin_dir = FileSpec();
166 Log *log = GetLog(LLDBLog::Host);
167 LLDB_LOG(log,
"user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);
169 return g_fields->m_lldb_user_plugin_dir;
173 llvm::call_once(
g_fields->m_lldb_process_tmp_dir_once, []() {
174 if (!HostInfo::ComputeProcessTempFileDirectory(
175 g_fields->m_lldb_process_tmp_dir))
176 g_fields->m_lldb_process_tmp_dir = FileSpec();
177 Log *log = GetLog(LLDBLog::Host);
178 LLDB_LOG(log,
"process temp dir -> `{0}`",
179 g_fields->m_lldb_process_tmp_dir);
181 return g_fields->m_lldb_process_tmp_dir;
185 llvm::call_once(
g_fields->m_lldb_global_tmp_dir_once, []() {
186 if (!HostInfo::ComputeGlobalTempFileDirectory(
187 g_fields->m_lldb_global_tmp_dir))
188 g_fields->m_lldb_global_tmp_dir = FileSpec();
190 Log *log = GetLog(LLDBLog::Host);
191 LLDB_LOG(log,
"global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);
193 return g_fields->m_lldb_global_tmp_dir;
196 ArchSpec HostInfoBase::GetAugmentedArchSpec(llvm::StringRef triple) {
199 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
200 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
203 if (
auto kind = HostInfo::ParseArchitectureKind(triple))
204 return HostInfo::GetArchitecture(*kind);
206 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
208 if (normalized_triple.getVendorName().empty())
209 normalized_triple.setVendor(host_triple.getVendor());
210 if (normalized_triple.getOSName().empty())
211 normalized_triple.setOS(host_triple.getOS());
212 if (normalized_triple.getEnvironmentName().empty() &&
213 !host_triple.getEnvironmentName().empty())
214 normalized_triple.setEnvironment(host_triple.getEnvironment());
218 bool HostInfoBase::ComputePathRelativeToLibrary(
FileSpec &file_spec,
219 llvm::StringRef dir) {
222 FileSpec lldb_file_spec = GetShlibDir();
228 "HostInfo::%s() attempting to "
229 "derive the path %s relative to liblldb install path: %s",
230 __FUNCTION__, dir.data(), raw_path.c_str());
233 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
234 if (parent_path.empty()) {
236 "HostInfo::%s() failed to find liblldb within the shared "
242 raw_path = (parent_path + dir).str();
243 LLDB_LOGF(log,
"HostInfo::%s() derived the path as: %s", __FUNCTION__,
249 bool HostInfoBase::ComputeSharedLibraryDirectory(
FileSpec &file_spec) {
254 FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress(
255 reinterpret_cast<void *
>(HostInfoBase::ComputeSharedLibraryDirectory)));
266 bool HostInfoBase::ComputeSupportExeDirectory(
FileSpec &file_spec) {
267 file_spec = GetShlibDir();
268 return bool(file_spec);
271 bool HostInfoBase::ComputeProcessTempFileDirectory(
FileSpec &file_spec) {
273 if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))
276 std::string pid_str{llvm::to_string(Host::GetCurrentProcessID())};
278 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
285 bool HostInfoBase::ComputeTempFileBaseDirectory(
FileSpec &file_spec) {
286 llvm::SmallVector<char, 16> tmpdir;
287 llvm::sys::path::system_temp_directory(
true, tmpdir);
289 FileSystem::Instance().Resolve(file_spec);
293 bool HostInfoBase::ComputeGlobalTempFileDirectory(
FileSpec &file_spec) {
297 if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))
301 if (llvm::sys::fs::create_directory(temp_file_spec.
GetPath()))
308 bool HostInfoBase::ComputeHeaderDirectory(
FileSpec &file_spec) {
314 bool HostInfoBase::ComputeSystemPluginsDirectory(
FileSpec &file_spec) {
320 bool HostInfoBase::ComputeUserPluginsDirectory(
FileSpec &file_spec) {
326 void HostInfoBase::ComputeHostArchitectureSupport(
ArchSpec &arch_32,
328 llvm::Triple triple(llvm::sys::getProcessTriple());
333 switch (triple.getArch()) {
338 case llvm::Triple::aarch64:
339 case llvm::Triple::ppc64:
340 case llvm::Triple::ppc64le:
341 case llvm::Triple::x86_64:
343 arch_32.
SetTriple(triple.get32BitArchVariant());
346 case llvm::Triple::mips64:
347 case llvm::Triple::mips64el:
348 case llvm::Triple::sparcv9:
349 case llvm::Triple::systemz: