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