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 
12 #include "lldb/Utility/ArchSpec.h"
13 #include "lldb/Utility/FileSpec.h"
14 #include "lldb/Utility/UUID.h"
15 #include "lldb/Utility/UserIDResolver.h"
16 #include "lldb/Utility/XcodeSDK.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Errc.h"
20 
21 #include <cstdint>
22 
23 #include <optional>
24 #include <string>
25 
26 namespace lldb_private {
27 
28 class FileSpec;
29 
30 struct SharedCacheImageInfo {
31   UUID uuid;
32   lldb::DataBufferSP data_sp;
33 };
34 
35 namespace {
36 struct HostInfoError : public llvm::ErrorInfo<HostInfoError> {
37   static char ID;
38   const std::string message_;
39 
40   HostInfoError(const std::string message) : message_(std::move(message)) {}
41 
42   void log(llvm::raw_ostream &OS) const override { OS << "HostInfoError"; }
43 
44   std::error_code convertToErrorCode() const override {
45     return llvm::inconvertibleErrorCode();
46   }
47 };
48 
49 char HostInfoError::ID = 0;
50 } // namespace
51 
52 class HostInfoBase {
53 private:
54   // Static class, unconstructable.
55   HostInfoBase() = default;
56   ~HostInfoBase() = default;
57 
58 public:
59   /// A helper function for determining the liblldb location. It receives a
60   /// FileSpec with the location of file containing _this_ code. It can
61   /// (optionally) replace it with a file spec pointing to a more canonical
62   /// copy.
63   using SharedLibraryDirectoryHelper = void(FileSpec &this_file);
64 
65   static void Initialize(SharedLibraryDirectoryHelper *helper = nullptr);
66   static void Terminate();
67 
68   /// Gets the host target triple.
69   ///
70   /// \return
71   ///     The host target triple.
72   static llvm::Triple GetTargetTriple();
73 
74   enum ArchitectureKind {
75     eArchKindDefault, // The overall default architecture that applications will
76                       // run on this host
77     eArchKind32, // If this host supports 32 bit programs, return the default 32
78                  // bit arch
79     eArchKind64  // If this host supports 64 bit programs, return the default 64
80                  // bit arch
81   };
82 
83   static const ArchSpec &
84   GetArchitecture(ArchitectureKind arch_kind = eArchKindDefault);
85 
86   static std::optional<ArchitectureKind>
87   ParseArchitectureKind(llvm::StringRef kind);
88 
89   /// Returns the directory containing the lldb shared library. Only the
90   /// directory member of the FileSpec is filled in.
91   static FileSpec GetShlibDir();
92 
93   /// Returns the directory containing the support executables (debugserver,
94   /// ...). Only the directory member of the FileSpec is filled in.
95   static FileSpec GetSupportExeDir();
96 
97   /// Returns the directory containing the lldb headers. Only the directory
98   /// member of the FileSpec is filled in.
99   static FileSpec GetHeaderDir();
100 
101   /// Returns the directory containing the system plugins. Only the directory
102   /// member of the FileSpec is filled in.
103   static FileSpec GetSystemPluginDir();
104 
105   /// Returns the directory containing the user plugins. Only the directory
106   /// member of the FileSpec is filled in.
107   static FileSpec GetUserPluginDir();
108 
109   /// Returns the proces temporary directory. This directory will be cleaned up
110   /// when this process exits. Only the directory member of the FileSpec is
111   /// filled in.
112   static FileSpec GetProcessTempDir();
113 
114   /// Returns the global temporary directory. This directory will **not** be
115   /// cleaned up when this process exits. Only the directory member of the
116   /// FileSpec is filled in.
117   static FileSpec GetGlobalTempDir();
118 
119   /// If the triple does not specify the vendor, os, and environment parts, we
120   /// "augment" these using information from the host and return the resulting
121   /// ArchSpec object.
122   static ArchSpec GetAugmentedArchSpec(llvm::StringRef triple);
123 
124   static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
125                                            llvm::StringRef dir);
126 
127   static FileSpec GetXcodeContentsDirectory() { return {}; }
128   static FileSpec GetXcodeDeveloperDirectory() { return {}; }
129 
130   struct SDKOptions {
131     std::optional<XcodeSDK> XcodeSDKSelection;
132   };
133 
134   /// Return the directory containing something like a SDK (reused for Swift).
135   static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options) {
136     return llvm::make_error<HostInfoError>("cannot determine SDK root");
137   }
138 
139   /// Return the path to a specific tool in the specified Xcode SDK.
140   static llvm::Expected<llvm::StringRef> FindSDKTool(XcodeSDK sdk,
141                                                      llvm::StringRef tool) {
142     return llvm::errorCodeToError(llvm::errc::no_such_file_or_directory);
143   }
144 
145   /// Return information about module \p image_name if it is loaded in
146   /// the current process's address space.
147   static SharedCacheImageInfo
148   GetSharedCacheImageInfo(llvm::StringRef image_name) {
149     return {};
150   }
151 
152   /// Returns the distribution id of the host
153   ///
154   /// This will be something like "ubuntu", "fedora", etc. on Linux.
155   ///
156   /// \return Returns either std::nullopt or a reference to a const std::string
157   /// containing the distribution id
158   static llvm::StringRef GetDistributionId() { return llvm::StringRef(); }
159 
160 protected:
161   static bool ComputeSharedLibraryDirectory(FileSpec &file_spec);
162   static bool ComputeSupportExeDirectory(FileSpec &file_spec);
163   static bool ComputeProcessTempFileDirectory(FileSpec &file_spec);
164   static bool ComputeGlobalTempFileDirectory(FileSpec &file_spec);
165   static bool ComputeTempFileBaseDirectory(FileSpec &file_spec);
166   static bool ComputeHeaderDirectory(FileSpec &file_spec);
167   static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
168   static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
169 
170   static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
171                                              ArchSpec &arch_64);
172 };
173 }
174 
175 #endif
176