blob: 65a35549e7f6ff9c095be75a3996dd1dad952e59 [file] [log] [blame]
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001//===-- DynamicLoaderMacOS.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Breakpoint/StoppointCallbackContext.h"
11#include "lldb/Core/Debugger.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/PluginManager.h"
14#include "lldb/Core/Section.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000015#include "lldb/Symbol/ClangASTContext.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000016#include "lldb/Symbol/ObjectFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000018#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Target/StackFrame.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000020#include "lldb/Target/Target.h"
21#include "lldb/Target/Thread.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000022#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000023#include "lldb/Utility/State.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000024
Jason Molenda9ab5dc22016-07-21 08:30:55 +000025#include "DynamicLoaderDarwin.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "DynamicLoaderMacOS.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Jason Molenda9ab5dc22016-07-21 08:30:55 +000031//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000032// Create an instance of this class. This function is filled into the plugin
33// info class that gets handed out by the plugin factory and allows the lldb to
34// instantiate an instance of this class.
Jason Molenda9ab5dc22016-07-21 08:30:55 +000035//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000036DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
37 bool force) {
38 bool create = force;
39 if (!create) {
40 create = true;
41 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
42 if (exe_module) {
43 ObjectFile *object_file = exe_module->GetObjectFile();
44 if (object_file) {
45 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
46 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +000047 }
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 if (create) {
50 const llvm::Triple &triple_ref =
51 process->GetTarget().GetArchitecture().GetTriple();
52 switch (triple_ref.getOS()) {
53 case llvm::Triple::Darwin:
54 case llvm::Triple::MacOSX:
55 case llvm::Triple::IOS:
56 case llvm::Triple::TvOS:
57 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000058 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 create = triple_ref.getVendor() == llvm::Triple::Apple;
60 break;
61 default:
Jason Molenda716814a2016-07-22 22:26:26 +000062 create = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 break;
64 }
Jason Molenda716814a2016-07-22 22:26:26 +000065 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 }
67
68 if (UseDYLDSPI(process) == false) {
69 create = false;
70 }
71
72 if (create)
73 return new DynamicLoaderMacOS(process);
74 return NULL;
Jason Molenda9ab5dc22016-07-21 08:30:55 +000075}
76
77//----------------------------------------------------------------------
78// Constructor
79//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000080DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
81 : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
82 m_break_id(LLDB_INVALID_BREAK_ID), m_mutex() {}
Jason Molenda9ab5dc22016-07-21 08:30:55 +000083
84//----------------------------------------------------------------------
85// Destructor
86//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000087DynamicLoaderMacOS::~DynamicLoaderMacOS() {
88 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
89 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092bool DynamicLoaderMacOS::ProcessDidExec() {
93 std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
94 bool did_exec = false;
95 if (m_process) {
96 // If we are stopped after an exec, we will have only one thread...
97 if (m_process->GetThreadList().GetSize() == 1) {
98 // See if we are stopped at '_dyld_start'
99 ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
100 if (thread_sp) {
101 lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
102 if (frame_sp) {
103 const Symbol *symbol =
104 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
105 if (symbol) {
106 if (symbol->GetName() == ConstString("_dyld_start"))
107 did_exec = true;
108 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 if (did_exec) {
115 m_libpthread_module_wp.reset();
116 m_pthread_getspecific_addr.Clear();
117 }
118 return did_exec;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000119}
120
121//----------------------------------------------------------------------
122// Clear out the state of this class.
123//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void DynamicLoaderMacOS::DoClear() {
125 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
128 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000131}
132
133//----------------------------------------------------------------------
134// Check if we have found DYLD yet
135//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
137 return LLDB_BREAK_ID_IS_VALID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
141 if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
142 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda777fcec2017-01-21 01:17:36 +0000143 m_break_id = LLDB_INVALID_BREAK_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000145}
146
147//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000148// Try and figure out where dyld is by first asking the Process if it knows
149// (which currently calls down in the lldb::Process to get the DYLD info
150// (available on SnowLeopard only). If that fails, then check in the default
151// addresses.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000152//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153void DynamicLoaderMacOS::DoInitialImageFetch() {
154 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000155
Adrian Prantl05097242018-04-30 16:49:04 +0000156 // Remove any binaries we pre-loaded in the Target before
157 // launching/attaching. If the same binaries are present in the process,
158 // we'll get them from the shared module cache, we won't need to re-load them
159 // from disk.
Jason Molenda848c7be2017-01-19 00:20:29 +0000160 UnloadAllImages();
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 StructuredData::ObjectSP all_image_info_json_sp(
163 m_process->GetLoadedDynamicLibrariesInfos());
164 ImageInfo::collection image_infos;
165 if (all_image_info_json_sp.get() &&
166 all_image_info_json_sp->GetAsDictionary() &&
167 all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
168 all_image_info_json_sp->GetAsDictionary()
169 ->GetValueForKey("images")
170 ->GetAsArray()) {
171 if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
172 image_infos)) {
173 if (log)
174 log->Printf("Initial module fetch: Adding %" PRId64 " modules.\n",
175 (uint64_t)image_infos.size());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 UpdateSpecialBinariesFromNewImageInfos(image_infos);
178 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 }
181
182 m_dyld_image_infos_stop_id = m_process->GetStopID();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000186
187//----------------------------------------------------------------------
188// Static callback function that gets called when our DYLD notification
Adrian Prantl05097242018-04-30 16:49:04 +0000189// breakpoint gets hit. We update all of our image infos and then let our super
190// class DynamicLoader class decide if we should stop or not (based on global
191// preference).
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000192//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
194 StoppointCallbackContext *context,
195 lldb::user_id_t break_id,
196 lldb::user_id_t break_loc_id) {
197 // Let the event know that the images have changed
198 // DYLD passes three arguments to the notification breakpoint.
Adrian Prantl05097242018-04-30 16:49:04 +0000199 // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
200 // all Arg2: unsigned long icount - Number of shared libraries
201 // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses
202 // of binaries added/removed
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 ExecutionContext exe_ctx(context->exe_ctx_ref);
207 Process *process = exe_ctx.GetProcessPtr();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 // This is a sanity check just in case this dyld_instance is an old dyld
210 // plugin's breakpoint still lying around.
211 if (process != dyld_instance->m_process)
212 return false;
213
214 if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
215 process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
216 return false;
217 }
218
219 const lldb::ABISP &abi = process->GetABI();
220 if (abi) {
221 // Build up the value array to store the three arguments given above, then
222 // get the values from the ABI:
223
224 ClangASTContext *clang_ast_context =
225 process->GetTarget().GetScratchClangASTContext();
226 ValueList argument_values;
227
228 Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
229 // dyld_notify_removing=1, dyld_notify_remove_all=2 };
230 Value count_value; // unsigned long count
231 Value headers_value; // uint64_t machHeaders[] (aka void*)
232
233 CompilerType clang_void_ptr_type =
234 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
235 CompilerType clang_uint32_type =
236 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
237 lldb::eEncodingUint, 32);
238 CompilerType clang_uint64_type =
239 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
240 lldb::eEncodingUint, 32);
241
242 mode_value.SetValueType(Value::eValueTypeScalar);
243 mode_value.SetCompilerType(clang_uint32_type);
244
245 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
246 count_value.SetValueType(Value::eValueTypeScalar);
247 count_value.SetCompilerType(clang_uint32_type);
248 } else {
249 count_value.SetValueType(Value::eValueTypeScalar);
250 count_value.SetCompilerType(clang_uint64_type);
Jason Molenda716814a2016-07-22 22:26:26 +0000251 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 headers_value.SetValueType(Value::eValueTypeScalar);
254 headers_value.SetCompilerType(clang_void_ptr_type);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 argument_values.PushValue(mode_value);
257 argument_values.PushValue(count_value);
258 argument_values.PushValue(headers_value);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
261 uint32_t dyld_mode =
262 argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
263 if (dyld_mode != static_cast<uint32_t>(-1)) {
264 // Okay the mode was right, now get the number of elements, and the
265 // array of new elements...
266 uint32_t image_infos_count =
267 argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
268 if (image_infos_count != static_cast<uint32_t>(-1)) {
269 addr_t header_array =
270 argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
271 if (header_array != static_cast<uint64_t>(-1)) {
272 std::vector<addr_t> image_load_addresses;
273 for (uint64_t i = 0; i < image_infos_count; i++) {
Zachary Turner97206d52017-05-12 04:51:55 +0000274 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 addr_t addr = process->ReadUnsignedIntegerFromMemory(
276 header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
277 if (addr != LLDB_INVALID_ADDRESS) {
278 image_load_addresses.push_back(addr);
279 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000280 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 if (dyld_mode == 0) {
282 // dyld_notify_adding
283 dyld_instance->AddBinaries(image_load_addresses);
284 } else if (dyld_mode == 1) {
285 // dyld_notify_removing
286 dyld_instance->UnloadImages(image_load_addresses);
287 } else if (dyld_mode == 2) {
288 // dyld_notify_remove_all
289 dyld_instance->UnloadAllImages();
290 }
291 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000292 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 } else {
296 process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
297 "No ABI plugin located for triple %s -- shared libraries will not be "
298 "registered!\n",
299 process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
300 }
301
302 // Return true to stop the target, false to just let the target run
303 return dyld_instance->GetStopWhenImagesChange();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000304}
305
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306void DynamicLoaderMacOS::AddBinaries(
307 const std::vector<lldb::addr_t> &load_addresses) {
308 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
309 ImageInfo::collection image_infos;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 if (log)
312 log->Printf("Adding %" PRId64 " modules.", (uint64_t)load_addresses.size());
313 StructuredData::ObjectSP binaries_info_sp =
314 m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
315 if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
316 binaries_info_sp->GetAsDictionary()->HasKey("images") &&
317 binaries_info_sp->GetAsDictionary()
318 ->GetValueForKey("images")
319 ->GetAsArray() &&
320 binaries_info_sp->GetAsDictionary()
321 ->GetValueForKey("images")
322 ->GetAsArray()
323 ->GetSize() == load_addresses.size()) {
324 if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
325 UpdateSpecialBinariesFromNewImageInfos(image_infos);
326 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000327 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 m_dyld_image_infos_stop_id = m_process->GetStopID();
329 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000330}
331
Adrian Prantl05097242018-04-30 16:49:04 +0000332// Dump the _dyld_all_image_infos members and all current image infos that we
333// have parsed to the file handle provided.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000334//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335void DynamicLoaderMacOS::PutToLog(Log *log) const {
336 if (log == NULL)
337 return;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000338}
339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
341 if (m_break_id == LLDB_INVALID_BREAK_ID) {
342 ConstString g_symbol_name("_dyld_debugger_notification");
343 const Symbol *symbol = nullptr;
344 ModuleSP dyld_sp(GetDYLDModule());
345 if (dyld_sp) {
346 symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name,
347 eSymbolTypeCode);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000348 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 if (symbol &&
350 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
351 addr_t symbol_address =
352 symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget());
353 if (symbol_address != LLDB_INVALID_ADDRESS) {
354 bool internal = true;
355 bool hardware = false;
356 Breakpoint *breakpoint =
357 m_process->GetTarget()
358 .CreateBreakpoint(symbol_address, internal, hardware)
359 .get();
360 breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
361 true);
362 breakpoint->SetBreakpointKind("shared-library-event");
363 m_break_id = breakpoint->GetID();
364 }
365 }
366 }
367 return m_break_id != LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000368}
369
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000370addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
372 SymbolContext sc;
373 SymbolVendor *sym_vendor = module->GetSymbolVendor();
374 Target &target = m_process->GetTarget();
375 if (sym_vendor) {
376 Symtab *symtab = sym_vendor->GetSymtab();
377 if (symtab) {
378 std::vector<uint32_t> match_indexes;
379 ConstString g_symbol_name("_dyld_global_lock_held");
380 uint32_t num_matches = 0;
381 num_matches =
382 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
383 if (num_matches == 1) {
384 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
385 if (symbol &&
386 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
387 return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 }
392 return LLDB_INVALID_ADDRESS;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000393}
394
395// Look for this symbol:
396//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397// int __attribute__((visibility("hidden"))) _dyld_global_lock_held =
398// 0;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000399//
400// in libdyld.dylib.
Zachary Turner97206d52017-05-12 04:51:55 +0000401Status DynamicLoaderMacOS::CanLoadImage() {
402 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 addr_t symbol_address = LLDB_INVALID_ADDRESS;
404 Target &target = m_process->GetTarget();
405 const ModuleList &target_modules = target.GetImages();
406 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
407 const size_t num_modules = target_modules.GetSize();
408 ConstString g_libdyld_name("libdyld.dylib");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 // Find any modules named "libdyld.dylib" and look for the symbol there first
411 for (size_t i = 0; i < num_modules; i++) {
412 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
413 if (module_pointer) {
414 if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) {
415 symbol_address = GetDyldLockVariableAddressFromModule(module_pointer);
416 if (symbol_address != LLDB_INVALID_ADDRESS)
417 break;
418 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000419 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 // Search through all modules looking for the symbol in them
423 if (symbol_address == LLDB_INVALID_ADDRESS) {
424 for (size_t i = 0; i < num_modules; i++) {
425 Module *module_pointer =
426 target_modules.GetModulePointerAtIndexUnlocked(i);
427 if (module_pointer) {
428 addr_t symbol_address =
429 GetDyldLockVariableAddressFromModule(module_pointer);
430 if (symbol_address != LLDB_INVALID_ADDRESS)
431 break;
432 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000435
Adrian Prantl05097242018-04-30 16:49:04 +0000436 // Default assumption is that it is OK to load images. Only say that we
437 // cannot load images if we find the symbol in libdyld and it indicates that
438 // we cannot.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 if (symbol_address != LLDB_INVALID_ADDRESS) {
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000441 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 int lock_held =
443 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
444 if (lock_held != 0) {
Jim Inghamabc5d722017-05-06 01:15:47 +0000445 error.SetErrorString("dyld lock held - unsafe to load images.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 }
Jason Molenda37397352016-07-22 00:17:55 +0000447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000449 // If we were unable to find _dyld_global_lock_held in any modules, or it
450 // is not loaded into memory yet, we may be at process startup (sitting at
451 // _dyld_start) - so we should not allow dlopen calls. But if we found more
452 // than one module then we are clearly past _dyld_start so in that case
453 // we'll default to "it's safe".
Jim Inghamabc5d722017-05-06 01:15:47 +0000454 if (num_modules <= 1)
455 error.SetErrorString("could not find the dyld library or "
456 "the dyld lock symbol");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 }
458 return error;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000459}
460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461bool DynamicLoaderMacOS::GetSharedCacheInformation(
462 lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
463 LazyBool &private_shared_cache) {
464 base_address = LLDB_INVALID_ADDRESS;
465 uuid.Clear();
466 using_shared_cache = eLazyBoolCalculate;
467 private_shared_cache = eLazyBoolCalculate;
Jason Molenda13becd42016-07-29 00:18:39 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 if (m_process) {
470 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
471 StructuredData::Dictionary *info_dict = nullptr;
472 if (info.get() && info->GetAsDictionary()) {
473 info_dict = info->GetAsDictionary();
Jason Molenda13becd42016-07-29 00:18:39 +0000474 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475
Adrian Prantl05097242018-04-30 16:49:04 +0000476 // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
477 // ":"DDB8D70C-
478 // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479
480 if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
481 info_dict->HasKey("no_shared_cache") &&
482 info_dict->HasKey("shared_cache_base_address")) {
483 base_address = info_dict->GetValueForKey("shared_cache_base_address")
484 ->GetIntegerValue(LLDB_INVALID_ADDRESS);
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000485 std::string uuid_str =
486 info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 if (!uuid_str.empty())
Pavel Labatha174bcb2018-06-21 15:24:39 +0000488 uuid.SetFromStringRef(uuid_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 if (info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue() ==
490 false)
491 using_shared_cache = eLazyBoolYes;
492 else
493 using_shared_cache = eLazyBoolNo;
494 if (info_dict->GetValueForKey("shared_cache_private_cache")
495 ->GetBooleanValue())
496 private_shared_cache = eLazyBoolYes;
497 else
498 private_shared_cache = eLazyBoolNo;
499
500 return true;
501 }
502 }
503 return false;
Jason Molenda13becd42016-07-29 00:18:39 +0000504}
505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506void DynamicLoaderMacOS::Initialize() {
507 PluginManager::RegisterPlugin(GetPluginNameStatic(),
508 GetPluginDescriptionStatic(), CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000509}
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511void DynamicLoaderMacOS::Terminate() {
512 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000513}
514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() {
516 static ConstString g_name("macos-dyld");
517 return g_name;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000518}
519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() {
521 return "Dynamic loader plug-in that watches for shared library loads/unloads "
522 "in MacOSX user processes.";
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000523}
524
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000525//------------------------------------------------------------------
526// PluginInterface protocol
527//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() {
529 return GetPluginNameStatic();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; }