Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderMacOS.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 10 | #include "lldb/Core/Debugger.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
| 12 | #include "lldb/Core/PluginManager.h" |
| 13 | #include "lldb/Core/Section.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/ClangASTContext.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/SymbolVendor.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 17 | #include "lldb/Target/ABI.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "lldb/Target/StackFrame.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 19 | #include "lldb/Target/Target.h" |
| 20 | #include "lldb/Target/Thread.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/State.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 23 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 24 | #include "DynamicLoaderDarwin.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | #include "DynamicLoaderMacOS.h" |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 30 | // Create an instance of this class. This function is filled into the plugin |
| 31 | // info class that gets handed out by the plugin factory and allows the lldb to |
| 32 | // instantiate an instance of this class. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process, |
| 34 | bool force) { |
| 35 | bool create = force; |
| 36 | if (!create) { |
| 37 | create = true; |
| 38 | Module *exe_module = process->GetTarget().GetExecutableModulePointer(); |
| 39 | if (exe_module) { |
| 40 | ObjectFile *object_file = exe_module->GetObjectFile(); |
| 41 | if (object_file) { |
| 42 | create = (object_file->GetStrata() == ObjectFile::eStrataUser); |
| 43 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | if (create) { |
| 47 | const llvm::Triple &triple_ref = |
| 48 | process->GetTarget().GetArchitecture().GetTriple(); |
| 49 | switch (triple_ref.getOS()) { |
| 50 | case llvm::Triple::Darwin: |
| 51 | case llvm::Triple::MacOSX: |
| 52 | case llvm::Triple::IOS: |
| 53 | case llvm::Triple::TvOS: |
| 54 | case llvm::Triple::WatchOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 55 | // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | create = triple_ref.getVendor() == llvm::Triple::Apple; |
| 57 | break; |
| 58 | default: |
Jason Molenda | 716814a | 2016-07-22 22:26:26 +0000 | [diff] [blame] | 59 | create = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | break; |
| 61 | } |
Jason Molenda | 716814a | 2016-07-22 22:26:26 +0000 | [diff] [blame] | 62 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 65 | if (!UseDYLDSPI(process)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | create = false; |
| 67 | } |
| 68 | |
| 69 | if (create) |
| 70 | return new DynamicLoaderMacOS(process); |
| 71 | return NULL; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 74 | // Constructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process) |
| 76 | : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX), |
Jim Ingham | 29ae659 | 2018-12-07 01:18:40 +0000 | [diff] [blame] | 77 | m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(), |
| 78 | m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {} |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 79 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 80 | // Destructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | DynamicLoaderMacOS::~DynamicLoaderMacOS() { |
| 82 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) |
| 83 | m_process->GetTarget().RemoveBreakpointByID(m_break_id); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | bool DynamicLoaderMacOS::ProcessDidExec() { |
| 87 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
| 88 | bool did_exec = false; |
| 89 | if (m_process) { |
| 90 | // If we are stopped after an exec, we will have only one thread... |
| 91 | if (m_process->GetThreadList().GetSize() == 1) { |
Jim Ingham | 29ae659 | 2018-12-07 01:18:40 +0000 | [diff] [blame] | 92 | // Maybe we still have an image infos address around? If so see |
| 93 | // if that has changed, and if so we have exec'ed. |
| 94 | if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) { |
| 95 | lldb::addr_t image_infos_address = m_process->GetImageInfoAddress(); |
| 96 | if (image_infos_address != m_maybe_image_infos_address) { |
| 97 | // We don't really have to reset this here, since we are going to |
| 98 | // call DoInitialImageFetch right away to handle the exec. But in |
| 99 | // case anybody looks at it in the meantime, it can't hurt. |
| 100 | m_maybe_image_infos_address = image_infos_address; |
| 101 | did_exec = true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (!did_exec) { |
| 106 | // See if we are stopped at '_dyld_start' |
| 107 | ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0)); |
| 108 | if (thread_sp) { |
| 109 | lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0)); |
| 110 | if (frame_sp) { |
| 111 | const Symbol *symbol = |
| 112 | frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol; |
| 113 | if (symbol) { |
Raphael Isemann | 05cfdb0 | 2019-04-26 07:21:36 +0000 | [diff] [blame^] | 114 | if (symbol->GetName() == "_dyld_start") |
Jim Ingham | 29ae659 | 2018-12-07 01:18:40 +0000 | [diff] [blame] | 115 | did_exec = true; |
| 116 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 118 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 120 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | if (did_exec) { |
| 124 | m_libpthread_module_wp.reset(); |
| 125 | m_pthread_getspecific_addr.Clear(); |
| 126 | } |
| 127 | return did_exec; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 130 | // Clear out the state of this class. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | void DynamicLoaderMacOS::DoClear() { |
| 132 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) |
| 135 | m_process->GetTarget().RemoveBreakpointByID(m_break_id); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | m_break_id = LLDB_INVALID_BREAK_ID; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 140 | // Check if we have found DYLD yet |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() { |
| 142 | return LLDB_BREAK_ID_IS_VALID(m_break_id); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | void DynamicLoaderMacOS::ClearNotificationBreakpoint() { |
| 146 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) { |
| 147 | m_process->GetTarget().RemoveBreakpointByID(m_break_id); |
Jason Molenda | 777fcec | 2017-01-21 01:17:36 +0000 | [diff] [blame] | 148 | m_break_id = LLDB_INVALID_BREAK_ID; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 152 | // Try and figure out where dyld is by first asking the Process if it knows |
| 153 | // (which currently calls down in the lldb::Process to get the DYLD info |
| 154 | // (available on SnowLeopard only). If that fails, then check in the default |
| 155 | // addresses. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | void DynamicLoaderMacOS::DoInitialImageFetch() { |
| 157 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 158 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 159 | // Remove any binaries we pre-loaded in the Target before |
| 160 | // launching/attaching. If the same binaries are present in the process, |
| 161 | // we'll get them from the shared module cache, we won't need to re-load them |
| 162 | // from disk. |
Jason Molenda | 848c7be | 2017-01-19 00:20:29 +0000 | [diff] [blame] | 163 | UnloadAllImages(); |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | StructuredData::ObjectSP all_image_info_json_sp( |
| 166 | m_process->GetLoadedDynamicLibrariesInfos()); |
| 167 | ImageInfo::collection image_infos; |
| 168 | if (all_image_info_json_sp.get() && |
| 169 | all_image_info_json_sp->GetAsDictionary() && |
| 170 | all_image_info_json_sp->GetAsDictionary()->HasKey("images") && |
| 171 | all_image_info_json_sp->GetAsDictionary() |
| 172 | ->GetValueForKey("images") |
| 173 | ->GetAsArray()) { |
| 174 | if (JSONImageInformationIntoImageInfo(all_image_info_json_sp, |
| 175 | image_infos)) { |
| 176 | if (log) |
| 177 | log->Printf("Initial module fetch: Adding %" PRId64 " modules.\n", |
| 178 | (uint64_t)image_infos.size()); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | UpdateSpecialBinariesFromNewImageInfos(image_infos); |
| 181 | AddModulesUsingImageInfos(image_infos); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 182 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
Jim Ingham | 29ae659 | 2018-12-07 01:18:40 +0000 | [diff] [blame] | 186 | m_maybe_image_infos_address = m_process->GetImageInfoAddress(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 190 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 191 | // Static callback function that gets called when our DYLD notification |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 192 | // breakpoint gets hit. We update all of our image infos and then let our super |
| 193 | // class DynamicLoader class decide if we should stop or not (based on global |
| 194 | // preference). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton, |
| 196 | StoppointCallbackContext *context, |
| 197 | lldb::user_id_t break_id, |
| 198 | lldb::user_id_t break_loc_id) { |
| 199 | // Let the event know that the images have changed |
| 200 | // DYLD passes three arguments to the notification breakpoint. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 201 | // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove |
| 202 | // all Arg2: unsigned long icount - Number of shared libraries |
| 203 | // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses |
| 204 | // of binaries added/removed |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | ExecutionContext exe_ctx(context->exe_ctx_ref); |
| 209 | Process *process = exe_ctx.GetProcessPtr(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | // This is a sanity check just in case this dyld_instance is an old dyld |
| 212 | // plugin's breakpoint still lying around. |
| 213 | if (process != dyld_instance->m_process) |
| 214 | return false; |
| 215 | |
| 216 | if (dyld_instance->m_image_infos_stop_id != UINT32_MAX && |
| 217 | process->GetStopID() < dyld_instance->m_image_infos_stop_id) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | const lldb::ABISP &abi = process->GetABI(); |
| 222 | if (abi) { |
| 223 | // Build up the value array to store the three arguments given above, then |
| 224 | // get the values from the ABI: |
| 225 | |
| 226 | ClangASTContext *clang_ast_context = |
| 227 | process->GetTarget().GetScratchClangASTContext(); |
| 228 | ValueList argument_values; |
| 229 | |
| 230 | Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0, |
| 231 | // dyld_notify_removing=1, dyld_notify_remove_all=2 }; |
| 232 | Value count_value; // unsigned long count |
| 233 | Value headers_value; // uint64_t machHeaders[] (aka void*) |
| 234 | |
| 235 | CompilerType clang_void_ptr_type = |
| 236 | clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); |
| 237 | CompilerType clang_uint32_type = |
| 238 | clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( |
| 239 | lldb::eEncodingUint, 32); |
| 240 | CompilerType clang_uint64_type = |
| 241 | clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( |
| 242 | lldb::eEncodingUint, 32); |
| 243 | |
| 244 | mode_value.SetValueType(Value::eValueTypeScalar); |
| 245 | mode_value.SetCompilerType(clang_uint32_type); |
| 246 | |
| 247 | if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) { |
| 248 | count_value.SetValueType(Value::eValueTypeScalar); |
| 249 | count_value.SetCompilerType(clang_uint32_type); |
| 250 | } else { |
| 251 | count_value.SetValueType(Value::eValueTypeScalar); |
| 252 | count_value.SetCompilerType(clang_uint64_type); |
Jason Molenda | 716814a | 2016-07-22 22:26:26 +0000 | [diff] [blame] | 253 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | headers_value.SetValueType(Value::eValueTypeScalar); |
| 256 | headers_value.SetCompilerType(clang_void_ptr_type); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | argument_values.PushValue(mode_value); |
| 259 | argument_values.PushValue(count_value); |
| 260 | argument_values.PushValue(headers_value); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) { |
| 263 | uint32_t dyld_mode = |
| 264 | argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1); |
| 265 | if (dyld_mode != static_cast<uint32_t>(-1)) { |
| 266 | // Okay the mode was right, now get the number of elements, and the |
| 267 | // array of new elements... |
| 268 | uint32_t image_infos_count = |
| 269 | argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1); |
| 270 | if (image_infos_count != static_cast<uint32_t>(-1)) { |
| 271 | addr_t header_array = |
| 272 | argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1); |
| 273 | if (header_array != static_cast<uint64_t>(-1)) { |
| 274 | std::vector<addr_t> image_load_addresses; |
| 275 | for (uint64_t i = 0; i < image_infos_count; i++) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 276 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | addr_t addr = process->ReadUnsignedIntegerFromMemory( |
| 278 | header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error); |
| 279 | if (addr != LLDB_INVALID_ADDRESS) { |
| 280 | image_load_addresses.push_back(addr); |
| 281 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 282 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | if (dyld_mode == 0) { |
| 284 | // dyld_notify_adding |
| 285 | dyld_instance->AddBinaries(image_load_addresses); |
| 286 | } else if (dyld_mode == 1) { |
| 287 | // dyld_notify_removing |
| 288 | dyld_instance->UnloadImages(image_load_addresses); |
| 289 | } else if (dyld_mode == 2) { |
| 290 | // dyld_notify_remove_all |
| 291 | dyld_instance->UnloadAllImages(); |
| 292 | } |
| 293 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 294 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 296 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | } else { |
| 298 | process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf( |
| 299 | "No ABI plugin located for triple %s -- shared libraries will not be " |
| 300 | "registered!\n", |
| 301 | process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str()); |
| 302 | } |
| 303 | |
| 304 | // Return true to stop the target, false to just let the target run |
| 305 | return dyld_instance->GetStopWhenImagesChange(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 308 | void DynamicLoaderMacOS::AddBinaries( |
| 309 | const std::vector<lldb::addr_t> &load_addresses) { |
| 310 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 311 | ImageInfo::collection image_infos; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | if (log) |
| 314 | log->Printf("Adding %" PRId64 " modules.", (uint64_t)load_addresses.size()); |
| 315 | StructuredData::ObjectSP binaries_info_sp = |
| 316 | m_process->GetLoadedDynamicLibrariesInfos(load_addresses); |
| 317 | if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() && |
| 318 | binaries_info_sp->GetAsDictionary()->HasKey("images") && |
| 319 | binaries_info_sp->GetAsDictionary() |
| 320 | ->GetValueForKey("images") |
| 321 | ->GetAsArray() && |
| 322 | binaries_info_sp->GetAsDictionary() |
| 323 | ->GetValueForKey("images") |
| 324 | ->GetAsArray() |
| 325 | ->GetSize() == load_addresses.size()) { |
| 326 | if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) { |
| 327 | UpdateSpecialBinariesFromNewImageInfos(image_infos); |
| 328 | AddModulesUsingImageInfos(image_infos); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 329 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
| 331 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 334 | // Dump the _dyld_all_image_infos members and all current image infos that we |
| 335 | // have parsed to the file handle provided. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | void DynamicLoaderMacOS::PutToLog(Log *log) const { |
| 337 | if (log == NULL) |
| 338 | return; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | bool DynamicLoaderMacOS::SetNotificationBreakpoint() { |
| 342 | if (m_break_id == LLDB_INVALID_BREAK_ID) { |
| 343 | ConstString g_symbol_name("_dyld_debugger_notification"); |
| 344 | const Symbol *symbol = nullptr; |
| 345 | ModuleSP dyld_sp(GetDYLDModule()); |
| 346 | if (dyld_sp) { |
| 347 | symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name, |
| 348 | eSymbolTypeCode); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 349 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | if (symbol && |
| 351 | (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) { |
| 352 | addr_t symbol_address = |
| 353 | symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget()); |
| 354 | if (symbol_address != LLDB_INVALID_ADDRESS) { |
| 355 | bool internal = true; |
| 356 | bool hardware = false; |
| 357 | Breakpoint *breakpoint = |
| 358 | m_process->GetTarget() |
| 359 | .CreateBreakpoint(symbol_address, internal, hardware) |
| 360 | .get(); |
| 361 | breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this, |
| 362 | true); |
| 363 | breakpoint->SetBreakpointKind("shared-library-event"); |
| 364 | m_break_id = breakpoint->GetID(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | return m_break_id != LLDB_INVALID_BREAK_ID; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 371 | addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) { |
| 373 | SymbolContext sc; |
| 374 | SymbolVendor *sym_vendor = module->GetSymbolVendor(); |
| 375 | Target &target = m_process->GetTarget(); |
| 376 | if (sym_vendor) { |
| 377 | Symtab *symtab = sym_vendor->GetSymtab(); |
| 378 | if (symtab) { |
| 379 | std::vector<uint32_t> match_indexes; |
| 380 | ConstString g_symbol_name("_dyld_global_lock_held"); |
| 381 | uint32_t num_matches = 0; |
| 382 | num_matches = |
| 383 | symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes); |
| 384 | if (num_matches == 1) { |
| 385 | Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]); |
| 386 | if (symbol && |
| 387 | (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) { |
| 388 | return symbol->GetAddressRef().GetOpcodeLoadAddress(&target); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 389 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 391 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | } |
| 393 | return LLDB_INVALID_ADDRESS; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Look for this symbol: |
| 397 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | // int __attribute__((visibility("hidden"))) _dyld_global_lock_held = |
| 399 | // 0; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 400 | // |
| 401 | // in libdyld.dylib. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 402 | Status DynamicLoaderMacOS::CanLoadImage() { |
| 403 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | addr_t symbol_address = LLDB_INVALID_ADDRESS; |
| 405 | Target &target = m_process->GetTarget(); |
| 406 | const ModuleList &target_modules = target.GetImages(); |
| 407 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
| 408 | const size_t num_modules = target_modules.GetSize(); |
| 409 | ConstString g_libdyld_name("libdyld.dylib"); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | // Find any modules named "libdyld.dylib" and look for the symbol there first |
| 412 | for (size_t i = 0; i < num_modules; i++) { |
| 413 | Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); |
| 414 | if (module_pointer) { |
| 415 | if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) { |
| 416 | symbol_address = GetDyldLockVariableAddressFromModule(module_pointer); |
| 417 | if (symbol_address != LLDB_INVALID_ADDRESS) |
| 418 | break; |
| 419 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 420 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | // Search through all modules looking for the symbol in them |
| 424 | if (symbol_address == LLDB_INVALID_ADDRESS) { |
| 425 | for (size_t i = 0; i < num_modules; i++) { |
| 426 | Module *module_pointer = |
| 427 | target_modules.GetModulePointerAtIndexUnlocked(i); |
| 428 | if (module_pointer) { |
| 429 | addr_t symbol_address = |
| 430 | GetDyldLockVariableAddressFromModule(module_pointer); |
| 431 | if (symbol_address != LLDB_INVALID_ADDRESS) |
| 432 | break; |
| 433 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 434 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 435 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 436 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 437 | // Default assumption is that it is OK to load images. Only say that we |
| 438 | // cannot load images if we find the symbol in libdyld and it indicates that |
| 439 | // we cannot. |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | if (symbol_address != LLDB_INVALID_ADDRESS) { |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 442 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | int lock_held = |
| 444 | m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error); |
| 445 | if (lock_held != 0) { |
Jim Ingham | abc5d72 | 2017-05-06 01:15:47 +0000 | [diff] [blame] | 446 | error.SetErrorString("dyld lock held - unsafe to load images."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 447 | } |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 448 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 450 | // If we were unable to find _dyld_global_lock_held in any modules, or it |
| 451 | // is not loaded into memory yet, we may be at process startup (sitting at |
| 452 | // _dyld_start) - so we should not allow dlopen calls. But if we found more |
| 453 | // than one module then we are clearly past _dyld_start so in that case |
| 454 | // we'll default to "it's safe". |
Jim Ingham | abc5d72 | 2017-05-06 01:15:47 +0000 | [diff] [blame] | 455 | if (num_modules <= 1) |
| 456 | error.SetErrorString("could not find the dyld library or " |
| 457 | "the dyld lock symbol"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | } |
| 459 | return error; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | bool DynamicLoaderMacOS::GetSharedCacheInformation( |
| 463 | lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache, |
| 464 | LazyBool &private_shared_cache) { |
| 465 | base_address = LLDB_INVALID_ADDRESS; |
| 466 | uuid.Clear(); |
| 467 | using_shared_cache = eLazyBoolCalculate; |
| 468 | private_shared_cache = eLazyBoolCalculate; |
Jason Molenda | 13becd4 | 2016-07-29 00:18:39 +0000 | [diff] [blame] | 469 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 470 | if (m_process) { |
| 471 | StructuredData::ObjectSP info = m_process->GetSharedCacheInfo(); |
| 472 | StructuredData::Dictionary *info_dict = nullptr; |
| 473 | if (info.get() && info->GetAsDictionary()) { |
| 474 | info_dict = info->GetAsDictionary(); |
Jason Molenda | 13becd4 | 2016-07-29 00:18:39 +0000 | [diff] [blame] | 475 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 477 | // {"shared_cache_base_address":140735683125248,"shared_cache_uuid |
| 478 | // ":"DDB8D70C- |
| 479 | // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | |
| 481 | if (info_dict && info_dict->HasKey("shared_cache_uuid") && |
| 482 | info_dict->HasKey("no_shared_cache") && |
| 483 | info_dict->HasKey("shared_cache_base_address")) { |
| 484 | base_address = info_dict->GetValueForKey("shared_cache_base_address") |
| 485 | ->GetIntegerValue(LLDB_INVALID_ADDRESS); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 486 | std::string uuid_str = |
| 487 | info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | if (!uuid_str.empty()) |
Pavel Labath | a174bcb | 2018-06-21 15:24:39 +0000 | [diff] [blame] | 489 | uuid.SetFromStringRef(uuid_str); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 490 | if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 Molenda | 13becd4 | 2016-07-29 00:18:39 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | void DynamicLoaderMacOS::Initialize() { |
| 507 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 508 | GetPluginDescriptionStatic(), CreateInstance); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | void DynamicLoaderMacOS::Terminate() { |
| 512 | PluginManager::UnregisterPlugin(CreateInstance); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 515 | lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() { |
| 516 | static ConstString g_name("macos-dyld"); |
| 517 | return g_name; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 520 | const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() { |
| 521 | return "Dynamic loader plug-in that watches for shared library loads/unloads " |
| 522 | "in MacOSX user processes."; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 525 | // PluginInterface protocol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 526 | lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() { |
| 527 | return GetPluginNameStatic(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; } |