1 //===-- PlatformDarwinKernel.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "PlatformDarwinKernel.h"
11 
12 #if defined (__APPLE__)  // This Plugin uses the Mac-specific source/Host/macosx/cfcpp utilities
13 
14 
15 // C Includes
16 // C++ Includes
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/ArchSpec.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleList.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Host/FileSpec.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Interpreter/OptionValueFileSpecList.h"
30 #include "lldb/Interpreter/OptionValueProperties.h"
31 #include "lldb/Interpreter/Property.h"
32 #include "lldb/Target/Platform.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/Target.h"
35 
36 #include <CoreFoundation/CoreFoundation.h>
37 
38 #include "Host/macosx/cfcpp/CFCBundle.h"
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 
43 //------------------------------------------------------------------
44 // Static Variables
45 //------------------------------------------------------------------
46 static uint32_t g_initialize_count = 0;
47 
48 //------------------------------------------------------------------
49 // Static Functions
50 //------------------------------------------------------------------
51 void
52 PlatformDarwinKernel::Initialize ()
53 {
54     if (g_initialize_count++ == 0)
55     {
56         PluginManager::RegisterPlugin (PlatformDarwinKernel::GetPluginNameStatic(),
57                                        PlatformDarwinKernel::GetDescriptionStatic(),
58                                        PlatformDarwinKernel::CreateInstance,
59                                        PlatformDarwinKernel::DebuggerInitialize);
60     }
61 }
62 
63 void
64 PlatformDarwinKernel::Terminate ()
65 {
66     if (g_initialize_count > 0)
67     {
68         if (--g_initialize_count == 0)
69         {
70             PluginManager::UnregisterPlugin (PlatformDarwinKernel::CreateInstance);
71         }
72     }
73 }
74 
75 Platform*
76 PlatformDarwinKernel::CreateInstance (bool force, const ArchSpec *arch)
77 {
78     // This is a special plugin that we don't want to activate just based on an ArchSpec for normal
79     // userlnad debugging.  It is only useful in kernel debug sessions and the DynamicLoaderDarwinPlugin
80     // (or a user doing 'platform select') will force the creation of this Platform plugin.
81     if (force == false)
82         return NULL;
83 
84     bool create = force;
85     LazyBool is_ios_debug_session = eLazyBoolCalculate;
86 
87     if (create == false && arch && arch->IsValid())
88     {
89         const llvm::Triple &triple = arch->GetTriple();
90         switch (triple.getVendor())
91         {
92             case llvm::Triple::Apple:
93                 create = true;
94                 break;
95 
96             // Only accept "unknown" for vendor if the host is Apple and
97             // it "unknown" wasn't specified (it was just returned because it
98             // was NOT specified)
99             case llvm::Triple::UnknownArch:
100                 create = !arch->TripleVendorWasSpecified();
101                 break;
102             default:
103                 break;
104         }
105 
106         if (create)
107         {
108             switch (triple.getOS())
109             {
110                 case llvm::Triple::Darwin:  // Deprecated, but still support Darwin for historical reasons
111                 case llvm::Triple::MacOSX:
112                     break;
113                 // Only accept "vendor" for vendor if the host is Apple and
114                 // it "unknown" wasn't specified (it was just returned because it
115                 // was NOT specified)
116                 case llvm::Triple::UnknownOS:
117                     create = !arch->TripleOSWasSpecified();
118                     break;
119                 default:
120                     create = false;
121                     break;
122             }
123         }
124     }
125     if (arch && arch->IsValid())
126     {
127         switch (arch->GetMachine())
128         {
129         case llvm::Triple::x86:
130         case llvm::Triple::x86_64:
131         case llvm::Triple::ppc:
132         case llvm::Triple::ppc64:
133             is_ios_debug_session = eLazyBoolNo;
134             break;
135         case llvm::Triple::arm:
136         case llvm::Triple::aarch64:
137         case llvm::Triple::thumb:
138             is_ios_debug_session = eLazyBoolYes;
139             break;
140         default:
141             is_ios_debug_session = eLazyBoolCalculate;
142             break;
143         }
144     }
145     if (create)
146         return new PlatformDarwinKernel (is_ios_debug_session);
147     return NULL;
148 }
149 
150 
151 lldb_private::ConstString
152 PlatformDarwinKernel::GetPluginNameStatic ()
153 {
154     static ConstString g_name("darwin-kernel");
155     return g_name;
156 }
157 
158 const char *
159 PlatformDarwinKernel::GetDescriptionStatic()
160 {
161     return "Darwin Kernel platform plug-in.";
162 }
163 
164 //------------------------------------------------------------------
165 /// Code to handle the PlatformDarwinKernel settings
166 //------------------------------------------------------------------
167 
168 static PropertyDefinition
169 g_properties[] =
170 {
171     { "search-locally-for-kexts" , OptionValue::eTypeBoolean,      true, true, NULL, NULL, "Automatically search for kexts on the local system when doing kernel debugging." },
172     { "kext-directories",          OptionValue::eTypeFileSpecList, false, 0,   NULL, NULL, "Directories/KDKs to search for kexts in when starting a kernel debug session." },
173     {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
174 };
175 
176 enum {
177     ePropertySearchForKexts = 0,
178     ePropertyKextDirectories
179 };
180 
181 
182 
183 class PlatformDarwinKernelProperties : public Properties
184 {
185 public:
186 
187     static ConstString &
188     GetSettingName ()
189     {
190         static ConstString g_setting_name("darwin-kernel");
191         return g_setting_name;
192     }
193 
194     PlatformDarwinKernelProperties() :
195         Properties ()
196     {
197         m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
198         m_collection_sp->Initialize(g_properties);
199     }
200 
201     virtual
202     ~PlatformDarwinKernelProperties()
203     {
204     }
205 
206     bool
207     GetSearchForKexts() const
208     {
209         const uint32_t idx = ePropertySearchForKexts;
210         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
211     }
212 
213     FileSpecList &
214     GetKextDirectories() const
215     {
216         const uint32_t idx = ePropertyKextDirectories;
217         OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
218         assert(option_value);
219         return option_value->GetCurrentValue();
220     }
221 };
222 
223 typedef std::shared_ptr<PlatformDarwinKernelProperties> PlatformDarwinKernelPropertiesSP;
224 
225 static const PlatformDarwinKernelPropertiesSP &
226 GetGlobalProperties()
227 {
228     static PlatformDarwinKernelPropertiesSP g_settings_sp;
229     if (!g_settings_sp)
230         g_settings_sp.reset (new PlatformDarwinKernelProperties ());
231     return g_settings_sp;
232 }
233 
234 void
235 PlatformDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
236 {
237     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformDarwinKernelProperties::GetSettingName()))
238     {
239         const bool is_global_setting = true;
240         PluginManager::CreateSettingForPlatformPlugin (debugger,
241                                                             GetGlobalProperties()->GetValueProperties(),
242                                                             ConstString ("Properties for the PlatformDarwinKernel plug-in."),
243                                                             is_global_setting);
244     }
245 }
246 
247 //------------------------------------------------------------------
248 /// Default Constructor
249 //------------------------------------------------------------------
250 PlatformDarwinKernel::PlatformDarwinKernel (lldb_private::LazyBool is_ios_debug_session) :
251     PlatformDarwin (false),    // This is a remote platform
252     m_name_to_kext_path_map(),
253     m_directories_searched(),
254     m_ios_debug_session(is_ios_debug_session)
255 
256 {
257     if (GetGlobalProperties()->GetSearchForKexts())
258     {
259         SearchForKexts ();
260     }
261 }
262 
263 //------------------------------------------------------------------
264 /// Destructor.
265 ///
266 /// The destructor is virtual since this class is designed to be
267 /// inherited from by the plug-in instance.
268 //------------------------------------------------------------------
269 PlatformDarwinKernel::~PlatformDarwinKernel()
270 {
271 }
272 
273 
274 void
275 PlatformDarwinKernel::GetStatus (Stream &strm)
276 {
277     Platform::GetStatus (strm);
278     strm.Printf (" Debug session type: ");
279     if (m_ios_debug_session == eLazyBoolYes)
280         strm.Printf ("iOS kernel debugging\n");
281     else if (m_ios_debug_session == eLazyBoolNo)
282         strm.Printf ("Mac OS X kernel debugging\n");
283     else
284             strm.Printf ("unknown kernel debugging\n");
285     const uint32_t num_kext_dirs = m_directories_searched.size();
286     for (uint32_t i=0; i<num_kext_dirs; ++i)
287     {
288         const FileSpec &kext_dir = m_directories_searched[i];
289         strm.Printf (" Kext directories: [%2u] \"%s\"\n", i, kext_dir.GetPath().c_str());
290     }
291     strm.Printf (" Total number of kexts indexed: %d\n", (int) m_name_to_kext_path_map.size());
292 }
293 
294 void
295 PlatformDarwinKernel::SearchForKexts ()
296 {
297     // Differentiate between "ios debug session" and "mac debug session" so we don't index
298     // kext bundles that won't be used in this debug session.  If this is an ios kext debug
299     // session, looking in /System/Library/Extensions is a waste of stat()s, for example.
300 
301     // Build up a list of all SDKs we'll be searching for directories of kexts
302     // e.g. /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk
303     std::vector<FileSpec> sdk_dirs;
304     if (m_ios_debug_session != eLazyBoolNo)
305         GetiOSSDKDirectoriesToSearch (sdk_dirs);
306     if (m_ios_debug_session != eLazyBoolYes)
307         GetMacSDKDirectoriesToSearch (sdk_dirs);
308 
309     GetGenericSDKDirectoriesToSearch (sdk_dirs);
310 
311     // Build up a list of directories that hold kext bundles on the system
312     // e.g. /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk/System/Library/Extensions
313     std::vector<FileSpec> kext_dirs;
314     SearchSDKsForKextDirectories (sdk_dirs, kext_dirs);
315 
316     if (m_ios_debug_session != eLazyBoolNo)
317         GetiOSDirectoriesToSearch (kext_dirs);
318     if (m_ios_debug_session != eLazyBoolYes)
319         GetMacDirectoriesToSearch (kext_dirs);
320 
321     GetGenericDirectoriesToSearch (kext_dirs);
322 
323     GetUserSpecifiedDirectoriesToSearch (kext_dirs);
324 
325     // We now have a complete list of directories that we will search for kext bundles
326     m_directories_searched = kext_dirs;
327 
328     IndexKextsInDirectories (kext_dirs);
329 }
330 
331 void
332 PlatformDarwinKernel::GetiOSSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
333 {
334     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
335     const char *developer_dir = GetDeveloperDirectory();
336     if (developer_dir == NULL)
337         developer_dir = "/Applications/Xcode.app/Contents/Developer";
338 
339     char pathbuf[PATH_MAX];
340     ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/iPhoneOS.platform/Developer/SDKs", developer_dir);
341     FileSpec ios_sdk(pathbuf, true);
342     if (ios_sdk.Exists() && ios_sdk.IsDirectory())
343     {
344         directories.push_back (ios_sdk);
345     }
346 }
347 
348 void
349 PlatformDarwinKernel::GetMacSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
350 {
351     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
352     const char *developer_dir = GetDeveloperDirectory();
353     if (developer_dir == NULL)
354         developer_dir = "/Applications/Xcode.app/Contents/Developer";
355 
356     char pathbuf[PATH_MAX];
357     ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/MacOSX.platform/Developer/SDKs", developer_dir);
358     FileSpec mac_sdk(pathbuf, true);
359     if (mac_sdk.Exists() && mac_sdk.IsDirectory())
360     {
361         directories.push_back (mac_sdk);
362     }
363 }
364 
365 void
366 PlatformDarwinKernel::GetGenericSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
367 {
368     FileSpec generic_sdk("/AppleInternal/Developer/KDKs", true);
369     if (generic_sdk.Exists() && generic_sdk.IsDirectory())
370     {
371         directories.push_back (generic_sdk);
372     }
373 
374     // The KDKs distributed from Apple installed on external
375     // developer systems may be in directories like
376     // /Library/Developer/KDKs/KDK_10.10_14A298i.kdk
377     FileSpec installed_kdks("/Library/Developer/KDKs", true);
378     if (installed_kdks.Exists() && installed_kdks.IsDirectory())
379     {
380         directories.push_back (installed_kdks);
381     }
382 
383 }
384 
385 void
386 PlatformDarwinKernel::GetiOSDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
387 {
388 }
389 
390 void
391 PlatformDarwinKernel::GetMacDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
392 {
393     FileSpec sle("/System/Library/Extensions", true);
394     if (sle.Exists() && sle.IsDirectory())
395     {
396         directories.push_back(sle);
397     }
398 
399     FileSpec le("/Library/Extensions", true);
400     if (le.Exists() && le.IsDirectory())
401     {
402         directories.push_back(le);
403     }
404 
405     FileSpec kdk("/Volumes/KernelDebugKit", true);
406     if (kdk.Exists() && kdk.IsDirectory())
407     {
408         directories.push_back(kdk);
409     }
410 }
411 
412 void
413 PlatformDarwinKernel::GetGenericDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
414 {
415     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
416     const char *developer_dir = GetDeveloperDirectory();
417     if (developer_dir == NULL)
418         developer_dir = "/Applications/Xcode.app/Contents/Developer";
419 
420     char pathbuf[PATH_MAX];
421     ::snprintf (pathbuf, sizeof (pathbuf), "%s/../Symbols", developer_dir);
422     FileSpec symbols_dir (pathbuf, true);
423     if (symbols_dir.Exists() && symbols_dir.IsDirectory())
424     {
425         directories.push_back (symbols_dir);
426     }
427 }
428 
429 void
430 PlatformDarwinKernel::GetUserSpecifiedDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
431 {
432     FileSpecList user_dirs(GetGlobalProperties()->GetKextDirectories());
433     std::vector<FileSpec> possible_sdk_dirs;
434 
435     const uint32_t user_dirs_count = user_dirs.GetSize();
436     for (uint32_t i = 0; i < user_dirs_count; i++)
437     {
438         FileSpec dir = user_dirs.GetFileSpecAtIndex (i);
439         dir.ResolvePath();
440         if (dir.Exists() && dir.IsDirectory())
441         {
442             directories.push_back (dir);
443             possible_sdk_dirs.push_back (dir);  // does this directory have a *.sdk or *.kdk that we should look in?
444 
445             // Is there a "System/Library/Extensions" subdir of this directory?
446             std::string dir_sle_path = dir.GetPath();
447             dir_sle_path.append ("/System/Library/Extensions");
448             FileSpec dir_sle(dir_sle_path.c_str(), true);
449             if (dir_sle.Exists() && dir_sle.IsDirectory())
450             {
451                 directories.push_back (dir_sle);
452             }
453         }
454     }
455 
456     SearchSDKsForKextDirectories (possible_sdk_dirs, directories);
457 }
458 
459 // Scan through the SDK directories, looking for directories where kexts are likely.
460 // Add those directories to kext_dirs.
461 void
462 PlatformDarwinKernel::SearchSDKsForKextDirectories (std::vector<lldb_private::FileSpec> sdk_dirs, std::vector<lldb_private::FileSpec> &kext_dirs)
463 {
464     const uint32_t num_sdks = sdk_dirs.size();
465     for (uint32_t i = 0; i < num_sdks; i++)
466     {
467         const FileSpec &sdk_dir = sdk_dirs[i];
468         std::string sdk_dir_path = sdk_dir.GetPath();
469         if (!sdk_dir_path.empty())
470         {
471             const bool find_directories = true;
472             const bool find_files = false;
473             const bool find_other = false;
474             FileSpec::EnumerateDirectory (sdk_dir_path.c_str(),
475                                           find_directories,
476                                           find_files,
477                                           find_other,
478                                           GetKextDirectoriesInSDK,
479                                           &kext_dirs);
480         }
481     }
482 }
483 
484 // Callback for FileSpec::EnumerateDirectory().
485 // Step through the entries in a directory like
486 //    /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
487 // looking for any subdirectories of the form MacOSX10.8.Internal.sdk/System/Library/Extensions
488 // Adds these to the vector of FileSpec's.
489 
490 FileSpec::EnumerateDirectoryResult
491 PlatformDarwinKernel::GetKextDirectoriesInSDK (void *baton,
492                                                FileSpec::FileType file_type,
493                                                const FileSpec &file_spec)
494 {
495     if (file_type == FileSpec::eFileTypeDirectory
496         && (file_spec.GetFileNameExtension() == ConstString("sdk")
497             || file_spec.GetFileNameExtension() == ConstString("kdk")))
498     {
499         std::string kext_directory_path = file_spec.GetPath();
500 
501         // Append the raw directory path, e.g. /Library/Developer/KDKs/KDK_10.10_14A298i.kdk
502         // to the directory search list -- there may be kexts sitting directly
503         // in that directory instead of being in a System/Library/Extensions subdir.
504         ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
505 
506         // Check to see if there is a System/Library/Extensions subdir & add it if it exists
507 
508         std::string sle_kext_directory_path (kext_directory_path);
509         sle_kext_directory_path.append ("/System/Library/Extensions");
510         FileSpec sle_kext_directory (sle_kext_directory_path.c_str(), true);
511         if (sle_kext_directory.Exists() && sle_kext_directory.IsDirectory())
512         {
513             ((std::vector<lldb_private::FileSpec> *)baton)->push_back(sle_kext_directory);
514         }
515 
516         // Check to see if there is a Library/Extensions subdir & add it if it exists
517 
518         std::string le_kext_directory_path (kext_directory_path);
519         le_kext_directory_path.append ("/Library/Extensions");
520         FileSpec le_kext_directory (le_kext_directory_path.c_str(), true);
521         if (le_kext_directory.Exists() && le_kext_directory.IsDirectory())
522         {
523             ((std::vector<lldb_private::FileSpec> *)baton)->push_back(le_kext_directory);
524         }
525 
526     }
527     return FileSpec::eEnumerateDirectoryResultNext;
528 }
529 
530 void
531 PlatformDarwinKernel::IndexKextsInDirectories (std::vector<lldb_private::FileSpec> kext_dirs)
532 {
533     std::vector<FileSpec> kext_bundles;
534 
535     const uint32_t num_dirs = kext_dirs.size();
536     for (uint32_t i = 0; i < num_dirs; i++)
537     {
538         const FileSpec &dir = kext_dirs[i];
539         const bool find_directories = true;
540         const bool find_files = false;
541         const bool find_other = false;
542         FileSpec::EnumerateDirectory (dir.GetPath().c_str(),
543                                       find_directories,
544                                       find_files,
545                                       find_other,
546                                       GetKextsInDirectory,
547                                       &kext_bundles);
548     }
549 
550     const uint32_t num_kexts = kext_bundles.size();
551     for (uint32_t i = 0; i < num_kexts; i++)
552     {
553         const FileSpec &kext = kext_bundles[i];
554         CFCBundle bundle (kext.GetPath().c_str());
555         CFStringRef bundle_id (bundle.GetIdentifier());
556         if (bundle_id && CFGetTypeID (bundle_id) == CFStringGetTypeID ())
557         {
558             char bundle_id_buf[PATH_MAX];
559             if (CFStringGetCString (bundle_id, bundle_id_buf, sizeof (bundle_id_buf), kCFStringEncodingUTF8))
560             {
561                 ConstString bundle_conststr(bundle_id_buf);
562                 m_name_to_kext_path_map.insert(std::pair<ConstString, FileSpec>(bundle_conststr, kext));
563             }
564         }
565     }
566 }
567 
568 // Callback for FileSpec::EnumerateDirectory().
569 // Step through the entries in a directory like /System/Library/Extensions, find .kext bundles, add them
570 // to the vector of FileSpecs.
571 // If a .kext bundle has a Contents/PlugIns or PlugIns subdir, search for kexts in there too.
572 
573 FileSpec::EnumerateDirectoryResult
574 PlatformDarwinKernel::GetKextsInDirectory (void *baton,
575                                            FileSpec::FileType file_type,
576                                            const FileSpec &file_spec)
577 {
578     if (file_type == FileSpec::eFileTypeDirectory && file_spec.GetFileNameExtension() == ConstString("kext"))
579     {
580         ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
581         std::string kext_bundle_path = file_spec.GetPath();
582         std::string search_here_too;
583         std::string contents_plugins_path = kext_bundle_path + "/Contents/PlugIns";
584         FileSpec contents_plugins (contents_plugins_path.c_str(), false);
585         if (contents_plugins.Exists() && contents_plugins.IsDirectory())
586         {
587             search_here_too = contents_plugins_path;
588         }
589         else
590         {
591             std::string plugins_path = kext_bundle_path + "/PlugIns";
592             FileSpec plugins (plugins_path.c_str(), false);
593             if (plugins.Exists() && plugins.IsDirectory())
594             {
595                 search_here_too = plugins_path;
596             }
597         }
598 
599         if (!search_here_too.empty())
600         {
601             const bool find_directories = true;
602             const bool find_files = false;
603             const bool find_other = false;
604             FileSpec::EnumerateDirectory (search_here_too.c_str(),
605                                           find_directories,
606                                           find_files,
607                                           find_other,
608                                           GetKextsInDirectory,
609                                           baton);
610         }
611     }
612     return FileSpec::eEnumerateDirectoryResultNext;
613 }
614 
615 Error
616 PlatformDarwinKernel::GetSharedModule (const ModuleSpec &module_spec,
617                                        ModuleSP &module_sp,
618                                        const FileSpecList *module_search_paths_ptr,
619                                        ModuleSP *old_module_sp_ptr,
620                                        bool *did_create_ptr)
621 {
622     Error error;
623     module_sp.reset();
624     const FileSpec &platform_file = module_spec.GetFileSpec();
625 
626     // Treat the file's path as a kext bundle ID (e.g. "com.apple.driver.AppleIRController") and search our kext index.
627     std::string kext_bundle_id = platform_file.GetPath();
628     if (!kext_bundle_id.empty())
629     {
630         ConstString kext_bundle_cs(kext_bundle_id.c_str());
631         if (m_name_to_kext_path_map.count(kext_bundle_cs) > 0)
632         {
633             for (BundleIDToKextIterator it = m_name_to_kext_path_map.begin (); it != m_name_to_kext_path_map.end (); ++it)
634             {
635                 if (it->first == kext_bundle_cs)
636                 {
637                     error = ExamineKextForMatchingUUID (it->second, module_spec.GetUUID(), module_spec.GetArchitecture(), module_sp);
638                     if (module_sp.get())
639                     {
640                         return error;
641                     }
642                 }
643             }
644         }
645     }
646 
647     // Else fall back to treating the file's path as an actual file path - defer to PlatformDarwin's GetSharedModule.
648     return PlatformDarwin::GetSharedModule (module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
649 }
650 
651 Error
652 PlatformDarwinKernel::ExamineKextForMatchingUUID (const FileSpec &kext_bundle_path, const lldb_private::UUID &uuid, const ArchSpec &arch, ModuleSP &exe_module_sp)
653 {
654     Error error;
655     FileSpec exe_file = kext_bundle_path;
656     Host::ResolveExecutableInBundle (exe_file);
657     if (exe_file.Exists())
658     {
659         ModuleSpec exe_spec (exe_file);
660         exe_spec.GetUUID() = uuid;
661         exe_spec.GetArchitecture() = arch;
662 
663         // First try to create a ModuleSP with the file / arch and see if the UUID matches.
664         // If that fails (this exec file doesn't have the correct uuid), don't call GetSharedModule
665         // (which may call in to the DebugSymbols framework and therefore can be slow.)
666         ModuleSP module_sp (new Module (exe_file, arch));
667         if (module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec (exe_spec))
668         {
669             error = ModuleList::GetSharedModule (exe_spec, exe_module_sp, NULL, NULL, NULL);
670             if (exe_module_sp && exe_module_sp->GetObjectFile())
671             {
672                 return error;
673             }
674         }
675         exe_module_sp.reset();
676     }
677     return error;
678 }
679 
680 bool
681 PlatformDarwinKernel::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
682 {
683 #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
684     return ARMGetSupportedArchitectureAtIndex (idx, arch);
685 #else
686     return x86GetSupportedArchitectureAtIndex (idx, arch);
687 #endif
688 }
689 
690 void
691 PlatformDarwinKernel::CalculateTrapHandlerSymbolNames ()
692 {
693     m_trap_handlers.push_back(ConstString ("trap_from_kernel"));
694     m_trap_handlers.push_back(ConstString ("hndl_double_fault"));
695     m_trap_handlers.push_back(ConstString ("hndl_allintrs"));
696     m_trap_handlers.push_back(ConstString ("hndl_alltraps"));
697     m_trap_handlers.push_back(ConstString ("interrupt"));
698 }
699 
700 #else  // __APPLE__
701 
702 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
703 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name in
704 // here to avoid issues. We are tracking an internal bug to resolve this issue by
705 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to make
706 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently not
707 // compiled on other platforms due to the use of the Mac-specific
708 // source/Host/macosx/cfcpp utilities.
709 
710 lldb_private::ConstString
711 PlatformDarwinKernel::GetPluginNameStatic ()
712 {
713     static lldb_private::ConstString g_name("darwin-kernel");
714     return g_name;
715 }
716 
717 #endif // __APPLE__
718