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