Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderDarwin.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 | |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 10 | #include "DynamicLoaderDarwin.h" |
| 11 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 12 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 13 | #include "lldb/Core/DataBuffer.h" |
| 14 | #include "lldb/Core/DataBufferHeap.h" |
| 15 | #include "lldb/Core/Debugger.h" |
| 16 | #include "lldb/Core/Log.h" |
| 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/ModuleSpec.h" |
| 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/Section.h" |
| 21 | #include "lldb/Core/State.h" |
| 22 | #include "lldb/Expression/DiagnosticManager.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 23 | #include "lldb/Host/FileSystem.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/ClangASTContext.h" |
| 25 | #include "lldb/Symbol/Function.h" |
| 26 | #include "lldb/Symbol/ObjectFile.h" |
| 27 | #include "lldb/Target/ABI.h" |
| 28 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 29 | #include "lldb/Target/RegisterContext.h" |
| 30 | #include "lldb/Target/StackFrame.h" |
| 31 | #include "lldb/Target/Target.h" |
| 32 | #include "lldb/Target/Thread.h" |
| 33 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 34 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
| 35 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 36 | //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN |
| 37 | #ifdef ENABLE_DEBUG_PRINTF |
| 38 | #include <stdio.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 40 | #else |
| 41 | #define DEBUG_PRINTF(fmt, ...) |
| 42 | #endif |
| 43 | |
| 44 | #ifndef __APPLE__ |
| 45 | #include "Utility/UuidCompatibility.h" |
| 46 | #else |
| 47 | #include <uuid/uuid.h> |
| 48 | #endif |
| 49 | |
| 50 | using namespace lldb; |
| 51 | using namespace lldb_private; |
| 52 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 53 | //---------------------------------------------------------------------- |
| 54 | // Constructor |
| 55 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process) |
| 57 | : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(), |
| 58 | m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(), |
| 59 | m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {} |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 60 | |
| 61 | //---------------------------------------------------------------------- |
| 62 | // Destructor |
| 63 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | DynamicLoaderDarwin::~DynamicLoaderDarwin() {} |
| 65 | |
| 66 | //------------------------------------------------------------------ |
| 67 | /// Called after attaching a process. |
| 68 | /// |
| 69 | /// Allow DynamicLoader plug-ins to execute some code after |
| 70 | /// attaching to a process. |
| 71 | //------------------------------------------------------------------ |
| 72 | void DynamicLoaderDarwin::DidAttach() { |
| 73 | PrivateInitialize(m_process); |
| 74 | DoInitialImageFetch(); |
| 75 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //------------------------------------------------------------------ |
| 79 | /// Called after attaching a process. |
| 80 | /// |
| 81 | /// Allow DynamicLoader plug-ins to execute some code after |
| 82 | /// attaching to a process. |
| 83 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | void DynamicLoaderDarwin::DidLaunch() { |
| 85 | PrivateInitialize(m_process); |
| 86 | DoInitialImageFetch(); |
| 87 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 90 | //---------------------------------------------------------------------- |
| 91 | // Clear out the state of this class. |
| 92 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | void DynamicLoaderDarwin::Clear(bool clear_process) { |
| 94 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 95 | if (clear_process) |
| 96 | m_process = NULL; |
| 97 | m_dyld_image_infos.clear(); |
| 98 | m_dyld_image_infos_stop_id = UINT32_MAX; |
| 99 | m_dyld.Clear(false); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo( |
| 103 | ImageInfo &image_info, bool can_create, bool *did_create_ptr) { |
| 104 | if (did_create_ptr) |
| 105 | *did_create_ptr = false; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | Target &target = m_process->GetTarget(); |
| 108 | const ModuleList &target_images = target.GetImages(); |
| 109 | ModuleSpec module_spec(image_info.file_spec); |
| 110 | module_spec.GetUUID() = image_info.uuid; |
| 111 | ModuleSP module_sp(target_images.FindFirstModule(module_spec)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | if (module_sp && !module_spec.GetUUID().IsValid() && |
| 114 | !module_sp->GetUUID().IsValid()) { |
| 115 | // No UUID, we must rely upon the cached module modification |
| 116 | // time and the modification time of the file on disk |
| 117 | if (module_sp->GetModificationTime() != |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 118 | FileSystem::GetModificationTime(module_sp->GetFileSpec())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | module_sp.reset(); |
| 120 | } |
| 121 | |
| 122 | if (!module_sp) { |
| 123 | if (can_create) { |
| 124 | module_sp = target.GetSharedModule(module_spec); |
| 125 | if (!module_sp || module_sp->GetObjectFile() == NULL) |
| 126 | module_sp = m_process->ReadModuleFromMemory(image_info.file_spec, |
| 127 | image_info.address); |
| 128 | |
| 129 | if (did_create_ptr) |
| 130 | *did_create_ptr = (bool)module_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 131 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } |
| 133 | return module_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | void DynamicLoaderDarwin::UnloadImages( |
| 137 | const std::vector<lldb::addr_t> &solib_addresses) { |
| 138 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 139 | if (m_process->GetStopID() == m_dyld_image_infos_stop_id) |
| 140 | return; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 143 | Target &target = m_process->GetTarget(); |
| 144 | if (log) |
| 145 | log->Printf("Removing %" PRId64 " modules.", |
| 146 | (uint64_t)solib_addresses.size()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | ModuleList unloaded_module_list; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | for (addr_t solib_addr : solib_addresses) { |
| 151 | Address header; |
| 152 | if (header.SetLoadAddress(solib_addr, &target)) { |
| 153 | if (header.GetOffset() == 0) { |
| 154 | ModuleSP module_to_remove(header.GetModule()); |
| 155 | if (module_to_remove.get()) { |
| 156 | if (log) |
| 157 | log->Printf("Removing module at address 0x%" PRIx64, solib_addr); |
| 158 | // remove the sections from the Target |
| 159 | UnloadSections(module_to_remove); |
| 160 | // add this to the list of modules to remove |
| 161 | unloaded_module_list.AppendIfNeeded(module_to_remove); |
| 162 | // remove the entry from the m_dyld_image_infos |
| 163 | ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end(); |
| 164 | for (pos = m_dyld_image_infos.begin(); pos != end; pos++) { |
| 165 | if (solib_addr == (*pos).address) { |
| 166 | m_dyld_image_infos.erase(pos); |
| 167 | break; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 168 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 170 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 172 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | if (unloaded_module_list.GetSize() > 0) { |
| 176 | if (log) { |
| 177 | log->PutCString("Unloaded:"); |
| 178 | unloaded_module_list.LogUUIDAndPaths( |
| 179 | log, "DynamicLoaderDarwin::UnloadModules"); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 180 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | m_process->GetTarget().GetImages().Remove(unloaded_module_list); |
| 182 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
| 183 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | void DynamicLoaderDarwin::UnloadAllImages() { |
| 187 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 188 | ModuleList unloaded_modules_list; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | Target &target = m_process->GetTarget(); |
| 191 | const ModuleList &target_modules = target.GetImages(); |
| 192 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | size_t num_modules = target_modules.GetSize(); |
| 195 | ModuleSP dyld_sp(GetDYLDModule()); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | for (size_t i = 0; i < num_modules; i++) { |
| 198 | ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | // Don't remove dyld - else we'll lose our breakpoint notifying us about |
| 201 | // libraries |
| 202 | // being re-loaded... |
| 203 | if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) { |
| 204 | UnloadSections(module_sp); |
| 205 | unloaded_modules_list.Append(module_sp); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 206 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | if (unloaded_modules_list.GetSize() != 0) { |
| 210 | if (log) { |
| 211 | log->PutCString("Unloaded:"); |
| 212 | unloaded_modules_list.LogUUIDAndPaths( |
| 213 | log, "DynamicLoaderDarwin::UnloadAllImages"); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 214 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | target.GetImages().Remove(unloaded_modules_list); |
| 216 | m_dyld_image_infos.clear(); |
| 217 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
| 218 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 221 | //---------------------------------------------------------------------- |
| 222 | // Update the load addresses for all segments in MODULE using the |
| 223 | // updated INFO that is passed in. |
| 224 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module, |
| 226 | ImageInfo &info) { |
| 227 | bool changed = false; |
| 228 | if (module) { |
| 229 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 230 | if (image_object_file) { |
| 231 | SectionList *section_list = image_object_file->GetSectionList(); |
| 232 | if (section_list) { |
| 233 | std::vector<uint32_t> inaccessible_segment_indexes; |
| 234 | // We now know the slide amount, so go through all sections |
| 235 | // and update the load addresses with the correct values. |
| 236 | const size_t num_segments = info.segments.size(); |
| 237 | for (size_t i = 0; i < num_segments; ++i) { |
| 238 | // Only load a segment if it has protections. Things like |
| 239 | // __PAGEZERO don't have any protections, and they shouldn't |
| 240 | // be slid |
| 241 | SectionSP section_sp( |
| 242 | section_list->FindSectionByName(info.segments[i].name)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | if (info.segments[i].maxprot == 0) { |
| 245 | inaccessible_segment_indexes.push_back(i); |
| 246 | } else { |
| 247 | const addr_t new_section_load_addr = |
| 248 | info.segments[i].vmaddr + info.slide; |
| 249 | static ConstString g_section_name_LINKEDIT("__LINKEDIT"); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 250 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | if (section_sp) { |
| 252 | // __LINKEDIT sections from files in the shared cache |
| 253 | // can overlap so check to see what the segment name is |
| 254 | // and pass "false" so we don't warn of overlapping |
| 255 | // "Section" objects, and "true" for all other sections. |
| 256 | const bool warn_multiple = |
| 257 | section_sp->GetName() != g_section_name_LINKEDIT; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | changed = m_process->GetTarget().SetSectionLoadAddress( |
| 260 | section_sp, new_section_load_addr, warn_multiple); |
| 261 | } else { |
| 262 | Host::SystemLog( |
| 263 | Host::eSystemLogWarning, |
| 264 | "warning: unable to find and load segment named '%s' at " |
| 265 | "0x%" PRIx64 " in '%s' in macosx dynamic loader plug-in.\n", |
| 266 | info.segments[i].name.AsCString("<invalid>"), |
| 267 | (uint64_t)new_section_load_addr, |
| 268 | image_object_file->GetFileSpec().GetPath().c_str()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 269 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 271 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | |
| 273 | // If the loaded the file (it changed) and we have segments that |
| 274 | // are not readable or writeable, add them to the invalid memory |
| 275 | // region cache for the process. This will typically only be |
| 276 | // the __PAGEZERO segment in the main executable. We might be able |
| 277 | // to apply this more generally to more sections that have no |
| 278 | // protections in the future, but for now we are going to just |
| 279 | // do __PAGEZERO. |
| 280 | if (changed && !inaccessible_segment_indexes.empty()) { |
| 281 | for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) { |
| 282 | const uint32_t seg_idx = inaccessible_segment_indexes[i]; |
| 283 | SectionSP section_sp( |
| 284 | section_list->FindSectionByName(info.segments[seg_idx].name)); |
| 285 | |
| 286 | if (section_sp) { |
| 287 | static ConstString g_pagezero_section_name("__PAGEZERO"); |
| 288 | if (g_pagezero_section_name == section_sp->GetName()) { |
| 289 | // __PAGEZERO never slides... |
| 290 | const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr; |
| 291 | const lldb::addr_t vmsize = info.segments[seg_idx].vmsize; |
| 292 | Process::LoadRange pagezero_range(vmaddr, vmsize); |
| 293 | m_process->AddInvalidMemoryRegion(pagezero_range); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 299 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | } |
| 301 | // We might have an in memory image that was loaded as soon as it was created |
| 302 | if (info.load_stop_id == m_process->GetStopID()) |
| 303 | changed = true; |
| 304 | else if (changed) { |
| 305 | // Update the stop ID when this library was updated |
| 306 | info.load_stop_id = m_process->GetStopID(); |
| 307 | } |
| 308 | return changed; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //---------------------------------------------------------------------- |
| 312 | // Unload the segments in MODULE using the INFO that is passed in. |
| 313 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | bool DynamicLoaderDarwin::UnloadModuleSections(Module *module, |
| 315 | ImageInfo &info) { |
| 316 | bool changed = false; |
| 317 | if (module) { |
| 318 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 319 | if (image_object_file) { |
| 320 | SectionList *section_list = image_object_file->GetSectionList(); |
| 321 | if (section_list) { |
| 322 | const size_t num_segments = info.segments.size(); |
| 323 | for (size_t i = 0; i < num_segments; ++i) { |
| 324 | SectionSP section_sp( |
| 325 | section_list->FindSectionByName(info.segments[i].name)); |
| 326 | if (section_sp) { |
| 327 | const addr_t old_section_load_addr = |
| 328 | info.segments[i].vmaddr + info.slide; |
| 329 | if (m_process->GetTarget().SetSectionUnloaded( |
| 330 | section_sp, old_section_load_addr)) |
| 331 | changed = true; |
| 332 | } else { |
| 333 | Host::SystemLog(Host::eSystemLogWarning, |
| 334 | "warning: unable to find and unload segment named " |
| 335 | "'%s' in '%s' in macosx dynamic loader plug-in.\n", |
| 336 | info.segments[i].name.AsCString("<invalid>"), |
| 337 | image_object_file->GetFileSpec().GetPath().c_str()); |
| 338 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 339 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 341 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | } |
| 343 | return changed; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 346 | // Given a JSON dictionary (from debugserver, most likely) of binary images |
| 347 | // loaded in the inferior |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 348 | // process, add the images to the ImageInfo collection. |
| 349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo( |
| 351 | StructuredData::ObjectSP image_details, |
| 352 | ImageInfo::collection &image_infos) { |
| 353 | StructuredData::ObjectSP images_sp = |
| 354 | image_details->GetAsDictionary()->GetValueForKey("images"); |
| 355 | if (images_sp.get() == nullptr) |
| 356 | return false; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 358 | image_infos.resize(images_sp->GetAsArray()->GetSize()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 359 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | for (size_t i = 0; i < image_infos.size(); i++) { |
| 361 | StructuredData::ObjectSP image_sp = |
| 362 | images_sp->GetAsArray()->GetItemAtIndex(i); |
| 363 | if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr) |
| 364 | return false; |
| 365 | StructuredData::Dictionary *image = image_sp->GetAsDictionary(); |
| 366 | if (image->HasKey("load_address") == false || |
| 367 | image->HasKey("pathname") == false || |
| 368 | image->HasKey("mod_date") == false || |
| 369 | image->HasKey("mach_header") == false || |
| 370 | image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr || |
| 371 | image->HasKey("segments") == false || |
| 372 | image->GetValueForKey("segments")->GetAsArray() == nullptr || |
| 373 | image->HasKey("uuid") == false) { |
| 374 | return false; |
| 375 | } |
| 376 | image_infos[i].address = |
| 377 | image->GetValueForKey("load_address")->GetAsInteger()->GetValue(); |
| 378 | image_infos[i].mod_date = |
| 379 | image->GetValueForKey("mod_date")->GetAsInteger()->GetValue(); |
| 380 | image_infos[i].file_spec.SetFile( |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 381 | image->GetValueForKey("pathname")->GetAsString()->GetValue(), false); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | StructuredData::Dictionary *mh = |
| 384 | image->GetValueForKey("mach_header")->GetAsDictionary(); |
| 385 | image_infos[i].header.magic = |
| 386 | mh->GetValueForKey("magic")->GetAsInteger()->GetValue(); |
| 387 | image_infos[i].header.cputype = |
| 388 | mh->GetValueForKey("cputype")->GetAsInteger()->GetValue(); |
| 389 | image_infos[i].header.cpusubtype = |
| 390 | mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue(); |
| 391 | image_infos[i].header.filetype = |
| 392 | mh->GetValueForKey("filetype")->GetAsInteger()->GetValue(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | if (image->HasKey("min_version_os_name")) { |
| 395 | std::string os_name = image->GetValueForKey("min_version_os_name") |
| 396 | ->GetAsString() |
| 397 | ->GetValue(); |
| 398 | if (os_name == "macosx") |
| 399 | image_infos[i].os_type = llvm::Triple::MacOSX; |
| 400 | else if (os_name == "ios" || os_name == "iphoneos") |
| 401 | image_infos[i].os_type = llvm::Triple::IOS; |
| 402 | else if (os_name == "tvos") |
| 403 | image_infos[i].os_type = llvm::Triple::TvOS; |
| 404 | else if (os_name == "watchos") |
| 405 | image_infos[i].os_type = llvm::Triple::WatchOS; |
| 406 | } |
| 407 | if (image->HasKey("min_version_os_sdk")) { |
| 408 | image_infos[i].min_version_os_sdk = |
| 409 | image->GetValueForKey("min_version_os_sdk") |
| 410 | ->GetAsString() |
| 411 | ->GetValue(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't |
| 415 | // currently send them |
| 416 | // in the reply. |
| 417 | |
| 418 | if (mh->HasKey("flags")) |
| 419 | image_infos[i].header.flags = |
| 420 | mh->GetValueForKey("flags")->GetAsInteger()->GetValue(); |
| 421 | else |
| 422 | image_infos[i].header.flags = 0; |
| 423 | |
| 424 | if (mh->HasKey("ncmds")) |
| 425 | image_infos[i].header.ncmds = |
| 426 | mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue(); |
| 427 | else |
| 428 | image_infos[i].header.ncmds = 0; |
| 429 | |
| 430 | if (mh->HasKey("sizeofcmds")) |
| 431 | image_infos[i].header.sizeofcmds = |
| 432 | mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue(); |
| 433 | else |
| 434 | image_infos[i].header.sizeofcmds = 0; |
| 435 | |
| 436 | StructuredData::Array *segments = |
| 437 | image->GetValueForKey("segments")->GetAsArray(); |
| 438 | uint32_t segcount = segments->GetSize(); |
| 439 | for (size_t j = 0; j < segcount; j++) { |
| 440 | Segment segment; |
| 441 | StructuredData::Dictionary *seg = |
| 442 | segments->GetItemAtIndex(j)->GetAsDictionary(); |
| 443 | segment.name = ConstString( |
| 444 | seg->GetValueForKey("name")->GetAsString()->GetValue().c_str()); |
| 445 | segment.vmaddr = |
| 446 | seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue(); |
| 447 | segment.vmsize = |
| 448 | seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue(); |
| 449 | segment.fileoff = |
| 450 | seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue(); |
| 451 | segment.filesize = |
| 452 | seg->GetValueForKey("filesize")->GetAsInteger()->GetValue(); |
| 453 | segment.maxprot = |
| 454 | seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue(); |
| 455 | |
| 456 | // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't |
| 457 | // currently send them |
| 458 | // in the reply. |
| 459 | |
| 460 | if (seg->HasKey("initprot")) |
| 461 | segment.initprot = |
| 462 | seg->GetValueForKey("initprot")->GetAsInteger()->GetValue(); |
| 463 | else |
| 464 | segment.initprot = 0; |
| 465 | |
| 466 | if (seg->HasKey("flags")) |
| 467 | segment.flags = |
| 468 | seg->GetValueForKey("flags")->GetAsInteger()->GetValue(); |
| 469 | else |
| 470 | segment.flags = 0; |
| 471 | |
| 472 | if (seg->HasKey("nsects")) |
| 473 | segment.nsects = |
| 474 | seg->GetValueForKey("nsects")->GetAsInteger()->GetValue(); |
| 475 | else |
| 476 | segment.nsects = 0; |
| 477 | |
| 478 | image_infos[i].segments.push_back(segment); |
| 479 | } |
| 480 | |
| 481 | image_infos[i].uuid.SetFromCString( |
| 482 | image->GetValueForKey("uuid")->GetAsString()->GetValue().c_str()); |
| 483 | |
| 484 | // All sections listed in the dyld image info structure will all |
| 485 | // either be fixed up already, or they will all be off by a single |
| 486 | // slide amount that is determined by finding the first segment |
| 487 | // that is at file offset zero which also has bytes (a file size |
| 488 | // that is greater than zero) in the object file. |
| 489 | |
| 490 | // Determine the slide amount (if any) |
| 491 | const size_t num_sections = image_infos[i].segments.size(); |
| 492 | for (size_t k = 0; k < num_sections; ++k) { |
| 493 | // Iterate through the object file sections to find the |
| 494 | // first section that starts of file offset zero and that |
| 495 | // has bytes in the file... |
| 496 | if ((image_infos[i].segments[k].fileoff == 0 && |
| 497 | image_infos[i].segments[k].filesize > 0) || |
| 498 | (image_infos[i].segments[k].name == ConstString("__TEXT"))) { |
| 499 | image_infos[i].slide = |
| 500 | image_infos[i].address - image_infos[i].segments[k].vmaddr; |
| 501 | // We have found the slide amount, so we can exit |
| 502 | // this for loop. |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return true; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos( |
| 512 | ImageInfo::collection &image_infos) { |
| 513 | uint32_t exe_idx = UINT32_MAX; |
| 514 | uint32_t dyld_idx = UINT32_MAX; |
| 515 | Target &target = m_process->GetTarget(); |
| 516 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 517 | ConstString g_dyld_sim_filename("dyld_sim"); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | ArchSpec target_arch = target.GetArchitecture(); |
| 520 | const size_t image_infos_size = image_infos.size(); |
| 521 | for (size_t i = 0; i < image_infos_size; i++) { |
| 522 | if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) { |
| 523 | // In a "simulator" process (an x86 process that is ios/tvos/watchos) |
| 524 | // we will have two dyld modules -- a "dyld" that we want to keep track |
| 525 | // of, |
| 526 | // and a "dyld_sim" which we don't need to keep track of here. |
| 527 | // If the target is an x86 system and the OS of the dyld binary is |
| 528 | // ios/tvos/watchos, then we are looking at dyld_sym. |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | // debugserver has only recently (late 2016) started sending up the |
| 531 | // os type for each binary it sees -- so if we don't have an os |
| 532 | // type, use a filename check as our next best guess. |
| 533 | if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) { |
| 534 | if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) { |
| 535 | dyld_idx = i; |
| 536 | } |
| 537 | } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 || |
| 538 | target_arch.GetTriple().getArch() == llvm::Triple::x86_64) { |
| 539 | if (image_infos[i].os_type != llvm::Triple::OSType::IOS && |
| 540 | image_infos[i].os_type != llvm::Triple::TvOS && |
| 541 | image_infos[i].os_type != llvm::Triple::WatchOS) { |
| 542 | dyld_idx = i; |
| 543 | } |
| 544 | } |
Jason Molenda | 777fcec | 2017-01-21 01:17:36 +0000 | [diff] [blame^] | 545 | else { |
| 546 | // catch-all for any other environment -- trust that dyld is actually dyld |
| 547 | dyld_idx = i; |
| 548 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) { |
| 550 | exe_idx = i; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (exe_idx != UINT32_MAX) { |
| 555 | const bool can_create = true; |
| 556 | ModuleSP exe_module_sp( |
| 557 | FindTargetModuleForImageInfo(image_infos[exe_idx], can_create, NULL)); |
| 558 | if (exe_module_sp) { |
| 559 | if (log) |
| 560 | log->Printf("Found executable module: %s", |
| 561 | exe_module_sp->GetFileSpec().GetPath().c_str()); |
| 562 | target.GetImages().AppendIfNeeded(exe_module_sp); |
| 563 | UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]); |
| 564 | if (exe_module_sp.get() != target.GetExecutableModulePointer()) { |
| 565 | const bool get_dependent_images = false; |
| 566 | target.SetExecutableModule(exe_module_sp, get_dependent_images); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (dyld_idx != UINT32_MAX) { |
| 572 | const bool can_create = true; |
| 573 | ModuleSP dyld_sp = |
| 574 | FindTargetModuleForImageInfo(image_infos[dyld_idx], can_create, NULL); |
| 575 | if (dyld_sp.get()) { |
| 576 | if (log) |
| 577 | log->Printf("Found dyld module: %s", |
| 578 | dyld_sp->GetFileSpec().GetPath().c_str()); |
| 579 | target.GetImages().AppendIfNeeded(dyld_sp); |
| 580 | UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]); |
| 581 | SetDYLDModule(dyld_sp); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo( |
| 587 | ImageInfo &image_info) { |
| 588 | if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) { |
| 589 | const bool can_create = true; |
| 590 | ModuleSP dyld_sp = |
| 591 | FindTargetModuleForImageInfo(image_info, can_create, NULL); |
| 592 | if (dyld_sp.get()) { |
| 593 | Target &target = m_process->GetTarget(); |
| 594 | target.GetImages().AppendIfNeeded(dyld_sp); |
| 595 | UpdateImageLoadAddress(dyld_sp.get(), image_info); |
| 596 | SetDYLDModule(dyld_sp); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) { |
| 602 | m_dyld_module_wp = dyld_module_sp; |
| 603 | } |
| 604 | |
| 605 | ModuleSP DynamicLoaderDarwin::GetDYLDModule() { |
| 606 | ModuleSP dyld_sp(m_dyld_module_wp.lock()); |
| 607 | return dyld_sp; |
| 608 | } |
| 609 | |
| 610 | bool DynamicLoaderDarwin::AddModulesUsingImageInfos( |
| 611 | ImageInfo::collection &image_infos) { |
| 612 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 613 | // Now add these images to the main list. |
| 614 | ModuleList loaded_module_list; |
| 615 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 616 | Target &target = m_process->GetTarget(); |
| 617 | ModuleList &target_images = target.GetImages(); |
| 618 | |
| 619 | for (uint32_t idx = 0; idx < image_infos.size(); ++idx) { |
| 620 | if (log) { |
| 621 | log->Printf("Adding new image at address=0x%16.16" PRIx64 ".", |
| 622 | image_infos[idx].address); |
| 623 | image_infos[idx].PutToLog(log); |
| 624 | } |
| 625 | |
| 626 | m_dyld_image_infos.push_back(image_infos[idx]); |
| 627 | |
| 628 | ModuleSP image_module_sp( |
| 629 | FindTargetModuleForImageInfo(image_infos[idx], true, NULL)); |
| 630 | |
| 631 | if (image_module_sp) { |
| 632 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 633 | if (objfile) { |
| 634 | SectionList *sections = objfile->GetSectionList(); |
| 635 | if (sections) { |
| 636 | ConstString commpage_dbstr("__commpage"); |
| 637 | Section *commpage_section = |
| 638 | sections->FindSectionByName(commpage_dbstr).get(); |
| 639 | if (commpage_section) { |
| 640 | ModuleSpec module_spec(objfile->GetFileSpec(), |
| 641 | image_infos[idx].GetArchitecture()); |
| 642 | module_spec.GetObjectName() = commpage_dbstr; |
| 643 | ModuleSP commpage_image_module_sp( |
| 644 | target_images.FindFirstModule(module_spec)); |
| 645 | if (!commpage_image_module_sp) { |
| 646 | module_spec.SetObjectOffset(objfile->GetFileOffset() + |
| 647 | commpage_section->GetFileOffset()); |
| 648 | module_spec.SetObjectSize(objfile->GetByteSize()); |
| 649 | commpage_image_module_sp = target.GetSharedModule(module_spec); |
| 650 | if (!commpage_image_module_sp || |
| 651 | commpage_image_module_sp->GetObjectFile() == NULL) { |
| 652 | commpage_image_module_sp = m_process->ReadModuleFromMemory( |
| 653 | image_infos[idx].file_spec, image_infos[idx].address); |
| 654 | // Always load a memory image right away in the target in case |
| 655 | // we end up trying to read the symbol table from memory... The |
| 656 | // __LINKEDIT will need to be mapped so we can figure out where |
| 657 | // the symbol table bits are... |
| 658 | bool changed = false; |
| 659 | UpdateImageLoadAddress(commpage_image_module_sp.get(), |
| 660 | image_infos[idx]); |
| 661 | target.GetImages().Append(commpage_image_module_sp); |
| 662 | if (changed) { |
| 663 | image_infos[idx].load_stop_id = m_process->GetStopID(); |
| 664 | loaded_module_list.AppendIfNeeded(commpage_image_module_sp); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 665 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 666 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 667 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 668 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 669 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 670 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 671 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 672 | // UpdateImageLoadAddress will return true if any segments |
| 673 | // change load address. We need to check this so we don't |
| 674 | // mention that all loaded shared libraries are newly loaded |
| 675 | // each time we hit out dyld breakpoint since dyld will list all |
| 676 | // shared libraries each time. |
| 677 | if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) { |
| 678 | target_images.AppendIfNeeded(image_module_sp); |
| 679 | loaded_module_list.AppendIfNeeded(image_module_sp); |
| 680 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 681 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 682 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 683 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 684 | if (loaded_module_list.GetSize() > 0) { |
| 685 | if (log) |
| 686 | loaded_module_list.LogUUIDAndPaths(log, |
| 687 | "DynamicLoaderDarwin::ModulesDidLoad"); |
| 688 | m_process->GetTarget().ModulesDidLoad(loaded_module_list); |
| 689 | } |
| 690 | return true; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 693 | //---------------------------------------------------------------------- |
| 694 | // On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch |
| 695 | // functions written in hand-written assembly, and also have hand-written unwind |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 696 | // information in the eh_frame section. Normally we prefer analyzing the |
| 697 | // assembly instructions of a currently executing frame to unwind from that |
| 698 | // frame -- |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 699 | // but on hand-written functions this profiling can fail. We should use the |
| 700 | // eh_frame instructions for these functions all the time. |
| 701 | // |
| 702 | // As an aside, it would be better if the eh_frame entries had a flag (or were |
| 703 | // extensible so they could have an Apple-specific flag) which indicates that |
| 704 | // the instructions are asynchronous -- accurate at every instruction, instead |
| 705 | // of our normal default assumption that they are not. |
| 706 | //---------------------------------------------------------------------- |
| 707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 708 | bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) { |
| 709 | ModuleSP module_sp; |
| 710 | if (sym_ctx.symbol) { |
| 711 | module_sp = sym_ctx.symbol->GetAddressRef().GetModule(); |
| 712 | } |
| 713 | if (module_sp.get() == NULL && sym_ctx.function) { |
| 714 | module_sp = |
| 715 | sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule(); |
| 716 | } |
| 717 | if (module_sp.get() == NULL) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 718 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 719 | |
| 720 | ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime(); |
| 721 | if (objc_runtime != NULL && objc_runtime->IsModuleObjCLibrary(module_sp)) { |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | return false; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 728 | //---------------------------------------------------------------------- |
| 729 | // Dump a Segment to the file handle provided. |
| 730 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 731 | void DynamicLoaderDarwin::Segment::PutToLog(Log *log, |
| 732 | lldb::addr_t slide) const { |
| 733 | if (log) { |
| 734 | if (slide == 0) |
| 735 | log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")", |
| 736 | name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize); |
| 737 | else |
| 738 | log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 |
| 739 | ") slide = 0x%" PRIx64, |
| 740 | name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize, |
| 741 | slide); |
| 742 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | const DynamicLoaderDarwin::Segment * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | DynamicLoaderDarwin::ImageInfo::FindSegment(const ConstString &name) const { |
| 747 | const size_t num_segments = segments.size(); |
| 748 | for (size_t i = 0; i < num_segments; ++i) { |
| 749 | if (segments[i].name == name) |
| 750 | return &segments[i]; |
| 751 | } |
| 752 | return NULL; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 755 | //---------------------------------------------------------------------- |
| 756 | // Dump an image info structure to the file handle provided. |
| 757 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const { |
| 759 | if (log == NULL) |
| 760 | return; |
| 761 | const uint8_t *u = (const uint8_t *)uuid.GetBytes(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | if (address == LLDB_INVALID_ADDRESS) { |
| 764 | if (u) { |
| 765 | log->Printf("\t modtime=0x%8.8" PRIx64 |
| 766 | " uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-" |
| 767 | "%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X path='%s' (UNLOADED)", |
| 768 | mod_date, u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], |
| 769 | u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15], |
| 770 | file_spec.GetPath().c_str()); |
| 771 | } else |
| 772 | log->Printf("\t modtime=0x%8.8" PRIx64 |
| 773 | " path='%s' (UNLOADED)", |
| 774 | mod_date, file_spec.GetPath().c_str()); |
| 775 | } else { |
| 776 | if (u) { |
| 777 | log->Printf("\taddress=0x%16.16" PRIx64 " modtime=0x%8.8" PRIx64 |
| 778 | " uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-" |
| 779 | "%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X path='%s'", |
| 780 | address, mod_date, u[0], u[1], u[2], u[3], u[4], u[5], u[6], |
| 781 | u[7], u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15], |
| 782 | file_spec.GetPath().c_str()); |
| 783 | } else { |
| 784 | log->Printf("\taddress=0x%16.16" PRIx64 " modtime=0x%8.8" PRIx64 |
| 785 | " path='%s'", |
| 786 | address, mod_date, file_spec.GetPath().c_str()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 787 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 788 | for (uint32_t i = 0; i < segments.size(); ++i) |
| 789 | segments[i].PutToLog(log, slide); |
| 790 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 793 | void DynamicLoaderDarwin::PrivateInitialize(Process *process) { |
| 794 | DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__, |
| 795 | StateAsCString(m_process->GetState())); |
| 796 | Clear(true); |
| 797 | m_process = process; |
| 798 | m_process->GetTarget().ClearAllLoadedSections(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | //---------------------------------------------------------------------- |
| 802 | // Member function that gets called when the process state changes. |
| 803 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 804 | void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process, |
| 805 | StateType state) { |
| 806 | DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__, |
| 807 | StateAsCString(state)); |
| 808 | switch (state) { |
| 809 | case eStateConnected: |
| 810 | case eStateAttaching: |
| 811 | case eStateLaunching: |
| 812 | case eStateInvalid: |
| 813 | case eStateUnloaded: |
| 814 | case eStateExited: |
| 815 | case eStateDetached: |
| 816 | Clear(false); |
| 817 | break; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 818 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | case eStateStopped: |
| 820 | // Keep trying find dyld and set our notification breakpoint each time |
| 821 | // we stop until we succeed |
| 822 | if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) { |
| 823 | if (NeedToDoInitialImageFetch()) |
| 824 | DoInitialImageFetch(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 826 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 827 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 828 | break; |
| 829 | |
| 830 | case eStateRunning: |
| 831 | case eStateStepping: |
| 832 | case eStateCrashed: |
| 833 | case eStateSuspended: |
| 834 | break; |
| 835 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | ThreadPlanSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread, |
| 840 | bool stop_others) { |
| 841 | ThreadPlanSP thread_plan_sp; |
| 842 | StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get(); |
| 843 | const SymbolContext ¤t_context = |
| 844 | current_frame->GetSymbolContext(eSymbolContextSymbol); |
| 845 | Symbol *current_symbol = current_context.symbol; |
| 846 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 847 | TargetSP target_sp(thread.CalculateTarget()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 848 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 849 | if (current_symbol != NULL) { |
| 850 | std::vector<Address> addresses; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 852 | if (current_symbol->IsTrampoline()) { |
| 853 | const ConstString &trampoline_name = current_symbol->GetMangled().GetName( |
| 854 | current_symbol->GetLanguage(), Mangled::ePreferMangled); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 856 | if (trampoline_name) { |
| 857 | const ModuleList &images = target_sp->GetImages(); |
| 858 | |
| 859 | SymbolContextList code_symbols; |
| 860 | images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode, |
| 861 | code_symbols); |
| 862 | size_t num_code_symbols = code_symbols.GetSize(); |
| 863 | |
| 864 | if (num_code_symbols > 0) { |
| 865 | for (uint32_t i = 0; i < num_code_symbols; i++) { |
| 866 | SymbolContext context; |
| 867 | AddressRange addr_range; |
| 868 | if (code_symbols.GetContextAtIndex(i, context)) { |
| 869 | context.GetAddressRange(eSymbolContextEverything, 0, false, |
| 870 | addr_range); |
| 871 | addresses.push_back(addr_range.GetBaseAddress()); |
| 872 | if (log) { |
| 873 | addr_t load_addr = |
| 874 | addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); |
| 875 | |
| 876 | log->Printf("Found a trampoline target symbol at 0x%" PRIx64 |
| 877 | ".", |
| 878 | load_addr); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | SymbolContextList reexported_symbols; |
| 885 | images.FindSymbolsWithNameAndType( |
| 886 | trampoline_name, eSymbolTypeReExported, reexported_symbols); |
| 887 | size_t num_reexported_symbols = reexported_symbols.GetSize(); |
| 888 | if (num_reexported_symbols > 0) { |
| 889 | for (uint32_t i = 0; i < num_reexported_symbols; i++) { |
| 890 | SymbolContext context; |
| 891 | if (reexported_symbols.GetContextAtIndex(i, context)) { |
| 892 | if (context.symbol) { |
| 893 | Symbol *actual_symbol = |
| 894 | context.symbol->ResolveReExportedSymbol(*target_sp.get()); |
| 895 | if (actual_symbol) { |
| 896 | const Address actual_symbol_addr = |
| 897 | actual_symbol->GetAddress(); |
| 898 | if (actual_symbol_addr.IsValid()) { |
| 899 | addresses.push_back(actual_symbol_addr); |
| 900 | if (log) { |
| 901 | lldb::addr_t load_addr = |
| 902 | actual_symbol_addr.GetLoadAddress(target_sp.get()); |
| 903 | log->Printf( |
| 904 | "Found a re-exported symbol: %s at 0x%" PRIx64 ".", |
| 905 | actual_symbol->GetName().GetCString(), load_addr); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 906 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 907 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 908 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 909 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 910 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 911 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 912 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 913 | |
| 914 | SymbolContextList indirect_symbols; |
| 915 | images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver, |
| 916 | indirect_symbols); |
| 917 | size_t num_indirect_symbols = indirect_symbols.GetSize(); |
| 918 | if (num_indirect_symbols > 0) { |
| 919 | for (uint32_t i = 0; i < num_indirect_symbols; i++) { |
| 920 | SymbolContext context; |
| 921 | AddressRange addr_range; |
| 922 | if (indirect_symbols.GetContextAtIndex(i, context)) { |
| 923 | context.GetAddressRange(eSymbolContextEverything, 0, false, |
| 924 | addr_range); |
| 925 | addresses.push_back(addr_range.GetBaseAddress()); |
| 926 | if (log) { |
| 927 | addr_t load_addr = |
| 928 | addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); |
| 929 | |
| 930 | log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".", |
| 931 | load_addr); |
| 932 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 933 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 934 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 935 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 936 | } |
| 937 | } else if (current_symbol->GetType() == eSymbolTypeReExported) { |
| 938 | // I am not sure we could ever end up stopped AT a re-exported symbol. |
| 939 | // But just in case: |
| 940 | |
| 941 | const Symbol *actual_symbol = |
| 942 | current_symbol->ResolveReExportedSymbol(*(target_sp.get())); |
| 943 | if (actual_symbol) { |
| 944 | Address target_addr(actual_symbol->GetAddress()); |
| 945 | if (target_addr.IsValid()) { |
| 946 | if (log) |
| 947 | log->Printf( |
| 948 | "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64 |
| 949 | ".", |
| 950 | current_symbol->GetName().GetCString(), |
| 951 | actual_symbol->GetName().GetCString(), |
| 952 | target_addr.GetLoadAddress(target_sp.get())); |
| 953 | addresses.push_back(target_addr.GetLoadAddress(target_sp.get())); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 954 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 955 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 958 | if (addresses.size() > 0) { |
| 959 | // First check whether any of the addresses point to Indirect symbols, and |
| 960 | // if they do, resolve them: |
| 961 | std::vector<lldb::addr_t> load_addrs; |
| 962 | for (Address address : addresses) { |
| 963 | Symbol *symbol = address.CalculateSymbolContextSymbol(); |
| 964 | if (symbol && symbol->IsIndirect()) { |
| 965 | Error error; |
| 966 | Address symbol_address = symbol->GetAddress(); |
| 967 | addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction( |
| 968 | &symbol_address, error); |
| 969 | if (error.Success()) { |
| 970 | load_addrs.push_back(resolved_addr); |
| 971 | if (log) |
| 972 | log->Printf("ResolveIndirectFunction found resolved target for " |
| 973 | "%s at 0x%" PRIx64 ".", |
| 974 | symbol->GetName().GetCString(), resolved_addr); |
| 975 | } |
| 976 | } else { |
| 977 | load_addrs.push_back(address.GetLoadAddress(target_sp.get())); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 978 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 979 | } |
| 980 | thread_plan_sp.reset( |
| 981 | new ThreadPlanRunToAddress(thread, load_addrs, stop_others)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 982 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 983 | } else { |
| 984 | if (log) |
| 985 | log->Printf("Could not find symbol for step through."); |
| 986 | } |
| 987 | |
| 988 | return thread_plan_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | size_t DynamicLoaderDarwin::FindEquivalentSymbols( |
| 992 | lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images, |
| 993 | lldb_private::SymbolContextList &equivalent_symbols) { |
| 994 | const ConstString &trampoline_name = original_symbol->GetMangled().GetName( |
| 995 | original_symbol->GetLanguage(), Mangled::ePreferMangled); |
| 996 | if (!trampoline_name) |
| 997 | return 0; |
| 998 | |
| 999 | size_t initial_size = equivalent_symbols.GetSize(); |
| 1000 | |
| 1001 | static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$"; |
| 1002 | std::string equivalent_regex_buf("^"); |
| 1003 | equivalent_regex_buf.append(trampoline_name.GetCString()); |
| 1004 | equivalent_regex_buf.append(resolver_name_regex); |
| 1005 | |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 1006 | RegularExpression equivalent_name_regex(equivalent_regex_buf); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | const bool append = true; |
| 1008 | images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode, |
| 1009 | equivalent_symbols, append); |
| 1010 | |
| 1011 | return equivalent_symbols.GetSize() - initial_size; |
| 1012 | } |
| 1013 | |
| 1014 | lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() { |
| 1015 | ModuleSP module_sp = m_libpthread_module_wp.lock(); |
| 1016 | if (!module_sp) { |
| 1017 | SymbolContextList sc_list; |
| 1018 | ModuleSpec module_spec; |
| 1019 | module_spec.GetFileSpec().GetFilename().SetCString( |
| 1020 | "libsystem_pthread.dylib"); |
| 1021 | ModuleList module_list; |
| 1022 | if (m_process->GetTarget().GetImages().FindModules(module_spec, |
| 1023 | module_list)) { |
| 1024 | if (module_list.GetSize() == 1) { |
| 1025 | module_sp = module_list.GetModuleAtIndex(0); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1026 | if (module_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1027 | m_libpthread_module_wp = module_sp; |
| 1028 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1029 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1030 | } |
| 1031 | return module_sp; |
| 1032 | } |
| 1033 | |
| 1034 | Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() { |
| 1035 | if (!m_pthread_getspecific_addr.IsValid()) { |
| 1036 | ModuleSP module_sp = GetPThreadLibraryModule(); |
| 1037 | if (module_sp) { |
| 1038 | lldb_private::SymbolContextList sc_list; |
| 1039 | module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"), |
| 1040 | eSymbolTypeCode, sc_list); |
| 1041 | SymbolContext sc; |
| 1042 | if (sc_list.GetContextAtIndex(0, sc)) { |
| 1043 | if (sc.symbol) |
| 1044 | m_pthread_getspecific_addr = sc.symbol->GetAddress(); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | return m_pthread_getspecific_addr; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | lldb::addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1052 | DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp, |
| 1053 | const lldb::ThreadSP thread_sp, |
| 1054 | lldb::addr_t tls_file_addr) { |
| 1055 | if (!thread_sp || !module_sp) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1056 | return LLDB_INVALID_ADDRESS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | |
| 1058 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1059 | |
| 1060 | const uint32_t addr_size = m_process->GetAddressByteSize(); |
| 1061 | uint8_t buf[sizeof(lldb::addr_t) * 3]; |
| 1062 | |
| 1063 | lldb_private::Address tls_addr; |
| 1064 | if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) { |
| 1065 | Error error; |
| 1066 | const size_t tsl_data_size = addr_size * 3; |
| 1067 | Target &target = m_process->GetTarget(); |
| 1068 | if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) == |
| 1069 | tsl_data_size) { |
| 1070 | const ByteOrder byte_order = m_process->GetByteOrder(); |
| 1071 | DataExtractor data(buf, sizeof(buf), byte_order, addr_size); |
| 1072 | lldb::offset_t offset = addr_size; // Skip the first pointer |
| 1073 | const lldb::addr_t pthread_key = data.GetAddress(&offset); |
| 1074 | const lldb::addr_t tls_offset = data.GetAddress(&offset); |
| 1075 | if (pthread_key != 0) { |
| 1076 | // First check to see if we have already figured out the location |
| 1077 | // of TLS data for the pthread_key on a specific thread yet. If we |
| 1078 | // have we can re-use it since its location will not change unless |
| 1079 | // the process execs. |
| 1080 | const tid_t tid = thread_sp->GetID(); |
| 1081 | auto tid_pos = m_tid_to_tls_map.find(tid); |
| 1082 | if (tid_pos != m_tid_to_tls_map.end()) { |
| 1083 | auto tls_pos = tid_pos->second.find(pthread_key); |
| 1084 | if (tls_pos != tid_pos->second.end()) { |
| 1085 | return tls_pos->second + tls_offset; |
| 1086 | } |
| 1087 | } |
| 1088 | StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0); |
| 1089 | if (frame_sp) { |
| 1090 | ClangASTContext *clang_ast_context = |
| 1091 | target.GetScratchClangASTContext(); |
| 1092 | |
| 1093 | if (!clang_ast_context) |
| 1094 | return LLDB_INVALID_ADDRESS; |
| 1095 | |
| 1096 | CompilerType clang_void_ptr_type = |
| 1097 | clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); |
| 1098 | Address pthread_getspecific_addr = GetPthreadSetSpecificAddress(); |
| 1099 | if (pthread_getspecific_addr.IsValid()) { |
| 1100 | EvaluateExpressionOptions options; |
| 1101 | |
| 1102 | lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction( |
| 1103 | *thread_sp, pthread_getspecific_addr, clang_void_ptr_type, |
| 1104 | llvm::ArrayRef<lldb::addr_t>(pthread_key), options)); |
| 1105 | |
| 1106 | DiagnosticManager execution_errors; |
| 1107 | ExecutionContext exe_ctx(thread_sp); |
| 1108 | lldb::ExpressionResults results = m_process->RunThreadPlan( |
| 1109 | exe_ctx, thread_plan_sp, options, execution_errors); |
| 1110 | |
| 1111 | if (results == lldb::eExpressionCompleted) { |
| 1112 | lldb::ValueObjectSP result_valobj_sp = |
| 1113 | thread_plan_sp->GetReturnValueObject(); |
| 1114 | if (result_valobj_sp) { |
| 1115 | const lldb::addr_t pthread_key_data = |
| 1116 | result_valobj_sp->GetValueAsUnsigned(0); |
| 1117 | if (pthread_key_data) { |
| 1118 | m_tid_to_tls_map[tid].insert( |
| 1119 | std::make_pair(pthread_key, pthread_key_data)); |
| 1120 | return pthread_key_data + tls_offset; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | return LLDB_INVALID_ADDRESS; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) { |
| 1133 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 1134 | uint32_t major, minor, update; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1136 | bool use_new_spi_interface = false; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1138 | if (process->GetHostOSVersion(major, minor, update)) { |
| 1139 | const llvm::Triple::OSType os_type = |
| 1140 | process->GetTarget().GetArchitecture().GetTriple().getOS(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1142 | // macOS 10.12 and newer |
| 1143 | if (os_type == llvm::Triple::MacOSX && |
Dawn Perchik | 49df389 | 2016-11-02 20:57:34 +0000 | [diff] [blame] | 1144 | (major > 10 || (major == 10 && minor >= 12))) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1145 | use_new_spi_interface = true; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1148 | // iOS 10 and newer |
| 1149 | if (os_type == llvm::Triple::IOS && major >= 10) { |
| 1150 | use_new_spi_interface = true; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1151 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1152 | |
| 1153 | // tvOS 10 and newer |
| 1154 | if (os_type == llvm::Triple::TvOS && major >= 10) { |
| 1155 | use_new_spi_interface = true; |
| 1156 | } |
| 1157 | |
| 1158 | // watchOS 3 and newer |
| 1159 | if (os_type == llvm::Triple::WatchOS && major >= 3) { |
| 1160 | use_new_spi_interface = true; |
| 1161 | } |
| 1162 | } |
| 1163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1164 | if (log) { |
| 1165 | if (use_new_spi_interface) |
| 1166 | log->Printf( |
| 1167 | "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin"); |
| 1168 | else |
| 1169 | log->Printf( |
| 1170 | "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin"); |
| 1171 | } |
| 1172 | return use_new_spi_interface; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1173 | } |