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 becasue 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 becasue 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::arm64:
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 
375 void
376 PlatformDarwinKernel::GetiOSDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
377 {
378 }
379 
380 void
381 PlatformDarwinKernel::GetMacDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
382 {
383     FileSpec sle("/System/Library/Extensions", true);
384     if (sle.Exists() && sle.IsDirectory())
385     {
386         directories.push_back(sle);
387     }
388 
389     FileSpec le("/Library/Extensions", true);
390     if (le.Exists() && le.IsDirectory())
391     {
392         directories.push_back(le);
393     }
394 
395     FileSpec kdk("/Volumes/KernelDebugKit", true);
396     if (kdk.Exists() && kdk.IsDirectory())
397     {
398         directories.push_back(kdk);
399     }
400 }
401 
402 void
403 PlatformDarwinKernel::GetGenericDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
404 {
405     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
406     const char *developer_dir = GetDeveloperDirectory();
407     if (developer_dir == NULL)
408         developer_dir = "/Applications/Xcode.app/Contents/Developer";
409 
410     char pathbuf[PATH_MAX];
411     ::snprintf (pathbuf, sizeof (pathbuf), "%s/../Symbols", developer_dir);
412     FileSpec symbols_dir (pathbuf, true);
413     if (symbols_dir.Exists() && symbols_dir.IsDirectory())
414     {
415         directories.push_back (symbols_dir);
416     }
417 }
418 
419 void
420 PlatformDarwinKernel::GetUserSpecifiedDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
421 {
422     FileSpecList user_dirs(GetGlobalProperties()->GetKextDirectories());
423     std::vector<FileSpec> possible_sdk_dirs;
424 
425     const uint32_t user_dirs_count = user_dirs.GetSize();
426     for (uint32_t i = 0; i < user_dirs_count; i++)
427     {
428         FileSpec dir = user_dirs.GetFileSpecAtIndex (i);
429         dir.ResolvePath();
430         if (dir.Exists() && dir.IsDirectory())
431         {
432             directories.push_back (dir);
433             possible_sdk_dirs.push_back (dir);  // does this directory have a *.sdk or *.kdk that we should look in?
434 
435             // Is there a "System/Library/Extensions" subdir of this directory?
436             std::string dir_sle_path = dir.GetPath();
437             dir_sle_path.append ("/System/Library/Extensions");
438             FileSpec dir_sle(dir_sle_path.c_str(), true);
439             if (dir_sle.Exists() && dir_sle.IsDirectory())
440             {
441                 directories.push_back (dir_sle);
442             }
443         }
444     }
445 
446     SearchSDKsForKextDirectories (possible_sdk_dirs, directories);
447 }
448 
449 // Scan through the SDK directories, looking for directories where kexts are likely.
450 // Add those directories to kext_dirs.
451 void
452 PlatformDarwinKernel::SearchSDKsForKextDirectories (std::vector<lldb_private::FileSpec> sdk_dirs, std::vector<lldb_private::FileSpec> &kext_dirs)
453 {
454     const uint32_t num_sdks = sdk_dirs.size();
455     for (uint32_t i = 0; i < num_sdks; i++)
456     {
457         const FileSpec &sdk_dir = sdk_dirs[i];
458         std::string sdk_dir_path = sdk_dir.GetPath();
459         if (!sdk_dir_path.empty())
460         {
461             const bool find_directories = true;
462             const bool find_files = false;
463             const bool find_other = false;
464             FileSpec::EnumerateDirectory (sdk_dir_path.c_str(),
465                                           find_directories,
466                                           find_files,
467                                           find_other,
468                                           GetKextDirectoriesInSDK,
469                                           &kext_dirs);
470         }
471     }
472 }
473 
474 // Callback for FileSpec::EnumerateDirectory().
475 // Step through the entries in a directory like
476 //    /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
477 // looking for any subdirectories of the form MacOSX10.8.Internal.sdk/System/Library/Extensions
478 // Adds these to the vector of FileSpec's.
479 
480 FileSpec::EnumerateDirectoryResult
481 PlatformDarwinKernel::GetKextDirectoriesInSDK (void *baton,
482                                                FileSpec::FileType file_type,
483                                                const FileSpec &file_spec)
484 {
485     if (file_type == FileSpec::eFileTypeDirectory
486         && (file_spec.GetFileNameExtension() == ConstString("sdk")
487             || file_spec.GetFileNameExtension() == ConstString("kdk")))
488     {
489         std::string kext_directory_path = file_spec.GetPath();
490         kext_directory_path.append ("/System/Library/Extensions");
491         FileSpec kext_directory (kext_directory_path.c_str(), true);
492         if (kext_directory.Exists() && kext_directory.IsDirectory())
493         {
494             ((std::vector<lldb_private::FileSpec> *)baton)->push_back(kext_directory);
495         }
496     }
497     return FileSpec::eEnumerateDirectoryResultNext;
498 }
499 
500 void
501 PlatformDarwinKernel::IndexKextsInDirectories (std::vector<lldb_private::FileSpec> kext_dirs)
502 {
503     std::vector<FileSpec> kext_bundles;
504 
505     const uint32_t num_dirs = kext_dirs.size();
506     for (uint32_t i = 0; i < num_dirs; i++)
507     {
508         const FileSpec &dir = kext_dirs[i];
509         const bool find_directories = true;
510         const bool find_files = false;
511         const bool find_other = false;
512         FileSpec::EnumerateDirectory (dir.GetPath().c_str(),
513                                       find_directories,
514                                       find_files,
515                                       find_other,
516                                       GetKextsInDirectory,
517                                       &kext_bundles);
518     }
519 
520     const uint32_t num_kexts = kext_bundles.size();
521     for (uint32_t i = 0; i < num_kexts; i++)
522     {
523         const FileSpec &kext = kext_bundles[i];
524         CFCBundle bundle (kext.GetPath().c_str());
525         CFStringRef bundle_id (bundle.GetIdentifier());
526         if (bundle_id && CFGetTypeID (bundle_id) == CFStringGetTypeID ())
527         {
528             char bundle_id_buf[PATH_MAX];
529             if (CFStringGetCString (bundle_id, bundle_id_buf, sizeof (bundle_id_buf), kCFStringEncodingUTF8))
530             {
531                 ConstString bundle_conststr(bundle_id_buf);
532                 m_name_to_kext_path_map.insert(std::pair<ConstString, FileSpec>(bundle_conststr, kext));
533             }
534         }
535     }
536 }
537 
538 // Callback for FileSpec::EnumerateDirectory().
539 // Step through the entries in a directory like /System/Library/Extensions, find .kext bundles, add them
540 // to the vector of FileSpecs.
541 // If a .kext bundle has a Contents/PlugIns or PlugIns subdir, search for kexts in there too.
542 
543 FileSpec::EnumerateDirectoryResult
544 PlatformDarwinKernel::GetKextsInDirectory (void *baton,
545                                            FileSpec::FileType file_type,
546                                            const FileSpec &file_spec)
547 {
548     if (file_type == FileSpec::eFileTypeDirectory && file_spec.GetFileNameExtension() == ConstString("kext"))
549     {
550         ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
551         std::string kext_bundle_path = file_spec.GetPath();
552         std::string search_here_too;
553         std::string contents_plugins_path = kext_bundle_path + "/Contents/PlugIns";
554         FileSpec contents_plugins (contents_plugins_path.c_str(), false);
555         if (contents_plugins.Exists() && contents_plugins.IsDirectory())
556         {
557             search_here_too = contents_plugins_path;
558         }
559         else
560         {
561             std::string plugins_path = kext_bundle_path + "/PlugIns";
562             FileSpec plugins (plugins_path.c_str(), false);
563             if (plugins.Exists() && plugins.IsDirectory())
564             {
565                 search_here_too = plugins_path;
566             }
567         }
568 
569         if (!search_here_too.empty())
570         {
571             const bool find_directories = true;
572             const bool find_files = false;
573             const bool find_other = false;
574             FileSpec::EnumerateDirectory (search_here_too.c_str(),
575                                           find_directories,
576                                           find_files,
577                                           find_other,
578                                           GetKextsInDirectory,
579                                           baton);
580         }
581     }
582     return FileSpec::eEnumerateDirectoryResultNext;
583 }
584 
585 Error
586 PlatformDarwinKernel::GetSharedModule (const ModuleSpec &module_spec,
587                                        ModuleSP &module_sp,
588                                        const FileSpecList *module_search_paths_ptr,
589                                        ModuleSP *old_module_sp_ptr,
590                                        bool *did_create_ptr)
591 {
592     Error error;
593     module_sp.reset();
594     const FileSpec &platform_file = module_spec.GetFileSpec();
595 
596     // Treat the file's path as a kext bundle ID (e.g. "com.apple.driver.AppleIRController") and search our kext index.
597     std::string kext_bundle_id = platform_file.GetPath();
598     if (!kext_bundle_id.empty())
599     {
600         ConstString kext_bundle_cs(kext_bundle_id.c_str());
601         if (m_name_to_kext_path_map.count(kext_bundle_cs) > 0)
602         {
603             for (BundleIDToKextIterator it = m_name_to_kext_path_map.begin (); it != m_name_to_kext_path_map.end (); ++it)
604             {
605                 if (it->first == kext_bundle_cs)
606                 {
607                     error = ExamineKextForMatchingUUID (it->second, module_spec.GetUUID(), module_spec.GetArchitecture(), module_sp);
608                     if (module_sp.get())
609                     {
610                         return error;
611                     }
612                 }
613             }
614         }
615     }
616 
617     // Else fall back to treating the file's path as an actual file path - defer to PlatformDarwin's GetSharedModule.
618     return PlatformDarwin::GetSharedModule (module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
619 }
620 
621 Error
622 PlatformDarwinKernel::ExamineKextForMatchingUUID (const FileSpec &kext_bundle_path, const lldb_private::UUID &uuid, const ArchSpec &arch, ModuleSP &exe_module_sp)
623 {
624     Error error;
625     FileSpec exe_file = kext_bundle_path;
626     Host::ResolveExecutableInBundle (exe_file);
627     if (exe_file.Exists())
628     {
629         ModuleSpec exe_spec (exe_file);
630         exe_spec.GetUUID() = uuid;
631         exe_spec.GetArchitecture() = arch;
632 
633         // First try to create a ModuleSP with the file / arch and see if the UUID matches.
634         // If that fails (this exec file doesn't have the correct uuid), don't call GetSharedModule
635         // (which may call in to the DebugSymbols framework and therefore can be slow.)
636         ModuleSP module_sp (new Module (exe_file, arch));
637         if (module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec (exe_spec))
638         {
639             error = ModuleList::GetSharedModule (exe_spec, exe_module_sp, NULL, NULL, NULL);
640             if (exe_module_sp && exe_module_sp->GetObjectFile())
641             {
642                 return error;
643             }
644         }
645         exe_module_sp.reset();
646     }
647     return error;
648 }
649 
650 bool
651 PlatformDarwinKernel::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
652 {
653 #if defined (__arm__) || defined (__arm64__)
654     return ARMGetSupportedArchitectureAtIndex (idx, arch);
655 #else
656     return x86GetSupportedArchitectureAtIndex (idx, arch);
657 #endif
658 }
659 
660 void
661 PlatformDarwinKernel::CalculateTrapHandlerSymbolNames ()
662 {
663     m_trap_handlers.push_back(ConstString ("trap_from_kernel"));
664     m_trap_handlers.push_back(ConstString ("hndl_double_fault"));
665     m_trap_handlers.push_back(ConstString ("hndl_allintrs"));
666     m_trap_handlers.push_back(ConstString ("hndl_alltraps"));
667     m_trap_handlers.push_back(ConstString ("interrupt"));
668 }
669 
670 #else  // __APPLE__
671 
672 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
673 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name in
674 // here to avoid issues. We are tracking an internal bug to resolve this issue by
675 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to make
676 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently not
677 // compiled on other platforms due to the use of the Mac-specific
678 // source/Host/macosx/cfcpp utilities.
679 
680 lldb_private::ConstString
681 PlatformDarwinKernel::GetPluginNameStatic ()
682 {
683     static lldb_private::ConstString g_name("darwin-kernel");
684     return g_name;
685 }
686 
687 #endif // __APPLE__
688