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/Breakpoint/StoppointCallbackContext.h"
11 #include "lldb/Core/DataBuffer.h"
12 #include "lldb/Core/DataBufferHeap.h"
13 #include "lldb/Core/Debugger.h"
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 "DynamicLoaderDarwinKernel.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 DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
51 {
52     bool create = force;
53     if (!create)
54     {
55         Module* exe_module = process->GetTarget().GetExecutableModulePointer();
56         if (exe_module)
57         {
58             ObjectFile *object_file = exe_module->GetObjectFile();
59             if (object_file)
60             {
61                 create = (object_file->GetStrata() == ObjectFile::eStrataKernel);
62             }
63         }
64 
65         if (create)
66         {
67             const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
68             switch (triple_ref.getOS())
69             {
70                 case llvm::Triple::Darwin:
71                 case llvm::Triple::MacOSX:
72                 case llvm::Triple::IOS:
73                     create = triple_ref.getVendor() == llvm::Triple::Apple;
74                     break;
75                 default:
76                     create = false;
77                     break;
78             }
79         }
80     }
81 
82     if (create)
83     {
84         process->SetCanJIT(false);
85         return new DynamicLoaderDarwinKernel (process);
86     }
87     return NULL;
88 }
89 
90 //----------------------------------------------------------------------
91 // Constructor
92 //----------------------------------------------------------------------
93 DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
94     DynamicLoader(process),
95     m_kernel(),
96     m_kext_summary_header_ptr_addr (),
97     m_kext_summary_header_addr (),
98     m_kext_summary_header (),
99     m_kext_summaries(),
100     m_mutex(Mutex::eMutexTypeRecursive),
101     m_break_id (LLDB_INVALID_BREAK_ID)
102 {
103 }
104 
105 //----------------------------------------------------------------------
106 // Destructor
107 //----------------------------------------------------------------------
108 DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
109 {
110     Clear(true);
111 }
112 
113 void
114 DynamicLoaderDarwinKernel::UpdateIfNeeded()
115 {
116     LoadKernelModuleIfNeeded();
117     SetNotificationBreakpointIfNeeded ();
118 }
119 //------------------------------------------------------------------
120 /// Called after attaching a process.
121 ///
122 /// Allow DynamicLoader plug-ins to execute some code after
123 /// attaching to a process.
124 //------------------------------------------------------------------
125 void
126 DynamicLoaderDarwinKernel::DidAttach ()
127 {
128     PrivateInitialize(m_process);
129     UpdateIfNeeded();
130 }
131 
132 //------------------------------------------------------------------
133 /// Called after attaching a process.
134 ///
135 /// Allow DynamicLoader plug-ins to execute some code after
136 /// attaching to a process.
137 //------------------------------------------------------------------
138 void
139 DynamicLoaderDarwinKernel::DidLaunch ()
140 {
141     PrivateInitialize(m_process);
142     UpdateIfNeeded();
143 }
144 
145 
146 //----------------------------------------------------------------------
147 // Clear out the state of this class.
148 //----------------------------------------------------------------------
149 void
150 DynamicLoaderDarwinKernel::Clear (bool clear_process)
151 {
152     Mutex::Locker locker(m_mutex);
153 
154     if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
155         m_process->ClearBreakpointSiteByID(m_break_id);
156 
157     if (clear_process)
158         m_process = NULL;
159     m_kernel.Clear(false);
160     m_kext_summary_header_ptr_addr.Clear();
161     m_kext_summary_header_addr.Clear();
162     m_kext_summaries.clear();
163     m_break_id = LLDB_INVALID_BREAK_ID;
164 }
165 
166 
167 bool
168 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageAtFileAddress (Process *process)
169 {
170     if (IsLoaded())
171         return true;
172 
173     if (module_sp)
174     {
175         bool changed = false;
176         if (module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
177             load_process_stop_id = process->GetStopID();
178     }
179     return false;
180 }
181 
182 bool
183 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process)
184 {
185     if (IsLoaded())
186         return true;
187 
188     bool uuid_is_valid = uuid.IsValid();
189 
190     Target &target = process->GetTarget();
191     ModuleSP memory_module_sp;
192     // Use the memory module as the module if we have one...
193     if (address != LLDB_INVALID_ADDRESS)
194     {
195         FileSpec file_spec;
196         if (module_sp)
197             file_spec = module_sp->GetFileSpec();
198         else
199             file_spec.SetFile (name, false);
200 
201         memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false);
202         if (memory_module_sp && !uuid_is_valid)
203         {
204             uuid = memory_module_sp->GetUUID();
205             uuid_is_valid = uuid.IsValid();
206         }
207     }
208 
209     if (!module_sp)
210     {
211         bool uuid_is_valid = uuid.IsValid();
212         if (uuid_is_valid)
213         {
214             ModuleList &target_images = target.GetImages();
215             module_sp = target_images.FindModule(uuid);
216 
217             if (!module_sp)
218             {
219                 ModuleSpec module_spec;
220                 module_spec.GetUUID() = uuid;
221                 module_sp = target.GetSharedModule (module_spec);
222             }
223         }
224     }
225 
226 
227     if (memory_module_sp)
228     {
229         // Someone already supplied a file, make sure it is the right one.
230         if (module_sp)
231         {
232             if (module_sp->GetUUID() == memory_module_sp->GetUUID())
233             {
234                 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
235                 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
236                 if (memory_object_file && ondisk_object_file)
237                 {
238                     SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
239                     SectionList *memory_section_list = memory_object_file->GetSectionList ();
240                     if (memory_section_list && ondisk_section_list)
241                     {
242                         const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
243                         // There may be CTF sections in the memory image so we can't
244                         // always just compare the number of sections (which are actually
245                         // segments in mach-o parlance)
246                         uint32_t sect_idx = 0;
247 
248 
249                         // We now iterate through all sections in the file module
250                         // and look to see if the memory module has a load address
251                         // for that section.
252                         uint32_t num_sections_loaded = 0;
253                         for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
254                         {
255                             const Section *ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
256                             if (ondisk_section)
257                             {
258                                 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section->GetName()).get();
259                                 if (memory_section)
260                                 {
261                                     target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
262                                     ++num_sections_loaded;
263                                 }
264                             }
265                         }
266                         if (num_sections_loaded > 0)
267                             load_process_stop_id = process->GetStopID();
268                         else
269                             module_sp.reset(); // No sections were loaded
270                     }
271                     else
272                         module_sp.reset(); // One or both section lists
273                 }
274                 else
275                     module_sp.reset(); // One or both object files missing
276             }
277             else
278                 module_sp.reset(); // UUID mismatch
279         }
280 
281         // Use the memory module as the module if we didn't like the file
282         // module we either found or were supplied with
283         if (!module_sp)
284         {
285             module_sp = memory_module_sp;
286             // Load the memory image in the target as all adresses are already correct
287             bool changed = false;
288             target.GetImages().Append (memory_module_sp);
289             if (module_sp->SetLoadAddress (target, 0, changed))
290                 load_process_stop_id = process->GetStopID();
291         }
292     }
293     bool is_loaded = IsLoaded();
294 
295     if (so_address.IsValid())
296     {
297         if (is_loaded)
298             so_address.SetLoadAddress (address, &target);
299         else
300             target.GetImages().ResolveFileAddress (address, so_address);
301 
302     }
303     return is_loaded;
304 }
305 
306 //----------------------------------------------------------------------
307 // Load the kernel module and initialize the "m_kernel" member. Return
308 // true _only_ if the kernel is loaded the first time through (subsequent
309 // calls to this function should return false after the kernel has been
310 // already loaded).
311 //----------------------------------------------------------------------
312 void
313 DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
314 {
315     if (!m_kext_summary_header_ptr_addr.IsValid())
316     {
317         m_kernel.Clear(false);
318         m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
319         strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
320         if (m_kernel.address == LLDB_INVALID_ADDRESS)
321         {
322             m_kernel.address = m_process->GetImageInfoAddress ();
323             if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
324             {
325                 // We didn't get a hint from the process, so we will
326                 // try the kernel at the address that it exists at in
327                 // the file if we have one
328                 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
329                 if (kernel_object_file)
330                     m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
331             }
332         }
333 
334         if (m_kernel.address != LLDB_INVALID_ADDRESS)
335         {
336             if (!m_kernel.LoadImageUsingMemoryModule (m_process))
337             {
338                 m_kernel.LoadImageAtFileAddress (m_process);
339             }
340         }
341 
342         if (m_kernel.IsLoaded())
343         {
344             static ConstString kext_summary_symbol ("gLoadedKextSummaries");
345             const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
346             if (symbol)
347             {
348                 m_kext_summary_header_ptr_addr = symbol->GetAddress();
349                 // Update all image infos
350                 ReadAllKextSummaries ();
351             }
352         }
353         else
354         {
355             m_kernel.Clear(false);
356         }
357     }
358 }
359 
360 //----------------------------------------------------------------------
361 // Static callback function that gets called when our DYLD notification
362 // breakpoint gets hit. We update all of our image infos and then
363 // let our super class DynamicLoader class decide if we should stop
364 // or not (based on global preference).
365 //----------------------------------------------------------------------
366 bool
367 DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
368                                                   StoppointCallbackContext *context,
369                                                   user_id_t break_id,
370                                                   user_id_t break_loc_id)
371 {
372     return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
373 }
374 
375 bool
376 DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
377                                           user_id_t break_id,
378                                           user_id_t break_loc_id)
379 {
380     LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
381     if (log)
382         log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
383 
384     ReadAllKextSummaries ();
385 
386     if (log)
387         PutToLog(log.get());
388 
389     return GetStopWhenImagesChange();
390 }
391 
392 
393 bool
394 DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
395 {
396     Mutex::Locker locker(m_mutex);
397 
398     // the all image infos is already valid for this process stop ID
399 
400     m_kext_summaries.clear();
401     if (m_kext_summary_header_ptr_addr.IsValid())
402     {
403         const uint32_t addr_size = m_kernel.GetAddressByteSize ();
404         const ByteOrder byte_order = m_kernel.GetByteOrder();
405         Error error;
406         // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
407         // which is currenty 4 uint32_t and a pointer.
408         uint8_t buf[24];
409         DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
410         const size_t count = 4 * sizeof(uint32_t) + addr_size;
411         const bool prefer_file_cache = false;
412         if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
413                                                           prefer_file_cache,
414                                                           error,
415                                                           m_kext_summary_header_addr))
416         {
417             // We got a valid address for our kext summary header and make sure it isn't NULL
418             if (m_kext_summary_header_addr.IsValid() &&
419                 m_kext_summary_header_addr.GetFileAddress() != 0)
420             {
421                 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
422                 if (bytes_read == count)
423                 {
424                     uint32_t offset = 0;
425                     m_kext_summary_header.version = data.GetU32(&offset);
426                     if (m_kext_summary_header.version >= 2)
427                     {
428                         m_kext_summary_header.entry_size = data.GetU32(&offset);
429                     }
430                     else
431                     {
432                         // Versions less than 2 didn't have an entry size, it was hard coded
433                         m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
434                     }
435                     m_kext_summary_header.entry_count = data.GetU32(&offset);
436                     return true;
437                 }
438             }
439         }
440     }
441     m_kext_summary_header_addr.Clear();
442     return false;
443 }
444 
445 
446 bool
447 DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
448                                                uint32_t count)
449 {
450     OSKextLoadedKextSummary::collection kext_summaries;
451     LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
452     if (log)
453         log->Printf ("Adding %d modules.\n", count);
454 
455     Mutex::Locker locker(m_mutex);
456 
457     if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
458         return false;
459 
460     Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
461     for (uint32_t i = 0; i < count; i++)
462     {
463         if (s)
464         {
465             const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
466             if (u)
467             {
468                 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\"...",
469                           u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
470                           u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
471                           kext_summaries[i].address, kext_summaries[i].name);
472             }
473             else
474             {
475                 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
476             }
477         }
478 
479         if (!kext_summaries[i].LoadImageUsingMemoryModule (m_process))
480             kext_summaries[i].LoadImageAtFileAddress (m_process);
481 
482         if (s)
483         {
484             if (kext_summaries[i].module_sp)
485             {
486                 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
487                     s->Printf("\n  found kext: %s/%s\n",
488                               kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
489                               kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
490                 else
491                     s->Printf("\n  found kext: %s\n",
492                               kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
493             }
494             else
495                 s->Printf (" failed to locate/load.\n");
496         }
497 
498         if (log)
499             kext_summaries[i].PutToLog (log.get());
500     }
501     bool return_value = AddModulesUsingImageInfos (kext_summaries);
502     return return_value;
503 }
504 
505 // Adds the modules in image_infos to m_kext_summaries.
506 // NB don't call this passing in m_kext_summaries.
507 
508 bool
509 DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
510 {
511     // Now add these images to the main list.
512     ModuleList loaded_module_list;
513 
514     for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
515     {
516         OSKextLoadedKextSummary &image_info = image_infos[idx];
517         m_kext_summaries.push_back(image_info);
518 
519         if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
520             loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
521     }
522 
523     if (loaded_module_list.GetSize() > 0)
524     {
525         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
526     }
527     return true;
528 }
529 
530 
531 uint32_t
532 DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
533                                               uint32_t image_infos_count,
534                                               OSKextLoadedKextSummary::collection &image_infos)
535 {
536     const ByteOrder endian = m_kernel.GetByteOrder();
537     const uint32_t addr_size = m_kernel.GetAddressByteSize();
538 
539     image_infos.resize(image_infos_count);
540     const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
541     DataBufferHeap data(count, 0);
542     Error error;
543 
544     Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
545 
546     if (s)
547         s->Printf ("Reading %u kext summaries...\n", image_infos_count);
548     const bool prefer_file_cache = false;
549     const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
550                                                                  prefer_file_cache,
551                                                                  data.GetBytes(),
552                                                                  data.GetByteSize(),
553                                                                  error);
554     if (bytes_read == count)
555     {
556 
557         DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
558         uint32_t i=0;
559         for (uint32_t kext_summary_offset = 0;
560              i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
561              ++i, kext_summary_offset += m_kext_summary_header.entry_size)
562         {
563             uint32_t offset = kext_summary_offset;
564             const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
565             if (name_data == NULL)
566                 break;
567             memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
568             image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
569             image_infos[i].address          = extractor.GetU64(&offset);
570             if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
571                 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
572             image_infos[i].size             = extractor.GetU64(&offset);
573             image_infos[i].version          = extractor.GetU64(&offset);
574             image_infos[i].load_tag         = extractor.GetU32(&offset);
575             image_infos[i].flags            = extractor.GetU32(&offset);
576             if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
577             {
578                 image_infos[i].reference_list = extractor.GetU64(&offset);
579             }
580             else
581             {
582                 image_infos[i].reference_list = 0;
583             }
584 //            printf ("[%3u] %*.*s: address=0x%16.16llx, size=0x%16.16llx, version=0x%16.16llx, load_tag=0x%8.8x, flags=0x%8.8x\n",
585 //                    i,
586 //                    KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME,  (char *)name_data,
587 //                    image_infos[i].address,
588 //                    image_infos[i].size,
589 //                    image_infos[i].version,
590 //                    image_infos[i].load_tag,
591 //                    image_infos[i].flags);
592         }
593         if (i < image_infos.size())
594             image_infos.resize(i);
595     }
596     else
597     {
598         image_infos.clear();
599     }
600     return image_infos.size();
601 }
602 
603 bool
604 DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
605 {
606     LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
607 
608     Mutex::Locker locker(m_mutex);
609 
610     if (ReadKextSummaryHeader ())
611     {
612         if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
613         {
614             Address summary_addr (m_kext_summary_header_addr);
615             summary_addr.Slide(m_kext_summary_header.GetSize());
616             if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
617             {
618                 m_kext_summaries.clear();
619             }
620             return true;
621         }
622     }
623     return false;
624 }
625 
626 //----------------------------------------------------------------------
627 // Dump an image info structure to the file handle provided.
628 //----------------------------------------------------------------------
629 void
630 DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
631 {
632     if (log == NULL)
633         return;
634     const uint8_t *u = (uint8_t *)uuid.GetBytes();
635 
636     if (address == LLDB_INVALID_ADDRESS)
637     {
638         if (u)
639         {
640             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)",
641                         u[ 0], u[ 1], u[ 2], u[ 3],
642                         u[ 4], u[ 5], u[ 6], u[ 7],
643                         u[ 8], u[ 9], u[10], u[11],
644                         u[12], u[13], u[14], u[15],
645                         name);
646         }
647         else
648             log->Printf("\tname=\"%s\" (UNLOADED)", name);
649     }
650     else
651     {
652         if (u)
653         {
654             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\"",
655                         address, size, version, load_tag, flags, reference_list,
656                         u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
657                         u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
658                         name);
659         }
660         else
661         {
662             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\"",
663                         address, address+size, version, load_tag, flags, reference_list,
664                         name);
665         }
666     }
667 }
668 
669 //----------------------------------------------------------------------
670 // Dump the _dyld_all_image_infos members and all current image infos
671 // that we have parsed to the file handle provided.
672 //----------------------------------------------------------------------
673 void
674 DynamicLoaderDarwinKernel::PutToLog(Log *log) const
675 {
676     if (log == NULL)
677         return;
678 
679     Mutex::Locker locker(m_mutex);
680     log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
681                 m_kext_summary_header_addr.GetFileAddress(),
682                 m_kext_summary_header.version,
683                 m_kext_summary_header.entry_size,
684                 m_kext_summary_header.entry_count);
685 
686     size_t i;
687     const size_t count = m_kext_summaries.size();
688     if (count > 0)
689     {
690         log->PutCString("Loaded:");
691         for (i = 0; i<count; i++)
692             m_kext_summaries[i].PutToLog(log);
693     }
694 }
695 
696 void
697 DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
698 {
699     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
700     Clear(true);
701     m_process = process;
702     m_process->GetTarget().GetSectionLoadList().Clear();
703 }
704 
705 void
706 DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
707 {
708     if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
709     {
710         DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
711 
712 
713         const bool internal_bp = true;
714         const LazyBool skip_prologue = eLazyBoolNo;
715         FileSpecList module_spec_list;
716         module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
717         Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
718                                                                   NULL,
719                                                                   "OSKextLoadedKextSummariesUpdated",
720                                                                   eFunctionNameTypeFull,
721                                                                   skip_prologue,
722                                                                   internal_bp).get();
723 
724         bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
725         m_break_id = bp->GetID();
726     }
727 }
728 
729 //----------------------------------------------------------------------
730 // Member function that gets called when the process state changes.
731 //----------------------------------------------------------------------
732 void
733 DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
734 {
735     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
736     switch (state)
737     {
738     case eStateConnected:
739     case eStateAttaching:
740     case eStateLaunching:
741     case eStateInvalid:
742     case eStateUnloaded:
743     case eStateExited:
744     case eStateDetached:
745         Clear(false);
746         break;
747 
748     case eStateStopped:
749         UpdateIfNeeded();
750         break;
751 
752     case eStateRunning:
753     case eStateStepping:
754     case eStateCrashed:
755     case eStateSuspended:
756         break;
757 
758     default:
759         break;
760     }
761 }
762 
763 ThreadPlanSP
764 DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
765 {
766     ThreadPlanSP thread_plan_sp;
767     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
768     if (log)
769         log->Printf ("Could not find symbol for step through.");
770     return thread_plan_sp;
771 }
772 
773 Error
774 DynamicLoaderDarwinKernel::CanLoadImage ()
775 {
776     Error error;
777     error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
778     return error;
779 }
780 
781 void
782 DynamicLoaderDarwinKernel::Initialize()
783 {
784     PluginManager::RegisterPlugin (GetPluginNameStatic(),
785                                    GetPluginDescriptionStatic(),
786                                    CreateInstance);
787 }
788 
789 void
790 DynamicLoaderDarwinKernel::Terminate()
791 {
792     PluginManager::UnregisterPlugin (CreateInstance);
793 }
794 
795 
796 const char *
797 DynamicLoaderDarwinKernel::GetPluginNameStatic()
798 {
799     return "dynamic-loader.macosx-kernel";
800 }
801 
802 const char *
803 DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
804 {
805     return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
806 }
807 
808 
809 //------------------------------------------------------------------
810 // PluginInterface protocol
811 //------------------------------------------------------------------
812 const char *
813 DynamicLoaderDarwinKernel::GetPluginName()
814 {
815     return "DynamicLoaderDarwinKernel";
816 }
817 
818 const char *
819 DynamicLoaderDarwinKernel::GetShortPluginName()
820 {
821     return GetPluginNameStatic();
822 }
823 
824 uint32_t
825 DynamicLoaderDarwinKernel::GetPluginVersion()
826 {
827     return 1;
828 }
829 
830