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