blob: 6ac210acf6d62e3a183be6958d1fdc36584bb717 [file] [log] [blame]
Greg Clayton944b8282011-08-22 22:30:57 +00001//===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -*-===//
Greg Clayton7b242382011-07-08 00:48:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Ingham46d005d2014-04-02 22:53:21 +000010#include "lldb/Utility/SafeMachO.h"
Greg Claytoneadcca92014-04-02 20:36:22 +000011
Greg Clayton7b242382011-07-08 00:48:09 +000012#include "lldb/Breakpoint/StoppointCallbackContext.h"
13#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000015#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Greg Clayton7b242382011-07-08 00:48:09 +000019#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Clayton7b242382011-07-08 00:48:09 +000021#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000022#include "lldb/Core/StreamFile.h"
Jason Molenda68b36072012-10-02 03:49:41 +000023#include "lldb/Host/Symbols.h"
Ilia K41204d02015-03-04 12:05:24 +000024#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton7b242382011-07-08 00:48:09 +000025#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000026#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000027#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000028#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlanRunToAddress.h"
Greg Clayton57abc5d2013-05-10 21:47:16 +000031#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000032
Greg Clayton944b8282011-08-22 22:30:57 +000033#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000034
35//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
36#ifdef ENABLE_DEBUG_PRINTF
37#include <stdio.h>
38#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
39#else
40#define DEBUG_PRINTF(fmt, ...)
41#endif
42
43using namespace lldb;
44using namespace lldb_private;
45
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000046// Progressively greater amounts of scanning we will allow
47// For some targets very early in startup, we can't do any random reads of memory or we can crash the device
48// so a setting is needed that can completely disable the KASLR scans.
49
50enum KASLRScanType
51{
52 eKASLRScanNone = 0, // No reading into the inferior at all
53 eKASLRScanLowgloAddresses, // Check one word of memory for a possible kernel addr, then see if a kernel is there
Jason Molenda63969222013-01-30 04:48:16 +000054 eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000055 eKASLRScanExhaustiveScan // Scan through the entire possible kernel address range looking for a kernel
56};
57
58OptionEnumValueElement
59g_kaslr_kernel_scan_enum_values[] =
60{
61 { eKASLRScanNone, "none", "Do not read memory looking for a Darwin kernel when attaching." },
62 { eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
63 { eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find the Darwin kernel's load address."},
64 { eKASLRScanExhaustiveScan, "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
65 { 0, NULL, NULL }
66};
67
Greg Claytone8cd0c92012-10-19 18:02:49 +000068static PropertyDefinition
69g_properties[] =
70{
Greg Clayton66763ee2012-10-19 20:53:18 +000071 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000072 { "scan-type", OptionValue::eTypeEnum, true, eKASLRScanNearPC, NULL, g_kaslr_kernel_scan_enum_values, "Control how many reads lldb will make while searching for a Darwin kernel on attach." },
Greg Clayton66763ee2012-10-19 20:53:18 +000073 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000074};
75
76enum {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000077 ePropertyLoadKexts,
78 ePropertyScanType
Greg Claytone8cd0c92012-10-19 18:02:49 +000079};
80
81class DynamicLoaderDarwinKernelProperties : public Properties
82{
83public:
84
85 static ConstString &
86 GetSettingName ()
87 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000088 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000089 return g_setting_name;
90 }
91
92 DynamicLoaderDarwinKernelProperties() :
93 Properties ()
94 {
95 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
96 m_collection_sp->Initialize(g_properties);
97 }
98
99 virtual
100 ~DynamicLoaderDarwinKernelProperties()
101 {
102 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000103
Greg Claytone8cd0c92012-10-19 18:02:49 +0000104 bool
Greg Clayton66763ee2012-10-19 20:53:18 +0000105 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +0000106 {
Greg Clayton66763ee2012-10-19 20:53:18 +0000107 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000108 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
109 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000110
111 KASLRScanType
112 GetScanType() const
113 {
114 const uint32_t idx = ePropertyScanType;
Jason Molenda63969222013-01-30 04:48:16 +0000115 return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000116 }
117
118
Greg Claytone8cd0c92012-10-19 18:02:49 +0000119};
120
Greg Clayton7b0992d2013-04-18 22:45:39 +0000121typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000122
123static const DynamicLoaderDarwinKernelPropertiesSP &
124GetGlobalProperties()
125{
126 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
127 if (!g_settings_sp)
128 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
129 return g_settings_sp;
130}
131
Greg Clayton7b242382011-07-08 00:48:09 +0000132//----------------------------------------------------------------------
133// Create an instance of this class. This function is filled into
134// the plugin info class that gets handed out by the plugin factory and
135// allows the lldb to instantiate an instance of this class.
136//----------------------------------------------------------------------
137DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000138DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000139{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000140 if (!force)
Greg Clayton7b242382011-07-08 00:48:09 +0000141 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000142 // If the user provided an executable binary and it is not a kernel,
143 // this plugin should not create an instance.
Greg Claytonaa149cb2011-08-11 02:48:45 +0000144 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000145 if (exe_module)
146 {
147 ObjectFile *object_file = exe_module->GetObjectFile();
148 if (object_file)
149 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000150 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
151 {
152 return NULL;
153 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000154 }
155 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000156
157 // If the target's architecture does not look like an Apple environment,
158 // this plugin should not create an instance.
159 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
160 switch (triple_ref.getOS())
Greg Claytondf0b7d52011-07-08 04:11:42 +0000161 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000162 case llvm::Triple::Darwin:
163 case llvm::Triple::MacOSX:
164 case llvm::Triple::IOS:
Jason Molendaa814f702015-11-05 23:03:44 +0000165 case llvm::Triple::TvOS:
166 case llvm::Triple::WatchOS:
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000167 if (triple_ref.getVendor() != llvm::Triple::Apple)
168 {
169 return NULL;
170 }
171 break;
172 // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
173 case llvm::Triple::UnknownOS:
174 break;
175 default:
176 return NULL;
177 break;
178 }
179 }
180
181 // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
182 // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
183
Jason Molenda503d0182013-03-02 07:19:32 +0000184 addr_t kernel_load_address = SearchForDarwinKernel (process);
185 if (kernel_load_address != LLDB_INVALID_ADDRESS)
186 {
Ewan Crawford90ff7912015-07-14 10:56:58 +0000187 process->SetCanRunCode(false);
Jason Molenda503d0182013-03-02 07:19:32 +0000188 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
189 }
190 return NULL;
191}
192
193lldb::addr_t
194DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
195{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000196 addr_t kernel_load_address = process->GetImageInfoAddress();
197 if (kernel_load_address == LLDB_INVALID_ADDRESS)
198 {
199 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
200 if (kernel_load_address == LLDB_INVALID_ADDRESS)
201 {
202 kernel_load_address = SearchForKernelWithDebugHints (process);
203 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000204 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000205 kernel_load_address = SearchForKernelNearPC (process);
206 if (kernel_load_address == LLDB_INVALID_ADDRESS)
207 {
208 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
209 }
Greg Clayton70512312012-05-08 01:45:38 +0000210 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000211 }
Greg Clayton7b242382011-07-08 00:48:09 +0000212 }
Jason Molenda503d0182013-03-02 07:19:32 +0000213 return kernel_load_address;
Greg Clayton7b242382011-07-08 00:48:09 +0000214}
215
216//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000217// Check if the kernel binary is loaded in memory without a slide.
218// First verify that the ExecutableModule is a kernel before we proceed.
219// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
220//----------------------------------------------------------------------
221lldb::addr_t
222DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
223{
224 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
225 if (exe_module == NULL)
226 return LLDB_INVALID_ADDRESS;
227
228 ObjectFile *exe_objfile = exe_module->GetObjectFile();
229 if (exe_objfile == NULL)
230 return LLDB_INVALID_ADDRESS;
231
232 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
233 return LLDB_INVALID_ADDRESS;
234
235 if (!exe_objfile->GetHeaderAddress().IsValid())
236 return LLDB_INVALID_ADDRESS;
237
238 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
239 return exe_objfile->GetHeaderAddress().GetFileAddress();
240
241 return LLDB_INVALID_ADDRESS;
242}
243
244//----------------------------------------------------------------------
245// If the debug flag is included in the boot-args nvram setting, the kernel's load address
246// will be noted in the lowglo page at a fixed address
247// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
248//----------------------------------------------------------------------
249lldb::addr_t
250DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
251{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000252 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
253 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000254
255 Error read_err;
256 addr_t addr = LLDB_INVALID_ADDRESS;
257 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
258 {
259 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
Jason Molenda7dd29392014-10-06 22:23:30 +0000260 if (CheckForKernelImageAtAddress (addr, process).IsValid())
261 {
262 return addr;
263 }
264 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000004010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
265 if (CheckForKernelImageAtAddress (addr, process).IsValid())
266 {
267 return addr;
268 }
Jason Molendaec504232016-02-05 01:38:56 +0000269 addr = process->ReadUnsignedIntegerFromMemory (0xfffffff000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
270 if (CheckForKernelImageAtAddress (addr, process).IsValid())
271 {
272 return addr;
273 }
274 addr = process->ReadUnsignedIntegerFromMemory (0xfffffff000004010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
275 if (CheckForKernelImageAtAddress (addr, process).IsValid())
276 {
277 return addr;
278 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000279 }
280 else
281 {
282 addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000283 if (CheckForKernelImageAtAddress (addr, process).IsValid())
Jason Molenda7dd29392014-10-06 22:23:30 +0000284 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000285 return addr;
Jason Molenda7dd29392014-10-06 22:23:30 +0000286 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000287 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000288 return LLDB_INVALID_ADDRESS;
289}
290
291//----------------------------------------------------------------------
292// If the kernel is currently executing when lldb attaches, and we don't have
293// a better way of finding the kernel's load address, try searching backwards
294// from the current pc value looking for the kernel's Mach header in memory.
295// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
296//----------------------------------------------------------------------
297lldb::addr_t
298DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
299{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000300 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
301 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
302 {
303 return LLDB_INVALID_ADDRESS;
304 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000305
306 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
307 if (thread.get() == NULL)
308 return LLDB_INVALID_ADDRESS;
309 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
310
311 if (pc == LLDB_INVALID_ADDRESS)
312 return LLDB_INVALID_ADDRESS;
313
Jason Molenda44edbf12013-04-19 00:50:28 +0000314 addr_t kernel_range_low;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000315 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
316 {
317 kernel_range_low = 1ULL << 63;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000318 }
319 else
320 {
321 kernel_range_low = 1ULL << 31;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000322 }
323
324 // Outside the normal kernel address range, this is probably userland code running right now
325 if (pc < kernel_range_low)
Jason Molenda44edbf12013-04-19 00:50:28 +0000326 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000327
328 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
329 // an offset of one page (0x1000) or two, depending on the device.
330
331 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
332 addr_t addr = pc & ~0xfffff;
333
334 int i = 0;
335 while (i < 32 && pc >= kernel_range_low)
336 {
337 if (CheckForKernelImageAtAddress (addr, process).IsValid())
338 return addr;
339 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
340 return addr + 0x1000;
341 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
342 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000343 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
344 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000345 i++;
346 addr -= 0x100000;
347 }
348
349 return LLDB_INVALID_ADDRESS;
350}
351
352//----------------------------------------------------------------------
353// Scan through the valid address range for a kernel binary.
354// This is uselessly slow in 64-bit environments so we don't even try it.
355// This scan is not enabled by default even for 32-bit targets.
356// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
357//----------------------------------------------------------------------
358lldb::addr_t
359DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
360{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000361 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
362 {
363 return LLDB_INVALID_ADDRESS;
364 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000365
366 addr_t kernel_range_low, kernel_range_high;
367 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
368 {
369 kernel_range_low = 1ULL << 63;
370 kernel_range_high = UINT64_MAX;
371 }
372 else
373 {
374 kernel_range_low = 1ULL << 31;
375 kernel_range_high = UINT32_MAX;
376 }
377
378 // Stepping through memory at one-megabyte resolution looking for a kernel
379 // rarely works (fast enough) with a 64-bit address space -- for now, let's
380 // not even bother. We may be attaching to something which *isn't* a kernel
381 // and we don't want to spin for minutes on-end looking for a kernel.
382 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
383 return LLDB_INVALID_ADDRESS;
384
385 addr_t addr = kernel_range_low;
386
387 while (addr >= kernel_range_low && addr < kernel_range_high)
388 {
389 if (CheckForKernelImageAtAddress (addr, process).IsValid())
390 return addr;
391 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
392 return addr + 0x1000;
393 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
394 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000395 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
396 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000397 addr += 0x100000;
398 }
399 return LLDB_INVALID_ADDRESS;
400}
401
402//----------------------------------------------------------------------
403// Given an address in memory, look to see if there is a kernel image at that
404// address.
405// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
406//----------------------------------------------------------------------
407lldb_private::UUID
408DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
409{
Jason Molendaec504232016-02-05 01:38:56 +0000410 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000411 if (addr == LLDB_INVALID_ADDRESS)
412 return UUID();
413
Jason Molendaec504232016-02-05 01:38:56 +0000414 if (log)
415 log->Printf ("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: looking for kernel binary at 0x%" PRIx64, addr);
416
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000417 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
418 // (the first field of the mach_header/mach_header_64 struct).
419
420 Error read_error;
421 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
Charles Davis510938e2013-08-27 05:04:57 +0000422 if (result != llvm::MachO::MH_MAGIC_64
423 && result != llvm::MachO::MH_MAGIC
424 && result != llvm::MachO::MH_CIGAM
425 && result != llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000426 {
427 return UUID();
428 }
429
430 // Read the mach header and see whether it looks like a kernel
431 llvm::MachO::mach_header header;
432 if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
433 return UUID();
434
Charles Davis510938e2013-08-27 05:04:57 +0000435 if (header.magic == llvm::MachO::MH_CIGAM ||
436 header.magic == llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000437 {
438 header.magic = llvm::ByteSwap_32(header.magic);
439 header.cputype = llvm::ByteSwap_32(header.cputype);
440 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
441 header.filetype = llvm::ByteSwap_32(header.filetype);
442 header.ncmds = llvm::ByteSwap_32(header.ncmds);
443 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
444 header.flags = llvm::ByteSwap_32(header.flags);
445 }
446
447 // A kernel is an executable which does not have the dynamic link object flag set.
Charles Davis510938e2013-08-27 05:04:57 +0000448 if (header.filetype == llvm::MachO::MH_EXECUTE
449 && (header.flags & llvm::MachO::MH_DYLDLINK) == 0)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000450 {
451 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000452 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000453 if (!memory_module_sp.get())
454 return UUID();
455
456 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
457 if (exe_objfile == NULL)
458 return UUID();
459
460 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
461 {
Jason Molendaa4ce2532013-05-02 22:02:57 +0000462 ArchSpec kernel_arch (eArchTypeMachO, header.cputype, header.cpusubtype);
463 if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
464 {
465 process->GetTarget().SetArchitecture (kernel_arch);
466 }
Jason Molendaec504232016-02-05 01:38:56 +0000467 if (log)
468 log->Printf ("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: kernel binary image found at 0x%" PRIx64, addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000469 return memory_module_sp->GetUUID();
470 }
471 }
472
473 return UUID();
474}
475
476//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000477// Constructor
478//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000479DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000480 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000481 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000482 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000483 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000484 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000485 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000486 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000487 m_mutex(Mutex::eMutexTypeRecursive),
488 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000489{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000490 Error error;
491 PlatformSP platform_sp(Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error));
Jason Molenda1c627542013-04-05 01:03:25 +0000492 // Only select the darwin-kernel Platform if we've been asked to load kexts.
493 // It can take some time to scan over all of the kext info.plists and that
494 // shouldn't be done if kext loading is explicitly disabled.
495 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
496 {
497 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000498 }
Greg Clayton7b242382011-07-08 00:48:09 +0000499}
500
501//----------------------------------------------------------------------
502// Destructor
503//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000504DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000505{
506 Clear(true);
507}
508
Greg Clayton374972e2011-07-09 17:15:55 +0000509void
Greg Clayton944b8282011-08-22 22:30:57 +0000510DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000511{
512 LoadKernelModuleIfNeeded();
513 SetNotificationBreakpointIfNeeded ();
514}
Greg Clayton7b242382011-07-08 00:48:09 +0000515//------------------------------------------------------------------
516/// Called after attaching a process.
517///
518/// Allow DynamicLoader plug-ins to execute some code after
519/// attaching to a process.
520//------------------------------------------------------------------
521void
Greg Clayton944b8282011-08-22 22:30:57 +0000522DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000523{
524 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000525 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000526}
527
528//------------------------------------------------------------------
529/// Called after attaching a process.
530///
531/// Allow DynamicLoader plug-ins to execute some code after
532/// attaching to a process.
533//------------------------------------------------------------------
534void
Greg Clayton944b8282011-08-22 22:30:57 +0000535DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000536{
537 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000538 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000539}
540
541
542//----------------------------------------------------------------------
543// Clear out the state of this class.
544//----------------------------------------------------------------------
545void
Greg Clayton944b8282011-08-22 22:30:57 +0000546DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000547{
548 Mutex::Locker locker(m_mutex);
549
550 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
551 m_process->ClearBreakpointSiteByID(m_break_id);
552
553 if (clear_process)
554 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000555 m_kernel.Clear();
556 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000557 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000558 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000559 m_break_id = LLDB_INVALID_BREAK_ID;
560}
561
Greg Clayton7b242382011-07-08 00:48:09 +0000562
Greg Claytonc859e2d2012-02-13 23:10:39 +0000563bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000564DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000565{
566 if (IsLoaded())
567 return true;
568
Jason Molenda306bd0a2013-02-19 05:42:46 +0000569 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000570 {
571 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +0000572 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
Jason Molenda306bd0a2013-02-19 05:42:46 +0000573 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000574 }
575 return false;
576}
577
Jason Molenda306bd0a2013-02-19 05:42:46 +0000578void
579DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
580{
581 m_module_sp = module_sp;
582 if (module_sp.get() && module_sp->GetObjectFile())
583 {
584 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
585 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
586 {
587 m_kernel_image = true;
588 }
589 else
590 {
591 m_kernel_image = false;
592 }
593 }
594}
595
596ModuleSP
597DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
598{
599 return m_module_sp;
600}
601
602void
603DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
604{
605 m_load_address = load_addr;
606}
607
608addr_t
609DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
610{
611 return m_load_address;
612}
613
614uint64_t
615DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
616{
617 return m_size;
618}
619
620void
621DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
622{
623 m_size = size;
624}
625
626uint32_t
627DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
628{
629 return m_load_process_stop_id;
630}
631
632void
633DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
634{
635 m_load_process_stop_id = stop_id;
636}
637
Greg Clayton2af282a2012-03-21 04:25:00 +0000638bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000639DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
640{
641 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
642 {
643 if (m_uuid == rhs.GetUUID())
644 {
645 return true;
646 }
647 return false;
648 }
649
650 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
651 return true;
652
653 return false;
654}
655
656void
657DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
658{
659 m_name = name;
660}
661
662std::string
663DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
664{
665 return m_name;
666}
667
668void
669DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
670{
671 m_uuid = uuid;
672}
673
674UUID
675DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
676{
677 return m_uuid;
678}
679
680// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
681// Module at that address. Require that the MemoryModule have a matching UUID and detect
682// if this MemoryModule is a kernel or a kext.
683//
684// Returns true if m_memory_module_sp is now set to a valid Module.
685
686bool
687DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
688{
Jason Molenda08a32582015-07-25 02:39:42 +0000689 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000690 if (m_memory_module_sp.get() != NULL)
691 return true;
692 if (m_load_address == LLDB_INVALID_ADDRESS)
693 return false;
694
695 FileSpec file_spec;
696 file_spec.SetFile (m_name.c_str(), false);
697
698 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
699
700 if (memory_module_sp.get() == NULL)
701 return false;
702
703 bool is_kernel = false;
704 if (memory_module_sp->GetObjectFile())
705 {
706 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
707 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
708 {
709 is_kernel = true;
710 }
711 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
712 {
713 is_kernel = false;
714 }
715 }
716
Jason Molenda38e70d12013-02-27 03:07:49 +0000717 // If this is a kext, and the kernel specified what UUID we should find at this
718 // load address, require that the memory module have a matching UUID or something
719 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000720 if (m_uuid.IsValid())
721 {
722 if (m_uuid != memory_module_sp->GetUUID())
723 {
Jason Molenda08a32582015-07-25 02:39:42 +0000724 if (log)
725 {
726 log->Printf ("KextImageInfo::ReadMemoryModule the kernel said to find uuid %s at 0x%" PRIx64 " but instead we found uuid %s, throwing it away", m_uuid.GetAsString().c_str(), m_load_address, memory_module_sp->GetUUID().GetAsString().c_str());
727 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000728 return false;
729 }
730 }
731
732 // If the in-memory Module has a UUID, let's use that.
733 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
734 {
735 m_uuid = memory_module_sp->GetUUID();
736 }
737
738 m_memory_module_sp = memory_module_sp;
739 m_kernel_image = is_kernel;
740 if (is_kernel)
741 {
Jason Molenda08a32582015-07-25 02:39:42 +0000742 if (log)
743 {
744 // This is unusual and probably not intended
745 log->Printf ("KextImageInfo::ReadMemoryModule read the kernel binary out of memory");
746 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000747 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000748 {
749 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
750 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000751 if (m_uuid.IsValid())
752 {
Jason Molenda48975832015-03-10 23:34:52 +0000753 ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule();
754 if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid())
Jason Molenda38e70d12013-02-27 03:07:49 +0000755 {
Jason Molenda48975832015-03-10 23:34:52 +0000756 if (m_uuid != exe_module_sp->GetUUID())
Jason Molenda38e70d12013-02-27 03:07:49 +0000757 {
Jason Molenda48975832015-03-10 23:34:52 +0000758 // The user specified a kernel binary that has a different UUID than
759 // the kernel actually running in memory. This never ends well;
760 // clear the user specified kernel binary from the Target.
761
762 m_module_sp.reset();
763
764 ModuleList user_specified_kernel_list;
765 user_specified_kernel_list.Append (exe_module_sp);
766 process->GetTarget().GetImages().Remove (user_specified_kernel_list);
Jason Molenda38e70d12013-02-27 03:07:49 +0000767 }
768 }
769 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000770 }
771
772 return true;
773}
774
775bool
776DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
777{
778 return m_kernel_image == true;
779}
780
781void
782DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
783{
784 m_kernel_image = is_kernel;
785}
786
787bool
788DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000789{
790 if (IsLoaded())
791 return true;
792
Greg Claytonc859e2d2012-02-13 23:10:39 +0000793
794 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000795
Jason Molenda306bd0a2013-02-19 05:42:46 +0000796 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
797 // have the correct segment load addresses.
798 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000799 {
800 return false;
801 }
802
Jason Molenda306bd0a2013-02-19 05:42:46 +0000803 bool uuid_is_valid = m_uuid.IsValid();
804
Jason Molendae575e7b2013-02-19 06:11:13 +0000805 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
806 {
Greg Clayton44d93782014-01-27 23:43:24 +0000807 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000808 if (s)
809 {
Jason Molendac16b4af2013-05-03 23:56:12 +0000810 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
Jason Molendae575e7b2013-02-19 06:11:13 +0000811 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
812 }
813 }
814
Jason Molenda306bd0a2013-02-19 05:42:46 +0000815 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000816 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000817 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
818 const ModuleList &target_images = target.GetImages();
819 m_module_sp = target_images.FindModule(m_uuid);
820
821 // Search for the kext on the local filesystem via the UUID
822 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000823 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000824 ModuleSpec module_spec;
825 module_spec.GetUUID() = m_uuid;
826 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000827
Jason Molendad76fb6e2013-02-19 07:16:22 +0000828 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
829 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000830 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000831 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000832 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000833 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000834 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000835 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000836 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
837 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
838 {
839 ModuleList loaded_module_list;
840 loaded_module_list.Append (m_module_sp);
841 target.ModulesDidLoad (loaded_module_list);
842 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000843 }
844 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000845 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000846
Jason Molenda1c627542013-04-05 01:03:25 +0000847 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
848 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
849 // to find it.
850 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0ddfe7f2014-03-05 03:33:01 +0000851 if (!m_module_sp && platform_sp)
Jason Molenda1c627542013-04-05 01:03:25 +0000852 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000853 ConstString platform_name (platform_sp->GetPluginName());
854 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
855 if (platform_name == g_platform_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000856 {
857 ModuleSpec kext_bundle_module_spec(module_spec);
858 FileSpec kext_filespec(m_name.c_str(), false);
859 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
Ilia K667ef222015-03-24 14:30:28 +0000860 platform_sp->GetSharedModule (kext_bundle_module_spec, process, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
Jason Molenda1c627542013-04-05 01:03:25 +0000861 }
862 }
863
Jason Molendad76fb6e2013-02-19 07:16:22 +0000864 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000865 // This will search in the list of currently-loaded files, look in the
866 // standard search paths on the system, and on a Mac it will try calling
867 // the DebugSymbols framework with the UUID to find the binary via its
868 // search methods.
869 if (!m_module_sp)
870 {
871 m_module_sp = target.GetSharedModule (module_spec);
872 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000873
Jason Molendad76fb6e2013-02-19 07:16:22 +0000874 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000875 {
Greg Clayton44d93782014-01-27 23:43:24 +0000876 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000877 if (s)
878 {
Jason Molendacc6dc782013-04-30 22:38:28 +0000879 s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000880 }
881 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000882 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000883
Jason Molenda306bd0a2013-02-19 05:42:46 +0000884 // If we managed to find a module, append it to the target's list of images.
885 // If we also have a memory module, require that they have matching UUIDs
886 if (m_module_sp)
887 {
888 bool uuid_match_ok = true;
889 if (m_memory_module_sp)
890 {
891 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000892 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000893 uuid_match_ok = false;
894 }
895 }
896 if (uuid_match_ok)
897 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000898 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000899 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
900 {
901 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000902 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000903 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000904 }
905 }
906
Jason Molenda56c23282013-02-19 06:39:56 +0000907 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
908 {
Greg Clayton44d93782014-01-27 23:43:24 +0000909 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda56c23282013-02-19 06:39:56 +0000910 if (s)
911 {
Jason Molenda56c23282013-02-19 06:39:56 +0000912 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
Jason Molendac16b4af2013-05-03 23:56:12 +0000913 m_name.c_str(), m_uuid.GetAsString().c_str());
Jason Molenda56c23282013-02-19 06:39:56 +0000914 }
915 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000916
Greg Clayton67408532012-12-11 01:20:51 +0000917 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
918
Jason Molenda306bd0a2013-02-19 05:42:46 +0000919 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000920 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000921 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000922 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000923 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
924 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000925
Jason Molendabb860bd2012-10-09 01:17:11 +0000926 if (memory_object_file && ondisk_object_file)
927 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000928 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
929 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000930
Jason Molendabb860bd2012-10-09 01:17:11 +0000931 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
932 SectionList *memory_section_list = memory_object_file->GetSectionList ();
933 if (memory_section_list && ondisk_section_list)
934 {
935 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
936 // There may be CTF sections in the memory image so we can't
937 // always just compare the number of sections (which are actually
938 // segments in mach-o parlance)
939 uint32_t sect_idx = 0;
940
941 // Use the memory_module's addresses for each section to set the
942 // file module's load address as appropriate. We don't want to use
943 // a single slide value for the entire kext - different segments may
944 // be slid different amounts by the kext loader.
945
946 uint32_t num_sections_loaded = 0;
947 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
948 {
949 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
950 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000951 {
Greg Clayton67408532012-12-11 01:20:51 +0000952 // Don't ever load __LINKEDIT as it may or may not be actually
953 // mapped into memory and there is no current way to tell.
954 // I filed rdar://problem/12851706 to track being able to tell
955 // if the __LINKEDIT is actually mapped, but until then, we need
956 // to not load the __LINKEDIT
957 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
958 continue;
959
Jason Molendabb860bd2012-10-09 01:17:11 +0000960 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
961 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000962 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000963 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
Jason Molendabb860bd2012-10-09 01:17:11 +0000964 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000965 }
966 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000967 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000968 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000969 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000970 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000971 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000972 }
973 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000974 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000975 }
976 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000977 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000978 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000979 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000980 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000981 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000982
Greg Claytonc859e2d2012-02-13 23:10:39 +0000983 bool is_loaded = IsLoaded();
984
Jason Molenda306bd0a2013-02-19 05:42:46 +0000985 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000986 {
Greg Clayton44d93782014-01-27 23:43:24 +0000987 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda68b36072012-10-02 03:49:41 +0000988 if (s)
989 {
Jason Molenda732238c2013-03-01 00:06:37 +0000990 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
991 if (kernel_object_file)
992 {
993 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
994 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
995 {
Matt Kopec787d1622013-03-12 17:45:38 +0000996 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000997 }
998 }
Jason Molenda743e43962012-10-02 22:23:42 +0000999 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001000 s->Printf ("Loaded kernel file %s\n",
1001 m_module_sp->GetFileSpec().GetPath().c_str());
Jason Molenda743e43962012-10-02 22:23:42 +00001002 }
Jason Molenda68b36072012-10-02 03:49:41 +00001003 s->Flush ();
1004 }
1005 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001006 return is_loaded;
1007}
1008
Greg Clayton1f746072012-08-29 21:13:06 +00001009uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +00001010DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +00001011{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001012 if (m_memory_module_sp)
1013 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
1014 if (m_module_sp)
1015 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +00001016 return 0;
1017}
1018
1019lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +00001020DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +00001021{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001022 if (m_memory_module_sp)
1023 return m_memory_module_sp->GetArchitecture().GetByteOrder();
1024 if (m_module_sp)
1025 return m_module_sp->GetArchitecture().GetByteOrder();
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001026 return endian::InlHostByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +00001027}
1028
1029lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +00001030DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +00001031{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001032 if (m_memory_module_sp)
1033 return m_memory_module_sp->GetArchitecture();
1034 if (m_module_sp)
1035 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001036 return lldb_private::ArchSpec ();
1037}
1038
1039
Greg Clayton7b242382011-07-08 00:48:09 +00001040//----------------------------------------------------------------------
1041// Load the kernel module and initialize the "m_kernel" member. Return
1042// true _only_ if the kernel is loaded the first time through (subsequent
1043// calls to this function should return false after the kernel has been
1044// already loaded).
1045//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001046void
Greg Clayton944b8282011-08-22 22:30:57 +00001047DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001048{
Greg Claytond16e1e52011-07-12 17:06:17 +00001049 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001050 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001051 m_kernel.Clear();
1052 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1053 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001054
1055 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001056 if (m_kernel.GetModule().get()
1057 && m_kernel.GetModule()->GetObjectFile()
1058 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001059 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001060 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001061 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001062 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001063
Jason Molenda306bd0a2013-02-19 05:42:46 +00001064 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001065 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001066 m_kernel.SetLoadAddress(m_kernel_load_address);
1067 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001068 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001069 // We didn't get a hint from the process, so we will
1070 // try the kernel at the address that it exists at in
1071 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001072 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001073 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001074 {
1075 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1076 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1077 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1078 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001079 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001080 if (load_address != file_address)
1081 {
1082 // Don't accidentally relocate the kernel to the File address --
1083 // the Load address has already been set to its actual in-memory address.
1084 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001085 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001086 }
1087 }
1088 else
1089 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001090 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001091 }
1092 }
Greg Clayton7b242382011-07-08 00:48:09 +00001093 }
1094 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001095
Jason Molenda306bd0a2013-02-19 05:42:46 +00001096 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001097 {
1098 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1099 {
1100 m_kernel.LoadImageAtFileAddress (m_process);
1101 }
1102 }
Greg Clayton7b242382011-07-08 00:48:09 +00001103
Jason Molenda306bd0a2013-02-19 05:42:46 +00001104 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001105 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001106 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001107 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001108 if (symbol)
1109 {
Greg Claytone7612132012-03-07 21:03:09 +00001110 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001111 // Update all image infos
1112 ReadAllKextSummaries ();
1113 }
Greg Clayton7b242382011-07-08 00:48:09 +00001114 }
1115 else
Greg Clayton7b242382011-07-08 00:48:09 +00001116 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001117 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001118 }
1119 }
Greg Clayton7b242382011-07-08 00:48:09 +00001120}
1121
Greg Clayton7b242382011-07-08 00:48:09 +00001122//----------------------------------------------------------------------
1123// Static callback function that gets called when our DYLD notification
1124// breakpoint gets hit. We update all of our image infos and then
1125// let our super class DynamicLoader class decide if we should stop
1126// or not (based on global preference).
1127//----------------------------------------------------------------------
1128bool
Greg Clayton944b8282011-08-22 22:30:57 +00001129DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001130 StoppointCallbackContext *context,
1131 user_id_t break_id,
1132 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001133{
Greg Clayton944b8282011-08-22 22:30:57 +00001134 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001135}
1136
1137bool
Greg Clayton944b8282011-08-22 22:30:57 +00001138DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001139 user_id_t break_id,
1140 user_id_t break_loc_id)
1141{
Greg Clayton5160ce52013-03-27 23:08:40 +00001142 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001143 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001144 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001145
Greg Clayton374972e2011-07-09 17:15:55 +00001146 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001147
1148 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001149 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001150
Greg Clayton374972e2011-07-09 17:15:55 +00001151 return GetStopWhenImagesChange();
1152}
1153
1154
1155bool
Greg Clayton944b8282011-08-22 22:30:57 +00001156DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001157{
1158 Mutex::Locker locker(m_mutex);
1159
1160 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001161
Greg Claytond16e1e52011-07-12 17:06:17 +00001162 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001163 {
1164 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1165 const ByteOrder byte_order = m_kernel.GetByteOrder();
1166 Error error;
1167 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001168 // which is currently 4 uint32_t and a pointer.
Greg Clayton7b242382011-07-08 00:48:09 +00001169 uint8_t buf[24];
1170 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1171 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001172 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001173 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1174 prefer_file_cache,
1175 error,
1176 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001177 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001178 // We got a valid address for our kext summary header and make sure it isn't NULL
1179 if (m_kext_summary_header_addr.IsValid() &&
1180 m_kext_summary_header_addr.GetFileAddress() != 0)
1181 {
1182 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1183 if (bytes_read == count)
1184 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001185 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001186 m_kext_summary_header.version = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001187 if (m_kext_summary_header.version > 128)
1188 {
1189 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1190 s->Printf ("WARNING: Unable to read kext summary header, got improbable version number %u\n", m_kext_summary_header.version);
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001191 // If we get an improbably large version number, we're probably getting bad memory.
Jason Molendac6fa5db2014-04-15 01:04:00 +00001192 m_kext_summary_header_addr.Clear();
1193 return false;
1194 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001195 if (m_kext_summary_header.version >= 2)
1196 {
1197 m_kext_summary_header.entry_size = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001198 if (m_kext_summary_header.entry_size > 4096)
1199 {
1200 // If we get an improbably large entry_size, we're probably getting bad memory.
1201 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1202 s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1203 m_kext_summary_header_addr.Clear();
1204 return false;
1205 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001206 }
1207 else
1208 {
1209 // Versions less than 2 didn't have an entry size, it was hard coded
1210 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1211 }
1212 m_kext_summary_header.entry_count = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001213 if (m_kext_summary_header.entry_count > 10000)
1214 {
1215 // If we get an improbably large number of kexts, we're probably getting bad memory.
1216 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1217 s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1218 m_kext_summary_header_addr.Clear();
1219 return false;
1220 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001221 return true;
1222 }
1223 }
Greg Clayton7b242382011-07-08 00:48:09 +00001224 }
1225 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001226 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001227 return false;
1228}
1229
Jason Molenda306bd0a2013-02-19 05:42:46 +00001230// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1231// and we need to figure out what kexts have been added or removed.
1232// Read the kext summaries from the inferior kernel memory, compare them against the
1233// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1234// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001235
1236bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001237DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001238{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001239 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001240 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001241 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001242 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001243
1244 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001245
1246 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1247 return false;
1248
Jason Molenda4da2e322013-02-26 00:26:47 +00001249 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1250 // kext loading, don't print any messages about kexts & don't try to read them.
1251 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1252
Jason Molenda306bd0a2013-02-19 05:42:46 +00001253 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1254 // we just found out about from ReadKextSummaries are marked as "add".
1255 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1256 std::vector<bool> to_be_added(count, true);
1257
1258 int number_of_new_kexts_being_added = 0;
1259 int number_of_old_kexts_being_removed = m_known_kexts.size();
1260
1261 const uint32_t new_kexts_size = kext_summaries.size();
1262 const uint32_t old_kexts_size = m_known_kexts.size();
1263
1264 // The m_known_kexts vector may have entries that have been Cleared,
1265 // or are a kernel.
1266 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1267 {
1268 bool ignore = false;
1269 KextImageInfo &image_info = m_known_kexts[old_kext];
1270 if (image_info.IsKernel())
1271 {
1272 ignore = true;
1273 }
1274 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1275 {
1276 ignore = true;
1277 }
1278
1279 if (ignore)
1280 {
1281 number_of_old_kexts_being_removed--;
1282 to_be_removed[old_kext] = false;
1283 }
1284 }
1285
1286 // Scan over the list of kexts we just read from the kernel, note those that
1287 // need to be added and those already loaded.
1288 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1289 {
1290 bool add_this_one = true;
1291 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1292 {
1293 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1294 {
1295 // We already have this kext, don't re-load it.
1296 to_be_added[new_kext] = false;
1297 // This kext is still present, do not remove it.
1298 to_be_removed[old_kext] = false;
1299
1300 number_of_old_kexts_being_removed--;
1301 add_this_one = false;
1302 break;
1303 }
1304 }
1305 if (add_this_one)
1306 {
1307 number_of_new_kexts_being_added++;
1308 }
1309 }
1310
1311 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1312 return true;
1313
Greg Clayton44d93782014-01-27 23:43:24 +00001314 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda4da2e322013-02-26 00:26:47 +00001315 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001316 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001317 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1318 {
1319 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1320 }
1321 else if (number_of_new_kexts_being_added > 0)
1322 {
1323 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1324 }
1325 else if (number_of_old_kexts_being_removed > 0)
1326 {
1327 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1328 }
Greg Clayton7b242382011-07-08 00:48:09 +00001329 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001330
1331 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001332 {
1333 if (load_kexts)
1334 {
1335 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1336 }
1337 else
1338 {
1339 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries kext loading is disabled, else would have %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1340 }
1341 }
1342
Jason Molenda306bd0a2013-02-19 05:42:46 +00001343
1344 if (number_of_new_kexts_being_added > 0)
1345 {
1346 ModuleList loaded_module_list;
1347
1348 const uint32_t num_of_new_kexts = kext_summaries.size();
1349 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1350 {
1351 if (to_be_added[new_kext] == true)
1352 {
1353 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001354 if (load_kexts)
1355 {
1356 if (!image_info.LoadImageUsingMemoryModule (m_process))
1357 {
1358 image_info.LoadImageAtFileAddress (m_process);
1359 }
1360 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001361
1362 m_known_kexts.push_back(image_info);
1363
1364 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1365 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1366
Jason Molenda4da2e322013-02-26 00:26:47 +00001367 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001368 s->Printf (".");
1369
1370 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001371 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001372 }
1373 }
1374 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1375 }
1376
1377 if (number_of_old_kexts_being_removed > 0)
1378 {
1379 ModuleList loaded_module_list;
1380 const uint32_t num_of_old_kexts = m_known_kexts.size();
1381 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1382 {
1383 ModuleList unloaded_module_list;
1384 if (to_be_removed[old_kext])
1385 {
1386 KextImageInfo &image_info = m_known_kexts[old_kext];
1387 // You can't unload the kernel.
1388 if (!image_info.IsKernel())
1389 {
1390 if (image_info.GetModule())
1391 {
1392 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1393 }
1394 if (s)
1395 s->Printf (".");
1396 image_info.Clear();
1397 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1398 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1399 }
1400 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001401 m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001402 }
1403 }
1404
Jason Molenda4da2e322013-02-26 00:26:47 +00001405 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001406 {
1407 s->Printf (" done.\n");
1408 s->Flush ();
1409 }
1410
Jason Molenda306bd0a2013-02-19 05:42:46 +00001411 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001412}
1413
Greg Clayton7b242382011-07-08 00:48:09 +00001414uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001415DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001416 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001417 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001418{
1419 const ByteOrder endian = m_kernel.GetByteOrder();
1420 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1421
1422 image_infos.resize(image_infos_count);
1423 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1424 DataBufferHeap data(count, 0);
1425 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001426
Greg Clayton0d9fc762011-07-08 03:21:57 +00001427 const bool prefer_file_cache = false;
1428 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1429 prefer_file_cache,
1430 data.GetBytes(),
1431 data.GetByteSize(),
1432 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001433 if (bytes_read == count)
1434 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001435
Greg Clayton7b242382011-07-08 00:48:09 +00001436 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1437 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001438 for (uint32_t kext_summary_offset = 0;
1439 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1440 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001441 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001442 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001443 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1444 if (name_data == NULL)
1445 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001446 image_infos[i].SetName ((const char *) name_data);
1447 UUID uuid (extractor.GetData (&offset, 16), 16);
1448 image_infos[i].SetUUID (uuid);
1449 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1450 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001451 }
1452 if (i < image_infos.size())
1453 image_infos.resize(i);
1454 }
1455 else
1456 {
1457 image_infos.clear();
1458 }
1459 return image_infos.size();
1460}
1461
1462bool
Greg Clayton944b8282011-08-22 22:30:57 +00001463DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001464{
Greg Clayton7b242382011-07-08 00:48:09 +00001465 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001466
Greg Clayton7b242382011-07-08 00:48:09 +00001467 if (ReadKextSummaryHeader ())
1468 {
Greg Clayton374972e2011-07-09 17:15:55 +00001469 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001470 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001471 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001472 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001473 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001474 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001475 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001476 }
1477 return true;
1478 }
1479 }
1480 return false;
1481}
1482
1483//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001484// Dump an image info structure to the file handle provided.
1485//----------------------------------------------------------------------
1486void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001487DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001488{
1489 if (log == NULL)
1490 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001491 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001492
Jason Molenda306bd0a2013-02-19 05:42:46 +00001493 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001494 {
1495 if (u)
1496 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001497 log->Printf("\tuuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\" (UNLOADED)",
Greg Clayton7b242382011-07-08 00:48:09 +00001498 u[ 0], u[ 1], u[ 2], u[ 3],
1499 u[ 4], u[ 5], u[ 6], u[ 7],
1500 u[ 8], u[ 9], u[10], u[11],
1501 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001502 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001503 }
1504 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001505 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001506 }
1507 else
1508 {
1509 if (u)
1510 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001511 log->Printf("\taddr=0x%16.16" PRIx64 " size=0x%16.16" PRIx64 " uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\"",
1512 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001513 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1514 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001515 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001516 }
1517 else
1518 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001519 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1520 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001521 }
Greg Clayton7b242382011-07-08 00:48:09 +00001522 }
1523}
1524
1525//----------------------------------------------------------------------
1526// Dump the _dyld_all_image_infos members and all current image infos
1527// that we have parsed to the file handle provided.
1528//----------------------------------------------------------------------
1529void
Greg Clayton944b8282011-08-22 22:30:57 +00001530DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001531{
1532 if (log == NULL)
1533 return;
1534
1535 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001536 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001537 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001538 m_kext_summary_header.version,
1539 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001540 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001541
1542 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001543 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001544 if (count > 0)
1545 {
1546 log->PutCString("Loaded:");
1547 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001548 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001549 }
1550}
1551
1552void
Greg Clayton944b8282011-08-22 22:30:57 +00001553DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001554{
Greg Clayton944b8282011-08-22 22:30:57 +00001555 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001556 Clear(true);
1557 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001558}
1559
Greg Clayton374972e2011-07-09 17:15:55 +00001560void
Greg Clayton944b8282011-08-22 22:30:57 +00001561DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001562{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001563 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001564 {
Greg Clayton944b8282011-08-22 22:30:57 +00001565 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001566
Greg Claytond16e1e52011-07-12 17:06:17 +00001567
Jim Inghama8558b62012-05-22 00:12:20 +00001568 const bool internal_bp = true;
Greg Claytoneb023e72013-10-11 19:48:25 +00001569 const bool hardware = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001570 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001571 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001572 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001573 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001574 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001575 "OSKextLoadedKextSummariesUpdated",
1576 eFunctionNameTypeFull,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001577 eLanguageTypeUnknown,
Jim Inghama8558b62012-05-22 00:12:20 +00001578 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001579 internal_bp,
1580 hardware).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001581
Greg Clayton944b8282011-08-22 22:30:57 +00001582 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001583 m_break_id = bp->GetID();
1584 }
Greg Clayton7b242382011-07-08 00:48:09 +00001585}
1586
1587//----------------------------------------------------------------------
1588// Member function that gets called when the process state changes.
1589//----------------------------------------------------------------------
1590void
Greg Clayton944b8282011-08-22 22:30:57 +00001591DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001592{
Greg Clayton944b8282011-08-22 22:30:57 +00001593 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001594 switch (state)
1595 {
1596 case eStateConnected:
1597 case eStateAttaching:
1598 case eStateLaunching:
1599 case eStateInvalid:
1600 case eStateUnloaded:
1601 case eStateExited:
1602 case eStateDetached:
1603 Clear(false);
1604 break;
1605
1606 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001607 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001608 break;
1609
1610 case eStateRunning:
1611 case eStateStepping:
1612 case eStateCrashed:
1613 case eStateSuspended:
1614 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001615 }
1616}
1617
1618ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001619DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001620{
1621 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001622 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001623 if (log)
1624 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001625 return thread_plan_sp;
1626}
1627
1628Error
Greg Clayton944b8282011-08-22 22:30:57 +00001629DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001630{
1631 Error error;
1632 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1633 return error;
1634}
1635
1636void
Greg Clayton944b8282011-08-22 22:30:57 +00001637DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001638{
1639 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1640 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001641 CreateInstance,
1642 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001643}
1644
1645void
Greg Clayton944b8282011-08-22 22:30:57 +00001646DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001647{
1648 PluginManager::UnregisterPlugin (CreateInstance);
1649}
1650
Greg Claytone8cd0c92012-10-19 18:02:49 +00001651void
1652DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1653{
1654 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1655 {
1656 const bool is_global_setting = true;
1657 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1658 GetGlobalProperties()->GetValueProperties(),
1659 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1660 is_global_setting);
1661 }
1662}
Greg Clayton7b242382011-07-08 00:48:09 +00001663
Greg Clayton57abc5d2013-05-10 21:47:16 +00001664lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001665DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001666{
Greg Clayton57abc5d2013-05-10 21:47:16 +00001667 static ConstString g_name("darwin-kernel");
1668 return g_name;
Greg Clayton7b242382011-07-08 00:48:09 +00001669}
1670
1671const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001672DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001673{
1674 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1675}
1676
1677
1678//------------------------------------------------------------------
1679// PluginInterface protocol
1680//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001681lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001682DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001683{
Greg Clayton7b242382011-07-08 00:48:09 +00001684 return GetPluginNameStatic();
1685}
1686
1687uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001688DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001689{
1690 return 1;
1691}
1692
Greg Clayton1f746072012-08-29 21:13:06 +00001693lldb::ByteOrder
1694DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1695{
1696 switch (magic)
1697 {
Charles Davis510938e2013-08-27 05:04:57 +00001698 case llvm::MachO::MH_MAGIC:
1699 case llvm::MachO::MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001700 return endian::InlHostByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +00001701
Charles Davis510938e2013-08-27 05:04:57 +00001702 case llvm::MachO::MH_CIGAM:
1703 case llvm::MachO::MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001704 if (endian::InlHostByteOrder() == lldb::eByteOrderBig)
Greg Clayton1f746072012-08-29 21:13:06 +00001705 return lldb::eByteOrderLittle;
1706 else
1707 return lldb::eByteOrderBig;
1708
1709 default:
1710 break;
1711 }
1712 return lldb::eByteOrderInvalid;
1713}
1714