25#include "llvm/Support/Error.h"
52 const llvm::Triple &triple_ref =
54 switch (triple_ref.getOS()) {
55 case llvm::Triple::Darwin:
56 case llvm::Triple::MacOSX:
57 case llvm::Triple::IOS:
58 case llvm::Triple::TvOS:
59 case llvm::Triple::WatchOS:
60 case llvm::Triple::BridgeOS:
61 case llvm::Triple::DriverKit:
62 case llvm::Triple::XROS:
63 create = triple_ref.getVendor() == llvm::Triple::Apple;
98 std::lock_guard<std::recursive_mutex> baseclass_guard(
GetMutex());
99 bool did_exec =
false;
102 if (
m_process->GetThreadList().GetSize() == 1) {
123 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
125 if (symbol->
GetName() ==
"_dyld_start")
144 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
161 m_process->GetDynamicLoaderProcessState());
162 if (!process_state_sp)
164 if (process_state_sp->GetAsDictionary()->HasKey(
"error"))
166 if (!process_state_sp->GetAsDictionary()->HasKey(
"process_state string"))
168 std::string proc_state = process_state_sp->GetAsDictionary()
169 ->GetValueForKey(
"process_state string")
173 if (proc_state ==
"dyld_process_state_not_started" ||
174 proc_state ==
"dyld_process_state_dyld_initialized" ||
175 proc_state ==
"dyld_process_state_terminated_before_inits") {
204 m_process->GetLoadedDynamicLibrariesInfos(
206 ImageInfo::collection image_infos;
207 if (all_image_info_json_sp.get() &&
208 all_image_info_json_sp->GetAsDictionary() &&
209 all_image_info_json_sp->GetAsDictionary()->HasKey(
"images") &&
210 all_image_info_json_sp->GetAsDictionary()
211 ->GetValueForKey(
"images")
220 ->GetValueForKey(
"images")
223 images->
GetItemAtIndex(0)->GetAsDictionary()->HasKey(
"mach_header")) {
226 LLDB_LOGF(log,
"Initial module fetch: Adding %" PRIu64
" modules.\n",
227 (uint64_t)image_infos.size());
238 size_t image_count = images->
GetSize();
239 std::vector<addr_t> load_addresses;
240 for (
size_t i = 0; i < image_count; i++) {
243 if (image && image->
HasKey(
"load_address")) {
247 load_addresses.push_back(val);
329 argument_values.
PushValue(headers_value);
332 if (abi->GetArgumentValues(thread, argument_values)) {
335 if (dyld_mode !=
static_cast<uint32_t
>(-1)) {
338 uint32_t image_infos_count =
340 if (image_infos_count !=
static_cast<uint32_t
>(-1)) {
343 if (header_array !=
static_cast<uint64_t
>(-1)) {
344 std::vector<addr_t> image_load_addresses;
357 image_load_addresses = thread.FetchNewlyAddedBinaries();
359 if (image_load_addresses.size() != image_infos_count) {
360 image_load_addresses.clear();
366 addrsize * 3 * image_infos_count, 0);
368 if (process->
ReadMemory(header_array, buffer_sp->GetBytes(),
369 buffer_sp->GetByteSize(),
370 read_error) == buffer_sp->GetByteSize() &&
376 for (uint64_t i = 0; i < image_infos_count; i++) {
378 image_load_addresses.push_back(addr);
379 offset += 2 * addrsize;
384 "DynamicLoaderMacOS::NotifyBreakpointHit unable "
385 "to read binary mach-o load address at 0x%" PRIx64,
388 if (dyld_mode == 0) {
405 thread.FetchDetailedBinariesInfo());
407 }
else if (dyld_mode == 1) {
410 }
else if (dyld_mode == 2) {
413 }
else if (dyld_mode == 3 && image_infos_count == 1) {
425 addr_t notification_location = all_image_infos + 4 +
428 llvm::Expected<lldb::addr_t> notification_addr =
430 if (!notification_addr) {
431 llvm::consumeError(notification_addr.takeError());
433 "DynamicLoaderMacOS::NotifyBreakpointHit unable "
434 "to read address of dyld-handover notification function at "
436 notification_location);
449 "no ABI plugin located for triple " +
451 ": shared libraries will not be registered",
460 if (!binaries_info_sp)
463 if (!dict->HasKey(
"images"))
467 return images->GetSize();
474 const std::vector<lldb::addr_t> &load_addresses,
477 ImageInfo::collection image_infos;
478 if (load_addresses.empty())
497 const size_t image_fetch_max = 600;
499 size_t total_image_size = load_addresses.size();
500 while (fetched < total_image_size) {
501 size_t this_fetch_amt =
502 std::min(image_fetch_max, total_image_size - fetched);
503 std::vector<addr_t> fetch_binaries(load_addresses.begin() + fetched,
504 load_addresses.begin() + fetched +
507 LLDB_LOGF(log,
"Adding %" PRId64
" modules.",
508 (uint64_t)fetch_binaries.size());
520 fetched += this_fetch_amt;
547 return notification_addr;
551 return notification_addr;
553 const uint32_t addr_size =
554 m_process->GetTarget().GetArchitecture().GetAddressByteSize();
555 offset_t registered_infos_addr_offset =
577 llvm::Expected<lldb::addr_t> registered_infos_addr =
578 m_process->ReadPointerFromMemory(all_image_infos_addr +
579 registered_infos_addr_offset);
580 if (!registered_infos_addr) {
581 llvm::consumeError(registered_infos_addr.takeError());
582 return notification_addr;
584 if (*registered_infos_addr != all_image_infos_addr)
585 return notification_addr;
587 offset_t notification_fptr_offset =
sizeof(uint32_t) +
591 llvm::Expected<lldb::addr_t> notification_fptr =
592 m_process->ReadPointerFromMemory(all_image_infos_addr +
593 notification_fptr_offset);
594 if (notification_fptr)
595 notification_addr =
m_process->FixCodeAddress(*notification_fptr);
597 llvm::consumeError(notification_fptr.takeError());
598 return notification_addr;
614 bool internal =
true;
615 bool hardware =
false;
619 dyld_filelist.
Append(dyld_sp->GetFileSpec());
623 .CreateBreakpoint(&dyld_filelist, source_files,
624 "lldb_image_notifier", eFunctionNameTypeFull,
639 .CreateBreakpoint(&dyld_filelist, source_files,
640 "gdb_image_notifier", eFunctionNameTypeFull,
643 skip_prologue, internal, hardware)
667 m_process->GetTarget().CreateBreakpoint(so_addr,
true,
false).get();
681 addr_t notification_address) {
684 notification_address,
true,
false);
687 dyld_handover_bp->SetOneShot(
true);
705 std::vector<uint32_t> match_indexes;
706 ConstString g_symbol_name(
"_dyld_global_lock_held");
707 uint32_t num_matches = 0;
709 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
710 if (num_matches == 1) {
711 const Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
733 std::lock_guard<std::recursive_mutex> guard(target_modules.
GetMutex());
738 if (module_sp->GetFileSpec().GetFilename() == g_libdyld_name) {
765 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0,
error);
766 if (lock_held != 0) {
779 "the dyld lock symbol");
787 std::optional<uint64_t> &size) {
797 if (info.get() && info->GetAsDictionary()) {
798 info_dict = info->GetAsDictionary();
809 if (info_dict && info_dict->
HasKey(
"shared_cache_uuid") &&
810 info_dict->
HasKey(
"no_shared_cache") &&
811 info_dict->
HasKey(
"shared_cache_private_cache") &&
812 info_dict->
HasKey(
"shared_cache_base_address")) {
813 base_address = info_dict->
GetValueForKey(
"shared_cache_base_address")
815 std::string uuid_str = std::string(
816 info_dict->
GetValueForKey(
"shared_cache_uuid")->GetStringValue());
817 if (!uuid_str.empty())
819 if (!info_dict->
GetValueForKey(
"no_shared_cache")->GetBooleanValue())
828 if (info_dict->
HasKey(
"shared_cache_path")) {
829 llvm::StringRef filepath =
831 shared_cache_path.
SetPath(filepath);
833 if (info_dict->
HasKey(
"shared_cache_size")) {
855 return "Dynamic loader plug-in that watches for shared library loads/unloads "
856 "in MacOSX user processes.";
static llvm::raw_ostream & error(Stream &strm)
static size_t LibraryInfosCount(StructuredData::ObjectSP binaries_info_sp)
#define LLDB_LOGF(log,...)
bool SetNotificationBreakpoint() override
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
uint32_t m_image_infos_stop_id
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
void PutToLog(lldb_private::Log *log) const
bool IsFullyInitialized() override
Return whether the dynamic loader is fully initialized and it's safe to call its APIs.
lldb::addr_t GetDyldLockVariableAddressFromModule(lldb_private::Module *module)
bool m_libsystem_fully_initalized
bool NeedToDoInitialImageFetch() override
std::recursive_mutex m_mutex
void DoInitialImageFetch() override
static llvm::StringRef GetPluginNameStatic()
static llvm::StringRef GetPluginDescriptionStatic()
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_path, std::optional< uint64_t > &size) override
Get information about the shared cache for a process, if possible.
lldb::user_id_t m_break_id
lldb::addr_t GetNotificationFuncAddrFromImageInfos()
bool SetDYLDHandoverBreakpoint(lldb::addr_t notification_address)
void ClearNotificationBreakpoint() override
void AddBinaries(const std::vector< lldb::addr_t > &load_addresses, lldb_private::StructuredData::ObjectSP expedited_binary_infos)
void ClearDYLDHandoverBreakpoint()
static bool NotifyBreakpointHit(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
lldb::addr_t m_maybe_image_infos_address
bool ProcessDidExec() override
Called after attaching a process.
~DynamicLoaderMacOS() override
lldb::user_id_t m_dyld_handover_break_id
DynamicLoaderMacOS(lldb_private::Process *process)
bool DidSetNotificationBreakpoint() override
A section + offset based address class.
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
void SetRawAddress(lldb::addr_t addr)
bool IsValid() const
Check if the object state is valid.
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.
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.
bool HasResolvedLocations() const
Return whether this breakpoint has any resolved locations.
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.
A uniqued constant string class.
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::ModuleSP GetDYLDModule()
DynamicLoaderDarwin(lldb_private::Process *process)
uint32_t m_dyld_image_infos_stop_id
std::recursive_mutex & GetMutex() const
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)
void UpdateSpecialBinariesFromPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
bool AddModulesUsingPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
static bool UseDYLDSPI(lldb_private::Process *process)
lldb_private::Address m_pthread_getspecific_addr
void UnloadImages(const std::vector< lldb::addr_t > &solib_addresses)
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 Append(const FileSpec &file)
Append a FileSpec object to the list.
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
A collection class for Module objects.
ModuleIterableNoLocking ModulesNoLocking() const
std::recursive_mutex & GetMutex() const
void Clear()
Clear the object's state.
ModuleIterable Modules() const
size_t GetSize() const
Gets the size of the module list.
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.
Symtab * GetSymtab(bool can_create=true)
Get the module's symbol table.
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.
virtual size_t ReadMemory(const ProcessAddress &process_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
llvm::Expected< lldb::addr_t > ReadPointerFromMemory(lldb::addr_t vm_addr)
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
lldb::addr_t FixCodeAddress(lldb::addr_t pc)
Some targets might use bits in a code address to indicate a mode switch, ARM uses bit zero to signify...
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.
static Status FromErrorString(const char *str)
bool Success() const
Test for success condition.
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
ObjectSP GetItemAtIndex(size_t idx) const
ObjectSP GetValueForKey(llvm::StringRef key) const
bool HasKey(llvm::StringRef key) const
std::shared_ptr< Object > ObjectSP
Defines a symbol context baton that can be handed other debug core functions.
bool ValueIsAddress() const
Address & GetAddressRef()
ConstString GetName() const
Module * GetExecutableModulePointer()
Debugger & GetDebugger() const
void ClearSectionLoadList()
const ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
Represents UUID's of various sizes.
bool SetFromStringRef(llvm::StringRef str)
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)
#define LLDB_INVALID_BREAK_ID
#define LLDB_BREAK_ID_IS_VALID(bid)
#define LLDB_INVALID_ADDRESS
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.
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
@ eEncodingUint
unsigned integer
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
@ eBinaryInformationLevelFull
@ eBinaryInformationLevelAddrOnly
std::shared_ptr< lldb_private::Module > ModuleSP
lldb::user_id_t GetID() const
Get accessor for the user ID.