| 1 | //===-- HostInfoMacOSX.mm ---------------------------------------*- C++ -*-===// |
| 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 "lldb/Host/macosx/HostInfoMacOSX.h" |
| 10 | #include "lldb/Host/FileSystem.h" |
| 11 | #include "lldb/Host/Host.h" |
| 12 | #include "lldb/Host/HostInfo.h" |
| 13 | #include "lldb/Utility/Args.h" |
| 14 | #include "lldb/Utility/LLDBLog.h" |
| 15 | #include "lldb/Utility/Log.h" |
| 16 | #include "lldb/Utility/Timer.h" |
| 17 | |
| 18 | #include "llvm/ADT/ScopeExit.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/Support/FileSystem.h" |
| 22 | #include "llvm/Support/Path.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | |
| 25 | // C++ Includes |
| 26 | #include <optional> |
| 27 | #include <string> |
| 28 | |
| 29 | // C inclues |
| 30 | #include <cstdlib> |
| 31 | #include <sys/sysctl.h> |
| 32 | #include <sys/syslimits.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <uuid/uuid.h> |
| 35 | |
| 36 | // Objective-C/C++ includes |
| 37 | #include <AvailabilityMacros.h> |
| 38 | #include <CoreFoundation/CoreFoundation.h> |
| 39 | #include <Foundation/Foundation.h> |
| 40 | #include <mach-o/dyld.h> |
| 41 | #if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
| 42 | MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_0 |
| 43 | #if __has_include(<mach-o/dyld_introspection.h>) |
| 44 | #include <mach-o/dyld_introspection.h> |
| 45 | #define SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS |
| 46 | #endif |
| 47 | #endif |
| 48 | #include <objc/objc-auto.h> |
| 49 | |
| 50 | // These are needed when compiling on systems |
| 51 | // that do not yet have these definitions |
| 52 | #ifndef CPU_SUBTYPE_X86_64_H |
| 53 | #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t)8) |
| 54 | #endif |
| 55 | #ifndef CPU_TYPE_ARM64 |
| 56 | #define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64) |
| 57 | #endif |
| 58 | |
| 59 | #ifndef CPU_TYPE_ARM64_32 |
| 60 | #define CPU_ARCH_ABI64_32 0x02000000 |
| 61 | #define CPU_TYPE_ARM64_32 (CPU_TYPE_ARM | CPU_ARCH_ABI64_32) |
| 62 | #endif |
| 63 | |
| 64 | #include <TargetConditionals.h> // for TARGET_OS_TV, TARGET_OS_WATCH |
| 65 | |
| 66 | using namespace lldb_private; |
| 67 | |
| 68 | std::optional<std::string> HostInfoMacOSX::GetOSBuildString() { |
| 69 | int mib[2] = {CTL_KERN, KERN_OSVERSION}; |
| 70 | char cstr[PATH_MAX]; |
| 71 | size_t cstr_len = sizeof(cstr); |
| 72 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
| 73 | return std::string(cstr, cstr_len - 1); |
| 74 | |
| 75 | return std::nullopt; |
| 76 | } |
| 77 | |
| 78 | static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) { |
| 79 | @autoreleasepool { |
| 80 | NSDictionary *version_info = |
| 81 | [NSDictionary dictionaryWithContentsOfFile: |
| 82 | @"/System/Library/CoreServices/SystemVersion.plist" ]; |
| 83 | NSString *version_value = [version_info objectForKey: Key]; |
| 84 | const char *version_str = [version_value UTF8String]; |
| 85 | version.tryParse(string: version_str); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | llvm::VersionTuple HostInfoMacOSX::GetOSVersion() { |
| 90 | static llvm::VersionTuple g_version; |
| 91 | if (g_version.empty()) |
| 92 | ParseOSVersion(g_version, @"ProductVersion" ); |
| 93 | return g_version; |
| 94 | } |
| 95 | |
| 96 | llvm::VersionTuple HostInfoMacOSX::GetMacCatalystVersion() { |
| 97 | static llvm::VersionTuple g_version; |
| 98 | if (g_version.empty()) |
| 99 | ParseOSVersion(g_version, @"iOSSupportVersion" ); |
| 100 | return g_version; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | FileSpec HostInfoMacOSX::GetProgramFileSpec() { |
| 105 | static FileSpec g_program_filespec; |
| 106 | if (!g_program_filespec) { |
| 107 | char program_fullpath[PATH_MAX]; |
| 108 | // If DST is NULL, then return the number of bytes needed. |
| 109 | uint32_t len = sizeof(program_fullpath); |
| 110 | int err = _NSGetExecutablePath(program_fullpath, &len); |
| 111 | if (err == 0) |
| 112 | g_program_filespec.SetFile(path: program_fullpath, style: FileSpec::Style::native); |
| 113 | else if (err == -1) { |
| 114 | char *large_program_fullpath = (char *)::malloc(size: len + 1); |
| 115 | |
| 116 | err = _NSGetExecutablePath(large_program_fullpath, &len); |
| 117 | if (err == 0) |
| 118 | g_program_filespec.SetFile(path: large_program_fullpath, |
| 119 | style: FileSpec::Style::native); |
| 120 | |
| 121 | ::free(ptr: large_program_fullpath); |
| 122 | } |
| 123 | } |
| 124 | return g_program_filespec; |
| 125 | } |
| 126 | |
| 127 | /// Resolve the given candidate support dir and return true if it's valid. |
| 128 | static bool ResolveAndVerifyCandidateSupportDir(FileSpec &path) { |
| 129 | FileSystem::Instance().Resolve(file_spec&: path); |
| 130 | return FileSystem::Instance().IsDirectory(file_spec: path); |
| 131 | } |
| 132 | |
| 133 | bool HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) { |
| 134 | FileSpec lldb_file_spec = GetShlibDir(); |
| 135 | if (!lldb_file_spec) |
| 136 | return false; |
| 137 | |
| 138 | std::string raw_path = lldb_file_spec.GetPath(); |
| 139 | |
| 140 | size_t framework_pos = raw_path.find(s: "LLDB.framework" ); |
| 141 | if (framework_pos != std::string::npos) { |
| 142 | framework_pos += strlen(s: "LLDB.framework" ); |
| 143 | #if TARGET_OS_IPHONE |
| 144 | // Shallow bundle |
| 145 | raw_path.resize(framework_pos); |
| 146 | #else |
| 147 | // Normal bundle |
| 148 | raw_path.resize(n: framework_pos); |
| 149 | raw_path.append(s: "/Resources" ); |
| 150 | #endif |
| 151 | } else { |
| 152 | // Find the bin path relative to the lib path where the cmake-based |
| 153 | // OS X .dylib lives. We try looking first at a possible sibling `bin` |
| 154 | // directory, and then at the `lib` directory itself. This last case is |
| 155 | // useful for supporting build systems like Bazel which in many cases prefer |
| 156 | // to place support binaries right next to dylibs. |
| 157 | // |
| 158 | // It is not going to work to do it by the executable path, |
| 159 | // as in the case of a python script, the executable is python, not |
| 160 | // the lldb driver. |
| 161 | FileSpec support_dir_spec_lib(raw_path); |
| 162 | FileSpec support_dir_spec_bin = |
| 163 | support_dir_spec_lib.CopyByAppendingPathComponent(component: "/../bin" ); |
| 164 | FileSpec support_dir_spec; |
| 165 | |
| 166 | if (ResolveAndVerifyCandidateSupportDir(path&: support_dir_spec_bin)) { |
| 167 | support_dir_spec = support_dir_spec_bin; |
| 168 | } else if (ResolveAndVerifyCandidateSupportDir(path&: support_dir_spec_lib)) { |
| 169 | support_dir_spec = support_dir_spec_lib; |
| 170 | } else { |
| 171 | Log *log = GetLog(mask: LLDBLog::Host); |
| 172 | LLDB_LOG(log, "failed to find support directory" ); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | // Get normalization from support_dir_spec. Note the FileSpec resolve |
| 177 | // does not remove '..' in the path. |
| 178 | char *const dir_realpath = |
| 179 | realpath(name: support_dir_spec.GetPath().c_str(), NULL); |
| 180 | if (dir_realpath) { |
| 181 | raw_path = dir_realpath; |
| 182 | free(ptr: dir_realpath); |
| 183 | } else { |
| 184 | raw_path = support_dir_spec.GetPath(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | file_spec.SetDirectory(raw_path); |
| 189 | return (bool)file_spec.GetDirectory(); |
| 190 | } |
| 191 | |
| 192 | bool HostInfoMacOSX::(FileSpec &file_spec) { |
| 193 | FileSpec lldb_file_spec = GetShlibDir(); |
| 194 | if (!lldb_file_spec) |
| 195 | return false; |
| 196 | |
| 197 | std::string raw_path = lldb_file_spec.GetPath(); |
| 198 | |
| 199 | size_t framework_pos = raw_path.find(s: "LLDB.framework" ); |
| 200 | if (framework_pos != std::string::npos) { |
| 201 | framework_pos += strlen(s: "LLDB.framework" ); |
| 202 | raw_path.resize(n: framework_pos); |
| 203 | raw_path.append(s: "/Headers" ); |
| 204 | } |
| 205 | file_spec.SetDirectory(raw_path); |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | bool HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) { |
| 210 | FileSpec lldb_file_spec = GetShlibDir(); |
| 211 | if (!lldb_file_spec) |
| 212 | return false; |
| 213 | |
| 214 | std::string raw_path = lldb_file_spec.GetPath(); |
| 215 | |
| 216 | size_t framework_pos = raw_path.find(s: "LLDB.framework" ); |
| 217 | if (framework_pos == std::string::npos) |
| 218 | return false; |
| 219 | |
| 220 | framework_pos += strlen(s: "LLDB.framework" ); |
| 221 | raw_path.resize(n: framework_pos); |
| 222 | raw_path.append(s: "/Resources/PlugIns" ); |
| 223 | file_spec.SetDirectory(raw_path); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec) { |
| 228 | FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns" ); |
| 229 | FileSystem::Instance().Resolve(file_spec&: temp_file); |
| 230 | file_spec.SetDirectory(temp_file.GetPathAsConstString()); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | void HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, |
| 235 | ArchSpec &arch_64) { |
| 236 | // All apple systems support 32 bit execution. |
| 237 | uint32_t cputype, cpusubtype; |
| 238 | uint32_t is_64_bit_capable = false; |
| 239 | size_t len = sizeof(cputype); |
| 240 | ArchSpec host_arch; |
| 241 | // These will tell us about the kernel architecture, which even on a 64 |
| 242 | // bit machine can be 32 bit... |
| 243 | if (::sysctlbyname("hw.cputype" , &cputype, &len, NULL, 0) == 0) { |
| 244 | len = sizeof(cpusubtype); |
| 245 | if (::sysctlbyname("hw.cpusubtype" , &cpusubtype, &len, NULL, 0) != 0) |
| 246 | cpusubtype = CPU_TYPE_ANY; |
| 247 | |
| 248 | len = sizeof(is_64_bit_capable); |
| 249 | ::sysctlbyname("hw.cpu64bit_capable" , &is_64_bit_capable, &len, NULL, 0); |
| 250 | |
| 251 | if (cputype == CPU_TYPE_ARM64 && cpusubtype == CPU_SUBTYPE_ARM64E) { |
| 252 | // The arm64e architecture is a preview. Pretend the host architecture |
| 253 | // is arm64. |
| 254 | cpusubtype = CPU_SUBTYPE_ARM64_ALL; |
| 255 | } |
| 256 | |
| 257 | if (is_64_bit_capable) { |
| 258 | if (cputype & CPU_ARCH_ABI64) { |
| 259 | // We have a 64 bit kernel on a 64 bit system |
| 260 | arch_64.SetArchitecture(arch_type: eArchTypeMachO, cpu: cputype, sub: cpusubtype); |
| 261 | } else { |
| 262 | // We have a 64 bit kernel that is returning a 32 bit cputype, the |
| 263 | // cpusubtype will be correct as if it were for a 64 bit architecture |
| 264 | arch_64.SetArchitecture(arch_type: eArchTypeMachO, cpu: cputype | CPU_ARCH_ABI64, |
| 265 | sub: cpusubtype); |
| 266 | } |
| 267 | |
| 268 | // Now we need modify the cpusubtype for the 32 bit slices. |
| 269 | uint32_t cpusubtype32 = cpusubtype; |
| 270 | #if defined(__i386__) || defined(__x86_64__) |
| 271 | if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H) |
| 272 | cpusubtype32 = CPU_SUBTYPE_I386_ALL; |
| 273 | #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 274 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 275 | cpusubtype32 = CPU_SUBTYPE_ARM_V7S; |
| 276 | #endif |
| 277 | arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), |
| 278 | cpusubtype32); |
| 279 | |
| 280 | if (cputype == CPU_TYPE_ARM || |
| 281 | cputype == CPU_TYPE_ARM64 || |
| 282 | cputype == CPU_TYPE_ARM64_32) { |
| 283 | // When running on a watch or tv, report the host os correctly |
| 284 | #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 |
| 285 | arch_32.GetTriple().setOS(llvm::Triple::TvOS); |
| 286 | arch_64.GetTriple().setOS(llvm::Triple::TvOS); |
| 287 | #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 |
| 288 | arch_32.GetTriple().setOS(llvm::Triple::BridgeOS); |
| 289 | arch_64.GetTriple().setOS(llvm::Triple::BridgeOS); |
| 290 | #elif defined(TARGET_OS_WATCHOS) && TARGET_OS_WATCHOS == 1 |
| 291 | arch_32.GetTriple().setOS(llvm::Triple::WatchOS); |
| 292 | arch_64.GetTriple().setOS(llvm::Triple::WatchOS); |
| 293 | #elif defined(TARGET_OS_XR) && TARGET_OS_XR == 1 |
| 294 | arch_32.GetTriple().setOS(llvm::Triple::XROS); |
| 295 | arch_64.GetTriple().setOS(llvm::Triple::XROS); |
| 296 | #elif defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1 |
| 297 | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
| 298 | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
| 299 | #else |
| 300 | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
| 301 | arch_64.GetTriple().setOS(llvm::Triple::IOS); |
| 302 | #endif |
| 303 | } else { |
| 304 | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
| 305 | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
| 306 | } |
| 307 | } else { |
| 308 | // We have a 32 bit kernel on a 32 bit system |
| 309 | arch_32.SetArchitecture(arch_type: eArchTypeMachO, cpu: cputype, sub: cpusubtype); |
| 310 | #if defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 |
| 311 | arch_32.GetTriple().setOS(llvm::Triple::WatchOS); |
| 312 | #else |
| 313 | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
| 314 | #endif |
| 315 | arch_64.Clear(); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /// Return and cache $DEVELOPER_DIR if it is set and exists. |
| 321 | static std::string GetEnvDeveloperDir() { |
| 322 | static std::string g_env_developer_dir; |
| 323 | static std::once_flag g_once_flag; |
| 324 | std::call_once(once&: g_once_flag, f: [&]() { |
| 325 | if (const char *developer_dir_env_var = getenv(name: "DEVELOPER_DIR" )) { |
| 326 | FileSpec fspec(developer_dir_env_var); |
| 327 | if (FileSystem::Instance().Exists(file_spec: fspec)) |
| 328 | g_env_developer_dir = fspec.GetPath(); |
| 329 | }}); |
| 330 | return g_env_developer_dir; |
| 331 | } |
| 332 | |
| 333 | FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() { |
| 334 | static FileSpec g_xcode_contents_path; |
| 335 | static std::once_flag g_once_flag; |
| 336 | std::call_once(once&: g_once_flag, f: [&]() { |
| 337 | // Try the shlib dir first. |
| 338 | if (FileSpec fspec = HostInfo::GetShlibDir()) { |
| 339 | if (FileSystem::Instance().Exists(file_spec: fspec)) { |
| 340 | std::string xcode_contents_dir = |
| 341 | XcodeSDK::FindXcodeContentsDirectoryInPath(path: fspec.GetPath()); |
| 342 | if (!xcode_contents_dir.empty()) { |
| 343 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | llvm::SmallString<128> env_developer_dir(GetEnvDeveloperDir()); |
| 350 | if (!env_developer_dir.empty()) { |
| 351 | llvm::sys::path::append(path&: env_developer_dir, a: "Contents" ); |
| 352 | std::string xcode_contents_dir = |
| 353 | XcodeSDK::FindXcodeContentsDirectoryInPath(path: env_developer_dir); |
| 354 | if (!xcode_contents_dir.empty()) { |
| 355 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
| 356 | return; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | auto sdk_path_or_err = |
| 361 | HostInfo::GetSDKRoot(options: SDKOptions{.XcodeSDKSelection: XcodeSDK::GetAnyMacOS()}); |
| 362 | if (!sdk_path_or_err) { |
| 363 | Log *log = GetLog(mask: LLDBLog::Host); |
| 364 | LLDB_LOG_ERROR(log, sdk_path_or_err.takeError(), |
| 365 | "Error while searching for Xcode SDK: {0}" ); |
| 366 | return; |
| 367 | } |
| 368 | FileSpec fspec(*sdk_path_or_err); |
| 369 | if (fspec) { |
| 370 | if (FileSystem::Instance().Exists(file_spec: fspec)) { |
| 371 | std::string xcode_contents_dir = |
| 372 | XcodeSDK::FindXcodeContentsDirectoryInPath(path: fspec.GetPath()); |
| 373 | if (!xcode_contents_dir.empty()) { |
| 374 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | }); |
| 380 | return g_xcode_contents_path; |
| 381 | } |
| 382 | |
| 383 | lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() { |
| 384 | static lldb_private::FileSpec g_developer_directory; |
| 385 | static llvm::once_flag g_once_flag; |
| 386 | llvm::call_once(flag&: g_once_flag, F: []() { |
| 387 | if (FileSpec fspec = GetXcodeContentsDirectory()) { |
| 388 | fspec.AppendPathComponent(component: "Developer" ); |
| 389 | if (FileSystem::Instance().Exists(file_spec: fspec)) |
| 390 | g_developer_directory = fspec; |
| 391 | } |
| 392 | }); |
| 393 | return g_developer_directory; |
| 394 | } |
| 395 | |
| 396 | std::string HostInfoMacOSX::FindComponentInPath(llvm::StringRef path, |
| 397 | llvm::StringRef component) { |
| 398 | auto begin = llvm::sys::path::begin(path); |
| 399 | auto end = llvm::sys::path::end(path); |
| 400 | for (auto it = begin; it != end; ++it) { |
| 401 | if (it->contains(Other: component)) { |
| 402 | llvm::SmallString<128> buffer; |
| 403 | llvm::sys::path::append(path&: buffer, begin, end: ++it, |
| 404 | style: llvm::sys::path::Style::posix); |
| 405 | return buffer.str().str(); |
| 406 | } |
| 407 | } |
| 408 | return {}; |
| 409 | } |
| 410 | |
| 411 | FileSpec HostInfoMacOSX::GetCurrentXcodeToolchainDirectory() { |
| 412 | if (FileSpec fspec = HostInfo::GetShlibDir()) |
| 413 | return FileSpec(FindComponentInPath(path: fspec.GetPath(), component: ".xctoolchain" )); |
| 414 | return {}; |
| 415 | } |
| 416 | |
| 417 | FileSpec HostInfoMacOSX::GetCurrentCommandLineToolsDirectory() { |
| 418 | if (FileSpec fspec = HostInfo::GetShlibDir()) |
| 419 | return FileSpec(FindComponentInPath(path: fspec.GetPath(), component: "CommandLineTools" )); |
| 420 | return {}; |
| 421 | } |
| 422 | |
| 423 | static llvm::Expected<std::string> |
| 424 | xcrun(const std::string &sdk, llvm::ArrayRef<llvm::StringRef> arguments, |
| 425 | llvm::StringRef developer_dir = "" ) { |
| 426 | Args args; |
| 427 | if (!developer_dir.empty()) { |
| 428 | args.AppendArgument(arg_str: "/usr/bin/env" ); |
| 429 | args.AppendArgument(arg_str: "DEVELOPER_DIR=" + developer_dir.str()); |
| 430 | } |
| 431 | args.AppendArgument(arg_str: "/usr/bin/xcrun" ); |
| 432 | args.AppendArgument(arg_str: "--sdk" ); |
| 433 | args.AppendArgument(arg_str: sdk); |
| 434 | for (auto arg: arguments) |
| 435 | args.AppendArgument(arg_str: arg); |
| 436 | |
| 437 | Log *log = GetLog(mask: LLDBLog::Host); |
| 438 | if (log) { |
| 439 | std::string cmdstr; |
| 440 | args.GetCommandString(command&: cmdstr); |
| 441 | LLDB_LOG(log, "GetXcodeSDK() running shell cmd '{0}'" , cmdstr); |
| 442 | } |
| 443 | |
| 444 | int status = 0; |
| 445 | int signo = 0; |
| 446 | std::string output_str; |
| 447 | // The first time after Xcode was updated or freshly installed, |
| 448 | // xcrun can take surprisingly long to build up its database. |
| 449 | auto timeout = std::chrono::seconds(60); |
| 450 | bool run_in_shell = false; |
| 451 | lldb_private::Status error = Host::RunShellCommand( |
| 452 | args, working_dir: FileSpec(), status_ptr: &status, signo_ptr: &signo, command_output: &output_str, timeout, run_in_shell); |
| 453 | |
| 454 | // Check that xcrun returned something useful. |
| 455 | if (error.Fail()) { |
| 456 | // Catastrophic error. |
| 457 | LLDB_LOG(log, "xcrun failed to execute: {0}" , error); |
| 458 | return error.ToError(); |
| 459 | } |
| 460 | if (status != 0) { |
| 461 | // xcrun didn't find a matching SDK. Not an error, we'll try |
| 462 | // different spellings. |
| 463 | LLDB_LOG(log, "xcrun returned exit code {0}" , status); |
| 464 | if (!output_str.empty()) |
| 465 | LLDB_LOG(log, "xcrun output was:\n{0}" , output_str); |
| 466 | return "" ; |
| 467 | } |
| 468 | if (output_str.empty()) { |
| 469 | LLDB_LOG(log, "xcrun returned no results" ); |
| 470 | return "" ; |
| 471 | } |
| 472 | |
| 473 | // Convert to a StringRef so we can manipulate the string without modifying |
| 474 | // the underlying data. |
| 475 | llvm::StringRef output(output_str); |
| 476 | |
| 477 | // Remove any trailing newline characters. |
| 478 | output = output.rtrim(); |
| 479 | |
| 480 | // Strip any leading newline characters and everything before them. |
| 481 | const size_t last_newline = output.rfind(C: '\n'); |
| 482 | if (last_newline != llvm::StringRef::npos) |
| 483 | output = output.substr(Start: last_newline + 1); |
| 484 | |
| 485 | return output.str(); |
| 486 | } |
| 487 | |
| 488 | static llvm::Expected<std::string> GetXcodeSDK(XcodeSDK sdk) { |
| 489 | XcodeSDK::Info info = sdk.Parse(); |
| 490 | std::string sdk_name = XcodeSDK::GetCanonicalName(info); |
| 491 | if (sdk_name.empty()) |
| 492 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 493 | S: "Unrecognized SDK type: " + sdk.GetString()); |
| 494 | |
| 495 | Log *log = GetLog(mask: LLDBLog::Host); |
| 496 | |
| 497 | auto find_sdk = |
| 498 | [](const std::string &sdk_name) -> llvm::Expected<std::string> { |
| 499 | llvm::SmallVector<llvm::StringRef, 1> show_sdk_path = {"--show-sdk-path" }; |
| 500 | // Invoke xcrun with the developer dir specified in the environment. |
| 501 | std::string developer_dir = GetEnvDeveloperDir(); |
| 502 | if (!developer_dir.empty()) { |
| 503 | // Don't fallback if DEVELOPER_DIR was set. |
| 504 | return xcrun(sdk: sdk_name, arguments: show_sdk_path, developer_dir); |
| 505 | } |
| 506 | |
| 507 | // Invoke xcrun with the shlib dir. |
| 508 | if (FileSpec fspec = HostInfo::GetShlibDir()) { |
| 509 | if (FileSystem::Instance().Exists(file_spec: fspec)) { |
| 510 | llvm::SmallString<0> shlib_developer_dir( |
| 511 | XcodeSDK::FindXcodeContentsDirectoryInPath(path: fspec.GetPath())); |
| 512 | llvm::sys::path::append(path&: shlib_developer_dir, a: "Developer" ); |
| 513 | if (FileSystem::Instance().Exists(path: shlib_developer_dir)) { |
| 514 | auto sdk = xcrun(sdk: sdk_name, arguments: show_sdk_path, developer_dir: shlib_developer_dir); |
| 515 | if (!sdk) |
| 516 | return sdk.takeError(); |
| 517 | if (!sdk->empty()) |
| 518 | return sdk; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | // Invoke xcrun without a developer dir as a last resort. |
| 524 | return xcrun(sdk: sdk_name, arguments: show_sdk_path); |
| 525 | }; |
| 526 | |
| 527 | auto path_or_err = find_sdk(sdk_name); |
| 528 | if (!path_or_err) |
| 529 | return path_or_err.takeError(); |
| 530 | std::string path = *path_or_err; |
| 531 | while (path.empty()) { |
| 532 | // Try an alternate spelling of the name ("macosx10.9internal"). |
| 533 | if (info.type == XcodeSDK::Type::MacOSX && !info.version.empty() && |
| 534 | info.internal) { |
| 535 | llvm::StringRef fixed(sdk_name); |
| 536 | if (fixed.consume_back(Suffix: ".internal" )) |
| 537 | sdk_name = fixed.str() + "internal" ; |
| 538 | path_or_err = find_sdk(sdk_name); |
| 539 | if (!path_or_err) |
| 540 | return path_or_err.takeError(); |
| 541 | path = *path_or_err; |
| 542 | if (!path.empty()) |
| 543 | break; |
| 544 | } |
| 545 | LLDB_LOG(log, "Couldn't find SDK {0} on host" , sdk_name); |
| 546 | |
| 547 | // Try without the version. |
| 548 | if (!info.version.empty()) { |
| 549 | info.version = {}; |
| 550 | sdk_name = XcodeSDK::GetCanonicalName(info); |
| 551 | path_or_err = find_sdk(sdk_name); |
| 552 | if (!path_or_err) |
| 553 | return path_or_err.takeError(); |
| 554 | path = *path_or_err; |
| 555 | if (!path.empty()) |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | LLDB_LOG(log, "Couldn't find any matching SDK on host" ); |
| 560 | return "" ; |
| 561 | } |
| 562 | |
| 563 | // Whatever is left in output should be a valid path. |
| 564 | if (!FileSystem::Instance().Exists(path)) { |
| 565 | LLDB_LOG(log, "SDK returned by xcrun doesn't exist" ); |
| 566 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 567 | S: "SDK returned by xcrun doesn't exist" ); |
| 568 | } |
| 569 | return path; |
| 570 | } |
| 571 | |
| 572 | namespace { |
| 573 | struct ErrorOrPath { |
| 574 | std::string str; |
| 575 | bool is_error; |
| 576 | }; |
| 577 | } // namespace |
| 578 | |
| 579 | static llvm::Expected<llvm::StringRef> |
| 580 | find_cached_path(llvm::StringMap<ErrorOrPath> &cache, std::mutex &mutex, |
| 581 | llvm::StringRef key, |
| 582 | std::function<llvm::Expected<std::string>(void)> compute) { |
| 583 | std::lock_guard<std::mutex> guard(mutex); |
| 584 | LLDB_SCOPED_TIMER(); |
| 585 | |
| 586 | auto it = cache.find(Key: key); |
| 587 | if (it != cache.end()) { |
| 588 | if (it->second.is_error) |
| 589 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 590 | S: it->second.str); |
| 591 | return it->second.str; |
| 592 | } |
| 593 | auto path_or_err = compute(); |
| 594 | if (!path_or_err) { |
| 595 | std::string error = toString(E: path_or_err.takeError()); |
| 596 | cache.insert(KV: {key, {.str: error, .is_error: true}}); |
| 597 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), S: error); |
| 598 | } |
| 599 | auto it_new = cache.insert(KV: {key, {.str: *path_or_err, .is_error: false}}); |
| 600 | return it_new.first->second.str; |
| 601 | } |
| 602 | |
| 603 | llvm::Expected<llvm::StringRef> HostInfoMacOSX::GetSDKRoot(SDKOptions options) { |
| 604 | static llvm::StringMap<ErrorOrPath> g_sdk_path; |
| 605 | static std::mutex g_sdk_path_mutex; |
| 606 | if (!options.XcodeSDKSelection) |
| 607 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 608 | S: "XcodeSDK not specified" ); |
| 609 | XcodeSDK sdk = *options.XcodeSDKSelection; |
| 610 | auto key = sdk.GetString(); |
| 611 | return find_cached_path(cache&: g_sdk_path, mutex&: g_sdk_path_mutex, key, compute: [&](){ |
| 612 | return GetXcodeSDK(sdk); |
| 613 | }); |
| 614 | } |
| 615 | |
| 616 | llvm::Expected<llvm::StringRef> |
| 617 | HostInfoMacOSX::FindSDKTool(XcodeSDK sdk, llvm::StringRef tool) { |
| 618 | static llvm::StringMap<ErrorOrPath> g_tool_path; |
| 619 | static std::mutex g_tool_path_mutex; |
| 620 | std::string key; |
| 621 | llvm::raw_string_ostream(key) << sdk.GetString() << ":" << tool; |
| 622 | return find_cached_path( |
| 623 | cache&: g_tool_path, mutex&: g_tool_path_mutex, key, |
| 624 | compute: [&]() -> llvm::Expected<std::string> { |
| 625 | std::string sdk_name = XcodeSDK::GetCanonicalName(info: sdk.Parse()); |
| 626 | if (sdk_name.empty()) |
| 627 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 628 | S: "Unrecognized SDK type: " + |
| 629 | sdk.GetString()); |
| 630 | llvm::SmallVector<llvm::StringRef, 2> find = {"-find" , tool}; |
| 631 | return xcrun(sdk: sdk_name, arguments: find); |
| 632 | }); |
| 633 | } |
| 634 | |
| 635 | namespace { |
| 636 | struct dyld_shared_cache_dylib_text_info { |
| 637 | uint64_t version; // current version 1 |
| 638 | // following fields all exist in version 1 |
| 639 | uint64_t loadAddressUnslid; |
| 640 | uint64_t textSegmentSize; |
| 641 | uuid_t dylibUuid; |
| 642 | const char *path; // pointer invalid at end of iterations |
| 643 | // following fields all exist in version 2 |
| 644 | uint64_t textSegmentOffset; // offset from start of cache |
| 645 | }; |
| 646 | typedef struct dyld_shared_cache_dylib_text_info |
| 647 | dyld_shared_cache_dylib_text_info; |
| 648 | } |
| 649 | |
| 650 | extern "C" int dyld_shared_cache_iterate_text( |
| 651 | const uuid_t cacheUuid, |
| 652 | void (^callback)(const dyld_shared_cache_dylib_text_info *info)); |
| 653 | extern "C" uint8_t *_dyld_get_shared_cache_range(size_t *length); |
| 654 | extern "C" bool _dyld_get_shared_cache_uuid(uuid_t uuid); |
| 655 | |
| 656 | namespace { |
| 657 | class SharedCacheInfo { |
| 658 | public: |
| 659 | const UUID &GetUUID() const { return m_uuid; } |
| 660 | const llvm::StringMap<SharedCacheImageInfo> &GetImages() const { |
| 661 | return m_images; |
| 662 | } |
| 663 | |
| 664 | SharedCacheInfo(); |
| 665 | |
| 666 | private: |
| 667 | bool CreateSharedCacheInfoWithInstrospectionSPIs(); |
| 668 | |
| 669 | llvm::StringMap<SharedCacheImageInfo> m_images; |
| 670 | UUID m_uuid; |
| 671 | }; |
| 672 | } |
| 673 | |
| 674 | bool SharedCacheInfo::CreateSharedCacheInfoWithInstrospectionSPIs() { |
| 675 | #if defined(SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS) |
| 676 | dyld_process_t dyld_process = dyld_process_create_for_current_task(); |
| 677 | if (!dyld_process) |
| 678 | return false; |
| 679 | |
| 680 | auto cleanup_process_on_exit = |
| 681 | llvm::make_scope_exit([&]() { dyld_process_dispose(dyld_process); }); |
| 682 | |
| 683 | dyld_process_snapshot_t snapshot = |
| 684 | dyld_process_snapshot_create_for_process(dyld_process, nullptr); |
| 685 | if (!snapshot) |
| 686 | return false; |
| 687 | |
| 688 | auto cleanup_snapshot_on_exit = |
| 689 | llvm::make_scope_exit([&]() { dyld_process_snapshot_dispose(snapshot); }); |
| 690 | |
| 691 | dyld_shared_cache_t shared_cache = |
| 692 | dyld_process_snapshot_get_shared_cache(snapshot); |
| 693 | if (!shared_cache) |
| 694 | return false; |
| 695 | |
| 696 | dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) { |
| 697 | __block uint64_t minVmAddr = UINT64_MAX; |
| 698 | __block uint64_t maxVmAddr = 0; |
| 699 | uuid_t uuidStore; |
| 700 | __block uuid_t *uuid = &uuidStore; |
| 701 | |
| 702 | dyld_image_for_each_segment_info( |
| 703 | image, |
| 704 | ^(const char *segmentName, uint64_t vmAddr, uint64_t vmSize, int perm) { |
| 705 | minVmAddr = std::min(minVmAddr, vmAddr); |
| 706 | maxVmAddr = std::max(maxVmAddr, vmAddr + vmSize); |
| 707 | dyld_image_copy_uuid(image, uuid); |
| 708 | }); |
| 709 | assert(minVmAddr != UINT_MAX); |
| 710 | assert(maxVmAddr != 0); |
| 711 | m_images[dyld_image_get_installname(image)] = SharedCacheImageInfo{ |
| 712 | UUID(uuid, 16), std::make_shared<DataBufferUnowned>( |
| 713 | (uint8_t *)minVmAddr, maxVmAddr - minVmAddr)}; |
| 714 | }); |
| 715 | return true; |
| 716 | #endif |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | SharedCacheInfo::SharedCacheInfo() { |
| 721 | if (CreateSharedCacheInfoWithInstrospectionSPIs()) |
| 722 | return; |
| 723 | |
| 724 | size_t shared_cache_size; |
| 725 | uint8_t *shared_cache_start = |
| 726 | _dyld_get_shared_cache_range(length: &shared_cache_size); |
| 727 | uuid_t dsc_uuid; |
| 728 | _dyld_get_shared_cache_uuid(uuid: dsc_uuid); |
| 729 | m_uuid = UUID(dsc_uuid); |
| 730 | |
| 731 | dyld_shared_cache_iterate_text( |
| 732 | cacheUuid: dsc_uuid, callback: ^(const dyld_shared_cache_dylib_text_info *info) { |
| 733 | m_images[info->path] = SharedCacheImageInfo{ |
| 734 | .uuid: UUID(info->dylibUuid, 16), |
| 735 | .data_sp: std::make_shared<DataBufferUnowned>( |
| 736 | args: shared_cache_start + info->textSegmentOffset, |
| 737 | args: shared_cache_size - info->textSegmentOffset)}; |
| 738 | }); |
| 739 | } |
| 740 | |
| 741 | SharedCacheImageInfo |
| 742 | HostInfoMacOSX::GetSharedCacheImageInfo(llvm::StringRef image_name) { |
| 743 | static SharedCacheInfo g_shared_cache_info; |
| 744 | return g_shared_cache_info.GetImages().lookup(Key: image_name); |
| 745 | } |
| 746 | |