Path: blob/main/contrib/llvm-project/lldb/source/Host/common/HostInfoBase.cpp
39607 views
//===-- HostInfoBase.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Host/Config.h"910#include "lldb/Host/FileSystem.h"11#include "lldb/Host/Host.h"12#include "lldb/Host/HostInfo.h"13#include "lldb/Host/HostInfoBase.h"14#include "lldb/Utility/ArchSpec.h"15#include "lldb/Utility/LLDBLog.h"16#include "lldb/Utility/Log.h"17#include "lldb/Utility/StreamString.h"1819#include "llvm/ADT/StringExtras.h"20#include "llvm/Support/Path.h"21#include "llvm/Support/ScopedPrinter.h"22#include "llvm/Support/Threading.h"23#include "llvm/Support/raw_ostream.h"24#include "llvm/TargetParser/Host.h"25#include "llvm/TargetParser/Triple.h"2627#include <mutex>28#include <optional>29#include <thread>3031using namespace lldb;32using namespace lldb_private;3334namespace {35/// Contains the state of the HostInfoBase plugin.36struct HostInfoBaseFields {37~HostInfoBaseFields() {38if (FileSystem::Instance().Exists(m_lldb_process_tmp_dir)) {39// Remove the LLDB temporary directory if we have one. Set "recurse" to40// true to all files that were created for the LLDB process can be41// cleaned up.42llvm::sys::fs::remove_directories(m_lldb_process_tmp_dir.GetPath());43}44}4546llvm::once_flag m_host_triple_once;47llvm::Triple m_host_triple;4849llvm::once_flag m_host_arch_once;50ArchSpec m_host_arch_32;51ArchSpec m_host_arch_64;5253llvm::once_flag m_lldb_so_dir_once;54FileSpec m_lldb_so_dir;55llvm::once_flag m_lldb_support_exe_dir_once;56FileSpec m_lldb_support_exe_dir;57llvm::once_flag m_lldb_headers_dir_once;58FileSpec m_lldb_headers_dir;59llvm::once_flag m_lldb_clang_resource_dir_once;60FileSpec m_lldb_clang_resource_dir;61llvm::once_flag m_lldb_system_plugin_dir_once;62FileSpec m_lldb_system_plugin_dir;63llvm::once_flag m_lldb_user_plugin_dir_once;64FileSpec m_lldb_user_plugin_dir;65llvm::once_flag m_lldb_process_tmp_dir_once;66FileSpec m_lldb_process_tmp_dir;67llvm::once_flag m_lldb_global_tmp_dir_once;68FileSpec m_lldb_global_tmp_dir;69};70} // namespace7172static HostInfoBaseFields *g_fields = nullptr;73static HostInfoBase::SharedLibraryDirectoryHelper *g_shlib_dir_helper = nullptr;7475void HostInfoBase::Initialize(SharedLibraryDirectoryHelper *helper) {76g_shlib_dir_helper = helper;77g_fields = new HostInfoBaseFields();78}7980void HostInfoBase::Terminate() {81g_shlib_dir_helper = nullptr;82delete g_fields;83g_fields = nullptr;84}8586llvm::Triple HostInfoBase::GetTargetTriple() {87llvm::call_once(g_fields->m_host_triple_once, []() {88g_fields->m_host_triple = HostInfo::GetArchitecture().GetTriple();89});90return g_fields->m_host_triple;91}9293const ArchSpec &HostInfoBase::GetArchitecture(ArchitectureKind arch_kind) {94llvm::call_once(g_fields->m_host_arch_once, []() {95HostInfo::ComputeHostArchitectureSupport(g_fields->m_host_arch_32,96g_fields->m_host_arch_64);97});9899// If an explicit 32 or 64-bit architecture was requested, return that.100if (arch_kind == eArchKind32)101return g_fields->m_host_arch_32;102if (arch_kind == eArchKind64)103return g_fields->m_host_arch_64;104105// Otherwise prefer the 64-bit architecture if it is valid.106return (g_fields->m_host_arch_64.IsValid()) ? g_fields->m_host_arch_64107: g_fields->m_host_arch_32;108}109110std::optional<HostInfoBase::ArchitectureKind>111HostInfoBase::ParseArchitectureKind(llvm::StringRef kind) {112return llvm::StringSwitch<std::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(std::nullopt);117}118119FileSpec HostInfoBase::GetShlibDir() {120llvm::call_once(g_fields->m_lldb_so_dir_once, []() {121if (!HostInfo::ComputeSharedLibraryDirectory(g_fields->m_lldb_so_dir))122g_fields->m_lldb_so_dir = FileSpec();123Log *log = GetLog(LLDBLog::Host);124LLDB_LOG(log, "shlib dir -> `{0}`", g_fields->m_lldb_so_dir);125});126return g_fields->m_lldb_so_dir;127}128129FileSpec HostInfoBase::GetSupportExeDir() {130llvm::call_once(g_fields->m_lldb_support_exe_dir_once, []() {131if (!HostInfo::ComputeSupportExeDirectory(g_fields->m_lldb_support_exe_dir))132g_fields->m_lldb_support_exe_dir = FileSpec();133Log *log = GetLog(LLDBLog::Host);134LLDB_LOG(log, "support exe dir -> `{0}`", g_fields->m_lldb_support_exe_dir);135});136return g_fields->m_lldb_support_exe_dir;137}138139FileSpec HostInfoBase::GetHeaderDir() {140llvm::call_once(g_fields->m_lldb_headers_dir_once, []() {141if (!HostInfo::ComputeHeaderDirectory(g_fields->m_lldb_headers_dir))142g_fields->m_lldb_headers_dir = FileSpec();143Log *log = GetLog(LLDBLog::Host);144LLDB_LOG(log, "header dir -> `{0}`", g_fields->m_lldb_headers_dir);145});146return g_fields->m_lldb_headers_dir;147}148149FileSpec HostInfoBase::GetSystemPluginDir() {150llvm::call_once(g_fields->m_lldb_system_plugin_dir_once, []() {151if (!HostInfo::ComputeSystemPluginsDirectory(152g_fields->m_lldb_system_plugin_dir))153g_fields->m_lldb_system_plugin_dir = FileSpec();154Log *log = GetLog(LLDBLog::Host);155LLDB_LOG(log, "system plugin dir -> `{0}`",156g_fields->m_lldb_system_plugin_dir);157});158return g_fields->m_lldb_system_plugin_dir;159}160161FileSpec HostInfoBase::GetUserPluginDir() {162llvm::call_once(g_fields->m_lldb_user_plugin_dir_once, []() {163if (!HostInfo::ComputeUserPluginsDirectory(164g_fields->m_lldb_user_plugin_dir))165g_fields->m_lldb_user_plugin_dir = FileSpec();166Log *log = GetLog(LLDBLog::Host);167LLDB_LOG(log, "user plugin dir -> `{0}`", g_fields->m_lldb_user_plugin_dir);168});169return g_fields->m_lldb_user_plugin_dir;170}171172FileSpec HostInfoBase::GetProcessTempDir() {173llvm::call_once(g_fields->m_lldb_process_tmp_dir_once, []() {174if (!HostInfo::ComputeProcessTempFileDirectory(175g_fields->m_lldb_process_tmp_dir))176g_fields->m_lldb_process_tmp_dir = FileSpec();177Log *log = GetLog(LLDBLog::Host);178LLDB_LOG(log, "process temp dir -> `{0}`",179g_fields->m_lldb_process_tmp_dir);180});181return g_fields->m_lldb_process_tmp_dir;182}183184FileSpec HostInfoBase::GetGlobalTempDir() {185llvm::call_once(g_fields->m_lldb_global_tmp_dir_once, []() {186if (!HostInfo::ComputeGlobalTempFileDirectory(187g_fields->m_lldb_global_tmp_dir))188g_fields->m_lldb_global_tmp_dir = FileSpec();189190Log *log = GetLog(LLDBLog::Host);191LLDB_LOG(log, "global temp dir -> `{0}`", g_fields->m_lldb_global_tmp_dir);192});193return g_fields->m_lldb_global_tmp_dir;194}195196ArchSpec HostInfoBase::GetAugmentedArchSpec(llvm::StringRef triple) {197if (triple.empty())198return ArchSpec();199llvm::Triple normalized_triple(llvm::Triple::normalize(triple));200if (!ArchSpec::ContainsOnlyArch(normalized_triple))201return ArchSpec(triple);202203if (auto kind = HostInfo::ParseArchitectureKind(triple))204return HostInfo::GetArchitecture(*kind);205206llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());207208if (normalized_triple.getVendorName().empty())209normalized_triple.setVendor(host_triple.getVendor());210if (normalized_triple.getOSName().empty())211normalized_triple.setOS(host_triple.getOS());212if (normalized_triple.getEnvironmentName().empty() &&213!host_triple.getEnvironmentName().empty())214normalized_triple.setEnvironment(host_triple.getEnvironment());215return ArchSpec(normalized_triple);216}217218bool HostInfoBase::ComputePathRelativeToLibrary(FileSpec &file_spec,219llvm::StringRef dir) {220Log *log = GetLog(LLDBLog::Host);221222FileSpec lldb_file_spec = GetShlibDir();223if (!lldb_file_spec)224return false;225226std::string raw_path = lldb_file_spec.GetPath();227LLDB_LOG(228log,229"Attempting to derive the path {0} relative to liblldb install path: {1}",230dir, raw_path);231232// Drop bin (windows) or lib233llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);234if (parent_path.empty()) {235LLDB_LOG(log, "Failed to find liblldb within the shared lib path");236return false;237}238239raw_path = (parent_path + dir).str();240LLDB_LOG(log, "Derived the path as: {0}", raw_path);241file_spec.SetDirectory(raw_path);242return (bool)file_spec.GetDirectory();243}244245bool HostInfoBase::ComputeSharedLibraryDirectory(FileSpec &file_spec) {246// To get paths related to LLDB we get the path to the executable that247// contains this function. On MacOSX this will be "LLDB.framework/.../LLDB".248// On other posix systems, we will get .../lib(64|32)?/liblldb.so.249250FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress(251reinterpret_cast<void *>(HostInfoBase::ComputeSharedLibraryDirectory)));252253if (g_shlib_dir_helper)254g_shlib_dir_helper(lldb_file_spec);255256// Remove the filename so that this FileSpec only represents the directory.257file_spec.SetDirectory(lldb_file_spec.GetDirectory());258259return (bool)file_spec.GetDirectory();260}261262bool HostInfoBase::ComputeSupportExeDirectory(FileSpec &file_spec) {263file_spec = GetShlibDir();264return bool(file_spec);265}266267bool HostInfoBase::ComputeProcessTempFileDirectory(FileSpec &file_spec) {268FileSpec temp_file_spec;269if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec))270return false;271272std::string pid_str{llvm::to_string(Host::GetCurrentProcessID())};273temp_file_spec.AppendPathComponent(pid_str);274if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))275return false;276277file_spec.SetDirectory(temp_file_spec.GetPathAsConstString());278return true;279}280281bool HostInfoBase::ComputeTempFileBaseDirectory(FileSpec &file_spec) {282llvm::SmallVector<char, 16> tmpdir;283llvm::sys::path::system_temp_directory(/*ErasedOnReboot*/ true, tmpdir);284file_spec = FileSpec(std::string(tmpdir.data(), tmpdir.size()));285FileSystem::Instance().Resolve(file_spec);286return true;287}288289bool HostInfoBase::ComputeGlobalTempFileDirectory(FileSpec &file_spec) {290file_spec.Clear();291292FileSpec temp_file_spec;293if (!HostInfo::ComputeTempFileBaseDirectory(temp_file_spec))294return false;295296temp_file_spec.AppendPathComponent("lldb");297if (llvm::sys::fs::create_directory(temp_file_spec.GetPath()))298return false;299300file_spec.SetDirectory(temp_file_spec.GetPathAsConstString());301return true;302}303304bool HostInfoBase::ComputeHeaderDirectory(FileSpec &file_spec) {305// TODO(zturner): Figure out how to compute the header directory for all306// platforms.307return false;308}309310bool HostInfoBase::ComputeSystemPluginsDirectory(FileSpec &file_spec) {311// TODO(zturner): Figure out how to compute the system plugins directory for312// all platforms.313return false;314}315316bool HostInfoBase::ComputeUserPluginsDirectory(FileSpec &file_spec) {317// TODO(zturner): Figure out how to compute the user plugins directory for318// all platforms.319return false;320}321322void HostInfoBase::ComputeHostArchitectureSupport(ArchSpec &arch_32,323ArchSpec &arch_64) {324llvm::Triple triple(llvm::sys::getProcessTriple());325326arch_32.Clear();327arch_64.Clear();328329switch (triple.getArch()) {330default:331arch_32.SetTriple(triple);332break;333334case llvm::Triple::aarch64:335case llvm::Triple::ppc64:336case llvm::Triple::ppc64le:337case llvm::Triple::x86_64:338case llvm::Triple::riscv64:339case llvm::Triple::loongarch64:340arch_64.SetTriple(triple);341arch_32.SetTriple(triple.get32BitArchVariant());342break;343344case llvm::Triple::mips64:345case llvm::Triple::mips64el:346case llvm::Triple::sparcv9:347case llvm::Triple::systemz:348arch_64.SetTriple(triple);349break;350}351}352353354