#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Section.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
#include "DynamicLoaderDarwin.h"
#include "DynamicLoaderMacOS.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
using namespace lldb;
using namespace lldb_private;
DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
bool force) {
bool create = force;
if (!create) {
create = true;
Module *exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module) {
ObjectFile *object_file = exe_module->GetObjectFile();
if (object_file) {
create = (object_file->GetStrata() == ObjectFile::eStrataUser);
}
}
if (create) {
const llvm::Triple &triple_ref =
process->GetTarget().GetArchitecture().GetTriple();
switch (triple_ref.getOS()) {
case llvm::Triple::Darwin:
case llvm::Triple::MacOSX:
case llvm::Triple::IOS:
case llvm::Triple::TvOS:
case llvm::Triple::WatchOS:
case llvm::Triple::XROS:
case llvm::Triple::BridgeOS:
create = triple_ref.getVendor() == llvm::Triple::Apple;
break;
default:
create = false;
break;
}
}
}
if (!UseDYLDSPI(process)) {
create = false;
}
if (create)
return new DynamicLoaderMacOS(process);
return nullptr;
}
DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
: DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
m_break_id(LLDB_INVALID_BREAK_ID),
m_dyld_handover_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
m_maybe_image_infos_address(LLDB_INVALID_ADDRESS),
m_libsystem_fully_initalized(false) {}
DynamicLoaderMacOS::~DynamicLoaderMacOS() {
if (LLDB_BREAK_ID_IS_VALID(m_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
}
bool DynamicLoaderMacOS::ProcessDidExec() {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
bool did_exec = false;
if (m_process) {
if (m_process->GetThreadList().GetSize() == 1) {
if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
if (image_infos_address != m_maybe_image_infos_address) {
m_maybe_image_infos_address = image_infos_address;
did_exec = true;
}
}
if (!did_exec) {
ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
if (thread_sp) {
lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
if (frame_sp) {
const Symbol *symbol =
frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
if (symbol) {
if (symbol->GetName() == "_dyld_start")
did_exec = true;
}
}
}
}
}
}
if (did_exec) {
m_libpthread_module_wp.reset();
m_pthread_getspecific_addr.Clear();
m_libsystem_fully_initalized = false;
}
return did_exec;
}
void DynamicLoaderMacOS::DoClear() {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
if (LLDB_BREAK_ID_IS_VALID(m_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
m_break_id = LLDB_INVALID_BREAK_ID;
m_dyld_handover_break_id = LLDB_INVALID_BREAK_ID;
m_libsystem_fully_initalized = false;
}
bool DynamicLoaderMacOS::IsFullyInitialized() {
if (m_libsystem_fully_initalized)
return true;
StructuredData::ObjectSP process_state_sp(
m_process->GetDynamicLoaderProcessState());
if (!process_state_sp)
return true;
if (process_state_sp->GetAsDictionary()->HasKey("error"))
return true;
if (!process_state_sp->GetAsDictionary()->HasKey("process_state string"))
return true;
std::string proc_state = process_state_sp->GetAsDictionary()
->GetValueForKey("process_state string")
->GetAsString()
->GetValue()
.str();
if (proc_state == "dyld_process_state_not_started" ||
proc_state == "dyld_process_state_dyld_initialized" ||
proc_state == "dyld_process_state_terminated_before_inits") {
return false;
}
m_libsystem_fully_initalized = true;
return true;
}
bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
return LLDB_BREAK_ID_IS_VALID(m_break_id);
}
void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
m_break_id = LLDB_INVALID_BREAK_ID;
}
}
void DynamicLoaderMacOS::DoInitialImageFetch() {
Log *log = GetLog(LLDBLog::DynamicLoader);
UnloadAllImages();
StructuredData::ObjectSP all_image_info_json_sp(
m_process->GetLoadedDynamicLibrariesInfos());
ImageInfo::collection image_infos;
if (all_image_info_json_sp.get() &&
all_image_info_json_sp->GetAsDictionary() &&
all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
all_image_info_json_sp->GetAsDictionary()
->GetValueForKey("images")
->GetAsArray()) {
if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
image_infos)) {
LLDB_LOGF(log, "Initial module fetch: Adding %" PRId64 " modules.\n",
(uint64_t)image_infos.size());
UpdateSpecialBinariesFromNewImageInfos(image_infos);
AddModulesUsingImageInfos(image_infos);
}
}
m_dyld_image_infos_stop_id = m_process->GetStopID();
m_maybe_image_infos_address = m_process->GetImageInfoAddress();
}
bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id) {
DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
ExecutionContext exe_ctx(context->exe_ctx_ref);
Process *process = exe_ctx.GetProcessPtr();
if (process != dyld_instance->m_process)
return false;
if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
return false;
}
const lldb::ABISP &abi = process->GetABI();
if (abi) {
TypeSystemClangSP scratch_ts_sp =
ScratchTypeSystemClang::GetForTarget(process->GetTarget());
if (!scratch_ts_sp)
return false;
ValueList argument_values;
Value mode_value;
Value count_value;
Value headers_value;
CompilerType clang_void_ptr_type =
scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
CompilerType clang_uint32_type =
scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint,
32);
CompilerType clang_uint64_type =
scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint,
32);
mode_value.SetValueType(Value::ValueType::Scalar);
mode_value.SetCompilerType(clang_uint32_type);
count_value.SetValueType(Value::ValueType::Scalar);
count_value.SetCompilerType(clang_uint32_type);
headers_value.SetValueType(Value::ValueType::Scalar);
headers_value.SetCompilerType(clang_void_ptr_type);
argument_values.PushValue(mode_value);
argument_values.PushValue(count_value);
argument_values.PushValue(headers_value);
if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
uint32_t dyld_mode =
argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
if (dyld_mode != static_cast<uint32_t>(-1)) {
uint32_t image_infos_count =
argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
if (image_infos_count != static_cast<uint32_t>(-1)) {
addr_t header_array =
argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
if (header_array != static_cast<uint64_t>(-1)) {
std::vector<addr_t> image_load_addresses;
const int addrsize =
process->GetTarget().GetArchitecture().GetAddressByteSize();
for (uint64_t i = 0; i < image_infos_count; i++) {
Status error;
addr_t dyld_image_info = header_array + (addrsize * 3 * i);
addr_t addr =
process->ReadPointerFromMemory(dyld_image_info, error);
if (error.Success()) {
image_load_addresses.push_back(addr);
} else {
Debugger::ReportWarning(
"DynamicLoaderMacOS::NotifyBreakpointHit unable "
"to read binary mach-o load address at 0x%" PRIx64,
addr);
}
}
if (dyld_mode == 0) {
if (process->GetTarget().GetImages().GetSize() == 0) {
dyld_instance->ClearDYLDHandoverBreakpoint();
dyld_instance->DoInitialImageFetch();
dyld_instance->SetNotificationBreakpoint();
} else {
dyld_instance->AddBinaries(image_load_addresses);
}
} else if (dyld_mode == 1) {
dyld_instance->UnloadImages(image_load_addresses);
} else if (dyld_mode == 2) {
dyld_instance->UnloadAllImages();
} else if (dyld_mode == 3 && image_infos_count == 1) {
dyld_instance->ClearNotificationBreakpoint();
dyld_instance->UnloadAllImages();
dyld_instance->ClearDYLDModule();
process->GetTarget().GetImages().Clear();
process->GetTarget().GetSectionLoadList().Clear();
addr_t all_image_infos = process->GetImageInfoAddress();
int addr_size =
process->GetTarget().GetArchitecture().GetAddressByteSize();
addr_t notification_location = all_image_infos + 4 +
4 +
addr_size;
Status error;
addr_t notification_addr =
process->ReadPointerFromMemory(notification_location, error);
if (!error.Success()) {
Debugger::ReportWarning(
"DynamicLoaderMacOS::NotifyBreakpointHit unable "
"to read address of dyld-handover notification function at "
"0x%" PRIx64,
notification_location);
} else {
notification_addr = process->FixCodeAddress(notification_addr);
dyld_instance->SetDYLDHandoverBreakpoint(notification_addr);
}
}
}
}
}
}
} else {
Target &target = process->GetTarget();
Debugger::ReportWarning(
"no ABI plugin located for triple " +
target.GetArchitecture().GetTriple().getTriple() +
": shared libraries will not be registered",
target.GetDebugger().GetID());
}
return dyld_instance->GetStopWhenImagesChange();
}
void DynamicLoaderMacOS::AddBinaries(
const std::vector<lldb::addr_t> &load_addresses) {
Log *log = GetLog(LLDBLog::DynamicLoader);
ImageInfo::collection image_infos;
LLDB_LOGF(log, "Adding %" PRId64 " modules.",
(uint64_t)load_addresses.size());
StructuredData::ObjectSP binaries_info_sp =
m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
binaries_info_sp->GetAsDictionary()->HasKey("images") &&
binaries_info_sp->GetAsDictionary()
->GetValueForKey("images")
->GetAsArray() &&
binaries_info_sp->GetAsDictionary()
->GetValueForKey("images")
->GetAsArray()
->GetSize() == load_addresses.size()) {
if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
UpdateSpecialBinariesFromNewImageInfos(image_infos);
AddModulesUsingImageInfos(image_infos);
}
m_dyld_image_infos_stop_id = m_process->GetStopID();
}
}
void DynamicLoaderMacOS::PutToLog(Log *log) const {
if (log == nullptr)
return;
}
addr_t DynamicLoaderMacOS::GetNotificationFuncAddrFromImageInfos() {
addr_t notification_addr = LLDB_INVALID_ADDRESS;
if (!m_process)
return notification_addr;
addr_t all_image_infos_addr = m_process->GetImageInfoAddress();
if (all_image_infos_addr == LLDB_INVALID_ADDRESS)
return notification_addr;
const uint32_t addr_size =
m_process->GetTarget().GetArchitecture().GetAddressByteSize();
offset_t registered_infos_addr_offset =
sizeof(uint32_t) +
sizeof(uint32_t) +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size;
Status error;
addr_t registered_infos_addr = m_process->ReadPointerFromMemory(
all_image_infos_addr + registered_infos_addr_offset, error);
if (!error.Success())
return notification_addr;
if (registered_infos_addr != all_image_infos_addr)
return notification_addr;
offset_t notification_fptr_offset = sizeof(uint32_t) +
sizeof(uint32_t) +
addr_size;
addr_t notification_fptr = m_process->ReadPointerFromMemory(
all_image_infos_addr + notification_fptr_offset, error);
if (error.Success())
notification_addr = m_process->FixCodeAddress(notification_fptr);
return notification_addr;
}
bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
if (m_break_id == LLDB_INVALID_BREAK_ID) {
ModuleSP dyld_sp(GetDYLDModule());
if (dyld_sp) {
bool internal = true;
bool hardware = false;
LazyBool skip_prologue = eLazyBoolNo;
FileSpecList *source_files = nullptr;
FileSpecList dyld_filelist;
dyld_filelist.Append(dyld_sp->GetFileSpec());
Breakpoint *breakpoint =
m_process->GetTarget()
.CreateBreakpoint(&dyld_filelist, source_files,
"lldb_image_notifier", eFunctionNameTypeFull,
eLanguageTypeUnknown, 0, skip_prologue,
internal, hardware)
.get();
breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
true);
breakpoint->SetBreakpointKind("shared-library-event");
if (breakpoint->HasResolvedLocations())
m_break_id = breakpoint->GetID();
else
m_process->GetTarget().RemoveBreakpointByID(breakpoint->GetID());
if (m_break_id == LLDB_INVALID_BREAK_ID) {
Breakpoint *breakpoint =
m_process->GetTarget()
.CreateBreakpoint(&dyld_filelist, source_files,
"gdb_image_notifier", eFunctionNameTypeFull,
eLanguageTypeUnknown, 0, skip_prologue,
internal, hardware)
.get();
breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
true);
breakpoint->SetBreakpointKind("shared-library-event");
if (breakpoint->HasResolvedLocations())
m_break_id = breakpoint->GetID();
else
m_process->GetTarget().RemoveBreakpointByID(breakpoint->GetID());
}
}
}
if (m_break_id == LLDB_INVALID_BREAK_ID) {
addr_t notification_addr = GetNotificationFuncAddrFromImageInfos();
if (notification_addr != LLDB_INVALID_ADDRESS) {
Address so_addr;
so_addr.SetRawAddress(notification_addr);
Breakpoint *dyld_break =
m_process->GetTarget().CreateBreakpoint(so_addr, true, false).get();
dyld_break->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
true);
dyld_break->SetBreakpointKind("shared-library-event");
if (dyld_break->HasResolvedLocations())
m_break_id = dyld_break->GetID();
else
m_process->GetTarget().RemoveBreakpointByID(dyld_break->GetID());
}
}
return m_break_id != LLDB_INVALID_BREAK_ID;
}
bool DynamicLoaderMacOS::SetDYLDHandoverBreakpoint(
addr_t notification_address) {
if (m_dyld_handover_break_id == LLDB_INVALID_BREAK_ID) {
BreakpointSP dyld_handover_bp = m_process->GetTarget().CreateBreakpoint(
notification_address, true, false);
dyld_handover_bp->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
true);
dyld_handover_bp->SetOneShot(true);
m_dyld_handover_break_id = dyld_handover_bp->GetID();
return true;
}
return false;
}
void DynamicLoaderMacOS::ClearDYLDHandoverBreakpoint() {
if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
m_dyld_handover_break_id = LLDB_INVALID_BREAK_ID;
}
addr_t
DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
SymbolContext sc;
Target &target = m_process->GetTarget();
if (Symtab *symtab = module->GetSymtab()) {
std::vector<uint32_t> match_indexes;
ConstString g_symbol_name("_dyld_global_lock_held");
uint32_t num_matches = 0;
num_matches =
symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
if (num_matches == 1) {
Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
if (symbol &&
(symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
}
}
}
return LLDB_INVALID_ADDRESS;
}
Status DynamicLoaderMacOS::CanLoadImage() {
Status error;
addr_t symbol_address = LLDB_INVALID_ADDRESS;
ConstString g_libdyld_name("libdyld.dylib");
Target &target = m_process->GetTarget();
const ModuleList &target_modules = target.GetImages();
std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
for (ModuleSP module_sp : target.GetImages().ModulesNoLocking()) {
if (module_sp) {
if (module_sp->GetFileSpec().GetFilename() == g_libdyld_name) {
symbol_address = GetDyldLockVariableAddressFromModule(module_sp.get());
if (symbol_address != LLDB_INVALID_ADDRESS)
break;
}
}
}
if (symbol_address == LLDB_INVALID_ADDRESS) {
for (ModuleSP module_sp : target.GetImages().Modules()) {
if (module_sp) {
addr_t symbol_address =
GetDyldLockVariableAddressFromModule(module_sp.get());
if (symbol_address != LLDB_INVALID_ADDRESS)
break;
}
}
}
if (symbol_address != LLDB_INVALID_ADDRESS) {
{
int lock_held =
m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
if (lock_held != 0) {
error.SetErrorString("dyld lock held - unsafe to load images.");
}
}
} else {
if (target.GetImages().GetSize() <= 1)
error.SetErrorString("could not find the dyld library or "
"the dyld lock symbol");
}
return error;
}
bool DynamicLoaderMacOS::GetSharedCacheInformation(
lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
LazyBool &private_shared_cache) {
base_address = LLDB_INVALID_ADDRESS;
uuid.Clear();
using_shared_cache = eLazyBoolCalculate;
private_shared_cache = eLazyBoolCalculate;
if (m_process) {
StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
StructuredData::Dictionary *info_dict = nullptr;
if (info.get() && info->GetAsDictionary()) {
info_dict = info->GetAsDictionary();
}
if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
info_dict->HasKey("no_shared_cache") &&
info_dict->HasKey("shared_cache_base_address")) {
base_address = info_dict->GetValueForKey("shared_cache_base_address")
->GetUnsignedIntegerValue(LLDB_INVALID_ADDRESS);
std::string uuid_str = std::string(
info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue());
if (!uuid_str.empty())
uuid.SetFromStringRef(uuid_str);
if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue())
using_shared_cache = eLazyBoolYes;
else
using_shared_cache = eLazyBoolNo;
if (info_dict->GetValueForKey("shared_cache_private_cache")
->GetBooleanValue())
private_shared_cache = eLazyBoolYes;
else
private_shared_cache = eLazyBoolNo;
return true;
}
}
return false;
}
void DynamicLoaderMacOS::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance);
}
void DynamicLoaderMacOS::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
llvm::StringRef DynamicLoaderMacOS::GetPluginDescriptionStatic() {
return "Dynamic loader plug-in that watches for shared library loads/unloads "
"in MacOSX user processes.";
}