blob: 9f7b1347412a4a7090c0b923e4faa65cc069b71f [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
119typedef STD_SHARED_PTR(DynamicLoaderDarwinKernelProperties) DynamicLoaderDarwinKernelPropertiesSP;
120
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
180 addr_t kernel_load_address = process->GetImageInfoAddress();
181 if (kernel_load_address == LLDB_INVALID_ADDRESS)
182 {
183 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
184 if (kernel_load_address == LLDB_INVALID_ADDRESS)
185 {
186 kernel_load_address = SearchForKernelWithDebugHints (process);
187 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000188 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000189 kernel_load_address = SearchForKernelNearPC (process);
190 if (kernel_load_address == LLDB_INVALID_ADDRESS)
191 {
192 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
193 }
Greg Clayton70512312012-05-08 01:45:38 +0000194 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000195 }
Greg Clayton7b242382011-07-08 00:48:09 +0000196 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000197
198 if (kernel_load_address != LLDB_INVALID_ADDRESS)
Sean Callanan90539452011-09-20 23:01:51 +0000199 {
200 process->SetCanJIT(false);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000201 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
Sean Callanan90539452011-09-20 23:01:51 +0000202 }
Greg Clayton7b242382011-07-08 00:48:09 +0000203 return NULL;
204}
205
206//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000207// Check if the kernel binary is loaded in memory without a slide.
208// First verify that the ExecutableModule is a kernel before we proceed.
209// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
210//----------------------------------------------------------------------
211lldb::addr_t
212DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
213{
214 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
215 if (exe_module == NULL)
216 return LLDB_INVALID_ADDRESS;
217
218 ObjectFile *exe_objfile = exe_module->GetObjectFile();
219 if (exe_objfile == NULL)
220 return LLDB_INVALID_ADDRESS;
221
222 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
223 return LLDB_INVALID_ADDRESS;
224
225 if (!exe_objfile->GetHeaderAddress().IsValid())
226 return LLDB_INVALID_ADDRESS;
227
228 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
229 return exe_objfile->GetHeaderAddress().GetFileAddress();
230
231 return LLDB_INVALID_ADDRESS;
232}
233
234//----------------------------------------------------------------------
235// If the debug flag is included in the boot-args nvram setting, the kernel's load address
236// will be noted in the lowglo page at a fixed address
237// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
238//----------------------------------------------------------------------
239lldb::addr_t
240DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
241{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000242 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
243 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000244
245 Error read_err;
246 addr_t addr = LLDB_INVALID_ADDRESS;
247 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
248 {
249 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
250 }
251 else
252 {
253 addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
254 }
255
256 if (addr == 0)
257 addr = LLDB_INVALID_ADDRESS;
258
259 if (addr != LLDB_INVALID_ADDRESS)
260 {
261 if (CheckForKernelImageAtAddress (addr, process).IsValid())
262 return addr;
263 }
264
265 return LLDB_INVALID_ADDRESS;
266}
267
268//----------------------------------------------------------------------
269// If the kernel is currently executing when lldb attaches, and we don't have
270// a better way of finding the kernel's load address, try searching backwards
271// from the current pc value looking for the kernel's Mach header in memory.
272// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
273//----------------------------------------------------------------------
274lldb::addr_t
275DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
276{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000277 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
278 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
279 {
280 return LLDB_INVALID_ADDRESS;
281 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000282
283 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
284 if (thread.get() == NULL)
285 return LLDB_INVALID_ADDRESS;
286 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
287
288 if (pc == LLDB_INVALID_ADDRESS)
289 return LLDB_INVALID_ADDRESS;
290
291 addr_t kernel_range_low, kernel_range_high;
292 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
293 {
294 kernel_range_low = 1ULL << 63;
295 kernel_range_high = UINT64_MAX;
296 }
297 else
298 {
299 kernel_range_low = 1ULL << 31;
300 kernel_range_high = UINT32_MAX;
301 }
302
303 // Outside the normal kernel address range, this is probably userland code running right now
304 if (pc < kernel_range_low)
305 LLDB_INVALID_ADDRESS;
306
307 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
308 // an offset of one page (0x1000) or two, depending on the device.
309
310 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
311 addr_t addr = pc & ~0xfffff;
312
313 int i = 0;
314 while (i < 32 && pc >= kernel_range_low)
315 {
316 if (CheckForKernelImageAtAddress (addr, process).IsValid())
317 return addr;
318 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
319 return addr + 0x1000;
320 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
321 return addr + 0x2000;
322 i++;
323 addr -= 0x100000;
324 }
325
326 return LLDB_INVALID_ADDRESS;
327}
328
329//----------------------------------------------------------------------
330// Scan through the valid address range for a kernel binary.
331// This is uselessly slow in 64-bit environments so we don't even try it.
332// This scan is not enabled by default even for 32-bit targets.
333// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
334//----------------------------------------------------------------------
335lldb::addr_t
336DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
337{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000338 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
339 {
340 return LLDB_INVALID_ADDRESS;
341 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000342
343 addr_t kernel_range_low, kernel_range_high;
344 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
345 {
346 kernel_range_low = 1ULL << 63;
347 kernel_range_high = UINT64_MAX;
348 }
349 else
350 {
351 kernel_range_low = 1ULL << 31;
352 kernel_range_high = UINT32_MAX;
353 }
354
355 // Stepping through memory at one-megabyte resolution looking for a kernel
356 // rarely works (fast enough) with a 64-bit address space -- for now, let's
357 // not even bother. We may be attaching to something which *isn't* a kernel
358 // and we don't want to spin for minutes on-end looking for a kernel.
359 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
360 return LLDB_INVALID_ADDRESS;
361
362 addr_t addr = kernel_range_low;
363
364 while (addr >= kernel_range_low && addr < kernel_range_high)
365 {
366 if (CheckForKernelImageAtAddress (addr, process).IsValid())
367 return addr;
368 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
369 return addr + 0x1000;
370 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
371 return addr + 0x2000;
372 addr += 0x100000;
373 }
374 return LLDB_INVALID_ADDRESS;
375}
376
377//----------------------------------------------------------------------
378// Given an address in memory, look to see if there is a kernel image at that
379// address.
380// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
381//----------------------------------------------------------------------
382lldb_private::UUID
383DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
384{
385 if (addr == LLDB_INVALID_ADDRESS)
386 return UUID();
387
388 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
389 // (the first field of the mach_header/mach_header_64 struct).
390
391 Error read_error;
392 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
393 if (result != llvm::MachO::HeaderMagic64
394 && result != llvm::MachO::HeaderMagic32
395 && result != llvm::MachO::HeaderMagic32Swapped
396 && result != llvm::MachO::HeaderMagic64Swapped)
397 {
398 return UUID();
399 }
400
401 // Read the mach header and see whether it looks like a kernel
402 llvm::MachO::mach_header header;
403 if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
404 return UUID();
405
406 if (header.magic == llvm::MachO::HeaderMagic32Swapped ||
407 header.magic == llvm::MachO::HeaderMagic64Swapped)
408 {
409 header.magic = llvm::ByteSwap_32(header.magic);
410 header.cputype = llvm::ByteSwap_32(header.cputype);
411 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
412 header.filetype = llvm::ByteSwap_32(header.filetype);
413 header.ncmds = llvm::ByteSwap_32(header.ncmds);
414 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
415 header.flags = llvm::ByteSwap_32(header.flags);
416 }
417
418 // A kernel is an executable which does not have the dynamic link object flag set.
419 if (header.filetype == llvm::MachO::HeaderFileTypeExecutable
420 && (header.flags & llvm::MachO::HeaderFlagBitIsDynamicLinkObject) == 0)
421 {
422 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000423 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000424 if (!memory_module_sp.get())
425 return UUID();
426
427 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
428 if (exe_objfile == NULL)
429 return UUID();
430
431 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
432 {
433 return memory_module_sp->GetUUID();
434 }
435 }
436
437 return UUID();
438}
439
440//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000441// Constructor
442//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000443DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000444 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000445 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000446 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000447 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000448 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000449 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000450 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000451 m_mutex(Mutex::eMutexTypeRecursive),
452 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000453{
454}
455
456//----------------------------------------------------------------------
457// Destructor
458//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000459DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000460{
461 Clear(true);
462}
463
Greg Clayton374972e2011-07-09 17:15:55 +0000464void
Greg Clayton944b8282011-08-22 22:30:57 +0000465DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000466{
467 LoadKernelModuleIfNeeded();
468 SetNotificationBreakpointIfNeeded ();
469}
Greg Clayton7b242382011-07-08 00:48:09 +0000470//------------------------------------------------------------------
471/// Called after attaching a process.
472///
473/// Allow DynamicLoader plug-ins to execute some code after
474/// attaching to a process.
475//------------------------------------------------------------------
476void
Greg Clayton944b8282011-08-22 22:30:57 +0000477DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000478{
479 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000480 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000481}
482
483//------------------------------------------------------------------
484/// Called after attaching a process.
485///
486/// Allow DynamicLoader plug-ins to execute some code after
487/// attaching to a process.
488//------------------------------------------------------------------
489void
Greg Clayton944b8282011-08-22 22:30:57 +0000490DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000491{
492 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000493 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000494}
495
496
497//----------------------------------------------------------------------
498// Clear out the state of this class.
499//----------------------------------------------------------------------
500void
Greg Clayton944b8282011-08-22 22:30:57 +0000501DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000502{
503 Mutex::Locker locker(m_mutex);
504
505 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
506 m_process->ClearBreakpointSiteByID(m_break_id);
507
508 if (clear_process)
509 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000510 m_kernel.Clear();
511 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000512 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000513 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000514 m_break_id = LLDB_INVALID_BREAK_ID;
515}
516
Greg Clayton7b242382011-07-08 00:48:09 +0000517
Greg Claytonc859e2d2012-02-13 23:10:39 +0000518bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000519DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000520{
521 if (IsLoaded())
522 return true;
523
Jason Molenda306bd0a2013-02-19 05:42:46 +0000524 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000525 {
526 bool changed = false;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000527 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
528 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000529 }
530 return false;
531}
532
Jason Molenda306bd0a2013-02-19 05:42:46 +0000533void
534DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
535{
536 m_module_sp = module_sp;
537 if (module_sp.get() && module_sp->GetObjectFile())
538 {
539 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
540 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
541 {
542 m_kernel_image = true;
543 }
544 else
545 {
546 m_kernel_image = false;
547 }
548 }
549}
550
551ModuleSP
552DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
553{
554 return m_module_sp;
555}
556
557void
558DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
559{
560 m_load_address = load_addr;
561}
562
563addr_t
564DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
565{
566 return m_load_address;
567}
568
569uint64_t
570DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
571{
572 return m_size;
573}
574
575void
576DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
577{
578 m_size = size;
579}
580
581uint32_t
582DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
583{
584 return m_load_process_stop_id;
585}
586
587void
588DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
589{
590 m_load_process_stop_id = stop_id;
591}
592
Greg Clayton2af282a2012-03-21 04:25:00 +0000593bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000594DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
595{
596 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
597 {
598 if (m_uuid == rhs.GetUUID())
599 {
600 return true;
601 }
602 return false;
603 }
604
605 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
606 return true;
607
608 return false;
609}
610
611void
612DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
613{
614 m_name = name;
615}
616
617std::string
618DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
619{
620 return m_name;
621}
622
623void
624DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
625{
626 m_uuid = uuid;
627}
628
629UUID
630DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
631{
632 return m_uuid;
633}
634
635// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
636// Module at that address. Require that the MemoryModule have a matching UUID and detect
637// if this MemoryModule is a kernel or a kext.
638//
639// Returns true if m_memory_module_sp is now set to a valid Module.
640
641bool
642DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
643{
644 if (m_memory_module_sp.get() != NULL)
645 return true;
646 if (m_load_address == LLDB_INVALID_ADDRESS)
647 return false;
648
649 FileSpec file_spec;
650 file_spec.SetFile (m_name.c_str(), false);
651
652 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
653
654 if (memory_module_sp.get() == NULL)
655 return false;
656
657 bool is_kernel = false;
658 if (memory_module_sp->GetObjectFile())
659 {
660 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
661 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
662 {
663 is_kernel = true;
664 }
665 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
666 {
667 is_kernel = false;
668 }
669 }
670
Jason Molenda38e70d12013-02-27 03:07:49 +0000671 // If this is a kext, and the kernel specified what UUID we should find at this
672 // load address, require that the memory module have a matching UUID or something
673 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000674 if (m_uuid.IsValid())
675 {
676 if (m_uuid != memory_module_sp->GetUUID())
677 {
678 return false;
679 }
680 }
681
682 // If the in-memory Module has a UUID, let's use that.
683 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
684 {
685 m_uuid = memory_module_sp->GetUUID();
686 }
687
688 m_memory_module_sp = memory_module_sp;
689 m_kernel_image = is_kernel;
690 if (is_kernel)
691 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000692 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000693 {
694 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
695 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000696 if (m_uuid.IsValid())
697 {
698 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
699 if (exe_module && exe_module->GetUUID().IsValid())
700 {
701 if (m_uuid != exe_module->GetUUID())
702 {
703 Stream *s = &process->GetTarget().GetDebugger().GetOutputStream();
704 if (s)
705 {
706 char memory_module_uuidbuf[64];
707 char exe_module_uuidbuf[64];
708 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",
709 exe_module->GetUUID().GetAsCString(exe_module_uuidbuf, sizeof (exe_module_uuidbuf)),
710 m_uuid.GetAsCString(memory_module_uuidbuf, sizeof (memory_module_uuidbuf)));
711 s->Flush ();
712 }
713 }
714 }
715 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000716 }
717
718 return true;
719}
720
721bool
722DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
723{
724 return m_kernel_image == true;
725}
726
727void
728DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
729{
730 m_kernel_image = is_kernel;
731}
732
733bool
734DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000735{
736 if (IsLoaded())
737 return true;
738
Greg Claytonc859e2d2012-02-13 23:10:39 +0000739
740 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000741
Jason Molenda306bd0a2013-02-19 05:42:46 +0000742 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
743 // have the correct segment load addresses.
744 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000745 {
746 return false;
747 }
748
Jason Molenda306bd0a2013-02-19 05:42:46 +0000749 bool uuid_is_valid = m_uuid.IsValid();
750
Jason Molendae575e7b2013-02-19 06:11:13 +0000751 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
752 {
753 Stream *s = &target.GetDebugger().GetOutputStream();
754 if (s)
755 {
756 char uuidbuf[64];
757 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsCString(uuidbuf, sizeof (uuidbuf)));
758 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
759 }
760 }
761
Jason Molenda306bd0a2013-02-19 05:42:46 +0000762 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000763 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000764 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
765 const ModuleList &target_images = target.GetImages();
766 m_module_sp = target_images.FindModule(m_uuid);
767
768 // Search for the kext on the local filesystem via the UUID
769 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000770 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000771 ModuleSpec module_spec;
772 module_spec.GetUUID() = m_uuid;
773 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000774
Jason Molendad76fb6e2013-02-19 07:16:22 +0000775 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
776 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000777 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000778 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000779 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000780 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000781 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000782 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000783 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
784 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
785 {
786 ModuleList loaded_module_list;
787 loaded_module_list.Append (m_module_sp);
788 target.ModulesDidLoad (loaded_module_list);
789 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000790 }
791 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000792 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000793
794 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000795 // This will search in the list of currently-loaded files, look in the
796 // standard search paths on the system, and on a Mac it will try calling
797 // the DebugSymbols framework with the UUID to find the binary via its
798 // search methods.
799 if (!m_module_sp)
800 {
801 m_module_sp = target.GetSharedModule (module_spec);
802 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000803
Jason Molendad76fb6e2013-02-19 07:16:22 +0000804 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000805 {
806 Stream *s = &target.GetDebugger().GetOutputStream();
807 if (s)
808 {
Jason Molenda56c23282013-02-19 06:39:56 +0000809 s->Printf ("WARNING: Unable to locate kernel binary on this system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000810 }
811 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000812 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000813
Jason Molenda306bd0a2013-02-19 05:42:46 +0000814 // If we managed to find a module, append it to the target's list of images.
815 // If we also have a memory module, require that they have matching UUIDs
816 if (m_module_sp)
817 {
818 bool uuid_match_ok = true;
819 if (m_memory_module_sp)
820 {
821 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000822 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000823 uuid_match_ok = false;
824 }
825 }
826 if (uuid_match_ok)
827 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000828 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000829 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
830 {
831 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000832 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000833 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000834 }
835 }
836
Jason Molenda56c23282013-02-19 06:39:56 +0000837 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
838 {
839 Stream *s = &target.GetDebugger().GetOutputStream();
840 if (s)
841 {
842 char uuidbuf[64];
843 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
844 m_name.c_str(), m_uuid.GetAsCString(uuidbuf, sizeof (uuidbuf)));
845 }
846 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000847
Greg Clayton67408532012-12-11 01:20:51 +0000848 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
849
Jason Molenda306bd0a2013-02-19 05:42:46 +0000850 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000851 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000852 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000853 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000854 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
855 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000856
Jason Molendabb860bd2012-10-09 01:17:11 +0000857 if (memory_object_file && ondisk_object_file)
858 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000859 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
860 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000861
Jason Molendabb860bd2012-10-09 01:17:11 +0000862 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
863 SectionList *memory_section_list = memory_object_file->GetSectionList ();
864 if (memory_section_list && ondisk_section_list)
865 {
866 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
867 // There may be CTF sections in the memory image so we can't
868 // always just compare the number of sections (which are actually
869 // segments in mach-o parlance)
870 uint32_t sect_idx = 0;
871
872 // Use the memory_module's addresses for each section to set the
873 // file module's load address as appropriate. We don't want to use
874 // a single slide value for the entire kext - different segments may
875 // be slid different amounts by the kext loader.
876
877 uint32_t num_sections_loaded = 0;
878 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
879 {
880 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
881 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000882 {
Greg Clayton67408532012-12-11 01:20:51 +0000883 // Don't ever load __LINKEDIT as it may or may not be actually
884 // mapped into memory and there is no current way to tell.
885 // I filed rdar://problem/12851706 to track being able to tell
886 // if the __LINKEDIT is actually mapped, but until then, we need
887 // to not load the __LINKEDIT
888 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
889 continue;
890
Jason Molendabb860bd2012-10-09 01:17:11 +0000891 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
892 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000893 {
Jason Molendabb860bd2012-10-09 01:17:11 +0000894 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
895 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000896 }
897 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000898 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000899 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000900 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000901 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000902 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000903 }
904 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000905 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000906 }
907 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000908 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000909 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000910 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000911 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000912 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000913
Greg Claytonc859e2d2012-02-13 23:10:39 +0000914 bool is_loaded = IsLoaded();
915
Jason Molenda306bd0a2013-02-19 05:42:46 +0000916 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000917 {
918 Stream *s = &target.GetDebugger().GetOutputStream();
919 if (s)
920 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000921 if (m_module_sp->GetFileSpec().GetDirectory().IsEmpty())
Jason Molenda743e43962012-10-02 22:23:42 +0000922 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000923 s->Printf ("Loaded kernel file %s\n", m_module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda743e43962012-10-02 22:23:42 +0000924 }
925 else
926 {
927 s->Printf ("Loaded kernel file %s/%s\n",
Jason Molenda306bd0a2013-02-19 05:42:46 +0000928 m_module_sp->GetFileSpec().GetDirectory().AsCString(),
929 m_module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda743e43962012-10-02 22:23:42 +0000930 }
Jason Molenda68b36072012-10-02 03:49:41 +0000931 s->Flush ();
932 }
933 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000934 return is_loaded;
935}
936
Greg Clayton1f746072012-08-29 21:13:06 +0000937uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000938DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000939{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000940 if (m_memory_module_sp)
941 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
942 if (m_module_sp)
943 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000944 return 0;
945}
946
947lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000948DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000949{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000950 if (m_memory_module_sp)
951 return m_memory_module_sp->GetArchitecture().GetByteOrder();
952 if (m_module_sp)
953 return m_module_sp->GetArchitecture().GetByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +0000954 return lldb::endian::InlHostByteOrder();
955}
956
957lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000958DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +0000959{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000960 if (m_memory_module_sp)
961 return m_memory_module_sp->GetArchitecture();
962 if (m_module_sp)
963 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +0000964 return lldb_private::ArchSpec ();
965}
966
967
Greg Clayton7b242382011-07-08 00:48:09 +0000968//----------------------------------------------------------------------
969// Load the kernel module and initialize the "m_kernel" member. Return
970// true _only_ if the kernel is loaded the first time through (subsequent
971// calls to this function should return false after the kernel has been
972// already loaded).
973//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000974void
Greg Clayton944b8282011-08-22 22:30:57 +0000975DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000976{
Greg Claytond16e1e52011-07-12 17:06:17 +0000977 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000978 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000979 m_kernel.Clear();
980 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
981 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000982
983 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +0000984 if (m_kernel.GetModule().get()
985 && m_kernel.GetModule()->GetObjectFile()
986 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000987 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000988 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000989 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000990 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000991
Jason Molenda306bd0a2013-02-19 05:42:46 +0000992 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000993 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000994 m_kernel.SetLoadAddress(m_kernel_load_address);
995 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +0000996 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000997 // We didn't get a hint from the process, so we will
998 // try the kernel at the address that it exists at in
999 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001000 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001001 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001002 {
1003 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1004 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1005 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1006 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001007 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001008 if (load_address != file_address)
1009 {
1010 // Don't accidentally relocate the kernel to the File address --
1011 // the Load address has already been set to its actual in-memory address.
1012 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001013 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001014 }
1015 }
1016 else
1017 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001018 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001019 }
1020 }
Greg Clayton7b242382011-07-08 00:48:09 +00001021 }
1022 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001023
Jason Molenda306bd0a2013-02-19 05:42:46 +00001024 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001025 {
1026 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1027 {
1028 m_kernel.LoadImageAtFileAddress (m_process);
1029 }
1030 }
Greg Clayton7b242382011-07-08 00:48:09 +00001031
Jason Molenda306bd0a2013-02-19 05:42:46 +00001032 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001033 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001034 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001035 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001036 if (symbol)
1037 {
Greg Claytone7612132012-03-07 21:03:09 +00001038 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001039 // Update all image infos
1040 ReadAllKextSummaries ();
1041 }
Greg Clayton7b242382011-07-08 00:48:09 +00001042 }
1043 else
Greg Clayton7b242382011-07-08 00:48:09 +00001044 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001045 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001046 }
1047 }
Greg Clayton7b242382011-07-08 00:48:09 +00001048}
1049
Greg Clayton7b242382011-07-08 00:48:09 +00001050//----------------------------------------------------------------------
1051// Static callback function that gets called when our DYLD notification
1052// breakpoint gets hit. We update all of our image infos and then
1053// let our super class DynamicLoader class decide if we should stop
1054// or not (based on global preference).
1055//----------------------------------------------------------------------
1056bool
Greg Clayton944b8282011-08-22 22:30:57 +00001057DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001058 StoppointCallbackContext *context,
1059 user_id_t break_id,
1060 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001061{
Greg Clayton944b8282011-08-22 22:30:57 +00001062 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001063}
1064
1065bool
Greg Clayton944b8282011-08-22 22:30:57 +00001066DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001067 user_id_t break_id,
1068 user_id_t break_loc_id)
1069{
Greg Claytond16e1e52011-07-12 17:06:17 +00001070 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
1071 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001072 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001073
Greg Clayton374972e2011-07-09 17:15:55 +00001074 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001075
1076 if (log)
1077 PutToLog(log.get());
1078
Greg Clayton374972e2011-07-09 17:15:55 +00001079 return GetStopWhenImagesChange();
1080}
1081
1082
1083bool
Greg Clayton944b8282011-08-22 22:30:57 +00001084DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001085{
1086 Mutex::Locker locker(m_mutex);
1087
1088 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001089
Greg Claytond16e1e52011-07-12 17:06:17 +00001090 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001091 {
1092 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1093 const ByteOrder byte_order = m_kernel.GetByteOrder();
1094 Error error;
1095 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
1096 // which is currenty 4 uint32_t and a pointer.
1097 uint8_t buf[24];
1098 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1099 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001100 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001101 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1102 prefer_file_cache,
1103 error,
1104 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001105 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001106 // We got a valid address for our kext summary header and make sure it isn't NULL
1107 if (m_kext_summary_header_addr.IsValid() &&
1108 m_kext_summary_header_addr.GetFileAddress() != 0)
1109 {
1110 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1111 if (bytes_read == count)
1112 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001113 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001114 m_kext_summary_header.version = data.GetU32(&offset);
1115 if (m_kext_summary_header.version >= 2)
1116 {
1117 m_kext_summary_header.entry_size = data.GetU32(&offset);
1118 }
1119 else
1120 {
1121 // Versions less than 2 didn't have an entry size, it was hard coded
1122 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1123 }
1124 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +00001125 return true;
1126 }
1127 }
Greg Clayton7b242382011-07-08 00:48:09 +00001128 }
1129 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001130 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001131 return false;
1132}
1133
Jason Molenda306bd0a2013-02-19 05:42:46 +00001134// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1135// and we need to figure out what kexts have been added or removed.
1136// Read the kext summaries from the inferior kernel memory, compare them against the
1137// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1138// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001139
1140bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001141DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001142{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001143 KextImageInfo::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +00001144 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001145 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001146 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001147
1148 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001149
1150 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1151 return false;
1152
Jason Molenda4da2e322013-02-26 00:26:47 +00001153 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1154 // kext loading, don't print any messages about kexts & don't try to read them.
1155 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1156
Jason Molenda306bd0a2013-02-19 05:42:46 +00001157 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1158 // we just found out about from ReadKextSummaries are marked as "add".
1159 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1160 std::vector<bool> to_be_added(count, true);
1161
1162 int number_of_new_kexts_being_added = 0;
1163 int number_of_old_kexts_being_removed = m_known_kexts.size();
1164
1165 const uint32_t new_kexts_size = kext_summaries.size();
1166 const uint32_t old_kexts_size = m_known_kexts.size();
1167
1168 // The m_known_kexts vector may have entries that have been Cleared,
1169 // or are a kernel.
1170 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1171 {
1172 bool ignore = false;
1173 KextImageInfo &image_info = m_known_kexts[old_kext];
1174 if (image_info.IsKernel())
1175 {
1176 ignore = true;
1177 }
1178 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1179 {
1180 ignore = true;
1181 }
1182
1183 if (ignore)
1184 {
1185 number_of_old_kexts_being_removed--;
1186 to_be_removed[old_kext] = false;
1187 }
1188 }
1189
1190 // Scan over the list of kexts we just read from the kernel, note those that
1191 // need to be added and those already loaded.
1192 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1193 {
1194 bool add_this_one = true;
1195 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1196 {
1197 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1198 {
1199 // We already have this kext, don't re-load it.
1200 to_be_added[new_kext] = false;
1201 // This kext is still present, do not remove it.
1202 to_be_removed[old_kext] = false;
1203
1204 number_of_old_kexts_being_removed--;
1205 add_this_one = false;
1206 break;
1207 }
1208 }
1209 if (add_this_one)
1210 {
1211 number_of_new_kexts_being_added++;
1212 }
1213 }
1214
1215 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1216 return true;
1217
Greg Clayton07e66e32011-07-20 03:41:06 +00001218 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Jason Molenda4da2e322013-02-26 00:26:47 +00001219 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001220 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001221 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1222 {
1223 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1224 }
1225 else if (number_of_new_kexts_being_added > 0)
1226 {
1227 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1228 }
1229 else if (number_of_old_kexts_being_removed > 0)
1230 {
1231 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1232 }
Greg Clayton7b242382011-07-08 00:48:09 +00001233 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001234
1235 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001236 {
1237 if (load_kexts)
1238 {
1239 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1240 }
1241 else
1242 {
1243 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);
1244 }
1245 }
1246
Jason Molenda306bd0a2013-02-19 05:42:46 +00001247
1248 if (number_of_new_kexts_being_added > 0)
1249 {
1250 ModuleList loaded_module_list;
1251
1252 const uint32_t num_of_new_kexts = kext_summaries.size();
1253 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1254 {
1255 if (to_be_added[new_kext] == true)
1256 {
1257 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001258 if (load_kexts)
1259 {
1260 if (!image_info.LoadImageUsingMemoryModule (m_process))
1261 {
1262 image_info.LoadImageAtFileAddress (m_process);
1263 }
1264 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001265
1266 m_known_kexts.push_back(image_info);
1267
1268 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1269 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1270
Jason Molenda4da2e322013-02-26 00:26:47 +00001271 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001272 s->Printf (".");
1273
1274 if (log)
1275 kext_summaries[new_kext].PutToLog (log.get());
1276 }
1277 }
1278 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1279 }
1280
1281 if (number_of_old_kexts_being_removed > 0)
1282 {
1283 ModuleList loaded_module_list;
1284 const uint32_t num_of_old_kexts = m_known_kexts.size();
1285 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1286 {
1287 ModuleList unloaded_module_list;
1288 if (to_be_removed[old_kext])
1289 {
1290 KextImageInfo &image_info = m_known_kexts[old_kext];
1291 // You can't unload the kernel.
1292 if (!image_info.IsKernel())
1293 {
1294 if (image_info.GetModule())
1295 {
1296 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1297 }
1298 if (s)
1299 s->Printf (".");
1300 image_info.Clear();
1301 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1302 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1303 }
1304 }
1305 m_process->GetTarget().ModulesDidUnload (unloaded_module_list);
1306 }
1307 }
1308
Jason Molenda4da2e322013-02-26 00:26:47 +00001309 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001310 {
1311 s->Printf (" done.\n");
1312 s->Flush ();
1313 }
1314
Jason Molenda306bd0a2013-02-19 05:42:46 +00001315 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001316}
1317
Greg Clayton7b242382011-07-08 00:48:09 +00001318uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001319DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001320 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001321 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001322{
1323 const ByteOrder endian = m_kernel.GetByteOrder();
1324 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1325
1326 image_infos.resize(image_infos_count);
1327 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1328 DataBufferHeap data(count, 0);
1329 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001330
Greg Clayton0d9fc762011-07-08 03:21:57 +00001331 const bool prefer_file_cache = false;
1332 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1333 prefer_file_cache,
1334 data.GetBytes(),
1335 data.GetByteSize(),
1336 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001337 if (bytes_read == count)
1338 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001339
Greg Clayton7b242382011-07-08 00:48:09 +00001340 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1341 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001342 for (uint32_t kext_summary_offset = 0;
1343 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1344 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001345 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001346 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001347 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1348 if (name_data == NULL)
1349 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001350 image_infos[i].SetName ((const char *) name_data);
1351 UUID uuid (extractor.GetData (&offset, 16), 16);
1352 image_infos[i].SetUUID (uuid);
1353 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1354 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001355 }
1356 if (i < image_infos.size())
1357 image_infos.resize(i);
1358 }
1359 else
1360 {
1361 image_infos.clear();
1362 }
1363 return image_infos.size();
1364}
1365
1366bool
Greg Clayton944b8282011-08-22 22:30:57 +00001367DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001368{
Greg Clayton374972e2011-07-09 17:15:55 +00001369 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001370
1371 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001372
Greg Clayton7b242382011-07-08 00:48:09 +00001373 if (ReadKextSummaryHeader ())
1374 {
Greg Clayton374972e2011-07-09 17:15:55 +00001375 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001376 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001377 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001378 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001379 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001380 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001381 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001382 }
1383 return true;
1384 }
1385 }
1386 return false;
1387}
1388
1389//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001390// Dump an image info structure to the file handle provided.
1391//----------------------------------------------------------------------
1392void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001393DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001394{
1395 if (log == NULL)
1396 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001397 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001398
Jason Molenda306bd0a2013-02-19 05:42:46 +00001399 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001400 {
1401 if (u)
1402 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001403 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 +00001404 u[ 0], u[ 1], u[ 2], u[ 3],
1405 u[ 4], u[ 5], u[ 6], u[ 7],
1406 u[ 8], u[ 9], u[10], u[11],
1407 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001408 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001409 }
1410 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001411 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001412 }
1413 else
1414 {
1415 if (u)
1416 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001417 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\"",
1418 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001419 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1420 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001421 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001422 }
1423 else
1424 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001425 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1426 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001427 }
Greg Clayton7b242382011-07-08 00:48:09 +00001428 }
1429}
1430
1431//----------------------------------------------------------------------
1432// Dump the _dyld_all_image_infos members and all current image infos
1433// that we have parsed to the file handle provided.
1434//----------------------------------------------------------------------
1435void
Greg Clayton944b8282011-08-22 22:30:57 +00001436DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001437{
1438 if (log == NULL)
1439 return;
1440
1441 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001442 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001443 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001444 m_kext_summary_header.version,
1445 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001446 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001447
1448 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001449 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001450 if (count > 0)
1451 {
1452 log->PutCString("Loaded:");
1453 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001454 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001455 }
1456}
1457
1458void
Greg Clayton944b8282011-08-22 22:30:57 +00001459DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001460{
Greg Clayton944b8282011-08-22 22:30:57 +00001461 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001462 Clear(true);
1463 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001464}
1465
Greg Clayton374972e2011-07-09 17:15:55 +00001466void
Greg Clayton944b8282011-08-22 22:30:57 +00001467DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001468{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001469 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001470 {
Greg Clayton944b8282011-08-22 22:30:57 +00001471 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001472
Greg Claytond16e1e52011-07-12 17:06:17 +00001473
Jim Inghama8558b62012-05-22 00:12:20 +00001474 const bool internal_bp = true;
Greg Claytond16e1e52011-07-12 17:06:17 +00001475 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001476 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001477 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001478 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001479 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001480 "OSKextLoadedKextSummariesUpdated",
1481 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +00001482 skip_prologue,
1483 internal_bp).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001484
Greg Clayton944b8282011-08-22 22:30:57 +00001485 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001486 m_break_id = bp->GetID();
1487 }
Greg Clayton7b242382011-07-08 00:48:09 +00001488}
1489
1490//----------------------------------------------------------------------
1491// Member function that gets called when the process state changes.
1492//----------------------------------------------------------------------
1493void
Greg Clayton944b8282011-08-22 22:30:57 +00001494DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001495{
Greg Clayton944b8282011-08-22 22:30:57 +00001496 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001497 switch (state)
1498 {
1499 case eStateConnected:
1500 case eStateAttaching:
1501 case eStateLaunching:
1502 case eStateInvalid:
1503 case eStateUnloaded:
1504 case eStateExited:
1505 case eStateDetached:
1506 Clear(false);
1507 break;
1508
1509 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001510 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001511 break;
1512
1513 case eStateRunning:
1514 case eStateStepping:
1515 case eStateCrashed:
1516 case eStateSuspended:
1517 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001518 }
1519}
1520
1521ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001522DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001523{
1524 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001525 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1526 if (log)
1527 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001528 return thread_plan_sp;
1529}
1530
1531Error
Greg Clayton944b8282011-08-22 22:30:57 +00001532DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001533{
1534 Error error;
1535 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1536 return error;
1537}
1538
1539void
Greg Clayton944b8282011-08-22 22:30:57 +00001540DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001541{
1542 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1543 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001544 CreateInstance,
1545 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001546}
1547
1548void
Greg Clayton944b8282011-08-22 22:30:57 +00001549DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001550{
1551 PluginManager::UnregisterPlugin (CreateInstance);
1552}
1553
Greg Claytone8cd0c92012-10-19 18:02:49 +00001554void
1555DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1556{
1557 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1558 {
1559 const bool is_global_setting = true;
1560 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1561 GetGlobalProperties()->GetValueProperties(),
1562 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1563 is_global_setting);
1564 }
1565}
Greg Clayton7b242382011-07-08 00:48:09 +00001566
1567const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001568DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001569{
Greg Clayton468ea4e2012-10-19 18:14:47 +00001570 return "dynamic-loader.darwin-kernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001571}
1572
1573const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001574DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001575{
1576 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1577}
1578
1579
1580//------------------------------------------------------------------
1581// PluginInterface protocol
1582//------------------------------------------------------------------
1583const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001584DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001585{
Greg Clayton944b8282011-08-22 22:30:57 +00001586 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001587}
1588
1589const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001590DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001591{
1592 return GetPluginNameStatic();
1593}
1594
1595uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001596DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001597{
1598 return 1;
1599}
1600
Greg Clayton1f746072012-08-29 21:13:06 +00001601lldb::ByteOrder
1602DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1603{
1604 switch (magic)
1605 {
1606 case llvm::MachO::HeaderMagic32:
1607 case llvm::MachO::HeaderMagic64:
1608 return lldb::endian::InlHostByteOrder();
1609
1610 case llvm::MachO::HeaderMagic32Swapped:
1611 case llvm::MachO::HeaderMagic64Swapped:
1612 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1613 return lldb::eByteOrderLittle;
1614 else
1615 return lldb::eByteOrderBig;
1616
1617 default:
1618 break;
1619 }
1620 return lldb::eByteOrderInvalid;
1621}
1622