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