blob: 7fd886cdeae8013f900b56a6e310b6a6d8c68724 [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),
Jim Ingham29ae6592018-12-07 01:18:40 +000082 m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
83 m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {}
Jason Molenda9ab5dc22016-07-21 08:30:55 +000084
85//----------------------------------------------------------------------
86// Destructor
87//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000088DynamicLoaderMacOS::~DynamicLoaderMacOS() {
89 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
90 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +000091}
92
Kate Stoneb9c1b512016-09-06 20:57:50 +000093bool DynamicLoaderMacOS::ProcessDidExec() {
94 std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
95 bool did_exec = false;
96 if (m_process) {
97 // If we are stopped after an exec, we will have only one thread...
98 if (m_process->GetThreadList().GetSize() == 1) {
Jim Ingham29ae6592018-12-07 01:18:40 +000099 // Maybe we still have an image infos address around? If so see
100 // if that has changed, and if so we have exec'ed.
101 if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
102 lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
103 if (image_infos_address != m_maybe_image_infos_address) {
104 // We don't really have to reset this here, since we are going to
105 // call DoInitialImageFetch right away to handle the exec. But in
106 // case anybody looks at it in the meantime, it can't hurt.
107 m_maybe_image_infos_address = image_infos_address;
108 did_exec = true;
109 }
110 }
111
112 if (!did_exec) {
113 // See if we are stopped at '_dyld_start'
114 ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
115 if (thread_sp) {
116 lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
117 if (frame_sp) {
118 const Symbol *symbol =
119 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
120 if (symbol) {
121 if (symbol->GetName() == ConstString("_dyld_start"))
122 did_exec = true;
123 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 if (did_exec) {
131 m_libpthread_module_wp.reset();
132 m_pthread_getspecific_addr.Clear();
133 }
134 return did_exec;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000135}
136
137//----------------------------------------------------------------------
138// Clear out the state of this class.
139//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140void DynamicLoaderMacOS::DoClear() {
141 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
144 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000147}
148
149//----------------------------------------------------------------------
150// Check if we have found DYLD yet
151//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
153 return LLDB_BREAK_ID_IS_VALID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
157 if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
158 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda777fcec2017-01-21 01:17:36 +0000159 m_break_id = LLDB_INVALID_BREAK_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000161}
162
163//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000164// Try and figure out where dyld is by first asking the Process if it knows
165// (which currently calls down in the lldb::Process to get the DYLD info
166// (available on SnowLeopard only). If that fails, then check in the default
167// addresses.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000168//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169void DynamicLoaderMacOS::DoInitialImageFetch() {
170 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000171
Adrian Prantl05097242018-04-30 16:49:04 +0000172 // Remove any binaries we pre-loaded in the Target before
173 // launching/attaching. If the same binaries are present in the process,
174 // we'll get them from the shared module cache, we won't need to re-load them
175 // from disk.
Jason Molenda848c7be2017-01-19 00:20:29 +0000176 UnloadAllImages();
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 StructuredData::ObjectSP all_image_info_json_sp(
179 m_process->GetLoadedDynamicLibrariesInfos());
180 ImageInfo::collection image_infos;
181 if (all_image_info_json_sp.get() &&
182 all_image_info_json_sp->GetAsDictionary() &&
183 all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
184 all_image_info_json_sp->GetAsDictionary()
185 ->GetValueForKey("images")
186 ->GetAsArray()) {
187 if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
188 image_infos)) {
189 if (log)
190 log->Printf("Initial module fetch: Adding %" PRId64 " modules.\n",
191 (uint64_t)image_infos.size());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 UpdateSpecialBinariesFromNewImageInfos(image_infos);
194 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000195 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 }
197
198 m_dyld_image_infos_stop_id = m_process->GetStopID();
Jim Ingham29ae6592018-12-07 01:18:40 +0000199 m_maybe_image_infos_address = m_process->GetImageInfoAddress();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000203
204//----------------------------------------------------------------------
205// Static callback function that gets called when our DYLD notification
Adrian Prantl05097242018-04-30 16:49:04 +0000206// breakpoint gets hit. We update all of our image infos and then let our super
207// class DynamicLoader class decide if we should stop or not (based on global
208// preference).
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000209//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
211 StoppointCallbackContext *context,
212 lldb::user_id_t break_id,
213 lldb::user_id_t break_loc_id) {
214 // Let the event know that the images have changed
215 // DYLD passes three arguments to the notification breakpoint.
Adrian Prantl05097242018-04-30 16:49:04 +0000216 // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
217 // all Arg2: unsigned long icount - Number of shared libraries
218 // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses
219 // of binaries added/removed
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 ExecutionContext exe_ctx(context->exe_ctx_ref);
224 Process *process = exe_ctx.GetProcessPtr();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 // This is a sanity check just in case this dyld_instance is an old dyld
227 // plugin's breakpoint still lying around.
228 if (process != dyld_instance->m_process)
229 return false;
230
231 if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
232 process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
233 return false;
234 }
235
236 const lldb::ABISP &abi = process->GetABI();
237 if (abi) {
238 // Build up the value array to store the three arguments given above, then
239 // get the values from the ABI:
240
241 ClangASTContext *clang_ast_context =
242 process->GetTarget().GetScratchClangASTContext();
243 ValueList argument_values;
244
245 Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
246 // dyld_notify_removing=1, dyld_notify_remove_all=2 };
247 Value count_value; // unsigned long count
248 Value headers_value; // uint64_t machHeaders[] (aka void*)
249
250 CompilerType clang_void_ptr_type =
251 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
252 CompilerType clang_uint32_type =
253 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
254 lldb::eEncodingUint, 32);
255 CompilerType clang_uint64_type =
256 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
257 lldb::eEncodingUint, 32);
258
259 mode_value.SetValueType(Value::eValueTypeScalar);
260 mode_value.SetCompilerType(clang_uint32_type);
261
262 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
263 count_value.SetValueType(Value::eValueTypeScalar);
264 count_value.SetCompilerType(clang_uint32_type);
265 } else {
266 count_value.SetValueType(Value::eValueTypeScalar);
267 count_value.SetCompilerType(clang_uint64_type);
Jason Molenda716814a2016-07-22 22:26:26 +0000268 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 headers_value.SetValueType(Value::eValueTypeScalar);
271 headers_value.SetCompilerType(clang_void_ptr_type);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 argument_values.PushValue(mode_value);
274 argument_values.PushValue(count_value);
275 argument_values.PushValue(headers_value);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
278 uint32_t dyld_mode =
279 argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
280 if (dyld_mode != static_cast<uint32_t>(-1)) {
281 // Okay the mode was right, now get the number of elements, and the
282 // array of new elements...
283 uint32_t image_infos_count =
284 argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
285 if (image_infos_count != static_cast<uint32_t>(-1)) {
286 addr_t header_array =
287 argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
288 if (header_array != static_cast<uint64_t>(-1)) {
289 std::vector<addr_t> image_load_addresses;
290 for (uint64_t i = 0; i < image_infos_count; i++) {
Zachary Turner97206d52017-05-12 04:51:55 +0000291 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 addr_t addr = process->ReadUnsignedIntegerFromMemory(
293 header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
294 if (addr != LLDB_INVALID_ADDRESS) {
295 image_load_addresses.push_back(addr);
296 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000297 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 if (dyld_mode == 0) {
299 // dyld_notify_adding
300 dyld_instance->AddBinaries(image_load_addresses);
301 } else if (dyld_mode == 1) {
302 // dyld_notify_removing
303 dyld_instance->UnloadImages(image_load_addresses);
304 } else if (dyld_mode == 2) {
305 // dyld_notify_remove_all
306 dyld_instance->UnloadAllImages();
307 }
308 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000311 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 } else {
313 process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
314 "No ABI plugin located for triple %s -- shared libraries will not be "
315 "registered!\n",
316 process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
317 }
318
319 // Return true to stop the target, false to just let the target run
320 return dyld_instance->GetStopWhenImagesChange();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000321}
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323void DynamicLoaderMacOS::AddBinaries(
324 const std::vector<lldb::addr_t> &load_addresses) {
325 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
326 ImageInfo::collection image_infos;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 if (log)
329 log->Printf("Adding %" PRId64 " modules.", (uint64_t)load_addresses.size());
330 StructuredData::ObjectSP binaries_info_sp =
331 m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
332 if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
333 binaries_info_sp->GetAsDictionary()->HasKey("images") &&
334 binaries_info_sp->GetAsDictionary()
335 ->GetValueForKey("images")
336 ->GetAsArray() &&
337 binaries_info_sp->GetAsDictionary()
338 ->GetValueForKey("images")
339 ->GetAsArray()
340 ->GetSize() == load_addresses.size()) {
341 if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
342 UpdateSpecialBinariesFromNewImageInfos(image_infos);
343 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 m_dyld_image_infos_stop_id = m_process->GetStopID();
346 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000347}
348
Adrian Prantl05097242018-04-30 16:49:04 +0000349// Dump the _dyld_all_image_infos members and all current image infos that we
350// have parsed to the file handle provided.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000351//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352void DynamicLoaderMacOS::PutToLog(Log *log) const {
353 if (log == NULL)
354 return;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000355}
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
358 if (m_break_id == LLDB_INVALID_BREAK_ID) {
359 ConstString g_symbol_name("_dyld_debugger_notification");
360 const Symbol *symbol = nullptr;
361 ModuleSP dyld_sp(GetDYLDModule());
362 if (dyld_sp) {
363 symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name,
364 eSymbolTypeCode);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000365 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if (symbol &&
367 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
368 addr_t symbol_address =
369 symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget());
370 if (symbol_address != LLDB_INVALID_ADDRESS) {
371 bool internal = true;
372 bool hardware = false;
373 Breakpoint *breakpoint =
374 m_process->GetTarget()
375 .CreateBreakpoint(symbol_address, internal, hardware)
376 .get();
377 breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
378 true);
379 breakpoint->SetBreakpointKind("shared-library-event");
380 m_break_id = breakpoint->GetID();
381 }
382 }
383 }
384 return m_break_id != LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000385}
386
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000387addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
389 SymbolContext sc;
390 SymbolVendor *sym_vendor = module->GetSymbolVendor();
391 Target &target = m_process->GetTarget();
392 if (sym_vendor) {
393 Symtab *symtab = sym_vendor->GetSymtab();
394 if (symtab) {
395 std::vector<uint32_t> match_indexes;
396 ConstString g_symbol_name("_dyld_global_lock_held");
397 uint32_t num_matches = 0;
398 num_matches =
399 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
400 if (num_matches == 1) {
401 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
402 if (symbol &&
403 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
404 return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000405 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000407 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 }
409 return LLDB_INVALID_ADDRESS;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000410}
411
412// Look for this symbol:
413//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414// int __attribute__((visibility("hidden"))) _dyld_global_lock_held =
415// 0;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000416//
417// in libdyld.dylib.
Zachary Turner97206d52017-05-12 04:51:55 +0000418Status DynamicLoaderMacOS::CanLoadImage() {
419 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 addr_t symbol_address = LLDB_INVALID_ADDRESS;
421 Target &target = m_process->GetTarget();
422 const ModuleList &target_modules = target.GetImages();
423 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
424 const size_t num_modules = target_modules.GetSize();
425 ConstString g_libdyld_name("libdyld.dylib");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 // Find any modules named "libdyld.dylib" and look for the symbol there first
428 for (size_t i = 0; i < num_modules; i++) {
429 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
430 if (module_pointer) {
431 if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) {
432 symbol_address = GetDyldLockVariableAddressFromModule(module_pointer);
433 if (symbol_address != LLDB_INVALID_ADDRESS)
434 break;
435 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000436 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 // Search through all modules looking for the symbol in them
440 if (symbol_address == LLDB_INVALID_ADDRESS) {
441 for (size_t i = 0; i < num_modules; i++) {
442 Module *module_pointer =
443 target_modules.GetModulePointerAtIndexUnlocked(i);
444 if (module_pointer) {
445 addr_t symbol_address =
446 GetDyldLockVariableAddressFromModule(module_pointer);
447 if (symbol_address != LLDB_INVALID_ADDRESS)
448 break;
449 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000450 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000452
Adrian Prantl05097242018-04-30 16:49:04 +0000453 // Default assumption is that it is OK to load images. Only say that we
454 // cannot load images if we find the symbol in libdyld and it indicates that
455 // we cannot.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 if (symbol_address != LLDB_INVALID_ADDRESS) {
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000458 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 int lock_held =
460 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
461 if (lock_held != 0) {
Jim Inghamabc5d722017-05-06 01:15:47 +0000462 error.SetErrorString("dyld lock held - unsafe to load images.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 }
Jason Molenda37397352016-07-22 00:17:55 +0000464 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000466 // If we were unable to find _dyld_global_lock_held in any modules, or it
467 // is not loaded into memory yet, we may be at process startup (sitting at
468 // _dyld_start) - so we should not allow dlopen calls. But if we found more
469 // than one module then we are clearly past _dyld_start so in that case
470 // we'll default to "it's safe".
Jim Inghamabc5d722017-05-06 01:15:47 +0000471 if (num_modules <= 1)
472 error.SetErrorString("could not find the dyld library or "
473 "the dyld lock symbol");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 }
475 return error;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000476}
477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478bool DynamicLoaderMacOS::GetSharedCacheInformation(
479 lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
480 LazyBool &private_shared_cache) {
481 base_address = LLDB_INVALID_ADDRESS;
482 uuid.Clear();
483 using_shared_cache = eLazyBoolCalculate;
484 private_shared_cache = eLazyBoolCalculate;
Jason Molenda13becd42016-07-29 00:18:39 +0000485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 if (m_process) {
487 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
488 StructuredData::Dictionary *info_dict = nullptr;
489 if (info.get() && info->GetAsDictionary()) {
490 info_dict = info->GetAsDictionary();
Jason Molenda13becd42016-07-29 00:18:39 +0000491 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492
Adrian Prantl05097242018-04-30 16:49:04 +0000493 // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
494 // ":"DDB8D70C-
495 // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496
497 if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
498 info_dict->HasKey("no_shared_cache") &&
499 info_dict->HasKey("shared_cache_base_address")) {
500 base_address = info_dict->GetValueForKey("shared_cache_base_address")
501 ->GetIntegerValue(LLDB_INVALID_ADDRESS);
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000502 std::string uuid_str =
503 info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 if (!uuid_str.empty())
Pavel Labatha174bcb2018-06-21 15:24:39 +0000505 uuid.SetFromStringRef(uuid_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 if (info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue() ==
507 false)
508 using_shared_cache = eLazyBoolYes;
509 else
510 using_shared_cache = eLazyBoolNo;
511 if (info_dict->GetValueForKey("shared_cache_private_cache")
512 ->GetBooleanValue())
513 private_shared_cache = eLazyBoolYes;
514 else
515 private_shared_cache = eLazyBoolNo;
516
517 return true;
518 }
519 }
520 return false;
Jason Molenda13becd42016-07-29 00:18:39 +0000521}
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523void DynamicLoaderMacOS::Initialize() {
524 PluginManager::RegisterPlugin(GetPluginNameStatic(),
525 GetPluginDescriptionStatic(), CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528void DynamicLoaderMacOS::Terminate() {
529 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() {
533 static ConstString g_name("macos-dyld");
534 return g_name;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000535}
536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() {
538 return "Dynamic loader plug-in that watches for shared library loads/unloads "
539 "in MacOSX user processes.";
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000540}
541
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000542//------------------------------------------------------------------
543// PluginInterface protocol
544//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() {
546 return GetPluginNameStatic();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000547}
548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; }