1 //===-- PlatformDarwinKernel.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformDarwinKernel.h"
12 #if defined (__APPLE__) // This Plugin uses the Mac-specific source/Host/macosx/cfcpp utilities
17 // Other libraries and framework includes
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/ArchSpec.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/ModuleList.h"
25 #include "lldb/Core/ModuleSpec.h"
26 #include "lldb/Core/PluginManager.h"
27 #include "lldb/Core/StreamString.h"
28 #include "lldb/Host/FileSpec.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Interpreter/OptionValueFileSpecList.h"
31 #include "lldb/Interpreter/OptionValueProperties.h"
32 #include "lldb/Interpreter/Property.h"
33 #include "lldb/Target/Platform.h"
34 #include "lldb/Target/Process.h"
35 #include "lldb/Target/Target.h"
37 #include <CoreFoundation/CoreFoundation.h>
39 #include "Host/macosx/cfcpp/CFCBundle.h"
42 using namespace lldb_private;
44 //------------------------------------------------------------------
46 //------------------------------------------------------------------
47 static uint32_t g_initialize_count = 0;
49 //------------------------------------------------------------------
51 //------------------------------------------------------------------
53 PlatformDarwinKernel::Initialize ()
55 PlatformDarwin::Initialize ();
57 if (g_initialize_count++ == 0)
59 PluginManager::RegisterPlugin (PlatformDarwinKernel::GetPluginNameStatic(),
60 PlatformDarwinKernel::GetDescriptionStatic(),
61 PlatformDarwinKernel::CreateInstance,
62 PlatformDarwinKernel::DebuggerInitialize);
67 PlatformDarwinKernel::Terminate ()
69 if (g_initialize_count > 0)
71 if (--g_initialize_count == 0)
73 PluginManager::UnregisterPlugin (PlatformDarwinKernel::CreateInstance);
77 PlatformDarwin::Terminate ();
81 PlatformDarwinKernel::CreateInstance (bool force, const ArchSpec *arch)
83 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
86 const char *arch_name;
87 if (arch && arch->GetArchitectureName ())
88 arch_name = arch->GetArchitectureName ();
92 const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
94 log->Printf ("PlatformDarwinKernel::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
97 // This is a special plugin that we don't want to activate just based on an ArchSpec for normal
98 // userland debugging. It is only useful in kernel debug sessions and the DynamicLoaderDarwinPlugin
99 // (or a user doing 'platform select') will force the creation of this Platform plugin.
103 log->Printf ("PlatformDarwinKernel::%s() aborting creation of platform because force == false", __FUNCTION__);
108 LazyBool is_ios_debug_session = eLazyBoolCalculate;
110 if (create == false && arch && arch->IsValid())
112 const llvm::Triple &triple = arch->GetTriple();
113 switch (triple.getVendor())
115 case llvm::Triple::Apple:
119 // Only accept "unknown" for vendor if the host is Apple and
120 // it "unknown" wasn't specified (it was just returned because it
121 // was NOT specified)
122 case llvm::Triple::UnknownArch:
123 create = !arch->TripleVendorWasSpecified();
131 switch (triple.getOS())
133 case llvm::Triple::Darwin:
134 case llvm::Triple::MacOSX:
135 case llvm::Triple::IOS:
136 case llvm::Triple::WatchOS:
137 case llvm::Triple::TvOS:
139 // Only accept "vendor" for vendor if the host is Apple and
140 // it "unknown" wasn't specified (it was just returned because it
141 // was NOT specified)
142 case llvm::Triple::UnknownOS:
143 create = !arch->TripleOSWasSpecified();
151 if (arch && arch->IsValid())
153 switch (arch->GetMachine())
155 case llvm::Triple::x86:
156 case llvm::Triple::x86_64:
157 case llvm::Triple::ppc:
158 case llvm::Triple::ppc64:
159 is_ios_debug_session = eLazyBoolNo;
161 case llvm::Triple::arm:
162 case llvm::Triple::aarch64:
163 case llvm::Triple::thumb:
164 is_ios_debug_session = eLazyBoolYes;
167 is_ios_debug_session = eLazyBoolCalculate;
174 log->Printf ("PlatformDarwinKernel::%s() creating platform", __FUNCTION__);
176 return PlatformSP(new PlatformDarwinKernel (is_ios_debug_session));
180 log->Printf ("PlatformDarwinKernel::%s() aborting creation of platform", __FUNCTION__);
186 lldb_private::ConstString
187 PlatformDarwinKernel::GetPluginNameStatic ()
189 static ConstString g_name("darwin-kernel");
194 PlatformDarwinKernel::GetDescriptionStatic()
196 return "Darwin Kernel platform plug-in.";
199 //------------------------------------------------------------------
200 /// Code to handle the PlatformDarwinKernel settings
201 //------------------------------------------------------------------
203 static PropertyDefinition
206 { "search-locally-for-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically search for kexts on the local system when doing kernel debugging." },
207 { "kext-directories", OptionValue::eTypeFileSpecList, false, 0, NULL, NULL, "Directories/KDKs to search for kexts in when starting a kernel debug session." },
208 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
212 ePropertySearchForKexts = 0,
213 ePropertyKextDirectories
218 class PlatformDarwinKernelProperties : public Properties
225 static ConstString g_setting_name("darwin-kernel");
226 return g_setting_name;
229 PlatformDarwinKernelProperties() :
232 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
233 m_collection_sp->Initialize(g_properties);
237 ~PlatformDarwinKernelProperties()
242 GetSearchForKexts() const
244 const uint32_t idx = ePropertySearchForKexts;
245 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
249 GetKextDirectories() const
251 const uint32_t idx = ePropertyKextDirectories;
252 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
253 assert(option_value);
254 return option_value->GetCurrentValue();
258 typedef std::shared_ptr<PlatformDarwinKernelProperties> PlatformDarwinKernelPropertiesSP;
260 static const PlatformDarwinKernelPropertiesSP &
261 GetGlobalProperties()
263 static PlatformDarwinKernelPropertiesSP g_settings_sp;
265 g_settings_sp.reset (new PlatformDarwinKernelProperties ());
266 return g_settings_sp;
270 PlatformDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
272 if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformDarwinKernelProperties::GetSettingName()))
274 const bool is_global_setting = true;
275 PluginManager::CreateSettingForPlatformPlugin (debugger,
276 GetGlobalProperties()->GetValueProperties(),
277 ConstString ("Properties for the PlatformDarwinKernel plug-in."),
282 //------------------------------------------------------------------
283 /// Default Constructor
284 //------------------------------------------------------------------
285 PlatformDarwinKernel::PlatformDarwinKernel (lldb_private::LazyBool is_ios_debug_session) :
286 PlatformDarwin (false), // This is a remote platform
287 m_name_to_kext_path_map(),
288 m_search_directories(),
290 m_ios_debug_session(is_ios_debug_session)
293 if (GetGlobalProperties()->GetSearchForKexts())
295 CollectKextAndKernelDirectories ();
296 IndexKextsInDirectories ();
297 IndexKernelsInDirectories ();
301 //------------------------------------------------------------------
304 /// The destructor is virtual since this class is designed to be
305 /// inherited from by the plug-in instance.
306 //------------------------------------------------------------------
307 PlatformDarwinKernel::~PlatformDarwinKernel()
313 PlatformDarwinKernel::GetStatus (Stream &strm)
315 Platform::GetStatus (strm);
316 strm.Printf (" Debug session type: ");
317 if (m_ios_debug_session == eLazyBoolYes)
318 strm.Printf ("iOS kernel debugging\n");
319 else if (m_ios_debug_session == eLazyBoolNo)
320 strm.Printf ("Mac OS X kernel debugging\n");
322 strm.Printf ("unknown kernel debugging\n");
323 const uint32_t num_kext_dirs = m_search_directories.size();
324 for (uint32_t i=0; i<num_kext_dirs; ++i)
326 const FileSpec &kext_dir = m_search_directories[i];
327 strm.Printf (" Kext directories: [%2u] \"%s\"\n", i, kext_dir.GetPath().c_str());
329 strm.Printf (" Total number of kexts indexed: %d\n", (int) m_name_to_kext_path_map.size());
332 // Populate the m_search_directories vector with directories we should search
333 // for kernel & kext binaries.
336 PlatformDarwinKernel::CollectKextAndKernelDirectories ()
338 // Differentiate between "ios debug session" and "mac debug session" so we don't index
339 // kext bundles that won't be used in this debug session. If this is an ios kext debug
340 // session, looking in /System/Library/Extensions is a waste of stat()s, for example.
342 // Build up a list of all SDKs we'll be searching for directories of kexts/kernels
343 // e.g. /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk
344 std::vector<FileSpec> sdk_dirs;
345 if (m_ios_debug_session != eLazyBoolNo)
347 GetiOSSDKDirectoriesToSearch (sdk_dirs);
348 GetAppleTVOSSDKDirectoriesToSearch (sdk_dirs);
349 GetWatchOSSDKDirectoriesToSearch (sdk_dirs);
351 if (m_ios_debug_session != eLazyBoolYes)
352 GetMacSDKDirectoriesToSearch (sdk_dirs);
354 GetGenericSDKDirectoriesToSearch (sdk_dirs);
356 // Build up a list of directories that hold may kext bundles & kernels
358 // e.g. given /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/
360 // /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk/
362 // /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.Internal.sdk/System/Library/Extensions
364 std::vector<FileSpec> kext_dirs;
365 SearchSDKsForKextDirectories (sdk_dirs, kext_dirs);
367 if (m_ios_debug_session != eLazyBoolNo)
368 GetiOSDirectoriesToSearch (kext_dirs);
369 if (m_ios_debug_session != eLazyBoolYes)
370 GetMacDirectoriesToSearch (kext_dirs);
372 GetGenericDirectoriesToSearch (kext_dirs);
374 GetUserSpecifiedDirectoriesToSearch (kext_dirs);
376 GetKernelDirectoriesToSearch (kext_dirs);
378 GetCurrentDirectoryToSearch (kext_dirs);
380 // We now have a complete list of directories that we will search for kext bundles
381 m_search_directories = kext_dirs;
385 PlatformDarwinKernel::GetiOSSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
387 // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
388 const char *developer_dir = GetDeveloperDirectory();
389 if (developer_dir == NULL)
390 developer_dir = "/Applications/Xcode.app/Contents/Developer";
392 char pathbuf[PATH_MAX];
393 ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/iPhoneOS.platform/Developer/SDKs", developer_dir);
394 FileSpec ios_sdk(pathbuf, true);
395 if (ios_sdk.Exists() && ios_sdk.IsDirectory())
397 directories.push_back (ios_sdk);
402 PlatformDarwinKernel::GetAppleTVOSSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
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";
409 char pathbuf[PATH_MAX];
410 ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/AppleTVOS.platform/Developer/SDKs", developer_dir);
411 FileSpec ios_sdk(pathbuf, true);
412 if (ios_sdk.Exists() && ios_sdk.IsDirectory())
414 directories.push_back (ios_sdk);
419 PlatformDarwinKernel::GetWatchOSSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
421 // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
422 const char *developer_dir = GetDeveloperDirectory();
423 if (developer_dir == NULL)
424 developer_dir = "/Applications/Xcode.app/Contents/Developer";
426 char pathbuf[PATH_MAX];
427 ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/watchOS.platform/Developer/SDKs", developer_dir);
428 FileSpec ios_sdk(pathbuf, true);
429 if (ios_sdk.Exists() && ios_sdk.IsDirectory())
431 directories.push_back (ios_sdk);
435 ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/WatchOS.platform/Developer/SDKs", developer_dir);
436 FileSpec alt_watch_sdk(pathbuf, true);
437 if (ios_sdk.Exists() && ios_sdk.IsDirectory())
439 directories.push_back (ios_sdk);
446 PlatformDarwinKernel::GetMacSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
448 // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
449 const char *developer_dir = GetDeveloperDirectory();
450 if (developer_dir == NULL)
451 developer_dir = "/Applications/Xcode.app/Contents/Developer";
453 char pathbuf[PATH_MAX];
454 ::snprintf (pathbuf, sizeof (pathbuf), "%s/Platforms/MacOSX.platform/Developer/SDKs", developer_dir);
455 FileSpec mac_sdk(pathbuf, true);
456 if (mac_sdk.Exists() && mac_sdk.IsDirectory())
458 directories.push_back (mac_sdk);
463 PlatformDarwinKernel::GetGenericSDKDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
465 FileSpec generic_sdk("/AppleInternal/Developer/KDKs", true);
466 if (generic_sdk.Exists() && generic_sdk.IsDirectory())
468 directories.push_back (generic_sdk);
471 // The KDKs distributed from Apple installed on external
472 // developer systems may be in directories like
473 // /Library/Developer/KDKs/KDK_10.10_14A298i.kdk
474 FileSpec installed_kdks("/Library/Developer/KDKs", true);
475 if (installed_kdks.Exists() && installed_kdks.IsDirectory())
477 directories.push_back (installed_kdks);
482 PlatformDarwinKernel::GetiOSDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
487 PlatformDarwinKernel::GetMacDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
489 FileSpec sle("/System/Library/Extensions", true);
490 if (sle.Exists() && sle.IsDirectory())
492 directories.push_back(sle);
495 FileSpec le("/Library/Extensions", true);
496 if (le.Exists() && le.IsDirectory())
498 directories.push_back(le);
501 FileSpec kdk("/Volumes/KernelDebugKit", true);
502 if (kdk.Exists() && kdk.IsDirectory())
504 directories.push_back(kdk);
509 PlatformDarwinKernel::GetGenericDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
511 // DeveloperDirectory is something like "/Applications/Xcode.app/Contents/Developer"
512 const char *developer_dir = GetDeveloperDirectory();
513 if (developer_dir == NULL)
514 developer_dir = "/Applications/Xcode.app/Contents/Developer";
516 char pathbuf[PATH_MAX];
517 ::snprintf (pathbuf, sizeof (pathbuf), "%s/../Symbols", developer_dir);
518 FileSpec symbols_dir (pathbuf, true);
519 if (symbols_dir.Exists() && symbols_dir.IsDirectory())
521 directories.push_back (symbols_dir);
526 PlatformDarwinKernel::GetKernelDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
528 FileSpec system_library_kernels ("/System/Library/Kernels", true);
529 if (system_library_kernels.Exists() && system_library_kernels.IsDirectory())
531 directories.push_back (system_library_kernels);
533 FileSpec slek("/System/Library/Extensions/KDK", true);
534 if (slek.Exists() && slek.IsDirectory())
536 directories.push_back(slek);
541 PlatformDarwinKernel::GetCurrentDirectoryToSearch (std::vector<lldb_private::FileSpec> &directories)
543 directories.push_back (FileSpec (".", true));
545 FileSpec sle_directory ("System/Library/Extensions", true);
546 if (sle_directory.Exists() && sle_directory.IsDirectory())
548 directories.push_back (sle_directory);
551 FileSpec le_directory ("Library/Extensions", true);
552 if (le_directory.Exists() && le_directory.IsDirectory())
554 directories.push_back (le_directory);
557 FileSpec slk_directory ("System/Library/Kernels", true);
558 if (slk_directory.Exists() && slk_directory.IsDirectory())
560 directories.push_back (slk_directory);
562 FileSpec slek("System/Library/Extensions/KDK", true);
563 if (slek.Exists() && slek.IsDirectory())
565 directories.push_back(slek);
570 PlatformDarwinKernel::GetUserSpecifiedDirectoriesToSearch (std::vector<lldb_private::FileSpec> &directories)
572 FileSpecList user_dirs(GetGlobalProperties()->GetKextDirectories());
573 std::vector<FileSpec> possible_sdk_dirs;
575 const uint32_t user_dirs_count = user_dirs.GetSize();
576 for (uint32_t i = 0; i < user_dirs_count; i++)
578 FileSpec dir = user_dirs.GetFileSpecAtIndex (i);
580 if (dir.Exists() && dir.IsDirectory())
582 directories.push_back (dir);
583 possible_sdk_dirs.push_back (dir); // does this directory have a *.sdk or *.kdk that we should look in?
585 // Is there a "System/Library/Extensions" subdir of this directory?
586 std::string dir_sle_path = dir.GetPath();
587 dir_sle_path.append ("/System/Library/Extensions");
588 FileSpec dir_sle(dir_sle_path.c_str(), true);
589 if (dir_sle.Exists() && dir_sle.IsDirectory())
591 directories.push_back (dir_sle);
594 // Is there a "System/Library/Kernels" subdir of this directory?
595 std::string dir_slk_path = dir.GetPath();
596 dir_slk_path.append ("/System/Library/Kernels");
597 FileSpec dir_slk(dir_slk_path.c_str(), true);
598 if (dir_slk.Exists() && dir_slk.IsDirectory())
600 directories.push_back (dir_slk);
603 // Is there a "System/Library/Extensions/KDK" subdir of this directory?
604 std::string dir_slek_path = dir.GetPath();
605 dir_slek_path.append ("/System/Library/Kernels");
606 FileSpec dir_slek(dir_slek_path.c_str(), true);
607 if (dir_slek.Exists() && dir_slek.IsDirectory())
609 directories.push_back (dir_slek);
614 SearchSDKsForKextDirectories (possible_sdk_dirs, directories);
617 // Scan through the SDK directories, looking for directories where kexts are likely.
618 // Add those directories to kext_dirs.
620 PlatformDarwinKernel::SearchSDKsForKextDirectories (std::vector<lldb_private::FileSpec> sdk_dirs, std::vector<lldb_private::FileSpec> &kext_dirs)
622 const uint32_t num_sdks = sdk_dirs.size();
623 for (uint32_t i = 0; i < num_sdks; i++)
625 const FileSpec &sdk_dir = sdk_dirs[i];
626 std::string sdk_dir_path = sdk_dir.GetPath();
627 if (!sdk_dir_path.empty())
629 const bool find_directories = true;
630 const bool find_files = false;
631 const bool find_other = false;
632 FileSpec::EnumerateDirectory (sdk_dir_path.c_str(),
636 GetKextDirectoriesInSDK,
642 // Callback for FileSpec::EnumerateDirectory().
643 // Step through the entries in a directory like
644 // /Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
645 // looking for any subdirectories of the form MacOSX10.8.Internal.sdk/System/Library/Extensions
646 // Adds these to the vector of FileSpec's.
648 FileSpec::EnumerateDirectoryResult
649 PlatformDarwinKernel::GetKextDirectoriesInSDK (void *baton,
650 FileSpec::FileType file_type,
651 const FileSpec &file_spec)
653 if (file_type == FileSpec::eFileTypeDirectory
654 && (file_spec.GetFileNameExtension() == ConstString("sdk")
655 || file_spec.GetFileNameExtension() == ConstString("kdk")))
657 std::string kext_directory_path = file_spec.GetPath();
659 // Append the raw directory path, e.g. /Library/Developer/KDKs/KDK_10.10_14A298i.kdk
660 // to the directory search list -- there may be kexts sitting directly
661 // in that directory instead of being in a System/Library/Extensions subdir.
662 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
664 // Check to see if there is a System/Library/Extensions subdir & add it if it exists
666 std::string sle_kext_directory_path (kext_directory_path);
667 sle_kext_directory_path.append ("/System/Library/Extensions");
668 FileSpec sle_kext_directory (sle_kext_directory_path.c_str(), true);
669 if (sle_kext_directory.Exists() && sle_kext_directory.IsDirectory())
671 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(sle_kext_directory);
674 // Check to see if there is a Library/Extensions subdir & add it if it exists
676 std::string le_kext_directory_path (kext_directory_path);
677 le_kext_directory_path.append ("/Library/Extensions");
678 FileSpec le_kext_directory (le_kext_directory_path.c_str(), true);
679 if (le_kext_directory.Exists() && le_kext_directory.IsDirectory())
681 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(le_kext_directory);
684 // Check to see if there is a System/Library/Kernels subdir & add it if it exists
685 std::string slk_kernel_path (kext_directory_path);
686 slk_kernel_path.append ("/System/Library/Kernels");
687 FileSpec slk_kernel_directory (slk_kernel_path.c_str(), true);
688 if (slk_kernel_directory.Exists() && slk_kernel_directory.IsDirectory())
690 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(slk_kernel_directory);
693 // Check to see if there is a System/Library/Extensions/KDK subdir & add it if it exists
694 std::string slek_kernel_path (kext_directory_path);
695 slek_kernel_path.append ("/System/Library/Extensions/KDK");
696 FileSpec slek_kernel_directory (slek_kernel_path.c_str(), true);
697 if (slek_kernel_directory.Exists() && slek_kernel_directory.IsDirectory())
699 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(slek_kernel_directory);
702 return FileSpec::eEnumerateDirectoryResultNext;
706 PlatformDarwinKernel::IndexKextsInDirectories ()
708 std::vector<FileSpec> kext_bundles;
710 const uint32_t num_dirs = m_search_directories.size();
711 for (uint32_t i = 0; i < num_dirs; i++)
713 const FileSpec &dir = m_search_directories[i];
714 const bool find_directories = true;
715 const bool find_files = false;
716 const bool find_other = false;
717 FileSpec::EnumerateDirectory (dir.GetPath().c_str(),
725 const uint32_t num_kexts = kext_bundles.size();
726 for (uint32_t i = 0; i < num_kexts; i++)
728 const FileSpec &kext = kext_bundles[i];
729 CFCBundle bundle (kext.GetPath().c_str());
730 CFStringRef bundle_id (bundle.GetIdentifier());
731 if (bundle_id && CFGetTypeID (bundle_id) == CFStringGetTypeID ())
733 char bundle_id_buf[PATH_MAX];
734 if (CFStringGetCString (bundle_id, bundle_id_buf, sizeof (bundle_id_buf), kCFStringEncodingUTF8))
736 ConstString bundle_conststr(bundle_id_buf);
737 m_name_to_kext_path_map.insert(std::pair<ConstString, FileSpec>(bundle_conststr, kext));
743 // Callback for FileSpec::EnumerateDirectory().
744 // Step through the entries in a directory like /System/Library/Extensions, find .kext bundles, add them
745 // to the vector of FileSpecs.
746 // If a .kext bundle has a Contents/PlugIns or PlugIns subdir, search for kexts in there too.
748 FileSpec::EnumerateDirectoryResult
749 PlatformDarwinKernel::GetKextsInDirectory (void *baton,
750 FileSpec::FileType file_type,
751 const FileSpec &file_spec)
753 if (file_type == FileSpec::eFileTypeDirectory && file_spec.GetFileNameExtension() == ConstString("kext"))
755 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
756 std::string kext_bundle_path = file_spec.GetPath();
757 std::string search_here_too;
758 std::string contents_plugins_path = kext_bundle_path + "/Contents/PlugIns";
759 FileSpec contents_plugins (contents_plugins_path.c_str(), false);
760 if (contents_plugins.Exists() && contents_plugins.IsDirectory())
762 search_here_too = contents_plugins_path;
766 std::string plugins_path = kext_bundle_path + "/PlugIns";
767 FileSpec plugins (plugins_path.c_str(), false);
768 if (plugins.Exists() && plugins.IsDirectory())
770 search_here_too = plugins_path;
774 if (!search_here_too.empty())
776 const bool find_directories = true;
777 const bool find_files = false;
778 const bool find_other = false;
779 FileSpec::EnumerateDirectory (search_here_too.c_str(),
787 return FileSpec::eEnumerateDirectoryResultNext;
791 PlatformDarwinKernel::IndexKernelsInDirectories ()
793 std::vector<FileSpec> kernels;
796 const uint32_t num_dirs = m_search_directories.size();
797 for (uint32_t i = 0; i < num_dirs; i++)
799 const FileSpec &dir = m_search_directories[i];
800 const bool find_directories = false;
801 const bool find_files = true;
802 const bool find_other = true; // I think eFileTypeSymbolicLink are "other"s.
803 FileSpec::EnumerateDirectory (dir.GetPath().c_str(),
807 GetKernelsInDirectory,
812 // Callback for FileSpec::EnumerateDirectory().
813 // Step through the entries in a directory like /System/Library/Kernels/, find kernel binaries,
814 // add them to m_kernel_binaries.
816 // We're only doing a filename match here. We won't try opening the file to see if it's really
817 // a kernel or not until we need to find a kernel of a given UUID. There's no cheap way to find
818 // the UUID of a file (or if it's a Mach-O binary at all) without creating a whole Module for
819 // the file and throwing it away if it's not wanted.
821 FileSpec::EnumerateDirectoryResult
822 PlatformDarwinKernel::GetKernelsInDirectory (void *baton,
823 FileSpec::FileType file_type,
824 const FileSpec &file_spec)
826 if (file_type == FileSpec::eFileTypeRegular || file_type == FileSpec::eFileTypeSymbolicLink)
828 ConstString filename = file_spec.GetFilename();
829 if (strncmp (filename.GetCString(), "kernel", 6) == 0
830 || strncmp (filename.GetCString(), "mach", 4) == 0)
832 // This is m_kernel_binaries but we're in a class method here
833 ((std::vector<lldb_private::FileSpec> *)baton)->push_back(file_spec);
836 return FileSpec::eEnumerateDirectoryResultNext;
841 PlatformDarwinKernel::GetSharedModule (const ModuleSpec &module_spec,
844 const FileSpecList *module_search_paths_ptr,
845 ModuleSP *old_module_sp_ptr,
846 bool *did_create_ptr)
850 const FileSpec &platform_file = module_spec.GetFileSpec();
852 // Treat the file's path as a kext bundle ID (e.g. "com.apple.driver.AppleIRController") and search our kext index.
853 std::string kext_bundle_id = platform_file.GetPath();
854 if (!kext_bundle_id.empty())
856 ConstString kext_bundle_cs(kext_bundle_id.c_str());
857 if (m_name_to_kext_path_map.count(kext_bundle_cs) > 0)
859 for (BundleIDToKextIterator it = m_name_to_kext_path_map.begin (); it != m_name_to_kext_path_map.end (); ++it)
861 if (it->first == kext_bundle_cs)
863 error = ExamineKextForMatchingUUID (it->second, module_spec.GetUUID(), module_spec.GetArchitecture(), module_sp);
873 if (kext_bundle_id.compare("mach_kernel") == 0 && module_spec.GetUUID().IsValid())
875 for (auto possible_kernel : m_kernel_binaries)
877 if (possible_kernel.Exists())
879 ModuleSpec kern_spec (possible_kernel);
880 kern_spec.GetUUID() = module_spec.GetUUID();
881 ModuleSP module_sp (new Module (kern_spec));
882 if (module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec (kern_spec))
885 error = ModuleList::GetSharedModule (kern_spec, module_sp, NULL, NULL, NULL);
886 if (module_sp && module_sp->GetObjectFile())
895 // Else fall back to treating the file's path as an actual file path - defer to PlatformDarwin's GetSharedModule.
896 return PlatformDarwin::GetSharedModule (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
900 PlatformDarwinKernel::ExamineKextForMatchingUUID (const FileSpec &kext_bundle_path, const lldb_private::UUID &uuid, const ArchSpec &arch, ModuleSP &exe_module_sp)
903 FileSpec exe_file = kext_bundle_path;
904 Host::ResolveExecutableInBundle (exe_file);
905 if (exe_file.Exists())
907 ModuleSpec exe_spec (exe_file);
908 exe_spec.GetUUID() = uuid;
911 exe_spec.GetArchitecture() = arch;
914 // First try to create a ModuleSP with the file / arch and see if the UUID matches.
915 // If that fails (this exec file doesn't have the correct uuid), don't call GetSharedModule
916 // (which may call in to the DebugSymbols framework and therefore can be slow.)
917 ModuleSP module_sp (new Module (exe_spec));
918 if (module_sp && module_sp->GetObjectFile() && module_sp->MatchesModuleSpec (exe_spec))
920 error = ModuleList::GetSharedModule (exe_spec, exe_module_sp, NULL, NULL, NULL);
921 if (exe_module_sp && exe_module_sp->GetObjectFile())
926 exe_module_sp.reset();
932 PlatformDarwinKernel::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
934 #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
935 return ARMGetSupportedArchitectureAtIndex (idx, arch);
937 return x86GetSupportedArchitectureAtIndex (idx, arch);
942 PlatformDarwinKernel::CalculateTrapHandlerSymbolNames ()
944 m_trap_handlers.push_back(ConstString ("trap_from_kernel"));
945 m_trap_handlers.push_back(ConstString ("hndl_machine_check"));
946 m_trap_handlers.push_back(ConstString ("hndl_double_fault"));
947 m_trap_handlers.push_back(ConstString ("hndl_allintrs"));
948 m_trap_handlers.push_back(ConstString ("hndl_alltraps"));
949 m_trap_handlers.push_back(ConstString ("interrupt"));
950 m_trap_handlers.push_back(ConstString ("fleh_prefabt"));
951 m_trap_handlers.push_back(ConstString ("ExceptionVectorsBase"));
952 m_trap_handlers.push_back(ConstString ("ExceptionVectorsTable"));
953 m_trap_handlers.push_back(ConstString ("fleh_undef"));
954 m_trap_handlers.push_back(ConstString ("fleh_dataabt"));
955 m_trap_handlers.push_back(ConstString ("fleh_irq"));
956 m_trap_handlers.push_back(ConstString ("fleh_decirq"));
957 m_trap_handlers.push_back(ConstString ("fleh_fiq_generic"));
958 m_trap_handlers.push_back(ConstString ("fleh_dec"));
964 // Since DynamicLoaderDarwinKernel is compiled in for all systems, and relies on
965 // PlatformDarwinKernel for the plug-in name, we compile just the plug-in name in
966 // here to avoid issues. We are tracking an internal bug to resolve this issue by
967 // either not compiling in DynamicLoaderDarwinKernel for non-apple builds, or to make
968 // PlatformDarwinKernel build on all systems. PlatformDarwinKernel is currently not
969 // compiled on other platforms due to the use of the Mac-specific
970 // source/Host/macosx/cfcpp utilities.
972 lldb_private::ConstString
973 PlatformDarwinKernel::GetPluginNameStatic ()
975 static lldb_private::ConstString g_name("darwin-kernel");