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
45   lldb_private::ConstString GetPluginName() override {
46     return GetPluginNameStatic();
47   }
48 
49   uint32_t GetPluginVersion() override { return 1; }
50 
51   // lldb_private::Platform functions
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 
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 &kext_bundle_filepath);
130 
131   lldb_private::Status
132   ExamineKextForMatchingUUID(const lldb_private::FileSpec &kext_bundle_path,
133                              const lldb_private::UUID &uuid,
134                              const lldb_private::ArchSpec &arch,
135                              lldb::ModuleSP &exe_module_sp);
136 
137   // Most of the ivars are assembled under FileSystem::EnumerateDirectory calls
138   // where the
139   // function being called for each file/directory must be static.  We'll pass a
140   // this pointer
141   // as a baton and access the ivars directly.  Toss-up whether this should just
142   // be a struct
143   // at this point.
144 
145 public:
146   BundleIDToKextMap m_name_to_kext_path_map_with_dsyms;    // multimap of
147                                                            // CFBundleID to
148                                                            // FileSpec on local
149                                                            // filesystem, kexts
150                                                            // with dSYMs next to
151                                                            // them
152   BundleIDToKextMap m_name_to_kext_path_map_without_dsyms; // multimap of
153                                                            // CFBundleID to
154                                                            // FileSpec on local
155                                                            // filesystem, kexts
156                                                            // without dSYMs next
157                                                            // to them
158   DirectoriesSearchedCollection
159       m_search_directories; // list of directories we search for kexts/kernels
160   DirectoriesSearchedCollection
161       m_search_directories_no_recursing; // list of directories we search for
162                                          // kexts/kernels, no recursion
163   KernelBinaryCollection m_kernel_binaries_with_dsyms; // list of kernel
164                                                        // binaries we found on
165                                                        // local filesystem,
166                                                        // without dSYMs next to
167                                                        // them
168   KernelBinaryCollection m_kernel_binaries_without_dsyms; // list of kernel
169                                                           // binaries we found
170                                                           // on local
171                                                           // filesystem, with
172                                                           // dSYMs next to them
173   lldb_private::LazyBool m_ios_debug_session;
174 
175   PlatformDarwinKernel(const PlatformDarwinKernel &) = delete;
176   const PlatformDarwinKernel &operator=(const PlatformDarwinKernel &) = delete;
177 };
178 
179 #else // __APPLE__
180 
181 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
182 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name
183 // in
184 // here to avoid issues. We are tracking an internal bug to resolve this issue
185 // by
186 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to
187 // make
188 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently
189 // not
190 // compiled on other platforms due to the use of the Mac-specific
191 // source/Host/macosx/cfcpp utilities.
192 
193 class PlatformDarwinKernel {
194 public:
195   static lldb_private::ConstString GetPluginNameStatic();
196 };
197 
198 #endif // __APPLE__
199 
200 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H
201