Jason Molenda 481f248e08
[lldb] Get shared cache path from inferior, open (#180323)
Get the shared cache filepath and uuid that the inferior process is
using from debugserver, try to open that shared cache on the lldb host
mac and if the UUID matches, index all of the binaries in that shared
cache. When looking for binaries loaded in the process, get them from
the already-indexed shared cache.

Every time a binary is loaded, PlatformMacOSX may query the shared cache
filepath and uuid from the Process, and pass that to
HostInfo::GetSharedCacheImageInfo() if available (else fall back to the
old HostInfo::GetSharedCacheImageInfo method which only looks at lldb's
own shared cache), to get the file being requested.

ProcessGDBRemote caches the shared cache filepath and uuid from the
inferior, once it has a non-zero UUID. I added a lock for this ivar
specifically, so I don't have 20 threads all asking for the shared cache
information from debugserver and updating the cached answer. If we never
get back a non-zero UUID shared cache reply, we will re-query at every
library loaded notification. debugserver has been providing the shared
cache UUID since 2013, although I only added the shared cache filepath
field last November.

Note that a process will not report its shared cache filepath or uuid at
initial launch. As dyld gets a chance to execute a bit, it will start
returning binaries -- it will be available at the point when libraries
start loading. (it won't be available yet when the binary & dyld are the
only two binaries loaded in the process)

I tested this by disabling lldb's scan of its own shared cache
pre-execution -- only loading the system shared cache when the inferior
process reports that it is using that. I got 6-7 additional testsuite
failures running lldb like that, because no system binaries were loaded
before exeuction start, and the tests assumed they would be.

rdar://148939795

---------

Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
2026-02-09 16:22:25 -08:00

174 lines
6.2 KiB
C++

//===-- DynamicLoaderMacOSXDYLD.h -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This is the DynamicLoader plugin for Darwin (macOS / iPhoneOS / tvOS /
// watchOS / BridgeOS)
// platforms earlier than 2016, where lldb would read the "dyld_all_image_infos"
// dyld internal structure to understand where things were loaded and the
// solib loaded/unloaded notification function we put a breakpoint on gives us
// an array of (load address, mod time, file path) tuples.
//
// As of late 2016, the new DynamicLoaderMacOS plugin should be used, which uses
// dyld SPI functions to get the same information without reading internal dyld
// data structures.
#ifndef LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H
#define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H
#include <mutex>
#include <vector>
#include "lldb/Host/SafeMachO.h"
#include "lldb/Target/DynamicLoader.h"
#include "lldb/Target/Process.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/Utility/UUID.h"
#include "DynamicLoaderDarwin.h"
class DynamicLoaderMacOSXDYLD : public lldb_private::DynamicLoaderDarwin {
public:
DynamicLoaderMacOSXDYLD(lldb_private::Process *process);
~DynamicLoaderMacOSXDYLD() override;
// Static Functions
static void Initialize();
static void Terminate();
static llvm::StringRef GetPluginNameStatic() { return "macosx-dyld"; }
static llvm::StringRef GetPluginDescriptionStatic();
static lldb_private::DynamicLoader *
CreateInstance(lldb_private::Process *process, bool force);
/// Called after attaching a process.
///
/// Allow DynamicLoader plug-ins to execute some code after
/// attaching to a process.
bool ProcessDidExec() override;
lldb_private::Status CanLoadImage() override;
bool GetSharedCacheInformation(
lldb::addr_t &base_address, lldb_private::UUID &uuid,
lldb_private::LazyBool &using_shared_cache,
lldb_private::LazyBool &private_shared_cache,
lldb_private::FileSpec &shared_cache_filepath) override;
// PluginInterface protocol
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
bool IsFullyInitialized() override;
protected:
void PutToLog(lldb_private::Log *log) const;
void DoInitialImageFetch() override;
bool NeedToDoInitialImageFetch() override;
bool DidSetNotificationBreakpoint() override;
void DoClear() override;
bool ReadDYLDInfoFromMemoryAndSetNotificationCallback(lldb::addr_t addr);
static bool
NotifyBreakpointHit(void *baton,
lldb_private::StoppointCallbackContext *context,
lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
uint32_t AddrByteSize();
bool ReadMachHeader(lldb::addr_t addr, llvm::MachO::mach_header *header,
lldb_private::DataExtractor *load_command_data);
uint32_t ParseLoadCommands(const lldb_private::DataExtractor &data,
ImageInfo &dylib_info,
lldb_private::FileSpec *lc_id_dylinker);
struct DYLDAllImageInfos {
uint32_t version = 0;
uint32_t dylib_info_count = 0; // Version >= 1
lldb::addr_t dylib_info_addr = LLDB_INVALID_ADDRESS; // Version >= 1
lldb::addr_t notification = LLDB_INVALID_ADDRESS; // Version >= 1
bool processDetachedFromSharedRegion = false; // Version >= 1
bool libSystemInitialized = false; // Version >= 2
lldb::addr_t dyldImageLoadAddress = LLDB_INVALID_ADDRESS; // Version >= 2
DYLDAllImageInfos() = default;
void Clear() {
version = 0;
dylib_info_count = 0;
dylib_info_addr = LLDB_INVALID_ADDRESS;
notification = LLDB_INVALID_ADDRESS;
processDetachedFromSharedRegion = false;
libSystemInitialized = false;
dyldImageLoadAddress = LLDB_INVALID_ADDRESS;
}
bool IsValid() const { return version >= 1 && version <= 6; }
};
static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic);
bool SetNotificationBreakpoint() override;
void ClearNotificationBreakpoint() override;
// There is a little tricky bit where you might initially attach while dyld is
// updating
// the all_image_infos, and you can't read the infos, so you have to continue
// and pick it
// up when you hit the update breakpoint. At that point, you need to run this
// initialize
// function, but when you do it that way you DON'T need to do the extra work
// you would at
// the breakpoint.
// So this function will only do actual work if the image infos haven't been
// read yet.
// If it does do any work, then it will return true, and false otherwise.
// That way you can
// call it in the breakpoint action, and if it returns true you're done.
bool InitializeFromAllImageInfos();
bool ReadAllImageInfosStructure();
bool AddModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr,
uint32_t image_infos_count);
bool RemoveModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr,
uint32_t image_infos_count);
void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos,
uint32_t infos_count,
bool update_executable);
bool ReadImageInfos(lldb::addr_t image_infos_addr, uint32_t image_infos_count,
ImageInfo::collection &image_infos);
lldb::addr_t m_dyld_all_image_infos_addr;
DYLDAllImageInfos m_dyld_all_image_infos;
uint32_t m_dyld_all_image_infos_stop_id;
lldb::user_id_t m_break_id;
mutable std::recursive_mutex m_mutex;
bool m_process_image_addr_is_all_images_infos;
private:
DynamicLoaderMacOSXDYLD(const DynamicLoaderMacOSXDYLD &) = delete;
const DynamicLoaderMacOSXDYLD &
operator=(const DynamicLoaderMacOSXDYLD &) = delete;
};
#endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H