LLDB  mainline
DynamicLoaderDarwinKernel.cpp
Go to the documentation of this file.
1 //===-- DynamicLoaderDarwinKernel.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/State.h"
30 
32 
33 #include <algorithm>
34 #include <memory>
35 
36 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
37 #ifdef ENABLE_DEBUG_PRINTF
38 #include <cstdio>
39 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
40 #else
41 #define DEBUG_PRINTF(fmt, ...)
42 #endif
43 
44 using namespace lldb;
45 using namespace lldb_private;
46 
48 
49 // Progressively greater amounts of scanning we will allow For some targets
50 // very early in startup, we can't do any random reads of memory or we can
51 // crash the device so a setting is needed that can completely disable the
52 // KASLR scans.
53 
55  eKASLRScanNone = 0, // No reading into the inferior at all
56  eKASLRScanLowgloAddresses, // Check one word of memory for a possible kernel
57  // addr, then see if a kernel is there
58  eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel;
59  // checking at 96 locations total
60  eKASLRScanExhaustiveScan // Scan through the entire possible kernel address
61  // range looking for a kernel
62 };
63 
64 static constexpr OptionEnumValueElement g_kaslr_kernel_scan_enum_values[] = {
65  {
67  "none",
68  "Do not read memory looking for a Darwin kernel when attaching.",
69  },
70  {
72  "basic",
73  "Check for the Darwin kernel's load addr in the lowglo page "
74  "(boot-args=debug) only.",
75  },
76  {
78  "fast-scan",
79  "Scan near the pc value on attach to find the Darwin kernel's load "
80  "address.",
81  },
82  {
84  "exhaustive-scan",
85  "Scan through the entire potential address range of Darwin kernel "
86  "(only on 32-bit targets).",
87  },
88 };
89 
90 #define LLDB_PROPERTIES_dynamicloaderdarwinkernel
91 #include "DynamicLoaderDarwinKernelProperties.inc"
92 
93 enum {
94 #define LLDB_PROPERTIES_dynamicloaderdarwinkernel
95 #include "DynamicLoaderDarwinKernelPropertiesEnum.inc"
96 };
97 
99 public:
101  static ConstString g_setting_name("darwin-kernel");
102  return g_setting_name;
103  }
104 
106  m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
107  m_collection_sp->Initialize(g_dynamicloaderdarwinkernel_properties);
108  }
109 
110  ~DynamicLoaderDarwinKernelProperties() override = default;
111 
112  bool GetLoadKexts() const {
113  const uint32_t idx = ePropertyLoadKexts;
114  return m_collection_sp->GetPropertyAtIndexAsBoolean(
115  nullptr, idx,
116  g_dynamicloaderdarwinkernel_properties[idx].default_uint_value != 0);
117  }
118 
120  const uint32_t idx = ePropertyScanType;
121  return (KASLRScanType)m_collection_sp->GetPropertyAtIndexAsEnumeration(
122  nullptr, idx,
123  g_dynamicloaderdarwinkernel_properties[idx].default_uint_value);
124  }
125 };
126 
128  static DynamicLoaderDarwinKernelProperties g_settings;
129  return g_settings;
130 }
131 
132 // Create an instance of this class. This function is filled into the plugin
133 // info class that gets handed out by the plugin factory and allows the lldb to
134 // instantiate an instance of this class.
136  bool force) {
137  if (!force) {
138  // If the user provided an executable binary and it is not a kernel, this
139  // plugin should not create an instance.
140  Module *exe_module = process->GetTarget().GetExecutableModulePointer();
141  if (exe_module) {
142  ObjectFile *object_file = exe_module->GetObjectFile();
143  if (object_file) {
144  if (object_file->GetStrata() != ObjectFile::eStrataKernel) {
145  return nullptr;
146  }
147  }
148  }
149 
150  // If the target's architecture does not look like an Apple environment,
151  // this plugin should not create an instance.
152  const llvm::Triple &triple_ref =
153  process->GetTarget().GetArchitecture().GetTriple();
154  switch (triple_ref.getOS()) {
155  case llvm::Triple::Darwin:
156  case llvm::Triple::MacOSX:
157  case llvm::Triple::IOS:
158  case llvm::Triple::TvOS:
159  case llvm::Triple::WatchOS:
160  // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
161  if (triple_ref.getVendor() != llvm::Triple::Apple) {
162  return nullptr;
163  }
164  break;
165  // If we have triple like armv7-unknown-unknown, we should try looking for
166  // a Darwin kernel.
167  case llvm::Triple::UnknownOS:
168  break;
169  default:
170  return nullptr;
171  break;
172  }
173  }
174 
175  // At this point if there is an ExecutableModule, it is a kernel and the
176  // Target is some variant of an Apple system. If the Process hasn't provided
177  // the kernel load address, we need to look around in memory to find it.
178 
179  const addr_t kernel_load_address = SearchForDarwinKernel(process);
180  if (CheckForKernelImageAtAddress(kernel_load_address, process).IsValid()) {
181  process->SetCanRunCode(false);
182  return new DynamicLoaderDarwinKernel(process, kernel_load_address);
183  }
184  return nullptr;
185 }
186 
189  addr_t kernel_load_address = process->GetImageInfoAddress();
190  if (kernel_load_address == LLDB_INVALID_ADDRESS) {
191  kernel_load_address = SearchForKernelAtSameLoadAddr(process);
192  if (kernel_load_address == LLDB_INVALID_ADDRESS) {
193  kernel_load_address = SearchForKernelWithDebugHints(process);
194  if (kernel_load_address == LLDB_INVALID_ADDRESS) {
195  kernel_load_address = SearchForKernelNearPC(process);
196  if (kernel_load_address == LLDB_INVALID_ADDRESS) {
197  kernel_load_address = SearchForKernelViaExhaustiveSearch(process);
198  }
199  }
200  }
201  }
202  return kernel_load_address;
203 }
204 
205 // Check if the kernel binary is loaded in memory without a slide. First verify
206 // that the ExecutableModule is a kernel before we proceed. Returns the address
207 // of the kernel if one was found, else LLDB_INVALID_ADDRESS.
210  Module *exe_module = process->GetTarget().GetExecutableModulePointer();
211  if (exe_module == nullptr)
212  return LLDB_INVALID_ADDRESS;
213 
214  ObjectFile *exe_objfile = exe_module->GetObjectFile();
215  if (exe_objfile == nullptr)
216  return LLDB_INVALID_ADDRESS;
217 
218  if (exe_objfile->GetType() != ObjectFile::eTypeExecutable ||
219  exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
220  return LLDB_INVALID_ADDRESS;
221 
222  if (!exe_objfile->GetBaseAddress().IsValid())
223  return LLDB_INVALID_ADDRESS;
224 
225  if (CheckForKernelImageAtAddress(
226  exe_objfile->GetBaseAddress().GetFileAddress(), process) ==
227  exe_module->GetUUID())
228  return exe_objfile->GetBaseAddress().GetFileAddress();
229 
230  return LLDB_INVALID_ADDRESS;
231 }
232 
233 // If the debug flag is included in the boot-args nvram setting, the kernel's
234 // load address will be noted in the lowglo page at a fixed address Returns the
235 // address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
238  if (GetGlobalProperties().GetScanType() == eKASLRScanNone)
239  return LLDB_INVALID_ADDRESS;
240 
241  Status read_err;
242  addr_t kernel_addresses_64[] = {
243  0xfffffff000002010ULL,
244  0xfffffff000004010ULL, // newest arm64 devices
245  0xffffff8000004010ULL, // 2014-2015-ish arm64 devices
246  0xffffff8000002010ULL, // oldest arm64 devices
248  addr_t kernel_addresses_32[] = {0xffff0110, // 2016 and earlier armv7 devices
249  0xffff1010, LLDB_INVALID_ADDRESS};
250 
251  uint8_t uval[8];
252  if (process->GetAddressByteSize() == 8) {
253  for (size_t i = 0; kernel_addresses_64[i] != LLDB_INVALID_ADDRESS; i++) {
254  if (process->ReadMemoryFromInferior (kernel_addresses_64[i], uval, 8, read_err) == 8)
255  {
256  DataExtractor data (&uval, 8, process->GetByteOrder(), process->GetAddressByteSize());
257  offset_t offset = 0;
258  uint64_t addr = data.GetU64 (&offset);
259  if (CheckForKernelImageAtAddress(addr, process).IsValid()) {
260  return addr;
261  }
262  }
263  }
264  }
265 
266  if (process->GetAddressByteSize() == 4) {
267  for (size_t i = 0; kernel_addresses_32[i] != LLDB_INVALID_ADDRESS; i++) {
268  if (process->ReadMemoryFromInferior (kernel_addresses_32[i], uval, 4, read_err) == 4)
269  {
270  DataExtractor data (&uval, 4, process->GetByteOrder(), process->GetAddressByteSize());
271  offset_t offset = 0;
272  uint32_t addr = data.GetU32 (&offset);
273  if (CheckForKernelImageAtAddress(addr, process).IsValid()) {
274  return addr;
275  }
276  }
277  }
278  }
279 
280  return LLDB_INVALID_ADDRESS;
281 }
282 
283 // If the kernel is currently executing when lldb attaches, and we don't have a
284 // better way of finding the kernel's load address, try searching backwards
285 // from the current pc value looking for the kernel's Mach header in memory.
286 // Returns the address of the kernel if one was found, else
287 // LLDB_INVALID_ADDRESS.
290  if (GetGlobalProperties().GetScanType() == eKASLRScanNone ||
291  GetGlobalProperties().GetScanType() == eKASLRScanLowgloAddresses) {
292  return LLDB_INVALID_ADDRESS;
293  }
294 
295  ThreadSP thread = process->GetThreadList().GetSelectedThread();
296  if (thread.get() == nullptr)
297  return LLDB_INVALID_ADDRESS;
298  addr_t pc = thread->GetRegisterContext()->GetPC(LLDB_INVALID_ADDRESS);
299 
300  int ptrsize = process->GetTarget().GetArchitecture().GetAddressByteSize();
301 
302  // The kernel is always loaded in high memory, if the top bit is zero,
303  // this isn't a kernel.
304  if (ptrsize == 8) {
305  if ((pc & (1ULL << 63)) == 0) {
306  return LLDB_INVALID_ADDRESS;
307  }
308  } else {
309  if ((pc & (1ULL << 31)) == 0) {
310  return LLDB_INVALID_ADDRESS;
311  }
312  }
313 
314  if (pc == LLDB_INVALID_ADDRESS)
315  return LLDB_INVALID_ADDRESS;
316 
317  int pagesize = 0x4000; // 16k pages on 64-bit targets
318  if (ptrsize == 4)
319  pagesize = 0x1000; // 4k pages on 32-bit targets
320 
321  // The kernel will be loaded on a page boundary.
322  // Round the current pc down to the nearest page boundary.
323  addr_t addr = pc & ~(pagesize - 1ULL);
324 
325  // Search backwards for 32 megabytes, or first memory read error.
326  while (pc - addr < 32 * 0x100000) {
327  bool read_error;
328  if (CheckForKernelImageAtAddress(addr, process, &read_error).IsValid())
329  return addr;
330 
331  // Stop scanning on the first read error we encounter; we've walked
332  // past this executable block of memory.
333  if (read_error == true)
334  break;
335 
336  addr -= pagesize;
337  }
338 
339  return LLDB_INVALID_ADDRESS;
340 }
341 
342 // Scan through the valid address range for a kernel binary. This is uselessly
343 // slow in 64-bit environments so we don't even try it. This scan is not
344 // enabled by default even for 32-bit targets. Returns the address of the
345 // kernel if one was found, else LLDB_INVALID_ADDRESS.
347  Process *process) {
348  if (GetGlobalProperties().GetScanType() != eKASLRScanExhaustiveScan) {
349  return LLDB_INVALID_ADDRESS;
350  }
351 
352  addr_t kernel_range_low, kernel_range_high;
353  if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8) {
354  kernel_range_low = 1ULL << 63;
355  kernel_range_high = UINT64_MAX;
356  } else {
357  kernel_range_low = 1ULL << 31;
358  kernel_range_high = UINT32_MAX;
359  }
360 
361  // Stepping through memory at one-megabyte resolution looking for a kernel
362  // rarely works (fast enough) with a 64-bit address space -- for now, let's
363  // not even bother. We may be attaching to something which *isn't* a kernel
364  // and we don't want to spin for minutes on-end looking for a kernel.
365  if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
366  return LLDB_INVALID_ADDRESS;
367 
368  addr_t addr = kernel_range_low;
369 
370  while (addr >= kernel_range_low && addr < kernel_range_high) {
371  // x86_64 kernels are at offset 0
372  if (CheckForKernelImageAtAddress(addr, process).IsValid())
373  return addr;
374  // 32-bit arm kernels are at offset 0x1000 (one 4k page)
375  if (CheckForKernelImageAtAddress(addr + 0x1000, process).IsValid())
376  return addr + 0x1000;
377  // 64-bit arm kernels are at offset 0x4000 (one 16k page)
378  if (CheckForKernelImageAtAddress(addr + 0x4000, process).IsValid())
379  return addr + 0x4000;
380  addr += 0x100000;
381  }
382  return LLDB_INVALID_ADDRESS;
383 }
384 
385 // Read the mach_header struct out of memory and return it.
386 // Returns true if the mach_header was successfully read,
387 // Returns false if there was a problem reading the header, or it was not
388 // a Mach-O header.
389 
390 bool
391 DynamicLoaderDarwinKernel::ReadMachHeader(addr_t addr, Process *process, llvm::MachO::mach_header &header,
392  bool *read_error) {
393  Status error;
394  if (read_error)
395  *read_error = false;
396 
397  // Read the mach header and see whether it looks like a kernel
398  if (process->ReadMemory(addr, &header, sizeof(header), error) !=
399  sizeof(header)) {
400  if (read_error)
401  *read_error = true;
402  return false;
403  }
404 
405  const uint32_t magicks[] = { llvm::MachO::MH_MAGIC_64, llvm::MachO::MH_MAGIC, llvm::MachO::MH_CIGAM, llvm::MachO::MH_CIGAM_64};
406 
407  bool found_matching_pattern = false;
408  for (size_t i = 0; i < llvm::array_lengthof (magicks); i++)
409  if (::memcmp (&header.magic, &magicks[i], sizeof (uint32_t)) == 0)
410  found_matching_pattern = true;
411 
412  if (!found_matching_pattern)
413  return false;
414 
415  if (header.magic == llvm::MachO::MH_CIGAM ||
416  header.magic == llvm::MachO::MH_CIGAM_64) {
417  header.magic = llvm::ByteSwap_32(header.magic);
418  header.cputype = llvm::ByteSwap_32(header.cputype);
419  header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
420  header.filetype = llvm::ByteSwap_32(header.filetype);
421  header.ncmds = llvm::ByteSwap_32(header.ncmds);
422  header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
423  header.flags = llvm::ByteSwap_32(header.flags);
424  }
425 
426  return true;
427 }
428 
429 // Given an address in memory, look to see if there is a kernel image at that
430 // address.
431 // Returns a UUID; if a kernel was not found at that address, UUID.IsValid()
432 // will be false.
435  Process *process,
436  bool *read_error) {
438  if (addr == LLDB_INVALID_ADDRESS) {
439  if (read_error)
440  *read_error = true;
441  return UUID();
442  }
443 
444  LLDB_LOGF(log,
445  "DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: "
446  "looking for kernel binary at 0x%" PRIx64,
447  addr);
448 
449  llvm::MachO::mach_header header;
450 
451  if (!ReadMachHeader(addr, process, header, read_error))
452  return UUID();
453 
454  // First try a quick test -- read the first 4 bytes and see if there is a
455  // valid Mach-O magic field there
456  // (the first field of the mach_header/mach_header_64 struct).
457  // A kernel is an executable which does not have the dynamic link object flag
458  // set.
459  if (header.filetype == llvm::MachO::MH_EXECUTE &&
460  (header.flags & llvm::MachO::MH_DYLDLINK) == 0) {
461  // Create a full module to get the UUID
462  ModuleSP memory_module_sp =
463  process->ReadModuleFromMemory(FileSpec("temp_mach_kernel"), addr);
464  if (!memory_module_sp.get())
465  return UUID();
466 
467  ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
468  if (exe_objfile == nullptr) {
469  LLDB_LOGF(log,
470  "DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress "
471  "found a binary at 0x%" PRIx64
472  " but could not create an object file from memory",
473  addr);
474  return UUID();
475  }
476 
477  if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
478  exe_objfile->GetStrata() == ObjectFile::eStrataKernel) {
479  ArchSpec kernel_arch(eArchTypeMachO, header.cputype, header.cpusubtype);
480  if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(
481  kernel_arch)) {
482  process->GetTarget().SetArchitecture(kernel_arch);
483  }
484  if (log) {
485  std::string uuid_str;
486  if (memory_module_sp->GetUUID().IsValid()) {
487  uuid_str = "with UUID ";
488  uuid_str += memory_module_sp->GetUUID().GetAsString();
489  } else {
490  uuid_str = "and no LC_UUID found in load commands ";
491  }
492  LLDB_LOGF(
493  log,
494  "DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: "
495  "kernel binary image found at 0x%" PRIx64 " with arch '%s' %s",
496  addr, kernel_arch.GetTriple().str().c_str(), uuid_str.c_str());
497  }
498  return memory_module_sp->GetUUID();
499  }
500  }
501 
502  return UUID();
503 }
504 
505 // Constructor
507  lldb::addr_t kernel_addr)
508  : DynamicLoader(process), m_kernel_load_address(kernel_addr), m_kernel(),
509  m_kext_summary_header_ptr_addr(), m_kext_summary_header_addr(),
510  m_kext_summary_header(), m_known_kexts(), m_mutex(),
511  m_break_id(LLDB_INVALID_BREAK_ID) {
512  Status error;
513  PlatformSP platform_sp(Platform::Create(
515  if (platform_sp.get())
516  process->GetTarget().SetPlatform(platform_sp);
517 }
518 
519 // Destructor
521 
525 }
526 /// Called after attaching a process.
527 ///
528 /// Allow DynamicLoader plug-ins to execute some code after
529 /// attaching to a process.
532  UpdateIfNeeded();
533 }
534 
535 /// Called after attaching a process.
536 ///
537 /// Allow DynamicLoader plug-ins to execute some code after
538 /// attaching to a process.
541  UpdateIfNeeded();
542 }
543 
544 // Clear out the state of this class.
545 void DynamicLoaderDarwinKernel::Clear(bool clear_process) {
546  std::lock_guard<std::recursive_mutex> guard(m_mutex);
547 
550 
551  if (clear_process)
552  m_process = nullptr;
553  m_kernel.Clear();
554  m_known_kexts.clear();
558 }
559 
561  Process *process) {
562  if (IsLoaded())
563  return true;
564 
565  if (m_module_sp) {
566  bool changed = false;
567  if (m_module_sp->SetLoadAddress(process->GetTarget(), 0, true, changed))
568  m_load_process_stop_id = process->GetStopID();
569  }
570  return false;
571 }
572 
574  m_module_sp = module_sp;
575  if (module_sp.get() && module_sp->GetObjectFile()) {
576  if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable &&
577  module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel) {
578  m_kernel_image = true;
579  } else {
580  m_kernel_image = false;
581  }
582  }
583 }
584 
586  return m_module_sp;
587 }
588 
590  addr_t load_addr) {
591  m_load_address = load_addr;
592 }
593 
595  return m_load_address;
596 }
597 
599  return m_size;
600 }
601 
603  m_size = size;
604 }
605 
607  return m_load_process_stop_id;
608 }
609 
611  uint32_t stop_id) {
612  m_load_process_stop_id = stop_id;
613 }
614 
617  if (m_uuid.IsValid() || rhs.GetUUID().IsValid()) {
618  return m_uuid == rhs.GetUUID();
619  }
620 
621  return m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress();
622 }
623 
625  m_name = name;
626 }
627 
629  return m_name;
630 }
631 
633  m_uuid = uuid;
634 }
635 
637  return m_uuid;
638 }
639 
640 // Given the m_load_address from the kext summaries, and a UUID, try to create
641 // an in-memory Module at that address. Require that the MemoryModule have a
642 // matching UUID and detect if this MemoryModule is a kernel or a kext.
643 //
644 // Returns true if m_memory_module_sp is now set to a valid Module.
645 
647  Process *process) {
649  if (m_memory_module_sp.get() != nullptr)
650  return true;
651  if (m_load_address == LLDB_INVALID_ADDRESS)
652  return false;
653 
654  FileSpec file_spec(m_name.c_str());
655 
656  llvm::MachO::mach_header mh;
657  size_t size_to_read = 512;
658  if (ReadMachHeader(m_load_address, process, mh)) {
659  if (mh.magic == llvm::MachO::MH_CIGAM || mh.magic == llvm::MachO::MH_MAGIC)
660  size_to_read = sizeof(llvm::MachO::mach_header) + mh.sizeofcmds;
661  if (mh.magic == llvm::MachO::MH_CIGAM_64 ||
662  mh.magic == llvm::MachO::MH_MAGIC_64)
663  size_to_read = sizeof(llvm::MachO::mach_header_64) + mh.sizeofcmds;
664  }
665 
666  ModuleSP memory_module_sp =
667  process->ReadModuleFromMemory(file_spec, m_load_address, size_to_read);
668 
669  if (memory_module_sp.get() == nullptr)
670  return false;
671 
672  bool is_kernel = false;
673  if (memory_module_sp->GetObjectFile()) {
674  if (memory_module_sp->GetObjectFile()->GetType() ==
675  ObjectFile::eTypeExecutable &&
676  memory_module_sp->GetObjectFile()->GetStrata() ==
677  ObjectFile::eStrataKernel) {
678  is_kernel = true;
679  } else if (memory_module_sp->GetObjectFile()->GetType() ==
680  ObjectFile::eTypeSharedLibrary) {
681  is_kernel = false;
682  }
683  }
684 
685  // If this is a kext, and the kernel specified what UUID we should find at
686  // this load address, require that the memory module have a matching UUID or
687  // something has gone wrong and we should discard it.
688  if (m_uuid.IsValid()) {
689  if (m_uuid != memory_module_sp->GetUUID()) {
690  if (log) {
691  LLDB_LOGF(log,
692  "KextImageInfo::ReadMemoryModule the kernel said to find "
693  "uuid %s at 0x%" PRIx64
694  " but instead we found uuid %s, throwing it away",
695  m_uuid.GetAsString().c_str(), m_load_address,
696  memory_module_sp->GetUUID().GetAsString().c_str());
697  }
698  return false;
699  }
700  }
701 
702  // If the in-memory Module has a UUID, let's use that.
703  if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid()) {
704  m_uuid = memory_module_sp->GetUUID();
705  }
706 
707  m_memory_module_sp = memory_module_sp;
708  m_kernel_image = is_kernel;
709  if (is_kernel) {
710  if (log) {
711  // This is unusual and probably not intended
712  LLDB_LOGF(log,
713  "KextImageInfo::ReadMemoryModule read the kernel binary out "
714  "of memory");
715  }
716  if (memory_module_sp->GetArchitecture().IsValid()) {
717  process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
718  }
719  if (m_uuid.IsValid()) {
720  ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule();
721  if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid()) {
722  if (m_uuid != exe_module_sp->GetUUID()) {
723  // The user specified a kernel binary that has a different UUID than
724  // the kernel actually running in memory. This never ends well;
725  // clear the user specified kernel binary from the Target.
726 
727  m_module_sp.reset();
728 
729  ModuleList user_specified_kernel_list;
730  user_specified_kernel_list.Append(exe_module_sp);
731  process->GetTarget().GetImages().Remove(user_specified_kernel_list);
732  }
733  }
734  }
735  }
736 
737  return true;
738 }
739 
741  return m_kernel_image;
742 }
743 
745  m_kernel_image = is_kernel;
746 }
747 
749  Process *process) {
750  if (IsLoaded())
751  return true;
752 
753  Target &target = process->GetTarget();
754 
755  // kexts will have a uuid from the table.
756  // for the kernel, we'll need to read the load commands out of memory to get it.
757  if (m_uuid.IsValid() == false) {
758  if (ReadMemoryModule(process) == false) {
760  LLDB_LOGF(log,
761  "Unable to read '%s' from memory at address 0x%" PRIx64
762  " to get the segment load addresses.",
763  m_name.c_str(), m_load_address);
764  return false;
765  }
766  }
767 
768  if (IsKernel() && m_uuid.IsValid()) {
769  Stream &s = target.GetDebugger().GetOutputStream();
770  s.Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
771  s.Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
772  }
773 
774  if (!m_module_sp) {
775  // See if the kext has already been loaded into the target, probably by the
776  // user doing target modules add.
777  const ModuleList &target_images = target.GetImages();
778  m_module_sp = target_images.FindModule(m_uuid);
779 
780  // Search for the kext on the local filesystem via the UUID
781  if (!m_module_sp && m_uuid.IsValid()) {
782  ModuleSpec module_spec;
783  module_spec.GetUUID() = m_uuid;
784  module_spec.GetArchitecture() = target.GetArchitecture();
785 
786  // For the kernel, we really do need an on-disk file copy of the binary
787  // to do anything useful. This will force a call to dsymForUUID if it
788  // exists, instead of depending on the DebugSymbols preferences being
789  // set.
790  if (IsKernel()) {
791  if (Symbols::DownloadObjectAndSymbolFile(module_spec, true)) {
792  if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
793  m_module_sp = std::make_shared<Module>(module_spec.GetFileSpec(),
794  target.GetArchitecture());
795  }
796  }
797  }
798 
799  // If the current platform is PlatformDarwinKernel, create a ModuleSpec
800  // with the filename set to be the bundle ID for this kext, e.g.
801  // "com.apple.filesystems.msdosfs", and ask the platform to find it.
802  // PlatformDarwinKernel does a special scan for kexts on the local
803  // system.
804  PlatformSP platform_sp(target.GetPlatform());
805  if (!m_module_sp && platform_sp) {
806  static ConstString g_platform_name(
808  if (platform_sp->GetPluginName() == g_platform_name.GetStringRef()) {
809  ModuleSpec kext_bundle_module_spec(module_spec);
810  FileSpec kext_filespec(m_name.c_str());
811  FileSpecList search_paths = target.GetExecutableSearchPaths();
812  kext_bundle_module_spec.GetFileSpec() = kext_filespec;
813  platform_sp->GetSharedModule(kext_bundle_module_spec, process,
814  m_module_sp, &search_paths, nullptr,
815  nullptr);
816  }
817  }
818 
819  // Ask the Target to find this file on the local system, if possible.
820  // This will search in the list of currently-loaded files, look in the
821  // standard search paths on the system, and on a Mac it will try calling
822  // the DebugSymbols framework with the UUID to find the binary via its
823  // search methods.
824  if (!m_module_sp) {
825  m_module_sp = target.GetOrCreateModule(module_spec, true /* notify */);
826  }
827 
828  if (IsKernel() && !m_module_sp) {
829  Stream &s = target.GetDebugger().GetOutputStream();
830  s.Printf("WARNING: Unable to locate kernel binary on the debugger "
831  "system.\n");
832  }
833  }
834 
835  // If we managed to find a module, append it to the target's list of
836  // images. If we also have a memory module, require that they have matching
837  // UUIDs
838  if (m_module_sp) {
839  if (m_uuid.IsValid() && m_module_sp->GetUUID() == m_uuid) {
840  target.GetImages().AppendIfNeeded(m_module_sp, false);
841  if (IsKernel() &&
842  target.GetExecutableModulePointer() != m_module_sp.get()) {
843  target.SetExecutableModule(m_module_sp, eLoadDependentsNo);
844  }
845  }
846  }
847  }
848 
849  // If we've found a binary, read the load commands out of memory so we
850  // can set the segment load addresses.
851  if (m_module_sp)
852  ReadMemoryModule (process);
853 
854  static ConstString g_section_name_LINKEDIT("__LINKEDIT");
855 
856  if (m_memory_module_sp && m_module_sp) {
857  if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID()) {
858  ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
859  ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
860 
861  if (memory_object_file && ondisk_object_file) {
862  // The memory_module for kexts may have an invalid __LINKEDIT seg; skip
863  // it.
864  const bool ignore_linkedit = !IsKernel();
865 
866  SectionList *ondisk_section_list = ondisk_object_file->GetSectionList();
867  SectionList *memory_section_list = memory_object_file->GetSectionList();
868  if (memory_section_list && ondisk_section_list) {
869  const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
870  // There may be CTF sections in the memory image so we can't always
871  // just compare the number of sections (which are actually segments
872  // in mach-o parlance)
873  uint32_t sect_idx = 0;
874 
875  // Use the memory_module's addresses for each section to set the file
876  // module's load address as appropriate. We don't want to use a
877  // single slide value for the entire kext - different segments may be
878  // slid different amounts by the kext loader.
879 
880  uint32_t num_sections_loaded = 0;
881  for (sect_idx = 0; sect_idx < num_ondisk_sections; ++sect_idx) {
882  SectionSP ondisk_section_sp(
883  ondisk_section_list->GetSectionAtIndex(sect_idx));
884  if (ondisk_section_sp) {
885  // Don't ever load __LINKEDIT as it may or may not be actually
886  // mapped into memory and there is no current way to tell.
887  // I filed rdar://problem/12851706 to track being able to tell
888  // if the __LINKEDIT is actually mapped, but until then, we need
889  // to not load the __LINKEDIT
890  if (ignore_linkedit &&
891  ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
892  continue;
893 
894  const Section *memory_section =
895  memory_section_list
896  ->FindSectionByName(ondisk_section_sp->GetName())
897  .get();
898  if (memory_section) {
899  target.SetSectionLoadAddress(ondisk_section_sp,
900  memory_section->GetFileAddress());
901  ++num_sections_loaded;
902  }
903  }
904  }
905  if (num_sections_loaded > 0)
906  m_load_process_stop_id = process->GetStopID();
907  else
908  m_module_sp.reset(); // No sections were loaded
909  } else
910  m_module_sp.reset(); // One or both section lists
911  } else
912  m_module_sp.reset(); // One or both object files missing
913  } else
914  m_module_sp.reset(); // UUID mismatch
915  }
916 
917  bool is_loaded = IsLoaded();
918 
919  if (is_loaded && m_module_sp && IsKernel()) {
920  Stream &s = target.GetDebugger().GetOutputStream();
921  ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
922  if (kernel_object_file) {
923  addr_t file_address =
924  kernel_object_file->GetBaseAddress().GetFileAddress();
925  if (m_load_address != LLDB_INVALID_ADDRESS &&
926  file_address != LLDB_INVALID_ADDRESS) {
927  s.Printf("Kernel slid 0x%" PRIx64 " in memory.\n",
928  m_load_address - file_address);
929  }
930  }
931  {
932  s.Printf("Loaded kernel file %s\n",
933  m_module_sp->GetFileSpec().GetPath().c_str());
934  }
935  s.Flush();
936  }
937 
938  // Notify the target about the module being added;
939  // set breakpoints, load dSYM scripts, etc. as needed.
940  if (is_loaded && m_module_sp) {
941  ModuleList loaded_module_list;
942  loaded_module_list.Append(m_module_sp);
943  target.ModulesDidLoad(loaded_module_list);
944  }
945 
946  return is_loaded;
947 }
948 
950  if (m_memory_module_sp)
951  return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
952  if (m_module_sp)
953  return m_module_sp->GetArchitecture().GetAddressByteSize();
954  return 0;
955 }
956 
958  if (m_memory_module_sp)
959  return m_memory_module_sp->GetArchitecture().GetByteOrder();
960  if (m_module_sp)
961  return m_module_sp->GetArchitecture().GetByteOrder();
962  return endian::InlHostByteOrder();
963 }
964 
967  if (m_memory_module_sp)
968  return m_memory_module_sp->GetArchitecture();
969  if (m_module_sp)
970  return m_module_sp->GetArchitecture();
971  return lldb_private::ArchSpec();
972 }
973 
974 // Load the kernel module and initialize the "m_kernel" member. Return true
975 // _only_ if the kernel is loaded the first time through (subsequent calls to
976 // this function should return false after the kernel has been already loaded).
979  m_kernel.Clear();
981  m_kernel.SetIsKernel(true);
982 
983  ConstString kernel_name("mach_kernel");
984  if (m_kernel.GetModule().get() && m_kernel.GetModule()->GetObjectFile() &&
986  ->GetObjectFile()
987  ->GetFileSpec()
988  .GetFilename()
989  .IsEmpty()) {
990  kernel_name =
991  m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
992  }
993  m_kernel.SetName(kernel_name.AsCString());
994 
998  m_kernel.GetModule()) {
999  // We didn't get a hint from the process, so we will try the kernel at
1000  // the address that it exists at in the file if we have one
1001  ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
1002  if (kernel_object_file) {
1003  addr_t load_address =
1004  kernel_object_file->GetBaseAddress().GetLoadAddress(
1005  &m_process->GetTarget());
1006  addr_t file_address =
1007  kernel_object_file->GetBaseAddress().GetFileAddress();
1008  if (load_address != LLDB_INVALID_ADDRESS && load_address != 0) {
1009  m_kernel.SetLoadAddress(load_address);
1010  if (load_address != file_address) {
1011  // Don't accidentally relocate the kernel to the File address --
1012  // the Load address has already been set to its actual in-memory
1013  // address. Mark it as IsLoaded.
1015  }
1016  } else {
1017  m_kernel.SetLoadAddress(file_address);
1018  }
1019  }
1020  }
1021  }
1022 
1026  }
1027  }
1028 
1029  // The operating system plugin gets loaded and initialized in
1030  // LoadImageUsingMemoryModule when we discover the kernel dSYM. For a core
1031  // file in particular, that's the wrong place to do this, since we haven't
1032  // fixed up the section addresses yet. So let's redo it here.
1034 
1035  if (m_kernel.IsLoaded() && m_kernel.GetModule()) {
1036  static ConstString kext_summary_symbol("gLoadedKextSummaries");
1037  const Symbol *symbol =
1038  m_kernel.GetModule()->FindFirstSymbolWithNameAndType(
1039  kext_summary_symbol, eSymbolTypeData);
1040  if (symbol) {
1042  // Update all image infos
1044  }
1045  } else {
1046  m_kernel.Clear();
1047  }
1048  }
1049 }
1050 
1051 // Static callback function that gets called when our DYLD notification
1052 // breakpoint gets hit. We update all of our image infos and then let our super
1053 // class DynamicLoader class decide if we should stop or not (based on global
1054 // preference).
1056  void *baton, StoppointCallbackContext *context, user_id_t break_id,
1057  user_id_t break_loc_id) {
1058  return static_cast<DynamicLoaderDarwinKernel *>(baton)->BreakpointHit(
1059  context, break_id, break_loc_id);
1060 }
1061 
1063  user_id_t break_id,
1064  user_id_t break_loc_id) {
1066  LLDB_LOGF(log, "DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
1067 
1069 
1070  if (log)
1071  PutToLog(log);
1072 
1073  return GetStopWhenImagesChange();
1074 }
1075 
1077  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1078 
1079  // the all image infos is already valid for this process stop ID
1080 
1082  const uint32_t addr_size = m_kernel.GetAddressByteSize();
1083  const ByteOrder byte_order = m_kernel.GetByteOrder();
1084  Status error;
1085  // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure which
1086  // is currently 4 uint32_t and a pointer.
1087  uint8_t buf[24];
1088  DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
1089  const size_t count = 4 * sizeof(uint32_t) + addr_size;
1090  const bool force_live_memory = true;
1093  m_kext_summary_header_addr, force_live_memory)) {
1094  // We got a valid address for our kext summary header and make sure it
1095  // isn't NULL
1098  const size_t bytes_read = m_process->GetTarget().ReadMemory(
1099  m_kext_summary_header_addr, buf, count, error, force_live_memory);
1100  if (bytes_read == count) {
1101  lldb::offset_t offset = 0;
1102  m_kext_summary_header.version = data.GetU32(&offset);
1103  if (m_kext_summary_header.version > 128) {
1105  s.Printf("WARNING: Unable to read kext summary header, got "
1106  "improbable version number %u\n",
1108  // If we get an improbably large version number, we're probably
1109  // getting bad memory.
1111  return false;
1112  }
1113  if (m_kext_summary_header.version >= 2) {
1114  m_kext_summary_header.entry_size = data.GetU32(&offset);
1115  if (m_kext_summary_header.entry_size > 4096) {
1116  // If we get an improbably large entry_size, we're probably
1117  // getting bad memory.
1118  Stream &s =
1120  s.Printf("WARNING: Unable to read kext summary header, got "
1121  "improbable entry_size %u\n",
1124  return false;
1125  }
1126  } else {
1127  // Versions less than 2 didn't have an entry size, it was hard
1128  // coded
1131  }
1132  m_kext_summary_header.entry_count = data.GetU32(&offset);
1133  if (m_kext_summary_header.entry_count > 10000) {
1134  // If we get an improbably large number of kexts, we're probably
1135  // getting bad memory.
1137  s.Printf("WARNING: Unable to read kext summary header, got "
1138  "improbable number of kexts %u\n",
1141  return false;
1142  }
1143  return true;
1144  }
1145  }
1146  }
1147  }
1149  return false;
1150 }
1151 
1152 // We've either (a) just attached to a new kernel, or (b) the kexts-changed
1153 // breakpoint was hit and we need to figure out what kexts have been added or
1154 // removed. Read the kext summaries from the inferior kernel memory, compare
1155 // them against the m_known_kexts vector and update the m_known_kexts vector as
1156 // needed to keep in sync with the inferior.
1157 
1159  const Address &kext_summary_addr, uint32_t count) {
1160  KextImageInfo::collection kext_summaries;
1162  LLDB_LOGF(log,
1163  "Kexts-changed breakpoint hit, there are %d kexts currently.\n",
1164  count);
1165 
1166  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1167 
1168  if (!ReadKextSummaries(kext_summary_addr, count, kext_summaries))
1169  return false;
1170 
1171  // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the
1172  // user requested no kext loading, don't print any messages about kexts &
1173  // don't try to read them.
1174  const bool load_kexts = GetGlobalProperties().GetLoadKexts();
1175 
1176  // By default, all kexts we've loaded in the past are marked as "remove" and
1177  // all of the kexts we just found out about from ReadKextSummaries are marked
1178  // as "add".
1179  std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1180  std::vector<bool> to_be_added(count, true);
1181 
1182  int number_of_new_kexts_being_added = 0;
1183  int number_of_old_kexts_being_removed = m_known_kexts.size();
1184 
1185  const uint32_t new_kexts_size = kext_summaries.size();
1186  const uint32_t old_kexts_size = m_known_kexts.size();
1187 
1188  // The m_known_kexts vector may have entries that have been Cleared, or are a
1189  // kernel.
1190  for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++) {
1191  bool ignore = false;
1192  KextImageInfo &image_info = m_known_kexts[old_kext];
1193  if (image_info.IsKernel()) {
1194  ignore = true;
1195  } else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS &&
1196  !image_info.GetModule()) {
1197  ignore = true;
1198  }
1199 
1200  if (ignore) {
1201  number_of_old_kexts_being_removed--;
1202  to_be_removed[old_kext] = false;
1203  }
1204  }
1205 
1206  // Scan over the list of kexts we just read from the kernel, note those that
1207  // need to be added and those already loaded.
1208  for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++) {
1209  bool add_this_one = true;
1210  for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++) {
1211  if (m_known_kexts[old_kext] == kext_summaries[new_kext]) {
1212  // We already have this kext, don't re-load it.
1213  to_be_added[new_kext] = false;
1214  // This kext is still present, do not remove it.
1215  to_be_removed[old_kext] = false;
1216 
1217  number_of_old_kexts_being_removed--;
1218  add_this_one = false;
1219  break;
1220  }
1221  }
1222  // If this "kext" entry is actually an alias for the kernel -- the kext was
1223  // compiled into the kernel or something -- then we don't want to load the
1224  // kernel's text section at a different address. Ignore this kext entry.
1225  if (kext_summaries[new_kext].GetUUID().IsValid() &&
1226  m_kernel.GetUUID().IsValid() &&
1227  kext_summaries[new_kext].GetUUID() == m_kernel.GetUUID()) {
1228  to_be_added[new_kext] = false;
1229  break;
1230  }
1231  if (add_this_one) {
1232  number_of_new_kexts_being_added++;
1233  }
1234  }
1235 
1236  if (number_of_new_kexts_being_added == 0 &&
1237  number_of_old_kexts_being_removed == 0)
1238  return true;
1239 
1241  if (load_kexts) {
1242  if (number_of_new_kexts_being_added > 0 &&
1243  number_of_old_kexts_being_removed > 0) {
1244  s.Printf("Loading %d kext modules and unloading %d kext modules ",
1245  number_of_new_kexts_being_added,
1246  number_of_old_kexts_being_removed);
1247  } else if (number_of_new_kexts_being_added > 0) {
1248  s.Printf("Loading %d kext modules ", number_of_new_kexts_being_added);
1249  } else if (number_of_old_kexts_being_removed > 0) {
1250  s.Printf("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1251  }
1252  }
1253 
1254  if (log) {
1255  if (load_kexts) {
1256  LLDB_LOGF(log,
1257  "DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts "
1258  "added, %d kexts removed",
1259  number_of_new_kexts_being_added,
1260  number_of_old_kexts_being_removed);
1261  } else {
1262  LLDB_LOGF(log,
1263  "DynamicLoaderDarwinKernel::ParseKextSummaries kext loading is "
1264  "disabled, else would have %d kexts added, %d kexts removed",
1265  number_of_new_kexts_being_added,
1266  number_of_old_kexts_being_removed);
1267  }
1268  }
1269 
1270  // Build up a list of <kext-name, uuid> for any kexts that fail to load
1271  std::vector<std::pair<std::string, UUID>> kexts_failed_to_load;
1272  if (number_of_new_kexts_being_added > 0) {
1273  ModuleList loaded_module_list;
1274 
1275  const uint32_t num_of_new_kexts = kext_summaries.size();
1276  for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++) {
1277  if (to_be_added[new_kext]) {
1278  KextImageInfo &image_info = kext_summaries[new_kext];
1279  bool kext_successfully_added = true;
1280  if (load_kexts) {
1281  if (!image_info.LoadImageUsingMemoryModule(m_process)) {
1282  kexts_failed_to_load.push_back(std::pair<std::string, UUID>(
1283  kext_summaries[new_kext].GetName(),
1284  kext_summaries[new_kext].GetUUID()));
1285  image_info.LoadImageAtFileAddress(m_process);
1286  kext_successfully_added = false;
1287  }
1288  }
1289 
1290  m_known_kexts.push_back(image_info);
1291 
1292  if (image_info.GetModule() &&
1293  m_process->GetStopID() == image_info.GetProcessStopId())
1294  loaded_module_list.AppendIfNeeded(image_info.GetModule());
1295 
1296  if (load_kexts) {
1297  if (kext_successfully_added)
1298  s.Printf(".");
1299  else
1300  s.Printf("-");
1301  }
1302 
1303  if (log)
1304  kext_summaries[new_kext].PutToLog(log);
1305  }
1306  }
1307  m_process->GetTarget().ModulesDidLoad(loaded_module_list);
1308  }
1309 
1310  if (number_of_old_kexts_being_removed > 0) {
1311  ModuleList loaded_module_list;
1312  const uint32_t num_of_old_kexts = m_known_kexts.size();
1313  for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++) {
1314  ModuleList unloaded_module_list;
1315  if (to_be_removed[old_kext]) {
1316  KextImageInfo &image_info = m_known_kexts[old_kext];
1317  // You can't unload the kernel.
1318  if (!image_info.IsKernel()) {
1319  if (image_info.GetModule()) {
1320  unloaded_module_list.AppendIfNeeded(image_info.GetModule());
1321  }
1322  s.Printf(".");
1323  image_info.Clear();
1324  // should pull it out of the KextImageInfos vector but that would
1325  // mutate the list and invalidate the to_be_removed bool vector;
1326  // leaving it in place once Cleared() is relatively harmless.
1327  }
1328  }
1329  m_process->GetTarget().ModulesDidUnload(unloaded_module_list, false);
1330  }
1331  }
1332 
1333  if (load_kexts) {
1334  s.Printf(" done.\n");
1335  if (kexts_failed_to_load.size() > 0 && number_of_new_kexts_being_added > 0) {
1336  s.Printf("Failed to load %d of %d kexts:\n",
1337  (int)kexts_failed_to_load.size(),
1338  number_of_new_kexts_being_added);
1339  // print a sorted list of <kext-name, uuid> kexts which failed to load
1340  unsigned longest_name = 0;
1341  std::sort(kexts_failed_to_load.begin(), kexts_failed_to_load.end());
1342  for (const auto &ku : kexts_failed_to_load) {
1343  if (ku.first.size() > longest_name)
1344  longest_name = ku.first.size();
1345  }
1346  for (const auto &ku : kexts_failed_to_load) {
1347  std::string uuid;
1348  if (ku.second.IsValid())
1349  uuid = ku.second.GetAsString();
1350  s.Printf(" %-*s %s\n", longest_name, ku.first.c_str(), uuid.c_str());
1351  }
1352  }
1353  s.Flush();
1354  }
1355 
1356  return true;
1357 }
1358 
1360  const Address &kext_summary_addr, uint32_t image_infos_count,
1361  KextImageInfo::collection &image_infos) {
1362  const ByteOrder endian = m_kernel.GetByteOrder();
1363  const uint32_t addr_size = m_kernel.GetAddressByteSize();
1364 
1365  image_infos.resize(image_infos_count);
1366  const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1367  DataBufferHeap data(count, 0);
1368  Status error;
1369 
1370  const bool force_live_memory = true;
1371  const size_t bytes_read = m_process->GetTarget().ReadMemory(
1372  kext_summary_addr, data.GetBytes(), data.GetByteSize(), error, force_live_memory);
1373  if (bytes_read == count) {
1374 
1375  DataExtractor extractor(data.GetBytes(), data.GetByteSize(), endian,
1376  addr_size);
1377  uint32_t i = 0;
1378  for (uint32_t kext_summary_offset = 0;
1379  i < image_infos.size() &&
1380  extractor.ValidOffsetForDataOfSize(kext_summary_offset,
1382  ++i, kext_summary_offset += m_kext_summary_header.entry_size) {
1383  lldb::offset_t offset = kext_summary_offset;
1384  const void *name_data =
1385  extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1386  if (name_data == nullptr)
1387  break;
1388  image_infos[i].SetName((const char *)name_data);
1389  UUID uuid = UUID::fromOptionalData(extractor.GetData(&offset, 16), 16);
1390  image_infos[i].SetUUID(uuid);
1391  image_infos[i].SetLoadAddress(extractor.GetU64(&offset));
1392  image_infos[i].SetSize(extractor.GetU64(&offset));
1393  }
1394  if (i < image_infos.size())
1395  image_infos.resize(i);
1396  } else {
1397  image_infos.clear();
1398  }
1399  return image_infos.size();
1400 }
1401 
1403  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1404 
1405  if (ReadKextSummaryHeader()) {
1408  Address summary_addr(m_kext_summary_header_addr);
1409  summary_addr.Slide(m_kext_summary_header.GetSize());
1410  if (!ParseKextSummaries(summary_addr,
1412  m_known_kexts.clear();
1413  }
1414  return true;
1415  }
1416  }
1417  return false;
1418 }
1419 
1420 // Dump an image info structure to the file handle provided.
1422  if (m_load_address == LLDB_INVALID_ADDRESS) {
1423  LLDB_LOG(log, "uuid={0} name=\"{1}\" (UNLOADED)", m_uuid.GetAsString(),
1424  m_name);
1425  } else {
1426  LLDB_LOG(log, "addr={0:x+16} size={1:x+16} uuid={2} name=\"{3}\"",
1427  m_load_address, m_size, m_uuid.GetAsString(), m_name);
1428  }
1429 }
1430 
1431 // Dump the _dyld_all_image_infos members and all current image infos that we
1432 // have parsed to the file handle provided.
1434  if (log == nullptr)
1435  return;
1436 
1437  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1438  LLDB_LOGF(log,
1439  "gLoadedKextSummaries = 0x%16.16" PRIx64
1440  " { version=%u, entry_size=%u, entry_count=%u }",
1444 
1445  size_t i;
1446  const size_t count = m_known_kexts.size();
1447  if (count > 0) {
1448  log->PutCString("Loaded:");
1449  for (i = 0; i < count; i++)
1450  m_known_kexts[i].PutToLog(log);
1451  }
1452 }
1453 
1455  DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n",
1456  __FUNCTION__, StateAsCString(m_process->GetState()));
1457  Clear(true);
1458  m_process = process;
1459 }
1460 
1463  DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n",
1464  __FUNCTION__, StateAsCString(m_process->GetState()));
1465 
1466  const bool internal_bp = true;
1467  const bool hardware = false;
1468  const LazyBool skip_prologue = eLazyBoolNo;
1469  FileSpecList module_spec_list;
1470  module_spec_list.Append(m_kernel.GetModule()->GetFileSpec());
1471  Breakpoint *bp =
1472  m_process->GetTarget()
1473  .CreateBreakpoint(&module_spec_list, nullptr,
1474  "OSKextLoadedKextSummariesUpdated",
1475  eFunctionNameTypeFull, eLanguageTypeUnknown, 0,
1476  skip_prologue, internal_bp, hardware)
1477  .get();
1478 
1480  true);
1481  m_break_id = bp->GetID();
1482  }
1483 }
1484 
1485 // Member function that gets called when the process state changes.
1487  StateType state) {
1488  DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__,
1489  StateAsCString(state));
1490  switch (state) {
1491  case eStateConnected:
1492  case eStateAttaching:
1493  case eStateLaunching:
1494  case eStateInvalid:
1495  case eStateUnloaded:
1496  case eStateExited:
1497  case eStateDetached:
1498  Clear(false);
1499  break;
1500 
1501  case eStateStopped:
1502  UpdateIfNeeded();
1503  break;
1504 
1505  case eStateRunning:
1506  case eStateStepping:
1507  case eStateCrashed:
1508  case eStateSuspended:
1509  break;
1510  }
1511 }
1512 
1513 ThreadPlanSP
1515  bool stop_others) {
1516  ThreadPlanSP thread_plan_sp;
1518  LLDB_LOGF(log, "Could not find symbol for step through.");
1519  return thread_plan_sp;
1520 }
1521 
1523  Status error;
1524  error.SetErrorString(
1525  "always unsafe to load or unload shared libraries in the darwin kernel");
1526  return error;
1527 }
1528 
1530  PluginManager::RegisterPlugin(GetPluginNameStatic(),
1533 }
1534 
1536  PluginManager::UnregisterPlugin(CreateInstance);
1537 }
1538 
1540  lldb_private::Debugger &debugger) {
1541  if (!PluginManager::GetSettingForDynamicLoaderPlugin(
1543  const bool is_global_setting = true;
1544  PluginManager::CreateSettingForDynamicLoaderPlugin(
1545  debugger, GetGlobalProperties().GetValueProperties(),
1546  ConstString("Properties for the DynamicLoaderDarwinKernel plug-in."),
1547  is_global_setting);
1548  }
1549 }
1550 
1552  return "Dynamic loader plug-in that watches for shared library loads/unloads "
1553  "in the MacOSX kernel.";
1554 }
1555 
1558  switch (magic) {
1559  case llvm::MachO::MH_MAGIC:
1560  case llvm::MachO::MH_MAGIC_64:
1561  return endian::InlHostByteOrder();
1562 
1563  case llvm::MachO::MH_CIGAM:
1564  case llvm::MachO::MH_CIGAM_64:
1566  return lldb::eByteOrderLittle;
1567  else
1568  return lldb::eByteOrderBig;
1569 
1570  default:
1571  break;
1572  }
1573  return lldb::eByteOrderInvalid;
1574 }
DynamicLoaderDarwinKernel::KextImageInfo::collection
std::vector< KextImageInfo > collection
Definition: DynamicLoaderDarwinKernel.h:190
lldb_private::UUID
Definition: UUID.h:23
lldb_private::Module::GetObjectFile
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition: Module.cpp:1239
LLDB_BREAK_ID_IS_VALID
#define LLDB_BREAK_ID_IS_VALID(bid)
Definition: lldb-defines.h:39
lldb_private::DataBufferHeap::GetBytes
uint8_t * GetBytes() override
Definition: DataBufferHeap.cpp:34
lldb_private::DataBufferHeap::GetByteSize
lldb::offset_t GetByteSize() const override
Definition: DataBufferHeap.cpp:45
lldb_private::SectionList::FindSectionByName
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
Definition: Section.cpp:554
DynamicLoaderDarwinKernel::KextImageInfo::SetModule
void SetModule(lldb::ModuleSP module)
Definition: DynamicLoaderDarwinKernel.cpp:573
PlatformDarwinKernel::GetPluginNameStatic
static llvm::StringRef GetPluginNameStatic()
Definition: PlatformDarwinKernel.h:221
lldb_private::ArchSpec
Definition: ArchSpec.h:33
LIBLLDB_LOG_DYNAMIC_LOADER
#define LIBLLDB_LOG_DYNAMIC_LOADER
Definition: Logging.h:58
lldb_private::StoppointCallbackContext
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Definition: StoppointCallbackContext.h:26
DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints
static lldb::addr_t SearchForKernelWithDebugHints(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:237
DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId
uint32_t GetProcessStopId() const
Definition: DynamicLoaderDarwinKernel.cpp:606
DynamicLoaderDarwinKernel::KextImageInfo::PutToLog
void PutToLog(lldb_private::Log *log) const
Definition: DynamicLoaderDarwinKernel.cpp:1421
lldb_private::Stoppoint::GetID
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
DynamicLoaderDarwinKernel::DebuggerInitialize
static void DebuggerInitialize(lldb_private::Debugger &debugger)
Definition: DynamicLoaderDarwinKernel.cpp:1539
lldb_private::Address::Clear
void Clear()
Clear the object's state.
Definition: Address.h:178
lldb_private::Address::IsValid
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:336
DynamicLoaderDarwinKernel::OSKextLoadedKextSummaryHeader::entry_size
uint32_t entry_size
Definition: DynamicLoaderDarwinKernel.h:211
lldb_private::Process::ReadMemory
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:1925
ModuleSpec.h
DynamicLoaderDarwinKernel::m_kext_summary_header
OSKextLoadedKextSummaryHeader m_kext_summary_header
Definition: DynamicLoaderDarwinKernel.h:287
DynamicLoaderDarwinKernel::m_mutex
std::recursive_mutex m_mutex
Definition: DynamicLoaderDarwinKernel.h:289
DynamicLoaderDarwinKernel::m_kernel_load_address
lldb::addr_t m_kernel_load_address
Definition: DynamicLoaderDarwinKernel.h:282
lldb_private::Symbol
Definition: Symbol.h:20
lldb_private::Target::GetOrCreateModule
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition: Target.cpp:2038
lldb::eStateAttaching
@ eStateAttaching
Process is currently trying to attach.
Definition: lldb-enumerations.h:78
lldb_private::UUID::IsValid
bool IsValid() const
Definition: UUID.h:79
lldb_private::SectionList::GetSize
size_t GetSize() const
Definition: Section.h:74
DynamicLoaderDarwinKernel::KextImageInfo::GetUUID
lldb_private::UUID GetUUID() const
Definition: DynamicLoaderDarwinKernel.cpp:636
DynamicLoaderDarwinKernel::m_kernel
KextImageInfo m_kernel
Definition: DynamicLoaderDarwinKernel.h:283
DynamicLoaderDarwinKernel::KextImageInfo::GetModule
lldb::ModuleSP GetModule()
Definition: DynamicLoaderDarwinKernel.cpp:585
lldb_private::Process::ReadModuleFromMemory
lldb::ModuleSP ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Definition: Process.cpp:2348
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:283
lldb_private::Process
Definition: Process.h:338
lldb_private::Target::GetExecutableModulePointer
Module * GetExecutableModulePointer()
Definition: Target.cpp:1358
DynamicLoaderDarwinKernel::SearchForDarwinKernel
static lldb::addr_t SearchForDarwinKernel(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:188
DynamicLoaderDarwinKernel::m_known_kexts
KextImageInfo::collection m_known_kexts
Definition: DynamicLoaderDarwinKernel.h:288
DynamicLoaderDarwinKernel::KextImageInfo::SetName
void SetName(const char *)
Definition: DynamicLoaderDarwinKernel.cpp:624
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1344
lldb_private::Process::ClearBreakpointSiteByID
Status ClearBreakpointSiteByID(lldb::user_id_t break_id)
Definition: Process.cpp:1580
Module.h
lldb_private::Process::GetThreadList
ThreadList & GetThreadList()
Definition: Process.h:2075
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1206
lldb_private::StateAsCString
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
StoppointCallbackContext.h
lldb_private::Stream::Flush
virtual void Flush()=0
Flush the stream.
lldb_private::Debugger::GetOutputStream
StreamFile & GetOutputStream()
Definition: Debugger.h:173
lldb::eSymbolTypeData
@ eSymbolTypeData
Definition: lldb-enumerations.h:618
lldb_private::Module
Definition: Module.h:85
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
LocateSymbolFile.h
lldb_private::SectionList
Definition: Section.h:34
DynamicLoaderDarwinKernel::PrivateProcessStateChanged
void PrivateProcessStateChanged(lldb_private::Process *process, lldb::StateType state)
Definition: DynamicLoaderDarwinKernel.cpp:1486
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr
static lldb::addr_t SearchForKernelAtSameLoadAddr(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:209
DynamicLoaderDarwinKernel::KextImageInfo::GetSize
uint64_t GetSize() const
Definition: DynamicLoaderDarwinKernel.cpp:598
StackFrame.h
lldb_private::Process::IsAlive
virtual bool IsAlive()
Check if a process is still alive.
Definition: Process.cpp:1084
DynamicLoaderDarwinKernel::ParseKextSummaries
bool ParseKextSummaries(const lldb_private::Address &kext_summary_addr, uint32_t count)
Definition: DynamicLoaderDarwinKernel.cpp:1158
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Target::CreateBreakpoint
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:331
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
pc
@ pc
Definition: CompactUnwindInfo.cpp:1250
lldb_private::LazyBool
LazyBool
Definition: lldb-private-enumerations.h:115
lldb_private::Target::GetDebugger
Debugger & GetDebugger()
Definition: Target.h:1001
DynamicLoaderDarwinKernel::Clear
void Clear(bool clear_process)
Definition: DynamicLoaderDarwinKernel.cpp:545
Debugger.h
lldb_private::Target
Definition: Target.h:452
Section.h
DynamicLoaderDarwinKernelProperties
Definition: DynamicLoaderDarwinKernel.cpp:98
lldb_private::DataExtractor::GetU64
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
Definition: DataExtractor.cpp:476
DynamicLoaderDarwinKernel::CreateInstance
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
Definition: DynamicLoaderDarwinKernel.cpp:135
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:926
lldb_private::Process::GetState
lldb::StateType GetState()
Get accessor for the current process state.
Definition: Process.cpp:1294
DynamicLoaderDarwinKernelProperties::GetSettingName
static ConstString & GetSettingName()
Definition: DynamicLoaderDarwinKernel.cpp:100
lldb_private::eArchTypeMachO
@ eArchTypeMachO
Definition: lldb-private-enumerations.h:63
lldb_private::Process::ReadMemoryFromInferior
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:2034
lldb::eStateExited
@ eStateExited
Process has exited and can't be examined.
Definition: lldb-enumerations.h:90
DynamicLoaderDarwinKernel::KextImageInfo::IsKernel
bool IsKernel() const
Definition: DynamicLoaderDarwinKernel.cpp:740
DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture
lldb_private::ArchSpec GetArchitecture() const
Definition: DynamicLoaderDarwinKernel.cpp:966
eKASLRScanLowgloAddresses
@ eKASLRScanLowgloAddresses
Definition: DynamicLoaderDarwinKernel.cpp:56
lldb_private::DynamicLoader::LoadOperatingSystemPlugin
void LoadOperatingSystemPlugin(bool flush)
Definition: DynamicLoader.cpp:236
lldb_private::ArchSpec::IsCompatibleMatch
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them.
Definition: ArchSpec.cpp:934
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
Target.h
lldb_private::ModuleSpec::GetUUID
UUID & GetUUID()
Definition: ModuleSpec.h:98
lldb_private::Properties
Definition: UserSettingsController.h:33
LIBLLDB_LOG_HOST
#define LIBLLDB_LOG_HOST
Definition: Logging.h:69
lldb_private::Target::GetPlatform
lldb::PlatformSP GetPlatform()
Definition: Target.h:1373
DynamicLoaderDarwinKernelProperties::GetLoadKexts
bool GetLoadKexts() const
Definition: DynamicLoaderDarwinKernel.cpp:112
lldb_private::Section::GetFileAddress
lldb::addr_t GetFileAddress() const
Definition: Section.cpp:203
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:587
DynamicLoaderDarwinKernel::UpdateIfNeeded
void UpdateIfNeeded()
Definition: DynamicLoaderDarwinKernel.cpp:522
KASLRScanType
KASLRScanType
Definition: DynamicLoaderDarwinKernel.cpp:54
DynamicLoaderDarwinKernel::KextImageInfo::IsLoaded
bool IsLoaded()
Definition: DynamicLoaderDarwinKernel.h:142
lldb::eStateCrashed
@ eStateCrashed
Process or thread has crashed and can be examined.
Definition: lldb-enumerations.h:88
DynamicLoaderDarwinKernelProperties::GetScanType
KASLRScanType GetScanType() const
Definition: DynamicLoaderDarwinKernel.cpp:119
DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel
DynamicLoaderDarwinKernel(lldb_private::Process *process, lldb::addr_t kernel_addr)
Definition: DynamicLoaderDarwinKernel.cpp:506
lldb_private::ModuleList::Remove
bool Remove(const lldb::ModuleSP &module_sp, bool notify=true)
Remove a module from the module list.
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::ModuleList
Definition: ModuleList.h:79
Log.h
DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule
bool LoadImageUsingMemoryModule(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:748
lldb_private::eLoadDependentsNo
@ eLoadDependentsNo
Definition: Target.h:65
lldb_private::Target::SetSectionLoadAddress
bool SetSectionLoadAddress(const lldb::SectionSP &section, lldb::addr_t load_addr, bool warn_multiple=false)
Definition: Target.cpp:2892
lldb_private::Thread
Definition: Thread.h:60
lldb::eStateConnected
@ eStateConnected
Process is connected to remote debug services, but not launched or attached to anything yet.
Definition: lldb-enumerations.h:76
DynamicLoaderDarwinKernel::BreakpointHitCallback
static bool BreakpointHitCallback(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Definition: DynamicLoaderDarwinKernel.cpp:1055
lldb_private::ObjectFile::GetBaseAddress
virtual lldb_private::Address GetBaseAddress()
Returns base address of this object file.
Definition: ObjectFile.h:497
lldb::eStateLaunching
@ eStateLaunching
Process is in the process of launching.
Definition: lldb-enumerations.h:79
GetName
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition: XcodeSDK.cpp:21
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
DynamicLoaderDarwinKernel
Definition: DynamicLoaderDarwinKernel.h:24
DynamicLoaderDarwinKernel::CanLoadImage
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
Definition: DynamicLoaderDarwinKernel.cpp:1522
DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel
~DynamicLoaderDarwinKernel() override
Definition: DynamicLoaderDarwinKernel.cpp:520
OperatingSystem.h
lldb_private::Process::GetAddressByteSize
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3306
lldb_private::ConstString
Definition: ConstString.h:40
DynamicLoaderDarwinKernel.h
DynamicLoaderDarwinKernel::KextImageInfo
Definition: DynamicLoaderDarwinKernel.h:124
DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule
bool ReadMemoryModule(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:646
eKASLRScanNearPC
@ eKASLRScanNearPC
Definition: DynamicLoaderDarwinKernel.cpp:58
lldb_private::SectionList::GetSectionAtIndex
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:546
DynamicLoaderDarwinKernel::OSKextLoadedKextSummaryHeader::version
uint32_t version
Definition: DynamicLoaderDarwinKernel.h:210
DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
Definition: DynamicLoaderDarwinKernel.cpp:1514
lldb_private::DynamicLoader
Definition: DynamicLoader.h:52
lldb::eStateDetached
@ eStateDetached
Process has been detached and can't be examined.
Definition: lldb-enumerations.h:89
lldb_private::DynamicLoader::m_process
Process * m_process
The process that this dynamic loader plug-in is tracking.
Definition: DynamicLoader.h:313
DynamicLoaderDarwinKernel::KextImageInfo::GetName
std::string GetName() const
Definition: DynamicLoaderDarwinKernel.cpp:628
DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress
bool LoadImageAtFileAddress(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:560
DynamicLoaderDarwinKernel::PrivateInitialize
void PrivateInitialize(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:1454
lldb_private::Target::SetArchitecture
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false)
Set the architecture for this target.
Definition: Target.cpp:1459
lldb_private::Debugger
Definition: Debugger.h:70
ThreadPlanRunToAddress.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress
void SetLoadAddress(lldb::addr_t load_addr)
Definition: DynamicLoaderDarwinKernel.cpp:589
DynamicLoaderDarwinKernel::OSKextLoadedKextSummaryHeader::entry_count
uint32_t entry_count
Definition: DynamicLoaderDarwinKernel.h:212
DynamicLoaderDarwinKernel::KextImageInfo::SetUUID
void SetUUID(const lldb_private::UUID &uuid)
Definition: DynamicLoaderDarwinKernel.cpp:632
Thread.h
lldb::eStateUnloaded
@ eStateUnloaded
Process is object is valid, but not currently loaded.
Definition: lldb-enumerations.h:75
DynamicLoaderDarwinKernel::ReadMachHeader
static bool ReadMachHeader(lldb::addr_t addr, lldb_private::Process *process, llvm::MachO::mach_header &mh, bool *read_error=nullptr)
Definition: DynamicLoaderDarwinKernel.cpp:391
OptionValueProperties.h
DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded
void SetNotificationBreakpointIfNeeded()
Definition: DynamicLoaderDarwinKernel.cpp:1461
lldb_private::ArchSpec::GetAddressByteSize
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:683
DynamicLoaderDarwinKernel::GetByteOrderFromMagic
static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic)
Definition: DynamicLoaderDarwinKernel.cpp:1557
ObjectFile.h
lldb_private::Target::SetExecutableModule
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
Definition: Target.cpp:1393
DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId
void SetProcessStopId(uint32_t stop_id)
Definition: DynamicLoaderDarwinKernel.cpp:610
lldb_private::Section
Definition: Section.h:102
DynamicLoaderDarwinKernel::ReadKextSummaries
uint32_t ReadKextSummaries(const lldb_private::Address &kext_summary_addr, uint32_t image_infos_count, KextImageInfo::collection &image_infos)
Definition: DynamicLoaderDarwinKernel.cpp:1359
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel
void SetIsKernel(bool is_kernel)
Definition: DynamicLoaderDarwinKernel.cpp:744
lldb_private::ModuleList::FindModule
lldb::ModuleSP FindModule(const Module *module_ptr) const
Definition: ModuleList.cpp:524
DynamicLoaderDarwinKernel::KextImageInfo::Clear
void Clear()
Definition: DynamicLoaderDarwinKernel.h:128
StreamFile.h
lldb::eStateStepping
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
Definition: lldb-enumerations.h:86
DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress
static lldb_private::UUID CheckForKernelImageAtAddress(lldb::addr_t addr, lldb_private::Process *process, bool *read_error=nullptr)
Definition: DynamicLoaderDarwinKernel.cpp:434
DynamicLoaderDarwinKernel::KextImageInfo::SetSize
void SetSize(uint64_t size)
Definition: DynamicLoaderDarwinKernel.cpp:602
lldb::eStateSuspended
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
Definition: lldb-enumerations.h:91
eKASLRScanExhaustiveScan
@ eKASLRScanExhaustiveScan
Definition: DynamicLoaderDarwinKernel.cpp:60
lldb_private::ModuleSpec::GetFileSpec
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:52
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:968
DynamicLoaderDarwinKernel::KERNEL_MODULE_MAX_NAME
@ KERNEL_MODULE_MAX_NAME
Definition: DynamicLoaderDarwinKernel.h:89
lldb_private::Status
Definition: Status.h:44
lldb_private::Target::ReadMemory
size_t ReadMemory(const Address &addr, void *dst, size_t dst_len, Status &error, bool force_live_memory=false, lldb::addr_t *load_addr_ptr=nullptr)
Definition: Target.cpp:1718
lldb::eByteOrderInvalid
@ eByteOrderInvalid
Definition: lldb-enumerations.h:139
lldb::eLanguageTypeUnknown
@ eLanguageTypeUnknown
Unknown or invalid language value.
Definition: lldb-enumerations.h:437
DynamicLoaderDarwinKernel::Terminate
static void Terminate()
Definition: DynamicLoaderDarwinKernel.cpp:1535
uint32_t
DynamicLoaderDarwinKernel::OSKextLoadedKextSummaryHeader::GetSize
uint32_t GetSize()
Definition: DynamicLoaderDarwinKernel.h:217
lldb_private::eLazyBoolNo
@ eLazyBoolNo
Definition: lldb-private-enumerations.h:115
DynamicLoaderDarwinKernel::BreakpointHit
bool BreakpointHit(lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Definition: DynamicLoaderDarwinKernel.cpp:1062
PlatformDarwinKernel.h
lldb_private::Address
Definition: Address.h:59
lldb_private::Target::ModulesDidLoad
void ModulesDidLoad(ModuleList &module_list)
Definition: Target.cpp:1610
DEBUG_PRINTF
#define DEBUG_PRINTF(fmt,...)
Definition: DynamicLoaderDarwinKernel.cpp:41
DynamicLoaderDarwinKernel::KextImageInfo::m_module_sp
lldb::ModuleSP m_module_sp
Definition: DynamicLoaderDarwinKernel.h:196
lldb_private::ObjectFile::GetStrata
Strata GetStrata()
Definition: ObjectFile.h:643
eKASLRScanNone
@ eKASLRScanNone
Definition: DynamicLoaderDarwinKernel.cpp:55
lldb_private::endian::InlHostByteOrder
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
DynamicLoaderDarwinKernel::PutToLog
void PutToLog(lldb_private::Log *log) const
Definition: DynamicLoaderDarwinKernel.cpp:1433
lldb_private::Breakpoint::SetCallback
void SetCallback(BreakpointHitCallback callback, void *baton, bool is_synchronous=false)
Set the callback action invoked when the breakpoint is hit.
Definition: Breakpoint.cpp:412
lldb_private::ModuleList::AppendIfNeeded
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
DynamicLoaderDarwinKernel::ReadAllKextSummaries
bool ReadAllKextSummaries()
Definition: DynamicLoaderDarwinKernel.cpp:1402
DynamicLoaderDarwinKernel::m_kext_summary_header_ptr_addr
lldb_private::Address m_kext_summary_header_ptr_addr
Definition: DynamicLoaderDarwinKernel.h:285
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:310
DynamicLoaderDarwinKernel::ReadKextSummaryHeader
bool ReadKextSummaryHeader()
Definition: DynamicLoaderDarwinKernel.cpp:1076
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(LLDBLog mask)
Definition: Logging.cpp:83
lldb_private::DynamicLoader::GetStopWhenImagesChange
bool GetStopWhenImagesChange() const
Get whether the process should stop when images change.
Definition: DynamicLoader.cpp:64
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
DynamicLoaderDarwinKernelProperties::DynamicLoaderDarwinKernelProperties
DynamicLoaderDarwinKernelProperties()
Definition: DynamicLoaderDarwinKernel.cpp:105
if
if(APPLE AND LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY) remove_module_flags() endif() macro(add_host_subdirectory group) list(APPEND HOST_SOURCES $
Definition: Host/CMakeLists.txt:1
lldb_private::GetLogIfAnyCategoriesSet
Log * GetLogIfAnyCategoriesSet(LLDBLog mask)
Definition: Logging.cpp:87
DynamicLoaderDarwinKernel::GetPluginNameStatic
static llvm::StringRef GetPluginNameStatic()
Definition: DynamicLoaderDarwinKernel.h:36
lldb_private::DataExtractor::GetU32
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:427
DynamicLoaderDarwinKernel::KextImageInfo::m_load_process_stop_id
uint32_t m_load_process_stop_id
Definition: DynamicLoaderDarwinKernel.h:198
PluginManager.h
DynamicLoaderDarwinKernel::m_kext_summary_header_addr
lldb_private::Address m_kext_summary_header_addr
Definition: DynamicLoaderDarwinKernel.h:286
DynamicLoaderDarwinKernel::SearchForKernelNearPC
static lldb::addr_t SearchForKernelNearPC(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:289
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
lldb_private::ModuleSpec::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:88
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:276
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
DynamicLoaderDarwinKernel::KextImageInfo::operator==
bool operator==(const KextImageInfo &rhs)
Definition: DynamicLoaderDarwinKernel.cpp:616
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize
uint32_t GetAddressByteSize()
Definition: DynamicLoaderDarwinKernel.cpp:949
lldb_private::Process::GetImageInfoAddress
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
Definition: Process.cpp:1484
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
DynamicLoaderDarwinKernel::Initialize
static void Initialize()
Definition: DynamicLoaderDarwinKernel.cpp:1529
LIBLLDB_LOG_STEP
#define LIBLLDB_LOG_STEP
Definition: Logging.h:62
lldb::eByteOrderBig
@ eByteOrderBig
Definition: lldb-enumerations.h:140
lldb::eStateInvalid
@ eStateInvalid
Definition: lldb-enumerations.h:74
GetGlobalProperties
static DynamicLoaderDarwinKernelProperties & GetGlobalProperties()
Definition: DynamicLoaderDarwinKernel.cpp:127
DynamicLoaderDarwinKernel::GetPluginDescriptionStatic
static llvm::StringRef GetPluginDescriptionStatic()
Definition: DynamicLoaderDarwinKernel.cpp:1551
lldb_private::DataExtractor::GetData
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
Definition: DataExtractor.h:337
State.h
DynamicLoaderDarwinKernel::DidAttach
void DidAttach() override
Called after attaching a process.
Definition: DynamicLoaderDarwinKernel.cpp:530
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Process::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3302
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:48
DynamicLoaderDarwinKernel::KERNEL_MODULE_ENTRY_SIZE_VERSION_1
@ KERNEL_MODULE_ENTRY_SIZE_VERSION_1
Definition: DynamicLoaderDarwinKernel.h:94
lldb_private::Module::GetUUID
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:349
lldb_private::Target::ModulesDidUnload
void ModulesDidUnload(ModuleList &module_list, bool delete_locations)
Definition: Target.cpp:1642
lldb_private::Target::SetPlatform
void SetPlatform(const lldb::PlatformSP &platform_sp)
Definition: Target.h:1375
lldb_private::Process::GetStopID
uint32_t GetStopID() const
Definition: Process.h:1365
lldb_private::ThreadList::GetSelectedThread
lldb::ThreadSP GetSelectedThread()
Definition: ThreadList.cpp:648
DataBuffer.h
lldb::eByteOrderLittle
@ eByteOrderLittle
Definition: lldb-enumerations.h:142
lldb_private::DataBufferHeap
Definition: DataBufferHeap.h:30
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:440
lldb_private::Process::SetCanRunCode
void SetCanRunCode(bool can_run_code)
Sets whether executing code in this process is possible.
Definition: Process.cpp:2323
lldb_private::DataExtractor::ValidOffsetForDataOfSize
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
Definition: DataExtractor.h:965
lldb
Definition: SBAddress.h:15
DynamicLoaderDarwinKernel::DidLaunch
void DidLaunch() override
Called after attaching a process.
Definition: DynamicLoaderDarwinKernel.cpp:539
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
RegisterContext.h
DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder
lldb::ByteOrder GetByteOrder()
Definition: DynamicLoaderDarwinKernel.cpp:957
g_kaslr_kernel_scan_enum_values
static constexpr OptionEnumValueElement g_kaslr_kernel_scan_enum_values[]
Definition: DynamicLoaderDarwinKernel.cpp:64
lldb_private::ObjectFile
Definition: ObjectFile.h:60
DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress
lldb::addr_t GetLoadAddress() const
Definition: DynamicLoaderDarwinKernel.cpp:594
lldb_private::TargetProperties::GetExecutableSearchPaths
FileSpecList GetExecutableSearchPaths()
Definition: Target.cpp:4101
lldb_private::ModuleList::Append
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
DataBufferHeap.h
UINT64_MAX
#define UINT64_MAX
Definition: lldb-defines.h:23
lldb_private::Breakpoint
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:80
DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded
void LoadKernelModuleIfNeeded()
Definition: DynamicLoaderDarwinKernel.cpp:977
DynamicLoaderDarwinKernel::m_break_id
lldb::user_id_t m_break_id
Definition: DynamicLoaderDarwinKernel.h:290
lldb_private::ObjectFile::GetType
Type GetType()
Definition: ObjectFile.h:637
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch
static lldb::addr_t SearchForKernelViaExhaustiveSearch(lldb_private::Process *process)
Definition: DynamicLoaderDarwinKernel.cpp:346
lldb_private::Log::PutCString
void PutCString(const char *cstr)
Definition: Log.cpp:117
lldb_private::Target::ReadPointerFromMemory
bool ReadPointerFromMemory(const Address &addr, Status &error, Address &pointer_addr, bool force_live_memory=false)
Definition: Target.cpp:2008
lldb_private::Symbol::GetAddress
Address GetAddress() const
Definition: Symbol.h:73