LLDB mainline
HostInfoBase.h
Go to the documentation of this file.
1//===-- HostInfoBase.h ------------------------------------------*- C++ -*-===//
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#ifndef LLDB_HOST_HOSTINFOBASE_H
10#define LLDB_HOST_HOSTINFOBASE_H
11
15#include "lldb/Utility/UUID.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Errc.h"
21#include "llvm/Support/Threading.h"
22
23#include <cstdint>
24
25#include <memory>
26#include <optional>
27#include <string>
28
29namespace lldb_private {
30
31class FileSpec;
32
37 SharedCacheImageInfo(std::string filename, UUID uuid,
38 lldb::DataExtractorSP extractor_sp)
39 : m_filename(std::move(filename)), m_uuid(uuid),
40 m_extractor_sp(extractor_sp), m_create_data_extractor(nullptr),
41 m_image_baton(nullptr), m_lazy() {}
43 std::string filename, UUID uuid,
44 lldb::DataExtractorSP (*create_data_extractor)(void *image),
45 void *image_baton)
46 : m_filename(std::move(filename)), m_uuid(uuid), m_extractor_sp(),
47 m_create_data_extractor(create_data_extractor),
48 m_image_baton(image_baton), m_lazy(std::make_shared<LazyExtractor>()) {}
49
50 /// Copies share the extractor, which is created at most once.
52 if (m_lazy) {
53 llvm::call_once(m_lazy->once, [&] {
54 m_lazy->extractor_sp = m_create_data_extractor(m_image_baton);
55 });
56 return m_lazy->extractor_sp;
57 }
58 return m_extractor_sp;
59 }
60 llvm::StringRef GetFilename() const { return m_filename; }
61 const UUID &GetUUID() const { return m_uuid; }
62
63private:
68
69 std::string m_filename;
74 std::shared_ptr<LazyExtractor> m_lazy;
75};
76
77namespace {
78struct HostInfoError : public llvm::ErrorInfo<HostInfoError> {
79 static char ID;
80 const std::string message_;
81
82 HostInfoError(const std::string message) : message_(std::move(message)) {}
83
84 void log(llvm::raw_ostream &OS) const override { OS << "HostInfoError"; }
85
86 std::error_code convertToErrorCode() const override {
87 return llvm::inconvertibleErrorCode();
88 }
89};
90
91char HostInfoError::ID = 0;
92} // namespace
93
95private:
96 // Static class, unconstructable.
97 HostInfoBase() = default;
98 ~HostInfoBase() = default;
99
100public:
101 /// A helper function for determining the liblldb location. It receives a
102 /// FileSpec with the location of file containing _this_ code. It can
103 /// (optionally) replace it with a file spec pointing to a more canonical
104 /// copy.
105 using SharedLibraryDirectoryHelper = void(FileSpec &this_file);
106 static void
108
109 static void Initialize();
110 static void Terminate();
111
112 /// Gets the host target triple.
113 ///
114 /// \return
115 /// The host target triple.
116 static llvm::Triple GetTargetTriple();
117
119 eArchKindDefault, // The overall default architecture that applications will
120 // run on this host
121 eArchKind32, // If this host supports 32 bit programs, return the default 32
122 // bit arch
123 eArchKind64 // If this host supports 64 bit programs, return the default 64
124 // bit arch
125 };
126
127 static const ArchSpec &
129
130 static std::optional<ArchitectureKind>
131 ParseArchitectureKind(llvm::StringRef kind);
132
133 /// Returns the directory containing the lldb shared library. Only the
134 /// directory member of the FileSpec is filled in.
135 static FileSpec GetShlibDir();
136
137 /// Returns the directory containing the support executables (debugserver,
138 /// ...). Only the directory member of the FileSpec is filled in.
139 static FileSpec GetSupportExeDir();
140
141 /// Returns the directory containing the lldb headers. Only the directory
142 /// member of the FileSpec is filled in.
143 static FileSpec GetHeaderDir();
144
145 /// Returns the directory containing the system plugins. Only the directory
146 /// member of the FileSpec is filled in.
148
149 /// Returns the directory containing the users home (e.g. `~/`). Only the
150 /// directory member of the FileSpec is filled in.
151 static FileSpec GetUserHomeDir();
152
153 /// Returns the directory containing the users lldb home (e.g. `~/.lldb/`).
154 /// Only the directory member of the FileSpec is filled in.
155 static FileSpec GetUserLLDBDir();
156
157 /// Returns the directory containing the user plugins. Only the directory
158 /// member of the FileSpec is filled in.
159 static FileSpec GetUserPluginDir();
160
161 /// Returns the process temporary directory. This directory will be cleaned up
162 /// when this process exits. Only the directory member of the FileSpec is
163 /// filled in.
165
166 /// Returns the global temporary directory. This directory will **not** be
167 /// cleaned up when this process exits. Only the directory member of the
168 /// FileSpec is filled in.
169 static FileSpec GetGlobalTempDir();
170
171 /// If the triple does not specify the vendor, os, and environment parts, we
172 /// "augment" these using information from the host and return the resulting
173 /// ArchSpec object.
174 static ArchSpec GetAugmentedArchSpec(llvm::StringRef triple);
175
176 static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
177 llvm::StringRef dir);
178
179 static FileSpec GetXcodeContentsDirectory() { return {}; }
180 static FileSpec GetXcodeDeveloperDirectory() { return {}; }
183
184 struct SDKOptions {
185 std::optional<XcodeSDK> XcodeSDKSelection;
186 };
187
188 /// Return the directory containing something like a SDK (reused for Swift).
189 static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options) {
190 return llvm::make_error<HostInfoError>("cannot determine SDK root");
191 }
192
193 /// Return the path to a specific tool in the specified Xcode SDK.
194 static llvm::Expected<llvm::StringRef> FindSDKTool(XcodeSDK sdk,
195 llvm::StringRef tool) {
196 return llvm::errorCodeToError(llvm::errc::no_such_file_or_directory);
197 }
198
199 /// Return information about module \p filepath if it is loaded in
200 /// the current process's address space.
201 ///
202 /// \param[in] sc_mode
203 /// Flag to control if this method can try to read a shared
204 /// cache binary blob directly, needed to keep user settings out of
205 /// Host.
207 GetSharedCacheImageInfo(llvm::StringRef filepath,
209 return {};
210 }
211
212 /// Return information about module \p uuid if it is loaded in
213 /// the current process's address space.
214 ///
215 /// \param[in] sc_mode
216 /// Flag to control if this method can try to read a shared
217 /// cache binary blob directly, needed to keep user settings out of
218 /// Host.
222 return {};
223 }
224
225 /// Return information about module \p filepath, if it is loaded in
226 /// the current process's address space using shared cache \p sc_uuid.
227 /// The shared cache must have been previously indexed.
228 ///
229 /// \param[in] sc_mode
230 /// Flag to control if this method can try to read a shared
231 /// cache binary blob directly, needed to keep user settings out of
232 /// Host.
234 GetSharedCacheImageInfo(llvm::StringRef filepath, const UUID &sc_uuid,
236 return {};
237 }
238
239 /// Return information about module \p uuid, if it is loaded in
240 /// the current process's address space using shared cache \p sc_uuid.
241 /// The shared cache must have been previously indexed.
242 ///
243 /// \param[in] sc_mode
244 /// Flag to control if this method can try to read a shared
245 /// cache binary blob directly, needed to keep user settings out of
246 /// Host.
248 GetSharedCacheImageInfo(const UUID &uuid, const UUID &sc_uuid,
250 return {};
251 }
252
253 /// Scan the files in a shared cache, if the filepath and uuid match
254 /// on the debug host.
255 /// Returns false if the shared cache filepath did not exist, or uuid
256 /// did not match.
257 static bool SharedCacheIndexFiles(FileSpec &filepath, UUID &uuid,
259 return false;
260 }
261
262 /// Returns the distribution id of the host
263 ///
264 /// This will be something like "ubuntu", "fedora", etc. on Linux.
265 ///
266 /// \return Returns either std::nullopt or a reference to a const std::string
267 /// containing the distribution id
268 static llvm::StringRef GetDistributionId() { return llvm::StringRef(); }
269
270protected:
271 static bool
274 static bool ComputeSupportExeDirectory(FileSpec &file_spec);
275 static bool ComputeProcessTempFileDirectory(FileSpec &file_spec);
276 static bool ComputeGlobalTempFileDirectory(FileSpec &file_spec);
277 static bool ComputeTempFileBaseDirectory(FileSpec &file_spec);
278 static bool ComputeHeaderDirectory(FileSpec &file_spec);
279 static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
280 static bool ComputeUserHomeDirectory(FileSpec &file_spec);
281 static bool ComputeUserLLDBHomeDirectory(FileSpec &file_spec);
282 static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
283
284 static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
285 ArchSpec &arch_64);
286};
287} // namespace lldb_private
288
289#endif
static char ID
An architecture specification class.
Definition ArchSpec.h:32
A file utility class.
Definition FileSpec.h:56
static FileSpec GetGlobalTempDir()
Returns the global temporary directory.
static FileSpec GetCurrentXcodeToolchainDirectory()
static llvm::Expected< llvm::StringRef > GetSDKRoot(SDKOptions options)
Return the directory containing something like a SDK (reused for Swift).
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 llvm::StringRef GetDistributionId()
Returns the distribution id of the host.
static SharedCacheImageInfo GetSharedCacheImageInfo(llvm::StringRef filepath, lldb::SymbolSharedCacheUse sc_mode)
Return information about module filepath if it is loaded in the current process's address space.
static FileSpec GetXcodeDeveloperDirectory()
static llvm::Expected< llvm::StringRef > FindSDKTool(XcodeSDK sdk, llvm::StringRef tool)
Return the path to a specific tool in the specified Xcode SDK.
static bool SharedCacheIndexFiles(FileSpec &filepath, UUID &uuid, lldb::SymbolSharedCacheUse sc_mode)
Scan the files in a shared cache, if the filepath and uuid match on the debug host.
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 SharedCacheImageInfo GetSharedCacheImageInfo(const UUID &uuid, const UUID &sc_uuid, lldb::SymbolSharedCacheUse sc_mode)
Return information about module uuid, if it is loaded in the current process's address space using sh...
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 GetCurrentCommandLineToolsDirectory()
static FileSpec GetUserHomeDir()
Returns the directory containing the users home (e.g.
static FileSpec GetUserPluginDir()
Returns the directory containing the user plugins.
static SharedCacheImageInfo GetSharedCacheImageInfo(const UUID &uuid, lldb::SymbolSharedCacheUse sc_mode)
Return information about module uuid if it is loaded in the current process's address space.
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec)
static SharedCacheImageInfo GetSharedCacheImageInfo(llvm::StringRef filepath, const UUID &sc_uuid, lldb::SymbolSharedCacheUse sc_mode)
Return information about module filepath, if it is loaded in the current process's address space usin...
static FileSpec GetXcodeContentsDirectory()
Represents UUID's of various sizes.
Definition UUID.h:27
An abstraction for Xcode-style SDKs that works like ArchSpec.
Definition XcodeSDK.h:25
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::optional< XcodeSDK > XcodeSDKSelection
lldb::DataExtractorSP(* m_create_data_extractor)(void *image)
llvm::StringRef GetFilename() const
SharedCacheImageInfo(std::string filename, UUID uuid, lldb::DataExtractorSP(*create_data_extractor)(void *image), void *image_baton)
std::shared_ptr< LazyExtractor > m_lazy
lldb::DataExtractorSP GetExtractor()
Copies share the extractor, which is created at most once.
SharedCacheImageInfo(std::string filename, UUID uuid, lldb::DataExtractorSP extractor_sp)
lldb::DataExtractorSP m_extractor_sp