/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- PlatformDarwinKernel.cpp ------------------------------------------===// |
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 | | #include "PlatformDarwinKernel.h" |
10 | | |
11 | | #if defined(__APPLE__) // This Plugin uses the Mac-specific |
12 | | // source/Host/macosx/cfcpp utilities |
13 | | |
14 | | #include "lldb/Breakpoint/BreakpointLocation.h" |
15 | | #include "lldb/Core/Debugger.h" |
16 | | #include "lldb/Core/Module.h" |
17 | | #include "lldb/Core/ModuleList.h" |
18 | | #include "lldb/Core/ModuleSpec.h" |
19 | | #include "lldb/Core/PluginManager.h" |
20 | | #include "lldb/Host/Host.h" |
21 | | #include "lldb/Host/HostInfo.h" |
22 | | #include "lldb/Interpreter/OptionValueFileSpecList.h" |
23 | | #include "lldb/Interpreter/OptionValueProperties.h" |
24 | | #include "lldb/Interpreter/Property.h" |
25 | | #include "lldb/Symbol/ObjectFile.h" |
26 | | #include "lldb/Target/Platform.h" |
27 | | #include "lldb/Target/Process.h" |
28 | | #include "lldb/Target/Target.h" |
29 | | #include "lldb/Utility/ArchSpec.h" |
30 | | #include "lldb/Utility/DataBufferHeap.h" |
31 | | #include "lldb/Utility/FileSpec.h" |
32 | | #include "lldb/Utility/LLDBLog.h" |
33 | | #include "lldb/Utility/Log.h" |
34 | | #include "lldb/Utility/Status.h" |
35 | | #include "lldb/Utility/StreamString.h" |
36 | | |
37 | | #include "llvm/Support/FileSystem.h" |
38 | | |
39 | | #include <CoreFoundation/CoreFoundation.h> |
40 | | |
41 | | #include <memory> |
42 | | |
43 | | #include "Host/macosx/cfcpp/CFCBundle.h" |
44 | | #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" |
45 | | #include "Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.h" |
46 | | |
47 | | using namespace lldb; |
48 | | using namespace lldb_private; |
49 | | |
50 | | // Static Variables |
51 | | static uint32_t g_initialize_count = 0; |
52 | | |
53 | | // Static Functions |
54 | 3.94k | void PlatformDarwinKernel::Initialize() { |
55 | 3.94k | PlatformDarwin::Initialize(); |
56 | | |
57 | 3.94k | if (g_initialize_count++ == 0) { |
58 | 3.94k | PluginManager::RegisterPlugin(PlatformDarwinKernel::GetPluginNameStatic(), |
59 | 3.94k | PlatformDarwinKernel::GetDescriptionStatic(), |
60 | 3.94k | PlatformDarwinKernel::CreateInstance, |
61 | 3.94k | PlatformDarwinKernel::DebuggerInitialize); |
62 | 3.94k | } |
63 | 3.94k | } |
64 | | |
65 | 3.93k | void PlatformDarwinKernel::Terminate() { |
66 | 3.93k | if (g_initialize_count > 0) { |
67 | 3.93k | if (--g_initialize_count == 0) { |
68 | 3.93k | PluginManager::UnregisterPlugin(PlatformDarwinKernel::CreateInstance); |
69 | 3.93k | } |
70 | 3.93k | } |
71 | | |
72 | 3.93k | PlatformDarwin::Terminate(); |
73 | 3.93k | } |
74 | | |
75 | | PlatformSP PlatformDarwinKernel::CreateInstance(bool force, |
76 | 207 | const ArchSpec *arch) { |
77 | 207 | Log *log = GetLog(LLDBLog::Platform); |
78 | 207 | if (log) { |
79 | 0 | const char *arch_name; |
80 | 0 | if (arch && arch->GetArchitectureName()) |
81 | 0 | arch_name = arch->GetArchitectureName(); |
82 | 0 | else |
83 | 0 | arch_name = "<null>"; |
84 | |
|
85 | 0 | const char *triple_cstr = |
86 | 0 | arch ? arch->GetTriple().getTriple().c_str() : "<null>"; |
87 | |
|
88 | 0 | LLDB_LOGF(log, "PlatformDarwinKernel::%s(force=%s, arch={%s,%s})", |
89 | 0 | __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); |
90 | 0 | } |
91 | | |
92 | | // This is a special plugin that we don't want to activate just based on an |
93 | | // ArchSpec for normal userland debugging. It is only useful in kernel debug |
94 | | // sessions and the DynamicLoaderDarwinPlugin (or a user doing 'platform |
95 | | // select') will force the creation of this Platform plugin. |
96 | 207 | if (!force) { |
97 | 203 | LLDB_LOGF(log, |
98 | 203 | "PlatformDarwinKernel::%s() aborting creation of platform " |
99 | 203 | "because force == false", |
100 | 203 | __FUNCTION__); |
101 | 203 | return PlatformSP(); |
102 | 203 | } |
103 | | |
104 | 4 | bool create = force; |
105 | 4 | LazyBool is_ios_debug_session = eLazyBoolCalculate; |
106 | | |
107 | 4 | if (!create && arch0 && arch->IsValid()0 ) { |
108 | 0 | const llvm::Triple &triple = arch->GetTriple(); |
109 | 0 | switch (triple.getVendor()) { |
110 | 0 | case llvm::Triple::Apple: |
111 | 0 | create = true; |
112 | 0 | break; |
113 | | |
114 | | // Only accept "unknown" for vendor if the host is Apple and it "unknown" |
115 | | // wasn't specified (it was just returned because it was NOT specified) |
116 | 0 | case llvm::Triple::UnknownVendor: |
117 | 0 | create = !arch->TripleVendorWasSpecified(); |
118 | 0 | break; |
119 | 0 | default: |
120 | 0 | break; |
121 | 0 | } |
122 | | |
123 | 0 | if (create) { |
124 | 0 | switch (triple.getOS()) { |
125 | 0 | case llvm::Triple::Darwin: |
126 | 0 | case llvm::Triple::MacOSX: |
127 | 0 | case llvm::Triple::IOS: |
128 | 0 | case llvm::Triple::WatchOS: |
129 | 0 | case llvm::Triple::TvOS: |
130 | | // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: |
131 | 0 | break; |
132 | | // Only accept "vendor" for vendor if the host is Apple and it "unknown" |
133 | | // wasn't specified (it was just returned because it was NOT specified) |
134 | 0 | case llvm::Triple::UnknownOS: |
135 | 0 | create = !arch->TripleOSWasSpecified(); |
136 | 0 | break; |
137 | 0 | default: |
138 | 0 | create = false; |
139 | 0 | break; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | 4 | if (arch && arch->IsValid()) { |
144 | 0 | switch (arch->GetMachine()) { |
145 | 0 | case llvm::Triple::x86: |
146 | 0 | case llvm::Triple::x86_64: |
147 | 0 | case llvm::Triple::ppc: |
148 | 0 | case llvm::Triple::ppc64: |
149 | 0 | is_ios_debug_session = eLazyBoolNo; |
150 | 0 | break; |
151 | 0 | case llvm::Triple::arm: |
152 | 0 | case llvm::Triple::aarch64: |
153 | 0 | case llvm::Triple::thumb: |
154 | 0 | is_ios_debug_session = eLazyBoolYes; |
155 | 0 | break; |
156 | 0 | default: |
157 | 0 | is_ios_debug_session = eLazyBoolCalculate; |
158 | 0 | break; |
159 | 0 | } |
160 | 0 | } |
161 | 4 | if (create) { |
162 | 4 | LLDB_LOGF(log, "PlatformDarwinKernel::%s() creating platform", |
163 | 4 | __FUNCTION__); |
164 | | |
165 | 4 | return PlatformSP(new PlatformDarwinKernel(is_ios_debug_session)); |
166 | 4 | } |
167 | | |
168 | 0 | LLDB_LOGF(log, "PlatformDarwinKernel::%s() aborting creation of platform", |
169 | 0 | __FUNCTION__); |
170 | |
|
171 | 0 | return PlatformSP(); |
172 | 4 | } |
173 | | |
174 | 3.94k | llvm::StringRef PlatformDarwinKernel::GetDescriptionStatic() { |
175 | 3.94k | return "Darwin Kernel platform plug-in."; |
176 | 3.94k | } |
177 | | |
178 | | /// Code to handle the PlatformDarwinKernel settings |
179 | | |
180 | | #define LLDB_PROPERTIES_platformdarwinkernel |
181 | | #include "PlatformMacOSXProperties.inc" |
182 | | |
183 | | enum { |
184 | | #define LLDB_PROPERTIES_platformdarwinkernel |
185 | | #include "PlatformMacOSXPropertiesEnum.inc" |
186 | | }; |
187 | | |
188 | | class PlatformDarwinKernelProperties : public Properties { |
189 | | public: |
190 | 9.93k | static llvm::StringRef GetSettingName() { |
191 | 9.93k | static constexpr llvm::StringLiteral g_setting_name("darwin-kernel"); |
192 | 9.93k | return g_setting_name; |
193 | 9.93k | } |
194 | | |
195 | 3.88k | PlatformDarwinKernelProperties() : Properties() { |
196 | 3.88k | m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName()); |
197 | 3.88k | m_collection_sp->Initialize(g_platformdarwinkernel_properties); |
198 | 3.88k | } |
199 | | |
200 | 3.88k | ~PlatformDarwinKernelProperties() override = default; |
201 | | |
202 | 0 | FileSpecList GetKextDirectories() const { |
203 | 0 | const uint32_t idx = ePropertyKextDirectories; |
204 | 0 | return GetPropertyAtIndexAs<FileSpecList>(idx, {}); |
205 | 0 | } |
206 | | }; |
207 | | |
208 | 3.88k | static PlatformDarwinKernelProperties &GetGlobalProperties() { |
209 | 3.88k | static PlatformDarwinKernelProperties g_settings; |
210 | 3.88k | return g_settings; |
211 | 3.88k | } |
212 | | |
213 | | void PlatformDarwinKernel::DebuggerInitialize( |
214 | 6.05k | lldb_private::Debugger &debugger) { |
215 | 6.05k | if (!PluginManager::GetSettingForPlatformPlugin( |
216 | 6.05k | debugger, PlatformDarwinKernelProperties::GetSettingName())) { |
217 | 3.88k | const bool is_global_setting = true; |
218 | 3.88k | PluginManager::CreateSettingForPlatformPlugin( |
219 | 3.88k | debugger, GetGlobalProperties().GetValueProperties(), |
220 | 3.88k | "Properties for the PlatformDarwinKernel plug-in.", is_global_setting); |
221 | 3.88k | } |
222 | 6.05k | } |
223 | | |
224 | | /// Default Constructor |
225 | | PlatformDarwinKernel::PlatformDarwinKernel( |
226 | | lldb_private::LazyBool is_ios_debug_session) |
227 | 4 | : PlatformDarwin(false), // This is a remote platform |
228 | 4 | m_name_to_kext_path_map_with_dsyms(), |
229 | 4 | m_name_to_kext_path_map_without_dsyms(), m_search_directories(), |
230 | 4 | m_search_directories_no_recursing(), m_kernel_binaries_with_dsyms(), |
231 | 4 | m_kernel_binaries_without_dsyms(), m_kernel_dsyms_no_binaries(), |
232 | 4 | m_kernel_dsyms_yaas(), m_ios_debug_session(is_ios_debug_session), |
233 | 4 | m_kext_scan_flag() {} |
234 | | |
235 | | /// Destructor. |
236 | | /// |
237 | | /// The destructor is virtual since this class is designed to be |
238 | | /// inherited from by the plug-in instance. |
239 | 4 | PlatformDarwinKernel::~PlatformDarwinKernel() = default; |
240 | | |
241 | 0 | void PlatformDarwinKernel::GetStatus(Stream &strm) { |
242 | 0 | UpdateKextandKernelsLocalScan(); |
243 | 0 | Platform::GetStatus(strm); |
244 | 0 | strm.Printf(" Debug session type: "); |
245 | 0 | if (m_ios_debug_session == eLazyBoolYes) |
246 | 0 | strm.Printf("iOS kernel debugging\n"); |
247 | 0 | else if (m_ios_debug_session == eLazyBoolNo) |
248 | 0 | strm.Printf("Mac OS X kernel debugging\n"); |
249 | 0 | else |
250 | 0 | strm.Printf("unknown kernel debugging\n"); |
251 | |
|
252 | 0 | strm.Printf("Directories searched recursively:\n"); |
253 | 0 | const uint32_t num_kext_dirs = m_search_directories.size(); |
254 | 0 | for (uint32_t i = 0; i < num_kext_dirs; ++i) { |
255 | 0 | strm.Printf("[%d] %s\n", i, m_search_directories[i].GetPath().c_str()); |
256 | 0 | } |
257 | |
|
258 | 0 | strm.Printf("Directories not searched recursively:\n"); |
259 | 0 | const uint32_t num_kext_dirs_no_recursion = |
260 | 0 | m_search_directories_no_recursing.size(); |
261 | 0 | for (uint32_t i = 0; i < num_kext_dirs_no_recursion; i++) { |
262 | 0 | strm.Printf("[%d] %s\n", i, |
263 | 0 | m_search_directories_no_recursing[i].GetPath().c_str()); |
264 | 0 | } |
265 | |
|
266 | 0 | strm.Printf(" Number of kexts with dSYMs indexed: %d\n", |
267 | 0 | (int)m_name_to_kext_path_map_with_dsyms.size()); |
268 | 0 | strm.Printf(" Number of kexts without dSYMs indexed: %d\n", |
269 | 0 | (int)m_name_to_kext_path_map_without_dsyms.size()); |
270 | 0 | strm.Printf(" Number of Kernel binaries with dSYMs indexed: %d\n", |
271 | 0 | (int)m_kernel_binaries_with_dsyms.size()); |
272 | 0 | strm.Printf(" Number of Kernel binaries without dSYMs indexed: %d\n", |
273 | 0 | (int)m_kernel_binaries_without_dsyms.size()); |
274 | 0 | strm.Printf(" Number of Kernel dSYMs with no binaries indexed: %d\n", |
275 | 0 | (int)m_kernel_dsyms_no_binaries.size()); |
276 | 0 | strm.Printf(" Number of Kernel dSYM.yaa's indexed: %d\n", |
277 | 0 | (int)m_kernel_dsyms_yaas.size()); |
278 | |
|
279 | 0 | Log *log = GetLog(LLDBLog::Platform); |
280 | 0 | if (log) { |
281 | 0 | LLDB_LOGF(log, "\nkexts with dSYMs"); |
282 | 0 | for (auto pos : m_name_to_kext_path_map_with_dsyms) { |
283 | 0 | LLDB_LOGF(log, "%s", pos.second.GetPath().c_str()); |
284 | 0 | } |
285 | 0 | LLDB_LOGF(log, "\nkexts without dSYMs"); |
286 | |
|
287 | 0 | for (auto pos : m_name_to_kext_path_map_without_dsyms) { |
288 | 0 | LLDB_LOGF(log, "%s", pos.second.GetPath().c_str()); |
289 | 0 | } |
290 | 0 | LLDB_LOGF(log, "\nkernel binaries with dSYMS"); |
291 | 0 | for (auto fs : m_kernel_binaries_with_dsyms) { |
292 | 0 | LLDB_LOGF(log, "%s", fs.GetPath().c_str()); |
293 | 0 | } |
294 | 0 | LLDB_LOGF(log, "\nkernel binaries without dSYMS"); |
295 | 0 | for (auto fs : m_kernel_binaries_without_dsyms) { |
296 | 0 | LLDB_LOGF(log, "%s", fs.GetPath().c_str()); |
297 | 0 | } |
298 | 0 | LLDB_LOGF(log, "\nkernel dSYMS with no binaries"); |
299 | 0 | for (auto fs : m_kernel_dsyms_no_binaries) { |
300 | 0 | LLDB_LOGF(log, "%s", fs.GetPath().c_str()); |
301 | 0 | } |
302 | 0 | LLDB_LOGF(log, "\nkernels .dSYM.yaa's"); |
303 | 0 | for (auto fs : m_kernel_dsyms_yaas) { |
304 | 0 | LLDB_LOGF(log, "%s", fs.GetPath().c_str()); |
305 | 0 | } |
306 | 0 | LLDB_LOGF(log, "\n"); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | // Populate the m_search_directories vector with directories we should search |
311 | | // for kernel & kext binaries. |
312 | | |
313 | 0 | void PlatformDarwinKernel::CollectKextAndKernelDirectories() { |
314 | | // Differentiate between "ios debug session" and "mac debug session" so we |
315 | | // don't index kext bundles that won't be used in this debug session. If |
316 | | // this is an ios kext debug session, looking in /System/Library/Extensions |
317 | | // is a waste of stat()s, for example. |
318 | | |
319 | | // DeveloperDirectory is something like |
320 | | // "/Applications/Xcode.app/Contents/Developer" |
321 | 0 | std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath(); |
322 | 0 | if (developer_dir.empty()) |
323 | 0 | developer_dir = "/Applications/Xcode.app/Contents/Developer"; |
324 | |
|
325 | 0 | if (m_ios_debug_session != eLazyBoolNo) { |
326 | 0 | AddSDKSubdirsToSearchPaths(developer_dir + |
327 | 0 | "/Platforms/iPhoneOS.platform/Developer/SDKs"); |
328 | 0 | AddSDKSubdirsToSearchPaths(developer_dir + |
329 | 0 | "/Platforms/AppleTVOS.platform/Developer/SDKs"); |
330 | 0 | AddSDKSubdirsToSearchPaths(developer_dir + |
331 | 0 | "/Platforms/WatchOS.platform/Developer/SDKs"); |
332 | 0 | AddSDKSubdirsToSearchPaths(developer_dir + |
333 | 0 | "/Platforms/BridgeOS.platform/Developer/SDKs"); |
334 | 0 | } |
335 | 0 | if (m_ios_debug_session != eLazyBoolYes) { |
336 | 0 | AddSDKSubdirsToSearchPaths(developer_dir + |
337 | 0 | "/Platforms/MacOSX.platform/Developer/SDKs"); |
338 | 0 | } |
339 | |
|
340 | 0 | AddSDKSubdirsToSearchPaths("/Volumes/KernelDebugKit"); |
341 | 0 | AddSDKSubdirsToSearchPaths("/AppleInternal/Developer/KDKs"); |
342 | | // The KDKs distributed from Apple installed on external developer systems |
343 | | // may be in directories like /Library/Developer/KDKs/KDK_10.10_14A298i.kdk |
344 | 0 | AddSDKSubdirsToSearchPaths("/Library/Developer/KDKs"); |
345 | |
|
346 | 0 | if (m_ios_debug_session != eLazyBoolNo) { |
347 | 0 | } |
348 | 0 | if (m_ios_debug_session != eLazyBoolYes) { |
349 | 0 | AddRootSubdirsToSearchPaths(this, "/"); |
350 | 0 | } |
351 | |
|
352 | 0 | GetUserSpecifiedDirectoriesToSearch(); |
353 | | |
354 | | // Add simple directory /Applications/Xcode.app/Contents/Developer/../Symbols |
355 | 0 | FileSpec possible_dir(developer_dir + "/../Symbols"); |
356 | 0 | FileSystem::Instance().Resolve(possible_dir); |
357 | 0 | if (FileSystem::Instance().IsDirectory(possible_dir)) |
358 | 0 | m_search_directories.push_back(possible_dir); |
359 | | |
360 | | // Add simple directory of the current working directory |
361 | 0 | FileSpec cwd("."); |
362 | 0 | FileSystem::Instance().Resolve(cwd); |
363 | 0 | m_search_directories_no_recursing.push_back(cwd); |
364 | 0 | } |
365 | | |
366 | 0 | void PlatformDarwinKernel::GetUserSpecifiedDirectoriesToSearch() { |
367 | 0 | FileSpecList user_dirs(GetGlobalProperties().GetKextDirectories()); |
368 | 0 | std::vector<FileSpec> possible_sdk_dirs; |
369 | |
|
370 | 0 | const uint32_t user_dirs_count = user_dirs.GetSize(); |
371 | 0 | for (uint32_t i = 0; i < user_dirs_count; i++) { |
372 | 0 | FileSpec dir = user_dirs.GetFileSpecAtIndex(i); |
373 | 0 | FileSystem::Instance().Resolve(dir); |
374 | 0 | if (FileSystem::Instance().IsDirectory(dir)) { |
375 | 0 | m_search_directories.push_back(dir); |
376 | 0 | } |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | | void PlatformDarwinKernel::AddRootSubdirsToSearchPaths( |
381 | 0 | PlatformDarwinKernel *thisp, const std::string &dir) { |
382 | 0 | const char *subdirs[] = { |
383 | 0 | "/System/Library/Extensions", "/Library/Extensions", |
384 | 0 | "/System/Library/Kernels", |
385 | 0 | "/System/Library/Extensions/KDK", // this one probably only exist in |
386 | | // /AppleInternal/Developer/KDKs/*.kdk/... |
387 | 0 | nullptr}; |
388 | 0 | for (int i = 0; subdirs[i] != nullptr; i++) { |
389 | 0 | FileSpec testdir(dir + subdirs[i]); |
390 | 0 | FileSystem::Instance().Resolve(testdir); |
391 | 0 | if (FileSystem::Instance().IsDirectory(testdir)) |
392 | 0 | thisp->m_search_directories.push_back(testdir); |
393 | 0 | } |
394 | | |
395 | | // Look for kernel binaries in the top level directory, without any recursion |
396 | 0 | thisp->m_search_directories_no_recursing.push_back(FileSpec(dir + "/")); |
397 | 0 | } |
398 | | |
399 | | // Given a directory path dir, look for any subdirs named *.kdk and *.sdk |
400 | 0 | void PlatformDarwinKernel::AddSDKSubdirsToSearchPaths(const std::string &dir) { |
401 | | // Look for *.kdk and *.sdk in dir |
402 | 0 | const bool find_directories = true; |
403 | 0 | const bool find_files = false; |
404 | 0 | const bool find_other = false; |
405 | 0 | FileSystem::Instance().EnumerateDirectory( |
406 | 0 | dir.c_str(), find_directories, find_files, find_other, |
407 | 0 | FindKDKandSDKDirectoriesInDirectory, this); |
408 | 0 | } |
409 | | |
410 | | // Helper function to find *.sdk and *.kdk directories in a given directory. |
411 | | FileSystem::EnumerateDirectoryResult |
412 | | PlatformDarwinKernel::FindKDKandSDKDirectoriesInDirectory( |
413 | 0 | void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path) { |
414 | 0 | static constexpr llvm::StringLiteral g_sdk_suffix = ".sdk"; |
415 | 0 | static constexpr llvm::StringLiteral g_kdk_suffix = ".kdk"; |
416 | |
|
417 | 0 | PlatformDarwinKernel *thisp = (PlatformDarwinKernel *)baton; |
418 | 0 | const FileSpec file_spec(path); |
419 | 0 | if (ft == llvm::sys::fs::file_type::directory_file && |
420 | 0 | (file_spec.GetFileNameExtension() == g_sdk_suffix || |
421 | 0 | file_spec.GetFileNameExtension() == g_kdk_suffix)) { |
422 | 0 | AddRootSubdirsToSearchPaths(thisp, file_spec.GetPath()); |
423 | 0 | } |
424 | 0 | return FileSystem::eEnumerateDirectoryResultNext; |
425 | 0 | } |
426 | | |
427 | | // Recursively search trough m_search_directories looking for kext and kernel |
428 | | // binaries, adding files found to the appropriate lists. |
429 | 0 | void PlatformDarwinKernel::SearchForKextsAndKernelsRecursively() { |
430 | 0 | const uint32_t num_dirs = m_search_directories.size(); |
431 | 0 | for (uint32_t i = 0; i < num_dirs; i++) { |
432 | 0 | const FileSpec &dir = m_search_directories[i]; |
433 | 0 | const bool find_directories = true; |
434 | 0 | const bool find_files = true; |
435 | 0 | const bool find_other = true; // I think eFileTypeSymbolicLink are "other"s. |
436 | 0 | FileSystem::Instance().EnumerateDirectory( |
437 | 0 | dir.GetPath().c_str(), find_directories, find_files, find_other, |
438 | 0 | GetKernelsAndKextsInDirectoryWithRecursion, this); |
439 | 0 | } |
440 | 0 | const uint32_t num_dirs_no_recurse = m_search_directories_no_recursing.size(); |
441 | 0 | for (uint32_t i = 0; i < num_dirs_no_recurse; i++) { |
442 | 0 | const FileSpec &dir = m_search_directories_no_recursing[i]; |
443 | 0 | const bool find_directories = true; |
444 | 0 | const bool find_files = true; |
445 | 0 | const bool find_other = true; // I think eFileTypeSymbolicLink are "other"s. |
446 | 0 | FileSystem::Instance().EnumerateDirectory( |
447 | 0 | dir.GetPath().c_str(), find_directories, find_files, find_other, |
448 | 0 | GetKernelsAndKextsInDirectoryNoRecursion, this); |
449 | 0 | } |
450 | 0 | } |
451 | | |
452 | | // We're only doing a filename match here. We won't try opening the file to |
453 | | // see if it's really a kernel or not until we need to find a kernel of a given |
454 | | // UUID. There's no cheap way to find the UUID of a file (or if it's a Mach-O |
455 | | // binary at all) without creating a whole Module for the file and throwing it |
456 | | // away if it's not wanted. |
457 | | // |
458 | | // Recurse into any subdirectories found. |
459 | | |
460 | | FileSystem::EnumerateDirectoryResult |
461 | | PlatformDarwinKernel::GetKernelsAndKextsInDirectoryWithRecursion( |
462 | 0 | void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path) { |
463 | 0 | return GetKernelsAndKextsInDirectoryHelper(baton, ft, path, true); |
464 | 0 | } |
465 | | |
466 | | FileSystem::EnumerateDirectoryResult |
467 | | PlatformDarwinKernel::GetKernelsAndKextsInDirectoryNoRecursion( |
468 | 0 | void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path) { |
469 | 0 | return GetKernelsAndKextsInDirectoryHelper(baton, ft, path, false); |
470 | 0 | } |
471 | | |
472 | | FileSystem::EnumerateDirectoryResult |
473 | | PlatformDarwinKernel::GetKernelsAndKextsInDirectoryHelper( |
474 | | void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path, |
475 | 0 | bool recurse) { |
476 | 0 | static constexpr llvm::StringLiteral g_kext_suffix = ".kext"; |
477 | 0 | static constexpr llvm::StringLiteral g_dsym_suffix = ".dSYM"; |
478 | |
|
479 | 0 | const FileSpec file_spec(path); |
480 | 0 | llvm::StringRef file_spec_extension = file_spec.GetFileNameExtension(); |
481 | |
|
482 | 0 | Log *log = GetLog(LLDBLog::Platform); |
483 | |
|
484 | 0 | LLDB_LOGV(log, "PlatformDarwinKernel examining '{0}'", file_spec); |
485 | |
|
486 | 0 | PlatformDarwinKernel *thisp = (PlatformDarwinKernel *)baton; |
487 | |
|
488 | 0 | llvm::StringRef filename = file_spec.GetFilename().GetStringRef(); |
489 | 0 | bool is_kernel_filename = |
490 | 0 | filename.startswith("kernel") || filename.startswith("mach"); |
491 | 0 | bool is_dsym_yaa = filename.endswith(".dSYM.yaa"); |
492 | |
|
493 | 0 | if (ft == llvm::sys::fs::file_type::regular_file || |
494 | 0 | ft == llvm::sys::fs::file_type::symlink_file) { |
495 | 0 | if (is_kernel_filename) { |
496 | 0 | if (file_spec_extension != g_dsym_suffix && !is_dsym_yaa) { |
497 | 0 | if (KernelHasdSYMSibling(file_spec)) { |
498 | 0 | LLDB_LOGF(log, |
499 | 0 | "PlatformDarwinKernel registering kernel binary '%s' with " |
500 | 0 | "dSYM sibling", |
501 | 0 | file_spec.GetPath().c_str()); |
502 | 0 | thisp->m_kernel_binaries_with_dsyms.push_back(file_spec); |
503 | 0 | } else { |
504 | 0 | LLDB_LOGF( |
505 | 0 | log, |
506 | 0 | "PlatformDarwinKernel registering kernel binary '%s', no dSYM", |
507 | 0 | file_spec.GetPath().c_str()); |
508 | 0 | thisp->m_kernel_binaries_without_dsyms.push_back(file_spec); |
509 | 0 | } |
510 | 0 | } |
511 | 0 | if (is_dsym_yaa) { |
512 | 0 | LLDB_LOGF(log, "PlatformDarwinKernel registering kernel .dSYM.yaa '%s'", |
513 | 0 | file_spec.GetPath().c_str()); |
514 | 0 | thisp->m_kernel_dsyms_yaas.push_back(file_spec); |
515 | 0 | } |
516 | 0 | return FileSystem::eEnumerateDirectoryResultNext; |
517 | 0 | } |
518 | 0 | } else { |
519 | 0 | if (ft == llvm::sys::fs::file_type::directory_file) { |
520 | 0 | if (file_spec_extension == g_kext_suffix) { |
521 | 0 | AddKextToMap(thisp, file_spec); |
522 | | // Look to see if there is a PlugIns subdir with more kexts |
523 | 0 | FileSpec contents_plugins(file_spec.GetPath() + "/Contents/PlugIns"); |
524 | 0 | std::string search_here_too; |
525 | 0 | if (FileSystem::Instance().IsDirectory(contents_plugins)) { |
526 | 0 | search_here_too = contents_plugins.GetPath(); |
527 | 0 | } else { |
528 | 0 | FileSpec plugins(file_spec.GetPath() + "/PlugIns"); |
529 | 0 | if (FileSystem::Instance().IsDirectory(plugins)) { |
530 | 0 | search_here_too = plugins.GetPath(); |
531 | 0 | } |
532 | 0 | } |
533 | |
|
534 | 0 | if (!search_here_too.empty()) { |
535 | 0 | const bool find_directories = true; |
536 | 0 | const bool find_files = false; |
537 | 0 | const bool find_other = true; |
538 | 0 | FileSystem::Instance().EnumerateDirectory( |
539 | 0 | search_here_too.c_str(), find_directories, find_files, find_other, |
540 | 0 | recurse ? GetKernelsAndKextsInDirectoryWithRecursion |
541 | 0 | : GetKernelsAndKextsInDirectoryNoRecursion, |
542 | 0 | baton); |
543 | 0 | } |
544 | 0 | return FileSystem::eEnumerateDirectoryResultNext; |
545 | 0 | } |
546 | | // Do we have a kernel dSYM with no kernel binary? |
547 | 0 | if (is_kernel_filename && file_spec_extension == g_dsym_suffix) { |
548 | 0 | if (KerneldSYMHasNoSiblingBinary(file_spec)) { |
549 | 0 | LLDB_LOGF(log, |
550 | 0 | "PlatformDarwinKernel registering kernel dSYM '%s' with " |
551 | 0 | "no binary sibling", |
552 | 0 | file_spec.GetPath().c_str()); |
553 | 0 | thisp->m_kernel_dsyms_no_binaries.push_back(file_spec); |
554 | 0 | return FileSystem::eEnumerateDirectoryResultNext; |
555 | 0 | } |
556 | 0 | } |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | // Don't recurse into dSYM/kext/bundle directories |
561 | 0 | if (recurse && file_spec_extension != g_dsym_suffix && |
562 | 0 | file_spec_extension != g_kext_suffix) { |
563 | 0 | LLDB_LOGV(log, "PlatformDarwinKernel descending into directory '{0}'", |
564 | 0 | file_spec); |
565 | 0 | return FileSystem::eEnumerateDirectoryResultEnter; |
566 | 0 | } else { |
567 | 0 | return FileSystem::eEnumerateDirectoryResultNext; |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | | void PlatformDarwinKernel::AddKextToMap(PlatformDarwinKernel *thisp, |
572 | 0 | const FileSpec &file_spec) { |
573 | 0 | Log *log = GetLog(LLDBLog::Platform); |
574 | 0 | CFCBundle bundle(file_spec.GetPath().c_str()); |
575 | 0 | CFStringRef bundle_id(bundle.GetIdentifier()); |
576 | 0 | if (bundle_id && CFGetTypeID(bundle_id) == CFStringGetTypeID()) { |
577 | 0 | char bundle_id_buf[PATH_MAX]; |
578 | 0 | if (CFStringGetCString(bundle_id, bundle_id_buf, sizeof(bundle_id_buf), |
579 | 0 | kCFStringEncodingUTF8)) { |
580 | 0 | ConstString bundle_conststr(bundle_id_buf); |
581 | 0 | if (KextHasdSYMSibling(file_spec)) |
582 | 0 | { |
583 | 0 | LLDB_LOGF(log, |
584 | 0 | "PlatformDarwinKernel registering kext binary '%s' with dSYM " |
585 | 0 | "sibling", |
586 | 0 | file_spec.GetPath().c_str()); |
587 | 0 | thisp->m_name_to_kext_path_map_with_dsyms.insert( |
588 | 0 | std::pair<ConstString, FileSpec>(bundle_conststr, file_spec)); |
589 | 0 | } |
590 | 0 | else |
591 | 0 | { |
592 | 0 | LLDB_LOGF(log, |
593 | 0 | "PlatformDarwinKernel registering kext binary '%s', no dSYM", |
594 | 0 | file_spec.GetPath().c_str()); |
595 | 0 | thisp->m_name_to_kext_path_map_without_dsyms.insert( |
596 | 0 | std::pair<ConstString, FileSpec>(bundle_conststr, file_spec)); |
597 | 0 | } |
598 | 0 | } |
599 | 0 | } |
600 | 0 | } |
601 | | |
602 | | // Given a FileSpec of /dir/dir/foo.kext |
603 | | // Return true if any of these exist: |
604 | | // /dir/dir/foo.kext.dSYM |
605 | | // /dir/dir/foo.kext/Contents/MacOS/foo.dSYM |
606 | | // /dir/dir/foo.kext/foo.dSYM |
607 | | bool PlatformDarwinKernel::KextHasdSYMSibling( |
608 | 0 | const FileSpec &kext_bundle_filepath) { |
609 | 0 | FileSpec dsym_fspec = kext_bundle_filepath; |
610 | 0 | std::string filename = dsym_fspec.GetFilename().AsCString(); |
611 | 0 | filename += ".dSYM"; |
612 | 0 | dsym_fspec.SetFilename(filename); |
613 | 0 | if (FileSystem::Instance().IsDirectory(dsym_fspec)) { |
614 | 0 | return true; |
615 | 0 | } |
616 | | // Should probably get the CFBundleExecutable here or call |
617 | | // CFBundleCopyExecutableURL |
618 | | |
619 | | // Look for a deep bundle foramt |
620 | 0 | ConstString executable_name = |
621 | 0 | kext_bundle_filepath.GetFileNameStrippingExtension(); |
622 | 0 | std::string deep_bundle_str = |
623 | 0 | kext_bundle_filepath.GetPath() + "/Contents/MacOS/"; |
624 | 0 | deep_bundle_str += executable_name.AsCString(); |
625 | 0 | deep_bundle_str += ".dSYM"; |
626 | 0 | dsym_fspec.SetFile(deep_bundle_str, FileSpec::Style::native); |
627 | 0 | FileSystem::Instance().Resolve(dsym_fspec); |
628 | 0 | if (FileSystem::Instance().IsDirectory(dsym_fspec)) { |
629 | 0 | return true; |
630 | 0 | } |
631 | | |
632 | | // look for a shallow bundle format |
633 | | // |
634 | 0 | std::string shallow_bundle_str = kext_bundle_filepath.GetPath() + "/"; |
635 | 0 | shallow_bundle_str += executable_name.AsCString(); |
636 | 0 | shallow_bundle_str += ".dSYM"; |
637 | 0 | dsym_fspec.SetFile(shallow_bundle_str, FileSpec::Style::native); |
638 | 0 | FileSystem::Instance().Resolve(dsym_fspec); |
639 | 0 | return FileSystem::Instance().IsDirectory(dsym_fspec); |
640 | 0 | } |
641 | | |
642 | | // Given a FileSpec of /dir/dir/mach.development.t7004 Return true if a dSYM |
643 | | // exists next to it: |
644 | | // /dir/dir/mach.development.t7004.dSYM |
645 | 0 | bool PlatformDarwinKernel::KernelHasdSYMSibling(const FileSpec &kernel_binary) { |
646 | 0 | FileSpec kernel_dsym = kernel_binary; |
647 | 0 | std::string filename = kernel_binary.GetFilename().AsCString(); |
648 | 0 | filename += ".dSYM"; |
649 | 0 | kernel_dsym.SetFilename(filename); |
650 | 0 | return FileSystem::Instance().IsDirectory(kernel_dsym); |
651 | 0 | } |
652 | | |
653 | | // Given a FileSpec of /dir/dir/mach.development.t7004.dSYM |
654 | | // Return true if only the dSYM exists, no binary next to it. |
655 | | // /dir/dir/mach.development.t7004.dSYM |
656 | | // but no |
657 | | // /dir/dir/mach.development.t7004 |
658 | | bool PlatformDarwinKernel::KerneldSYMHasNoSiblingBinary( |
659 | 0 | const FileSpec &kernel_dsym) { |
660 | 0 | static constexpr llvm::StringLiteral g_dsym_suffix = ".dSYM"; |
661 | 0 | std::string possible_path = kernel_dsym.GetPath(); |
662 | 0 | if (kernel_dsym.GetFileNameExtension() != g_dsym_suffix) |
663 | 0 | return false; |
664 | | |
665 | 0 | FileSpec binary_filespec = kernel_dsym; |
666 | | // Chop off the '.dSYM' extension on the filename |
667 | 0 | binary_filespec.SetFilename(binary_filespec.GetFileNameStrippingExtension()); |
668 | | |
669 | | // Is there a binary next to this this? Then return false. |
670 | 0 | if (FileSystem::Instance().Exists(binary_filespec)) |
671 | 0 | return false; |
672 | | |
673 | | // If we have at least one binary in the DWARF subdir, then |
674 | | // this is a properly formed dSYM and it has no binary next |
675 | | // to it. |
676 | 0 | if (GetDWARFBinaryInDSYMBundle(kernel_dsym).size() > 0) |
677 | 0 | return true; |
678 | | |
679 | 0 | return false; |
680 | 0 | } |
681 | | |
682 | | // TODO: This method returns a vector of FileSpec's because a |
683 | | // dSYM bundle may contain multiple DWARF binaries, but it |
684 | | // only implements returning the base name binary for now; |
685 | | // it should iterate over every binary in the DWARF subdir |
686 | | // and return them all. |
687 | | std::vector<FileSpec> |
688 | 0 | PlatformDarwinKernel::GetDWARFBinaryInDSYMBundle(const FileSpec &dsym_bundle) { |
689 | 0 | std::vector<FileSpec> results; |
690 | 0 | static constexpr llvm::StringLiteral g_dsym_suffix = ".dSYM"; |
691 | 0 | if (dsym_bundle.GetFileNameExtension() != g_dsym_suffix) { |
692 | 0 | return results; |
693 | 0 | } |
694 | | // Drop the '.dSYM' from the filename |
695 | 0 | std::string filename = |
696 | 0 | dsym_bundle.GetFileNameStrippingExtension().GetCString(); |
697 | 0 | std::string dirname = dsym_bundle.GetDirectory().GetCString(); |
698 | |
|
699 | 0 | std::string binary_filepath = dsym_bundle.GetPath(); |
700 | 0 | binary_filepath += "/Contents/Resources/DWARF/"; |
701 | 0 | binary_filepath += filename; |
702 | |
|
703 | 0 | FileSpec binary_fspec(binary_filepath); |
704 | 0 | if (FileSystem::Instance().Exists(binary_fspec)) |
705 | 0 | results.push_back(binary_fspec); |
706 | 0 | return results; |
707 | 0 | } |
708 | | |
709 | 0 | void PlatformDarwinKernel::UpdateKextandKernelsLocalScan() { |
710 | 0 | std::call_once(m_kext_scan_flag, [this]() { |
711 | 0 | CollectKextAndKernelDirectories(); |
712 | 0 | SearchForKextsAndKernelsRecursively(); |
713 | 0 | }); |
714 | 0 | } |
715 | | |
716 | | Status PlatformDarwinKernel::GetSharedModule( |
717 | | const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp, |
718 | | const FileSpecList *module_search_paths_ptr, |
719 | 0 | llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) { |
720 | 0 | Status error; |
721 | 0 | module_sp.reset(); |
722 | 0 | const FileSpec &platform_file = module_spec.GetFileSpec(); |
723 | | |
724 | | // Treat the file's path as a kext bundle ID (e.g. |
725 | | // "com.apple.driver.AppleIRController") and search our kext index. |
726 | 0 | std::string kext_bundle_id = platform_file.GetPath(); |
727 | |
|
728 | 0 | if (module_spec.GetUUID().IsValid()) { |
729 | | // DynamicLoaderDarwinKernel uses the magic name mach_kernel, |
730 | | // UUID search can get here with no name - and it may be a kernel. |
731 | 0 | if (kext_bundle_id == "mach_kernel" || kext_bundle_id.empty()) { |
732 | 0 | error = GetSharedModuleKernel(module_spec, process, module_sp, |
733 | 0 | module_search_paths_ptr, old_modules, |
734 | 0 | did_create_ptr); |
735 | 0 | if (error.Success() && module_sp) { |
736 | 0 | return error; |
737 | 0 | } |
738 | 0 | } else { |
739 | 0 | return GetSharedModuleKext(module_spec, process, module_sp, |
740 | 0 | module_search_paths_ptr, old_modules, |
741 | 0 | did_create_ptr); |
742 | 0 | } |
743 | 0 | } |
744 | | |
745 | | // Give the generic methods, including possibly calling into DebugSymbols |
746 | | // framework on macOS systems, a chance. |
747 | 0 | return PlatformDarwin::GetSharedModule(module_spec, process, module_sp, |
748 | 0 | module_search_paths_ptr, old_modules, |
749 | 0 | did_create_ptr); |
750 | 0 | } |
751 | | |
752 | | Status PlatformDarwinKernel::GetSharedModuleKext( |
753 | | const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp, |
754 | | const FileSpecList *module_search_paths_ptr, |
755 | 0 | llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) { |
756 | 0 | Status error; |
757 | 0 | module_sp.reset(); |
758 | 0 | const FileSpec &platform_file = module_spec.GetFileSpec(); |
759 | | |
760 | | // Treat the file's path as a kext bundle ID (e.g. |
761 | | // "com.apple.driver.AppleIRController") and search our kext index. |
762 | 0 | ConstString kext_bundle(platform_file.GetPath().c_str()); |
763 | | // First look through the kext bundles that had a dsym next to them |
764 | 0 | if (m_name_to_kext_path_map_with_dsyms.count(kext_bundle) > 0) { |
765 | 0 | for (BundleIDToKextIterator it = m_name_to_kext_path_map_with_dsyms.begin(); |
766 | 0 | it != m_name_to_kext_path_map_with_dsyms.end(); ++it) { |
767 | 0 | if (it->first == kext_bundle) { |
768 | 0 | error = ExamineKextForMatchingUUID(it->second, module_spec.GetUUID(), |
769 | 0 | module_spec.GetArchitecture(), |
770 | 0 | module_sp); |
771 | 0 | if (module_sp.get()) { |
772 | 0 | return error; |
773 | 0 | } |
774 | 0 | } |
775 | 0 | } |
776 | 0 | } |
777 | | |
778 | | // Give the generic methods, including possibly calling into DebugSymbols |
779 | | // framework on macOS systems, a chance. |
780 | 0 | error = PlatformDarwin::GetSharedModule(module_spec, process, module_sp, |
781 | 0 | module_search_paths_ptr, old_modules, |
782 | 0 | did_create_ptr); |
783 | 0 | if (error.Success() && module_sp.get()) { |
784 | 0 | return error; |
785 | 0 | } |
786 | | |
787 | 0 | return error; |
788 | 0 | } |
789 | | |
790 | | Status PlatformDarwinKernel::GetSharedModuleKernel( |
791 | | const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp, |
792 | | const FileSpecList *module_search_paths_ptr, |
793 | 0 | llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) { |
794 | 0 | Status error; |
795 | 0 | module_sp.reset(); |
796 | 0 | UpdateKextandKernelsLocalScan(); |
797 | | |
798 | | // First try all kernel binaries that have a dSYM next to them |
799 | 0 | for (auto possible_kernel : m_kernel_binaries_with_dsyms) { |
800 | 0 | if (FileSystem::Instance().Exists(possible_kernel)) { |
801 | 0 | ModuleSpec kern_spec(possible_kernel); |
802 | 0 | kern_spec.GetUUID() = module_spec.GetUUID(); |
803 | 0 | module_sp.reset(new Module(kern_spec)); |
804 | 0 | if (module_sp && module_sp->GetObjectFile() && |
805 | 0 | module_sp->MatchesModuleSpec(kern_spec)) { |
806 | | // module_sp is an actual kernel binary we want to add. |
807 | 0 | if (process) { |
808 | 0 | const bool notify = false; |
809 | 0 | process->GetTarget().GetImages().AppendIfNeeded(module_sp, notify); |
810 | 0 | error.Clear(); |
811 | 0 | return error; |
812 | 0 | } else { |
813 | 0 | error = ModuleList::GetSharedModule(kern_spec, module_sp, nullptr, |
814 | 0 | nullptr, nullptr); |
815 | 0 | if (module_sp && module_sp->GetObjectFile() && |
816 | 0 | module_sp->GetObjectFile()->GetType() != |
817 | 0 | ObjectFile::Type::eTypeCoreFile) { |
818 | 0 | return error; |
819 | 0 | } |
820 | 0 | module_sp.reset(); |
821 | 0 | } |
822 | 0 | } |
823 | 0 | } |
824 | 0 | } |
825 | | |
826 | | // Next try all dSYMs that have no kernel binary next to them (load |
827 | | // the kernel DWARF stub as the main binary) |
828 | 0 | for (auto possible_kernel_dsym : m_kernel_dsyms_no_binaries) { |
829 | 0 | std::vector<FileSpec> objfile_names = |
830 | 0 | GetDWARFBinaryInDSYMBundle(possible_kernel_dsym); |
831 | 0 | for (FileSpec objfile : objfile_names) { |
832 | 0 | ModuleSpec kern_spec(objfile); |
833 | 0 | kern_spec.GetUUID() = module_spec.GetUUID(); |
834 | 0 | kern_spec.GetSymbolFileSpec() = possible_kernel_dsym; |
835 | |
|
836 | 0 | module_sp.reset(new Module(kern_spec)); |
837 | 0 | if (module_sp && module_sp->GetObjectFile() && |
838 | 0 | module_sp->MatchesModuleSpec(kern_spec)) { |
839 | | // module_sp is an actual kernel binary we want to add. |
840 | 0 | if (process) { |
841 | 0 | const bool notify = false; |
842 | 0 | process->GetTarget().GetImages().AppendIfNeeded(module_sp, notify); |
843 | 0 | error.Clear(); |
844 | 0 | return error; |
845 | 0 | } else { |
846 | 0 | error = ModuleList::GetSharedModule(kern_spec, module_sp, nullptr, |
847 | 0 | nullptr, nullptr); |
848 | 0 | if (module_sp && module_sp->GetObjectFile() && |
849 | 0 | module_sp->GetObjectFile()->GetType() != |
850 | 0 | ObjectFile::Type::eTypeCoreFile) { |
851 | 0 | return error; |
852 | 0 | } |
853 | 0 | module_sp.reset(); |
854 | 0 | } |
855 | 0 | } |
856 | 0 | } |
857 | 0 | } |
858 | | |
859 | | // Give the generic methods, including possibly calling into DebugSymbols |
860 | | // framework on macOS systems, a chance. |
861 | 0 | error = PlatformDarwin::GetSharedModule(module_spec, process, module_sp, |
862 | 0 | module_search_paths_ptr, old_modules, |
863 | 0 | did_create_ptr); |
864 | 0 | if (error.Success() && module_sp.get()) { |
865 | 0 | return error; |
866 | 0 | } |
867 | | |
868 | 0 | return error; |
869 | 0 | } |
870 | | |
871 | | std::vector<lldb_private::FileSpec> |
872 | 0 | PlatformDarwinKernel::SearchForExecutablesRecursively(const std::string &dir) { |
873 | 0 | std::vector<FileSpec> executables; |
874 | 0 | std::error_code EC; |
875 | 0 | for (llvm::sys::fs::recursive_directory_iterator it(dir.c_str(), EC), |
876 | 0 | end; |
877 | 0 | it != end && !EC; it.increment(EC)) { |
878 | 0 | auto status = it->status(); |
879 | 0 | if (!status) |
880 | 0 | break; |
881 | 0 | if (llvm::sys::fs::is_regular_file(*status) && |
882 | 0 | llvm::sys::fs::can_execute(it->path())) |
883 | 0 | executables.emplace_back(it->path()); |
884 | 0 | } |
885 | 0 | return executables; |
886 | 0 | } |
887 | | |
888 | | Status PlatformDarwinKernel::ExamineKextForMatchingUUID( |
889 | | const FileSpec &kext_bundle_path, const lldb_private::UUID &uuid, |
890 | 0 | const ArchSpec &arch, ModuleSP &exe_module_sp) { |
891 | 0 | for (const auto &exe_file : |
892 | 0 | SearchForExecutablesRecursively(kext_bundle_path.GetPath())) { |
893 | 0 | if (FileSystem::Instance().Exists(exe_file)) { |
894 | 0 | ModuleSpec exe_spec(exe_file); |
895 | 0 | exe_spec.GetUUID() = uuid; |
896 | 0 | if (!uuid.IsValid()) { |
897 | 0 | exe_spec.GetArchitecture() = arch; |
898 | 0 | } |
899 | | |
900 | | // First try to create a ModuleSP with the file / arch and see if the UUID |
901 | | // matches. If that fails (this exec file doesn't have the correct uuid), |
902 | | // don't call GetSharedModule (which may call in to the DebugSymbols |
903 | | // framework and therefore can be slow.) |
904 | 0 | ModuleSP module_sp(new Module(exe_spec)); |
905 | 0 | if (module_sp && module_sp->GetObjectFile() && |
906 | 0 | module_sp->MatchesModuleSpec(exe_spec)) { |
907 | 0 | Status error = ModuleList::GetSharedModule(exe_spec, exe_module_sp, |
908 | 0 | NULL, NULL, NULL); |
909 | 0 | if (exe_module_sp && exe_module_sp->GetObjectFile()) { |
910 | 0 | return error; |
911 | 0 | } |
912 | 0 | } |
913 | 0 | exe_module_sp.reset(); |
914 | 0 | } |
915 | 0 | } |
916 | | |
917 | 0 | return {}; |
918 | 0 | } |
919 | | |
920 | | static addr_t find_kernel_in_macho_fileset(Process *process, |
921 | 4 | addr_t input_addr) { |
922 | 4 | Status error; |
923 | 4 | WritableDataBufferSP header_data(new DataBufferHeap(512, 0)); |
924 | 4 | if (!process->ReadMemory(input_addr, header_data->GetBytes(), |
925 | 4 | header_data->GetByteSize(), error) || |
926 | 4 | !error.Success()) |
927 | 0 | return LLDB_INVALID_ADDRESS; |
928 | 4 | ModuleSP module_sp(new Module(ModuleSpec())); |
929 | 4 | ObjectContainerSP container_sp( |
930 | 4 | ObjectContainerMachOFileset::CreateMemoryInstance( |
931 | 4 | module_sp, header_data, process->shared_from_this(), input_addr)); |
932 | 4 | if (!container_sp) |
933 | 4 | return LLDB_INVALID_ADDRESS; |
934 | | |
935 | 0 | ObjectContainerMachOFileset *fileset_container = |
936 | 0 | static_cast<ObjectContainerMachOFileset *>(container_sp.get()); |
937 | 0 | ObjectContainerMachOFileset::Entry *entry = |
938 | 0 | fileset_container->FindEntry("com.apple.kernel"); |
939 | 0 | if (entry) |
940 | 0 | return entry->vmaddr; |
941 | 0 | return LLDB_INVALID_ADDRESS; |
942 | 0 | } |
943 | | |
944 | | bool PlatformDarwinKernel::LoadPlatformBinaryAndSetup(Process *process, |
945 | | lldb::addr_t input_addr, |
946 | 4 | bool notify) { |
947 | 4 | Log *log = |
948 | 4 | GetLog(LLDBLog::Platform | LLDBLog::DynamicLoader | LLDBLog::Process); |
949 | | |
950 | 4 | if (!process) |
951 | 0 | return false; |
952 | | |
953 | 4 | addr_t actual_address = find_kernel_in_macho_fileset(process, input_addr); |
954 | | |
955 | 4 | if (actual_address == LLDB_INVALID_ADDRESS) |
956 | 4 | return false; |
957 | | |
958 | 0 | LLDB_LOGF(log, |
959 | 0 | "PlatformDarwinKernel::%s check address 0x%" PRIx64 " for " |
960 | 0 | "a macho fileset, got back kernel address 0x%" PRIx64, |
961 | 0 | __FUNCTION__, input_addr, actual_address); |
962 | | |
963 | | // We have a xnu kernel binary, this is a kernel debug session. |
964 | | // Set the Target's Platform to be PlatformDarwinKernel, and the |
965 | | // Process' DynamicLoader to be DynamicLoaderDarwinKernel. |
966 | |
|
967 | 0 | PlatformSP platform_sp = |
968 | 0 | process->GetTarget().GetDebugger().GetPlatformList().Create( |
969 | 0 | PlatformDarwinKernel::GetPluginNameStatic()); |
970 | 0 | if (platform_sp) |
971 | 0 | process->GetTarget().SetPlatform(platform_sp); |
972 | |
|
973 | 0 | DynamicLoaderUP dyld_up = |
974 | 0 | std::make_unique<DynamicLoaderDarwinKernel>(process, actual_address); |
975 | 0 | if (!dyld_up) |
976 | 0 | return false; |
977 | | |
978 | | // Process owns it now |
979 | 0 | process->SetDynamicLoader(std::move(dyld_up)); |
980 | |
|
981 | 0 | return true; |
982 | 0 | } |
983 | | |
984 | | std::vector<ArchSpec> PlatformDarwinKernel::GetSupportedArchitectures( |
985 | 0 | const ArchSpec &process_host_arch) { |
986 | 0 | std::vector<ArchSpec> result; |
987 | 0 | ARMGetSupportedArchitectures(result); |
988 | 0 | x86GetSupportedArchitectures(result); |
989 | 0 | return result; |
990 | 0 | } |
991 | | |
992 | 0 | void PlatformDarwinKernel::CalculateTrapHandlerSymbolNames() { |
993 | 0 | m_trap_handlers.push_back(ConstString("trap_from_kernel")); |
994 | 0 | m_trap_handlers.push_back(ConstString("hndl_machine_check")); |
995 | 0 | m_trap_handlers.push_back(ConstString("hndl_double_fault")); |
996 | 0 | m_trap_handlers.push_back(ConstString("hndl_allintrs")); |
997 | 0 | m_trap_handlers.push_back(ConstString("hndl_alltraps")); |
998 | 0 | m_trap_handlers.push_back(ConstString("interrupt")); |
999 | 0 | m_trap_handlers.push_back(ConstString("fleh_prefabt")); |
1000 | 0 | m_trap_handlers.push_back(ConstString("ExceptionVectorsBase")); |
1001 | 0 | m_trap_handlers.push_back(ConstString("ExceptionVectorsTable")); |
1002 | 0 | m_trap_handlers.push_back(ConstString("fleh_undef")); |
1003 | 0 | m_trap_handlers.push_back(ConstString("fleh_dataabt")); |
1004 | 0 | m_trap_handlers.push_back(ConstString("fleh_irq")); |
1005 | 0 | m_trap_handlers.push_back(ConstString("fleh_decirq")); |
1006 | 0 | m_trap_handlers.push_back(ConstString("fleh_fiq_generic")); |
1007 | 0 | m_trap_handlers.push_back(ConstString("fleh_dec")); |
1008 | 0 | } |
1009 | | |
1010 | | #endif // __APPLE__ |