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 | |
| 528 | if (log) |
| 529 | kext_summaries[i].PutToLog (log.get()); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 530 | } |
| 531 | bool return_value = AddModulesUsingImageInfos (kext_summaries); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 532 | return return_value; |
| 533 | } |
| 534 | |
| 535 | // Adds the modules in image_infos to m_kext_summaries. |
| 536 | // NB don't call this passing in m_kext_summaries. |
| 537 | |
| 538 | bool |
| 539 | DynamicLoaderMacOSXKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos) |
| 540 | { |
| 541 | // Now add these images to the main list. |
| 542 | ModuleList loaded_module_list; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 543 | |
| 544 | for (uint32_t idx = 0; idx < image_infos.size(); ++idx) |
| 545 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 546 | m_kext_summaries.push_back(image_infos[idx]); |
| 547 | |
| 548 | if (FindTargetModule (image_infos[idx], true, NULL)) |
| 549 | { |
| 550 | // UpdateImageLoadAddress will return true if any segments |
| 551 | // change load address. We need to check this so we don't |
| 552 | // mention that all loaded shared libraries are newly loaded |
| 553 | // each time we hit out dyld breakpoint since dyld will list all |
| 554 | // shared libraries each time. |
| 555 | if (UpdateImageLoadAddress (image_infos[idx])) |
| 556 | { |
| 557 | loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if (loaded_module_list.GetSize() > 0) |
| 563 | { |
| 564 | // FIXME: This should really be in the Runtime handlers class, which should get |
| 565 | // called by the target's ModulesDidLoad, but we're doing it all locally for now |
| 566 | // to save time. |
| 567 | // Also, I'm assuming there can be only one libobjc dylib loaded... |
| 568 | |
| 569 | ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime(); |
| 570 | if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary()) |
| 571 | { |
| 572 | size_t num_modules = loaded_module_list.GetSize(); |
| 573 | for (int i = 0; i < num_modules; i++) |
| 574 | { |
| 575 | if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i))) |
| 576 | { |
| 577 | objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i)); |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | } |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 582 | // if (log) |
| 583 | // loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderMacOSXKernel::ModulesDidLoad"); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 584 | m_process->GetTarget().ModulesDidLoad (loaded_module_list); |
| 585 | } |
| 586 | return true; |
| 587 | } |
| 588 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 589 | |
| 590 | uint32_t |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 591 | DynamicLoaderMacOSXKernel::ReadKextSummaries (const Address &kext_summary_addr, |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 592 | uint32_t image_infos_count, |
| 593 | OSKextLoadedKextSummary::collection &image_infos) |
| 594 | { |
| 595 | const ByteOrder endian = m_kernel.GetByteOrder(); |
| 596 | const uint32_t addr_size = m_kernel.GetAddressByteSize(); |
| 597 | |
| 598 | image_infos.resize(image_infos_count); |
| 599 | const size_t count = image_infos.size() * m_kext_summary_header.entry_size; |
| 600 | DataBufferHeap data(count, 0); |
| 601 | Error error; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 602 | const bool prefer_file_cache = false; |
| 603 | const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr, |
| 604 | prefer_file_cache, |
| 605 | data.GetBytes(), |
| 606 | data.GetByteSize(), |
| 607 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 608 | if (bytes_read == count) |
| 609 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 610 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 611 | DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size); |
| 612 | uint32_t i=0; |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 613 | for (uint32_t kext_summary_offset = 0; |
| 614 | i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size); |
| 615 | ++i, kext_summary_offset += m_kext_summary_header.entry_size) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 616 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 617 | uint32_t offset = kext_summary_offset; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 618 | const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME); |
| 619 | if (name_data == NULL) |
| 620 | break; |
| 621 | memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME); |
| 622 | image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16)); |
| 623 | image_infos[i].address = extractor.GetU64(&offset); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 624 | if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget())) |
| 625 | 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] | 626 | image_infos[i].size = extractor.GetU64(&offset); |
| 627 | image_infos[i].version = extractor.GetU64(&offset); |
| 628 | image_infos[i].load_tag = extractor.GetU32(&offset); |
| 629 | image_infos[i].flags = extractor.GetU32(&offset); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 630 | if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size) |
| 631 | { |
| 632 | image_infos[i].reference_list = extractor.GetU64(&offset); |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | image_infos[i].reference_list = 0; |
| 637 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 638 | } |
| 639 | if (i < image_infos.size()) |
| 640 | image_infos.resize(i); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | image_infos.clear(); |
| 645 | } |
| 646 | return image_infos.size(); |
| 647 | } |
| 648 | |
| 649 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 650 | DynamicLoaderMacOSXKernel::ReadAllKextSummaries () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 651 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 652 | LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 653 | |
| 654 | Mutex::Locker locker(m_mutex); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 655 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 656 | if (ReadKextSummaryHeader ()) |
| 657 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 658 | 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] | 659 | { |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 660 | Address summary_addr (m_kext_summary_header_addr); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 661 | summary_addr.Slide(m_kext_summary_header.GetSize()); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 662 | if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count)) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 663 | { |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 664 | m_kext_summaries.clear(); |
| 665 | } |
| 666 | return true; |
| 667 | } |
| 668 | } |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | //---------------------------------------------------------------------- |
| 673 | // Read a mach_header at ADDR into HEADER, and also fill in the load |
| 674 | // command data into LOAD_COMMAND_DATA if it is non-NULL. |
| 675 | // |
| 676 | // Returns true if we succeed, false if we fail for any reason. |
| 677 | //---------------------------------------------------------------------- |
| 678 | bool |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 679 | DynamicLoaderMacOSXKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 680 | { |
| 681 | DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0); |
| 682 | Error error; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 683 | const bool prefer_file_cache = false; |
| 684 | size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address, |
| 685 | prefer_file_cache, |
| 686 | header_bytes.GetBytes(), |
| 687 | header_bytes.GetByteSize(), |
| 688 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 689 | if (bytes_read == sizeof(llvm::MachO::mach_header)) |
| 690 | { |
| 691 | uint32_t offset = 0; |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 692 | ::memset (&kext_summary.header, 0, sizeof(kext_summary.header)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 693 | |
| 694 | // 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] | 695 | DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 696 | kext_summary.header.magic = data.GetU32(&offset); |
| 697 | Address load_cmd_addr = kext_summary.so_address; |
| 698 | data.SetByteOrder(DynamicLoaderMacOSXKernel::GetByteOrderFromMagic(kext_summary.header.magic)); |
| 699 | switch (kext_summary.header.magic) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 700 | { |
| 701 | case llvm::MachO::HeaderMagic32: |
| 702 | case llvm::MachO::HeaderMagic32Swapped: |
| 703 | data.SetAddressByteSize(4); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 704 | load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | case llvm::MachO::HeaderMagic64: |
| 708 | case llvm::MachO::HeaderMagic64Swapped: |
| 709 | data.SetAddressByteSize(8); |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 710 | load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 711 | break; |
| 712 | |
| 713 | default: |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | // Read the rest of dyld's mach header |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 718 | 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] | 719 | { |
| 720 | if (load_command_data == NULL) |
| 721 | return true; // We were able to read the mach_header and weren't asked to read the load command bytes |
| 722 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 723 | DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 724 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 725 | size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr, |
| 726 | prefer_file_cache, |
| 727 | load_cmd_data_sp->GetBytes(), |
| 728 | load_cmd_data_sp->GetByteSize(), |
| 729 | error); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 730 | |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 731 | if (load_cmd_bytes_read == kext_summary.header.sizeofcmds) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 732 | { |
| 733 | // Set the load command data and also set the correct endian |
| 734 | // swap settings and the correct address size |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 735 | 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] | 736 | load_command_data->SetByteOrder(data.GetByteOrder()); |
| 737 | load_command_data->SetAddressByteSize(data.GetAddressByteSize()); |
| 738 | return true; // We successfully read the mach_header and the load command data |
| 739 | } |
| 740 | |
| 741 | return false; // We weren't able to read the load command data |
| 742 | } |
| 743 | } |
| 744 | return false; // We failed the read the mach_header |
| 745 | } |
| 746 | |
| 747 | |
| 748 | //---------------------------------------------------------------------- |
| 749 | // Parse the load commands for an image |
| 750 | //---------------------------------------------------------------------- |
| 751 | uint32_t |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 752 | DynamicLoaderMacOSXKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 753 | { |
| 754 | uint32_t offset = 0; |
| 755 | uint32_t cmd_idx; |
| 756 | Segment segment; |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 757 | image_info.Clear (true); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 758 | |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 759 | for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++) |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 760 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 761 | // Clear out any load command specific data from image_info since |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 762 | // we are about to read it. |
| 763 | |
| 764 | if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command))) |
| 765 | { |
| 766 | llvm::MachO::load_command load_cmd; |
| 767 | uint32_t load_cmd_offset = offset; |
| 768 | load_cmd.cmd = data.GetU32 (&offset); |
| 769 | load_cmd.cmdsize = data.GetU32 (&offset); |
| 770 | switch (load_cmd.cmd) |
| 771 | { |
| 772 | case llvm::MachO::LoadCommandSegment32: |
| 773 | { |
| 774 | segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16); |
| 775 | // We are putting 4 uint32_t values 4 uint64_t values so |
| 776 | // we have to use multiple 32 bit gets below. |
| 777 | segment.vmaddr = data.GetU32 (&offset); |
| 778 | segment.vmsize = data.GetU32 (&offset); |
| 779 | segment.fileoff = data.GetU32 (&offset); |
| 780 | segment.filesize = data.GetU32 (&offset); |
| 781 | // Extract maxprot, initprot, nsects and flags all at once |
| 782 | data.GetU32(&offset, &segment.maxprot, 4); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 783 | image_info.segments.push_back (segment); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 784 | } |
| 785 | break; |
| 786 | |
| 787 | case llvm::MachO::LoadCommandSegment64: |
| 788 | { |
| 789 | segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16); |
| 790 | // Extract vmaddr, vmsize, fileoff, and filesize all at once |
| 791 | data.GetU64(&offset, &segment.vmaddr, 4); |
| 792 | // Extract maxprot, initprot, nsects and flags all at once |
| 793 | data.GetU32(&offset, &segment.maxprot, 4); |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 794 | image_info.segments.push_back (segment); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 795 | } |
| 796 | break; |
| 797 | |
| 798 | case llvm::MachO::LoadCommandUUID: |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 799 | image_info.uuid.SetBytes(data.GetData (&offset, 16)); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 800 | break; |
| 801 | |
| 802 | default: |
| 803 | break; |
| 804 | } |
| 805 | // Set offset to be the beginning of the next load command. |
| 806 | offset = load_cmd_offset + load_cmd.cmdsize; |
| 807 | } |
| 808 | } |
| 809 | #if 0 |
| 810 | // No slide in the kernel... |
| 811 | |
| 812 | // All sections listed in the dyld image info structure will all |
| 813 | // either be fixed up already, or they will all be off by a single |
| 814 | // slide amount that is determined by finding the first segment |
| 815 | // that is at file offset zero which also has bytes (a file size |
| 816 | // that is greater than zero) in the object file. |
| 817 | |
| 818 | // Determine the slide amount (if any) |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 819 | const size_t num_sections = image_info.segments.size(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 820 | for (size_t i = 0; i < num_sections; ++i) |
| 821 | { |
| 822 | // Iterate through the object file sections to find the |
| 823 | // first section that starts of file offset zero and that |
| 824 | // has bytes in the file... |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 825 | 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] | 826 | { |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 827 | image_info.slide = image_info.address - image_info.segments[i].vmaddr; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 828 | // We have found the slide amount, so we can exit |
| 829 | // this for loop. |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | #endif |
Greg Clayton | df0b7d5 | 2011-07-08 04:11:42 +0000 | [diff] [blame] | 834 | if (image_info.uuid.IsValid()) |
| 835 | { |
| 836 | bool did_create = false; |
| 837 | if (FindTargetModule(image_info, true, &did_create)) |
| 838 | { |
| 839 | if (did_create) |
| 840 | image_info.module_create_stop_id = m_process->GetStopID(); |
| 841 | } |
| 842 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 843 | return cmd_idx; |
| 844 | } |
| 845 | |
| 846 | //---------------------------------------------------------------------- |
| 847 | // Dump a Segment to the file handle provided. |
| 848 | //---------------------------------------------------------------------- |
| 849 | void |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 850 | DynamicLoaderMacOSXKernel::Segment::PutToLog (Log *log, addr_t slide) const |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 851 | { |
| 852 | if (log) |
| 853 | { |
| 854 | if (slide == 0) |
| 855 | log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)", |
| 856 | name.AsCString(""), |
| 857 | vmaddr + slide, |
| 858 | vmaddr + slide + vmsize); |
| 859 | else |
| 860 | log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx", |
| 861 | name.AsCString(""), |
| 862 | vmaddr + slide, |
| 863 | vmaddr + slide + vmsize, |
| 864 | slide); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | const DynamicLoaderMacOSXKernel::Segment * |
| 869 | DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const |
| 870 | { |
| 871 | const size_t num_segments = segments.size(); |
| 872 | for (size_t i=0; i<num_segments; ++i) |
| 873 | { |
| 874 | if (segments[i].name == name) |
| 875 | return &segments[i]; |
| 876 | } |
| 877 | return NULL; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | //---------------------------------------------------------------------- |
| 882 | // Dump an image info structure to the file handle provided. |
| 883 | //---------------------------------------------------------------------- |
| 884 | void |
| 885 | DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const |
| 886 | { |
| 887 | if (log == NULL) |
| 888 | return; |
Greg Clayton | 07e66e3 | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 889 | const uint8_t *u = (uint8_t *)uuid.GetBytes(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 890 | |
| 891 | if (address == LLDB_INVALID_ADDRESS) |
| 892 | { |
| 893 | if (u) |
| 894 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 895 | 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] | 896 | u[ 0], u[ 1], u[ 2], u[ 3], |
| 897 | u[ 4], u[ 5], u[ 6], u[ 7], |
| 898 | u[ 8], u[ 9], u[10], u[11], |
| 899 | u[12], u[13], u[14], u[15], |
| 900 | name); |
| 901 | } |
| 902 | else |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 903 | log->Printf("\tname=\"%s\" (UNLOADED)", name); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 904 | } |
| 905 | else |
| 906 | { |
| 907 | if (u) |
| 908 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 909 | 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\"", |
| 910 | address, size, version, load_tag, flags, reference_list, |
| 911 | u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7], |
| 912 | 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] | 913 | name); |
| 914 | } |
| 915 | else |
| 916 | { |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 917 | 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\"", |
| 918 | address, address+size, version, load_tag, flags, reference_list, |
| 919 | name); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 920 | } |
| 921 | for (uint32_t i=0; i<segments.size(); ++i) |
| 922 | segments[i].PutToLog(log, 0); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | //---------------------------------------------------------------------- |
| 927 | // Dump the _dyld_all_image_infos members and all current image infos |
| 928 | // that we have parsed to the file handle provided. |
| 929 | //---------------------------------------------------------------------- |
| 930 | void |
| 931 | DynamicLoaderMacOSXKernel::PutToLog(Log *log) const |
| 932 | { |
| 933 | if (log == NULL) |
| 934 | return; |
| 935 | |
| 936 | Mutex::Locker locker(m_mutex); |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 937 | 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] | 938 | m_kext_summary_header_addr.GetFileAddress(), |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 939 | m_kext_summary_header.version, |
| 940 | m_kext_summary_header.entry_size, |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 941 | m_kext_summary_header.entry_count); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 942 | |
| 943 | size_t i; |
| 944 | const size_t count = m_kext_summaries.size(); |
| 945 | if (count > 0) |
| 946 | { |
| 947 | log->PutCString("Loaded:"); |
| 948 | for (i = 0; i<count; i++) |
| 949 | m_kext_summaries[i].PutToLog(log); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void |
| 954 | DynamicLoaderMacOSXKernel::PrivateInitialize(Process *process) |
| 955 | { |
| 956 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); |
| 957 | Clear(true); |
| 958 | m_process = process; |
| 959 | m_process->GetTarget().GetSectionLoadList().Clear(); |
| 960 | } |
| 961 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 962 | void |
| 963 | DynamicLoaderMacOSXKernel::SetNotificationBreakpointIfNeeded () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 964 | { |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 965 | if (m_break_id == LLDB_INVALID_BREAK_ID) |
| 966 | { |
| 967 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); |
| 968 | |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 969 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 970 | const bool internal_bp = false; |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 971 | const LazyBool skip_prologue = eLazyBoolNo; |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 972 | Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&m_kernel.module_sp->GetFileSpec(), |
| 973 | "OSKextLoadedKextSummariesUpdated", |
| 974 | eFunctionNameTypeFull, |
Greg Clayton | d16e1e5 | 2011-07-12 17:06:17 +0000 | [diff] [blame] | 975 | internal_bp, |
| 976 | skip_prologue).get(); |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 977 | |
| 978 | bp->SetCallback (DynamicLoaderMacOSXKernel::BreakpointHitCallback, this, true); |
| 979 | m_break_id = bp->GetID(); |
| 980 | } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | //---------------------------------------------------------------------- |
| 984 | // Member function that gets called when the process state changes. |
| 985 | //---------------------------------------------------------------------- |
| 986 | void |
| 987 | DynamicLoaderMacOSXKernel::PrivateProcessStateChanged (Process *process, StateType state) |
| 988 | { |
| 989 | DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state)); |
| 990 | switch (state) |
| 991 | { |
| 992 | case eStateConnected: |
| 993 | case eStateAttaching: |
| 994 | case eStateLaunching: |
| 995 | case eStateInvalid: |
| 996 | case eStateUnloaded: |
| 997 | case eStateExited: |
| 998 | case eStateDetached: |
| 999 | Clear(false); |
| 1000 | break; |
| 1001 | |
| 1002 | case eStateStopped: |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 1003 | UpdateIfNeeded(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | |
| 1006 | case eStateRunning: |
| 1007 | case eStateStepping: |
| 1008 | case eStateCrashed: |
| 1009 | case eStateSuspended: |
| 1010 | break; |
| 1011 | |
| 1012 | default: |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | ThreadPlanSP |
| 1018 | DynamicLoaderMacOSXKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others) |
| 1019 | { |
| 1020 | ThreadPlanSP thread_plan_sp; |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 1021 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 1022 | if (log) |
| 1023 | log->Printf ("Could not find symbol for step through."); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 1024 | return thread_plan_sp; |
| 1025 | } |
| 1026 | |
| 1027 | Error |
| 1028 | DynamicLoaderMacOSXKernel::CanLoadImage () |
| 1029 | { |
| 1030 | Error error; |
| 1031 | error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel"); |
| 1032 | return error; |
| 1033 | } |
| 1034 | |
| 1035 | void |
| 1036 | DynamicLoaderMacOSXKernel::Initialize() |
| 1037 | { |
| 1038 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 1039 | GetPluginDescriptionStatic(), |
| 1040 | CreateInstance); |
| 1041 | } |
| 1042 | |
| 1043 | void |
| 1044 | DynamicLoaderMacOSXKernel::Terminate() |
| 1045 | { |
| 1046 | PluginManager::UnregisterPlugin (CreateInstance); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | const char * |
| 1051 | DynamicLoaderMacOSXKernel::GetPluginNameStatic() |
| 1052 | { |
| 1053 | return "dynamic-loader.macosx-kernel"; |
| 1054 | } |
| 1055 | |
| 1056 | const char * |
| 1057 | DynamicLoaderMacOSXKernel::GetPluginDescriptionStatic() |
| 1058 | { |
| 1059 | return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel."; |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | //------------------------------------------------------------------ |
| 1064 | // PluginInterface protocol |
| 1065 | //------------------------------------------------------------------ |
| 1066 | const char * |
| 1067 | DynamicLoaderMacOSXKernel::GetPluginName() |
| 1068 | { |
| 1069 | return "DynamicLoaderMacOSXKernel"; |
| 1070 | } |
| 1071 | |
| 1072 | const char * |
| 1073 | DynamicLoaderMacOSXKernel::GetShortPluginName() |
| 1074 | { |
| 1075 | return GetPluginNameStatic(); |
| 1076 | } |
| 1077 | |
| 1078 | uint32_t |
| 1079 | DynamicLoaderMacOSXKernel::GetPluginVersion() |
| 1080 | { |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |