225 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H10#define LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H11 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 26namespace lldb_private {27class ArchSpec;28class Debugger;29class FileSpecList;30class ModuleSpec;31class Process;32class Stream;33 34#if defined(__APPLE__) // This Plugin uses the Mac-specific35 // source/Host/macosx/cfcpp utilities36 37class PlatformDarwinKernel : public PlatformDarwin {38public: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 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 55 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }56 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 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,64 bool *did_create_ptr) override;65 66 std::vector<ArchSpec>67 GetSupportedArchitectures(const ArchSpec &process_host_arch) override;68 69 bool SupportsModules() override { return false; }70 71 void CalculateTrapHandlerSymbolNames() override;72 73protected:74 // Map from kext bundle ID ("com.apple.filesystems.exfat") to FileSpec for75 // the kext bundle on the host76 // ("/System/Library/Extensions/exfat.kext/Contents/Info.plist").77 typedef std::multimap<ConstString, FileSpec> BundleIDToKextMap;78 typedef BundleIDToKextMap::iterator BundleIDToKextIterator;79 80 typedef std::vector<FileSpec> KernelBinaryCollection;81 82 // Array of directories that were searched for kext bundles (used only for83 // reporting to user).84 typedef std::vector<FileSpec> DirectoriesSearchedCollection;85 typedef DirectoriesSearchedCollection::iterator DirectoriesSearchedIterator;86 87 // Populate m_search_directories and m_search_directories_no_recursing88 // vectors of directories.89 void CollectKextAndKernelDirectories();90 91 void GetUserSpecifiedDirectoriesToSearch();92 93 static void AddRootSubdirsToSearchPaths(PlatformDarwinKernel *thisp,94 const std::string &dir);95 96 void AddSDKSubdirsToSearchPaths(const std::string &dir);97 98 static FileSystem::EnumerateDirectoryResult99 FindKDKandSDKDirectoriesInDirectory(void *baton, llvm::sys::fs::file_type ft,100 llvm::StringRef path);101 102 void SearchForKextsAndKernelsRecursively();103 104 static FileSystem::EnumerateDirectoryResult105 GetKernelsAndKextsInDirectoryWithRecursion(void *baton,106 llvm::sys::fs::file_type ft,107 llvm::StringRef path);108 109 static FileSystem::EnumerateDirectoryResult110 GetKernelsAndKextsInDirectoryNoRecursion(void *baton,111 llvm::sys::fs::file_type ft,112 llvm::StringRef path);113 114 static FileSystem::EnumerateDirectoryResult115 GetKernelsAndKextsInDirectoryHelper(void *baton, llvm::sys::fs::file_type ft,116 llvm::StringRef path, bool recurse);117 118 static std::vector<FileSpec>119 SearchForExecutablesRecursively(const std::string &dir);120 121 static void AddKextToMap(PlatformDarwinKernel *thisp,122 const FileSpec &file_spec);123 124 // Returns true if there is a .dSYM bundle next to the kext, or next to the125 // binary inside the kext.126 static bool KextHasdSYMSibling(const FileSpec &kext_bundle_filepath);127 128 // Returns true if there is a .dSYM bundle next to the kernel129 static bool KernelHasdSYMSibling(const FileSpec &kernel_filepath);130 131 // Returns true if there is a .dSYM bundle with NO kernel binary next to it132 static bool133 KerneldSYMHasNoSiblingBinary(const FileSpec &kernel_dsym_filepath);134 135 // Given a dsym_bundle argument ('.../foo.dSYM'), return a FileSpec136 // with the binary inside it ('.../foo.dSYM/Contents/Resources/DWARF/foo').137 // A dSYM bundle may have multiple DWARF binaries in them, so a vector138 // of matches is returned.139 static std::vector<FileSpec>140 GetDWARFBinaryInDSYMBundle(const FileSpec &dsym_bundle);141 142 Status GetSharedModuleKext(const ModuleSpec &module_spec, Process *process,143 lldb::ModuleSP &module_sp,144 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,145 bool *did_create_ptr);146 147 Status148 GetSharedModuleKernel(const ModuleSpec &module_spec, Process *process,149 lldb::ModuleSP &module_sp,150 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,151 bool *did_create_ptr);152 153 Status ExamineKextForMatchingUUID(const FileSpec &kext_bundle_path,154 const UUID &uuid, const ArchSpec &arch,155 lldb::ModuleSP &exe_module_sp);156 157 bool LoadPlatformBinaryAndSetup(Process *process, lldb::addr_t addr,158 bool notify) override;159 160 void UpdateKextandKernelsLocalScan();161 162 // Most of the ivars are assembled under FileSystem::EnumerateDirectory calls163 // where the function being called for each file/directory must be static.164 // We'll pass a this pointer as a baton and access the ivars directly.165 // Toss-up whether this should just be a struct at this point.166 167public:168 /// Multimap of CFBundleID to FileSpec on local filesystem, kexts with dSYMs169 /// next to them.170 BundleIDToKextMap m_name_to_kext_path_map_with_dsyms;171 172 /// Multimap of CFBundleID to FileSpec on local filesystem, kexts without173 /// dSYMs next to them.174 BundleIDToKextMap m_name_to_kext_path_map_without_dsyms;175 176 /// List of directories we search for kexts/kernels.177 DirectoriesSearchedCollection m_search_directories;178 179 /// List of directories we search for kexts/kernels, no recursion.180 DirectoriesSearchedCollection m_search_directories_no_recursing;181 182 /// List of kernel binaries we found on local filesystem, without dSYMs next183 /// to them.184 KernelBinaryCollection m_kernel_binaries_with_dsyms;185 186 /// List of kernel binaries we found on local filesystem, with dSYMs next to187 /// them.188 KernelBinaryCollection m_kernel_binaries_without_dsyms;189 190 /// List of kernel dsyms with no binaries next to them.191 KernelBinaryCollection m_kernel_dsyms_no_binaries;192 193 /// List of kernel .dSYM.yaa files.194 KernelBinaryCollection m_kernel_dsyms_yaas;195 196 LazyBool m_ios_debug_session;197 198 std::once_flag m_kext_scan_flag;199 200 PlatformDarwinKernel(const PlatformDarwinKernel &) = delete;201 const PlatformDarwinKernel &operator=(const PlatformDarwinKernel &) = delete;202};203 204#else // __APPLE__205 206// Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies207// on PlatformDarwinKernel for the plug-in name, we compile just the plug-in208// name in here to avoid issues. We are tracking an internal bug to resolve209// this issue by either not compiling in DynamicLoaderDarwinKernel for210// non-apple builds, or to make PlatformDarwinKernel build on all systems.211//212// PlatformDarwinKernel is currently not compiled on other platforms due to the213// use of the Mac-specific source/Host/macosx/cfcpp utilities.214 215class PlatformDarwinKernel {216public:217 static llvm::StringRef GetPluginNameStatic() { return "darwin-kernel"; }218};219 220#endif // __APPLE__221 222} // namespace lldb_private223 224#endif // LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_PLATFORMDARWINKERNEL_H225