blob: 5047b1f2cf0a1b2dca79bb529e22f27b2cfee90c [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 Clayton615eb7e2014-09-19 20:11:50 +0000470 Error error;
471 PlatformSP platform_sp(Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error));
Jason Molenda1c627542013-04-05 01:03:25 +0000472 // Only select the darwin-kernel Platform if we've been asked to load kexts.
473 // It can take some time to scan over all of the kext info.plists and that
474 // shouldn't be done if kext loading is explicitly disabled.
475 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
476 {
477 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000478 }
Greg Clayton7b242382011-07-08 00:48:09 +0000479}
480
481//----------------------------------------------------------------------
482// Destructor
483//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000484DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000485{
486 Clear(true);
487}
488
Greg Clayton374972e2011-07-09 17:15:55 +0000489void
Greg Clayton944b8282011-08-22 22:30:57 +0000490DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000491{
492 LoadKernelModuleIfNeeded();
493 SetNotificationBreakpointIfNeeded ();
494}
Greg Clayton7b242382011-07-08 00:48:09 +0000495//------------------------------------------------------------------
496/// Called after attaching a process.
497///
498/// Allow DynamicLoader plug-ins to execute some code after
499/// attaching to a process.
500//------------------------------------------------------------------
501void
Greg Clayton944b8282011-08-22 22:30:57 +0000502DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000503{
504 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000505 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000506}
507
508//------------------------------------------------------------------
509/// Called after attaching a process.
510///
511/// Allow DynamicLoader plug-ins to execute some code after
512/// attaching to a process.
513//------------------------------------------------------------------
514void
Greg Clayton944b8282011-08-22 22:30:57 +0000515DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000516{
517 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000518 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000519}
520
521
522//----------------------------------------------------------------------
523// Clear out the state of this class.
524//----------------------------------------------------------------------
525void
Greg Clayton944b8282011-08-22 22:30:57 +0000526DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000527{
528 Mutex::Locker locker(m_mutex);
529
530 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
531 m_process->ClearBreakpointSiteByID(m_break_id);
532
533 if (clear_process)
534 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000535 m_kernel.Clear();
536 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000537 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000538 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000539 m_break_id = LLDB_INVALID_BREAK_ID;
540}
541
Greg Clayton7b242382011-07-08 00:48:09 +0000542
Greg Claytonc859e2d2012-02-13 23:10:39 +0000543bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000544DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000545{
546 if (IsLoaded())
547 return true;
548
Jason Molenda306bd0a2013-02-19 05:42:46 +0000549 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000550 {
551 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +0000552 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
Jason Molenda306bd0a2013-02-19 05:42:46 +0000553 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000554 }
555 return false;
556}
557
Jason Molenda306bd0a2013-02-19 05:42:46 +0000558void
559DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
560{
561 m_module_sp = module_sp;
562 if (module_sp.get() && module_sp->GetObjectFile())
563 {
564 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
565 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
566 {
567 m_kernel_image = true;
568 }
569 else
570 {
571 m_kernel_image = false;
572 }
573 }
574}
575
576ModuleSP
577DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
578{
579 return m_module_sp;
580}
581
582void
583DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
584{
585 m_load_address = load_addr;
586}
587
588addr_t
589DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
590{
591 return m_load_address;
592}
593
594uint64_t
595DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
596{
597 return m_size;
598}
599
600void
601DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
602{
603 m_size = size;
604}
605
606uint32_t
607DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
608{
609 return m_load_process_stop_id;
610}
611
612void
613DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
614{
615 m_load_process_stop_id = stop_id;
616}
617
Greg Clayton2af282a2012-03-21 04:25:00 +0000618bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000619DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
620{
621 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
622 {
623 if (m_uuid == rhs.GetUUID())
624 {
625 return true;
626 }
627 return false;
628 }
629
630 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
631 return true;
632
633 return false;
634}
635
636void
637DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
638{
639 m_name = name;
640}
641
642std::string
643DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
644{
645 return m_name;
646}
647
648void
649DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
650{
651 m_uuid = uuid;
652}
653
654UUID
655DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
656{
657 return m_uuid;
658}
659
660// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
661// Module at that address. Require that the MemoryModule have a matching UUID and detect
662// if this MemoryModule is a kernel or a kext.
663//
664// Returns true if m_memory_module_sp is now set to a valid Module.
665
666bool
667DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
668{
669 if (m_memory_module_sp.get() != NULL)
670 return true;
671 if (m_load_address == LLDB_INVALID_ADDRESS)
672 return false;
673
674 FileSpec file_spec;
675 file_spec.SetFile (m_name.c_str(), false);
676
677 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
678
679 if (memory_module_sp.get() == NULL)
680 return false;
681
682 bool is_kernel = false;
683 if (memory_module_sp->GetObjectFile())
684 {
685 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
686 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
687 {
688 is_kernel = true;
689 }
690 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
691 {
692 is_kernel = false;
693 }
694 }
695
Jason Molenda38e70d12013-02-27 03:07:49 +0000696 // If this is a kext, and the kernel specified what UUID we should find at this
697 // load address, require that the memory module have a matching UUID or something
698 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000699 if (m_uuid.IsValid())
700 {
701 if (m_uuid != memory_module_sp->GetUUID())
702 {
703 return false;
704 }
705 }
706
707 // If the in-memory Module has a UUID, let's use that.
708 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
709 {
710 m_uuid = memory_module_sp->GetUUID();
711 }
712
713 m_memory_module_sp = memory_module_sp;
714 m_kernel_image = is_kernel;
715 if (is_kernel)
716 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000717 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000718 {
719 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
720 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000721 if (m_uuid.IsValid())
722 {
723 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
724 if (exe_module && exe_module->GetUUID().IsValid())
725 {
726 if (m_uuid != exe_module->GetUUID())
727 {
Greg Clayton44d93782014-01-27 23:43:24 +0000728 Stream *s = process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda38e70d12013-02-27 03:07:49 +0000729 if (s)
730 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000731 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 +0000732 exe_module->GetUUID().GetAsString().c_str(),
733 m_uuid.GetAsString().c_str());
Jason Molenda38e70d12013-02-27 03:07:49 +0000734 s->Flush ();
735 }
736 }
737 }
738 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000739 }
740
741 return true;
742}
743
744bool
745DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
746{
747 return m_kernel_image == true;
748}
749
750void
751DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
752{
753 m_kernel_image = is_kernel;
754}
755
756bool
757DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000758{
759 if (IsLoaded())
760 return true;
761
Greg Claytonc859e2d2012-02-13 23:10:39 +0000762
763 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000764
Jason Molenda306bd0a2013-02-19 05:42:46 +0000765 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
766 // have the correct segment load addresses.
767 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000768 {
769 return false;
770 }
771
Jason Molenda306bd0a2013-02-19 05:42:46 +0000772 bool uuid_is_valid = m_uuid.IsValid();
773
Jason Molendae575e7b2013-02-19 06:11:13 +0000774 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
775 {
Greg Clayton44d93782014-01-27 23:43:24 +0000776 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000777 if (s)
778 {
Jason Molendac16b4af2013-05-03 23:56:12 +0000779 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
Jason Molendae575e7b2013-02-19 06:11:13 +0000780 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
781 }
782 }
783
Jason Molenda306bd0a2013-02-19 05:42:46 +0000784 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000785 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000786 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
787 const ModuleList &target_images = target.GetImages();
788 m_module_sp = target_images.FindModule(m_uuid);
789
790 // Search for the kext on the local filesystem via the UUID
791 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000792 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000793 ModuleSpec module_spec;
794 module_spec.GetUUID() = m_uuid;
795 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000796
Jason Molendad76fb6e2013-02-19 07:16:22 +0000797 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
798 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000799 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000800 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000801 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000802 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000803 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000804 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000805 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
806 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
807 {
808 ModuleList loaded_module_list;
809 loaded_module_list.Append (m_module_sp);
810 target.ModulesDidLoad (loaded_module_list);
811 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000812 }
813 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000814 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000815
Jason Molenda1c627542013-04-05 01:03:25 +0000816 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
817 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
818 // to find it.
819 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0ddfe7f2014-03-05 03:33:01 +0000820 if (!m_module_sp && platform_sp)
Jason Molenda1c627542013-04-05 01:03:25 +0000821 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000822 ConstString platform_name (platform_sp->GetPluginName());
823 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
824 if (platform_name == g_platform_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000825 {
826 ModuleSpec kext_bundle_module_spec(module_spec);
827 FileSpec kext_filespec(m_name.c_str(), false);
828 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
829 platform_sp->GetSharedModule (kext_bundle_module_spec, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
830 }
831 }
832
Jason Molendad76fb6e2013-02-19 07:16:22 +0000833 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000834 // This will search in the list of currently-loaded files, look in the
835 // standard search paths on the system, and on a Mac it will try calling
836 // the DebugSymbols framework with the UUID to find the binary via its
837 // search methods.
838 if (!m_module_sp)
839 {
840 m_module_sp = target.GetSharedModule (module_spec);
841 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000842
Jason Molendad76fb6e2013-02-19 07:16:22 +0000843 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000844 {
Greg Clayton44d93782014-01-27 23:43:24 +0000845 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000846 if (s)
847 {
Jason Molendacc6dc782013-04-30 22:38:28 +0000848 s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000849 }
850 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000851 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000852
Jason Molenda306bd0a2013-02-19 05:42:46 +0000853 // If we managed to find a module, append it to the target's list of images.
854 // If we also have a memory module, require that they have matching UUIDs
855 if (m_module_sp)
856 {
857 bool uuid_match_ok = true;
858 if (m_memory_module_sp)
859 {
860 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000861 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000862 uuid_match_ok = false;
863 }
864 }
865 if (uuid_match_ok)
866 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000867 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000868 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
869 {
870 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000871 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000872 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000873 }
874 }
875
Jason Molenda56c23282013-02-19 06:39:56 +0000876 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
877 {
Greg Clayton44d93782014-01-27 23:43:24 +0000878 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda56c23282013-02-19 06:39:56 +0000879 if (s)
880 {
Jason Molenda56c23282013-02-19 06:39:56 +0000881 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
Jason Molendac16b4af2013-05-03 23:56:12 +0000882 m_name.c_str(), m_uuid.GetAsString().c_str());
Jason Molenda56c23282013-02-19 06:39:56 +0000883 }
884 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000885
Greg Clayton67408532012-12-11 01:20:51 +0000886 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
887
Jason Molenda306bd0a2013-02-19 05:42:46 +0000888 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000889 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000890 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000891 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000892 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
893 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000894
Jason Molendabb860bd2012-10-09 01:17:11 +0000895 if (memory_object_file && ondisk_object_file)
896 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000897 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
898 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000899
Jason Molendabb860bd2012-10-09 01:17:11 +0000900 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
901 SectionList *memory_section_list = memory_object_file->GetSectionList ();
902 if (memory_section_list && ondisk_section_list)
903 {
904 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
905 // There may be CTF sections in the memory image so we can't
906 // always just compare the number of sections (which are actually
907 // segments in mach-o parlance)
908 uint32_t sect_idx = 0;
909
910 // Use the memory_module's addresses for each section to set the
911 // file module's load address as appropriate. We don't want to use
912 // a single slide value for the entire kext - different segments may
913 // be slid different amounts by the kext loader.
914
915 uint32_t num_sections_loaded = 0;
916 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
917 {
918 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
919 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000920 {
Greg Clayton67408532012-12-11 01:20:51 +0000921 // Don't ever load __LINKEDIT as it may or may not be actually
922 // mapped into memory and there is no current way to tell.
923 // I filed rdar://problem/12851706 to track being able to tell
924 // if the __LINKEDIT is actually mapped, but until then, we need
925 // to not load the __LINKEDIT
926 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
927 continue;
928
Jason Molendabb860bd2012-10-09 01:17:11 +0000929 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
930 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000931 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000932 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
Jason Molendabb860bd2012-10-09 01:17:11 +0000933 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000934 }
935 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000936 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000937 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000938 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000939 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000940 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000941 }
942 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000943 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000944 }
945 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000946 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000947 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000948 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000949 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000950 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000951
Greg Claytonc859e2d2012-02-13 23:10:39 +0000952 bool is_loaded = IsLoaded();
953
Jason Molenda306bd0a2013-02-19 05:42:46 +0000954 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000955 {
Greg Clayton44d93782014-01-27 23:43:24 +0000956 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda68b36072012-10-02 03:49:41 +0000957 if (s)
958 {
Jason Molenda732238c2013-03-01 00:06:37 +0000959 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
960 if (kernel_object_file)
961 {
962 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
963 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
964 {
Matt Kopec787d1622013-03-12 17:45:38 +0000965 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000966 }
967 }
Jason Molenda743e43962012-10-02 22:23:42 +0000968 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000969 s->Printf ("Loaded kernel file %s\n",
970 m_module_sp->GetFileSpec().GetPath().c_str());
Jason Molenda743e43962012-10-02 22:23:42 +0000971 }
Jason Molenda68b36072012-10-02 03:49:41 +0000972 s->Flush ();
973 }
974 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000975 return is_loaded;
976}
977
Greg Clayton1f746072012-08-29 21:13:06 +0000978uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000979DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000980{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000981 if (m_memory_module_sp)
982 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
983 if (m_module_sp)
984 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000985 return 0;
986}
987
988lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000989DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000990{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000991 if (m_memory_module_sp)
992 return m_memory_module_sp->GetArchitecture().GetByteOrder();
993 if (m_module_sp)
994 return m_module_sp->GetArchitecture().GetByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +0000995 return lldb::endian::InlHostByteOrder();
996}
997
998lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000999DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +00001000{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001001 if (m_memory_module_sp)
1002 return m_memory_module_sp->GetArchitecture();
1003 if (m_module_sp)
1004 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001005 return lldb_private::ArchSpec ();
1006}
1007
1008
Greg Clayton7b242382011-07-08 00:48:09 +00001009//----------------------------------------------------------------------
1010// Load the kernel module and initialize the "m_kernel" member. Return
1011// true _only_ if the kernel is loaded the first time through (subsequent
1012// calls to this function should return false after the kernel has been
1013// already loaded).
1014//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001015void
Greg Clayton944b8282011-08-22 22:30:57 +00001016DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001017{
Greg Claytond16e1e52011-07-12 17:06:17 +00001018 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001019 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001020 m_kernel.Clear();
1021 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1022 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001023
1024 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001025 if (m_kernel.GetModule().get()
1026 && m_kernel.GetModule()->GetObjectFile()
1027 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001028 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001029 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001030 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001031 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001032
Jason Molenda306bd0a2013-02-19 05:42:46 +00001033 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001034 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001035 m_kernel.SetLoadAddress(m_kernel_load_address);
1036 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001037 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001038 // We didn't get a hint from the process, so we will
1039 // try the kernel at the address that it exists at in
1040 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001041 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001042 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001043 {
1044 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1045 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1046 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1047 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001048 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001049 if (load_address != file_address)
1050 {
1051 // Don't accidentally relocate the kernel to the File address --
1052 // the Load address has already been set to its actual in-memory address.
1053 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001054 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001055 }
1056 }
1057 else
1058 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001059 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001060 }
1061 }
Greg Clayton7b242382011-07-08 00:48:09 +00001062 }
1063 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001064
Jason Molenda306bd0a2013-02-19 05:42:46 +00001065 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001066 {
1067 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1068 {
1069 m_kernel.LoadImageAtFileAddress (m_process);
1070 }
1071 }
Greg Clayton7b242382011-07-08 00:48:09 +00001072
Jason Molenda306bd0a2013-02-19 05:42:46 +00001073 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001074 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001075 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001076 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001077 if (symbol)
1078 {
Greg Claytone7612132012-03-07 21:03:09 +00001079 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001080 // Update all image infos
1081 ReadAllKextSummaries ();
1082 }
Greg Clayton7b242382011-07-08 00:48:09 +00001083 }
1084 else
Greg Clayton7b242382011-07-08 00:48:09 +00001085 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001086 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001087 }
1088 }
Greg Clayton7b242382011-07-08 00:48:09 +00001089}
1090
Greg Clayton7b242382011-07-08 00:48:09 +00001091//----------------------------------------------------------------------
1092// Static callback function that gets called when our DYLD notification
1093// breakpoint gets hit. We update all of our image infos and then
1094// let our super class DynamicLoader class decide if we should stop
1095// or not (based on global preference).
1096//----------------------------------------------------------------------
1097bool
Greg Clayton944b8282011-08-22 22:30:57 +00001098DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001099 StoppointCallbackContext *context,
1100 user_id_t break_id,
1101 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001102{
Greg Clayton944b8282011-08-22 22:30:57 +00001103 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001104}
1105
1106bool
Greg Clayton944b8282011-08-22 22:30:57 +00001107DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001108 user_id_t break_id,
1109 user_id_t break_loc_id)
1110{
Greg Clayton5160ce52013-03-27 23:08:40 +00001111 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001112 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001113 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001114
Greg Clayton374972e2011-07-09 17:15:55 +00001115 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001116
1117 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001118 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001119
Greg Clayton374972e2011-07-09 17:15:55 +00001120 return GetStopWhenImagesChange();
1121}
1122
1123
1124bool
Greg Clayton944b8282011-08-22 22:30:57 +00001125DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001126{
1127 Mutex::Locker locker(m_mutex);
1128
1129 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001130
Greg Claytond16e1e52011-07-12 17:06:17 +00001131 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001132 {
1133 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1134 const ByteOrder byte_order = m_kernel.GetByteOrder();
1135 Error error;
1136 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001137 // which is currently 4 uint32_t and a pointer.
Greg Clayton7b242382011-07-08 00:48:09 +00001138 uint8_t buf[24];
1139 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1140 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001141 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001142 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1143 prefer_file_cache,
1144 error,
1145 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001146 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001147 // We got a valid address for our kext summary header and make sure it isn't NULL
1148 if (m_kext_summary_header_addr.IsValid() &&
1149 m_kext_summary_header_addr.GetFileAddress() != 0)
1150 {
1151 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1152 if (bytes_read == count)
1153 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001154 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001155 m_kext_summary_header.version = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001156 if (m_kext_summary_header.version > 128)
1157 {
1158 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1159 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 +00001160 // If we get an improbably large version number, we're probably getting bad memory.
Jason Molendac6fa5db2014-04-15 01:04:00 +00001161 m_kext_summary_header_addr.Clear();
1162 return false;
1163 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001164 if (m_kext_summary_header.version >= 2)
1165 {
1166 m_kext_summary_header.entry_size = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001167 if (m_kext_summary_header.entry_size > 4096)
1168 {
1169 // If we get an improbably large entry_size, we're probably getting bad memory.
1170 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1171 s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1172 m_kext_summary_header_addr.Clear();
1173 return false;
1174 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001175 }
1176 else
1177 {
1178 // Versions less than 2 didn't have an entry size, it was hard coded
1179 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1180 }
1181 m_kext_summary_header.entry_count = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001182 if (m_kext_summary_header.entry_count > 10000)
1183 {
1184 // If we get an improbably large number of kexts, we're probably getting bad memory.
1185 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1186 s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1187 m_kext_summary_header_addr.Clear();
1188 return false;
1189 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001190 return true;
1191 }
1192 }
Greg Clayton7b242382011-07-08 00:48:09 +00001193 }
1194 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001195 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001196 return false;
1197}
1198
Jason Molenda306bd0a2013-02-19 05:42:46 +00001199// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1200// and we need to figure out what kexts have been added or removed.
1201// Read the kext summaries from the inferior kernel memory, compare them against the
1202// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1203// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001204
1205bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001206DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001207{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001208 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001209 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001210 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001211 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001212
1213 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001214
1215 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1216 return false;
1217
Jason Molenda4da2e322013-02-26 00:26:47 +00001218 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1219 // kext loading, don't print any messages about kexts & don't try to read them.
1220 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1221
Jason Molenda306bd0a2013-02-19 05:42:46 +00001222 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1223 // we just found out about from ReadKextSummaries are marked as "add".
1224 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1225 std::vector<bool> to_be_added(count, true);
1226
1227 int number_of_new_kexts_being_added = 0;
1228 int number_of_old_kexts_being_removed = m_known_kexts.size();
1229
1230 const uint32_t new_kexts_size = kext_summaries.size();
1231 const uint32_t old_kexts_size = m_known_kexts.size();
1232
1233 // The m_known_kexts vector may have entries that have been Cleared,
1234 // or are a kernel.
1235 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1236 {
1237 bool ignore = false;
1238 KextImageInfo &image_info = m_known_kexts[old_kext];
1239 if (image_info.IsKernel())
1240 {
1241 ignore = true;
1242 }
1243 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1244 {
1245 ignore = true;
1246 }
1247
1248 if (ignore)
1249 {
1250 number_of_old_kexts_being_removed--;
1251 to_be_removed[old_kext] = false;
1252 }
1253 }
1254
1255 // Scan over the list of kexts we just read from the kernel, note those that
1256 // need to be added and those already loaded.
1257 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1258 {
1259 bool add_this_one = true;
1260 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1261 {
1262 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1263 {
1264 // We already have this kext, don't re-load it.
1265 to_be_added[new_kext] = false;
1266 // This kext is still present, do not remove it.
1267 to_be_removed[old_kext] = false;
1268
1269 number_of_old_kexts_being_removed--;
1270 add_this_one = false;
1271 break;
1272 }
1273 }
1274 if (add_this_one)
1275 {
1276 number_of_new_kexts_being_added++;
1277 }
1278 }
1279
1280 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1281 return true;
1282
Greg Clayton44d93782014-01-27 23:43:24 +00001283 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda4da2e322013-02-26 00:26:47 +00001284 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001285 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001286 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1287 {
1288 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1289 }
1290 else if (number_of_new_kexts_being_added > 0)
1291 {
1292 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1293 }
1294 else if (number_of_old_kexts_being_removed > 0)
1295 {
1296 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1297 }
Greg Clayton7b242382011-07-08 00:48:09 +00001298 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001299
1300 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001301 {
1302 if (load_kexts)
1303 {
1304 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1305 }
1306 else
1307 {
1308 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);
1309 }
1310 }
1311
Jason Molenda306bd0a2013-02-19 05:42:46 +00001312
1313 if (number_of_new_kexts_being_added > 0)
1314 {
1315 ModuleList loaded_module_list;
1316
1317 const uint32_t num_of_new_kexts = kext_summaries.size();
1318 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1319 {
1320 if (to_be_added[new_kext] == true)
1321 {
1322 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001323 if (load_kexts)
1324 {
1325 if (!image_info.LoadImageUsingMemoryModule (m_process))
1326 {
1327 image_info.LoadImageAtFileAddress (m_process);
1328 }
1329 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001330
1331 m_known_kexts.push_back(image_info);
1332
1333 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1334 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1335
Jason Molenda4da2e322013-02-26 00:26:47 +00001336 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001337 s->Printf (".");
1338
1339 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001340 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001341 }
1342 }
1343 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1344 }
1345
1346 if (number_of_old_kexts_being_removed > 0)
1347 {
1348 ModuleList loaded_module_list;
1349 const uint32_t num_of_old_kexts = m_known_kexts.size();
1350 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1351 {
1352 ModuleList unloaded_module_list;
1353 if (to_be_removed[old_kext])
1354 {
1355 KextImageInfo &image_info = m_known_kexts[old_kext];
1356 // You can't unload the kernel.
1357 if (!image_info.IsKernel())
1358 {
1359 if (image_info.GetModule())
1360 {
1361 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1362 }
1363 if (s)
1364 s->Printf (".");
1365 image_info.Clear();
1366 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1367 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1368 }
1369 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001370 m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001371 }
1372 }
1373
Jason Molenda4da2e322013-02-26 00:26:47 +00001374 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001375 {
1376 s->Printf (" done.\n");
1377 s->Flush ();
1378 }
1379
Jason Molenda306bd0a2013-02-19 05:42:46 +00001380 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001381}
1382
Greg Clayton7b242382011-07-08 00:48:09 +00001383uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001384DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001385 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001386 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001387{
1388 const ByteOrder endian = m_kernel.GetByteOrder();
1389 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1390
1391 image_infos.resize(image_infos_count);
1392 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1393 DataBufferHeap data(count, 0);
1394 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001395
Greg Clayton0d9fc762011-07-08 03:21:57 +00001396 const bool prefer_file_cache = false;
1397 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1398 prefer_file_cache,
1399 data.GetBytes(),
1400 data.GetByteSize(),
1401 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001402 if (bytes_read == count)
1403 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001404
Greg Clayton7b242382011-07-08 00:48:09 +00001405 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1406 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001407 for (uint32_t kext_summary_offset = 0;
1408 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1409 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001410 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001411 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001412 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1413 if (name_data == NULL)
1414 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001415 image_infos[i].SetName ((const char *) name_data);
1416 UUID uuid (extractor.GetData (&offset, 16), 16);
1417 image_infos[i].SetUUID (uuid);
1418 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1419 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001420 }
1421 if (i < image_infos.size())
1422 image_infos.resize(i);
1423 }
1424 else
1425 {
1426 image_infos.clear();
1427 }
1428 return image_infos.size();
1429}
1430
1431bool
Greg Clayton944b8282011-08-22 22:30:57 +00001432DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001433{
Greg Clayton7b242382011-07-08 00:48:09 +00001434 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001435
Greg Clayton7b242382011-07-08 00:48:09 +00001436 if (ReadKextSummaryHeader ())
1437 {
Greg Clayton374972e2011-07-09 17:15:55 +00001438 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001439 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001440 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001441 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001442 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001443 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001444 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001445 }
1446 return true;
1447 }
1448 }
1449 return false;
1450}
1451
1452//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001453// Dump an image info structure to the file handle provided.
1454//----------------------------------------------------------------------
1455void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001456DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001457{
1458 if (log == NULL)
1459 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001460 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001461
Jason Molenda306bd0a2013-02-19 05:42:46 +00001462 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001463 {
1464 if (u)
1465 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001466 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 +00001467 u[ 0], u[ 1], u[ 2], u[ 3],
1468 u[ 4], u[ 5], u[ 6], u[ 7],
1469 u[ 8], u[ 9], u[10], u[11],
1470 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001471 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001472 }
1473 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001474 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001475 }
1476 else
1477 {
1478 if (u)
1479 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001480 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\"",
1481 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001482 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1483 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001484 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001485 }
1486 else
1487 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001488 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1489 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001490 }
Greg Clayton7b242382011-07-08 00:48:09 +00001491 }
1492}
1493
1494//----------------------------------------------------------------------
1495// Dump the _dyld_all_image_infos members and all current image infos
1496// that we have parsed to the file handle provided.
1497//----------------------------------------------------------------------
1498void
Greg Clayton944b8282011-08-22 22:30:57 +00001499DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001500{
1501 if (log == NULL)
1502 return;
1503
1504 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001505 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001506 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001507 m_kext_summary_header.version,
1508 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001509 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001510
1511 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001512 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001513 if (count > 0)
1514 {
1515 log->PutCString("Loaded:");
1516 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001517 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001518 }
1519}
1520
1521void
Greg Clayton944b8282011-08-22 22:30:57 +00001522DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001523{
Greg Clayton944b8282011-08-22 22:30:57 +00001524 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001525 Clear(true);
1526 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001527}
1528
Greg Clayton374972e2011-07-09 17:15:55 +00001529void
Greg Clayton944b8282011-08-22 22:30:57 +00001530DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001531{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001532 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001533 {
Greg Clayton944b8282011-08-22 22:30:57 +00001534 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001535
Greg Claytond16e1e52011-07-12 17:06:17 +00001536
Jim Inghama8558b62012-05-22 00:12:20 +00001537 const bool internal_bp = true;
Greg Claytoneb023e72013-10-11 19:48:25 +00001538 const bool hardware = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001539 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001540 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001541 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001542 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001543 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001544 "OSKextLoadedKextSummariesUpdated",
1545 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +00001546 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001547 internal_bp,
1548 hardware).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001549
Greg Clayton944b8282011-08-22 22:30:57 +00001550 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001551 m_break_id = bp->GetID();
1552 }
Greg Clayton7b242382011-07-08 00:48:09 +00001553}
1554
1555//----------------------------------------------------------------------
1556// Member function that gets called when the process state changes.
1557//----------------------------------------------------------------------
1558void
Greg Clayton944b8282011-08-22 22:30:57 +00001559DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001560{
Greg Clayton944b8282011-08-22 22:30:57 +00001561 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001562 switch (state)
1563 {
1564 case eStateConnected:
1565 case eStateAttaching:
1566 case eStateLaunching:
1567 case eStateInvalid:
1568 case eStateUnloaded:
1569 case eStateExited:
1570 case eStateDetached:
1571 Clear(false);
1572 break;
1573
1574 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001575 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001576 break;
1577
1578 case eStateRunning:
1579 case eStateStepping:
1580 case eStateCrashed:
1581 case eStateSuspended:
1582 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001583 }
1584}
1585
1586ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001587DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001588{
1589 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001590 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001591 if (log)
1592 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001593 return thread_plan_sp;
1594}
1595
1596Error
Greg Clayton944b8282011-08-22 22:30:57 +00001597DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001598{
1599 Error error;
1600 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1601 return error;
1602}
1603
1604void
Greg Clayton944b8282011-08-22 22:30:57 +00001605DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001606{
1607 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1608 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001609 CreateInstance,
1610 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001611}
1612
1613void
Greg Clayton944b8282011-08-22 22:30:57 +00001614DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001615{
1616 PluginManager::UnregisterPlugin (CreateInstance);
1617}
1618
Greg Claytone8cd0c92012-10-19 18:02:49 +00001619void
1620DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1621{
1622 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1623 {
1624 const bool is_global_setting = true;
1625 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1626 GetGlobalProperties()->GetValueProperties(),
1627 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1628 is_global_setting);
1629 }
1630}
Greg Clayton7b242382011-07-08 00:48:09 +00001631
Greg Clayton57abc5d2013-05-10 21:47:16 +00001632lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001633DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001634{
Greg Clayton57abc5d2013-05-10 21:47:16 +00001635 static ConstString g_name("darwin-kernel");
1636 return g_name;
Greg Clayton7b242382011-07-08 00:48:09 +00001637}
1638
1639const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001640DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001641{
1642 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1643}
1644
1645
1646//------------------------------------------------------------------
1647// PluginInterface protocol
1648//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001649lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001650DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001651{
Greg Clayton7b242382011-07-08 00:48:09 +00001652 return GetPluginNameStatic();
1653}
1654
1655uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001656DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001657{
1658 return 1;
1659}
1660
Greg Clayton1f746072012-08-29 21:13:06 +00001661lldb::ByteOrder
1662DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1663{
1664 switch (magic)
1665 {
Charles Davis510938e2013-08-27 05:04:57 +00001666 case llvm::MachO::MH_MAGIC:
1667 case llvm::MachO::MH_MAGIC_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001668 return lldb::endian::InlHostByteOrder();
1669
Charles Davis510938e2013-08-27 05:04:57 +00001670 case llvm::MachO::MH_CIGAM:
1671 case llvm::MachO::MH_CIGAM_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001672 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1673 return lldb::eByteOrderLittle;
1674 else
1675 return lldb::eByteOrderBig;
1676
1677 default:
1678 break;
1679 }
1680 return lldb::eByteOrderInvalid;
1681}
1682