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 =
71 process->GetTarget().GetArchitecture().GetTriple();
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::BridgeOS:
79 case llvm::Triple::DriverKit:
80 case llvm::Triple::XROS:
81 create = triple_ref.getVendor() == llvm::Triple::Apple;
114 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
115 bool did_exec =
false;
118 if (
m_process->GetThreadList().GetSize() == 1) {
129 shlib_addr ==
m_dyld.address) {
142 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
144 if (symbol->
GetName() ==
"_dyld_start")
162 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
194 m_process->GetTarget().GetArchitecture().GetByteOrder();
200 uint32_t magic = data.
GetU32(&offset);
202 case llvm::MachO::MH_MAGIC:
203 case llvm::MachO::MH_MAGIC_64:
204 case llvm::MachO::MH_CIGAM:
205 case llvm::MachO::MH_CIGAM_64:
233 Module *executable =
m_process->GetTarget().GetExecutableModulePointer();
239 }
else if (exe_arch.
GetMachine() == llvm::Triple::arm ||
240 exe_arch.
GetMachine() == llvm::Triple::thumb ||
241 exe_arch.
GetMachine() == llvm::Triple::aarch64 ||
242 exe_arch.
GetMachine() == llvm::Triple::aarch64_32) {
253 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
255 static ConstString g_dyld_all_image_infos(
"dyld_all_image_infos");
256 static ConstString g_new_dyld_all_image_infos(
"dyld4::dyld_all_image_infos");
258 if (
m_dyld.header.filetype == llvm::MachO::MH_DYLINKER) {
274 dyld_module_sp.get()) {
275 const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
278 symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
288 dyld_module_sp->GetSectionList()->FindSectionByName(g_sect_name);
289 if (dyld_aii_section_sp) {
290 Address dyld_aii_addr(dyld_aii_section_sp, 0);
303 if (dyld_module_sp) {
309 modules.
Append(dyld_module_sp);
389 if (abi->GetArgumentValues(exe_ctx.
GetThreadRef(), argument_values)) {
392 if (dyld_mode !=
static_cast<uint32_t
>(-1)) {
395 uint32_t image_infos_count =
397 if (image_infos_count !=
static_cast<uint32_t
>(-1)) {
405 if (dyld_mode == 0) {
412 image_infos_addr, image_infos_count);
420 "no ABI plugin located for triple " +
422 ": shared libraries will not be registered",
431 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
440 m_process->GetTarget().GetArchitecture().GetByteOrder();
442 m_process->GetTarget().GetArchitecture().GetAddressByteSize();
448 const size_t count_v2 =
sizeof(uint32_t) +
455 const size_t count_v11 = count_v2 + addr_size +
469 const size_t count_v13 = count_v11 + addr_size +
472 assert(
sizeof(buf) >= count_v13);
500 const size_t bytes_read =
502 if (bytes_read == count) {
512 offset += addr_size - 2;
515 offset += addr_size * 8;
516 uint64_t dyld_all_image_infos_addr = data.
GetAddress(&offset);
532 uint64_t image_infos_offset =
533 dyld_all_image_infos_addr -
535 uint64_t notification_offset =
552 lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
553 ImageInfo::collection image_infos;
555 LLDB_LOGF(log,
"Adding %d modules.\n", image_infos_count);
557 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
558 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
563 m_process->GetLoadedDynamicLibrariesInfos(image_infos_addr,
565 if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() &&
566 image_infos_json_sp->GetAsDictionary()->HasKey(
"images") &&
567 image_infos_json_sp->GetAsDictionary()
568 ->GetValueForKey(
"images")
570 image_infos_json_sp->GetAsDictionary()
571 ->GetValueForKey(
"images")
573 ->GetSize() == image_infos_count) {
574 bool return_value =
false;
584 if (!
ReadImageInfos(image_infos_addr, image_infos_count, image_infos))
594 lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
595 ImageInfo::collection image_infos;
598 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
599 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
605 if (!
ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) {
607 log->
PutCString(
"Failed reading image infos array.");
611 LLDB_LOGF(log,
"Removing %d modules.", image_infos_count);
614 for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
616 LLDB_LOGF(log,
"Removing module at address=0x%16.16" PRIx64
".",
617 image_infos[idx].address);
618 image_infos[idx].PutToLog(log);
633 if (image_infos[idx].address == (*pos).address) {
634 image_infos[idx].uuid = (*pos).uuid;
641 if (unload_image_module_sp.get()) {
648 LLDB_LOGF(log,
"Could not find module for unloading info entry:");
649 image_infos[idx].PutToLog(log);
663 LLDB_LOGF(log,
"Could not find image_info entry for unloading image:");
664 image_infos[idx].PutToLog(log);
668 if (unloaded_module_list.
GetSize() > 0) {
672 log,
"DynamicLoaderMacOSXDYLD::ModulesDidUnload");
674 m_process->GetTarget().GetImages().Remove(unloaded_module_list);
681 lldb::addr_t image_infos_addr, uint32_t image_infos_count,
682 ImageInfo::collection &image_infos) {
683 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
685 const uint32_t addr_size =
m_dyld.GetAddressByteSize();
687 image_infos.resize(image_infos_count);
688 const size_t count = image_infos.size() * 3 * addr_size;
691 const size_t bytes_read =
m_process->ReadMemory(
693 if (bytes_read == count) {
698 i < image_infos.size() && info_data_ref.
ValidOffset(info_data_offset);
700 image_infos[i].address = info_data_ref.
GetAddress(&info_data_offset);
705 m_process->ReadCStringFromMemory(path_addr, raw_path,
sizeof(raw_path),
708 if (
error.Success()) {
709 image_infos[i].file_spec.SetFile(raw_path, FileSpec::Style::native);
725 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
726 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
745 DEBUG_PRINTF(
"%s",
"unable to read all data for all_dylib_infos.");
759 if (!module_sp->IsLoadedInTarget(&target)) {
765 not_loaded_modules.
Append(module_sp);
769 if (not_loaded_modules.
GetSize() != 0) {
783 llvm::MachO::mach_header *header,
789 if (bytes_read ==
sizeof(llvm::MachO::mach_header)) {
791 ::memset(header, 0,
sizeof(llvm::MachO::mach_header));
797 header->magic = data.
GetU32(&offset);
801 switch (header->magic) {
802 case llvm::MachO::MH_MAGIC:
803 case llvm::MachO::MH_CIGAM:
805 load_cmd_addr +=
sizeof(llvm::MachO::mach_header);
808 case llvm::MachO::MH_MAGIC_64:
809 case llvm::MachO::MH_CIGAM_64:
811 load_cmd_addr +=
sizeof(llvm::MachO::mach_header_64);
819 if (data.
GetU32(&offset, &header->cputype,
820 (
sizeof(llvm::MachO::mach_header) /
sizeof(uint32_t)) -
822 if (load_command_data ==
nullptr)
829 size_t load_cmd_bytes_read =
830 m_process->ReadMemory(load_cmd_addr, load_cmd_data_sp->GetBytes(),
831 load_cmd_data_sp->GetByteSize(),
error);
833 if (load_cmd_bytes_read == header->sizeofcmds) {
836 load_command_data->
SetData(load_cmd_data_sp, 0, header->sizeofcmds);
856 dylib_info.
Clear(
true);
858 for (cmd_idx = 0; cmd_idx < dylib_info.
header.ncmds; cmd_idx++) {
863 sizeof(llvm::MachO::load_command))) {
864 llvm::MachO::load_command load_cmd;
866 load_cmd.cmd = data.
GetU32(&offset);
867 load_cmd.cmdsize = data.
GetU32(&offset);
868 switch (load_cmd.cmd) {
869 case llvm::MachO::LC_SEGMENT: {
871 (
const char *)data.
GetData(&offset, 16), 16);
880 dylib_info.
segments.push_back(segment);
883 case llvm::MachO::LC_SEGMENT_64: {
885 (
const char *)data.
GetData(&offset, 16), 16);
890 dylib_info.
segments.push_back(segment);
893 case llvm::MachO::LC_ID_DYLINKER:
894 if (lc_id_dylinker) {
896 load_cmd_offset + data.
GetU32(&offset);
897 const char *path = data.
PeekCStr(name_offset);
898 lc_id_dylinker->
SetFile(path, FileSpec::Style::native);
903 case llvm::MachO::LC_UUID:
911 offset = load_cmd_offset + load_cmd.cmdsize;
921 const size_t num_sections = dylib_info.
segments.size();
922 for (
size_t i = 0; i < num_sections; ++i) {
925 if ((dylib_info.
segments[i].fileoff == 0 &&
926 dylib_info.
segments[i].filesize > 0) ||
927 (dylib_info.
segments[i].name ==
"__TEXT")) {
940 ImageInfo::collection &image_infos, uint32_t infos_count,
941 bool update_executable) {
944 for (uint32_t i = 0; i < infos_count; i++) {
945 if (!image_infos[i].UUIDValid()) {
947 if (!
ReadMachHeader(image_infos[i].address, &image_infos[i].header,
953 if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE)
960 if (exe_idx < image_infos.size()) {
961 const bool can_create =
true;
963 can_create,
nullptr));
979 m_process->GetTarget().SetExecutableModule(exe_module_sp,
982 if (dyld_module_sp) {
984 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1001 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1002 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1004 "dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64
1005 ", notify=0x%8.8" PRIx64
" }",
1014 for (i = 0; i < count; i++)
1020 DEBUG_PRINTF(
"DynamicLoaderMacOSXDYLD::%s() process state = %s\n",
1028 bool resolved =
m_process->GetTarget().ResolveLoadAddress(
1032 if (dyld_module_sp) {
1033 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1036 resolved =
m_process->GetTarget().ResolveLoadAddress(
1043 m_process->GetTarget().CreateBreakpoint(so_addr,
true,
false).get();
1090 uint32_t version_or_magic =
1091 m_process->ReadUnsignedIntegerFromMemory(all_image_infos, 4, -1, err);
1092 if (version_or_magic !=
static_cast<uint32_t
>(-1) &&
1093 version_or_magic != llvm::MachO::MH_MAGIC &&
1094 version_or_magic != llvm::MachO::MH_CIGAM &&
1095 version_or_magic != llvm::MachO::MH_MAGIC_64 &&
1096 version_or_magic != llvm::MachO::MH_CIGAM_64 &&
1097 version_or_magic >= 13) {
1099 int wordsize =
m_process->GetAddressByteSize();
1100 if (wordsize == 8) {
1101 sharedCacheUUID_address =
1102 all_image_infos + 160;
1104 if (wordsize == 4) {
1105 sharedCacheUUID_address =
1106 all_image_infos + 84;
1109 uuid_t shared_cache_uuid;
1110 if (
m_process->ReadMemory(sharedCacheUUID_address, shared_cache_uuid,
1112 uuid =
UUID(shared_cache_uuid, 16);
1118 if (version_or_magic >= 15) {
1121 addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16;
1123 base_address =
m_process->ReadUnsignedIntegerFromMemory(
1162 return "Dynamic loader plug-in that watches for shared library loads/unloads "
1163 "in MacOSX user processes.";
1167 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
1169 switch (
m_dyld.header.magic) {
1170 case llvm::MachO::MH_MAGIC:
1171 case llvm::MachO::MH_CIGAM:
1174 case llvm::MachO::MH_MAGIC_64:
1175 case llvm::MachO::MH_CIGAM_64:
1186 case llvm::MachO::MH_MAGIC:
1187 case llvm::MachO::MH_MAGIC_64:
1190 case llvm::MachO::MH_CIGAM:
1191 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.
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.
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
bool UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info)
lldb::ModuleSP GetDYLDModule()
ImageInfo::collection m_dyld_image_infos
DynamicLoaderDarwin(lldb_private::Process *process)
uint32_t m_dyld_image_infos_stop_id
std::recursive_mutex & GetMutex() const
lldb::ModuleSP FindTargetModuleForImageInfo(const ImageInfo &image_info, bool can_create, bool *did_create_ptr)
bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos)
std::vector< std::pair< ImageInfo, lldb::ModuleSP > > PreloadModulesFromImageInfos(const 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)
void UpdateSpecialBinariesFromPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
bool AddModulesUsingPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
void SetDYLDModule(lldb::ModuleSP &dyld_module_sp)
static bool UseDYLDSPI(lldb_private::Process *process)
lldb_private::Address m_pthread_getspecific_addr
bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info)
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.
DynamicLoader(Process *process)
Construct with a process.
"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.
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.
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.
static Status FromErrorString(const char *str)
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()
Debugger & GetDebugger() const
const ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
Represents UUID's of various sizes.
void PushValue(const Value &value)
Value * GetValueAtIndex(size_t idx)
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
@ Scalar
A raw scalar value.
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
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).
void Clear(bool load_cmd_data_only)
lldb::user_id_t GetID() const
Get accessor for the user ID.