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 void *GetImageBaton() const { return m_image_baton; }
63
64private:
69
70 std::string m_filename;
75 std::shared_ptr<LazyExtractor> m_lazy;
76};
77
78namespace {
79struct HostInfoError : public llvm::ErrorInfo<HostInfoError> {
80 static char ID;
81 const std::string message_;
82
83 HostInfoError(const std::string message) : message_(std::move(message)) {}
84
85 void log(llvm::raw_ostream &OS) const override { OS << "HostInfoError"; }
86
87 std::error_code convertToErrorCode() const override {
88 return llvm::inconvertibleErrorCode();
89 }
90};
91
92char HostInfoError::ID = 0;
93} // namespace
94
96private:
97 // Static class, unconstructable.
98 HostInfoBase() = default;
99 ~HostInfoBase() = default;
100
101public:
102 /// A helper function for determining the liblldb location. It receives a
103 /// FileSpec with the location of file containing _this_ code. It can
104 /// (optionally) replace it with a file spec pointing to a more canonical
105 /// copy.
106 using SharedLibraryDirectoryHelper = void(FileSpec &this_file);
107 static void
109
110 static void Initialize();
111 static void Terminate();
112
113 /// Gets the host target triple.
114 ///
115 /// \return
116 /// The host target triple.
117 static llvm::Triple GetTargetTriple();
118
120 eArchKindDefault, // The overall default architecture that applications will
121 // run on this host
122 eArchKind32, // If this host supports 32 bit programs, return the default 32
123 // bit arch
124 eArchKind64 // If this host supports 64 bit programs, return the default 64
125 // bit arch
126 };
127
128 static const ArchSpec &
130
131 static std::optional<ArchitectureKind>
132 ParseArchitectureKind(llvm::StringRef kind);
133
134 /// Returns the directory containing the lldb shared library. Only the
135 /// directory member of the FileSpec is filled in.
136 static FileSpec GetShlibDir();
137
138 /// Returns the directory containing the support executables (debugserver,
139 /// ...). Only the directory member of the FileSpec is filled in.
140 static FileSpec GetSupportExeDir();
141
142 /// Returns the directory containing the lldb headers. Only the directory
143 /// member of the FileSpec is filled in.
144 static FileSpec GetHeaderDir();
145
146 /// Returns the directory containing the system plugins. Only the directory
147 /// member of the FileSpec is filled in.
149
150 /// Returns the directory containing the users home (e.g. `~/`). Only the
151 /// directory member of the FileSpec is filled in.
152 static FileSpec GetUserHomeDir();
153
154 /// Returns the directory containing the users lldb home (e.g. `~/.lldb/`).
155 /// Only the directory member of the FileSpec is filled in.
156 static FileSpec GetUserLLDBDir();
157
158 /// Returns the directory containing the user plugins. Only the directory
159 /// member of the FileSpec is filled in.
160 static FileSpec GetUserPluginDir();
161
162 /// Returns the process temporary directory. This directory will be cleaned up
163 /// when this process exits. Only the directory member of the FileSpec is
164 /// filled in.
166
167 /// Returns the global temporary directory. This directory will **not** be
168 /// cleaned up when this process exits. Only the directory member of the
169 /// FileSpec is filled in.
170 static FileSpec GetGlobalTempDir();
171
172 /// If the triple does not specify the vendor, os, and environment parts, we
173 /// "augment" these using information from the host and return the resulting
174 /// ArchSpec object.
175 static ArchSpec GetAugmentedArchSpec(llvm::StringRef triple);
176
177 static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
178 llvm::StringRef dir);
179
180 static FileSpec GetXcodeContentsDirectory() { return {}; }
181 static FileSpec GetXcodeDeveloperDirectory() { return {}; }
184
185 struct SDKOptions {
186 std::optional<XcodeSDK> XcodeSDKSelection;
187 };
188
189 /// Return the directory containing something like a SDK (reused for Swift).
190 static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options) {
191 return llvm::make_error<HostInfoError>("cannot determine SDK root");
192 }
193
194 /// Return the path to a specific tool in the specified Xcode SDK.
195 static llvm::Expected<llvm::StringRef> FindSDKTool(XcodeSDK sdk,
196 llvm::StringRef tool) {
197 return llvm::errorCodeToError(llvm::errc::no_such_file_or_directory);
198 }
199
200 /// Return information about module \p filepath if it is loaded in
201 /// the current process's address space.
202 ///
203 /// \param[in] sc_mode
204 /// Flag to control if this method can try to read a shared
205 /// cache binary blob directly, needed to keep user settings out of
206 /// Host.
208 GetSharedCacheImageInfo(llvm::StringRef filepath,
210 return {};
211 }
212
213 /// Return information about module \p uuid if it is loaded in
214 /// the current process's address space.
215 ///
216 /// \param[in] sc_mode
217 /// Flag to control if this method can try to read a shared
218 /// cache binary blob directly, needed to keep user settings out of
219 /// Host.
223 return {};
224 }
225
226 /// Return information about module \p filepath, if it is loaded in
227 /// the current process's address space using shared cache \p sc_uuid.
228 /// The shared cache must have been previously indexed.
229 ///
230 /// \param[in] sc_mode
231 /// Flag to control if this method can try to read a shared
232 /// cache binary blob directly, needed to keep user settings out of
233 /// Host.
235 GetSharedCacheImageInfo(llvm::StringRef filepath, const UUID &sc_uuid,
237 return {};
238 }
239
240 /// Return information about module \p uuid, if it is loaded in
241 /// the current process's address space using shared cache \p sc_uuid.
242 /// The shared cache must have been previously indexed.
243 ///
244 /// \param[in] sc_mode
245 /// Flag to control if this method can try to read a shared
246 /// cache binary blob directly, needed to keep user settings out of
247 /// Host.
249 GetSharedCacheImageInfo(const UUID &uuid, const UUID &sc_uuid,
251 return {};
252 }
253
254 /// Scan the files in a shared cache, if the filepath and uuid match
255 /// on the debug host.
256 /// Returns false if the shared cache filepath did not exist, or uuid
257 /// did not match.
258 static bool SharedCacheIndexFiles(FileSpec &filepath, UUID &uuid,
260 return false;
261 }
262
263 /// Returns the distribution id of the host
264 ///
265 /// This will be something like "ubuntu", "fedora", etc. on Linux.
266 ///
267 /// \return Returns either std::nullopt or a reference to a const std::string
268 /// containing the distribution id
269 static llvm::StringRef GetDistributionId() { return llvm::StringRef(); }
270
271protected:
272 static bool
275 static bool ComputeSupportExeDirectory(FileSpec &file_spec);
276 static bool ComputeProcessTempFileDirectory(FileSpec &file_spec);
277 static bool ComputeGlobalTempFileDirectory(FileSpec &file_spec);
278 static bool ComputeTempFileBaseDirectory(FileSpec &file_spec);
279 static bool ComputeHeaderDirectory(FileSpec &file_spec);
280 static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
281 static bool ComputeUserHomeDirectory(FileSpec &file_spec);
282 static bool ComputeUserLLDBHomeDirectory(FileSpec &file_spec);
283 static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
284
285 static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
286 ArchSpec &arch_64);
287};
288} // namespace lldb_private
289
290#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