blob: 9100bf0caee68936dd4e7e4949d3eef9f561c140 [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"
Ilia K41204d02015-03-04 12:05:24 +000026#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton7b242382011-07-08 00:48:09 +000027#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000028#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000029#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000030#include "lldb/Target/Target.h"
31#include "lldb/Target/Thread.h"
32#include "lldb/Target/ThreadPlanRunToAddress.h"
Greg Clayton57abc5d2013-05-10 21:47:16 +000033#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000034
Greg Clayton944b8282011-08-22 22:30:57 +000035#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000036
37//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
38#ifdef ENABLE_DEBUG_PRINTF
39#include <stdio.h>
40#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
41#else
42#define DEBUG_PRINTF(fmt, ...)
43#endif
44
45using namespace lldb;
46using namespace lldb_private;
47
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000048// Progressively greater amounts of scanning we will allow
49// For some targets very early in startup, we can't do any random reads of memory or we can crash the device
50// so a setting is needed that can completely disable the KASLR scans.
51
52enum KASLRScanType
53{
54 eKASLRScanNone = 0, // No reading into the inferior at all
55 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 +000056 eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000057 eKASLRScanExhaustiveScan // Scan through the entire possible kernel address range looking for a kernel
58};
59
60OptionEnumValueElement
61g_kaslr_kernel_scan_enum_values[] =
62{
63 { eKASLRScanNone, "none", "Do not read memory looking for a Darwin kernel when attaching." },
64 { eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
65 { eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find the Darwin kernel's load address."},
66 { eKASLRScanExhaustiveScan, "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
67 { 0, NULL, NULL }
68};
69
Greg Claytone8cd0c92012-10-19 18:02:49 +000070static PropertyDefinition
71g_properties[] =
72{
Greg Clayton66763ee2012-10-19 20:53:18 +000073 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000074 { "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 +000075 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000076};
77
78enum {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000079 ePropertyLoadKexts,
80 ePropertyScanType
Greg Claytone8cd0c92012-10-19 18:02:49 +000081};
82
83class DynamicLoaderDarwinKernelProperties : public Properties
84{
85public:
86
87 static ConstString &
88 GetSettingName ()
89 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000090 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000091 return g_setting_name;
92 }
93
94 DynamicLoaderDarwinKernelProperties() :
95 Properties ()
96 {
97 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
98 m_collection_sp->Initialize(g_properties);
99 }
100
101 virtual
102 ~DynamicLoaderDarwinKernelProperties()
103 {
104 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000105
Greg Claytone8cd0c92012-10-19 18:02:49 +0000106 bool
Greg Clayton66763ee2012-10-19 20:53:18 +0000107 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +0000108 {
Greg Clayton66763ee2012-10-19 20:53:18 +0000109 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000110 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
111 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000112
113 KASLRScanType
114 GetScanType() const
115 {
116 const uint32_t idx = ePropertyScanType;
Jason Molenda63969222013-01-30 04:48:16 +0000117 return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000118 }
119
120
Greg Claytone8cd0c92012-10-19 18:02:49 +0000121};
122
Greg Clayton7b0992d2013-04-18 22:45:39 +0000123typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000124
125static const DynamicLoaderDarwinKernelPropertiesSP &
126GetGlobalProperties()
127{
128 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
129 if (!g_settings_sp)
130 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
131 return g_settings_sp;
132}
133
Greg Clayton7b242382011-07-08 00:48:09 +0000134//----------------------------------------------------------------------
135// Create an instance of this class. This function is filled into
136// the plugin info class that gets handed out by the plugin factory and
137// allows the lldb to instantiate an instance of this class.
138//----------------------------------------------------------------------
139DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000140DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000141{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000142 if (!force)
Greg Clayton7b242382011-07-08 00:48:09 +0000143 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000144 // If the user provided an executable binary and it is not a kernel,
145 // this plugin should not create an instance.
Greg Claytonaa149cb2011-08-11 02:48:45 +0000146 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000147 if (exe_module)
148 {
149 ObjectFile *object_file = exe_module->GetObjectFile();
150 if (object_file)
151 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000152 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
153 {
154 return NULL;
155 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000156 }
157 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000158
159 // If the target's architecture does not look like an Apple environment,
160 // this plugin should not create an instance.
161 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
162 switch (triple_ref.getOS())
Greg Claytondf0b7d52011-07-08 04:11:42 +0000163 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000164 case llvm::Triple::Darwin:
165 case llvm::Triple::MacOSX:
166 case llvm::Triple::IOS:
167 if (triple_ref.getVendor() != llvm::Triple::Apple)
168 {
169 return NULL;
170 }
171 break;
172 // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
173 case llvm::Triple::UnknownOS:
174 break;
175 default:
176 return NULL;
177 break;
178 }
179 }
180
181 // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
182 // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
183
Jason Molenda503d0182013-03-02 07:19:32 +0000184 addr_t kernel_load_address = SearchForDarwinKernel (process);
185 if (kernel_load_address != LLDB_INVALID_ADDRESS)
186 {
187 process->SetCanJIT(false);
188 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
189 }
190 return NULL;
191}
192
193lldb::addr_t
194DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
195{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000196 addr_t kernel_load_address = process->GetImageInfoAddress();
197 if (kernel_load_address == LLDB_INVALID_ADDRESS)
198 {
199 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
200 if (kernel_load_address == LLDB_INVALID_ADDRESS)
201 {
202 kernel_load_address = SearchForKernelWithDebugHints (process);
203 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000204 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000205 kernel_load_address = SearchForKernelNearPC (process);
206 if (kernel_load_address == LLDB_INVALID_ADDRESS)
207 {
208 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
209 }
Greg Clayton70512312012-05-08 01:45:38 +0000210 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000211 }
Greg Clayton7b242382011-07-08 00:48:09 +0000212 }
Jason Molenda503d0182013-03-02 07:19:32 +0000213 return kernel_load_address;
Greg Clayton7b242382011-07-08 00:48:09 +0000214}
215
216//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000217// Check if the kernel binary is loaded in memory without a slide.
218// First verify that the ExecutableModule is a kernel before we proceed.
219// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
220//----------------------------------------------------------------------
221lldb::addr_t
222DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
223{
224 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
225 if (exe_module == NULL)
226 return LLDB_INVALID_ADDRESS;
227
228 ObjectFile *exe_objfile = exe_module->GetObjectFile();
229 if (exe_objfile == NULL)
230 return LLDB_INVALID_ADDRESS;
231
232 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
233 return LLDB_INVALID_ADDRESS;
234
235 if (!exe_objfile->GetHeaderAddress().IsValid())
236 return LLDB_INVALID_ADDRESS;
237
238 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
239 return exe_objfile->GetHeaderAddress().GetFileAddress();
240
241 return LLDB_INVALID_ADDRESS;
242}
243
244//----------------------------------------------------------------------
245// If the debug flag is included in the boot-args nvram setting, the kernel's load address
246// will be noted in the lowglo page at a fixed address
247// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
248//----------------------------------------------------------------------
249lldb::addr_t
250DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
251{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000252 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
253 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000254
255 Error read_err;
256 addr_t addr = LLDB_INVALID_ADDRESS;
257 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
258 {
259 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
Jason Molenda7dd29392014-10-06 22:23:30 +0000260 if (CheckForKernelImageAtAddress (addr, process).IsValid())
261 {
262 return addr;
263 }
264 addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000004010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
265 if (CheckForKernelImageAtAddress (addr, process).IsValid())
266 {
267 return addr;
268 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000269 }
270 else
271 {
272 addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000273 if (CheckForKernelImageAtAddress (addr, process).IsValid())
Jason Molenda7dd29392014-10-06 22:23:30 +0000274 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000275 return addr;
Jason Molenda7dd29392014-10-06 22:23:30 +0000276 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000277 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000278 return LLDB_INVALID_ADDRESS;
279}
280
281//----------------------------------------------------------------------
282// If the kernel is currently executing when lldb attaches, and we don't have
283// a better way of finding the kernel's load address, try searching backwards
284// from the current pc value looking for the kernel's Mach header in memory.
285// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
286//----------------------------------------------------------------------
287lldb::addr_t
288DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
289{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000290 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
291 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
292 {
293 return LLDB_INVALID_ADDRESS;
294 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000295
296 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
297 if (thread.get() == NULL)
298 return LLDB_INVALID_ADDRESS;
299 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
300
301 if (pc == LLDB_INVALID_ADDRESS)
302 return LLDB_INVALID_ADDRESS;
303
Jason Molenda44edbf12013-04-19 00:50:28 +0000304 addr_t kernel_range_low;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000305 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
306 {
307 kernel_range_low = 1ULL << 63;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000308 }
309 else
310 {
311 kernel_range_low = 1ULL << 31;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000312 }
313
314 // Outside the normal kernel address range, this is probably userland code running right now
315 if (pc < kernel_range_low)
Jason Molenda44edbf12013-04-19 00:50:28 +0000316 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000317
318 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
319 // an offset of one page (0x1000) or two, depending on the device.
320
321 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
322 addr_t addr = pc & ~0xfffff;
323
324 int i = 0;
325 while (i < 32 && pc >= kernel_range_low)
326 {
327 if (CheckForKernelImageAtAddress (addr, process).IsValid())
328 return addr;
329 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
330 return addr + 0x1000;
331 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
332 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000333 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
334 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000335 i++;
336 addr -= 0x100000;
337 }
338
339 return LLDB_INVALID_ADDRESS;
340}
341
342//----------------------------------------------------------------------
343// Scan through the valid address range for a kernel binary.
344// This is uselessly slow in 64-bit environments so we don't even try it.
345// This scan is not enabled by default even for 32-bit targets.
346// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
347//----------------------------------------------------------------------
348lldb::addr_t
349DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
350{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000351 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
352 {
353 return LLDB_INVALID_ADDRESS;
354 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000355
356 addr_t kernel_range_low, kernel_range_high;
357 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
358 {
359 kernel_range_low = 1ULL << 63;
360 kernel_range_high = UINT64_MAX;
361 }
362 else
363 {
364 kernel_range_low = 1ULL << 31;
365 kernel_range_high = UINT32_MAX;
366 }
367
368 // Stepping through memory at one-megabyte resolution looking for a kernel
369 // rarely works (fast enough) with a 64-bit address space -- for now, let's
370 // not even bother. We may be attaching to something which *isn't* a kernel
371 // and we don't want to spin for minutes on-end looking for a kernel.
372 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
373 return LLDB_INVALID_ADDRESS;
374
375 addr_t addr = kernel_range_low;
376
377 while (addr >= kernel_range_low && addr < kernel_range_high)
378 {
379 if (CheckForKernelImageAtAddress (addr, process).IsValid())
380 return addr;
381 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
382 return addr + 0x1000;
383 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
384 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000385 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
386 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000387 addr += 0x100000;
388 }
389 return LLDB_INVALID_ADDRESS;
390}
391
392//----------------------------------------------------------------------
393// Given an address in memory, look to see if there is a kernel image at that
394// address.
395// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
396//----------------------------------------------------------------------
397lldb_private::UUID
398DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
399{
400 if (addr == LLDB_INVALID_ADDRESS)
401 return UUID();
402
403 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
404 // (the first field of the mach_header/mach_header_64 struct).
405
406 Error read_error;
407 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
Charles Davis510938e2013-08-27 05:04:57 +0000408 if (result != llvm::MachO::MH_MAGIC_64
409 && result != llvm::MachO::MH_MAGIC
410 && result != llvm::MachO::MH_CIGAM
411 && result != llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000412 {
413 return UUID();
414 }
415
416 // Read the mach header and see whether it looks like a kernel
417 llvm::MachO::mach_header header;
418 if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
419 return UUID();
420
Charles Davis510938e2013-08-27 05:04:57 +0000421 if (header.magic == llvm::MachO::MH_CIGAM ||
422 header.magic == llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000423 {
424 header.magic = llvm::ByteSwap_32(header.magic);
425 header.cputype = llvm::ByteSwap_32(header.cputype);
426 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
427 header.filetype = llvm::ByteSwap_32(header.filetype);
428 header.ncmds = llvm::ByteSwap_32(header.ncmds);
429 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
430 header.flags = llvm::ByteSwap_32(header.flags);
431 }
432
433 // A kernel is an executable which does not have the dynamic link object flag set.
Charles Davis510938e2013-08-27 05:04:57 +0000434 if (header.filetype == llvm::MachO::MH_EXECUTE
435 && (header.flags & llvm::MachO::MH_DYLDLINK) == 0)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000436 {
437 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000438 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000439 if (!memory_module_sp.get())
440 return UUID();
441
442 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
443 if (exe_objfile == NULL)
444 return UUID();
445
446 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
447 {
Jason Molendaa4ce2532013-05-02 22:02:57 +0000448 ArchSpec kernel_arch (eArchTypeMachO, header.cputype, header.cpusubtype);
449 if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
450 {
451 process->GetTarget().SetArchitecture (kernel_arch);
452 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000453 return memory_module_sp->GetUUID();
454 }
455 }
456
457 return UUID();
458}
459
460//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000461// Constructor
462//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000463DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000464 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000465 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000466 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000467 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000468 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000469 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000470 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000471 m_mutex(Mutex::eMutexTypeRecursive),
472 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000473{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000474 Error error;
475 PlatformSP platform_sp(Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error));
Jason Molenda1c627542013-04-05 01:03:25 +0000476 // Only select the darwin-kernel Platform if we've been asked to load kexts.
477 // It can take some time to scan over all of the kext info.plists and that
478 // shouldn't be done if kext loading is explicitly disabled.
479 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
480 {
481 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000482 }
Greg Clayton7b242382011-07-08 00:48:09 +0000483}
484
485//----------------------------------------------------------------------
486// Destructor
487//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000488DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000489{
490 Clear(true);
491}
492
Greg Clayton374972e2011-07-09 17:15:55 +0000493void
Greg Clayton944b8282011-08-22 22:30:57 +0000494DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000495{
496 LoadKernelModuleIfNeeded();
497 SetNotificationBreakpointIfNeeded ();
498}
Greg Clayton7b242382011-07-08 00:48:09 +0000499//------------------------------------------------------------------
500/// Called after attaching a process.
501///
502/// Allow DynamicLoader plug-ins to execute some code after
503/// attaching to a process.
504//------------------------------------------------------------------
505void
Greg Clayton944b8282011-08-22 22:30:57 +0000506DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000507{
508 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000509 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000510}
511
512//------------------------------------------------------------------
513/// Called after attaching a process.
514///
515/// Allow DynamicLoader plug-ins to execute some code after
516/// attaching to a process.
517//------------------------------------------------------------------
518void
Greg Clayton944b8282011-08-22 22:30:57 +0000519DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000520{
521 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000522 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000523}
524
525
526//----------------------------------------------------------------------
527// Clear out the state of this class.
528//----------------------------------------------------------------------
529void
Greg Clayton944b8282011-08-22 22:30:57 +0000530DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000531{
532 Mutex::Locker locker(m_mutex);
533
534 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
535 m_process->ClearBreakpointSiteByID(m_break_id);
536
537 if (clear_process)
538 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000539 m_kernel.Clear();
540 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000541 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000542 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000543 m_break_id = LLDB_INVALID_BREAK_ID;
544}
545
Greg Clayton7b242382011-07-08 00:48:09 +0000546
Greg Claytonc859e2d2012-02-13 23:10:39 +0000547bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000548DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000549{
550 if (IsLoaded())
551 return true;
552
Jason Molenda306bd0a2013-02-19 05:42:46 +0000553 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000554 {
555 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +0000556 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
Jason Molenda306bd0a2013-02-19 05:42:46 +0000557 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000558 }
559 return false;
560}
561
Jason Molenda306bd0a2013-02-19 05:42:46 +0000562void
563DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
564{
565 m_module_sp = module_sp;
566 if (module_sp.get() && module_sp->GetObjectFile())
567 {
568 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
569 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
570 {
571 m_kernel_image = true;
572 }
573 else
574 {
575 m_kernel_image = false;
576 }
577 }
578}
579
580ModuleSP
581DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
582{
583 return m_module_sp;
584}
585
586void
587DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
588{
589 m_load_address = load_addr;
590}
591
592addr_t
593DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
594{
595 return m_load_address;
596}
597
598uint64_t
599DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
600{
601 return m_size;
602}
603
604void
605DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
606{
607 m_size = size;
608}
609
610uint32_t
611DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
612{
613 return m_load_process_stop_id;
614}
615
616void
617DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
618{
619 m_load_process_stop_id = stop_id;
620}
621
Greg Clayton2af282a2012-03-21 04:25:00 +0000622bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000623DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
624{
625 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
626 {
627 if (m_uuid == rhs.GetUUID())
628 {
629 return true;
630 }
631 return false;
632 }
633
634 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
635 return true;
636
637 return false;
638}
639
640void
641DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
642{
643 m_name = name;
644}
645
646std::string
647DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
648{
649 return m_name;
650}
651
652void
653DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
654{
655 m_uuid = uuid;
656}
657
658UUID
659DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
660{
661 return m_uuid;
662}
663
664// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
665// Module at that address. Require that the MemoryModule have a matching UUID and detect
666// if this MemoryModule is a kernel or a kext.
667//
668// Returns true if m_memory_module_sp is now set to a valid Module.
669
670bool
671DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
672{
673 if (m_memory_module_sp.get() != NULL)
674 return true;
675 if (m_load_address == LLDB_INVALID_ADDRESS)
676 return false;
677
678 FileSpec file_spec;
679 file_spec.SetFile (m_name.c_str(), false);
680
681 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
682
683 if (memory_module_sp.get() == NULL)
684 return false;
685
686 bool is_kernel = false;
687 if (memory_module_sp->GetObjectFile())
688 {
689 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
690 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
691 {
692 is_kernel = true;
693 }
694 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
695 {
696 is_kernel = false;
697 }
698 }
699
Jason Molenda38e70d12013-02-27 03:07:49 +0000700 // If this is a kext, and the kernel specified what UUID we should find at this
701 // load address, require that the memory module have a matching UUID or something
702 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000703 if (m_uuid.IsValid())
704 {
705 if (m_uuid != memory_module_sp->GetUUID())
706 {
707 return false;
708 }
709 }
710
711 // If the in-memory Module has a UUID, let's use that.
712 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
713 {
714 m_uuid = memory_module_sp->GetUUID();
715 }
716
717 m_memory_module_sp = memory_module_sp;
718 m_kernel_image = is_kernel;
719 if (is_kernel)
720 {
Jason Molenda38e70d12013-02-27 03:07:49 +0000721 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000722 {
723 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
724 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000725 if (m_uuid.IsValid())
726 {
Jason Molenda48975832015-03-10 23:34:52 +0000727 ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule();
728 if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid())
Jason Molenda38e70d12013-02-27 03:07:49 +0000729 {
Jason Molenda48975832015-03-10 23:34:52 +0000730 if (m_uuid != exe_module_sp->GetUUID())
Jason Molenda38e70d12013-02-27 03:07:49 +0000731 {
Jason Molenda48975832015-03-10 23:34:52 +0000732 // The user specified a kernel binary that has a different UUID than
733 // the kernel actually running in memory. This never ends well;
734 // clear the user specified kernel binary from the Target.
735
736 m_module_sp.reset();
737
738 ModuleList user_specified_kernel_list;
739 user_specified_kernel_list.Append (exe_module_sp);
740 process->GetTarget().GetImages().Remove (user_specified_kernel_list);
Jason Molenda38e70d12013-02-27 03:07:49 +0000741 }
742 }
743 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000744 }
745
746 return true;
747}
748
749bool
750DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
751{
752 return m_kernel_image == true;
753}
754
755void
756DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
757{
758 m_kernel_image = is_kernel;
759}
760
761bool
762DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000763{
764 if (IsLoaded())
765 return true;
766
Greg Claytonc859e2d2012-02-13 23:10:39 +0000767
768 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000769
Jason Molenda306bd0a2013-02-19 05:42:46 +0000770 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
771 // have the correct segment load addresses.
772 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000773 {
774 return false;
775 }
776
Jason Molenda306bd0a2013-02-19 05:42:46 +0000777 bool uuid_is_valid = m_uuid.IsValid();
778
Jason Molendae575e7b2013-02-19 06:11:13 +0000779 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
780 {
Greg Clayton44d93782014-01-27 23:43:24 +0000781 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000782 if (s)
783 {
Jason Molendac16b4af2013-05-03 23:56:12 +0000784 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
Jason Molendae575e7b2013-02-19 06:11:13 +0000785 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
786 }
787 }
788
Jason Molenda306bd0a2013-02-19 05:42:46 +0000789 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000790 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000791 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
792 const ModuleList &target_images = target.GetImages();
793 m_module_sp = target_images.FindModule(m_uuid);
794
795 // Search for the kext on the local filesystem via the UUID
796 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000797 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000798 ModuleSpec module_spec;
799 module_spec.GetUUID() = m_uuid;
800 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000801
Jason Molendad76fb6e2013-02-19 07:16:22 +0000802 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
803 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000804 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000805 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000806 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000807 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000808 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000809 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000810 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
811 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
812 {
813 ModuleList loaded_module_list;
814 loaded_module_list.Append (m_module_sp);
815 target.ModulesDidLoad (loaded_module_list);
816 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000817 }
818 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000819 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000820
Jason Molenda1c627542013-04-05 01:03:25 +0000821 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
822 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
823 // to find it.
824 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0ddfe7f2014-03-05 03:33:01 +0000825 if (!m_module_sp && platform_sp)
Jason Molenda1c627542013-04-05 01:03:25 +0000826 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000827 ConstString platform_name (platform_sp->GetPluginName());
828 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
829 if (platform_name == g_platform_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000830 {
831 ModuleSpec kext_bundle_module_spec(module_spec);
832 FileSpec kext_filespec(m_name.c_str(), false);
833 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
Ilia K667ef222015-03-24 14:30:28 +0000834 platform_sp->GetSharedModule (kext_bundle_module_spec, process, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
Jason Molenda1c627542013-04-05 01:03:25 +0000835 }
836 }
837
Jason Molendad76fb6e2013-02-19 07:16:22 +0000838 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000839 // This will search in the list of currently-loaded files, look in the
840 // standard search paths on the system, and on a Mac it will try calling
841 // the DebugSymbols framework with the UUID to find the binary via its
842 // search methods.
843 if (!m_module_sp)
844 {
845 m_module_sp = target.GetSharedModule (module_spec);
846 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000847
Jason Molendad76fb6e2013-02-19 07:16:22 +0000848 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000849 {
Greg Clayton44d93782014-01-27 23:43:24 +0000850 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000851 if (s)
852 {
Jason Molendacc6dc782013-04-30 22:38:28 +0000853 s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000854 }
855 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000856 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000857
Jason Molenda306bd0a2013-02-19 05:42:46 +0000858 // If we managed to find a module, append it to the target's list of images.
859 // If we also have a memory module, require that they have matching UUIDs
860 if (m_module_sp)
861 {
862 bool uuid_match_ok = true;
863 if (m_memory_module_sp)
864 {
865 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000866 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000867 uuid_match_ok = false;
868 }
869 }
870 if (uuid_match_ok)
871 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000872 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000873 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
874 {
875 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000876 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000877 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000878 }
879 }
880
Jason Molenda56c23282013-02-19 06:39:56 +0000881 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
882 {
Greg Clayton44d93782014-01-27 23:43:24 +0000883 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda56c23282013-02-19 06:39:56 +0000884 if (s)
885 {
Jason Molenda56c23282013-02-19 06:39:56 +0000886 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
Jason Molendac16b4af2013-05-03 23:56:12 +0000887 m_name.c_str(), m_uuid.GetAsString().c_str());
Jason Molenda56c23282013-02-19 06:39:56 +0000888 }
889 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000890
Greg Clayton67408532012-12-11 01:20:51 +0000891 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
892
Jason Molenda306bd0a2013-02-19 05:42:46 +0000893 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000894 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000895 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000896 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000897 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
898 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000899
Jason Molendabb860bd2012-10-09 01:17:11 +0000900 if (memory_object_file && ondisk_object_file)
901 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000902 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
903 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000904
Jason Molendabb860bd2012-10-09 01:17:11 +0000905 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
906 SectionList *memory_section_list = memory_object_file->GetSectionList ();
907 if (memory_section_list && ondisk_section_list)
908 {
909 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
910 // There may be CTF sections in the memory image so we can't
911 // always just compare the number of sections (which are actually
912 // segments in mach-o parlance)
913 uint32_t sect_idx = 0;
914
915 // Use the memory_module's addresses for each section to set the
916 // file module's load address as appropriate. We don't want to use
917 // a single slide value for the entire kext - different segments may
918 // be slid different amounts by the kext loader.
919
920 uint32_t num_sections_loaded = 0;
921 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
922 {
923 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
924 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000925 {
Greg Clayton67408532012-12-11 01:20:51 +0000926 // Don't ever load __LINKEDIT as it may or may not be actually
927 // mapped into memory and there is no current way to tell.
928 // I filed rdar://problem/12851706 to track being able to tell
929 // if the __LINKEDIT is actually mapped, but until then, we need
930 // to not load the __LINKEDIT
931 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
932 continue;
933
Jason Molendabb860bd2012-10-09 01:17:11 +0000934 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
935 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000936 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000937 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
Jason Molendabb860bd2012-10-09 01:17:11 +0000938 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000939 }
940 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000941 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000942 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000943 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000944 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000945 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000946 }
947 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000948 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000949 }
950 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000951 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000952 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000953 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000954 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000955 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000956
Greg Claytonc859e2d2012-02-13 23:10:39 +0000957 bool is_loaded = IsLoaded();
958
Jason Molenda306bd0a2013-02-19 05:42:46 +0000959 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000960 {
Greg Clayton44d93782014-01-27 23:43:24 +0000961 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda68b36072012-10-02 03:49:41 +0000962 if (s)
963 {
Jason Molenda732238c2013-03-01 00:06:37 +0000964 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
965 if (kernel_object_file)
966 {
967 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
968 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
969 {
Matt Kopec787d1622013-03-12 17:45:38 +0000970 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000971 }
972 }
Jason Molenda743e43962012-10-02 22:23:42 +0000973 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000974 s->Printf ("Loaded kernel file %s\n",
975 m_module_sp->GetFileSpec().GetPath().c_str());
Jason Molenda743e43962012-10-02 22:23:42 +0000976 }
Jason Molenda68b36072012-10-02 03:49:41 +0000977 s->Flush ();
978 }
979 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000980 return is_loaded;
981}
982
Greg Clayton1f746072012-08-29 21:13:06 +0000983uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000984DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000985{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000986 if (m_memory_module_sp)
987 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
988 if (m_module_sp)
989 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000990 return 0;
991}
992
993lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000994DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000995{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000996 if (m_memory_module_sp)
997 return m_memory_module_sp->GetArchitecture().GetByteOrder();
998 if (m_module_sp)
999 return m_module_sp->GetArchitecture().GetByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +00001000 return lldb::endian::InlHostByteOrder();
1001}
1002
1003lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +00001004DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +00001005{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001006 if (m_memory_module_sp)
1007 return m_memory_module_sp->GetArchitecture();
1008 if (m_module_sp)
1009 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001010 return lldb_private::ArchSpec ();
1011}
1012
1013
Greg Clayton7b242382011-07-08 00:48:09 +00001014//----------------------------------------------------------------------
1015// Load the kernel module and initialize the "m_kernel" member. Return
1016// true _only_ if the kernel is loaded the first time through (subsequent
1017// calls to this function should return false after the kernel has been
1018// already loaded).
1019//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001020void
Greg Clayton944b8282011-08-22 22:30:57 +00001021DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001022{
Greg Claytond16e1e52011-07-12 17:06:17 +00001023 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001024 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001025 m_kernel.Clear();
1026 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1027 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001028
1029 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001030 if (m_kernel.GetModule().get()
1031 && m_kernel.GetModule()->GetObjectFile()
1032 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001033 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001034 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001035 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001036 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001037
Jason Molenda306bd0a2013-02-19 05:42:46 +00001038 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001039 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001040 m_kernel.SetLoadAddress(m_kernel_load_address);
1041 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001042 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001043 // We didn't get a hint from the process, so we will
1044 // try the kernel at the address that it exists at in
1045 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001046 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001047 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001048 {
1049 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1050 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1051 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1052 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001053 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001054 if (load_address != file_address)
1055 {
1056 // Don't accidentally relocate the kernel to the File address --
1057 // the Load address has already been set to its actual in-memory address.
1058 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001059 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001060 }
1061 }
1062 else
1063 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001064 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001065 }
1066 }
Greg Clayton7b242382011-07-08 00:48:09 +00001067 }
1068 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001069
Jason Molenda306bd0a2013-02-19 05:42:46 +00001070 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001071 {
1072 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1073 {
1074 m_kernel.LoadImageAtFileAddress (m_process);
1075 }
1076 }
Greg Clayton7b242382011-07-08 00:48:09 +00001077
Jason Molenda306bd0a2013-02-19 05:42:46 +00001078 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001079 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001080 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001081 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001082 if (symbol)
1083 {
Greg Claytone7612132012-03-07 21:03:09 +00001084 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001085 // Update all image infos
1086 ReadAllKextSummaries ();
1087 }
Greg Clayton7b242382011-07-08 00:48:09 +00001088 }
1089 else
Greg Clayton7b242382011-07-08 00:48:09 +00001090 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001091 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001092 }
1093 }
Greg Clayton7b242382011-07-08 00:48:09 +00001094}
1095
Greg Clayton7b242382011-07-08 00:48:09 +00001096//----------------------------------------------------------------------
1097// Static callback function that gets called when our DYLD notification
1098// breakpoint gets hit. We update all of our image infos and then
1099// let our super class DynamicLoader class decide if we should stop
1100// or not (based on global preference).
1101//----------------------------------------------------------------------
1102bool
Greg Clayton944b8282011-08-22 22:30:57 +00001103DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001104 StoppointCallbackContext *context,
1105 user_id_t break_id,
1106 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001107{
Greg Clayton944b8282011-08-22 22:30:57 +00001108 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001109}
1110
1111bool
Greg Clayton944b8282011-08-22 22:30:57 +00001112DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001113 user_id_t break_id,
1114 user_id_t break_loc_id)
1115{
Greg Clayton5160ce52013-03-27 23:08:40 +00001116 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001117 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001118 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001119
Greg Clayton374972e2011-07-09 17:15:55 +00001120 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001121
1122 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001123 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001124
Greg Clayton374972e2011-07-09 17:15:55 +00001125 return GetStopWhenImagesChange();
1126}
1127
1128
1129bool
Greg Clayton944b8282011-08-22 22:30:57 +00001130DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001131{
1132 Mutex::Locker locker(m_mutex);
1133
1134 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001135
Greg Claytond16e1e52011-07-12 17:06:17 +00001136 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001137 {
1138 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1139 const ByteOrder byte_order = m_kernel.GetByteOrder();
1140 Error error;
1141 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001142 // which is currently 4 uint32_t and a pointer.
Greg Clayton7b242382011-07-08 00:48:09 +00001143 uint8_t buf[24];
1144 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1145 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001146 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001147 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1148 prefer_file_cache,
1149 error,
1150 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001151 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001152 // We got a valid address for our kext summary header and make sure it isn't NULL
1153 if (m_kext_summary_header_addr.IsValid() &&
1154 m_kext_summary_header_addr.GetFileAddress() != 0)
1155 {
1156 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1157 if (bytes_read == count)
1158 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001159 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001160 m_kext_summary_header.version = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001161 if (m_kext_summary_header.version > 128)
1162 {
1163 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1164 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 +00001165 // If we get an improbably large version number, we're probably getting bad memory.
Jason Molendac6fa5db2014-04-15 01:04:00 +00001166 m_kext_summary_header_addr.Clear();
1167 return false;
1168 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001169 if (m_kext_summary_header.version >= 2)
1170 {
1171 m_kext_summary_header.entry_size = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001172 if (m_kext_summary_header.entry_size > 4096)
1173 {
1174 // If we get an improbably large entry_size, we're probably getting bad memory.
1175 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1176 s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1177 m_kext_summary_header_addr.Clear();
1178 return false;
1179 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001180 }
1181 else
1182 {
1183 // Versions less than 2 didn't have an entry size, it was hard coded
1184 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1185 }
1186 m_kext_summary_header.entry_count = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001187 if (m_kext_summary_header.entry_count > 10000)
1188 {
1189 // If we get an improbably large number of kexts, we're probably getting bad memory.
1190 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1191 s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1192 m_kext_summary_header_addr.Clear();
1193 return false;
1194 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001195 return true;
1196 }
1197 }
Greg Clayton7b242382011-07-08 00:48:09 +00001198 }
1199 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001200 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001201 return false;
1202}
1203
Jason Molenda306bd0a2013-02-19 05:42:46 +00001204// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1205// and we need to figure out what kexts have been added or removed.
1206// Read the kext summaries from the inferior kernel memory, compare them against the
1207// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1208// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001209
1210bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001211DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001212{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001213 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001214 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001215 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001216 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001217
1218 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001219
1220 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1221 return false;
1222
Jason Molenda4da2e322013-02-26 00:26:47 +00001223 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1224 // kext loading, don't print any messages about kexts & don't try to read them.
1225 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1226
Jason Molenda306bd0a2013-02-19 05:42:46 +00001227 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1228 // we just found out about from ReadKextSummaries are marked as "add".
1229 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1230 std::vector<bool> to_be_added(count, true);
1231
1232 int number_of_new_kexts_being_added = 0;
1233 int number_of_old_kexts_being_removed = m_known_kexts.size();
1234
1235 const uint32_t new_kexts_size = kext_summaries.size();
1236 const uint32_t old_kexts_size = m_known_kexts.size();
1237
1238 // The m_known_kexts vector may have entries that have been Cleared,
1239 // or are a kernel.
1240 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1241 {
1242 bool ignore = false;
1243 KextImageInfo &image_info = m_known_kexts[old_kext];
1244 if (image_info.IsKernel())
1245 {
1246 ignore = true;
1247 }
1248 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1249 {
1250 ignore = true;
1251 }
1252
1253 if (ignore)
1254 {
1255 number_of_old_kexts_being_removed--;
1256 to_be_removed[old_kext] = false;
1257 }
1258 }
1259
1260 // Scan over the list of kexts we just read from the kernel, note those that
1261 // need to be added and those already loaded.
1262 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1263 {
1264 bool add_this_one = true;
1265 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1266 {
1267 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1268 {
1269 // We already have this kext, don't re-load it.
1270 to_be_added[new_kext] = false;
1271 // This kext is still present, do not remove it.
1272 to_be_removed[old_kext] = false;
1273
1274 number_of_old_kexts_being_removed--;
1275 add_this_one = false;
1276 break;
1277 }
1278 }
1279 if (add_this_one)
1280 {
1281 number_of_new_kexts_being_added++;
1282 }
1283 }
1284
1285 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1286 return true;
1287
Greg Clayton44d93782014-01-27 23:43:24 +00001288 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda4da2e322013-02-26 00:26:47 +00001289 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001290 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001291 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1292 {
1293 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1294 }
1295 else if (number_of_new_kexts_being_added > 0)
1296 {
1297 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1298 }
1299 else if (number_of_old_kexts_being_removed > 0)
1300 {
1301 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1302 }
Greg Clayton7b242382011-07-08 00:48:09 +00001303 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001304
1305 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001306 {
1307 if (load_kexts)
1308 {
1309 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1310 }
1311 else
1312 {
1313 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);
1314 }
1315 }
1316
Jason Molenda306bd0a2013-02-19 05:42:46 +00001317
1318 if (number_of_new_kexts_being_added > 0)
1319 {
1320 ModuleList loaded_module_list;
1321
1322 const uint32_t num_of_new_kexts = kext_summaries.size();
1323 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1324 {
1325 if (to_be_added[new_kext] == true)
1326 {
1327 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001328 if (load_kexts)
1329 {
1330 if (!image_info.LoadImageUsingMemoryModule (m_process))
1331 {
1332 image_info.LoadImageAtFileAddress (m_process);
1333 }
1334 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001335
1336 m_known_kexts.push_back(image_info);
1337
1338 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1339 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1340
Jason Molenda4da2e322013-02-26 00:26:47 +00001341 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001342 s->Printf (".");
1343
1344 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001345 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001346 }
1347 }
1348 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1349 }
1350
1351 if (number_of_old_kexts_being_removed > 0)
1352 {
1353 ModuleList loaded_module_list;
1354 const uint32_t num_of_old_kexts = m_known_kexts.size();
1355 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1356 {
1357 ModuleList unloaded_module_list;
1358 if (to_be_removed[old_kext])
1359 {
1360 KextImageInfo &image_info = m_known_kexts[old_kext];
1361 // You can't unload the kernel.
1362 if (!image_info.IsKernel())
1363 {
1364 if (image_info.GetModule())
1365 {
1366 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1367 }
1368 if (s)
1369 s->Printf (".");
1370 image_info.Clear();
1371 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1372 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1373 }
1374 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001375 m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001376 }
1377 }
1378
Jason Molenda4da2e322013-02-26 00:26:47 +00001379 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001380 {
1381 s->Printf (" done.\n");
1382 s->Flush ();
1383 }
1384
Jason Molenda306bd0a2013-02-19 05:42:46 +00001385 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001386}
1387
Greg Clayton7b242382011-07-08 00:48:09 +00001388uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001389DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001390 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001391 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001392{
1393 const ByteOrder endian = m_kernel.GetByteOrder();
1394 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1395
1396 image_infos.resize(image_infos_count);
1397 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1398 DataBufferHeap data(count, 0);
1399 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001400
Greg Clayton0d9fc762011-07-08 03:21:57 +00001401 const bool prefer_file_cache = false;
1402 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1403 prefer_file_cache,
1404 data.GetBytes(),
1405 data.GetByteSize(),
1406 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001407 if (bytes_read == count)
1408 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001409
Greg Clayton7b242382011-07-08 00:48:09 +00001410 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1411 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001412 for (uint32_t kext_summary_offset = 0;
1413 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1414 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001415 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001416 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001417 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1418 if (name_data == NULL)
1419 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001420 image_infos[i].SetName ((const char *) name_data);
1421 UUID uuid (extractor.GetData (&offset, 16), 16);
1422 image_infos[i].SetUUID (uuid);
1423 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1424 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001425 }
1426 if (i < image_infos.size())
1427 image_infos.resize(i);
1428 }
1429 else
1430 {
1431 image_infos.clear();
1432 }
1433 return image_infos.size();
1434}
1435
1436bool
Greg Clayton944b8282011-08-22 22:30:57 +00001437DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001438{
Greg Clayton7b242382011-07-08 00:48:09 +00001439 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001440
Greg Clayton7b242382011-07-08 00:48:09 +00001441 if (ReadKextSummaryHeader ())
1442 {
Greg Clayton374972e2011-07-09 17:15:55 +00001443 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001444 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001445 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001446 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001447 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001448 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001449 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001450 }
1451 return true;
1452 }
1453 }
1454 return false;
1455}
1456
1457//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001458// Dump an image info structure to the file handle provided.
1459//----------------------------------------------------------------------
1460void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001461DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001462{
1463 if (log == NULL)
1464 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001465 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001466
Jason Molenda306bd0a2013-02-19 05:42:46 +00001467 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001468 {
1469 if (u)
1470 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001471 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 +00001472 u[ 0], u[ 1], u[ 2], u[ 3],
1473 u[ 4], u[ 5], u[ 6], u[ 7],
1474 u[ 8], u[ 9], u[10], u[11],
1475 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001476 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001477 }
1478 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001479 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001480 }
1481 else
1482 {
1483 if (u)
1484 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001485 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\"",
1486 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001487 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1488 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001489 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001490 }
1491 else
1492 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001493 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1494 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001495 }
Greg Clayton7b242382011-07-08 00:48:09 +00001496 }
1497}
1498
1499//----------------------------------------------------------------------
1500// Dump the _dyld_all_image_infos members and all current image infos
1501// that we have parsed to the file handle provided.
1502//----------------------------------------------------------------------
1503void
Greg Clayton944b8282011-08-22 22:30:57 +00001504DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001505{
1506 if (log == NULL)
1507 return;
1508
1509 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001510 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001511 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001512 m_kext_summary_header.version,
1513 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001514 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001515
1516 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001517 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001518 if (count > 0)
1519 {
1520 log->PutCString("Loaded:");
1521 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001522 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001523 }
1524}
1525
1526void
Greg Clayton944b8282011-08-22 22:30:57 +00001527DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001528{
Greg Clayton944b8282011-08-22 22:30:57 +00001529 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001530 Clear(true);
1531 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001532}
1533
Greg Clayton374972e2011-07-09 17:15:55 +00001534void
Greg Clayton944b8282011-08-22 22:30:57 +00001535DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001536{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001537 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001538 {
Greg Clayton944b8282011-08-22 22:30:57 +00001539 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001540
Greg Claytond16e1e52011-07-12 17:06:17 +00001541
Jim Inghama8558b62012-05-22 00:12:20 +00001542 const bool internal_bp = true;
Greg Claytoneb023e72013-10-11 19:48:25 +00001543 const bool hardware = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001544 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001545 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001546 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001547 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001548 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001549 "OSKextLoadedKextSummariesUpdated",
1550 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +00001551 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001552 internal_bp,
1553 hardware).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001554
Greg Clayton944b8282011-08-22 22:30:57 +00001555 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001556 m_break_id = bp->GetID();
1557 }
Greg Clayton7b242382011-07-08 00:48:09 +00001558}
1559
1560//----------------------------------------------------------------------
1561// Member function that gets called when the process state changes.
1562//----------------------------------------------------------------------
1563void
Greg Clayton944b8282011-08-22 22:30:57 +00001564DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001565{
Greg Clayton944b8282011-08-22 22:30:57 +00001566 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001567 switch (state)
1568 {
1569 case eStateConnected:
1570 case eStateAttaching:
1571 case eStateLaunching:
1572 case eStateInvalid:
1573 case eStateUnloaded:
1574 case eStateExited:
1575 case eStateDetached:
1576 Clear(false);
1577 break;
1578
1579 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001580 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001581 break;
1582
1583 case eStateRunning:
1584 case eStateStepping:
1585 case eStateCrashed:
1586 case eStateSuspended:
1587 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001588 }
1589}
1590
1591ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001592DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001593{
1594 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001595 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001596 if (log)
1597 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001598 return thread_plan_sp;
1599}
1600
1601Error
Greg Clayton944b8282011-08-22 22:30:57 +00001602DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001603{
1604 Error error;
1605 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1606 return error;
1607}
1608
1609void
Greg Clayton944b8282011-08-22 22:30:57 +00001610DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001611{
1612 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1613 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001614 CreateInstance,
1615 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001616}
1617
1618void
Greg Clayton944b8282011-08-22 22:30:57 +00001619DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001620{
1621 PluginManager::UnregisterPlugin (CreateInstance);
1622}
1623
Greg Claytone8cd0c92012-10-19 18:02:49 +00001624void
1625DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1626{
1627 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1628 {
1629 const bool is_global_setting = true;
1630 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1631 GetGlobalProperties()->GetValueProperties(),
1632 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1633 is_global_setting);
1634 }
1635}
Greg Clayton7b242382011-07-08 00:48:09 +00001636
Greg Clayton57abc5d2013-05-10 21:47:16 +00001637lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001638DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001639{
Greg Clayton57abc5d2013-05-10 21:47:16 +00001640 static ConstString g_name("darwin-kernel");
1641 return g_name;
Greg Clayton7b242382011-07-08 00:48:09 +00001642}
1643
1644const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001645DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001646{
1647 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1648}
1649
1650
1651//------------------------------------------------------------------
1652// PluginInterface protocol
1653//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001654lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001655DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001656{
Greg Clayton7b242382011-07-08 00:48:09 +00001657 return GetPluginNameStatic();
1658}
1659
1660uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001661DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001662{
1663 return 1;
1664}
1665
Greg Clayton1f746072012-08-29 21:13:06 +00001666lldb::ByteOrder
1667DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1668{
1669 switch (magic)
1670 {
Charles Davis510938e2013-08-27 05:04:57 +00001671 case llvm::MachO::MH_MAGIC:
1672 case llvm::MachO::MH_MAGIC_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001673 return lldb::endian::InlHostByteOrder();
1674
Charles Davis510938e2013-08-27 05:04:57 +00001675 case llvm::MachO::MH_CIGAM:
1676 case llvm::MachO::MH_CIGAM_64:
Greg Clayton1f746072012-08-29 21:13:06 +00001677 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1678 return lldb::eByteOrderLittle;
1679 else
1680 return lldb::eByteOrderBig;
1681
1682 default:
1683 break;
1684 }
1685 return lldb::eByteOrderInvalid;
1686}
1687