blob: fa518d30aec073b838247a74c707b341fda16d18 [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
Jim Ingham46d005d2014-04-02 22:53:21 +000012#include "lldb/Utility/SafeMachO.h"
Greg Claytoneadcca92014-04-02 20:36:22 +000013
Greg Clayton7b242382011-07-08 00:48:09 +000014#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Core/DataBuffer.h"
16#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000017#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000018#include "lldb/Core/Log.h"
19#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/ModuleSpec.h"
Greg Clayton7b242382011-07-08 00:48:09 +000021#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Section.h"
Greg Clayton7b242382011-07-08 00:48:09 +000023#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000024#include "lldb/Core/StreamFile.h"
Jason Molenda68b36072012-10-02 03:49:41 +000025#include "lldb/Host/Symbols.h"
Greg Clayton7b242382011-07-08 00:48:09 +000026#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000027#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000028#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31#include "lldb/Target/ThreadPlanRunToAddress.h"
Greg Clayton57abc5d2013-05-10 21:47:16 +000032#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000033
Greg Clayton944b8282011-08-22 22:30:57 +000034#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000035
36//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
37#ifdef ENABLE_DEBUG_PRINTF
38#include <stdio.h>
39#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
40#else
41#define DEBUG_PRINTF(fmt, ...)
42#endif
43
44using namespace lldb;
45using namespace lldb_private;
46
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000047// Progressively greater amounts of scanning we will allow
48// For some targets very early in startup, we can't do any random reads of memory or we can crash the device
49// so a setting is needed that can completely disable the KASLR scans.
50
51enum KASLRScanType
52{
53 eKASLRScanNone = 0, // No reading into the inferior at all
54 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 +000055 eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000056 eKASLRScanExhaustiveScan // Scan through the entire possible kernel address range looking for a kernel
57};
58
59OptionEnumValueElement
60g_kaslr_kernel_scan_enum_values[] =
61{
62 { eKASLRScanNone, "none", "Do not read memory looking for a Darwin kernel when attaching." },
63 { eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
64 { eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find the Darwin kernel's load address."},
65 { eKASLRScanExhaustiveScan, "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
66 { 0, NULL, NULL }
67};
68
Greg Claytone8cd0c92012-10-19 18:02:49 +000069static PropertyDefinition
70g_properties[] =
71{
Greg Clayton66763ee2012-10-19 20:53:18 +000072 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000073 { "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 +000074 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000075};
76
77enum {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000078 ePropertyLoadKexts,
79 ePropertyScanType
Greg Claytone8cd0c92012-10-19 18:02:49 +000080};
81
82class DynamicLoaderDarwinKernelProperties : public Properties
83{
84public:
85
86 static ConstString &
87 GetSettingName ()
88 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000089 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000090 return g_setting_name;
91 }
92
93 DynamicLoaderDarwinKernelProperties() :
94 Properties ()
95 {
96 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
97 m_collection_sp->Initialize(g_properties);
98 }
99
100 virtual
101 ~DynamicLoaderDarwinKernelProperties()
102 {
103 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000104
Greg Claytone8cd0c92012-10-19 18:02:49 +0000105 bool
Greg Clayton66763ee2012-10-19 20:53:18 +0000106 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +0000107 {
Greg Clayton66763ee2012-10-19 20:53:18 +0000108 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000109 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
110 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000111
112 KASLRScanType
113 GetScanType() const
114 {
115 const uint32_t idx = ePropertyScanType;
Jason Molenda63969222013-01-30 04:48:16 +0000116 return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000117 }
118
119
Greg Claytone8cd0c92012-10-19 18:02:49 +0000120};
121
Greg Clayton7b0992d2013-04-18 22:45:39 +0000122typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000123
124static const DynamicLoaderDarwinKernelPropertiesSP &
125GetGlobalProperties()
126{
127 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
128 if (!g_settings_sp)
129 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
130 return g_settings_sp;
131}
132
Greg Clayton7b242382011-07-08 00:48:09 +0000133//----------------------------------------------------------------------
134// Create an instance of this class. This function is filled into
135// the plugin info class that gets handed out by the plugin factory and
136// allows the lldb to instantiate an instance of this class.
137//----------------------------------------------------------------------
138DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000139DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000140{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000141 if (!force)
Greg Clayton7b242382011-07-08 00:48:09 +0000142 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000143 // If the user provided an executable binary and it is not a kernel,
144 // this plugin should not create an instance.
Greg Claytonaa149cb2011-08-11 02:48:45 +0000145 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000146 if (exe_module)
147 {
148 ObjectFile *object_file = exe_module->GetObjectFile();
149 if (object_file)
150 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000151 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
152 {
153 return NULL;
154 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000155 }
156 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000157
158 // If the target's architecture does not look like an Apple environment,
159 // this plugin should not create an instance.
160 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
161 switch (triple_ref.getOS())
Greg Claytondf0b7d52011-07-08 04:11:42 +0000162 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000163 case llvm::Triple::Darwin:
164 case llvm::Triple::MacOSX:
165 case llvm::Triple::IOS:
166 if (triple_ref.getVendor() != llvm::Triple::Apple)
167 {
168 return NULL;
169 }
170 break;
171 // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
172 case llvm::Triple::UnknownOS:
173 break;
174 default:
175 return NULL;
176 break;
177 }
178 }
179
180 // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
181 // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
182
Jason Molenda503d0182013-03-02 07:19:32 +0000183 addr_t kernel_load_address = SearchForDarwinKernel (process);
184 if (kernel_load_address != LLDB_INVALID_ADDRESS)
185 {
186 process->SetCanJIT(false);
187 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
188 }
189 return NULL;
190}
191
192lldb::addr_t
193DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
194{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000195 addr_t kernel_load_address = process->GetImageInfoAddress();
196 if (kernel_load_address == LLDB_INVALID_ADDRESS)
197 {
198 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
199 if (kernel_load_address == LLDB_INVALID_ADDRESS)
200 {
201 kernel_load_address = SearchForKernelWithDebugHints (process);
202 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000203 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000204 kernel_load_address = SearchForKernelNearPC (process);
205 if (kernel_load_address == LLDB_INVALID_ADDRESS)
206 {
207 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
208 }
Greg Clayton70512312012-05-08 01:45:38 +0000209 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000210 }
Greg Clayton7b242382011-07-08 00:48:09 +0000211 }
Jason Molenda503d0182013-03-02 07:19:32 +0000212 return kernel_load_address;
Greg Clayton7b242382011-07-08 00:48:09 +0000213}
214
215//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000216// Check if the kernel binary is loaded in memory without a slide.
217// First verify that the ExecutableModule is a kernel before we proceed.
218// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
219//----------------------------------------------------------------------
220lldb::addr_t
221DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
222{
223 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
224 if (exe_module == NULL)
225 return LLDB_INVALID_ADDRESS;
226
227 ObjectFile *exe_objfile = exe_module->GetObjectFile();
228 if (exe_objfile == NULL)
229 return LLDB_INVALID_ADDRESS;
230
231 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
232 return LLDB_INVALID_ADDRESS;
233
234 if (!exe_objfile->GetHeaderAddress().IsValid())
235 return LLDB_INVALID_ADDRESS;
236
237 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
238 return exe_objfile->GetHeaderAddress().GetFileAddress();
239
240 return LLDB_INVALID_ADDRESS;
241}
242
243//----------------------------------------------------------------------
244// If the debug flag is included in the boot-args nvram setting, the kernel's load address
245// will be noted in the lowglo page at a fixed address
246// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
247//----------------------------------------------------------------------
248lldb::addr_t
249DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
250{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000251 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
252 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000253
254 Error read_err;
255 addr_t addr = LLDB_INVALID_ADDRESS;
256 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
257 {
258 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
259 }
260 else
261 {
262 addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
263 }
264
265 if (addr == 0)
266 addr = LLDB_INVALID_ADDRESS;
267
268 if (addr != LLDB_INVALID_ADDRESS)
269 {
270 if (CheckForKernelImageAtAddress (addr, process).IsValid())
271 return addr;
272 }
273
274 return LLDB_INVALID_ADDRESS;
275}
276
277//----------------------------------------------------------------------
278// If the kernel is currently executing when lldb attaches, and we don't have
279// a better way of finding the kernel's load address, try searching backwards
280// from the current pc value looking for the kernel's Mach header in memory.
281// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
282//----------------------------------------------------------------------
283lldb::addr_t
284DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
285{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000286 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
287 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
288 {
289 return LLDB_INVALID_ADDRESS;
290 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000291
292 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
293 if (thread.get() == NULL)
294 return LLDB_INVALID_ADDRESS;
295 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
296
297 if (pc == LLDB_INVALID_ADDRESS)
298 return LLDB_INVALID_ADDRESS;
299
Jason Molenda44edbf12013-04-19 00:50:28 +0000300 addr_t kernel_range_low;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000301 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
302 {
303 kernel_range_low = 1ULL << 63;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000304 }
305 else
306 {
307 kernel_range_low = 1ULL << 31;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000308 }
309
310 // Outside the normal kernel address range, this is probably userland code running right now
311 if (pc < kernel_range_low)
Jason Molenda44edbf12013-04-19 00:50:28 +0000312 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000313
314 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
315 // an offset of one page (0x1000) or two, depending on the device.
316
317 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
318 addr_t addr = pc & ~0xfffff;
319
320 int i = 0;
321 while (i < 32 && pc >= kernel_range_low)
322 {
323 if (CheckForKernelImageAtAddress (addr, process).IsValid())
324 return addr;
325 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
326 return addr + 0x1000;
327 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
328 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000329 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
330 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000331 i++;
332 addr -= 0x100000;
333 }
334
335 return LLDB_INVALID_ADDRESS;
336}
337
338//----------------------------------------------------------------------
339// Scan through the valid address range for a kernel binary.
340// This is uselessly slow in 64-bit environments so we don't even try it.
341// This scan is not enabled by default even for 32-bit targets.
342// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
343//----------------------------------------------------------------------
344lldb::addr_t
345DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
346{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000347 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
348 {
349 return LLDB_INVALID_ADDRESS;
350 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000351
352 addr_t kernel_range_low, kernel_range_high;
353 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
354 {
355 kernel_range_low = 1ULL << 63;
356 kernel_range_high = UINT64_MAX;
357 }
358 else
359 {
360 kernel_range_low = 1ULL << 31;
361 kernel_range_high = UINT32_MAX;
362 }
363
364 // Stepping through memory at one-megabyte resolution looking for a kernel
365 // rarely works (fast enough) with a 64-bit address space -- for now, let's
366 // not even bother. We may be attaching to something which *isn't* a kernel
367 // and we don't want to spin for minutes on-end looking for a kernel.
368 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
369 return LLDB_INVALID_ADDRESS;
370
371 addr_t addr = kernel_range_low;
372
373 while (addr >= kernel_range_low && addr < kernel_range_high)
374 {
375 if (CheckForKernelImageAtAddress (addr, process).IsValid())
376 return addr;
377 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
378 return addr + 0x1000;
379 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
380 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000381 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
382 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000383 addr += 0x100000;
384 }
385 return LLDB_INVALID_ADDRESS;
386}
387
388//----------------------------------------------------------------------
389// Given an address in memory, look to see if there is a kernel image at that
390// address.
391// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
392//----------------------------------------------------------------------
393lldb_private::UUID
394DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
395{
396 if (addr == LLDB_INVALID_ADDRESS)
397 return UUID();
398
399 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
400 // (the first field of the mach_header/mach_header_64 struct).
401
402 Error read_error;
403 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
Charles Davis510938e2013-08-27 05:04:57 +0000404 if (result != llvm::MachO::MH_MAGIC_64
405 && result != llvm::MachO::MH_MAGIC
406 && result != llvm::MachO::MH_CIGAM
407 && result != llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000408 {
409 return UUID();
410 }
411
412 // Read the mach header and see whether it looks like a kernel
413 llvm::MachO::mach_header header;
414 if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
415 return UUID();
416
Charles Davis510938e2013-08-27 05:04:57 +0000417 if (header.magic == llvm::MachO::MH_CIGAM ||
418 header.magic == llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000419 {
420 header.magic = llvm::ByteSwap_32(header.magic);
421 header.cputype = llvm::ByteSwap_32(header.cputype);
422 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
423 header.filetype = llvm::ByteSwap_32(header.filetype);
424 header.ncmds = llvm::ByteSwap_32(header.ncmds);
425 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
426 header.flags = llvm::ByteSwap_32(header.flags);
427 }
428
429 // A kernel is an executable which does not have the dynamic link object flag set.
Charles Davis510938e2013-08-27 05:04:57 +0000430 if (header.filetype == llvm::MachO::MH_EXECUTE
431 && (header.flags & llvm::MachO::MH_DYLDLINK) == 0)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000432 {
433 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000434 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000435 if (!memory_module_sp.get())
436 return UUID();
437
438 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
439 if (exe_objfile == NULL)
440 return UUID();
441
442 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
443 {
Jason Molendaa4ce2532013-05-02 22:02:57 +0000444 ArchSpec kernel_arch (eArchTypeMachO, header.cputype, header.cpusubtype);
445 if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
446 {
447 process->GetTarget().SetArchitecture (kernel_arch);
448 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000449 return memory_module_sp->GetUUID();
450 }
451 }
452
453 return UUID();
454}
455
456//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000457// Constructor
458//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000459DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000460 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000461 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000462 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000463 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000464 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000465 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000466 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000467 m_mutex(Mutex::eMutexTypeRecursive),
468 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000469{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000470 PlatformSP platform_sp(Platform::FindPlugin (process, PlatformDarwinKernel::GetPluginNameStatic ()));
Jason Molenda1c627542013-04-05 01:03:25 +0000471 // Only select the darwin-kernel Platform if we've been asked to load kexts.
472 // It can take some time to scan over all of the kext info.plists and that
473 // shouldn't be done if kext loading is explicitly disabled.
474 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
475 {
476 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000477 }
Greg Clayton7b242382011-07-08 00:48:09 +0000478}
479
480//----------------------------------------------------------------------
481// Destructor
482//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000483DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000484{
485 Clear(true);
486}
487
Greg Clayton374972e2011-07-09 17:15:55 +0000488void
Greg Clayton944b8282011-08-22 22:30:57 +0000489DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000490{
491 LoadKernelModuleIfNeeded();
492 SetNotificationBreakpointIfNeeded ();
493}
Greg Clayton7b242382011-07-08 00:48:09 +0000494//------------------------------------------------------------------
495/// Called after attaching a process.
496///
497/// Allow DynamicLoader plug-ins to execute some code after
498/// attaching to a process.
499//------------------------------------------------------------------
500void
Greg Clayton944b8282011-08-22 22:30:57 +0000501DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000502{
503 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000504 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000505}
506
507//------------------------------------------------------------------
508/// Called after attaching a process.
509///
510/// Allow DynamicLoader plug-ins to execute some code after
511/// attaching to a process.
512//------------------------------------------------------------------
513void
Greg Clayton944b8282011-08-22 22:30:57 +0000514DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000515{
516 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000517 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000518}
519
520
521//----------------------------------------------------------------------
522// Clear out the state of this class.
523//----------------------------------------------------------------------
524void
Greg Clayton944b8282011-08-22 22:30:57 +0000525DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000526{
527 Mutex::Locker locker(m_mutex);
528
529 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
530 m_process->ClearBreakpointSiteByID(m_break_id);
531
532 if (clear_process)
533 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000534 m_kernel.Clear();
535 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000536 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000537 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000538 m_break_id = LLDB_INVALID_BREAK_ID;
539}
540
Greg Clayton7b242382011-07-08 00:48:09 +0000541
Greg Claytonc859e2d2012-02-13 23:10:39 +0000542bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000543DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000544{
545 if (IsLoaded())
546 return true;
547
Jason Molenda306bd0a2013-02-19 05:42:46 +0000548 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000549 {
550 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +0000551 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
Jason Molenda306bd0a2013-02-19 05:42:46 +0000552 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000553 }
554 return false;
555}
556
Jason Molenda306bd0a2013-02-19 05:42:46 +0000557void
558DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
559{
560 m_module_sp = module_sp;
561 if (module_sp.get() && module_sp->GetObjectFile())
562 {
563 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
564 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
565 {
566 m_kernel_image = true;
567 }
568 else
569 {
570 m_kernel_image = false;
571 }
572 }
573}
574
575ModuleSP
576DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
577{
578 return m_module_sp;
579}
580
581void
582DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
583{
584 m_load_address = load_addr;
585}
586
587addr_t
588DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
589{
590 return m_load_address;
591}
592
593uint64_t
594DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
595{
596 return m_size;
597}
598
599void
600DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
601{
602 m_size = size;
603}
604
605uint32_t
606DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
607{
608 return m_load_process_stop_id;
609}
610
611void
612DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
613{
614 m_load_process_stop_id = stop_id;
615}
616
Greg Clayton2af282a2012-03-21 04:25:00 +0000617bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000618DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
619{
620 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
621 {
622 if (m_uuid == rhs.GetUUID())
623 {
624 return true;
625 }
626 return false;
627 }
628
629 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
630 return true;
631
632 return false;
633}
634
635void
636DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
637{
638 m_name = name;
639}
640
641std::string
642DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
643{
644 return m_name;
645}
646
647void
648DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
649{
650 m_uuid = uuid;
651}
652
653UUID
654DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
655{
656 return m_uuid;
657}
658
659// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
660// Module at that address. Require that the MemoryModule have a matching UUID and detect
661// if this MemoryModule is a kernel or a kext.
662//
663// Returns true if m_memory_module_sp is now set to a valid Module.
664
665bool
666DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
667{
668 if (m_memory_module_sp.get() != NULL)
669 return true;
670 if (m_load_address == LLDB_INVALID_ADDRESS)
671 return false;
672
673 FileSpec file_spec;
674 file_spec.SetFile (m_name.c_str(), false);
675
676 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
677
678 if (memory_module_sp.get() == NULL)
679 return false;
680
681 bool is_kernel = false;
682 if (memory_module_sp->GetObjectFile())
683 {
684 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
685 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
686 {
687 is_kernel = true;
688 }
689 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
690 {
691 is_kernel = false;
692 }
693 }
694
Jason Molenda38e70d12013-02-27 03:07:49 +0000695 // If this is a kext, and the kernel specified what UUID we should find at this
696 // load address, require that the memory module have a matching UUID or something
697 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000698 if (m_uuid.IsValid())
699 {
700 if (m_uuid != memory_module_sp->GetUUID())
701 {
702 return false;
703 }
704 }
705
706 // If the in-memory Module has a UUID, let's use that.
707 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
708 {
709 m_uuid = memory_module_sp->GetUUID();
710 }
711
712 m_memory_module_sp = memory_module_sp;
713 m_kernel_image = is_kernel;
714 if (is_kernel)
715 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000716 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000717 {
718 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
719 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000720 if (m_uuid.IsValid())
721 {
722 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
723 if (exe_module && exe_module->GetUUID().IsValid())
724 {
725 if (m_uuid != exe_module->GetUUID())
726 {
Greg Clayton44d93782014-01-27 23:43:24 +0000727 Stream *s = process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda38e70d12013-02-27 03:07:49 +0000728 if (s)
729 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000730 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",
Jason Molendac16b4af2013-05-03 23:56:12 +0000731 exe_module->GetUUID().GetAsString().c_str(),
732 m_uuid.GetAsString().c_str());
Jason Molenda38e70d12013-02-27 03:07:49 +0000733 s->Flush ();
734 }
735 }
736 }
737 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000738 }
739
740 return true;
741}
742
743bool
744DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
745{
746 return m_kernel_image == true;
747}
748
749void
750DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
751{
752 m_kernel_image = is_kernel;
753}
754
755bool
756DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000757{
758 if (IsLoaded())
759 return true;
760
Greg Claytonc859e2d2012-02-13 23:10:39 +0000761
762 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000763
Jason Molenda306bd0a2013-02-19 05:42:46 +0000764 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
765 // have the correct segment load addresses.
766 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000767 {
768 return false;
769 }
770
Jason Molenda306bd0a2013-02-19 05:42:46 +0000771 bool uuid_is_valid = m_uuid.IsValid();
772
Jason Molendae575e7b2013-02-19 06:11:13 +0000773 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
774 {
Greg Clayton44d93782014-01-27 23:43:24 +0000775 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000776 if (s)
777 {
Jason Molendac16b4af2013-05-03 23:56:12 +0000778 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
Jason Molendae575e7b2013-02-19 06:11:13 +0000779 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
780 }
781 }
782
Jason Molenda306bd0a2013-02-19 05:42:46 +0000783 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000784 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000785 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
786 const ModuleList &target_images = target.GetImages();
787 m_module_sp = target_images.FindModule(m_uuid);
788
789 // Search for the kext on the local filesystem via the UUID
790 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000791 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000792 ModuleSpec module_spec;
793 module_spec.GetUUID() = m_uuid;
794 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000795
Jason Molendad76fb6e2013-02-19 07:16:22 +0000796 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
797 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000798 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000799 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000800 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000801 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000802 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000803 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000804 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
805 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
806 {
807 ModuleList loaded_module_list;
808 loaded_module_list.Append (m_module_sp);
809 target.ModulesDidLoad (loaded_module_list);
810 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000811 }
812 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000813 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000814
Jason Molenda1c627542013-04-05 01:03:25 +0000815 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
816 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
817 // to find it.
818 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0ddfe7f2014-03-05 03:33:01 +0000819 if (!m_module_sp && platform_sp)
Jason Molenda1c627542013-04-05 01:03:25 +0000820 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000821 ConstString platform_name (platform_sp->GetPluginName());
822 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
823 if (platform_name == g_platform_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000824 {
825 ModuleSpec kext_bundle_module_spec(module_spec);
826 FileSpec kext_filespec(m_name.c_str(), false);
827 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
828 platform_sp->GetSharedModule (kext_bundle_module_spec, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
829 }
830 }
831
Jason Molendad76fb6e2013-02-19 07:16:22 +0000832 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000833 // This will search in the list of currently-loaded files, look in the
834 // standard search paths on the system, and on a Mac it will try calling
835 // the DebugSymbols framework with the UUID to find the binary via its
836 // search methods.
837 if (!m_module_sp)
838 {
839 m_module_sp = target.GetSharedModule (module_spec);
840 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000841
Jason Molendad76fb6e2013-02-19 07:16:22 +0000842 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000843 {
Greg Clayton44d93782014-01-27 23:43:24 +0000844 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000845 if (s)
846 {
Jason Molendacc6dc782013-04-30 22:38:28 +0000847 s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000848 }
849 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000850 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000851
Jason Molenda306bd0a2013-02-19 05:42:46 +0000852 // If we managed to find a module, append it to the target's list of images.
853 // If we also have a memory module, require that they have matching UUIDs
854 if (m_module_sp)
855 {
856 bool uuid_match_ok = true;
857 if (m_memory_module_sp)
858 {
859 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000860 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000861 uuid_match_ok = false;
862 }
863 }
864 if (uuid_match_ok)
865 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000866 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000867 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
868 {
869 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000870 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000871 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000872 }
873 }
874
Jason Molenda56c23282013-02-19 06:39:56 +0000875 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
876 {
Greg Clayton44d93782014-01-27 23:43:24 +0000877 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda56c23282013-02-19 06:39:56 +0000878 if (s)
879 {
Jason Molenda56c23282013-02-19 06:39:56 +0000880 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
Jason Molendac16b4af2013-05-03 23:56:12 +0000881 m_name.c_str(), m_uuid.GetAsString().c_str());
Jason Molenda56c23282013-02-19 06:39:56 +0000882 }
883 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000884
Greg Clayton67408532012-12-11 01:20:51 +0000885 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
886
Jason Molenda306bd0a2013-02-19 05:42:46 +0000887 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000888 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000889 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000890 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000891 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
892 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000893
Jason Molendabb860bd2012-10-09 01:17:11 +0000894 if (memory_object_file && ondisk_object_file)
895 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000896 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
897 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000898
Jason Molendabb860bd2012-10-09 01:17:11 +0000899 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
900 SectionList *memory_section_list = memory_object_file->GetSectionList ();
901 if (memory_section_list && ondisk_section_list)
902 {
903 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
904 // There may be CTF sections in the memory image so we can't
905 // always just compare the number of sections (which are actually
906 // segments in mach-o parlance)
907 uint32_t sect_idx = 0;
908
909 // Use the memory_module's addresses for each section to set the
910 // file module's load address as appropriate. We don't want to use
911 // a single slide value for the entire kext - different segments may
912 // be slid different amounts by the kext loader.
913
914 uint32_t num_sections_loaded = 0;
915 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
916 {
917 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
918 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000919 {
Greg Clayton67408532012-12-11 01:20:51 +0000920 // Don't ever load __LINKEDIT as it may or may not be actually
921 // mapped into memory and there is no current way to tell.
922 // I filed rdar://problem/12851706 to track being able to tell
923 // if the __LINKEDIT is actually mapped, but until then, we need
924 // to not load the __LINKEDIT
925 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
926 continue;
927
Jason Molendabb860bd2012-10-09 01:17:11 +0000928 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
929 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000930 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000931 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
Jason Molendabb860bd2012-10-09 01:17:11 +0000932 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000933 }
934 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000935 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000936 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000937 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000938 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000939 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000940 }
941 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000942 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000943 }
944 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000945 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000946 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000947 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000948 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000949 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000950
Greg Claytonc859e2d2012-02-13 23:10:39 +0000951 bool is_loaded = IsLoaded();
952
Jason Molenda306bd0a2013-02-19 05:42:46 +0000953 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000954 {
Greg Clayton44d93782014-01-27 23:43:24 +0000955 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda68b36072012-10-02 03:49:41 +0000956 if (s)
957 {
Jason Molenda732238c2013-03-01 00:06:37 +0000958 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
959 if (kernel_object_file)
960 {
961 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
962 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
963 {
Matt Kopec787d1622013-03-12 17:45:38 +0000964 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000965 }
966 }
Jason Molenda743e43962012-10-02 22:23:42 +0000967 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000968 s->Printf ("Loaded kernel file %s\n",
969 m_module_sp->GetFileSpec().GetPath().c_str());
Jason Molenda743e43962012-10-02 22:23:42 +0000970 }
Jason Molenda68b36072012-10-02 03:49:41 +0000971 s->Flush ();
972 }
973 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000974 return is_loaded;
975}
976
Greg Clayton1f746072012-08-29 21:13:06 +0000977uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000978DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000979{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000980 if (m_memory_module_sp)
981 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
982 if (m_module_sp)
983 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000984 return 0;
985}
986
987lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000988DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000989{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000990 if (m_memory_module_sp)
991 return m_memory_module_sp->GetArchitecture().GetByteOrder();
992 if (m_module_sp)
993 return m_module_sp->GetArchitecture().GetByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +0000994 return lldb::endian::InlHostByteOrder();
995}
996
997lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000998DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +0000999{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001000 if (m_memory_module_sp)
1001 return m_memory_module_sp->GetArchitecture();
1002 if (m_module_sp)
1003 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001004 return lldb_private::ArchSpec ();
1005}
1006
1007
Greg Clayton7b242382011-07-08 00:48:09 +00001008//----------------------------------------------------------------------
1009// Load the kernel module and initialize the "m_kernel" member. Return
1010// true _only_ if the kernel is loaded the first time through (subsequent
1011// calls to this function should return false after the kernel has been
1012// already loaded).
1013//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001014void
Greg Clayton944b8282011-08-22 22:30:57 +00001015DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001016{
Greg Claytond16e1e52011-07-12 17:06:17 +00001017 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001018 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001019 m_kernel.Clear();
1020 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1021 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001022
1023 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001024 if (m_kernel.GetModule().get()
1025 && m_kernel.GetModule()->GetObjectFile()
1026 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001027 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001028 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001029 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001030 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001031
Jason Molenda306bd0a2013-02-19 05:42:46 +00001032 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001033 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001034 m_kernel.SetLoadAddress(m_kernel_load_address);
1035 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001036 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001037 // We didn't get a hint from the process, so we will
1038 // try the kernel at the address that it exists at in
1039 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001040 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001041 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001042 {
1043 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1044 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1045 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1046 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001047 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001048 if (load_address != file_address)
1049 {
1050 // Don't accidentally relocate the kernel to the File address --
1051 // the Load address has already been set to its actual in-memory address.
1052 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001053 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001054 }
1055 }
1056 else
1057 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001058 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001059 }
1060 }
Greg Clayton7b242382011-07-08 00:48:09 +00001061 }
1062 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001063
Jason Molenda306bd0a2013-02-19 05:42:46 +00001064 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001065 {
1066 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1067 {
1068 m_kernel.LoadImageAtFileAddress (m_process);
1069 }
1070 }
Greg Clayton7b242382011-07-08 00:48:09 +00001071
Jason Molenda306bd0a2013-02-19 05:42:46 +00001072 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001073 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001074 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001075 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001076 if (symbol)
1077 {
Greg Claytone7612132012-03-07 21:03:09 +00001078 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001079 // Update all image infos
1080 ReadAllKextSummaries ();
1081 }
Greg Clayton7b242382011-07-08 00:48:09 +00001082 }
1083 else
Greg Clayton7b242382011-07-08 00:48:09 +00001084 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001085 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001086 }
1087 }
Greg Clayton7b242382011-07-08 00:48:09 +00001088}
1089
Greg Clayton7b242382011-07-08 00:48:09 +00001090//----------------------------------------------------------------------
1091// Static callback function that gets called when our DYLD notification
1092// breakpoint gets hit. We update all of our image infos and then
1093// let our super class DynamicLoader class decide if we should stop
1094// or not (based on global preference).
1095//----------------------------------------------------------------------
1096bool
Greg Clayton944b8282011-08-22 22:30:57 +00001097DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001098 StoppointCallbackContext *context,
1099 user_id_t break_id,
1100 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001101{
Greg Clayton944b8282011-08-22 22:30:57 +00001102 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001103}
1104
1105bool
Greg Clayton944b8282011-08-22 22:30:57 +00001106DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001107 user_id_t break_id,
1108 user_id_t break_loc_id)
1109{
Greg Clayton5160ce52013-03-27 23:08:40 +00001110 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001111 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001112 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001113
Greg Clayton374972e2011-07-09 17:15:55 +00001114 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001115
1116 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001117 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001118
Greg Clayton374972e2011-07-09 17:15:55 +00001119 return GetStopWhenImagesChange();
1120}
1121
1122
1123bool
Greg Clayton944b8282011-08-22 22:30:57 +00001124DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001125{
1126 Mutex::Locker locker(m_mutex);
1127
1128 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001129
Greg Claytond16e1e52011-07-12 17:06:17 +00001130 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001131 {
1132 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1133 const ByteOrder byte_order = m_kernel.GetByteOrder();
1134 Error error;
1135 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001136 // which is currently 4 uint32_t and a pointer.
Greg Clayton7b242382011-07-08 00:48:09 +00001137 uint8_t buf[24];
1138 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1139 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001140 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001141 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1142 prefer_file_cache,
1143 error,
1144 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001145 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001146 // We got a valid address for our kext summary header and make sure it isn't NULL
1147 if (m_kext_summary_header_addr.IsValid() &&
1148 m_kext_summary_header_addr.GetFileAddress() != 0)
1149 {
1150 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1151 if (bytes_read == count)
1152 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001153 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001154 m_kext_summary_header.version = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001155 if (m_kext_summary_header.version > 128)
1156 {
1157 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1158 s->Printf ("WARNING: Unable to read kext summary header, got improbable version number %u\n", m_kext_summary_header.version);
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001159 // If we get an improbably large version number, we're probably getting bad memory.
Jason Molendac6fa5db2014-04-15 01:04:00 +00001160 m_kext_summary_header_addr.Clear();
1161 return false;
1162 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001163 if (m_kext_summary_header.version >= 2)
1164 {
1165 m_kext_summary_header.entry_size = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001166 if (m_kext_summary_header.entry_size > 4096)
1167 {
1168 // If we get an improbably large entry_size, we're probably getting bad memory.
1169 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1170 s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1171 m_kext_summary_header_addr.Clear();
1172 return false;
1173 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001174 }
1175 else
1176 {
1177 // Versions less than 2 didn't have an entry size, it was hard coded
1178 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1179 }
1180 m_kext_summary_header.entry_count = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001181 if (m_kext_summary_header.entry_count > 10000)
1182 {
1183 // If we get an improbably large number of kexts, we're probably getting bad memory.
1184 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1185 s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1186 m_kext_summary_header_addr.Clear();
1187 return false;
1188 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001189 return true;
1190 }
1191 }
Greg Clayton7b242382011-07-08 00:48:09 +00001192 }
1193 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001194 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001195 return false;
1196}
1197
Jason Molenda306bd0a2013-02-19 05:42:46 +00001198// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1199// and we need to figure out what kexts have been added or removed.
1200// Read the kext summaries from the inferior kernel memory, compare them against the
1201// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1202// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001203
1204bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001205DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001206{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001207 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001208 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001209 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001210 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001211
1212 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001213
1214 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1215 return false;
1216
Jason Molenda4da2e322013-02-26 00:26:47 +00001217 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1218 // kext loading, don't print any messages about kexts & don't try to read them.
1219 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1220
Jason Molenda306bd0a2013-02-19 05:42:46 +00001221 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1222 // we just found out about from ReadKextSummaries are marked as "add".
1223 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1224 std::vector<bool> to_be_added(count, true);
1225
1226 int number_of_new_kexts_being_added = 0;
1227 int number_of_old_kexts_being_removed = m_known_kexts.size();
1228
1229 const uint32_t new_kexts_size = kext_summaries.size();
1230 const uint32_t old_kexts_size = m_known_kexts.size();
1231
1232 // The m_known_kexts vector may have entries that have been Cleared,
1233 // or are a kernel.
1234 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1235 {
1236 bool ignore = false;
1237 KextImageInfo &image_info = m_known_kexts[old_kext];
1238 if (image_info.IsKernel())
1239 {
1240 ignore = true;
1241 }
1242 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1243 {
1244 ignore = true;
1245 }
1246
1247 if (ignore)
1248 {
1249 number_of_old_kexts_being_removed--;
1250 to_be_removed[old_kext] = false;
1251 }
1252 }
1253
1254 // Scan over the list of kexts we just read from the kernel, note those that
1255 // need to be added and those already loaded.
1256 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1257 {
1258 bool add_this_one = true;
1259 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1260 {
1261 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1262 {
1263 // We already have this kext, don't re-load it.
1264 to_be_added[new_kext] = false;
1265 // This kext is still present, do not remove it.
1266 to_be_removed[old_kext] = false;
1267
1268 number_of_old_kexts_being_removed--;
1269 add_this_one = false;
1270 break;
1271 }
1272 }
1273 if (add_this_one)
1274 {
1275 number_of_new_kexts_being_added++;
1276 }
1277 }
1278
1279 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1280 return true;
1281
Greg Clayton44d93782014-01-27 23:43:24 +00001282 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda4da2e322013-02-26 00:26:47 +00001283 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001284 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001285 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1286 {
1287 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1288 }
1289 else if (number_of_new_kexts_being_added > 0)
1290 {
1291 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1292 }
1293 else if (number_of_old_kexts_being_removed > 0)
1294 {
1295 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1296 }
Greg Clayton7b242382011-07-08 00:48:09 +00001297 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001298
1299 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001300 {
1301 if (load_kexts)
1302 {
1303 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1304 }
1305 else
1306 {
1307 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);
1308 }
1309 }
1310
Jason Molenda306bd0a2013-02-19 05:42:46 +00001311
1312 if (number_of_new_kexts_being_added > 0)
1313 {
1314 ModuleList loaded_module_list;
1315
1316 const uint32_t num_of_new_kexts = kext_summaries.size();
1317 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1318 {
1319 if (to_be_added[new_kext] == true)
1320 {
1321 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001322 if (load_kexts)
1323 {
1324 if (!image_info.LoadImageUsingMemoryModule (m_process))
1325 {
1326 image_info.LoadImageAtFileAddress (m_process);
1327 }
1328 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001329
1330 m_known_kexts.push_back(image_info);
1331
1332 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1333 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1334
Jason Molenda4da2e322013-02-26 00:26:47 +00001335 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001336 s->Printf (".");
1337
1338 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001339 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001340 }
1341 }
1342 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1343 }
1344
1345 if (number_of_old_kexts_being_removed > 0)
1346 {
1347 ModuleList loaded_module_list;
1348 const uint32_t num_of_old_kexts = m_known_kexts.size();
1349 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1350 {
1351 ModuleList unloaded_module_list;
1352 if (to_be_removed[old_kext])
1353 {
1354 KextImageInfo &image_info = m_known_kexts[old_kext];
1355 // You can't unload the kernel.
1356 if (!image_info.IsKernel())
1357 {
1358 if (image_info.GetModule())
1359 {
1360 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1361 }
1362 if (s)
1363 s->Printf (".");
1364 image_info.Clear();
1365 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1366 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1367 }
1368 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001369 m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001370 }
1371 }
1372
Jason Molenda4da2e322013-02-26 00:26:47 +00001373 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001374 {
1375 s->Printf (" done.\n");
1376 s->Flush ();
1377 }
1378
Jason Molenda306bd0a2013-02-19 05:42:46 +00001379 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001380}
1381
Greg Clayton7b242382011-07-08 00:48:09 +00001382uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001383DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001384 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001385 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001386{
1387 const ByteOrder endian = m_kernel.GetByteOrder();
1388 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1389
1390 image_infos.resize(image_infos_count);
1391 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1392 DataBufferHeap data(count, 0);
1393 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001394
Greg Clayton0d9fc762011-07-08 03:21:57 +00001395 const bool prefer_file_cache = false;
1396 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1397 prefer_file_cache,
1398 data.GetBytes(),
1399 data.GetByteSize(),
1400 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001401 if (bytes_read == count)
1402 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001403
Greg Clayton7b242382011-07-08 00:48:09 +00001404 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1405 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001406 for (uint32_t kext_summary_offset = 0;
1407 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1408 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001409 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001410 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001411 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1412 if (name_data == NULL)
1413 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001414 image_infos[i].SetName ((const char *) name_data);
1415 UUID uuid (extractor.GetData (&offset, 16), 16);
1416 image_infos[i].SetUUID (uuid);
1417 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1418 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001419 }
1420 if (i < image_infos.size())
1421 image_infos.resize(i);
1422 }
1423 else
1424 {
1425 image_infos.clear();
1426 }
1427 return image_infos.size();
1428}
1429
1430bool
Greg Clayton944b8282011-08-22 22:30:57 +00001431DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001432{
Greg Clayton7b242382011-07-08 00:48:09 +00001433 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001434
Greg Clayton7b242382011-07-08 00:48:09 +00001435 if (ReadKextSummaryHeader ())
1436 {
Greg Clayton374972e2011-07-09 17:15:55 +00001437 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001438 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001439 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001440 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001441 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001442 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001443 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001444 }
1445 return true;
1446 }
1447 }
1448 return false;
1449}
1450
1451//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001452// Dump an image info structure to the file handle provided.
1453//----------------------------------------------------------------------
1454void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001455DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001456{
1457 if (log == NULL)
1458 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001459 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001460
Jason Molenda306bd0a2013-02-19 05:42:46 +00001461 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001462 {
1463 if (u)
1464 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001465 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 +00001466 u[ 0], u[ 1], u[ 2], u[ 3],
1467 u[ 4], u[ 5], u[ 6], u[ 7],
1468 u[ 8], u[ 9], u[10], u[11],
1469 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001470 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001471 }
1472 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001473 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001474 }
1475 else
1476 {
1477 if (u)
1478 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001479 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\"",
1480 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001481 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1482 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001483 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001484 }
1485 else
1486 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001487 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1488 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001489 }
Greg Clayton7b242382011-07-08 00:48:09 +00001490 }
1491}
1492
1493//----------------------------------------------------------------------
1494// Dump the _dyld_all_image_infos members and all current image infos
1495// that we have parsed to the file handle provided.
1496//----------------------------------------------------------------------
1497void
Greg Clayton944b8282011-08-22 22:30:57 +00001498DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001499{
1500 if (log == NULL)
1501 return;
1502
1503 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001504 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001505 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001506 m_kext_summary_header.version,
1507 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001508 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001509
1510 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001511 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001512 if (count > 0)
1513 {
1514 log->PutCString("Loaded:");
1515 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001516 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001517 }
1518}
1519
1520void
Greg Clayton944b8282011-08-22 22:30:57 +00001521DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001522{
Greg Clayton944b8282011-08-22 22:30:57 +00001523 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001524 Clear(true);
1525 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001526}
1527
Greg Clayton374972e2011-07-09 17:15:55 +00001528void
Greg Clayton944b8282011-08-22 22:30:57 +00001529DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001530{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001531 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001532 {
Greg Clayton944b8282011-08-22 22:30:57 +00001533 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001534
Greg Claytond16e1e52011-07-12 17:06:17 +00001535
Jim Inghama8558b62012-05-22 00:12:20 +00001536 const bool internal_bp = true;
Greg Claytoneb023e72013-10-11 19:48:25 +00001537 const bool hardware = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001538 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001539 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001540 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001541 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001542 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001543 "OSKextLoadedKextSummariesUpdated",
1544 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +00001545 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001546 internal_bp,
1547 hardware).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001548
Greg Clayton944b8282011-08-22 22:30:57 +00001549 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001550 m_break_id = bp->GetID();
1551 }
Greg Clayton7b242382011-07-08 00:48:09 +00001552}
1553
1554//----------------------------------------------------------------------
1555// Member function that gets called when the process state changes.
1556//----------------------------------------------------------------------
1557void
Greg Clayton944b8282011-08-22 22:30:57 +00001558DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001559{
Greg Clayton944b8282011-08-22 22:30:57 +00001560 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001561 switch (state)
1562 {
1563 case eStateConnected:
1564 case eStateAttaching:
1565 case eStateLaunching:
1566 case eStateInvalid:
1567 case eStateUnloaded:
1568 case eStateExited:
1569 case eStateDetached:
1570 Clear(false);
1571 break;
1572
1573 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001574 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001575 break;
1576
1577 case eStateRunning:
1578 case eStateStepping:
1579 case eStateCrashed:
1580 case eStateSuspended:
1581 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001582 }
1583}
1584
1585ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001586DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001587{
1588 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001589 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001590 if (log)
1591 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001592 return thread_plan_sp;
1593}
1594
1595Error
Greg Clayton944b8282011-08-22 22:30:57 +00001596DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001597{
1598 Error error;
1599 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1600 return error;
1601}
1602
1603void
Greg Clayton944b8282011-08-22 22:30:57 +00001604DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001605{
1606 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1607 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001608 CreateInstance,
1609 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001610}
1611
1612void
Greg Clayton944b8282011-08-22 22:30:57 +00001613DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001614{
1615 PluginManager::UnregisterPlugin (CreateInstance);
1616}
1617
Greg Claytone8cd0c92012-10-19 18:02:49 +00001618void
1619DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1620{
1621 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1622 {
1623 const bool is_global_setting = true;
1624 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1625 GetGlobalProperties()->GetValueProperties(),
1626 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1627 is_global_setting);
1628 }
1629}
Greg Clayton7b242382011-07-08 00:48:09 +00001630
Greg Clayton57abc5d2013-05-10 21:47:16 +00001631lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001632DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001633{
Greg Clayton57abc5d2013-05-10 21:47:16 +00001634 static ConstString g_name("darwin-kernel");
1635 return g_name;
Greg Clayton7b242382011-07-08 00:48:09 +00001636}
1637
1638const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001639DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001640{
1641 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1642}
1643
1644
1645//------------------------------------------------------------------
1646// PluginInterface protocol
1647//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001648lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001649DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001650{
Greg Clayton7b242382011-07-08 00:48:09 +00001651 return GetPluginNameStatic();
1652}
1653
1654uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001655DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001656{
1657 return 1;
1658}
1659
Greg Clayton1f746072012-08-29 21:13:06 +00001660lldb::ByteOrder
1661DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1662{
1663 switch (magic)
1664 {
Charles Davis510938e2013-08-27 05:04:57 +00001665 case llvm::MachO::MH_MAGIC:
1666 case llvm::MachO::MH_MAGIC_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001667 return lldb::endian::InlHostByteOrder();
1668
Charles Davis510938e2013-08-27 05:04:57 +00001669 case llvm::MachO::MH_CIGAM:
1670 case llvm::MachO::MH_CIGAM_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001671 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1672 return lldb::eByteOrderLittle;
1673 else
1674 return lldb::eByteOrderBig;
1675
1676 default:
1677 break;
1678 }
1679 return lldb::eByteOrderInvalid;
1680}
1681