1 //===-- PlatformDarwinKernel.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_PLATFORMDARWINKERNEL_H
10 #define LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H
11 
12 #include "lldb/Utility/ConstString.h"
13 
14 #if defined(__APPLE__) // This Plugin uses the Mac-specific
15                        // source/Host/macosx/cfcpp utilities
16 
17 #include "lldb/Utility/FileSpec.h"
18 
19 #include "llvm/Support/FileSystem.h"
20 
21 #include "PlatformDarwin.h"
22 
23 class PlatformDarwinKernel : public PlatformDarwin {
24 public:
25   // Class Functions
26   static lldb::PlatformSP CreateInstance(bool force,
27                                          const lldb_private::ArchSpec *arch);
28 
29   static void DebuggerInitialize(lldb_private::Debugger &debugger);
30 
31   static void Initialize();
32 
33   static void Terminate();
34 
35   static lldb_private::ConstString GetPluginNameStatic();
36 
37   static const char *GetDescriptionStatic();
38 
39   // Class Methods
40   PlatformDarwinKernel(lldb_private::LazyBool is_ios_debug_session);
41 
42   virtual ~PlatformDarwinKernel();
43 
44   // lldb_private::PluginInterface functions
GetPluginName()45   lldb_private::ConstString GetPluginName() override {
46     return GetPluginNameStatic();
47   }
48 
GetPluginVersion()49   uint32_t GetPluginVersion() override { return 1; }
50 
51   // lldb_private::Platform functions
GetDescription()52   const char *GetDescription() override { return GetDescriptionStatic(); }
53 
54   void GetStatus(lldb_private::Stream &strm) override;
55 
56   lldb_private::Status
57   GetSharedModule(const lldb_private::ModuleSpec &module_spec,
58                   lldb_private::Process *process, lldb::ModuleSP &module_sp,
59                   const lldb_private::FileSpecList *module_search_paths_ptr,
60                   llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
61                   bool *did_create_ptr) override;
62 
63   bool GetSupportedArchitectureAtIndex(uint32_t idx,
64                                        lldb_private::ArchSpec &arch) override;
65 
SupportsModules()66   bool SupportsModules() override { return false; }
67 
68   void CalculateTrapHandlerSymbolNames() override;
69 
70 protected:
71   // Map from kext bundle ID ("com.apple.filesystems.exfat") to FileSpec for the
72   // kext bundle on
73   // the host ("/System/Library/Extensions/exfat.kext/Contents/Info.plist").
74   typedef std::multimap<lldb_private::ConstString, lldb_private::FileSpec>
75       BundleIDToKextMap;
76   typedef BundleIDToKextMap::iterator BundleIDToKextIterator;
77 
78   typedef std::vector<lldb_private::FileSpec> KernelBinaryCollection;
79 
80   // Array of directories that were searched for kext bundles (used only for
81   // reporting to user)
82   typedef std::vector<lldb_private::FileSpec> DirectoriesSearchedCollection;
83   typedef DirectoriesSearchedCollection::iterator DirectoriesSearchedIterator;
84 
85   // Populate m_search_directories and m_search_directories_no_recursing vectors
86   // of directories
87   void CollectKextAndKernelDirectories();
88 
89   void GetUserSpecifiedDirectoriesToSearch();
90 
91   static void AddRootSubdirsToSearchPaths(PlatformDarwinKernel *thisp,
92                                           const std::string &dir);
93 
94   void AddSDKSubdirsToSearchPaths(const std::string &dir);
95 
96   static lldb_private::FileSystem::EnumerateDirectoryResult
97   FindKDKandSDKDirectoriesInDirectory(void *baton, llvm::sys::fs::file_type ft,
98                                       llvm::StringRef path);
99 
100   void SearchForKextsAndKernelsRecursively();
101 
102   static lldb_private::FileSystem::EnumerateDirectoryResult
103   GetKernelsAndKextsInDirectoryWithRecursion(void *baton,
104                                              llvm::sys::fs::file_type ft,
105                                              llvm::StringRef path);
106 
107   static lldb_private::FileSystem::EnumerateDirectoryResult
108   GetKernelsAndKextsInDirectoryNoRecursion(void *baton,
109                                            llvm::sys::fs::file_type ft,
110                                            llvm::StringRef path);
111 
112   static lldb_private::FileSystem::EnumerateDirectoryResult
113   GetKernelsAndKextsInDirectoryHelper(void *baton, llvm::sys::fs::file_type ft,
114                                       llvm::StringRef path, bool recurse);
115 
116   static std::vector<lldb_private::FileSpec>
117   SearchForExecutablesRecursively(const std::string &dir);
118 
119   static void AddKextToMap(PlatformDarwinKernel *thisp,
120                            const lldb_private::FileSpec &file_spec);
121 
122   // Returns true if there is a .dSYM bundle next to the kext, or next to the
123   // binary inside the kext.
124   static bool
125   KextHasdSYMSibling(const lldb_private::FileSpec &kext_bundle_filepath);
126 
127   // Returns true if there is a .dSYM bundle next to the kernel
128   static bool
129   KernelHasdSYMSibling(const lldb_private::FileSpec &kernel_filepath);
130 
131   // Returns true if there is a .dSYM bundle with NO kernel binary next to it
132   static bool KerneldSYMHasNoSiblingBinary(
133       const lldb_private::FileSpec &kernel_dsym_filepath);
134 
135   // Given a dsym_bundle argument ('.../foo.dSYM'), return a FileSpec
136   // with the binary inside it ('.../foo.dSYM/Contents/Resources/DWARF/foo').
137   // A dSYM bundle may have multiple DWARF binaries in them, so a vector
138   // of matches is returned.
139   static std::vector<lldb_private::FileSpec>
140   GetDWARFBinaryInDSYMBundle(lldb_private::FileSpec dsym_bundle);
141 
142   lldb_private::Status
143   GetSharedModuleKext(const lldb_private::ModuleSpec &module_spec,
144                       lldb_private::Process *process, lldb::ModuleSP &module_sp,
145                       const lldb_private::FileSpecList *module_search_paths_ptr,
146                       llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
147                       bool *did_create_ptr);
148 
149   lldb_private::Status GetSharedModuleKernel(
150       const lldb_private::ModuleSpec &module_spec,
151       lldb_private::Process *process, lldb::ModuleSP &module_sp,
152       const lldb_private::FileSpecList *module_search_paths_ptr,
153       llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr);
154 
155   lldb_private::Status
156   ExamineKextForMatchingUUID(const lldb_private::FileSpec &kext_bundle_path,
157                              const lldb_private::UUID &uuid,
158                              const lldb_private::ArchSpec &arch,
159                              lldb::ModuleSP &exe_module_sp);
160 
161   // Most of the ivars are assembled under FileSystem::EnumerateDirectory calls
162   // where the
163   // function being called for each file/directory must be static.  We'll pass a
164   // this pointer
165   // as a baton and access the ivars directly.  Toss-up whether this should just
166   // be a struct
167   // at this point.
168 
169 public:
170   BundleIDToKextMap m_name_to_kext_path_map_with_dsyms;    // multimap of
171                                                            // CFBundleID to
172                                                            // FileSpec on local
173                                                            // filesystem, kexts
174                                                            // with dSYMs next to
175                                                            // them
176   BundleIDToKextMap m_name_to_kext_path_map_without_dsyms; // multimap of
177                                                            // CFBundleID to
178                                                            // FileSpec on local
179                                                            // filesystem, kexts
180                                                            // without dSYMs next
181                                                            // to them
182   DirectoriesSearchedCollection
183       m_search_directories; // list of directories we search for kexts/kernels
184   DirectoriesSearchedCollection
185       m_search_directories_no_recursing; // list of directories we search for
186                                          // kexts/kernels, no recursion
187   KernelBinaryCollection m_kernel_binaries_with_dsyms; // list of kernel
188                                                        // binaries we found on
189                                                        // local filesystem,
190                                                        // without dSYMs next to
191                                                        // them
192   KernelBinaryCollection m_kernel_binaries_without_dsyms; // list of kernel
193                                                           // binaries we found
194                                                           // on local
195                                                           // filesystem, with
196                                                           // dSYMs next to them
197   KernelBinaryCollection m_kernel_dsyms_no_binaries;      // list of kernel
198                                                           // dsyms with no
199                                                           // binaries next to
200                                                           // them
201   KernelBinaryCollection m_kernel_dsyms_yaas;             // list of kernel
202                                                           // .dSYM.yaa files
203 
204   lldb_private::LazyBool m_ios_debug_session;
205 
206   PlatformDarwinKernel(const PlatformDarwinKernel &) = delete;
207   const PlatformDarwinKernel &operator=(const PlatformDarwinKernel &) = delete;
208 };
209 
210 #else // __APPLE__
211 
212 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
213 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name
214 // in
215 // here to avoid issues. We are tracking an internal bug to resolve this issue
216 // by
217 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to
218 // make
219 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently
220 // not
221 // compiled on other platforms due to the use of the Mac-specific
222 // source/Host/macosx/cfcpp utilities.
223 
224 class PlatformDarwinKernel {
225 public:
226   static lldb_private::ConstString GetPluginNameStatic();
227 };
228 
229 #endif // __APPLE__
230 
231 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H
232