35#ifdef ENABLE_DEBUG_PRINTF
37#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
39#define DEBUG_PRINTF(fmt, ...)
61 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
70 const llvm::Triple &triple_ref =
72 switch (triple_ref.getOS()) {
73 case llvm::Triple::Darwin:
74 case llvm::Triple::MacOSX:
75 case llvm::Triple::IOS:
76 case llvm::Triple::TvOS:
77 case llvm::Triple::WatchOS:
78 case llvm::Triple::XROS:
79 case llvm::Triple::BridgeOS:
80 create = triple_ref.getVendor() == llvm::Triple::Apple;
89 if (UseDYLDSPI(process)) {
102 m_dyld_all_image_infos(), m_dyld_all_image_infos_stop_id(
UINT32_MAX),
104 m_process_image_addr_is_all_images_infos(false) {}
113 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
114 bool did_exec =
false;
141 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
143 if (symbol->
GetName() ==
"_dyld_start")
161 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
199 uint32_t magic = data.
GetU32(&offset);
201 case llvm::MachO::MH_MAGIC:
202 case llvm::MachO::MH_MAGIC_64:
203 case llvm::MachO::MH_CIGAM:
204 case llvm::MachO::MH_CIGAM_64:
238 }
else if (exe_arch.
GetMachine() == llvm::Triple::arm ||
239 exe_arch.
GetMachine() == llvm::Triple::thumb ||
240 exe_arch.
GetMachine() == llvm::Triple::aarch64 ||
241 exe_arch.
GetMachine() == llvm::Triple::aarch64_32) {
252 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
254 static ConstString g_dyld_all_image_infos(
"dyld_all_image_infos");
255 static ConstString g_new_dyld_all_image_infos(
"dyld4::dyld_all_image_infos");
257 if (
m_dyld.
header.filetype == llvm::MachO::MH_DYLINKER) {
270 dyld_module_sp.get()) {
271 const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
274 symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
284 dyld_module_sp->GetSectionList()->FindSectionByName(g_sect_name);
285 if (dyld_aii_section_sp) {
286 Address dyld_aii_addr(dyld_aii_section_sp, 0);
299 if (dyld_module_sp) {
305 modules.
Append(dyld_module_sp);
385 if (abi->GetArgumentValues(exe_ctx.
GetThreadRef(), argument_values)) {
388 if (dyld_mode !=
static_cast<uint32_t
>(-1)) {
391 uint32_t image_infos_count =
393 if (image_infos_count !=
static_cast<uint32_t
>(-1)) {
401 if (dyld_mode == 0) {
408 image_infos_addr, image_infos_count);
416 "no ABI plugin located for triple " +
418 ": shared libraries will not be registered",
427 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
444 const size_t count_v2 =
sizeof(uint32_t) +
451 const size_t count_v11 = count_v2 + addr_size +
465 const size_t count_v13 = count_v11 + addr_size +
468 assert(
sizeof(buf) >= count_v13);
496 const size_t bytes_read =
498 if (bytes_read == count) {
508 offset += addr_size - 2;
511 offset += addr_size * 8;
512 uint64_t dyld_all_image_infos_addr = data.
GetAddress(&offset);
528 uint64_t image_infos_offset =
529 dyld_all_image_infos_addr -
531 uint64_t notification_offset =
548 lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
549 ImageInfo::collection image_infos;
550 Log *log =
GetLog(LLDBLog::DynamicLoader);
551 LLDB_LOGF(log,
"Adding %d modules.\n", image_infos_count);
553 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
554 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
561 if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() &&
562 image_infos_json_sp->GetAsDictionary()->HasKey(
"images") &&
563 image_infos_json_sp->GetAsDictionary()
564 ->GetValueForKey(
"images")
566 image_infos_json_sp->GetAsDictionary()
567 ->GetValueForKey(
"images")
569 ->GetSize() == image_infos_count) {
570 bool return_value =
false;
579 if (!
ReadImageInfos(image_infos_addr, image_infos_count, image_infos))
589 lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
590 ImageInfo::collection image_infos;
591 Log *log =
GetLog(LLDBLog::DynamicLoader);
593 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
594 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
600 if (!
ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) {
602 log->
PutCString(
"Failed reading image infos array.");
606 LLDB_LOGF(log,
"Removing %d modules.", image_infos_count);
609 for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
611 LLDB_LOGF(log,
"Removing module at address=0x%16.16" PRIx64
".",
612 image_infos[idx].address);
613 image_infos[idx].PutToLog(log);
628 if (image_infos[idx].address == (*pos).address) {
629 image_infos[idx].uuid = (*pos).uuid;
636 if (unload_image_module_sp.get()) {
643 LLDB_LOGF(log,
"Could not find module for unloading info entry:");
644 image_infos[idx].PutToLog(log);
658 LLDB_LOGF(log,
"Could not find image_info entry for unloading image:");
659 image_infos[idx].PutToLog(log);
663 if (unloaded_module_list.
GetSize() > 0) {
667 log,
"DynamicLoaderMacOSXDYLD::ModulesDidUnload");
676 lldb::addr_t image_infos_addr, uint32_t image_infos_count,
677 ImageInfo::collection &image_infos) {
678 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
682 image_infos.resize(image_infos_count);
683 const size_t count = image_infos.size() * 3 * addr_size;
688 if (bytes_read == count) {
693 i < image_infos.size() && info_data_ref.
ValidOffset(info_data_offset);
695 image_infos[i].address = info_data_ref.
GetAddress(&info_data_offset);
703 if (
error.Success()) {
704 image_infos[i].file_spec.SetFile(raw_path, FileSpec::Style::native);
718 Log *log =
GetLog(LLDBLog::DynamicLoader);
720 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
721 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
740 DEBUG_PRINTF(
"%s",
"unable to read all data for all_dylib_infos.");
754 if (!module_sp->IsLoadedInTarget(&target)) {
760 not_loaded_modules.
Append(module_sp);
764 if (not_loaded_modules.
GetSize() != 0) {
778 llvm::MachO::mach_header *header,
784 if (bytes_read ==
sizeof(llvm::MachO::mach_header)) {
786 ::memset(header, 0,
sizeof(llvm::MachO::mach_header));
792 header->magic = data.
GetU32(&offset);
796 switch (header->magic) {
797 case llvm::MachO::MH_MAGIC:
798 case llvm::MachO::MH_CIGAM:
800 load_cmd_addr +=
sizeof(llvm::MachO::mach_header);
803 case llvm::MachO::MH_MAGIC_64:
804 case llvm::MachO::MH_CIGAM_64:
806 load_cmd_addr +=
sizeof(llvm::MachO::mach_header_64);
814 if (data.
GetU32(&offset, &header->cputype,
815 (
sizeof(llvm::MachO::mach_header) /
sizeof(uint32_t)) -
817 if (load_command_data ==
nullptr)
824 size_t load_cmd_bytes_read =
826 load_cmd_data_sp->GetByteSize(),
error);
828 if (load_cmd_bytes_read == header->sizeofcmds) {
831 load_command_data->
SetData(load_cmd_data_sp, 0, header->sizeofcmds);
851 dylib_info.
Clear(
true);
853 for (cmd_idx = 0; cmd_idx < dylib_info.
header.ncmds; cmd_idx++) {
858 sizeof(llvm::MachO::load_command))) {
859 llvm::MachO::load_command load_cmd;
861 load_cmd.cmd = data.
GetU32(&offset);
862 load_cmd.cmdsize = data.
GetU32(&offset);
863 switch (load_cmd.cmd) {
864 case llvm::MachO::LC_SEGMENT: {
866 (
const char *)data.
GetData(&offset, 16), 16);
875 dylib_info.
segments.push_back(segment);
878 case llvm::MachO::LC_SEGMENT_64: {
880 (
const char *)data.
GetData(&offset, 16), 16);
885 dylib_info.
segments.push_back(segment);
888 case llvm::MachO::LC_ID_DYLINKER:
889 if (lc_id_dylinker) {
891 load_cmd_offset + data.
GetU32(&offset);
892 const char *path = data.
PeekCStr(name_offset);
893 lc_id_dylinker->
SetFile(path, FileSpec::Style::native);
898 case llvm::MachO::LC_UUID:
906 offset = load_cmd_offset + load_cmd.cmdsize;
916 const size_t num_sections = dylib_info.
segments.size();
917 for (
size_t i = 0; i < num_sections; ++i) {
920 if ((dylib_info.
segments[i].fileoff == 0 &&
921 dylib_info.
segments[i].filesize > 0) ||
922 (dylib_info.
segments[i].name ==
"__TEXT")) {
935 ImageInfo::collection &image_infos, uint32_t infos_count,
936 bool update_executable) {
939 for (uint32_t i = 0; i < infos_count; i++) {
940 if (!image_infos[i].UUIDValid()) {
942 if (!
ReadMachHeader(image_infos[i].address, &image_infos[i].header,
948 if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE)
955 if (exe_idx < image_infos.size()) {
956 const bool can_create =
true;
958 can_create,
nullptr));
977 if (dyld_module_sp) {
979 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
996 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
997 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
999 "dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64
1000 ", notify=0x%8.8" PRIx64
" }",
1009 for (i = 0; i < count; i++)
1015 DEBUG_PRINTF(
"DynamicLoaderMacOSXDYLD::%s() process state = %s\n",
1027 if (dyld_module_sp) {
1028 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1063 error.SetErrorString(
"unsafe to load or unload shared libraries");
1085 uint32_t version_or_magic =
1087 if (version_or_magic !=
static_cast<uint32_t
>(-1) &&
1088 version_or_magic != llvm::MachO::MH_MAGIC &&
1089 version_or_magic != llvm::MachO::MH_CIGAM &&
1090 version_or_magic != llvm::MachO::MH_MAGIC_64 &&
1091 version_or_magic != llvm::MachO::MH_CIGAM_64 &&
1092 version_or_magic >= 13) {
1095 if (wordsize == 8) {
1096 sharedCacheUUID_address =
1097 all_image_infos + 160;
1099 if (wordsize == 4) {
1100 sharedCacheUUID_address =
1101 all_image_infos + 84;
1104 uuid_t shared_cache_uuid;
1107 uuid =
UUID(shared_cache_uuid, 16);
1113 if (version_or_magic >= 15) {
1116 addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16;
1157 return "Dynamic loader plug-in that watches for shared library loads/unloads "
1158 "in MacOSX user processes.";
1162 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1165 case llvm::MachO::MH_MAGIC:
1166 case llvm::MachO::MH_CIGAM:
1169 case llvm::MachO::MH_MAGIC_64:
1170 case llvm::MachO::MH_CIGAM_64:
1181 case llvm::MachO::MH_MAGIC:
1182 case llvm::MachO::MH_MAGIC_64:
1185 case llvm::MachO::MH_CIGAM:
1186 case llvm::MachO::MH_CIGAM_64:
static llvm::raw_ostream & error(Stream &strm)
#define DEBUG_PRINTF(fmt,...)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
bool ReadDYLDInfoFromMemoryAndSetNotificationCallback(lldb::addr_t addr)
void PutToLog(lldb_private::Log *log) const
bool RemoveModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, uint32_t image_infos_count)
~DynamicLoaderMacOSXDYLD() override
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos, uint32_t infos_count, bool update_executable)
static llvm::StringRef GetPluginNameStatic()
void DoInitialImageFetch() override
static llvm::StringRef GetPluginDescriptionStatic()
static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic)
void ClearNotificationBreakpoint() override
DynamicLoaderMacOSXDYLD(lldb_private::Process *process)
bool SetNotificationBreakpoint() override
bool InitializeFromAllImageInfos()
bool ReadImageInfos(lldb::addr_t image_infos_addr, uint32_t image_infos_count, ImageInfo::collection &image_infos)
bool DidSetNotificationBreakpoint() override
uint32_t ParseLoadCommands(const lldb_private::DataExtractor &data, ImageInfo &dylib_info, lldb_private::FileSpec *lc_id_dylinker)
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
uint32_t m_dyld_all_image_infos_stop_id
bool ReadMachHeader(lldb::addr_t addr, llvm::MachO::mach_header *header, lldb_private::DataExtractor *load_command_data)
bool ProcessDidExec() override
Called after attaching a process.
bool IsFullyInitialized() override
Return whether the dynamic loader is fully initialized and it's safe to call its APIs.
bool GetSharedCacheInformation(lldb::addr_t &base_address, lldb_private::UUID &uuid, lldb_private::LazyBool &using_shared_cache, lldb_private::LazyBool &private_shared_cache) override
Get information about the shared cache for a process, if possible.
bool AddModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, uint32_t image_infos_count)
static bool NotifyBreakpointHit(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
bool m_process_image_addr_is_all_images_infos
std::recursive_mutex m_mutex
DYLDAllImageInfos m_dyld_all_image_infos
bool ReadAllImageInfosStructure()
lldb::user_id_t m_break_id
lldb::addr_t m_dyld_all_image_infos_addr
bool NeedToDoInitialImageFetch() override
A section + offset based address class.
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
void Clear()
Clear the object's state.
An architecture specification class.
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
llvm::Triple & GetTriple()
Architecture triple accessor.
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
void SetCallback(BreakpointHitCallback callback, void *baton, bool is_synchronous=false)
Set the callback action invoked when the breakpoint is hit.
Generic representation of a type in a programming language.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len)
Set the C string value with the minimum length between fixed_cstr_len and the actual length of the C ...
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
lldb_private::ConstString name
lldb::ModuleSP GetDYLDModule()
void UpdateSpecialBinariesFromNewImageInfos(ImageInfo::collection &image_infos)
ImageInfo::collection m_dyld_image_infos
uint32_t m_dyld_image_infos_stop_id
std::recursive_mutex & GetMutex() const
void UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info)
bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos)
lldb::ModuleWP m_libpthread_module_wp
bool JSONImageInformationIntoImageInfo(lldb_private::StructuredData::ObjectSP image_details, ImageInfo::collection &image_infos)
bool UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo &info)
lldb::ModuleSP FindTargetModuleForImageInfo(ImageInfo &image_info, bool can_create, bool *did_create_ptr)
void SetDYLDModule(lldb::ModuleSP &dyld_module_sp)
lldb_private::Address m_pthread_getspecific_addr
bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info)
A plug-in interface definition class for dynamic loaders.
Process * m_process
The process that this dynamic loader plug-in is tracking.
bool GetStopWhenImagesChange() const
Get whether the process should stop when images change.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread & GetThreadRef() const
Returns a reference to the thread object.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
void PutCString(const char *cstr)
A collection class for Module objects.
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
bool Remove(const lldb::ModuleSP &module_sp, bool notify=true)
Remove a module from the module list.
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
ModuleIterable Modules() const
size_t GetSize() const
Gets the size of the module list.
void LogUUIDAndPaths(Log *log, const char *prefix_cstr)
A class that describes an executable image and its associated object and symbol files.
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
A plug-in interface definition class for object file parsers.
virtual ArchSpec GetArchitecture()=0
Get the ArchSpec for this object file.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
ThreadList & GetThreadList()
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
virtual lldb_private::StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address, lldb::addr_t image_count)
Retrieve the list of shared libraries that are loaded for this process This method is used on pre-mac...
lldb::StateType GetState()
Get accessor for the current process state.
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
uint32_t GetAddressByteSize() const
uint32_t GetStopID() const
const lldb::ABISP & GetABI()
Target & GetTarget()
Get the target object pointer for this module.
unsigned int UInt(unsigned int fail_value=0) const
unsigned long long ULongLong(unsigned long long fail_value=0) const
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
ExecutionContextRef exe_ctx_ref
lldb::break_id_t GetID() const
const char * GetData() const
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
std::shared_ptr< Object > ObjectSP
lldb::addr_t GetLoadAddress(Target *target) const
ConstString GetName() const
void ModulesDidLoad(ModuleList &module_list)
Module * GetExecutableModulePointer()
bool RemoveBreakpointByID(lldb::break_id_t break_id)
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
const ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
uint32_t GetSize(bool can_update=true)
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
void PushValue(const Value &value)
Value * GetValueAtIndex(size_t idx)
const Scalar & GetScalar() const
void SetCompilerType(const CompilerType &compiler_type)
void SetValueType(ValueType value_type)
uint8_t * GetBytes()
Get a pointer to the data.
#define LLDB_INVALID_BREAK_ID
#define UNUSED_IF_ASSERT_DISABLED(x)
#define LLDB_BREAK_ID_IS_VALID(bid)
#define LLDB_INVALID_ADDRESS
lldb::ByteOrder InlHostByteOrder()
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
@ eEncodingUint
unsigned integer
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
std::shared_ptr< lldb_private::Module > ModuleSP
uint32_t dylib_info_count
bool processDetachedFromSharedRegion
bool libSystemInitialized
lldb::addr_t dylib_info_addr
lldb::addr_t notification
lldb::addr_t dyldImageLoadAddress
lldb_private::UUID uuid
UUID for this dylib if it has one, else all zeros.
lldb::addr_t address
Address of mach header for this dylib.
lldb::addr_t slide
The amount to slide all segments by if there is a global slide.
llvm::MachO::mach_header header
The mach header for this image.
std::vector< Segment > segments
All segment vmaddr and vmsize pairs for this executable (from memory of inferior).
lldb_private::FileSpec file_spec
Resolved path for this dylib.
void Clear(bool load_cmd_data_only)
uint32_t GetAddressByteSize()
lldb::user_id_t GetID() const
Get accessor for the user ID.