Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderMacOSXKernel.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/DataBuffer.h" |
| 12 | #include "lldb/Core/DataBufferHeap.h" |
Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Log.h" |
| 15 | #include "lldb/Core/Module.h" |
| 16 | #include "lldb/Core/PluginManager.h" |
| 17 | #include "lldb/Core/State.h" |
| 18 | #include "lldb/Symbol/ObjectFile.h" |
| 19 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 20 | #include "lldb/Target/RegisterContext.h" |
| 21 | #include "lldb/Target/Target.h" |
| 22 | #include "lldb/Target/Thread.h" |
| 23 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
| 24 | #include "lldb/Target/StackFrame.h" |
| 25 | |
| 26 | #include "DynamicLoaderMacOSXKernel.h" |
| 27 | |
| 28 | //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN |
| 29 | #ifdef ENABLE_DEBUG_PRINTF |
| 30 | #include <stdio.h> |
| 31 | #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) |
| 32 | #else |
| 33 | #define DEBUG_PRINTF(fmt, ...) |
| 34 | #endif |
| 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
| 39 | /// FIXME - The ObjC Runtime trampoline handler doesn't really belong here. |
| 40 | /// I am putting it here so I can invoke it in the Trampoline code here, but |
| 41 | /// it should be moved to the ObjC Runtime support when it is set up. |
| 42 | |
| 43 | |
| 44 | //---------------------------------------------------------------------- |
| 45 | // Create an instance of this class. This function is filled into |
| 46 | // the plugin info class that gets handed out by the plugin factory and |
| 47 | // allows the lldb to instantiate an instance of this class. |
| 48 | //---------------------------------------------------------------------- |
| 49 | DynamicLoader * |
| 50 | DynamicLoaderMacOSXKernel::CreateInstance (Process* process, bool force) |
| 51 | { |
| 52 | bool create = force; |
| 53 | if (!create) |
| 54 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 55 | Module* exe_module = process->GetTarget().GetExecutableModule().get(); |
| 56 | if (exe_module) |
| 57 | { |
| 58 | ObjectFile *object_file = exe_module->GetObjectFile(); |
| 59 | if (object_file) |
| 60 | { |
| 61 | SectionList *section_list = object_file->GetSectionList(); |
| 62 | if (section_list) |
| 63 | { |
| 64 | static ConstString g_kld_section_name ("__KLD"); |
| 65 | if (section_list->FindSectionByName (g_kld_section_name)) |
| 66 | { |
| 67 | create = true; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (create) |
| 74 | { |
| 75 | const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); |
| 76 | create = triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple; |
| 77 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (create) |
| 81 | return new DynamicLoaderMacOSXKernel (process); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | //---------------------------------------------------------------------- |
| 86 | // Constructor |
| 87 | //---------------------------------------------------------------------- |
| 88 | DynamicLoaderMacOSXKernel::DynamicLoaderMacOSXKernel (Process* process) : |
| 89 | DynamicLoader(process), |
| 90 | m_kernel(), |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 91 | m_kext_summary_header_ptr_addr (), |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 92 | m_kext_summary_header_addr (), |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 93 | m_kext_summary_header (), |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 94 | m_break_id (LLDB_INVALID_BREAK_ID), |
| 95 | m_kext_summaries(), |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 96 | m_mutex(Mutex::eMutexTypeRecursive) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
| 100 | //---------------------------------------------------------------------- |
| 101 | // Destructor |
| 102 | //---------------------------------------------------------------------- |
| 103 | DynamicLoaderMacOSXKernel::~DynamicLoaderMacOSXKernel() |
| 104 | { |
| 105 | Clear(true); |
| 106 | } |
| 107 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 108 | void |
| 109 | DynamicLoaderMacOSXKernel::UpdateIfNeeded() |
| 110 | { |
| 111 | LoadKernelModuleIfNeeded(); |
| 112 | SetNotificationBreakpointIfNeeded (); |
| 113 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 114 | //------------------------------------------------------------------ |
| 115 | /// Called after attaching a process. |
| 116 | /// |
| 117 | /// Allow DynamicLoader plug-ins to execute some code after |
| 118 | /// attaching to a process. |
| 119 | //------------------------------------------------------------------ |
| 120 | void |
| 121 | DynamicLoaderMacOSXKernel::DidAttach () |
| 122 | { |
| 123 | PrivateInitialize(m_process); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 124 | UpdateIfNeeded(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | //------------------------------------------------------------------ |
| 128 | /// Called after attaching a process. |
| 129 | /// |
| 130 | /// Allow DynamicLoader plug-ins to execute some code after |
| 131 | /// attaching to a process. |
| 132 | //------------------------------------------------------------------ |
| 133 | void |
| 134 | DynamicLoaderMacOSXKernel::DidLaunch () |
| 135 | { |
| 136 | PrivateInitialize(m_process); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 137 | UpdateIfNeeded(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | //---------------------------------------------------------------------- |
| 142 | // Clear out the state of this class. |
| 143 | //---------------------------------------------------------------------- |
| 144 | void |
| 145 | DynamicLoaderMacOSXKernel::Clear (bool clear_process) |
| 146 | { |
| 147 | Mutex::Locker locker(m_mutex); |
| 148 | |
| 149 | if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id)) |
| 150 | m_process->ClearBreakpointSiteByID(m_break_id); |
| 151 | |
| 152 | if (clear_process) |
| 153 | m_process = NULL; |
| 154 | m_kernel.Clear(false); |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 155 | m_kext_summary_header_ptr_addr.Clear(); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 156 | m_kext_summary_header_addr.Clear(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 157 | m_kext_summaries.clear(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 158 | m_break_id = LLDB_INVALID_BREAK_ID; |
| 159 | } |
| 160 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 161 | |
| 162 | //---------------------------------------------------------------------- |
| 163 | // Load the kernel module and initialize the "m_kernel" member. Return |
| 164 | // true _only_ if the kernel is loaded the first time through (subsequent |
| 165 | // calls to this function should return false after the kernel has been |
| 166 | // already loaded). |
| 167 | //---------------------------------------------------------------------- |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 168 | void |
| 169 | DynamicLoaderMacOSXKernel::LoadKernelModuleIfNeeded() |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 170 | { |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 171 | if (!m_kext_summary_header_ptr_addr.IsValid()) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 172 | { |
| 173 | m_kernel.Clear(false); |
| 174 | m_kernel.module_sp = m_process->GetTarget().GetExecutableModule(); |
| 175 | if (m_kernel.module_sp) |
| 176 | { |
| 177 | static ConstString mach_header_name ("_mh_execute_header"); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 178 | static ConstString kext_summary_symbol ("gLoadedKextSummaries"); |
| 179 | const Symbol *symbol = NULL; |
| 180 | symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData); |
| 181 | if (symbol) |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 182 | m_kext_summary_header_ptr_addr = symbol->GetValue(); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 183 | |
| 184 | symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (mach_header_name, eSymbolTypeAbsolute); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 185 | if (symbol) |
| 186 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 187 | // The "_mh_execute_header" symbol is absolute and not a section based |
| 188 | // symbol that will have a valid address, so we need to resolve it... |
| 189 | m_process->GetTarget().GetImages().ResolveFileAddress (symbol->GetValue().GetFileAddress(), m_kernel.so_address); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 190 | DataExtractor data; // Load command data |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 191 | if (ReadMachHeader (m_kernel, &data)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 192 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 193 | if (m_kernel.header.filetype == llvm::MachO::HeaderFileTypeExecutable) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 194 | { |
| 195 | if (ParseLoadCommands (data, m_kernel)) |
| 196 | UpdateImageLoadAddress (m_kernel); |
| 197 | |
| 198 | // Update all image infos |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 199 | ReadAllKextSummaries (); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | m_kernel.Clear(false); |
| 205 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool |
| 212 | DynamicLoaderMacOSXKernel::FindTargetModule (OSKextLoadedKextSummary &image_info, bool can_create, bool *did_create_ptr) |
| 213 | { |
| 214 | if (did_create_ptr) |
| 215 | *did_create_ptr = false; |
| 216 | |
| 217 | const bool image_info_uuid_is_valid = image_info.uuid.IsValid(); |
| 218 | |
| 219 | if (image_info.module_sp) |
| 220 | { |
| 221 | if (image_info_uuid_is_valid) |
| 222 | { |
| 223 | if (image_info.module_sp->GetUUID() == image_info.uuid) |
| 224 | return true; |
| 225 | else |
| 226 | image_info.module_sp.reset(); |
| 227 | } |
| 228 | else |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | ModuleList &target_images = m_process->GetTarget().GetImages(); |
| 233 | if (image_info_uuid_is_valid) |
| 234 | image_info.module_sp = target_images.FindModule(image_info.uuid); |
| 235 | |
| 236 | if (image_info.module_sp) |
| 237 | return true; |
| 238 | |
| 239 | ArchSpec arch (image_info.GetArchitecture ()); |
| 240 | if (can_create) |
| 241 | { |
| 242 | if (image_info_uuid_is_valid) |
| 243 | { |
| 244 | image_info.module_sp = m_process->GetTarget().GetSharedModule (FileSpec(), |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 245 | arch, |
| 246 | &image_info.uuid); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 247 | if (did_create_ptr) |
| 248 | *did_create_ptr = image_info.module_sp; |
| 249 | } |
| 250 | } |
| 251 | return image_info.module_sp; |
| 252 | } |
| 253 | |
| 254 | bool |
| 255 | DynamicLoaderMacOSXKernel::UpdateCommPageLoadAddress(Module *module) |
| 256 | { |
| 257 | bool changed = false; |
| 258 | if (module) |
| 259 | { |
| 260 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 261 | if (image_object_file) |
| 262 | { |
| 263 | SectionList *section_list = image_object_file->GetSectionList (); |
| 264 | if (section_list) |
| 265 | { |
| 266 | uint32_t num_sections = section_list->GetSize(); |
| 267 | for (uint32_t i=0; i<num_sections; ++i) |
| 268 | { |
| 269 | Section* section = section_list->GetSectionAtIndex (i).get(); |
| 270 | if (section) |
| 271 | { |
| 272 | const addr_t new_section_load_addr = section->GetFileAddress (); |
| 273 | const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section); |
| 274 | if (old_section_load_addr == LLDB_INVALID_ADDRESS || |
| 275 | old_section_load_addr != new_section_load_addr) |
| 276 | { |
| 277 | if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress ())) |
| 278 | changed = true; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return changed; |
| 286 | } |
| 287 | |
| 288 | //---------------------------------------------------------------------- |
| 289 | // Update the load addresses for all segments in MODULE using the |
| 290 | // updated INFO that is passed in. |
| 291 | //---------------------------------------------------------------------- |
| 292 | bool |
| 293 | DynamicLoaderMacOSXKernel::UpdateImageLoadAddress (OSKextLoadedKextSummary& info) |
| 294 | { |
| 295 | Module *module = info.module_sp.get(); |
| 296 | bool changed = false; |
| 297 | if (module) |
| 298 | { |
| 299 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 300 | if (image_object_file) |
| 301 | { |
| 302 | SectionList *section_list = image_object_file->GetSectionList (); |
| 303 | if (section_list) |
| 304 | { |
| 305 | // We now know the slide amount, so go through all sections |
| 306 | // and update the load addresses with the correct values. |
| 307 | uint32_t num_segments = info.segments.size(); |
| 308 | for (uint32_t i=0; i<num_segments; ++i) |
| 309 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 310 | const addr_t new_section_load_addr = info.segments[i].vmaddr; |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 311 | if (section_list->FindSectionByName(info.segments[i].name)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 312 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 313 | SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name)); |
| 314 | if (section_sp) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 315 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 316 | const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section_sp.get()); |
| 317 | if (old_section_load_addr == LLDB_INVALID_ADDRESS || |
| 318 | old_section_load_addr != new_section_load_addr) |
| 319 | { |
| 320 | if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), new_section_load_addr)) |
| 321 | changed = true; |
| 322 | } |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | fprintf (stderr, |
| 327 | "warning: unable to find and load segment named '%s' at 0x%llx in '%s/%s' in macosx dynamic loader plug-in.\n", |
| 328 | info.segments[i].name.AsCString("<invalid>"), |
| 329 | (uint64_t)new_section_load_addr, |
| 330 | image_object_file->GetFileSpec().GetDirectory().AsCString(), |
| 331 | image_object_file->GetFileSpec().GetFilename().AsCString()); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | else |
| 335 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 336 | // The segment name is empty which means this is a .o file. |
| 337 | // Object files in LLDB end up getting reorganized so that |
| 338 | // the segment name that is in the section is promoted into |
| 339 | // an actual segment, so we just need to go through all sections |
| 340 | // and slide them by a single amount. |
| 341 | |
| 342 | uint32_t num_sections = section_list->GetSize(); |
| 343 | for (uint32_t i=0; i<num_sections; ++i) |
| 344 | { |
| 345 | Section* section = section_list->GetSectionAtIndex (i).get(); |
| 346 | if (section) |
| 347 | { |
| 348 | if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + new_section_load_addr)) |
| 349 | changed = true; |
| 350 | } |
| 351 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | return changed; |
| 358 | } |
| 359 | |
| 360 | //---------------------------------------------------------------------- |
| 361 | // Update the load addresses for all segments in MODULE using the |
| 362 | // updated INFO that is passed in. |
| 363 | //---------------------------------------------------------------------- |
| 364 | bool |
| 365 | DynamicLoaderMacOSXKernel::UnloadImageLoadAddress (OSKextLoadedKextSummary& info) |
| 366 | { |
| 367 | Module *module = info.module_sp.get(); |
| 368 | bool changed = false; |
| 369 | if (module) |
| 370 | { |
| 371 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 372 | if (image_object_file) |
| 373 | { |
| 374 | SectionList *section_list = image_object_file->GetSectionList (); |
| 375 | if (section_list) |
| 376 | { |
| 377 | uint32_t num_segments = info.segments.size(); |
| 378 | for (uint32_t i=0; i<num_segments; ++i) |
| 379 | { |
| 380 | SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name)); |
| 381 | if (section_sp) |
| 382 | { |
| 383 | const addr_t old_section_load_addr = info.segments[i].vmaddr; |
| 384 | if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr)) |
| 385 | changed = true; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | fprintf (stderr, |
| 390 | "warning: unable to find and unload segment named '%s' in '%s/%s' in macosx dynamic loader plug-in.\n", |
| 391 | info.segments[i].name.AsCString("<invalid>"), |
| 392 | image_object_file->GetFileSpec().GetDirectory().AsCString(), |
| 393 | image_object_file->GetFileSpec().GetFilename().AsCString()); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | return changed; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | //---------------------------------------------------------------------- |
| 404 | // Static callback function that gets called when our DYLD notification |
| 405 | // breakpoint gets hit. We update all of our image infos and then |
| 406 | // let our super class DynamicLoader class decide if we should stop |
| 407 | // or not (based on global preference). |
| 408 | //---------------------------------------------------------------------- |
| 409 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 410 | DynamicLoaderMacOSXKernel::BreakpointHitCallback (void *baton, |
| 411 | StoppointCallbackContext *context, |
| 412 | user_id_t break_id, |
| 413 | user_id_t break_loc_id) |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 414 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 415 | return static_cast<DynamicLoaderMacOSXKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 419 | DynamicLoaderMacOSXKernel::BreakpointHit (StoppointCallbackContext *context, |
| 420 | user_id_t break_id, |
| 421 | user_id_t break_loc_id) |
| 422 | { |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 423 | LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 424 | if (log) |
| 425 | log->Printf ("DynamicLoaderMacOSXKernel::BreakpointHit (...)\n"); |
| 426 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 427 | ReadAllKextSummaries (); |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 428 | |
| 429 | if (log) |
| 430 | PutToLog(log.get()); |
| 431 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 432 | return GetStopWhenImagesChange(); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | bool |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 437 | DynamicLoaderMacOSXKernel::ReadKextSummaryHeader () |
| 438 | { |
| 439 | Mutex::Locker locker(m_mutex); |
| 440 | |
| 441 | // the all image infos is already valid for this process stop ID |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 442 | |
| 443 | m_kext_summaries.clear(); |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 444 | if (m_kext_summary_header_ptr_addr.IsValid()) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 445 | { |
| 446 | const uint32_t addr_size = m_kernel.GetAddressByteSize (); |
| 447 | const ByteOrder byte_order = m_kernel.GetByteOrder(); |
| 448 | Error error; |
| 449 | // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure |
| 450 | // which is currenty 4 uint32_t and a pointer. |
| 451 | uint8_t buf[24]; |
| 452 | DataExtractor data (buf, sizeof(buf), byte_order, addr_size); |
| 453 | const size_t count = 4 * sizeof(uint32_t) + addr_size; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 454 | const bool prefer_file_cache = false; |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 455 | if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr, |
| 456 | prefer_file_cache, |
| 457 | error, |
| 458 | m_kext_summary_header_addr)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 459 | { |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 460 | // We got a valid address for our kext summary header and make sure it isn't NULL |
| 461 | if (m_kext_summary_header_addr.IsValid() && |
| 462 | m_kext_summary_header_addr.GetFileAddress() != 0) |
| 463 | { |
| 464 | const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error); |
| 465 | if (bytes_read == count) |
| 466 | { |
| 467 | uint32_t offset = 0; |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 468 | m_kext_summary_header.version = data.GetU32(&offset); |
| 469 | if (m_kext_summary_header.version >= 2) |
| 470 | { |
| 471 | m_kext_summary_header.entry_size = data.GetU32(&offset); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | // Versions less than 2 didn't have an entry size, it was hard coded |
| 476 | m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1; |
| 477 | } |
| 478 | m_kext_summary_header.entry_count = data.GetU32(&offset); |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 479 | return true; |
| 480 | } |
| 481 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 484 | m_kext_summary_header_addr.Clear(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 490 | DynamicLoaderMacOSXKernel::ParseKextSummaries (const Address &kext_summary_addr, |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 491 | uint32_t count) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 492 | { |
| 493 | OSKextLoadedKextSummary::collection kext_summaries; |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 494 | LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 495 | if (log) |
| 496 | log->Printf ("Adding %d modules.\n"); |
| 497 | |
| 498 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 499 | |
| 500 | if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries)) |
| 501 | return false; |
| 502 | |
Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 503 | Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 504 | for (uint32_t i = 0; i < count; i++) |
| 505 | { |
Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 506 | if (s) |
| 507 | { |
| 508 | const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes(); |
| 509 | if (u) |
| 510 | { |
| 511 | s->Printf("Loading kext: %2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X 0x%16.16llx \"%s\"...\n", |
| 512 | u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7], |
| 513 | u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15], |
| 514 | kext_summaries[i].address, kext_summaries[i].name); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | s->Printf("0x%16.16llx \"%s\"...\n", kext_summaries[i].address, kext_summaries[i].name); |
| 519 | } |
| 520 | } |
| 521 | |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 522 | DataExtractor data; // Load command data |
| 523 | if (ReadMachHeader (kext_summaries[i], &data)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 524 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 525 | ParseLoadCommands (data, kext_summaries[i]); |
| 526 | } |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 527 | |
Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 528 | if (s) |
| 529 | { |
| 530 | if (kext_summaries[i].module_sp) |
| 531 | s->Printf(" found kext: %s/%s\n", |
| 532 | kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(), |
| 533 | kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString()); |
| 534 | } |
| 535 | |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 536 | if (log) |
| 537 | kext_summaries[i].PutToLog (log.get()); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 538 | } |
| 539 | bool return_value = AddModulesUsingImageInfos (kext_summaries); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 540 | return return_value; |
| 541 | } |
| 542 | |
| 543 | // Adds the modules in image_infos to m_kext_summaries. |
| 544 | // NB don't call this passing in m_kext_summaries. |
| 545 | |
| 546 | bool |
| 547 | DynamicLoaderMacOSXKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos) |
| 548 | { |
| 549 | // Now add these images to the main list. |
| 550 | ModuleList loaded_module_list; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 551 | |
| 552 | for (uint32_t idx = 0; idx < image_infos.size(); ++idx) |
| 553 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 554 | m_kext_summaries.push_back(image_infos[idx]); |
| 555 | |
| 556 | if (FindTargetModule (image_infos[idx], true, NULL)) |
| 557 | { |
| 558 | // UpdateImageLoadAddress will return true if any segments |
| 559 | // change load address. We need to check this so we don't |
| 560 | // mention that all loaded shared libraries are newly loaded |
| 561 | // each time we hit out dyld breakpoint since dyld will list all |
| 562 | // shared libraries each time. |
| 563 | if (UpdateImageLoadAddress (image_infos[idx])) |
| 564 | { |
| 565 | loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (loaded_module_list.GetSize() > 0) |
| 571 | { |
| 572 | // FIXME: This should really be in the Runtime handlers class, which should get |
| 573 | // called by the target's ModulesDidLoad, but we're doing it all locally for now |
| 574 | // to save time. |
| 575 | // Also, I'm assuming there can be only one libobjc dylib loaded... |
| 576 | |
| 577 | ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime(); |
| 578 | if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary()) |
| 579 | { |
| 580 | size_t num_modules = loaded_module_list.GetSize(); |
| 581 | for (int i = 0; i < num_modules; i++) |
| 582 | { |
| 583 | if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i))) |
| 584 | { |
| 585 | objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i)); |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | } |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 590 | // if (log) |
| 591 | // loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderMacOSXKernel::ModulesDidLoad"); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 592 | m_process->GetTarget().ModulesDidLoad (loaded_module_list); |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 597 | |
| 598 | uint32_t |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 599 | DynamicLoaderMacOSXKernel::ReadKextSummaries (const Address &kext_summary_addr, |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 600 | uint32_t image_infos_count, |
| 601 | OSKextLoadedKextSummary::collection &image_infos) |
| 602 | { |
| 603 | const ByteOrder endian = m_kernel.GetByteOrder(); |
| 604 | const uint32_t addr_size = m_kernel.GetAddressByteSize(); |
| 605 | |
| 606 | image_infos.resize(image_infos_count); |
| 607 | const size_t count = image_infos.size() * m_kext_summary_header.entry_size; |
| 608 | DataBufferHeap data(count, 0); |
| 609 | Error error; |
Greg Clayton | 5b88216 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 610 | |
| 611 | Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream(); |
| 612 | |
| 613 | if (s) |
| 614 | s->Printf ("Reading %u kext summaries...\n", image_infos_count); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 615 | const bool prefer_file_cache = false; |
| 616 | const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr, |
| 617 | prefer_file_cache, |
| 618 | data.GetBytes(), |
| 619 | data.GetByteSize(), |
| 620 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 621 | if (bytes_read == count) |
| 622 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 623 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 624 | DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size); |
| 625 | uint32_t i=0; |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 626 | for (uint32_t kext_summary_offset = 0; |
| 627 | i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size); |
| 628 | ++i, kext_summary_offset += m_kext_summary_header.entry_size) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 629 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 630 | uint32_t offset = kext_summary_offset; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 631 | const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME); |
| 632 | if (name_data == NULL) |
| 633 | break; |
| 634 | memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME); |
| 635 | image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16)); |
| 636 | image_infos[i].address = extractor.GetU64(&offset); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 637 | if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget())) |
| 638 | m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 639 | image_infos[i].size = extractor.GetU64(&offset); |
| 640 | image_infos[i].version = extractor.GetU64(&offset); |
| 641 | image_infos[i].load_tag = extractor.GetU32(&offset); |
| 642 | image_infos[i].flags = extractor.GetU32(&offset); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 643 | if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size) |
| 644 | { |
| 645 | image_infos[i].reference_list = extractor.GetU64(&offset); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | image_infos[i].reference_list = 0; |
| 650 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 651 | } |
| 652 | if (i < image_infos.size()) |
| 653 | image_infos.resize(i); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | image_infos.clear(); |
| 658 | } |
| 659 | return image_infos.size(); |
| 660 | } |
| 661 | |
| 662 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 663 | DynamicLoaderMacOSXKernel::ReadAllKextSummaries () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 664 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 665 | LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 666 | |
| 667 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 668 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 669 | if (ReadKextSummaryHeader ()) |
| 670 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 671 | if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid()) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 672 | { |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 673 | Address summary_addr (m_kext_summary_header_addr); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 674 | summary_addr.Slide(m_kext_summary_header.GetSize()); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 675 | if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 676 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 677 | m_kext_summaries.clear(); |
| 678 | } |
| 679 | return true; |
| 680 | } |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | //---------------------------------------------------------------------- |
| 686 | // Read a mach_header at ADDR into HEADER, and also fill in the load |
| 687 | // command data into LOAD_COMMAND_DATA if it is non-NULL. |
| 688 | // |
| 689 | // Returns true if we succeed, false if we fail for any reason. |
| 690 | //---------------------------------------------------------------------- |
| 691 | bool |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 692 | DynamicLoaderMacOSXKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 693 | { |
| 694 | DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0); |
| 695 | Error error; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 696 | const bool prefer_file_cache = false; |
| 697 | size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address, |
| 698 | prefer_file_cache, |
| 699 | header_bytes.GetBytes(), |
| 700 | header_bytes.GetByteSize(), |
| 701 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 702 | if (bytes_read == sizeof(llvm::MachO::mach_header)) |
| 703 | { |
| 704 | uint32_t offset = 0; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 705 | ::memset (&kext_summary.header, 0, sizeof(kext_summary.header)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 706 | |
| 707 | // Get the magic byte unswapped so we can figure out what we are dealing with |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 708 | DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 709 | kext_summary.header.magic = data.GetU32(&offset); |
| 710 | Address load_cmd_addr = kext_summary.so_address; |
| 711 | data.SetByteOrder(DynamicLoaderMacOSXKernel::GetByteOrderFromMagic(kext_summary.header.magic)); |
| 712 | switch (kext_summary.header.magic) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 713 | { |
| 714 | case llvm::MachO::HeaderMagic32: |
| 715 | case llvm::MachO::HeaderMagic32Swapped: |
| 716 | data.SetAddressByteSize(4); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 717 | load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | case llvm::MachO::HeaderMagic64: |
| 721 | case llvm::MachO::HeaderMagic64Swapped: |
| 722 | data.SetAddressByteSize(8); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 723 | load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 724 | break; |
| 725 | |
| 726 | default: |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | // Read the rest of dyld's mach header |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 731 | if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 732 | { |
| 733 | if (load_command_data == NULL) |
| 734 | return true; // We were able to read the mach_header and weren't asked to read the load command bytes |
| 735 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 736 | DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 737 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 738 | size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr, |
| 739 | prefer_file_cache, |
| 740 | load_cmd_data_sp->GetBytes(), |
| 741 | load_cmd_data_sp->GetByteSize(), |
| 742 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 743 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 744 | if (load_cmd_bytes_read == kext_summary.header.sizeofcmds) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 745 | { |
| 746 | // Set the load command data and also set the correct endian |
| 747 | // swap settings and the correct address size |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 748 | load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 749 | load_command_data->SetByteOrder(data.GetByteOrder()); |
| 750 | load_command_data->SetAddressByteSize(data.GetAddressByteSize()); |
| 751 | return true; // We successfully read the mach_header and the load command data |
| 752 | } |
| 753 | |
| 754 | return false; // We weren't able to read the load command data |
| 755 | } |
| 756 | } |
| 757 | return false; // We failed the read the mach_header |
| 758 | } |
| 759 | |
| 760 | |
| 761 | //---------------------------------------------------------------------- |
| 762 | // Parse the load commands for an image |
| 763 | //---------------------------------------------------------------------- |
| 764 | uint32_t |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 765 | DynamicLoaderMacOSXKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 766 | { |
| 767 | uint32_t offset = 0; |
| 768 | uint32_t cmd_idx; |
| 769 | Segment segment; |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 770 | image_info.Clear (true); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 771 | |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 772 | for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 773 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 774 | // Clear out any load command specific data from image_info since |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 775 | // we are about to read it. |
| 776 | |
| 777 | if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command))) |
| 778 | { |
| 779 | llvm::MachO::load_command load_cmd; |
| 780 | uint32_t load_cmd_offset = offset; |
| 781 | load_cmd.cmd = data.GetU32 (&offset); |
| 782 | load_cmd.cmdsize = data.GetU32 (&offset); |
| 783 | switch (load_cmd.cmd) |
| 784 | { |
| 785 | case llvm::MachO::LoadCommandSegment32: |
| 786 | { |
| 787 | segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16); |
| 788 | // We are putting 4 uint32_t values 4 uint64_t values so |
| 789 | // we have to use multiple 32 bit gets below. |
| 790 | segment.vmaddr = data.GetU32 (&offset); |
| 791 | segment.vmsize = data.GetU32 (&offset); |
| 792 | segment.fileoff = data.GetU32 (&offset); |
| 793 | segment.filesize = data.GetU32 (&offset); |
| 794 | // Extract maxprot, initprot, nsects and flags all at once |
| 795 | data.GetU32(&offset, &segment.maxprot, 4); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 796 | image_info.segments.push_back (segment); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 797 | } |
| 798 | break; |
| 799 | |
| 800 | case llvm::MachO::LoadCommandSegment64: |
| 801 | { |
| 802 | segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16); |
| 803 | // Extract vmaddr, vmsize, fileoff, and filesize all at once |
| 804 | data.GetU64(&offset, &segment.vmaddr, 4); |
| 805 | // Extract maxprot, initprot, nsects and flags all at once |
| 806 | data.GetU32(&offset, &segment.maxprot, 4); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 807 | image_info.segments.push_back (segment); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 808 | } |
| 809 | break; |
| 810 | |
| 811 | case llvm::MachO::LoadCommandUUID: |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 812 | image_info.uuid.SetBytes(data.GetData (&offset, 16)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 813 | break; |
| 814 | |
| 815 | default: |
| 816 | break; |
| 817 | } |
| 818 | // Set offset to be the beginning of the next load command. |
| 819 | offset = load_cmd_offset + load_cmd.cmdsize; |
| 820 | } |
| 821 | } |
| 822 | #if 0 |
| 823 | // No slide in the kernel... |
| 824 | |
| 825 | // All sections listed in the dyld image info structure will all |
| 826 | // either be fixed up already, or they will all be off by a single |
| 827 | // slide amount that is determined by finding the first segment |
| 828 | // that is at file offset zero which also has bytes (a file size |
| 829 | // that is greater than zero) in the object file. |
| 830 | |
| 831 | // Determine the slide amount (if any) |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 832 | const size_t num_sections = image_info.segments.size(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 833 | for (size_t i = 0; i < num_sections; ++i) |
| 834 | { |
| 835 | // Iterate through the object file sections to find the |
| 836 | // first section that starts of file offset zero and that |
| 837 | // has bytes in the file... |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 838 | if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 839 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 840 | image_info.slide = image_info.address - image_info.segments[i].vmaddr; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 841 | // We have found the slide amount, so we can exit |
| 842 | // this for loop. |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | #endif |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 847 | if (image_info.uuid.IsValid()) |
| 848 | { |
| 849 | bool did_create = false; |
| 850 | if (FindTargetModule(image_info, true, &did_create)) |
| 851 | { |
| 852 | if (did_create) |
| 853 | image_info.module_create_stop_id = m_process->GetStopID(); |
| 854 | } |
| 855 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 856 | return cmd_idx; |
| 857 | } |
| 858 | |
| 859 | //---------------------------------------------------------------------- |
| 860 | // Dump a Segment to the file handle provided. |
| 861 | //---------------------------------------------------------------------- |
| 862 | void |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 863 | DynamicLoaderMacOSXKernel::Segment::PutToLog (Log *log, addr_t slide) const |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 864 | { |
| 865 | if (log) |
| 866 | { |
| 867 | if (slide == 0) |
| 868 | log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)", |
| 869 | name.AsCString(""), |
| 870 | vmaddr + slide, |
| 871 | vmaddr + slide + vmsize); |
| 872 | else |
| 873 | log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx", |
| 874 | name.AsCString(""), |
| 875 | vmaddr + slide, |
| 876 | vmaddr + slide + vmsize, |
| 877 | slide); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | const DynamicLoaderMacOSXKernel::Segment * |
| 882 | DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const |
| 883 | { |
| 884 | const size_t num_segments = segments.size(); |
| 885 | for (size_t i=0; i<num_segments; ++i) |
| 886 | { |
| 887 | if (segments[i].name == name) |
| 888 | return &segments[i]; |
| 889 | } |
| 890 | return NULL; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | //---------------------------------------------------------------------- |
| 895 | // Dump an image info structure to the file handle provided. |
| 896 | //---------------------------------------------------------------------- |
| 897 | void |
| 898 | DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const |
| 899 | { |
| 900 | if (log == NULL) |
| 901 | return; |
Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 902 | const uint8_t *u = (uint8_t *)uuid.GetBytes(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 903 | |
| 904 | if (address == LLDB_INVALID_ADDRESS) |
| 905 | { |
| 906 | if (u) |
| 907 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 908 | log->Printf("\tuuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\" (UNLOADED)", |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 909 | u[ 0], u[ 1], u[ 2], u[ 3], |
| 910 | u[ 4], u[ 5], u[ 6], u[ 7], |
| 911 | u[ 8], u[ 9], u[10], u[11], |
| 912 | u[12], u[13], u[14], u[15], |
| 913 | name); |
| 914 | } |
| 915 | else |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 916 | log->Printf("\tname=\"%s\" (UNLOADED)", name); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 917 | } |
| 918 | else |
| 919 | { |
| 920 | if (u) |
| 921 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 922 | log->Printf("\taddr=0x%16.16llx size=0x%16.16llx version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\"", |
| 923 | address, size, version, load_tag, flags, reference_list, |
| 924 | u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7], |
| 925 | u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15], |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 926 | name); |
| 927 | } |
| 928 | else |
| 929 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 930 | log->Printf("\t[0x%16.16llx - 0x%16.16llx) version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx name=\"%s\"", |
| 931 | address, address+size, version, load_tag, flags, reference_list, |
| 932 | name); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 933 | } |
| 934 | for (uint32_t i=0; i<segments.size(); ++i) |
| 935 | segments[i].PutToLog(log, 0); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | //---------------------------------------------------------------------- |
| 940 | // Dump the _dyld_all_image_infos members and all current image infos |
| 941 | // that we have parsed to the file handle provided. |
| 942 | //---------------------------------------------------------------------- |
| 943 | void |
| 944 | DynamicLoaderMacOSXKernel::PutToLog(Log *log) const |
| 945 | { |
| 946 | if (log == NULL) |
| 947 | return; |
| 948 | |
| 949 | Mutex::Locker locker(m_mutex); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 950 | log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }", |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 951 | m_kext_summary_header_addr.GetFileAddress(), |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 952 | m_kext_summary_header.version, |
| 953 | m_kext_summary_header.entry_size, |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 954 | m_kext_summary_header.entry_count); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 955 | |
| 956 | size_t i; |
| 957 | const size_t count = m_kext_summaries.size(); |
| 958 | if (count > 0) |
| 959 | { |
| 960 | log->PutCString("Loaded:"); |
| 961 | for (i = 0; i<count; i++) |
| 962 | m_kext_summaries[i].PutToLog(log); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | void |
| 967 | DynamicLoaderMacOSXKernel::PrivateInitialize(Process *process) |
| 968 | { |
| 969 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); |
| 970 | Clear(true); |
| 971 | m_process = process; |
| 972 | m_process->GetTarget().GetSectionLoadList().Clear(); |
| 973 | } |
| 974 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 975 | void |
| 976 | DynamicLoaderMacOSXKernel::SetNotificationBreakpointIfNeeded () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 977 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 978 | if (m_break_id == LLDB_INVALID_BREAK_ID) |
| 979 | { |
| 980 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); |
| 981 | |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 982 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 983 | const bool internal_bp = false; |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 984 | const LazyBool skip_prologue = eLazyBoolNo; |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 985 | Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&m_kernel.module_sp->GetFileSpec(), |
| 986 | "OSKextLoadedKextSummariesUpdated", |
| 987 | eFunctionNameTypeFull, |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 988 | internal_bp, |
| 989 | skip_prologue).get(); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 990 | |
| 991 | bp->SetCallback (DynamicLoaderMacOSXKernel::BreakpointHitCallback, this, true); |
| 992 | m_break_id = bp->GetID(); |
| 993 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | //---------------------------------------------------------------------- |
| 997 | // Member function that gets called when the process state changes. |
| 998 | //---------------------------------------------------------------------- |
| 999 | void |
| 1000 | DynamicLoaderMacOSXKernel::PrivateProcessStateChanged (Process *process, StateType state) |
| 1001 | { |
| 1002 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state)); |
| 1003 | switch (state) |
| 1004 | { |
| 1005 | case eStateConnected: |
| 1006 | case eStateAttaching: |
| 1007 | case eStateLaunching: |
| 1008 | case eStateInvalid: |
| 1009 | case eStateUnloaded: |
| 1010 | case eStateExited: |
| 1011 | case eStateDetached: |
| 1012 | Clear(false); |
| 1013 | break; |
| 1014 | |
| 1015 | case eStateStopped: |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 1016 | UpdateIfNeeded(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | |
| 1019 | case eStateRunning: |
| 1020 | case eStateStepping: |
| 1021 | case eStateCrashed: |
| 1022 | case eStateSuspended: |
| 1023 | break; |
| 1024 | |
| 1025 | default: |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | ThreadPlanSP |
| 1031 | DynamicLoaderMacOSXKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others) |
| 1032 | { |
| 1033 | ThreadPlanSP thread_plan_sp; |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 1034 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 1035 | if (log) |
| 1036 | log->Printf ("Could not find symbol for step through."); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 1037 | return thread_plan_sp; |
| 1038 | } |
| 1039 | |
| 1040 | Error |
| 1041 | DynamicLoaderMacOSXKernel::CanLoadImage () |
| 1042 | { |
| 1043 | Error error; |
| 1044 | error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel"); |
| 1045 | return error; |
| 1046 | } |
| 1047 | |
| 1048 | void |
| 1049 | DynamicLoaderMacOSXKernel::Initialize() |
| 1050 | { |
| 1051 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 1052 | GetPluginDescriptionStatic(), |
| 1053 | CreateInstance); |
| 1054 | } |
| 1055 | |
| 1056 | void |
| 1057 | DynamicLoaderMacOSXKernel::Terminate() |
| 1058 | { |
| 1059 | PluginManager::UnregisterPlugin (CreateInstance); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | const char * |
| 1064 | DynamicLoaderMacOSXKernel::GetPluginNameStatic() |
| 1065 | { |
| 1066 | return "dynamic-loader.macosx-kernel"; |
| 1067 | } |
| 1068 | |
| 1069 | const char * |
| 1070 | DynamicLoaderMacOSXKernel::GetPluginDescriptionStatic() |
| 1071 | { |
| 1072 | return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel."; |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | //------------------------------------------------------------------ |
| 1077 | // PluginInterface protocol |
| 1078 | //------------------------------------------------------------------ |
| 1079 | const char * |
| 1080 | DynamicLoaderMacOSXKernel::GetPluginName() |
| 1081 | { |
| 1082 | return "DynamicLoaderMacOSXKernel"; |
| 1083 | } |
| 1084 | |
| 1085 | const char * |
| 1086 | DynamicLoaderMacOSXKernel::GetShortPluginName() |
| 1087 | { |
| 1088 | return GetPluginNameStatic(); |
| 1089 | } |
| 1090 | |
| 1091 | uint32_t |
| 1092 | DynamicLoaderMacOSXKernel::GetPluginVersion() |
| 1093 | { |
| 1094 | return 1; |
| 1095 | } |
| 1096 | |