1 //===-- DynamicLoaderDarwinKernel.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/lldb-python.h" 11 12 #include "lldb/Breakpoint/StoppointCallbackContext.h" 13 #include "lldb/Core/DataBuffer.h" 14 #include "lldb/Core/DataBufferHeap.h" 15 #include "lldb/Core/Debugger.h" 16 #include "lldb/Core/Log.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/ModuleSpec.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Core/State.h" 22 #include "lldb/Host/Symbols.h" 23 #include "lldb/Symbol/ObjectFile.h" 24 #include "lldb/Target/RegisterContext.h" 25 #include "lldb/Target/StackFrame.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Target/Thread.h" 28 #include "lldb/Target/ThreadPlanRunToAddress.h" 29 30 31 #include "DynamicLoaderDarwinKernel.h" 32 33 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN 34 #ifdef ENABLE_DEBUG_PRINTF 35 #include <stdio.h> 36 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) 37 #else 38 #define DEBUG_PRINTF(fmt, ...) 39 #endif 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 static PropertyDefinition 45 g_properties[] = 46 { 47 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." }, 48 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 49 }; 50 51 enum { 52 ePropertyLoadKexts 53 }; 54 55 class DynamicLoaderDarwinKernelProperties : public Properties 56 { 57 public: 58 59 static ConstString & 60 GetSettingName () 61 { 62 static ConstString g_setting_name("darwin-kernel"); 63 return g_setting_name; 64 } 65 66 DynamicLoaderDarwinKernelProperties() : 67 Properties () 68 { 69 m_collection_sp.reset (new OptionValueProperties(GetSettingName())); 70 m_collection_sp->Initialize(g_properties); 71 } 72 73 virtual 74 ~DynamicLoaderDarwinKernelProperties() 75 { 76 } 77 78 bool 79 GetLoadKexts() const 80 { 81 const uint32_t idx = ePropertyLoadKexts; 82 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 83 } 84 85 }; 86 87 typedef STD_SHARED_PTR(DynamicLoaderDarwinKernelProperties) DynamicLoaderDarwinKernelPropertiesSP; 88 89 static const DynamicLoaderDarwinKernelPropertiesSP & 90 GetGlobalProperties() 91 { 92 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp; 93 if (!g_settings_sp) 94 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ()); 95 return g_settings_sp; 96 } 97 98 //---------------------------------------------------------------------- 99 // Create an instance of this class. This function is filled into 100 // the plugin info class that gets handed out by the plugin factory and 101 // allows the lldb to instantiate an instance of this class. 102 //---------------------------------------------------------------------- 103 DynamicLoader * 104 DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force) 105 { 106 bool create = force; 107 if (!create) 108 { 109 Module* exe_module = process->GetTarget().GetExecutableModulePointer(); 110 if (exe_module) 111 { 112 ObjectFile *object_file = exe_module->GetObjectFile(); 113 if (object_file) 114 { 115 create = (object_file->GetStrata() == ObjectFile::eStrataKernel); 116 } 117 } 118 119 if (create) 120 { 121 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); 122 switch (triple_ref.getOS()) 123 { 124 case llvm::Triple::Darwin: 125 case llvm::Triple::MacOSX: 126 case llvm::Triple::IOS: 127 create = triple_ref.getVendor() == llvm::Triple::Apple; 128 break; 129 default: 130 create = false; 131 break; 132 } 133 } 134 } 135 136 if (create) 137 { 138 process->SetCanJIT(false); 139 return new DynamicLoaderDarwinKernel (process); 140 } 141 return NULL; 142 } 143 144 //---------------------------------------------------------------------- 145 // Constructor 146 //---------------------------------------------------------------------- 147 DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) : 148 DynamicLoader(process), 149 m_kernel(), 150 m_kext_summary_header_ptr_addr (), 151 m_kext_summary_header_addr (), 152 m_kext_summary_header (), 153 m_kext_summaries(), 154 m_mutex(Mutex::eMutexTypeRecursive), 155 m_break_id (LLDB_INVALID_BREAK_ID) 156 { 157 } 158 159 //---------------------------------------------------------------------- 160 // Destructor 161 //---------------------------------------------------------------------- 162 DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel() 163 { 164 Clear(true); 165 } 166 167 void 168 DynamicLoaderDarwinKernel::UpdateIfNeeded() 169 { 170 LoadKernelModuleIfNeeded(); 171 SetNotificationBreakpointIfNeeded (); 172 } 173 //------------------------------------------------------------------ 174 /// Called after attaching a process. 175 /// 176 /// Allow DynamicLoader plug-ins to execute some code after 177 /// attaching to a process. 178 //------------------------------------------------------------------ 179 void 180 DynamicLoaderDarwinKernel::DidAttach () 181 { 182 PrivateInitialize(m_process); 183 UpdateIfNeeded(); 184 } 185 186 //------------------------------------------------------------------ 187 /// Called after attaching a process. 188 /// 189 /// Allow DynamicLoader plug-ins to execute some code after 190 /// attaching to a process. 191 //------------------------------------------------------------------ 192 void 193 DynamicLoaderDarwinKernel::DidLaunch () 194 { 195 PrivateInitialize(m_process); 196 UpdateIfNeeded(); 197 } 198 199 200 //---------------------------------------------------------------------- 201 // Clear out the state of this class. 202 //---------------------------------------------------------------------- 203 void 204 DynamicLoaderDarwinKernel::Clear (bool clear_process) 205 { 206 Mutex::Locker locker(m_mutex); 207 208 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id)) 209 m_process->ClearBreakpointSiteByID(m_break_id); 210 211 if (clear_process) 212 m_process = NULL; 213 m_kernel.Clear(false); 214 m_kext_summary_header_ptr_addr.Clear(); 215 m_kext_summary_header_addr.Clear(); 216 m_kext_summaries.clear(); 217 m_break_id = LLDB_INVALID_BREAK_ID; 218 } 219 220 221 bool 222 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageAtFileAddress (Process *process) 223 { 224 if (IsLoaded()) 225 return true; 226 227 if (module_sp) 228 { 229 bool changed = false; 230 if (module_sp->SetLoadAddress (process->GetTarget(), 0, changed)) 231 load_process_stop_id = process->GetStopID(); 232 } 233 return false; 234 } 235 236 bool 237 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process) 238 { 239 if (IsLoaded()) 240 return true; 241 242 bool uuid_is_valid = uuid.IsValid(); 243 bool memory_module_is_kernel = false; 244 245 Target &target = process->GetTarget(); 246 ModuleSP memory_module_sp; 247 248 // If this is a kext and the user asked us to ignore kexts, don't try to load it. 249 if (kernel_image == false && GetGlobalProperties()->GetLoadKexts() == false) 250 { 251 return false; 252 } 253 254 // Use the memory module as the module if we have one 255 if (address != LLDB_INVALID_ADDRESS) 256 { 257 FileSpec file_spec; 258 if (module_sp) 259 file_spec = module_sp->GetFileSpec(); 260 else 261 file_spec.SetFile (name, false); 262 263 memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false); 264 if (memory_module_sp && !uuid_is_valid) 265 { 266 uuid = memory_module_sp->GetUUID(); 267 uuid_is_valid = uuid.IsValid(); 268 } 269 if (memory_module_sp 270 && memory_module_sp->GetObjectFile() 271 && memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable 272 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel) 273 { 274 memory_module_is_kernel = true; 275 if (memory_module_sp->GetArchitecture().IsValid()) 276 { 277 target.SetArchitecture(memory_module_sp->GetArchitecture()); 278 } 279 } 280 } 281 282 if (!module_sp) 283 { 284 if (uuid_is_valid) 285 { 286 const ModuleList &target_images = target.GetImages(); 287 module_sp = target_images.FindModule(uuid); 288 289 if (!module_sp) 290 { 291 ModuleSpec module_spec; 292 module_spec.GetUUID() = uuid; 293 module_spec.GetArchitecture() = target.GetArchitecture(); 294 295 // For the kernel, we really do need an on-disk file copy of the 296 // binary. 297 bool force_symbols_search = false; 298 if (memory_module_is_kernel) 299 { 300 force_symbols_search = true; 301 } 302 303 if (Symbols::DownloadObjectAndSymbolFile (module_spec, force_symbols_search)) 304 { 305 if (module_spec.GetFileSpec().Exists()) 306 { 307 module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture())); 308 if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec)) 309 { 310 ModuleList loaded_module_list; 311 loaded_module_list.Append (module_sp); 312 target.ModulesDidLoad (loaded_module_list); 313 } 314 } 315 } 316 317 // Ask the Target to find this file on the local system, if possible. 318 // This will search in the list of currently-loaded files, look in the 319 // standard search paths on the system, and on a Mac it will try calling 320 // the DebugSymbols framework with the UUID to find the binary via its 321 // search methods. 322 if (!module_sp) 323 { 324 module_sp = target.GetSharedModule (module_spec); 325 } 326 327 // If we managed to find a module, append it to the target's list of images 328 if (module_sp && module_sp->GetUUID() == memory_module_sp->GetUUID()) 329 { 330 target.GetImages().Append(module_sp); 331 if (memory_module_is_kernel && target.GetExecutableModulePointer() != module_sp.get()) 332 { 333 target.SetExecutableModule (module_sp, false); 334 } 335 } 336 } 337 } 338 } 339 340 341 if (memory_module_sp && module_sp) 342 { 343 if (module_sp->GetUUID() == memory_module_sp->GetUUID()) 344 { 345 ObjectFile *ondisk_object_file = module_sp->GetObjectFile(); 346 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile(); 347 if (memory_object_file && ondisk_object_file) 348 { 349 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList (); 350 SectionList *memory_section_list = memory_object_file->GetSectionList (); 351 if (memory_section_list && ondisk_section_list) 352 { 353 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize(); 354 // There may be CTF sections in the memory image so we can't 355 // always just compare the number of sections (which are actually 356 // segments in mach-o parlance) 357 uint32_t sect_idx = 0; 358 359 // Use the memory_module's addresses for each section to set the 360 // file module's load address as appropriate. We don't want to use 361 // a single slide value for the entire kext - different segments may 362 // be slid different amounts by the kext loader. 363 364 uint32_t num_sections_loaded = 0; 365 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx) 366 { 367 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx)); 368 if (ondisk_section_sp) 369 { 370 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get(); 371 if (memory_section) 372 { 373 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress()); 374 ++num_sections_loaded; 375 } 376 } 377 } 378 if (num_sections_loaded > 0) 379 load_process_stop_id = process->GetStopID(); 380 else 381 module_sp.reset(); // No sections were loaded 382 } 383 else 384 module_sp.reset(); // One or both section lists 385 } 386 else 387 module_sp.reset(); // One or both object files missing 388 } 389 else 390 module_sp.reset(); // UUID mismatch 391 } 392 393 bool is_loaded = IsLoaded(); 394 395 if (so_address.IsValid()) 396 { 397 if (is_loaded) 398 so_address.SetLoadAddress (address, &target); 399 else 400 target.GetImages().ResolveFileAddress (address, so_address); 401 402 } 403 404 if (is_loaded && module_sp && memory_module_is_kernel) 405 { 406 Stream *s = &target.GetDebugger().GetOutputStream(); 407 if (s) 408 { 409 char uuidbuf[64]; 410 s->Printf ("Kernel UUID: %s\n", module_sp->GetUUID().GetAsCString(uuidbuf, sizeof (uuidbuf))); 411 s->Printf ("Load Address: 0x%" PRIx64 "\n", address); 412 if (module_sp->GetFileSpec().GetDirectory().IsEmpty()) 413 { 414 s->Printf ("Loaded kernel file %s\n", module_sp->GetFileSpec().GetFilename().AsCString()); 415 } 416 else 417 { 418 s->Printf ("Loaded kernel file %s/%s\n", 419 module_sp->GetFileSpec().GetDirectory().AsCString(), 420 module_sp->GetFileSpec().GetFilename().AsCString()); 421 } 422 s->Flush (); 423 } 424 } 425 return is_loaded; 426 } 427 428 uint32_t 429 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetAddressByteSize () 430 { 431 if (module_sp) 432 return module_sp->GetArchitecture().GetAddressByteSize(); 433 return 0; 434 } 435 436 lldb::ByteOrder 437 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetByteOrder() 438 { 439 if (module_sp) 440 return module_sp->GetArchitecture().GetByteOrder(); 441 return lldb::endian::InlHostByteOrder(); 442 } 443 444 lldb_private::ArchSpec 445 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetArchitecture () const 446 { 447 if (module_sp) 448 return module_sp->GetArchitecture(); 449 return lldb_private::ArchSpec (); 450 } 451 452 453 //---------------------------------------------------------------------- 454 // Load the kernel module and initialize the "m_kernel" member. Return 455 // true _only_ if the kernel is loaded the first time through (subsequent 456 // calls to this function should return false after the kernel has been 457 // already loaded). 458 //---------------------------------------------------------------------- 459 void 460 DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded() 461 { 462 if (!m_kext_summary_header_ptr_addr.IsValid()) 463 { 464 m_kernel.Clear(false); 465 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule(); 466 m_kernel.kernel_image = true; 467 468 ConstString kernel_name("mach_kernel"); 469 if (m_kernel.module_sp.get() 470 && m_kernel.module_sp->GetObjectFile() 471 && !m_kernel.module_sp->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty()) 472 { 473 kernel_name = m_kernel.module_sp->GetObjectFile()->GetFileSpec().GetFilename(); 474 } 475 strncpy (m_kernel.name, kernel_name.AsCString(), sizeof(m_kernel.name)); 476 m_kernel.name[sizeof (m_kernel.name) - 1] = '\0'; 477 478 if (m_kernel.address == LLDB_INVALID_ADDRESS) 479 { 480 m_kernel.address = m_process->GetImageInfoAddress (); 481 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp) 482 { 483 // We didn't get a hint from the process, so we will 484 // try the kernel at the address that it exists at in 485 // the file if we have one 486 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile(); 487 if (kernel_object_file) 488 { 489 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget()); 490 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress(); 491 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0) 492 { 493 m_kernel.address = load_address; 494 if (load_address != file_address) 495 { 496 // Don't accidentally relocate the kernel to the File address -- 497 // the Load address has already been set to its actual in-memory address. 498 // Mark it as IsLoaded. 499 m_kernel.load_process_stop_id = m_process->GetStopID(); 500 } 501 } 502 else 503 { 504 m_kernel.address = file_address; 505 } 506 } 507 } 508 } 509 510 if (m_kernel.address != LLDB_INVALID_ADDRESS) 511 { 512 if (!m_kernel.LoadImageUsingMemoryModule (m_process)) 513 { 514 m_kernel.LoadImageAtFileAddress (m_process); 515 } 516 } 517 518 if (m_kernel.IsLoaded() && m_kernel.module_sp) 519 { 520 static ConstString kext_summary_symbol ("gLoadedKextSummaries"); 521 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData); 522 if (symbol) 523 { 524 m_kext_summary_header_ptr_addr = symbol->GetAddress(); 525 // Update all image infos 526 ReadAllKextSummaries (); 527 } 528 } 529 else 530 { 531 m_kernel.Clear(false); 532 } 533 } 534 } 535 536 //---------------------------------------------------------------------- 537 // Static callback function that gets called when our DYLD notification 538 // breakpoint gets hit. We update all of our image infos and then 539 // let our super class DynamicLoader class decide if we should stop 540 // or not (based on global preference). 541 //---------------------------------------------------------------------- 542 bool 543 DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton, 544 StoppointCallbackContext *context, 545 user_id_t break_id, 546 user_id_t break_loc_id) 547 { 548 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id); 549 } 550 551 bool 552 DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context, 553 user_id_t break_id, 554 user_id_t break_loc_id) 555 { 556 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); 557 if (log) 558 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n"); 559 560 ReadAllKextSummaries (); 561 562 if (log) 563 PutToLog(log.get()); 564 565 return GetStopWhenImagesChange(); 566 } 567 568 569 bool 570 DynamicLoaderDarwinKernel::ReadKextSummaryHeader () 571 { 572 Mutex::Locker locker(m_mutex); 573 574 // the all image infos is already valid for this process stop ID 575 576 m_kext_summaries.clear(); 577 if (m_kext_summary_header_ptr_addr.IsValid()) 578 { 579 const uint32_t addr_size = m_kernel.GetAddressByteSize (); 580 const ByteOrder byte_order = m_kernel.GetByteOrder(); 581 Error error; 582 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure 583 // which is currenty 4 uint32_t and a pointer. 584 uint8_t buf[24]; 585 DataExtractor data (buf, sizeof(buf), byte_order, addr_size); 586 const size_t count = 4 * sizeof(uint32_t) + addr_size; 587 const bool prefer_file_cache = false; 588 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr, 589 prefer_file_cache, 590 error, 591 m_kext_summary_header_addr)) 592 { 593 // We got a valid address for our kext summary header and make sure it isn't NULL 594 if (m_kext_summary_header_addr.IsValid() && 595 m_kext_summary_header_addr.GetFileAddress() != 0) 596 { 597 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error); 598 if (bytes_read == count) 599 { 600 uint32_t offset = 0; 601 m_kext_summary_header.version = data.GetU32(&offset); 602 if (m_kext_summary_header.version >= 2) 603 { 604 m_kext_summary_header.entry_size = data.GetU32(&offset); 605 } 606 else 607 { 608 // Versions less than 2 didn't have an entry size, it was hard coded 609 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1; 610 } 611 m_kext_summary_header.entry_count = data.GetU32(&offset); 612 return true; 613 } 614 } 615 } 616 } 617 m_kext_summary_header_addr.Clear(); 618 return false; 619 } 620 621 622 bool 623 DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, 624 uint32_t count) 625 { 626 OSKextLoadedKextSummary::collection kext_summaries; 627 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); 628 if (log) 629 log->Printf ("Adding %d modules.\n", count); 630 631 Mutex::Locker locker(m_mutex); 632 633 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries)) 634 return false; 635 636 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream(); 637 if (s) 638 s->Printf ("Loading %d kext modules ", count); 639 for (uint32_t i = 0; i < count; i++) 640 { 641 if (!kext_summaries[i].LoadImageUsingMemoryModule (m_process)) 642 kext_summaries[i].LoadImageAtFileAddress (m_process); 643 644 if (s) 645 s->Printf ("."); 646 647 if (log) 648 kext_summaries[i].PutToLog (log.get()); 649 } 650 if (s) 651 { 652 s->Printf (" done.\n"); 653 s->Flush (); 654 } 655 656 bool return_value = AddModulesUsingImageInfos (kext_summaries); 657 return return_value; 658 } 659 660 // Adds the modules in image_infos to m_kext_summaries. 661 // NB don't call this passing in m_kext_summaries. 662 663 bool 664 DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos) 665 { 666 // Now add these images to the main list. 667 ModuleList loaded_module_list; 668 669 for (uint32_t idx = 0; idx < image_infos.size(); ++idx) 670 { 671 OSKextLoadedKextSummary &image_info = image_infos[idx]; 672 m_kext_summaries.push_back(image_info); 673 674 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id) 675 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp); 676 } 677 678 m_process->GetTarget().ModulesDidLoad (loaded_module_list); 679 return true; 680 } 681 682 683 uint32_t 684 DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr, 685 uint32_t image_infos_count, 686 OSKextLoadedKextSummary::collection &image_infos) 687 { 688 const ByteOrder endian = m_kernel.GetByteOrder(); 689 const uint32_t addr_size = m_kernel.GetAddressByteSize(); 690 691 image_infos.resize(image_infos_count); 692 const size_t count = image_infos.size() * m_kext_summary_header.entry_size; 693 DataBufferHeap data(count, 0); 694 Error error; 695 696 const bool prefer_file_cache = false; 697 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr, 698 prefer_file_cache, 699 data.GetBytes(), 700 data.GetByteSize(), 701 error); 702 if (bytes_read == count) 703 { 704 705 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size); 706 uint32_t i=0; 707 for (uint32_t kext_summary_offset = 0; 708 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size); 709 ++i, kext_summary_offset += m_kext_summary_header.entry_size) 710 { 711 uint32_t offset = kext_summary_offset; 712 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME); 713 if (name_data == NULL) 714 break; 715 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME); 716 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16)); 717 image_infos[i].address = extractor.GetU64(&offset); 718 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget())) 719 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address); 720 image_infos[i].size = extractor.GetU64(&offset); 721 image_infos[i].version = extractor.GetU64(&offset); 722 image_infos[i].load_tag = extractor.GetU32(&offset); 723 image_infos[i].flags = extractor.GetU32(&offset); 724 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size) 725 { 726 image_infos[i].reference_list = extractor.GetU64(&offset); 727 } 728 else 729 { 730 image_infos[i].reference_list = 0; 731 } 732 } 733 if (i < image_infos.size()) 734 image_infos.resize(i); 735 } 736 else 737 { 738 image_infos.clear(); 739 } 740 return image_infos.size(); 741 } 742 743 bool 744 DynamicLoaderDarwinKernel::ReadAllKextSummaries () 745 { 746 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER)); 747 748 Mutex::Locker locker(m_mutex); 749 750 if (ReadKextSummaryHeader ()) 751 { 752 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid()) 753 { 754 Address summary_addr (m_kext_summary_header_addr); 755 summary_addr.Slide(m_kext_summary_header.GetSize()); 756 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count)) 757 { 758 m_kext_summaries.clear(); 759 } 760 return true; 761 } 762 } 763 return false; 764 } 765 766 //---------------------------------------------------------------------- 767 // Dump an image info structure to the file handle provided. 768 //---------------------------------------------------------------------- 769 void 770 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const 771 { 772 if (log == NULL) 773 return; 774 const uint8_t *u = (uint8_t *)uuid.GetBytes(); 775 776 if (address == LLDB_INVALID_ADDRESS) 777 { 778 if (u) 779 { 780 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)", 781 u[ 0], u[ 1], u[ 2], u[ 3], 782 u[ 4], u[ 5], u[ 6], u[ 7], 783 u[ 8], u[ 9], u[10], u[11], 784 u[12], u[13], u[14], u[15], 785 name); 786 } 787 else 788 log->Printf("\tname=\"%s\" (UNLOADED)", name); 789 } 790 else 791 { 792 if (u) 793 { 794 log->Printf("\taddr=0x%16.16" PRIx64 " size=0x%16.16" PRIx64 " version=0x%16.16" PRIx64 " load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16" PRIx64 " 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\"", 795 address, size, version, load_tag, flags, reference_list, 796 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7], 797 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15], 798 name); 799 } 800 else 801 { 802 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") version=0x%16.16" PRIx64 " load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16" PRIx64 " name=\"%s\"", 803 address, address+size, version, load_tag, flags, reference_list, 804 name); 805 } 806 } 807 } 808 809 //---------------------------------------------------------------------- 810 // Dump the _dyld_all_image_infos members and all current image infos 811 // that we have parsed to the file handle provided. 812 //---------------------------------------------------------------------- 813 void 814 DynamicLoaderDarwinKernel::PutToLog(Log *log) const 815 { 816 if (log == NULL) 817 return; 818 819 Mutex::Locker locker(m_mutex); 820 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }", 821 m_kext_summary_header_addr.GetFileAddress(), 822 m_kext_summary_header.version, 823 m_kext_summary_header.entry_size, 824 m_kext_summary_header.entry_count); 825 826 size_t i; 827 const size_t count = m_kext_summaries.size(); 828 if (count > 0) 829 { 830 log->PutCString("Loaded:"); 831 for (i = 0; i<count; i++) 832 m_kext_summaries[i].PutToLog(log); 833 } 834 } 835 836 void 837 DynamicLoaderDarwinKernel::PrivateInitialize(Process *process) 838 { 839 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); 840 Clear(true); 841 m_process = process; 842 } 843 844 void 845 DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded () 846 { 847 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp) 848 { 849 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState())); 850 851 852 const bool internal_bp = true; 853 const LazyBool skip_prologue = eLazyBoolNo; 854 FileSpecList module_spec_list; 855 module_spec_list.Append (m_kernel.module_sp->GetFileSpec()); 856 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list, 857 NULL, 858 "OSKextLoadedKextSummariesUpdated", 859 eFunctionNameTypeFull, 860 skip_prologue, 861 internal_bp).get(); 862 863 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true); 864 m_break_id = bp->GetID(); 865 } 866 } 867 868 //---------------------------------------------------------------------- 869 // Member function that gets called when the process state changes. 870 //---------------------------------------------------------------------- 871 void 872 DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state) 873 { 874 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state)); 875 switch (state) 876 { 877 case eStateConnected: 878 case eStateAttaching: 879 case eStateLaunching: 880 case eStateInvalid: 881 case eStateUnloaded: 882 case eStateExited: 883 case eStateDetached: 884 Clear(false); 885 break; 886 887 case eStateStopped: 888 UpdateIfNeeded(); 889 break; 890 891 case eStateRunning: 892 case eStateStepping: 893 case eStateCrashed: 894 case eStateSuspended: 895 break; 896 897 default: 898 break; 899 } 900 } 901 902 ThreadPlanSP 903 DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others) 904 { 905 ThreadPlanSP thread_plan_sp; 906 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 907 if (log) 908 log->Printf ("Could not find symbol for step through."); 909 return thread_plan_sp; 910 } 911 912 Error 913 DynamicLoaderDarwinKernel::CanLoadImage () 914 { 915 Error error; 916 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel"); 917 return error; 918 } 919 920 void 921 DynamicLoaderDarwinKernel::Initialize() 922 { 923 PluginManager::RegisterPlugin (GetPluginNameStatic(), 924 GetPluginDescriptionStatic(), 925 CreateInstance, 926 DebuggerInitialize); 927 } 928 929 void 930 DynamicLoaderDarwinKernel::Terminate() 931 { 932 PluginManager::UnregisterPlugin (CreateInstance); 933 } 934 935 void 936 DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger) 937 { 938 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName())) 939 { 940 const bool is_global_setting = true; 941 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger, 942 GetGlobalProperties()->GetValueProperties(), 943 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."), 944 is_global_setting); 945 } 946 } 947 948 const char * 949 DynamicLoaderDarwinKernel::GetPluginNameStatic() 950 { 951 return "dynamic-loader.darwin-kernel"; 952 } 953 954 const char * 955 DynamicLoaderDarwinKernel::GetPluginDescriptionStatic() 956 { 957 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel."; 958 } 959 960 961 //------------------------------------------------------------------ 962 // PluginInterface protocol 963 //------------------------------------------------------------------ 964 const char * 965 DynamicLoaderDarwinKernel::GetPluginName() 966 { 967 return "DynamicLoaderDarwinKernel"; 968 } 969 970 const char * 971 DynamicLoaderDarwinKernel::GetShortPluginName() 972 { 973 return GetPluginNameStatic(); 974 } 975 976 uint32_t 977 DynamicLoaderDarwinKernel::GetPluginVersion() 978 { 979 return 1; 980 } 981 982 lldb::ByteOrder 983 DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic) 984 { 985 switch (magic) 986 { 987 case llvm::MachO::HeaderMagic32: 988 case llvm::MachO::HeaderMagic64: 989 return lldb::endian::InlHostByteOrder(); 990 991 case llvm::MachO::HeaderMagic32Swapped: 992 case llvm::MachO::HeaderMagic64Swapped: 993 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig) 994 return lldb::eByteOrderLittle; 995 else 996 return lldb::eByteOrderBig; 997 998 default: 999 break; 1000 } 1001 return lldb::eByteOrderInvalid; 1002 } 1003 1004