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