Skip to content

Commit 3427edd

Browse files
fredrissJDevlieghere
authored andcommitted
Adopt new dyld SPIs to introspect the shared cache.
With the shared cache getting split into multiple files, the current way we created ObjectFileMachO objects for shared cache dylib images will break. This patch conditionally adopts new SPIs which will do the right thing in the new world of multi-file caches.
1 parent ead8586 commit 3427edd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <CoreFoundation/CoreFoundation.h>
3636
#include <Foundation/Foundation.h>
3737
#include <mach-o/dyld.h>
38+
#if __has_include(<mach-o/dyld_introspection.h>)
39+
#include <mach-o/dyld_introspection.h>
40+
#define SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS
41+
#endif
3842
#include <objc/objc-auto.h>
3943

4044
// These are needed when compiling on systems
@@ -525,6 +529,41 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
525529
}
526530

527531
SharedCacheInfo::SharedCacheInfo() {
532+
#if defined(SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS)
533+
if (__builtin_available(macOS 12, *)) {
534+
if (dyld_process_create_for_current_task) {
535+
auto dyld_process = dyld_process_create_for_current_task();
536+
auto snapshot =
537+
dyld_process_snapshot_create_for_process(dyld_process, nullptr);
538+
auto shared_cache = dyld_process_snapshot_get_shared_cache(snapshot);
539+
assert(dyld_process && snapshot && shared_cache);
540+
541+
dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) {
542+
__block uint64_t minVmAddr = UINT64_MAX;
543+
__block uint64_t maxVmAddr = 0;
544+
uuid_t uuidStore;
545+
__block uuid_t *uuid = &uuidStore;
546+
547+
dyld_image_for_each_segment_info(image, ^(const char *segmentName,
548+
uint64_t vmAddr,
549+
uint64_t vmSize, int perm) {
550+
minVmAddr = std::min(minVmAddr, vmAddr);
551+
maxVmAddr = std::max(maxVmAddr, vmAddr + vmSize);
552+
dyld_image_copy_uuid(image, uuid);
553+
});
554+
assert(minVmAddr != UINT_MAX);
555+
assert(maxVmAddr != 0);
556+
m_images[dyld_image_get_installname(image)] = SharedCacheImageInfo{
557+
UUID::fromData(uuid, 16),
558+
std::make_shared<DataBufferUnowned>((uint8_t *)minVmAddr,
559+
maxVmAddr - minVmAddr)};
560+
});
561+
dyld_process_snapshot_dispose(snapshot);
562+
return;
563+
}
564+
}
565+
#endif
566+
528567
size_t shared_cache_size;
529568
uint8_t *shared_cache_start =
530569
_dyld_get_shared_cache_range(&shared_cache_size);

0 commit comments

Comments
 (0)