1 //===-- PlatformDarwin.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_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWIN_H
10 #define LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWIN_H
11 
12 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/ProcessLaunchInfo.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/StructuredData.h"
18 #include "lldb/Utility/XcodeSDK.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/FileSystem.h"
21 
22 #include <string>
23 #include <tuple>
24 
25 class PlatformDarwin : public PlatformPOSIX {
26 public:
27   PlatformDarwin(bool is_host);
28 
29   ~PlatformDarwin() override;
30 
31   lldb_private::Status PutFile(const lldb_private::FileSpec &source,
32                                const lldb_private::FileSpec &destination,
33                                uint32_t uid = UINT32_MAX,
34                                uint32_t gid = UINT32_MAX) override;
35 
36   // lldb_private::Platform functions
37   lldb_private::Status
38   ResolveSymbolFile(lldb_private::Target &target,
39                     const lldb_private::ModuleSpec &sym_spec,
40                     lldb_private::FileSpec &sym_file) override;
41 
42   lldb_private::FileSpecList LocateExecutableScriptingResources(
43       lldb_private::Target *target, lldb_private::Module &module,
44       lldb_private::Stream *feedback_stream) override;
45 
46   lldb_private::Status
47   GetSharedModule(const lldb_private::ModuleSpec &module_spec,
48                   lldb_private::Process *process, lldb::ModuleSP &module_sp,
49                   const lldb_private::FileSpecList *module_search_paths_ptr,
50                   llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
51                   bool *did_create_ptr) override;
52 
53   size_t GetSoftwareBreakpointTrapOpcode(
54       lldb_private::Target &target,
55       lldb_private::BreakpointSite *bp_site) override;
56 
57   lldb::BreakpointSP
58   SetThreadCreationBreakpoint(lldb_private::Target &target) override;
59 
60   bool ModuleIsExcludedForUnconstrainedSearches(
61       lldb_private::Target &target, const lldb::ModuleSP &module_sp) override;
62 
63   bool ARMGetSupportedArchitectureAtIndex(uint32_t idx,
64                                           lldb_private::ArchSpec &arch);
65 
66   bool x86GetSupportedArchitectureAtIndex(uint32_t idx,
67                                           lldb_private::ArchSpec &arch);
68 
69   uint32_t GetResumeCountForLaunchInfo(
70       lldb_private::ProcessLaunchInfo &launch_info) override;
71 
72   lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
73                                lldb_private::Debugger &debugger,
74                                lldb_private::Target *target,
75                                lldb_private::Status &error) override;
76 
77   void CalculateTrapHandlerSymbolNames() override;
78 
79   llvm::VersionTuple
80   GetOSVersion(lldb_private::Process *process = nullptr) override;
81 
SupportsModules()82   bool SupportsModules() override { return true; }
83 
84   lldb_private::ConstString
85   GetFullNameForDylib(lldb_private::ConstString basename) override;
86 
87   lldb_private::FileSpec LocateExecutable(const char *basename) override;
88 
89   lldb_private::Status
90   LaunchProcess(lldb_private::ProcessLaunchInfo &launch_info) override;
91 
92   static std::tuple<llvm::VersionTuple, llvm::StringRef>
93   ParseVersionBuildDir(llvm::StringRef str);
94 
95   llvm::Expected<lldb_private::StructuredData::DictionarySP>
96   FetchExtendedCrashInformation(lldb_private::Process &process) override;
97 
98   /// Return the toolchain directory the current LLDB instance is located in.
99   static lldb_private::FileSpec GetCurrentToolchainDirectory();
100 
101   /// Return the command line tools directory the current LLDB instance is
102   /// located in.
103   static lldb_private::FileSpec GetCurrentCommandLineToolsDirectory();
104 
105 protected:
106   struct CrashInfoAnnotations {
107     uint64_t version;          // unsigned long
108     uint64_t message;          // char *
109     uint64_t signature_string; // char *
110     uint64_t backtrace;        // char *
111     uint64_t message2;         // char *
112     uint64_t thread;           // uint64_t
113     uint64_t dialog_mode;      // unsigned int
114     uint64_t abort_cause;      // unsigned int
115   };
116 
117   /// Extract the `__crash_info` annotations from each of of the target's
118   /// modules.
119   ///
120   /// If the platform have a crashed processes with a `__crash_info` section,
121   /// extract the section to gather the messages annotations and the abort
122   /// cause.
123   ///
124   /// \param[in] process
125   ///     The crashed process.
126   ///
127   /// \return
128   ///     A  structured data array containing at each entry in each entry, the
129   ///     module spec, its UUID, the crash messages and the abort cause.
130   ///     \b nullptr if process has no crash information annotations.
131   lldb_private::StructuredData::ArraySP
132   ExtractCrashInfoAnnotations(lldb_private::Process &process);
133 
134   void ReadLibdispatchOffsetsAddress(lldb_private::Process *process);
135 
136   void ReadLibdispatchOffsets(lldb_private::Process *process);
137 
138   virtual lldb_private::Status GetSharedModuleWithLocalCache(
139       const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
140       const lldb_private::FileSpecList *module_search_paths_ptr,
141       llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr);
142 
143   struct SDKEnumeratorInfo {
144     lldb_private::FileSpec found_path;
145     lldb_private::XcodeSDK::Type sdk_type;
146   };
147 
148   static lldb_private::FileSystem::EnumerateDirectoryResult
149   DirectoryEnumerator(void *baton, llvm::sys::fs::file_type file_type,
150                       llvm::StringRef path);
151 
152   static lldb_private::FileSpec
153   FindSDKInXcodeForModules(lldb_private::XcodeSDK::Type sdk_type,
154                            const lldb_private::FileSpec &sdks_spec);
155 
156   static lldb_private::FileSpec
157   GetSDKDirectoryForModules(lldb_private::XcodeSDK::Type sdk_type);
158 
159   void AddClangModuleCompilationOptionsForSDKType(
160       lldb_private::Target *target, std::vector<std::string> &options,
161       lldb_private::XcodeSDK::Type sdk_type);
162 
163   lldb_private::Status FindBundleBinaryInExecSearchPaths(
164       const lldb_private::ModuleSpec &module_spec,
165       lldb_private::Process *process, lldb::ModuleSP &module_sp,
166       const lldb_private::FileSpecList *module_search_paths_ptr,
167       llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr);
168 
169   static std::string FindComponentInPath(llvm::StringRef path,
170                                          llvm::StringRef component);
171 
172   std::string m_developer_directory;
173   llvm::StringMap<std::string> m_sdk_path;
174   std::mutex m_sdk_path_mutex;
175 
176 private:
177   PlatformDarwin(const PlatformDarwin &) = delete;
178   const PlatformDarwin &operator=(const PlatformDarwin &) = delete;
179 };
180 
181 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWIN_H
182