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