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
Initialize()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
Terminate()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*
CreateInstance(bool force,const ArchSpec * arch)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::thumb:
137             is_ios_debug_session = eLazyBoolYes;
138             break;
139         default:
140             is_ios_debug_session = eLazyBoolCalculate;
141             break;
142         }
143     }
144     if (create)
145         return new PlatformDarwinKernel (is_ios_debug_session);
146     return NULL;
147 }
148 
149 
150 lldb_private::ConstString
GetPluginNameStatic()151 PlatformDarwinKernel::GetPluginNameStatic ()
152 {
153     static ConstString g_name("darwin-kernel");
154     return g_name;
155 }
156 
157 const char *
GetDescriptionStatic()158 PlatformDarwinKernel::GetDescriptionStatic()
159 {
160     return "Darwin Kernel platform plug-in.";
161 }
162 
163 //------------------------------------------------------------------
164 /// Code to handle the PlatformDarwinKernel settings
165 //------------------------------------------------------------------
166 
167 static PropertyDefinition
168 g_properties[] =
169 {
170     { "search-locally-for-kexts" , OptionValue::eTypeBoolean,      true, true, NULL, NULL, "Automatically search for kexts on the local system when doing kernel debugging." },
171     { "kext-directories",          OptionValue::eTypeFileSpecList, false, 0,   NULL, NULL, "Directories/KDKs to search for kexts in when starting a kernel debug session." },
172     {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
173 };
174 
175 enum {
176     ePropertySearchForKexts = 0,
177     ePropertyKextDirectories
178 };
179 
180 
181 
182 class PlatformDarwinKernelProperties : public Properties
183 {
184 public:
185 
186     static ConstString &
GetSettingName()187     GetSettingName ()
188     {
189         static ConstString g_setting_name("darwin-kernel");
190         return g_setting_name;
191     }
192 
PlatformDarwinKernelProperties()193     PlatformDarwinKernelProperties() :
194         Properties ()
195     {
196         m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
197         m_collection_sp->Initialize(g_properties);
198     }
199 
200     virtual
~PlatformDarwinKernelProperties()201     ~PlatformDarwinKernelProperties()
202     {
203     }
204 
205     bool
GetSearchForKexts() const206     GetSearchForKexts() const
207     {
208         const uint32_t idx = ePropertySearchForKexts;
209         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
210     }
211 
212     FileSpecList &
GetKextDirectories() const213     GetKextDirectories() const
214     {
215         const uint32_t idx = ePropertyKextDirectories;
216         OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
217         assert(option_value);
218         return option_value->GetCurrentValue();
219     }
220 };
221 
222 typedef std::shared_ptr<PlatformDarwinKernelProperties> PlatformDarwinKernelPropertiesSP;
223 
224 static const PlatformDarwinKernelPropertiesSP &
GetGlobalProperties()225 GetGlobalProperties()
226 {
227     static PlatformDarwinKernelPropertiesSP g_settings_sp;
228     if (!g_settings_sp)
229         g_settings_sp.reset (new PlatformDarwinKernelProperties ());
230     return g_settings_sp;
231 }
232 
233 void
DebuggerInitialize(lldb_private::Debugger & debugger)234 PlatformDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
235 {
236     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformDarwinKernelProperties::GetSettingName()))
237     {
238         const bool is_global_setting = true;
239         PluginManager::CreateSettingForPlatformPlugin (debugger,
240                                                             GetGlobalProperties()->GetValueProperties(),
241                                                             ConstString ("Properties for the PlatformDarwinKernel plug-in."),
242                                                             is_global_setting);
243     }
244 }
245 
246 //------------------------------------------------------------------
247 /// Default Constructor
248 //------------------------------------------------------------------
PlatformDarwinKernel(lldb_private::LazyBool is_ios_debug_session)249 PlatformDarwinKernel::PlatformDarwinKernel (lldb_private::LazyBool is_ios_debug_session) :
250     PlatformDarwin (false),    // This is a remote platform
251     m_name_to_kext_path_map(),
252     m_directories_searched(),
253     m_ios_debug_session(is_ios_debug_session)
254 
255 {
256     if (GetGlobalProperties()->GetSearchForKexts())
257     {
258         SearchForKexts ();
259     }
260 }
261 
262 //------------------------------------------------------------------
263 /// Destructor.
264 ///
265 /// The destructor is virtual since this class is designed to be
266 /// inherited from by the plug-in instance.
267 //------------------------------------------------------------------
~PlatformDarwinKernel()268 PlatformDarwinKernel::~PlatformDarwinKernel()
269 {
270 }
271 
272 
273 void
GetStatus(Stream & strm)274 PlatformDarwinKernel::GetStatus (Stream &strm)
275 {
276     Platform::GetStatus (strm);
277     strm.Printf (" Debug session type: ");
278     if (m_ios_debug_session == eLazyBoolYes)
279         strm.Printf ("iOS kernel debugging\n");
280     else if (m_ios_debug_session == eLazyBoolNo)
281         strm.Printf ("Mac OS X kernel debugging\n");
282     else
283             strm.Printf ("unknown kernel debugging\n");
284     const uint32_t num_kext_dirs = m_directories_searched.size();
285     for (uint32_t i=0; i<num_kext_dirs; ++i)
286     {
287         const FileSpec &kext_dir = m_directories_searched[i];
288         strm.Printf (" Kext directories: [%2u] \"%s\"\n", i, kext_dir.GetPath().c_str());
289     }
290     strm.Printf (" Total number of kexts indexed: %d\n", (int) m_name_to_kext_path_map.size());
291 }
292 
293 void
SearchForKexts()294 PlatformDarwinKernel::SearchForKexts ()
295 {
296     // Differentiate between "ios debug session" and "mac debug session" so we don't index
297     // kext bundles that won't be used in this debug session.  If this is an ios kext debug
298     // session, looking in /System/Library/Extensions is a waste of stat()s, for example.
299 
300     // Build up a list of all SDKs we'll be searching for directories of kexts
301     // e.g. /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk
302     std::vector<FileSpec> sdk_dirs;
303     if (m_ios_debug_session != eLazyBoolNo)
304         GetiOSSDKDirectoriesToSearch (sdk_dirs);
305     if (m_ios_debug_session != eLazyBoolYes)
306         GetMacSDKDirectoriesToSearch (sdk_dirs);
307 
308     GetGenericSDKDirectoriesToSearch (sdk_dirs);
309 
310     // Build up a list of directories that hold kext bundles on the system
311     // e.g. /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk/System/Library/Extensions
312     std::vector<FileSpec> kext_dirs;
313     SearchSDKsForKextDirectories (sdk_dirs, kext_dirs);
314 
315     if (m_ios_debug_session != eLazyBoolNo)
316         GetiOSDirectoriesToSearch (kext_dirs);
317     if (m_ios_debug_session != eLazyBoolYes)
318         GetMacDirectoriesToSearch (kext_dirs);
319 
320     GetGenericDirectoriesToSearch (kext_dirs);
321 
322     GetUserSpecifiedDirectoriesToSearch (kext_dirs);
323 
324     // We now have a complete list of directories that we will search for kext bundles
325     m_directories_searched = kext_dirs;
326 
327     IndexKextsInDirectories (kext_dirs);
328 }
329 
330 void
GetiOSSDKDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)331 PlatformDarwinKernel::GetiOSSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
332 {
333     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
334     const char *developer_dir = GetDeveloperDirectory();
335     if (developer_dir == NULL)
336         developer_dir = "/Applications/Xcode.app/Contents/Developer";
337 
338     char pathbuf[PATH_MAX];
339     ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/iPhoneOS.platform/Developer/SDKs", developer_dir);
340     FileSpec ios_sdk(pathbuf, true);
341     if (ios_sdk.Exists() && ios_sdk.IsDirectory())
342     {
343         directories.push_back (ios_sdk);
344     }
345 }
346 
347 void
GetMacSDKDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)348 PlatformDarwinKernel::GetMacSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
349 {
350     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
351     const char *developer_dir = GetDeveloperDirectory();
352     if (developer_dir == NULL)
353         developer_dir = "/Applications/Xcode.app/Contents/Developer";
354 
355     char pathbuf[PATH_MAX];
356     ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/MacOSX.platform/Developer/SDKs", developer_dir);
357     FileSpec mac_sdk(pathbuf, true);
358     if (mac_sdk.Exists() && mac_sdk.IsDirectory())
359     {
360         directories.push_back (mac_sdk);
361     }
362 }
363 
364 void
GetGenericSDKDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)365 PlatformDarwinKernel::GetGenericSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
366 {
367     FileSpec generic_sdk("/AppleInternal/Developer/KDKs", true);
368     if (generic_sdk.Exists() && generic_sdk.IsDirectory())
369     {
370         directories.push_back (generic_sdk);
371     }
372 }
373 
374 void
GetiOSDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)375 PlatformDarwinKernel::GetiOSDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
376 {
377 }
378 
379 void
GetMacDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)380 PlatformDarwinKernel::GetMacDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
381 {
382     FileSpec sle("/System/Library/Extensions", true);
383     if (sle.Exists() && sle.IsDirectory())
384     {
385         directories.push_back(sle);
386     }
387 
388     FileSpec le("/Library/Extensions", true);
389     if (le.Exists() && le.IsDirectory())
390     {
391         directories.push_back(le);
392     }
393 
394     FileSpec kdk("/Volumes/KernelDebugKit", true);
395     if (kdk.Exists() && kdk.IsDirectory())
396     {
397         directories.push_back(kdk);
398     }
399 }
400 
401 void
GetGenericDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)402 PlatformDarwinKernel::GetGenericDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
403 {
404     // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
405     const char *developer_dir = GetDeveloperDirectory();
406     if (developer_dir == NULL)
407         developer_dir = "/Applications/Xcode.app/Contents/Developer";
408 
409     char pathbuf[PATH_MAX];
410     ::snprintf (pathbuf, sizeof (pathbuf), "%s/../Symbols", developer_dir);
411     FileSpec symbols_dir (pathbuf, true);
412     if (symbols_dir.Exists() && symbols_dir.IsDirectory())
413     {
414         directories.push_back (symbols_dir);
415     }
416 }
417 
418 void
GetUserSpecifiedDirectoriesToSearch(std::vector<lldb_private::FileSpec> & directories)419 PlatformDarwinKernel::GetUserSpecifiedDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
420 {
421     FileSpecList user_dirs(GetGlobalProperties()->GetKextDirectories());
422     std::vector<FileSpec> possible_sdk_dirs;
423 
424     const uint32_t user_dirs_count = user_dirs.GetSize();
425     for (uint32_t i = 0; i < user_dirs_count; i++)
426     {
427         FileSpec dir = user_dirs.GetFileSpecAtIndex (i);
428         dir.ResolvePath();
429         if (dir.Exists() && dir.IsDirectory())
430         {
431             directories.push_back (dir);
432             possible_sdk_dirs.push_back (dir);  // does this directory have a *.sdk or *.kdk that we should look in?
433 
434             // Is there a "System/Library/Extensions" subdir of this directory?
435             std::string dir_sle_path = dir.GetPath();
436             dir_sle_path.append ("/System/Library/Extensions");
437             FileSpec dir_sle(dir_sle_path.c_str(), true);
438             if (dir_sle.Exists() && dir_sle.IsDirectory())
439             {
440                 directories.push_back (dir_sle);
441             }
442         }
443     }
444 
445     SearchSDKsForKextDirectories (possible_sdk_dirs, directories);
446 }
447 
448 // Scan through the SDK directories, looking for directories where kexts are likely.
449 // Add those directories to kext_dirs.
450 void
SearchSDKsForKextDirectories(std::vector<lldb_private::FileSpec> sdk_dirs,std::vector<lldb_private::FileSpec> & kext_dirs)451 PlatformDarwinKernel::SearchSDKsForKextDirectories (std::vector<lldb_private::FileSpec> sdk_dirs, std::vector<lldb_private::FileSpec> &kext_dirs)
452 {
453     const uint32_t num_sdks = sdk_dirs.size();
454     for (uint32_t i = 0; i < num_sdks; i++)
455     {
456         const FileSpec &sdk_dir = sdk_dirs[i];
457         std::string sdk_dir_path = sdk_dir.GetPath();
458         if (!sdk_dir_path.empty())
459         {
460             const bool find_directories = true;
461             const bool find_files = false;
462             const bool find_other = false;
463             FileSpec::EnumerateDirectory (sdk_dir_path.c_str(),
464                                           find_directories,
465                                           find_files,
466                                           find_other,
467                                           GetKextDirectoriesInSDK,
468                                           &kext_dirs);
469         }
470     }
471 }
472 
473 // Callback for FileSpec::EnumerateDirectory().
474 // Step through the entries in a directory like
475 //    /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
476 // looking for any subdirectories of the form MacOSX10.8.Internal.sdk/System/Library/Extensions
477 // Adds these to the vector of FileSpec's.
478 
479 FileSpec::EnumerateDirectoryResult
GetKextDirectoriesInSDK(void * baton,FileSpec::FileType file_type,const FileSpec & file_spec)480 PlatformDarwinKernel::GetKextDirectoriesInSDK (void *baton,
481                                                FileSpec::FileType file_type,
482                                                const FileSpec &file_spec)
483 {
484     if (file_type == FileSpec::eFileTypeDirectory
485         && (file_spec.GetFileNameExtension() == ConstString("sdk")
486             || file_spec.GetFileNameExtension() == ConstString("kdk")))
487     {
488         std::string kext_directory_path = file_spec.GetPath();
489         kext_directory_path.append ("/System/Library/Extensions");
490         FileSpec kext_directory (kext_directory_path.c_str(), true);
491         if (kext_directory.Exists() && kext_directory.IsDirectory())
492         {
493             ((std::vector<lldb_private::FileSpec> *)baton)->push_back(kext_directory);
494         }
495     }
496     return FileSpec::eEnumerateDirectoryResultNext;
497 }
498 
499 void
IndexKextsInDirectories(std::vector<lldb_private::FileSpec> kext_dirs)500 PlatformDarwinKernel::IndexKextsInDirectories (std::vector<lldb_private::FileSpec> kext_dirs)
501 {
502     std::vector<FileSpec> kext_bundles;
503 
504     const uint32_t num_dirs = kext_dirs.size();
505     for (uint32_t i = 0; i < num_dirs; i++)
506     {
507         const FileSpec &dir = kext_dirs[i];
508         const bool find_directories = true;
509         const bool find_files = false;
510         const bool find_other = false;
511         FileSpec::EnumerateDirectory (dir.GetPath().c_str(),
512                                       find_directories,
513                                       find_files,
514                                       find_other,
515                                       GetKextsInDirectory,
516                                       &kext_bundles);
517     }
518 
519     const uint32_t num_kexts = kext_bundles.size();
520     for (uint32_t i = 0; i < num_kexts; i++)
521     {
522         const FileSpec &kext = kext_bundles[i];
523         CFCBundle bundle (kext.GetPath().c_str());
524         CFStringRef bundle_id (bundle.GetIdentifier());
525         if (bundle_id && CFGetTypeID (bundle_id) == CFStringGetTypeID ())
526         {
527             char bundle_id_buf[PATH_MAX];
528             if (CFStringGetCString (bundle_id, bundle_id_buf, sizeof (bundle_id_buf), kCFStringEncodingUTF8))
529             {
530                 ConstString bundle_conststr(bundle_id_buf);
531                 m_name_to_kext_path_map.insert(std::pair<ConstString, FileSpec>(bundle_conststr, kext));
532             }
533         }
534     }
535 }
536 
537 // Callback for FileSpec::EnumerateDirectory().
538 // Step through the entries in a directory like /System/Library/Extensions, find .kext bundles, add them
539 // to the vector of FileSpecs.
540 // If a .kext bundle has a Contents/PlugIns or PlugIns subdir, search for kexts in there too.
541 
542 FileSpec::EnumerateDirectoryResult
GetKextsInDirectory(void * baton,FileSpec::FileType file_type,const FileSpec & file_spec)543 PlatformDarwinKernel::GetKextsInDirectory (void *baton,
544                                            FileSpec::FileType file_type,
545                                            const FileSpec &file_spec)
546 {
547     if (file_type == FileSpec::eFileTypeDirectory && file_spec.GetFileNameExtension() == ConstString("kext"))
548     {
549         ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
550         std::string kext_bundle_path = file_spec.GetPath();
551         std::string search_here_too;
552         std::string contents_plugins_path = kext_bundle_path + "/Contents/PlugIns";
553         FileSpec contents_plugins (contents_plugins_path.c_str(), false);
554         if (contents_plugins.Exists() && contents_plugins.IsDirectory())
555         {
556             search_here_too = contents_plugins_path;
557         }
558         else
559         {
560             std::string plugins_path = kext_bundle_path + "/PlugIns";
561             FileSpec plugins (plugins_path.c_str(), false);
562             if (plugins.Exists() && plugins.IsDirectory())
563             {
564                 search_here_too = plugins_path;
565             }
566         }
567 
568         if (!search_here_too.empty())
569         {
570             const bool find_directories = true;
571             const bool find_files = false;
572             const bool find_other = false;
573             FileSpec::EnumerateDirectory (search_here_too.c_str(),
574                                           find_directories,
575                                           find_files,
576                                           find_other,
577                                           GetKextsInDirectory,
578                                           baton);
579         }
580     }
581     return FileSpec::eEnumerateDirectoryResultNext;
582 }
583 
584 Error
GetSharedModule(const ModuleSpec & module_spec,ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr,ModuleSP * old_module_sp_ptr,bool * did_create_ptr)585 PlatformDarwinKernel::GetSharedModule (const ModuleSpec &module_spec,
586                                        ModuleSP &module_sp,
587                                        const FileSpecList *module_search_paths_ptr,
588                                        ModuleSP *old_module_sp_ptr,
589                                        bool *did_create_ptr)
590 {
591     Error error;
592     module_sp.reset();
593     const FileSpec &platform_file = module_spec.GetFileSpec();
594 
595     // Treat the file's path as a kext bundle ID (e.g. "com.apple.driver.AppleIRController") and search our kext index.
596     std::string kext_bundle_id = platform_file.GetPath();
597     if (!kext_bundle_id.empty())
598     {
599         ConstString kext_bundle_cs(kext_bundle_id.c_str());
600         if (m_name_to_kext_path_map.count(kext_bundle_cs) > 0)
601         {
602             for (BundleIDToKextIterator it = m_name_to_kext_path_map.begin (); it != m_name_to_kext_path_map.end (); ++it)
603             {
604                 if (it->first == kext_bundle_cs)
605                 {
606                     error = ExamineKextForMatchingUUID (it->second, module_spec.GetUUID(), module_spec.GetArchitecture(), module_sp);
607                     if (module_sp.get())
608                     {
609                         return error;
610                     }
611                 }
612             }
613         }
614     }
615 
616     // Else fall back to treating the file's path as an actual file path - defer to PlatformDarwin's GetSharedModule.
617     return PlatformDarwin::GetSharedModule (module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
618 }
619 
620 Error
ExamineKextForMatchingUUID(const FileSpec & kext_bundle_path,const lldb_private::UUID & uuid,const ArchSpec & arch,ModuleSP & exe_module_sp)621 PlatformDarwinKernel::ExamineKextForMatchingUUID (const FileSpec &kext_bundle_path, const lldb_private::UUID &uuid, const ArchSpec &arch, ModuleSP &exe_module_sp)
622 {
623     Error error;
624     FileSpec exe_file = kext_bundle_path;
625     Host::ResolveExecutableInBundle (exe_file);
626     if (exe_file.Exists())
627     {
628         ModuleSpec exe_spec (exe_file);
629         exe_spec.GetUUID() = uuid;
630         exe_spec.GetArchitecture() = arch;
631 
632         // First try to create a ModuleSP with the file / arch and see if the UUID matches.
633         // If that fails (this exec file doesn't have the correct uuid), don't call GetSharedModule
634         // (which may call in to the DebugSymbols framework and therefore can be slow.)
635         ModuleSP module_sp (new Module (exe_file, arch));
636         if (module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec (exe_spec))
637         {
638             error = ModuleList::GetSharedModule (exe_spec, exe_module_sp, NULL, NULL, NULL);
639             if (exe_module_sp && exe_module_sp->GetObjectFile())
640             {
641                 return error;
642             }
643         }
644         exe_module_sp.reset();
645     }
646     return error;
647 }
648 
649 bool
GetSupportedArchitectureAtIndex(uint32_t idx,ArchSpec & arch)650 PlatformDarwinKernel::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
651 {
652 #if defined (__arm__)
653     return ARMGetSupportedArchitectureAtIndex (idx, arch);
654 #else
655     return x86GetSupportedArchitectureAtIndex (idx, arch);
656 #endif
657 }
658 
659 #else  // __APPLE__
660 
661 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
662 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name in
663 // here to avoid issues. We are tracking an internal bug to resolve this issue by
664 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to make
665 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently not
666 // compiled on other platforms due to the use of the Mac-specific
667 // source/Host/macosx/cfcpp utilities.
668 
669 lldb_private::ConstString
GetPluginNameStatic()670 PlatformDarwinKernel::GetPluginNameStatic ()
671 {
672     static lldb_private::ConstString g_name("darwin-kernel");
673     return g_name;
674 }
675 
676 
677 #endif // __APPLE__
678