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