blob: 5367a6ddf9254c8f4041000085ef8ae79212738a [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
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"
Jason Molenda68b36072012-10-02 03:49:41 +000022#include "lldb/Host/Symbols.h"
Greg Clayton7b242382011-07-08 00:48:09 +000023#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000024#include "lldb/Target/RegisterContext.h"
Jason Molenda68b36072012-10-02 03:49:41 +000025#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000026#include "lldb/Target/Target.h"
27#include "lldb/Target/Thread.h"
28#include "lldb/Target/ThreadPlanRunToAddress.h"
Jason Molenda68b36072012-10-02 03:49:41 +000029
Greg Clayton7b242382011-07-08 00:48:09 +000030
Greg Clayton944b8282011-08-22 22:30:57 +000031#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000032
33//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
34#ifdef ENABLE_DEBUG_PRINTF
35#include <stdio.h>
36#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
37#else
38#define DEBUG_PRINTF(fmt, ...)
39#endif
40
41using namespace lldb;
42using namespace lldb_private;
43
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000044// Progressively greater amounts of scanning we will allow
45// For some targets very early in startup, we can't do any random reads of memory or we can crash the device
46// so a setting is needed that can completely disable the KASLR scans.
47
48enum KASLRScanType
49{
50 eKASLRScanNone = 0, // No reading into the inferior at all
51 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 +000052 eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000053 eKASLRScanExhaustiveScan // Scan through the entire possible kernel address range looking for a kernel
54};
55
56OptionEnumValueElement
57g_kaslr_kernel_scan_enum_values[] =
58{
59 { eKASLRScanNone, "none", "Do not read memory looking for a Darwin kernel when attaching." },
60 { eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
61 { eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find the Darwin kernel's load address."},
62 { eKASLRScanExhaustiveScan, "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
63 { 0, NULL, NULL }
64};
65
Greg Claytone8cd0c92012-10-19 18:02:49 +000066static PropertyDefinition
67g_properties[] =
68{
Greg Clayton66763ee2012-10-19 20:53:18 +000069 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000070 { "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 +000071 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000072};
73
74enum {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000075 ePropertyLoadKexts,
76 ePropertyScanType
Greg Claytone8cd0c92012-10-19 18:02:49 +000077};
78
79class DynamicLoaderDarwinKernelProperties : public Properties
80{
81public:
82
83 static ConstString &
84 GetSettingName ()
85 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000086 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000087 return g_setting_name;
88 }
89
90 DynamicLoaderDarwinKernelProperties() :
91 Properties ()
92 {
93 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
94 m_collection_sp->Initialize(g_properties);
95 }
96
97 virtual
98 ~DynamicLoaderDarwinKernelProperties()
99 {
100 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000101
Greg Claytone8cd0c92012-10-19 18:02:49 +0000102 bool
Greg Clayton66763ee2012-10-19 20:53:18 +0000103 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +0000104 {
Greg Clayton66763ee2012-10-19 20:53:18 +0000105 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000106 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
107 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000108
109 KASLRScanType
110 GetScanType() const
111 {
112 const uint32_t idx = ePropertyScanType;
Jason Molenda63969222013-01-30 04:48:16 +0000113 return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000114 }
115
116
Greg Claytone8cd0c92012-10-19 18:02:49 +0000117};
118
Greg Clayton7b0992d2013-04-18 22:45:39 +0000119typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000120
121static const DynamicLoaderDarwinKernelPropertiesSP &
122GetGlobalProperties()
123{
124 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
125 if (!g_settings_sp)
126 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
127 return g_settings_sp;
128}
129
Greg Clayton7b242382011-07-08 00:48:09 +0000130//----------------------------------------------------------------------
131// Create an instance of this class. This function is filled into
132// the plugin info class that gets handed out by the plugin factory and
133// allows the lldb to instantiate an instance of this class.
134//----------------------------------------------------------------------
135DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000136DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000137{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000138 if (!force)
Greg Clayton7b242382011-07-08 00:48:09 +0000139 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000140 // If the user provided an executable binary and it is not a kernel,
141 // this plugin should not create an instance.
Greg Claytonaa149cb2011-08-11 02:48:45 +0000142 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000143 if (exe_module)
144 {
145 ObjectFile *object_file = exe_module->GetObjectFile();
146 if (object_file)
147 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000148 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
149 {
150 return NULL;
151 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000152 }
153 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000154
155 // If the target's architecture does not look like an Apple environment,
156 // this plugin should not create an instance.
157 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
158 switch (triple_ref.getOS())
Greg Claytondf0b7d52011-07-08 04:11:42 +0000159 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000160 case llvm::Triple::Darwin:
161 case llvm::Triple::MacOSX:
162 case llvm::Triple::IOS:
163 if (triple_ref.getVendor() != llvm::Triple::Apple)
164 {
165 return NULL;
166 }
167 break;
168 // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
169 case llvm::Triple::UnknownOS:
170 break;
171 default:
172 return NULL;
173 break;
174 }
175 }
176
177 // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
178 // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
179
Jason Molenda503d0182013-03-02 07:19:32 +0000180 addr_t kernel_load_address = SearchForDarwinKernel (process);
181 if (kernel_load_address != LLDB_INVALID_ADDRESS)
182 {
183 process->SetCanJIT(false);
184 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
185 }
186 return NULL;
187}
188
189lldb::addr_t
190DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
191{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000192 addr_t kernel_load_address = process->GetImageInfoAddress();
193 if (kernel_load_address == LLDB_INVALID_ADDRESS)
194 {
195 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
196 if (kernel_load_address == LLDB_INVALID_ADDRESS)
197 {
198 kernel_load_address = SearchForKernelWithDebugHints (process);
199 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000200 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000201 kernel_load_address = SearchForKernelNearPC (process);
202 if (kernel_load_address == LLDB_INVALID_ADDRESS)
203 {
204 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
205 }
Greg Clayton70512312012-05-08 01:45:38 +0000206 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000207 }
Greg Clayton7b242382011-07-08 00:48:09 +0000208 }
Jason Molenda503d0182013-03-02 07:19:32 +0000209 return kernel_load_address;
Greg Clayton7b242382011-07-08 00:48:09 +0000210}
211
212//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000213// Check if the kernel binary is loaded in memory without a slide.
214// First verify that the ExecutableModule is a kernel before we proceed.
215// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
216//----------------------------------------------------------------------
217lldb::addr_t
218DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
219{
220 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
221 if (exe_module == NULL)
222 return LLDB_INVALID_ADDRESS;
223
224 ObjectFile *exe_objfile = exe_module->GetObjectFile();
225 if (exe_objfile == NULL)
226 return LLDB_INVALID_ADDRESS;
227
228 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
229 return LLDB_INVALID_ADDRESS;
230
231 if (!exe_objfile->GetHeaderAddress().IsValid())
232 return LLDB_INVALID_ADDRESS;
233
234 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
235 return exe_objfile->GetHeaderAddress().GetFileAddress();
236
237 return LLDB_INVALID_ADDRESS;
238}
239
240//----------------------------------------------------------------------
241// If the debug flag is included in the boot-args nvram setting, the kernel's load address
242// will be noted in the lowglo page at a fixed address
243// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
244//----------------------------------------------------------------------
245lldb::addr_t
246DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
247{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000248 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
249 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000250
251 Error read_err;
252 addr_t addr = LLDB_INVALID_ADDRESS;
253 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
254 {
255 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
256 }
257 else
258 {
259 addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
260 }
261
262 if (addr == 0)
263 addr = LLDB_INVALID_ADDRESS;
264
265 if (addr != LLDB_INVALID_ADDRESS)
266 {
267 if (CheckForKernelImageAtAddress (addr, process).IsValid())
268 return addr;
269 }
270
271 return LLDB_INVALID_ADDRESS;
272}
273
274//----------------------------------------------------------------------
275// If the kernel is currently executing when lldb attaches, and we don't have
276// a better way of finding the kernel's load address, try searching backwards
277// from the current pc value looking for the kernel's Mach header in memory.
278// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
279//----------------------------------------------------------------------
280lldb::addr_t
281DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
282{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000283 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
284 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
285 {
286 return LLDB_INVALID_ADDRESS;
287 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000288
289 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
290 if (thread.get() == NULL)
291 return LLDB_INVALID_ADDRESS;
292 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
293
294 if (pc == LLDB_INVALID_ADDRESS)
295 return LLDB_INVALID_ADDRESS;
296
297 addr_t kernel_range_low, kernel_range_high;
298 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
299 {
300 kernel_range_low = 1ULL << 63;
301 kernel_range_high = UINT64_MAX;
302 }
303 else
304 {
305 kernel_range_low = 1ULL << 31;
306 kernel_range_high = UINT32_MAX;
307 }
308
309 // Outside the normal kernel address range, this is probably userland code running right now
310 if (pc < kernel_range_low)
311 LLDB_INVALID_ADDRESS;
312
313 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
314 // an offset of one page (0x1000) or two, depending on the device.
315
316 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
317 addr_t addr = pc & ~0xfffff;
318
319 int i = 0;
320 while (i < 32 && pc >= kernel_range_low)
321 {
322 if (CheckForKernelImageAtAddress (addr, process).IsValid())
323 return addr;
324 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
325 return addr + 0x1000;
326 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
327 return addr + 0x2000;
328 i++;
329 addr -= 0x100000;
330 }
331
332 return LLDB_INVALID_ADDRESS;
333}
334
335//----------------------------------------------------------------------
336// Scan through the valid address range for a kernel binary.
337// This is uselessly slow in 64-bit environments so we don't even try it.
338// This scan is not enabled by default even for 32-bit targets.
339// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
340//----------------------------------------------------------------------
341lldb::addr_t
342DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
343{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000344 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
345 {
346 return LLDB_INVALID_ADDRESS;
347 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000348
349 addr_t kernel_range_low, kernel_range_high;
350 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
351 {
352 kernel_range_low = 1ULL << 63;
353 kernel_range_high = UINT64_MAX;
354 }
355 else
356 {
357 kernel_range_low = 1ULL << 31;
358 kernel_range_high = UINT32_MAX;
359 }
360
361 // Stepping through memory at one-megabyte resolution looking for a kernel
362 // rarely works (fast enough) with a 64-bit address space -- for now, let's
363 // not even bother. We may be attaching to something which *isn't* a kernel
364 // and we don't want to spin for minutes on-end looking for a kernel.
365 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
366 return LLDB_INVALID_ADDRESS;
367
368 addr_t addr = kernel_range_low;
369
370 while (addr >= kernel_range_low && addr < kernel_range_high)
371 {
372 if (CheckForKernelImageAtAddress (addr, process).IsValid())
373 return addr;
374 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
375 return addr + 0x1000;
376 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
377 return addr + 0x2000;
378 addr += 0x100000;
379 }
380 return LLDB_INVALID_ADDRESS;
381}
382
383//----------------------------------------------------------------------
384// Given an address in memory, look to see if there is a kernel image at that
385// address.
386// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
387//----------------------------------------------------------------------
388lldb_private::UUID
389DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
390{
391 if (addr == LLDB_INVALID_ADDRESS)
392 return UUID();
393
394 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
395 // (the first field of the mach_header/mach_header_64 struct).
396
397 Error read_error;
398 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
399 if (result != llvm::MachO::HeaderMagic64
400 && result != llvm::MachO::HeaderMagic32
401 && result != llvm::MachO::HeaderMagic32Swapped
402 && result != llvm::MachO::HeaderMagic64Swapped)
403 {
404 return UUID();
405 }
406
407 // Read the mach header and see whether it looks like a kernel
408 llvm::MachO::mach_header header;
409 if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
410 return UUID();
411
412 if (header.magic == llvm::MachO::HeaderMagic32Swapped ||
413 header.magic == llvm::MachO::HeaderMagic64Swapped)
414 {
415 header.magic = llvm::ByteSwap_32(header.magic);
416 header.cputype = llvm::ByteSwap_32(header.cputype);
417 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
418 header.filetype = llvm::ByteSwap_32(header.filetype);
419 header.ncmds = llvm::ByteSwap_32(header.ncmds);
420 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
421 header.flags = llvm::ByteSwap_32(header.flags);
422 }
423
424 // A kernel is an executable which does not have the dynamic link object flag set.
425 if (header.filetype == llvm::MachO::HeaderFileTypeExecutable
426 && (header.flags & llvm::MachO::HeaderFlagBitIsDynamicLinkObject) == 0)
427 {
428 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000429 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000430 if (!memory_module_sp.get())
431 return UUID();
432
433 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
434 if (exe_objfile == NULL)
435 return UUID();
436
437 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
438 {
439 return memory_module_sp->GetUUID();
440 }
441 }
442
443 return UUID();
444}
445
446//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000447// Constructor
448//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000449DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000450 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000451 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000452 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000453 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000454 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000455 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000456 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000457 m_mutex(Mutex::eMutexTypeRecursive),
458 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000459{
Jason Molenda1c627542013-04-05 01:03:25 +0000460 PlatformSP platform_sp(Platform::FindPlugin (process, "darwin-kernel"));
461 // Only select the darwin-kernel Platform if we've been asked to load kexts.
462 // It can take some time to scan over all of the kext info.plists and that
463 // shouldn't be done if kext loading is explicitly disabled.
464 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
465 {
466 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000467 }
Greg Clayton7b242382011-07-08 00:48:09 +0000468}
469
470//----------------------------------------------------------------------
471// Destructor
472//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000473DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000474{
475 Clear(true);
476}
477
Greg Clayton374972e2011-07-09 17:15:55 +0000478void
Greg Clayton944b8282011-08-22 22:30:57 +0000479DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000480{
481 LoadKernelModuleIfNeeded();
482 SetNotificationBreakpointIfNeeded ();
483}
Greg Clayton7b242382011-07-08 00:48:09 +0000484//------------------------------------------------------------------
485/// Called after attaching a process.
486///
487/// Allow DynamicLoader plug-ins to execute some code after
488/// attaching to a process.
489//------------------------------------------------------------------
490void
Greg Clayton944b8282011-08-22 22:30:57 +0000491DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000492{
493 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000494 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000495}
496
497//------------------------------------------------------------------
498/// Called after attaching a process.
499///
500/// Allow DynamicLoader plug-ins to execute some code after
501/// attaching to a process.
502//------------------------------------------------------------------
503void
Greg Clayton944b8282011-08-22 22:30:57 +0000504DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000505{
506 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000507 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000508}
509
510
511//----------------------------------------------------------------------
512// Clear out the state of this class.
513//----------------------------------------------------------------------
514void
Greg Clayton944b8282011-08-22 22:30:57 +0000515DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000516{
517 Mutex::Locker locker(m_mutex);
518
519 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
520 m_process->ClearBreakpointSiteByID(m_break_id);
521
522 if (clear_process)
523 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000524 m_kernel.Clear();
525 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000526 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000527 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000528 m_break_id = LLDB_INVALID_BREAK_ID;
529}
530
Greg Clayton7b242382011-07-08 00:48:09 +0000531
Greg Claytonc859e2d2012-02-13 23:10:39 +0000532bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000533DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000534{
535 if (IsLoaded())
536 return true;
537
Jason Molenda306bd0a2013-02-19 05:42:46 +0000538 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000539 {
540 bool changed = false;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000541 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
542 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000543 }
544 return false;
545}
546
Jason Molenda306bd0a2013-02-19 05:42:46 +0000547void
548DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
549{
550 m_module_sp = module_sp;
551 if (module_sp.get() && module_sp->GetObjectFile())
552 {
553 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
554 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
555 {
556 m_kernel_image = true;
557 }
558 else
559 {
560 m_kernel_image = false;
561 }
562 }
563}
564
565ModuleSP
566DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
567{
568 return m_module_sp;
569}
570
571void
572DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
573{
574 m_load_address = load_addr;
575}
576
577addr_t
578DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
579{
580 return m_load_address;
581}
582
583uint64_t
584DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
585{
586 return m_size;
587}
588
589void
590DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
591{
592 m_size = size;
593}
594
595uint32_t
596DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
597{
598 return m_load_process_stop_id;
599}
600
601void
602DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
603{
604 m_load_process_stop_id = stop_id;
605}
606
Greg Clayton2af282a2012-03-21 04:25:00 +0000607bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000608DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
609{
610 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
611 {
612 if (m_uuid == rhs.GetUUID())
613 {
614 return true;
615 }
616 return false;
617 }
618
619 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
620 return true;
621
622 return false;
623}
624
625void
626DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
627{
628 m_name = name;
629}
630
631std::string
632DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
633{
634 return m_name;
635}
636
637void
638DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
639{
640 m_uuid = uuid;
641}
642
643UUID
644DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
645{
646 return m_uuid;
647}
648
649// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
650// Module at that address. Require that the MemoryModule have a matching UUID and detect
651// if this MemoryModule is a kernel or a kext.
652//
653// Returns true if m_memory_module_sp is now set to a valid Module.
654
655bool
656DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
657{
658 if (m_memory_module_sp.get() != NULL)
659 return true;
660 if (m_load_address == LLDB_INVALID_ADDRESS)
661 return false;
662
663 FileSpec file_spec;
664 file_spec.SetFile (m_name.c_str(), false);
665
666 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
667
668 if (memory_module_sp.get() == NULL)
669 return false;
670
671 bool is_kernel = false;
672 if (memory_module_sp->GetObjectFile())
673 {
674 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
675 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
676 {
677 is_kernel = true;
678 }
679 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
680 {
681 is_kernel = false;
682 }
683 }
684
Jason Molenda38e70d12013-02-27 03:07:49 +0000685 // If this is a kext, and the kernel specified what UUID we should find at this
686 // load address, require that the memory module have a matching UUID or something
687 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000688 if (m_uuid.IsValid())
689 {
690 if (m_uuid != memory_module_sp->GetUUID())
691 {
692 return false;
693 }
694 }
695
696 // If the in-memory Module has a UUID, let's use that.
697 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
698 {
699 m_uuid = memory_module_sp->GetUUID();
700 }
701
702 m_memory_module_sp = memory_module_sp;
703 m_kernel_image = is_kernel;
704 if (is_kernel)
705 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000706 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000707 {
708 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
709 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000710 if (m_uuid.IsValid())
711 {
712 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
713 if (exe_module && exe_module->GetUUID().IsValid())
714 {
715 if (m_uuid != exe_module->GetUUID())
716 {
717 Stream *s = &process->GetTarget().GetDebugger().GetOutputStream();
718 if (s)
719 {
720 char memory_module_uuidbuf[64];
721 char exe_module_uuidbuf[64];
722 s->Printf ("warning: Host-side kernel file has Mach-O UUID of %s but remote kernel has a UUID of %s -- a mismatched kernel file will result in a poor debugger experience.\n",
723 exe_module->GetUUID().GetAsCString(exe_module_uuidbuf, sizeof (exe_module_uuidbuf)),
724 m_uuid.GetAsCString(memory_module_uuidbuf, sizeof (memory_module_uuidbuf)));
725 s->Flush ();
726 }
727 }
728 }
729 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000730 }
731
732 return true;
733}
734
735bool
736DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
737{
738 return m_kernel_image == true;
739}
740
741void
742DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
743{
744 m_kernel_image = is_kernel;
745}
746
747bool
748DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000749{
750 if (IsLoaded())
751 return true;
752
Greg Claytonc859e2d2012-02-13 23:10:39 +0000753
754 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000755
Jason Molenda306bd0a2013-02-19 05:42:46 +0000756 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
757 // have the correct segment load addresses.
758 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000759 {
760 return false;
761 }
762
Jason Molenda306bd0a2013-02-19 05:42:46 +0000763 bool uuid_is_valid = m_uuid.IsValid();
764
Jason Molendae575e7b2013-02-19 06:11:13 +0000765 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
766 {
767 Stream *s = &target.GetDebugger().GetOutputStream();
768 if (s)
769 {
770 char uuidbuf[64];
771 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsCString(uuidbuf, sizeof (uuidbuf)));
772 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
773 }
774 }
775
Jason Molenda306bd0a2013-02-19 05:42:46 +0000776 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000777 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000778 // See if the kext has already been loaded into the target, probably by the 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 && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000784 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000785 ModuleSpec module_spec;
786 module_spec.GetUUID() = m_uuid;
787 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000788
Jason Molendad76fb6e2013-02-19 07:16:22 +0000789 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
790 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000791 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000792 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000793 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000794 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000795 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000796 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000797 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
798 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
799 {
800 ModuleList loaded_module_list;
801 loaded_module_list.Append (m_module_sp);
802 target.ModulesDidLoad (loaded_module_list);
803 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000804 }
805 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000806 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000807
Jason Molenda1c627542013-04-05 01:03:25 +0000808 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
809 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
810 // to find it.
811 PlatformSP platform_sp (target.GetPlatform());
812 if (platform_sp)
813 {
814 const char *pname = platform_sp->GetShortPluginName();
815 if (pname && strcmp (pname, "darwin-kernel") == 0)
816 {
817 ModuleSpec kext_bundle_module_spec(module_spec);
818 FileSpec kext_filespec(m_name.c_str(), false);
819 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
820 platform_sp->GetSharedModule (kext_bundle_module_spec, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
821 }
822 }
823
Jason Molendad76fb6e2013-02-19 07:16:22 +0000824 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000825 // This will search in the list of currently-loaded files, look in the
826 // standard search paths on the system, and on a Mac it will try calling
827 // the DebugSymbols framework with the UUID to find the binary via its
828 // search methods.
829 if (!m_module_sp)
830 {
831 m_module_sp = target.GetSharedModule (module_spec);
832 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000833
Jason Molendad76fb6e2013-02-19 07:16:22 +0000834 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000835 {
836 Stream *s = &target.GetDebugger().GetOutputStream();
837 if (s)
838 {
Jason Molenda56c23282013-02-19 06:39:56 +0000839 s->Printf ("WARNING: Unable to locate kernel binary on this system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000840 }
841 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000842 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000843
Jason Molenda306bd0a2013-02-19 05:42:46 +0000844 // If we managed to find a module, append it to the target's list of images.
845 // If we also have a memory module, require that they have matching UUIDs
846 if (m_module_sp)
847 {
848 bool uuid_match_ok = true;
849 if (m_memory_module_sp)
850 {
851 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000852 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000853 uuid_match_ok = false;
854 }
855 }
856 if (uuid_match_ok)
857 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000858 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000859 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
860 {
861 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000862 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000863 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000864 }
865 }
866
Jason Molenda56c23282013-02-19 06:39:56 +0000867 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
868 {
869 Stream *s = &target.GetDebugger().GetOutputStream();
870 if (s)
871 {
872 char uuidbuf[64];
873 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
874 m_name.c_str(), m_uuid.GetAsCString(uuidbuf, sizeof (uuidbuf)));
875 }
876 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000877
Greg Clayton67408532012-12-11 01:20:51 +0000878 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
879
Jason Molenda306bd0a2013-02-19 05:42:46 +0000880 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000881 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000882 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000883 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000884 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
885 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000886
Jason Molendabb860bd2012-10-09 01:17:11 +0000887 if (memory_object_file && ondisk_object_file)
888 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000889 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
890 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000891
Jason Molendabb860bd2012-10-09 01:17:11 +0000892 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
893 SectionList *memory_section_list = memory_object_file->GetSectionList ();
894 if (memory_section_list && ondisk_section_list)
895 {
896 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
897 // There may be CTF sections in the memory image so we can't
898 // always just compare the number of sections (which are actually
899 // segments in mach-o parlance)
900 uint32_t sect_idx = 0;
901
902 // Use the memory_module's addresses for each section to set the
903 // file module's load address as appropriate. We don't want to use
904 // a single slide value for the entire kext - different segments may
905 // be slid different amounts by the kext loader.
906
907 uint32_t num_sections_loaded = 0;
908 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
909 {
910 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
911 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000912 {
Greg Clayton67408532012-12-11 01:20:51 +0000913 // Don't ever load __LINKEDIT as it may or may not be actually
914 // mapped into memory and there is no current way to tell.
915 // I filed rdar://problem/12851706 to track being able to tell
916 // if the __LINKEDIT is actually mapped, but until then, we need
917 // to not load the __LINKEDIT
918 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
919 continue;
920
Jason Molendabb860bd2012-10-09 01:17:11 +0000921 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
922 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000923 {
Jason Molendabb860bd2012-10-09 01:17:11 +0000924 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
925 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000926 }
927 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000928 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000929 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000930 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000931 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000932 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000933 }
934 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000935 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000936 }
937 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000938 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000939 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000940 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000941 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000942 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000943
Greg Claytonc859e2d2012-02-13 23:10:39 +0000944 bool is_loaded = IsLoaded();
945
Jason Molenda306bd0a2013-02-19 05:42:46 +0000946 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000947 {
948 Stream *s = &target.GetDebugger().GetOutputStream();
949 if (s)
950 {
Jason Molenda732238c2013-03-01 00:06:37 +0000951 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
952 if (kernel_object_file)
953 {
954 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
955 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
956 {
Matt Kopec787d1622013-03-12 17:45:38 +0000957 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000958 }
959 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000960 if (m_module_sp->GetFileSpec().GetDirectory().IsEmpty())
Jason Molenda743e43962012-10-02 22:23:42 +0000961 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000962 s->Printf ("Loaded kernel file %s\n", m_module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda743e43962012-10-02 22:23:42 +0000963 }
964 else
965 {
966 s->Printf ("Loaded kernel file %s/%s\n",
Jason Molenda306bd0a2013-02-19 05:42:46 +0000967 m_module_sp->GetFileSpec().GetDirectory().AsCString(),
968 m_module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda743e43962012-10-02 22:23:42 +0000969 }
Jason Molenda68b36072012-10-02 03:49:41 +0000970 s->Flush ();
971 }
972 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000973 return is_loaded;
974}
975
Greg Clayton1f746072012-08-29 21:13:06 +0000976uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000977DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000978{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000979 if (m_memory_module_sp)
980 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
981 if (m_module_sp)
982 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000983 return 0;
984}
985
986lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000987DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000988{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000989 if (m_memory_module_sp)
990 return m_memory_module_sp->GetArchitecture().GetByteOrder();
991 if (m_module_sp)
992 return m_module_sp->GetArchitecture().GetByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +0000993 return lldb::endian::InlHostByteOrder();
994}
995
996lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000997DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +0000998{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000999 if (m_memory_module_sp)
1000 return m_memory_module_sp->GetArchitecture();
1001 if (m_module_sp)
1002 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001003 return lldb_private::ArchSpec ();
1004}
1005
1006
Greg Clayton7b242382011-07-08 00:48:09 +00001007//----------------------------------------------------------------------
1008// Load the kernel module and initialize the "m_kernel" member. Return
1009// true _only_ if the kernel is loaded the first time through (subsequent
1010// calls to this function should return false after the kernel has been
1011// already loaded).
1012//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001013void
Greg Clayton944b8282011-08-22 22:30:57 +00001014DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001015{
Greg Claytond16e1e52011-07-12 17:06:17 +00001016 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001017 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001018 m_kernel.Clear();
1019 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1020 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001021
1022 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001023 if (m_kernel.GetModule().get()
1024 && m_kernel.GetModule()->GetObjectFile()
1025 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001026 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001027 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001028 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001029 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001030
Jason Molenda306bd0a2013-02-19 05:42:46 +00001031 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001032 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001033 m_kernel.SetLoadAddress(m_kernel_load_address);
1034 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001035 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001036 // We didn't get a hint from the process, so we will
1037 // try the kernel at the address that it exists at in
1038 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001039 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001040 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001041 {
1042 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1043 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1044 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1045 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001046 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001047 if (load_address != file_address)
1048 {
1049 // Don't accidentally relocate the kernel to the File address --
1050 // the Load address has already been set to its actual in-memory address.
1051 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001052 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001053 }
1054 }
1055 else
1056 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001057 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001058 }
1059 }
Greg Clayton7b242382011-07-08 00:48:09 +00001060 }
1061 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001062
Jason Molenda306bd0a2013-02-19 05:42:46 +00001063 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001064 {
1065 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1066 {
1067 m_kernel.LoadImageAtFileAddress (m_process);
1068 }
1069 }
Greg Clayton7b242382011-07-08 00:48:09 +00001070
Jason Molenda306bd0a2013-02-19 05:42:46 +00001071 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001072 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001073 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001074 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001075 if (symbol)
1076 {
Greg Claytone7612132012-03-07 21:03:09 +00001077 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001078 // Update all image infos
1079 ReadAllKextSummaries ();
1080 }
Greg Clayton7b242382011-07-08 00:48:09 +00001081 }
1082 else
Greg Clayton7b242382011-07-08 00:48:09 +00001083 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001084 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001085 }
1086 }
Greg Clayton7b242382011-07-08 00:48:09 +00001087}
1088
Greg Clayton7b242382011-07-08 00:48:09 +00001089//----------------------------------------------------------------------
1090// Static callback function that gets called when our DYLD notification
1091// breakpoint gets hit. We update all of our image infos and then
1092// let our super class DynamicLoader class decide if we should stop
1093// or not (based on global preference).
1094//----------------------------------------------------------------------
1095bool
Greg Clayton944b8282011-08-22 22:30:57 +00001096DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001097 StoppointCallbackContext *context,
1098 user_id_t break_id,
1099 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001100{
Greg Clayton944b8282011-08-22 22:30:57 +00001101 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001102}
1103
1104bool
Greg Clayton944b8282011-08-22 22:30:57 +00001105DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001106 user_id_t break_id,
1107 user_id_t break_loc_id)
1108{
Greg Clayton5160ce52013-03-27 23:08:40 +00001109 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001110 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001111 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001112
Greg Clayton374972e2011-07-09 17:15:55 +00001113 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001114
1115 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001116 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001117
Greg Clayton374972e2011-07-09 17:15:55 +00001118 return GetStopWhenImagesChange();
1119}
1120
1121
1122bool
Greg Clayton944b8282011-08-22 22:30:57 +00001123DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001124{
1125 Mutex::Locker locker(m_mutex);
1126
1127 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001128
Greg Claytond16e1e52011-07-12 17:06:17 +00001129 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001130 {
1131 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1132 const ByteOrder byte_order = m_kernel.GetByteOrder();
1133 Error error;
1134 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
1135 // which is currenty 4 uint32_t and a pointer.
1136 uint8_t buf[24];
1137 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1138 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001139 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001140 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1141 prefer_file_cache,
1142 error,
1143 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001144 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001145 // We got a valid address for our kext summary header and make sure it isn't NULL
1146 if (m_kext_summary_header_addr.IsValid() &&
1147 m_kext_summary_header_addr.GetFileAddress() != 0)
1148 {
1149 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1150 if (bytes_read == count)
1151 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001152 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001153 m_kext_summary_header.version = data.GetU32(&offset);
1154 if (m_kext_summary_header.version >= 2)
1155 {
1156 m_kext_summary_header.entry_size = data.GetU32(&offset);
1157 }
1158 else
1159 {
1160 // Versions less than 2 didn't have an entry size, it was hard coded
1161 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1162 }
1163 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +00001164 return true;
1165 }
1166 }
Greg Clayton7b242382011-07-08 00:48:09 +00001167 }
1168 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001169 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001170 return false;
1171}
1172
Jason Molenda306bd0a2013-02-19 05:42:46 +00001173// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1174// and we need to figure out what kexts have been added or removed.
1175// Read the kext summaries from the inferior kernel memory, compare them against the
1176// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1177// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001178
1179bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001180DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001181{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001182 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001183 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001184 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001185 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001186
1187 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001188
1189 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1190 return false;
1191
Jason Molenda4da2e322013-02-26 00:26:47 +00001192 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1193 // kext loading, don't print any messages about kexts & don't try to read them.
1194 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1195
Jason Molenda306bd0a2013-02-19 05:42:46 +00001196 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1197 // we just found out about from ReadKextSummaries are marked as "add".
1198 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1199 std::vector<bool> to_be_added(count, true);
1200
1201 int number_of_new_kexts_being_added = 0;
1202 int number_of_old_kexts_being_removed = m_known_kexts.size();
1203
1204 const uint32_t new_kexts_size = kext_summaries.size();
1205 const uint32_t old_kexts_size = m_known_kexts.size();
1206
1207 // The m_known_kexts vector may have entries that have been Cleared,
1208 // or are a kernel.
1209 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1210 {
1211 bool ignore = false;
1212 KextImageInfo &image_info = m_known_kexts[old_kext];
1213 if (image_info.IsKernel())
1214 {
1215 ignore = true;
1216 }
1217 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1218 {
1219 ignore = true;
1220 }
1221
1222 if (ignore)
1223 {
1224 number_of_old_kexts_being_removed--;
1225 to_be_removed[old_kext] = false;
1226 }
1227 }
1228
1229 // Scan over the list of kexts we just read from the kernel, note those that
1230 // need to be added and those already loaded.
1231 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1232 {
1233 bool add_this_one = true;
1234 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1235 {
1236 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1237 {
1238 // We already have this kext, don't re-load it.
1239 to_be_added[new_kext] = false;
1240 // This kext is still present, do not remove it.
1241 to_be_removed[old_kext] = false;
1242
1243 number_of_old_kexts_being_removed--;
1244 add_this_one = false;
1245 break;
1246 }
1247 }
1248 if (add_this_one)
1249 {
1250 number_of_new_kexts_being_added++;
1251 }
1252 }
1253
1254 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1255 return true;
1256
Greg Clayton07e66e32011-07-20 03:41:06 +00001257 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Jason Molenda4da2e322013-02-26 00:26:47 +00001258 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001259 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001260 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1261 {
1262 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1263 }
1264 else if (number_of_new_kexts_being_added > 0)
1265 {
1266 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1267 }
1268 else if (number_of_old_kexts_being_removed > 0)
1269 {
1270 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1271 }
Greg Clayton7b242382011-07-08 00:48:09 +00001272 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001273
1274 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001275 {
1276 if (load_kexts)
1277 {
1278 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1279 }
1280 else
1281 {
1282 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);
1283 }
1284 }
1285
Jason Molenda306bd0a2013-02-19 05:42:46 +00001286
1287 if (number_of_new_kexts_being_added > 0)
1288 {
1289 ModuleList loaded_module_list;
1290
1291 const uint32_t num_of_new_kexts = kext_summaries.size();
1292 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1293 {
1294 if (to_be_added[new_kext] == true)
1295 {
1296 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001297 if (load_kexts)
1298 {
1299 if (!image_info.LoadImageUsingMemoryModule (m_process))
1300 {
1301 image_info.LoadImageAtFileAddress (m_process);
1302 }
1303 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001304
1305 m_known_kexts.push_back(image_info);
1306
1307 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1308 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1309
Jason Molenda4da2e322013-02-26 00:26:47 +00001310 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001311 s->Printf (".");
1312
1313 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001314 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001315 }
1316 }
1317 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1318 }
1319
1320 if (number_of_old_kexts_being_removed > 0)
1321 {
1322 ModuleList loaded_module_list;
1323 const uint32_t num_of_old_kexts = m_known_kexts.size();
1324 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1325 {
1326 ModuleList unloaded_module_list;
1327 if (to_be_removed[old_kext])
1328 {
1329 KextImageInfo &image_info = m_known_kexts[old_kext];
1330 // You can't unload the kernel.
1331 if (!image_info.IsKernel())
1332 {
1333 if (image_info.GetModule())
1334 {
1335 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1336 }
1337 if (s)
1338 s->Printf (".");
1339 image_info.Clear();
1340 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1341 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1342 }
1343 }
1344 m_process->GetTarget().ModulesDidUnload (unloaded_module_list);
1345 }
1346 }
1347
Jason Molenda4da2e322013-02-26 00:26:47 +00001348 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001349 {
1350 s->Printf (" done.\n");
1351 s->Flush ();
1352 }
1353
Jason Molenda306bd0a2013-02-19 05:42:46 +00001354 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001355}
1356
Greg Clayton7b242382011-07-08 00:48:09 +00001357uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001358DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001359 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001360 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001361{
1362 const ByteOrder endian = m_kernel.GetByteOrder();
1363 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1364
1365 image_infos.resize(image_infos_count);
1366 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1367 DataBufferHeap data(count, 0);
1368 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001369
Greg Clayton0d9fc762011-07-08 03:21:57 +00001370 const bool prefer_file_cache = false;
1371 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1372 prefer_file_cache,
1373 data.GetBytes(),
1374 data.GetByteSize(),
1375 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001376 if (bytes_read == count)
1377 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001378
Greg Clayton7b242382011-07-08 00:48:09 +00001379 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1380 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001381 for (uint32_t kext_summary_offset = 0;
1382 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1383 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001384 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001385 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001386 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1387 if (name_data == NULL)
1388 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001389 image_infos[i].SetName ((const char *) name_data);
1390 UUID uuid (extractor.GetData (&offset, 16), 16);
1391 image_infos[i].SetUUID (uuid);
1392 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1393 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001394 }
1395 if (i < image_infos.size())
1396 image_infos.resize(i);
1397 }
1398 else
1399 {
1400 image_infos.clear();
1401 }
1402 return image_infos.size();
1403}
1404
1405bool
Greg Clayton944b8282011-08-22 22:30:57 +00001406DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001407{
Greg Clayton7b242382011-07-08 00:48:09 +00001408 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001409
Greg Clayton7b242382011-07-08 00:48:09 +00001410 if (ReadKextSummaryHeader ())
1411 {
Greg Clayton374972e2011-07-09 17:15:55 +00001412 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001413 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001414 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001415 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001416 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001417 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001418 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001419 }
1420 return true;
1421 }
1422 }
1423 return false;
1424}
1425
1426//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001427// Dump an image info structure to the file handle provided.
1428//----------------------------------------------------------------------
1429void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001430DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001431{
1432 if (log == NULL)
1433 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001434 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001435
Jason Molenda306bd0a2013-02-19 05:42:46 +00001436 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001437 {
1438 if (u)
1439 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001440 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 +00001441 u[ 0], u[ 1], u[ 2], u[ 3],
1442 u[ 4], u[ 5], u[ 6], u[ 7],
1443 u[ 8], u[ 9], u[10], u[11],
1444 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001445 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001446 }
1447 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001448 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001449 }
1450 else
1451 {
1452 if (u)
1453 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001454 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\"",
1455 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001456 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1457 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001458 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001459 }
1460 else
1461 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001462 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1463 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001464 }
Greg Clayton7b242382011-07-08 00:48:09 +00001465 }
1466}
1467
1468//----------------------------------------------------------------------
1469// Dump the _dyld_all_image_infos members and all current image infos
1470// that we have parsed to the file handle provided.
1471//----------------------------------------------------------------------
1472void
Greg Clayton944b8282011-08-22 22:30:57 +00001473DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001474{
1475 if (log == NULL)
1476 return;
1477
1478 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001479 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001480 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001481 m_kext_summary_header.version,
1482 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001483 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001484
1485 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001486 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001487 if (count > 0)
1488 {
1489 log->PutCString("Loaded:");
1490 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001491 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001492 }
1493}
1494
1495void
Greg Clayton944b8282011-08-22 22:30:57 +00001496DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001497{
Greg Clayton944b8282011-08-22 22:30:57 +00001498 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001499 Clear(true);
1500 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001501}
1502
Greg Clayton374972e2011-07-09 17:15:55 +00001503void
Greg Clayton944b8282011-08-22 22:30:57 +00001504DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001505{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001506 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001507 {
Greg Clayton944b8282011-08-22 22:30:57 +00001508 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001509
Greg Claytond16e1e52011-07-12 17:06:17 +00001510
Jim Inghama8558b62012-05-22 00:12:20 +00001511 const bool internal_bp = true;
Greg Claytond16e1e52011-07-12 17:06:17 +00001512 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001513 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001514 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001515 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001516 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001517 "OSKextLoadedKextSummariesUpdated",
1518 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +00001519 skip_prologue,
1520 internal_bp).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001521
Greg Clayton944b8282011-08-22 22:30:57 +00001522 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001523 m_break_id = bp->GetID();
1524 }
Greg Clayton7b242382011-07-08 00:48:09 +00001525}
1526
1527//----------------------------------------------------------------------
1528// Member function that gets called when the process state changes.
1529//----------------------------------------------------------------------
1530void
Greg Clayton944b8282011-08-22 22:30:57 +00001531DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001532{
Greg Clayton944b8282011-08-22 22:30:57 +00001533 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001534 switch (state)
1535 {
1536 case eStateConnected:
1537 case eStateAttaching:
1538 case eStateLaunching:
1539 case eStateInvalid:
1540 case eStateUnloaded:
1541 case eStateExited:
1542 case eStateDetached:
1543 Clear(false);
1544 break;
1545
1546 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001547 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001548 break;
1549
1550 case eStateRunning:
1551 case eStateStepping:
1552 case eStateCrashed:
1553 case eStateSuspended:
1554 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001555 }
1556}
1557
1558ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001559DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001560{
1561 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001562 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001563 if (log)
1564 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001565 return thread_plan_sp;
1566}
1567
1568Error
Greg Clayton944b8282011-08-22 22:30:57 +00001569DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001570{
1571 Error error;
1572 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1573 return error;
1574}
1575
1576void
Greg Clayton944b8282011-08-22 22:30:57 +00001577DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001578{
1579 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1580 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001581 CreateInstance,
1582 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001583}
1584
1585void
Greg Clayton944b8282011-08-22 22:30:57 +00001586DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001587{
1588 PluginManager::UnregisterPlugin (CreateInstance);
1589}
1590
Greg Claytone8cd0c92012-10-19 18:02:49 +00001591void
1592DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1593{
1594 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1595 {
1596 const bool is_global_setting = true;
1597 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1598 GetGlobalProperties()->GetValueProperties(),
1599 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1600 is_global_setting);
1601 }
1602}
Greg Clayton7b242382011-07-08 00:48:09 +00001603
1604const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001605DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001606{
Greg Clayton468ea4e2012-10-19 18:14:47 +00001607 return "dynamic-loader.darwin-kernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001608}
1609
1610const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001611DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001612{
1613 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1614}
1615
1616
1617//------------------------------------------------------------------
1618// PluginInterface protocol
1619//------------------------------------------------------------------
1620const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001621DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001622{
Greg Clayton944b8282011-08-22 22:30:57 +00001623 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001624}
1625
1626const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001627DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001628{
1629 return GetPluginNameStatic();
1630}
1631
1632uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001633DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001634{
1635 return 1;
1636}
1637
Greg Clayton1f746072012-08-29 21:13:06 +00001638lldb::ByteOrder
1639DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1640{
1641 switch (magic)
1642 {
1643 case llvm::MachO::HeaderMagic32:
1644 case llvm::MachO::HeaderMagic64:
1645 return lldb::endian::InlHostByteOrder();
1646
1647 case llvm::MachO::HeaderMagic32Swapped:
1648 case llvm::MachO::HeaderMagic64Swapped:
1649 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1650 return lldb::eByteOrderLittle;
1651 else
1652 return lldb::eByteOrderBig;
1653
1654 default:
1655 break;
1656 }
1657 return lldb::eByteOrderInvalid;
1658}
1659