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/LLDBLog.h" 17 #include "lldb/Utility/Log.h" 18 #include "lldb/Utility/StreamString.h" 19 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" 27 28 #include <mutex> 29 #include <thread> 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 namespace { 35 /// Contains the state of the HostInfoBase plugin. 36 struct HostInfoBaseFields { 37 ~HostInfoBaseFields() { 38 if (FileSystem::Instance().Exists(m_lldb_process_tmp_dir)) { 39 // Remove the LLDB temporary directory if we have one. Set "recurse" to 40 // true to all files that were created for the LLDB process can be 41 // cleaned up. 42 llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath()); 43 } 44 } 45 46 llvm::once_flag m_host_triple_once; 47 llvm::Triple m_host_triple; 48 49 llvm::once_flag m_host_arch_once; 50 ArchSpec m_host_arch_32; 51 ArchSpec m_host_arch_64; 52 53 llvm::once_flag m_lldb_so_dir_once; 54 FileSpec m_lldb_so_dir; 55 llvm::once_flag m_lldb_support_exe_dir_once; 56 FileSpec m_lldb_support_exe_dir; 57 llvm::once_flag m_lldb_headers_dir_once; 58 FileSpec m_lldb_headers_dir; 59 llvm::once_flag m_lldb_clang_resource_dir_once; 60 FileSpec m_lldb_clang_resource_dir; 61 llvm::once_flag m_lldb_system_plugin_dir_once; 62 FileSpec m_lldb_system_plugin_dir; 63 llvm::once_flag m_lldb_user_plugin_dir_once; 64 FileSpec m_lldb_user_plugin_dir; 65 llvm::once_flag m_lldb_process_tmp_dir_once; 66 FileSpec m_lldb_process_tmp_dir; 67 llvm::once_flag m_lldb_global_tmp_dir_once; 68 FileSpec m_lldb_global_tmp_dir; 69 }; 70 } // namespace 71 72 static HostInfoBaseFields *g_fields = nullptr; 73 static HostInfoBase::SharedLibraryDirectoryHelper *g_shlib_dir_helper = nullptr; 74 75 void HostInfoBase::Initialize(SharedLibraryDirectoryHelper *helper) { 76 g_shlib_dir_helper = helper; 77 g_fields = new HostInfoBaseFields(); 78 } 79 80 void HostInfoBase::Terminate() { 81 g_shlib_dir_helper = nullptr; 82 delete g_fields; 83 g_fields = nullptr; 84 } 85 86 llvm::Triple HostInfoBase::GetTargetTriple() { 87 llvm::call_once(g_fields->m_host_triple_once, []() { 88 g_fields->m_host_triple = HostInfo::GetArchitecture().GetTriple(); 89 }); 90 return g_fields->m_host_triple; 91 } 92 93 const 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 110 llvm::Optional<HostInfoBase::ArchitectureKind> 111 HostInfoBase::ParseArchitectureKind(llvm::StringRef kind) { 112 return llvm::StringSwitch<llvm::Optional<ArchitectureKind>>(kind) 113 .Case(LLDB_ARCH_DEFAULT, eArchKindDefault) 114 .Case(LLDB_ARCH_DEFAULT_32BIT, eArchKind32) 115 .Case(LLDB_ARCH_DEFAULT_64BIT, eArchKind64) 116 .Default(llvm::None); 117 } 118 119 FileSpec HostInfoBase::GetShlibDir() { 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); 125 }); 126 return g_fields->m_lldb_so_dir; 127 } 128 129 FileSpec HostInfoBase::GetSupportExeDir() { 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); 135 }); 136 return g_fields->m_lldb_support_exe_dir; 137 } 138 139 FileSpec HostInfoBase::GetHeaderDir() { 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); 145 }); 146 return g_fields->m_lldb_headers_dir; 147 } 148 149 FileSpec HostInfoBase::GetSystemPluginDir() { 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); 157 }); 158 return g_fields->m_lldb_system_plugin_dir; 159 } 160 161 FileSpec HostInfoBase::GetUserPluginDir() { 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); 168 }); 169 return g_fields->m_lldb_user_plugin_dir; 170 } 171 172 FileSpec HostInfoBase::GetProcessTempDir() { 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); 180 }); 181 return g_fields->m_lldb_process_tmp_dir; 182 } 183 184 FileSpec HostInfoBase::GetGlobalTempDir() { 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(); 189 190 Log *log = GetLog(LLDBLog::Host); 191 LLDB_LOG(log, "global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir); 192 }); 193 return g_fields->m_lldb_global_tmp_dir; 194 } 195 196 ArchSpec HostInfoBase::GetAugmentedArchSpec(llvm::StringRef triple) { 197 if (triple.empty()) 198 return ArchSpec(); 199 llvm::Triple normalized_triple(llvm::Triple::normalize(triple)); 200 if (!ArchSpec::ContainsOnlyArch(normalized_triple)) 201 return ArchSpec(triple); 202 203 if (auto kind = HostInfo::ParseArchitectureKind(triple)) 204 return HostInfo::GetArchitecture(*kind); 205 206 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple()); 207 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 normalized_triple.setEnvironment(host_triple.getEnvironment()); 214 return ArchSpec(normalized_triple); 215 } 216 217 bool HostInfoBase::ComputePathRelativeToLibrary(FileSpec &file_spec, 218 llvm::StringRef dir) { 219 Log *log = GetLog(LLDBLog::Host); 220 221 FileSpec lldb_file_spec = GetShlibDir(); 222 if (!lldb_file_spec) 223 return false; 224 225 std::string raw_path = lldb_file_spec.GetPath(); 226 LLDB_LOGF(log, 227 "HostInfo::%s() attempting to " 228 "derive the path %s relative to liblldb install path: %s", 229 __FUNCTION__, dir.data(), raw_path.c_str()); 230 231 // Drop bin (windows) or lib 232 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path); 233 if (parent_path.empty()) { 234 LLDB_LOGF(log, 235 "HostInfo::%s() failed to find liblldb within the shared " 236 "lib path", 237 __FUNCTION__); 238 return false; 239 } 240 241 raw_path = (parent_path + dir).str(); 242 LLDB_LOGF(log, "HostInfo::%s() derived the path as: %s", __FUNCTION__, 243 raw_path.c_str()); 244 file_spec.GetDirectory().SetString(raw_path); 245 return (bool)file_spec.GetDirectory(); 246 } 247 248 bool HostInfoBase::ComputeSharedLibraryDirectory(FileSpec &file_spec) { 249 // To get paths related to LLDB we get the path to the executable that 250 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB". 251 // On other posix systems, we will get .../lib(64|32)?/liblldb.so. 252 253 FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress( 254 reinterpret_cast<void *>(HostInfoBase::ComputeSharedLibraryDirectory))); 255 256 if (g_shlib_dir_helper) 257 g_shlib_dir_helper(lldb_file_spec); 258 259 // Remove the filename so that this FileSpec only represents the directory. 260 file_spec.GetDirectory() = lldb_file_spec.GetDirectory(); 261 262 return (bool)file_spec.GetDirectory(); 263 } 264 265 bool HostInfoBase::ComputeSupportExeDirectory(FileSpec &file_spec) { 266 file_spec = GetShlibDir(); 267 return bool(file_spec); 268 } 269 270 bool HostInfoBase::ComputeProcessTempFileDirectory(FileSpec &file_spec) { 271 FileSpec temp_file_spec; 272 if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec)) 273 return false; 274 275 std::string pid_str{llvm::to_string(Host::GetCurrentProcessID())}; 276 temp_file_spec.AppendPathComponent(pid_str); 277 if (llvm::sys::fs::create_directory(temp_file_spec.GetPath())) 278 return false; 279 280 file_spec.GetDirectory().SetCString(temp_file_spec.GetCString()); 281 return true; 282 } 283 284 bool HostInfoBase::ComputeTempFileBaseDirectory(FileSpec &file_spec) { 285 llvm::SmallVector<char, 16> tmpdir; 286 llvm::sys::path::system_temp_directory(/*ErasedOnReboot*/ true, tmpdir); 287 file_spec = FileSpec(std::string(tmpdir.data(), tmpdir.size())); 288 FileSystem::Instance().Resolve(file_spec); 289 return true; 290 } 291 292 bool HostInfoBase::ComputeGlobalTempFileDirectory(FileSpec &file_spec) { 293 file_spec.Clear(); 294 295 FileSpec temp_file_spec; 296 if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec)) 297 return false; 298 299 temp_file_spec.AppendPathComponent("lldb"); 300 if (llvm::sys::fs::create_directory(temp_file_spec.GetPath())) 301 return false; 302 303 file_spec.GetDirectory().SetCString(temp_file_spec.GetCString()); 304 return true; 305 } 306 307 bool HostInfoBase::ComputeHeaderDirectory(FileSpec &file_spec) { 308 // TODO(zturner): Figure out how to compute the header directory for all 309 // platforms. 310 return false; 311 } 312 313 bool HostInfoBase::ComputeSystemPluginsDirectory(FileSpec &file_spec) { 314 // TODO(zturner): Figure out how to compute the system plugins directory for 315 // all platforms. 316 return false; 317 } 318 319 bool HostInfoBase::ComputeUserPluginsDirectory(FileSpec &file_spec) { 320 // TODO(zturner): Figure out how to compute the user plugins directory for 321 // all platforms. 322 return false; 323 } 324 325 void HostInfoBase::ComputeHostArchitectureSupport(ArchSpec &arch_32, 326 ArchSpec &arch_64) { 327 llvm::Triple triple(llvm::sys::getProcessTriple()); 328 329 arch_32.Clear(); 330 arch_64.Clear(); 331 332 switch (triple.getArch()) { 333 default: 334 arch_32.SetTriple(triple); 335 break; 336 337 case llvm::Triple::aarch64: 338 case llvm::Triple::ppc64: 339 case llvm::Triple::ppc64le: 340 case llvm::Triple::x86_64: 341 arch_64.SetTriple(triple); 342 arch_32.SetTriple(triple.get32BitArchVariant()); 343 break; 344 345 case llvm::Triple::mips64: 346 case llvm::Triple::mips64el: 347 case llvm::Triple::sparcv9: 348 case llvm::Triple::systemz: 349 arch_64.SetTriple(triple); 350 break; 351 } 352 } 353