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