blob: 0cb04468fbb6cb45179e92d6f13b11e722c3523f [file] [log] [blame]
Greg Clayton944b8282011-08-22 22:30:57 +00001//===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -*-===//
Greg Clayton7b242382011-07-08 00:48:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton7b242382011-07-08 00:48:09 +000012#include "lldb/Breakpoint/StoppointCallbackContext.h"
13#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000015#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Greg Clayton7b242382011-07-08 00:48:09 +000019#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Clayton7b242382011-07-08 00:48:09 +000021#include "lldb/Core/State.h"
Jason Molenda68b36072012-10-02 03:49:41 +000022#include "lldb/Host/Symbols.h"
Greg Clayton7b242382011-07-08 00:48:09 +000023#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000024#include "lldb/Target/RegisterContext.h"
Jason Molenda68b36072012-10-02 03:49:41 +000025#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000026#include "lldb/Target/Target.h"
27#include "lldb/Target/Thread.h"
28#include "lldb/Target/ThreadPlanRunToAddress.h"
Jason Molenda68b36072012-10-02 03:49:41 +000029
Greg Clayton7b242382011-07-08 00:48:09 +000030
Greg Clayton944b8282011-08-22 22:30:57 +000031#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000032
33//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
34#ifdef ENABLE_DEBUG_PRINTF
35#include <stdio.h>
36#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
37#else
38#define DEBUG_PRINTF(fmt, ...)
39#endif
40
41using namespace lldb;
42using namespace lldb_private;
43
Greg Claytone8cd0c92012-10-19 18:02:49 +000044static PropertyDefinition
45g_properties[] =
46{
Greg Clayton66763ee2012-10-19 20:53:18 +000047 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
48 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000049};
50
51enum {
Greg Clayton66763ee2012-10-19 20:53:18 +000052 ePropertyLoadKexts
Greg Claytone8cd0c92012-10-19 18:02:49 +000053};
54
55class DynamicLoaderDarwinKernelProperties : public Properties
56{
57public:
58
59 static ConstString &
60 GetSettingName ()
61 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000062 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000063 return g_setting_name;
64 }
65
66 DynamicLoaderDarwinKernelProperties() :
67 Properties ()
68 {
69 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
70 m_collection_sp->Initialize(g_properties);
71 }
72
73 virtual
74 ~DynamicLoaderDarwinKernelProperties()
75 {
76 }
77
78 bool
Greg Clayton66763ee2012-10-19 20:53:18 +000079 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +000080 {
Greg Clayton66763ee2012-10-19 20:53:18 +000081 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +000082 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
83 }
84
85};
86
87typedef STD_SHARED_PTR(DynamicLoaderDarwinKernelProperties) DynamicLoaderDarwinKernelPropertiesSP;
88
89static const DynamicLoaderDarwinKernelPropertiesSP &
90GetGlobalProperties()
91{
92 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
93 if (!g_settings_sp)
94 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
95 return g_settings_sp;
96}
97
Greg Clayton7b242382011-07-08 00:48:09 +000098//----------------------------------------------------------------------
99// Create an instance of this class. This function is filled into
100// the plugin info class that gets handed out by the plugin factory and
101// allows the lldb to instantiate an instance of this class.
102//----------------------------------------------------------------------
103DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000104DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000105{
106 bool create = force;
107 if (!create)
108 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000109 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000110 if (exe_module)
111 {
112 ObjectFile *object_file = exe_module->GetObjectFile();
113 if (object_file)
114 {
Sean Callanan49bce8e2012-02-10 20:22:35 +0000115 create = (object_file->GetStrata() == ObjectFile::eStrataKernel);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000116 }
117 }
118
119 if (create)
120 {
121 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +0000122 switch (triple_ref.getOS())
123 {
124 case llvm::Triple::Darwin:
125 case llvm::Triple::MacOSX:
126 case llvm::Triple::IOS:
127 create = triple_ref.getVendor() == llvm::Triple::Apple;
128 break;
129 default:
130 create = false;
131 break;
132 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000133 }
Greg Clayton7b242382011-07-08 00:48:09 +0000134 }
135
136 if (create)
Sean Callanan90539452011-09-20 23:01:51 +0000137 {
138 process->SetCanJIT(false);
Greg Clayton944b8282011-08-22 22:30:57 +0000139 return new DynamicLoaderDarwinKernel (process);
Sean Callanan90539452011-09-20 23:01:51 +0000140 }
Greg Clayton7b242382011-07-08 00:48:09 +0000141 return NULL;
142}
143
144//----------------------------------------------------------------------
145// Constructor
146//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000147DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
Greg Clayton7b242382011-07-08 00:48:09 +0000148 DynamicLoader(process),
149 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000150 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000151 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000152 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +0000153 m_kext_summaries(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000154 m_mutex(Mutex::eMutexTypeRecursive),
155 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000156{
157}
158
159//----------------------------------------------------------------------
160// Destructor
161//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000162DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000163{
164 Clear(true);
165}
166
Greg Clayton374972e2011-07-09 17:15:55 +0000167void
Greg Clayton944b8282011-08-22 22:30:57 +0000168DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000169{
170 LoadKernelModuleIfNeeded();
171 SetNotificationBreakpointIfNeeded ();
172}
Greg Clayton7b242382011-07-08 00:48:09 +0000173//------------------------------------------------------------------
174/// Called after attaching a process.
175///
176/// Allow DynamicLoader plug-ins to execute some code after
177/// attaching to a process.
178//------------------------------------------------------------------
179void
Greg Clayton944b8282011-08-22 22:30:57 +0000180DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000181{
182 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000183 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000184}
185
186//------------------------------------------------------------------
187/// Called after attaching a process.
188///
189/// Allow DynamicLoader plug-ins to execute some code after
190/// attaching to a process.
191//------------------------------------------------------------------
192void
Greg Clayton944b8282011-08-22 22:30:57 +0000193DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000194{
195 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000196 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000197}
198
199
200//----------------------------------------------------------------------
201// Clear out the state of this class.
202//----------------------------------------------------------------------
203void
Greg Clayton944b8282011-08-22 22:30:57 +0000204DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000205{
206 Mutex::Locker locker(m_mutex);
207
208 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
209 m_process->ClearBreakpointSiteByID(m_break_id);
210
211 if (clear_process)
212 m_process = NULL;
213 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000214 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000215 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000216 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000217 m_break_id = LLDB_INVALID_BREAK_ID;
218}
219
Greg Clayton7b242382011-07-08 00:48:09 +0000220
Greg Claytonc859e2d2012-02-13 23:10:39 +0000221bool
Greg Clayton2af282a2012-03-21 04:25:00 +0000222DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageAtFileAddress (Process *process)
223{
224 if (IsLoaded())
225 return true;
226
227 if (module_sp)
228 {
229 bool changed = false;
230 if (module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
231 load_process_stop_id = process->GetStopID();
232 }
233 return false;
234}
235
236bool
Greg Claytonc859e2d2012-02-13 23:10:39 +0000237DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process)
238{
239 if (IsLoaded())
240 return true;
241
242 bool uuid_is_valid = uuid.IsValid();
Jason Molenda68b36072012-10-02 03:49:41 +0000243 bool memory_module_is_kernel = false;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000244
245 Target &target = process->GetTarget();
246 ModuleSP memory_module_sp;
Jason Molenda87a04b22012-10-19 03:40:45 +0000247
248 // If this is a kext and the user asked us to ignore kexts, don't try to load it.
Greg Clayton66763ee2012-10-19 20:53:18 +0000249 if (kernel_image == false && GetGlobalProperties()->GetLoadKexts() == false)
Jason Molenda87a04b22012-10-19 03:40:45 +0000250 {
251 return false;
252 }
253
254 // Use the memory module as the module if we have one
Greg Claytonc859e2d2012-02-13 23:10:39 +0000255 if (address != LLDB_INVALID_ADDRESS)
256 {
257 FileSpec file_spec;
258 if (module_sp)
259 file_spec = module_sp->GetFileSpec();
260 else
261 file_spec.SetFile (name, false);
262
263 memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false);
264 if (memory_module_sp && !uuid_is_valid)
265 {
266 uuid = memory_module_sp->GetUUID();
267 uuid_is_valid = uuid.IsValid();
268 }
Jason Molendac56bd082012-11-08 00:19:28 +0000269 if (memory_module_sp
270 && memory_module_sp->GetObjectFile()
Jason Molenda68b36072012-10-02 03:49:41 +0000271 && memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
272 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
273 {
274 memory_module_is_kernel = true;
Jason Molenda53667f52012-10-06 02:02:26 +0000275 if (memory_module_sp->GetArchitecture().IsValid())
276 {
277 target.SetArchitecture(memory_module_sp->GetArchitecture());
278 }
Jason Molenda68b36072012-10-02 03:49:41 +0000279 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000280 }
281
282 if (!module_sp)
283 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000284 if (uuid_is_valid)
285 {
Enrico Granata17598482012-11-08 02:22:02 +0000286 const ModuleList &target_images = target.GetImages();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000287 module_sp = target_images.FindModule(uuid);
Jason Molenda53667f52012-10-06 02:02:26 +0000288
Greg Claytonc859e2d2012-02-13 23:10:39 +0000289 if (!module_sp)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000290 {
Greg Clayton2af282a2012-03-21 04:25:00 +0000291 ModuleSpec module_spec;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000292 module_spec.GetUUID() = uuid;
Jason Molenda53667f52012-10-06 02:02:26 +0000293 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molendabb860bd2012-10-09 01:17:11 +0000294
295 // For the kernel, we really do need an on-disk file copy of the
296 // binary.
297 bool force_symbols_search = false;
298 if (memory_module_is_kernel)
299 {
300 force_symbols_search = true;
301 }
302
303 if (Symbols::DownloadObjectAndSymbolFile (module_spec, force_symbols_search))
304 {
305 if (module_spec.GetFileSpec().Exists())
306 {
307 module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
308 if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec))
309 {
310 ModuleList loaded_module_list;
311 loaded_module_list.Append (module_sp);
312 target.ModulesDidLoad (loaded_module_list);
313 }
314 }
315 }
316
317 // Ask the Target to find this file on the local system, if possible.
318 // This will search in the list of currently-loaded files, look in the
319 // standard search paths on the system, and on a Mac it will try calling
320 // the DebugSymbols framework with the UUID to find the binary via its
321 // search methods.
322 if (!module_sp)
323 {
324 module_sp = target.GetSharedModule (module_spec);
325 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000326
327 // If we managed to find a module, append it to the target's list of images
328 if (module_sp && module_sp->GetUUID() == memory_module_sp->GetUUID())
329 {
330 target.GetImages().Append(module_sp);
331 if (memory_module_is_kernel && target.GetExecutableModulePointer() != module_sp.get())
332 {
333 target.SetExecutableModule (module_sp, false);
334 }
335 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000336 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000337 }
338 }
339
340
Greg Clayton67408532012-12-11 01:20:51 +0000341 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
342
Jason Molendabb860bd2012-10-09 01:17:11 +0000343 if (memory_module_sp && module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000344 {
Jason Molendabb860bd2012-10-09 01:17:11 +0000345 if (module_sp->GetUUID() == memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000346 {
Jason Molendabb860bd2012-10-09 01:17:11 +0000347 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
348 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000349
Jason Molendabb860bd2012-10-09 01:17:11 +0000350 if (memory_object_file && ondisk_object_file)
351 {
Greg Clayton67408532012-12-11 01:20:51 +0000352 // Kexts are classified with a type of ObjectFile::eTypeSharedLibrary and
353 // a strata of ObjectFile::eStrataKernel. Ignore __LINKEDIT for kexts
354 const bool ignore_linkedit = ondisk_object_file->GetType() == ObjectFile::eTypeSharedLibrary;
355
Jason Molendabb860bd2012-10-09 01:17:11 +0000356 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
357 SectionList *memory_section_list = memory_object_file->GetSectionList ();
358 if (memory_section_list && ondisk_section_list)
359 {
360 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
361 // There may be CTF sections in the memory image so we can't
362 // always just compare the number of sections (which are actually
363 // segments in mach-o parlance)
364 uint32_t sect_idx = 0;
365
366 // Use the memory_module's addresses for each section to set the
367 // file module's load address as appropriate. We don't want to use
368 // a single slide value for the entire kext - different segments may
369 // be slid different amounts by the kext loader.
370
371 uint32_t num_sections_loaded = 0;
372 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
373 {
374 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
375 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000376 {
Greg Clayton67408532012-12-11 01:20:51 +0000377 // Don't ever load __LINKEDIT as it may or may not be actually
378 // mapped into memory and there is no current way to tell.
379 // I filed rdar://problem/12851706 to track being able to tell
380 // if the __LINKEDIT is actually mapped, but until then, we need
381 // to not load the __LINKEDIT
382 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
383 continue;
384
Jason Molendabb860bd2012-10-09 01:17:11 +0000385 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
386 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000387 {
Jason Molendabb860bd2012-10-09 01:17:11 +0000388 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
389 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000390 }
391 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000392 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000393 if (num_sections_loaded > 0)
394 load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000395 else
Jason Molendabb860bd2012-10-09 01:17:11 +0000396 module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000397 }
398 else
Jason Molendabb860bd2012-10-09 01:17:11 +0000399 module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000400 }
401 else
Jason Molendabb860bd2012-10-09 01:17:11 +0000402 module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000403 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000404 else
405 module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000406 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000407
Greg Claytonc859e2d2012-02-13 23:10:39 +0000408 bool is_loaded = IsLoaded();
409
410 if (so_address.IsValid())
411 {
412 if (is_loaded)
413 so_address.SetLoadAddress (address, &target);
414 else
415 target.GetImages().ResolveFileAddress (address, so_address);
416
417 }
Jason Molenda68b36072012-10-02 03:49:41 +0000418
419 if (is_loaded && module_sp && memory_module_is_kernel)
420 {
421 Stream *s = &target.GetDebugger().GetOutputStream();
422 if (s)
423 {
424 char uuidbuf[64];
425 s->Printf ("Kernel UUID: %s\n", module_sp->GetUUID().GetAsCString(uuidbuf, sizeof (uuidbuf)));
Daniel Malead01b2952012-11-29 21:49:15 +0000426 s->Printf ("Load Address: 0x%" PRIx64 "\n", address);
Jason Molenda743e43962012-10-02 22:23:42 +0000427 if (module_sp->GetFileSpec().GetDirectory().IsEmpty())
428 {
429 s->Printf ("Loaded kernel file %s\n", module_sp->GetFileSpec().GetFilename().AsCString());
430 }
431 else
432 {
433 s->Printf ("Loaded kernel file %s/%s\n",
434 module_sp->GetFileSpec().GetDirectory().AsCString(),
435 module_sp->GetFileSpec().GetFilename().AsCString());
436 }
Jason Molenda68b36072012-10-02 03:49:41 +0000437 s->Flush ();
438 }
439 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000440 return is_loaded;
441}
442
Greg Clayton1f746072012-08-29 21:13:06 +0000443uint32_t
444DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetAddressByteSize ()
445{
446 if (module_sp)
447 return module_sp->GetArchitecture().GetAddressByteSize();
448 return 0;
449}
450
451lldb::ByteOrder
452DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetByteOrder()
453{
454 if (module_sp)
455 return module_sp->GetArchitecture().GetByteOrder();
456 return lldb::endian::InlHostByteOrder();
457}
458
459lldb_private::ArchSpec
460DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::GetArchitecture () const
461{
462 if (module_sp)
463 return module_sp->GetArchitecture();
464 return lldb_private::ArchSpec ();
465}
466
467
Greg Clayton7b242382011-07-08 00:48:09 +0000468//----------------------------------------------------------------------
469// Load the kernel module and initialize the "m_kernel" member. Return
470// true _only_ if the kernel is loaded the first time through (subsequent
471// calls to this function should return false after the kernel has been
472// already loaded).
473//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000474void
Greg Clayton944b8282011-08-22 22:30:57 +0000475DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000476{
Greg Claytond16e1e52011-07-12 17:06:17 +0000477 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000478 {
479 m_kernel.Clear(false);
480 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Jason Molenda87a04b22012-10-19 03:40:45 +0000481 m_kernel.kernel_image = true;
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000482
483 ConstString kernel_name("mach_kernel");
484 if (m_kernel.module_sp.get()
485 && m_kernel.module_sp->GetObjectFile()
486 && !m_kernel.module_sp->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
487 {
488 kernel_name = m_kernel.module_sp->GetObjectFile()->GetFileSpec().GetFilename();
489 }
Jason Molenda31a69612012-10-04 02:16:06 +0000490 strncpy (m_kernel.name, kernel_name.AsCString(), sizeof(m_kernel.name));
491 m_kernel.name[sizeof (m_kernel.name) - 1] = '\0';
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000492
Greg Claytonc859e2d2012-02-13 23:10:39 +0000493 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000494 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000495 m_kernel.address = m_process->GetImageInfoAddress ();
496 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000497 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000498 // We didn't get a hint from the process, so we will
499 // try the kernel at the address that it exists at in
500 // the file if we have one
501 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
502 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000503 {
504 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
505 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
506 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
507 {
508 m_kernel.address = load_address;
509 if (load_address != file_address)
510 {
511 // Don't accidentally relocate the kernel to the File address --
512 // the Load address has already been set to its actual in-memory address.
513 // Mark it as IsLoaded.
514 m_kernel.load_process_stop_id = m_process->GetStopID();
515 }
516 }
517 else
518 {
519 m_kernel.address = file_address;
520 }
521 }
Greg Clayton7b242382011-07-08 00:48:09 +0000522 }
523 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000524
525 if (m_kernel.address != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +0000526 {
527 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
528 {
529 m_kernel.LoadImageAtFileAddress (m_process);
530 }
531 }
Greg Clayton7b242382011-07-08 00:48:09 +0000532
Jim Ingham28eb5712012-10-12 17:34:26 +0000533 if (m_kernel.IsLoaded() && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000534 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000535 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
536 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
537 if (symbol)
538 {
Greg Claytone7612132012-03-07 21:03:09 +0000539 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000540 // Update all image infos
541 ReadAllKextSummaries ();
542 }
Greg Clayton7b242382011-07-08 00:48:09 +0000543 }
544 else
Greg Clayton7b242382011-07-08 00:48:09 +0000545 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000546 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000547 }
548 }
Greg Clayton7b242382011-07-08 00:48:09 +0000549}
550
Greg Clayton7b242382011-07-08 00:48:09 +0000551//----------------------------------------------------------------------
552// Static callback function that gets called when our DYLD notification
553// breakpoint gets hit. We update all of our image infos and then
554// let our super class DynamicLoader class decide if we should stop
555// or not (based on global preference).
556//----------------------------------------------------------------------
557bool
Greg Clayton944b8282011-08-22 22:30:57 +0000558DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000559 StoppointCallbackContext *context,
560 user_id_t break_id,
561 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000562{
Greg Clayton944b8282011-08-22 22:30:57 +0000563 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000564}
565
566bool
Greg Clayton944b8282011-08-22 22:30:57 +0000567DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000568 user_id_t break_id,
569 user_id_t break_loc_id)
570{
Greg Claytond16e1e52011-07-12 17:06:17 +0000571 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
572 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000573 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000574
Greg Clayton374972e2011-07-09 17:15:55 +0000575 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000576
577 if (log)
578 PutToLog(log.get());
579
Greg Clayton374972e2011-07-09 17:15:55 +0000580 return GetStopWhenImagesChange();
581}
582
583
584bool
Greg Clayton944b8282011-08-22 22:30:57 +0000585DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000586{
587 Mutex::Locker locker(m_mutex);
588
589 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000590
591 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000592 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000593 {
594 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
595 const ByteOrder byte_order = m_kernel.GetByteOrder();
596 Error error;
597 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
598 // which is currenty 4 uint32_t and a pointer.
599 uint8_t buf[24];
600 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
601 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000602 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000603 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
604 prefer_file_cache,
605 error,
606 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000607 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000608 // We got a valid address for our kext summary header and make sure it isn't NULL
609 if (m_kext_summary_header_addr.IsValid() &&
610 m_kext_summary_header_addr.GetFileAddress() != 0)
611 {
612 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
613 if (bytes_read == count)
614 {
615 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000616 m_kext_summary_header.version = data.GetU32(&offset);
617 if (m_kext_summary_header.version >= 2)
618 {
619 m_kext_summary_header.entry_size = data.GetU32(&offset);
620 }
621 else
622 {
623 // Versions less than 2 didn't have an entry size, it was hard coded
624 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
625 }
626 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000627 return true;
628 }
629 }
Greg Clayton7b242382011-07-08 00:48:09 +0000630 }
631 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000632 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000633 return false;
634}
635
636
637bool
Greg Clayton944b8282011-08-22 22:30:57 +0000638DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000639 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000640{
641 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000642 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000643 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000644 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000645
646 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000647
648 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
649 return false;
650
Greg Clayton07e66e32011-07-20 03:41:06 +0000651 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Jason Molenda68b36072012-10-02 03:49:41 +0000652 if (s)
653 s->Printf ("Loading %d kext modules ", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000654 for (uint32_t i = 0; i < count; i++)
655 {
Greg Clayton2af282a2012-03-21 04:25:00 +0000656 if (!kext_summaries[i].LoadImageUsingMemoryModule (m_process))
657 kext_summaries[i].LoadImageAtFileAddress (m_process);
Greg Claytonc859e2d2012-02-13 23:10:39 +0000658
Greg Clayton5b882162011-07-21 01:12:01 +0000659 if (s)
Jason Molenda68b36072012-10-02 03:49:41 +0000660 s->Printf (".");
661
Greg Claytona63d08c2011-07-19 03:57:15 +0000662 if (log)
663 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000664 }
Jason Molenda68b36072012-10-02 03:49:41 +0000665 if (s)
666 {
667 s->Printf (" done.\n");
668 s->Flush ();
669 }
670
Greg Clayton7b242382011-07-08 00:48:09 +0000671 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000672 return return_value;
673}
674
675// Adds the modules in image_infos to m_kext_summaries.
676// NB don't call this passing in m_kext_summaries.
677
678bool
Greg Clayton944b8282011-08-22 22:30:57 +0000679DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000680{
681 // Now add these images to the main list.
682 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000683
684 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
685 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000686 OSKextLoadedKextSummary &image_info = image_infos[idx];
687 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000688
Greg Claytonc859e2d2012-02-13 23:10:39 +0000689 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
690 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000691 }
692
Enrico Granata17598482012-11-08 02:22:02 +0000693 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
Greg Clayton7b242382011-07-08 00:48:09 +0000694 return true;
695}
696
Greg Clayton7b242382011-07-08 00:48:09 +0000697
698uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000699DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000700 uint32_t image_infos_count,
701 OSKextLoadedKextSummary::collection &image_infos)
702{
703 const ByteOrder endian = m_kernel.GetByteOrder();
704 const uint32_t addr_size = m_kernel.GetAddressByteSize();
705
706 image_infos.resize(image_infos_count);
707 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
708 DataBufferHeap data(count, 0);
709 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000710
Greg Clayton0d9fc762011-07-08 03:21:57 +0000711 const bool prefer_file_cache = false;
712 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
713 prefer_file_cache,
714 data.GetBytes(),
715 data.GetByteSize(),
716 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000717 if (bytes_read == count)
718 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000719
Greg Clayton7b242382011-07-08 00:48:09 +0000720 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
721 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000722 for (uint32_t kext_summary_offset = 0;
723 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
724 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000725 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000726 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000727 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
728 if (name_data == NULL)
729 break;
730 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
731 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
732 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000733 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
734 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000735 image_infos[i].size = extractor.GetU64(&offset);
736 image_infos[i].version = extractor.GetU64(&offset);
737 image_infos[i].load_tag = extractor.GetU32(&offset);
738 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000739 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
740 {
741 image_infos[i].reference_list = extractor.GetU64(&offset);
742 }
743 else
744 {
745 image_infos[i].reference_list = 0;
746 }
Greg Clayton7b242382011-07-08 00:48:09 +0000747 }
748 if (i < image_infos.size())
749 image_infos.resize(i);
750 }
751 else
752 {
753 image_infos.clear();
754 }
755 return image_infos.size();
756}
757
758bool
Greg Clayton944b8282011-08-22 22:30:57 +0000759DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000760{
Greg Clayton374972e2011-07-09 17:15:55 +0000761 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000762
763 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000764
Greg Clayton7b242382011-07-08 00:48:09 +0000765 if (ReadKextSummaryHeader ())
766 {
Greg Clayton374972e2011-07-09 17:15:55 +0000767 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000768 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000769 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000770 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000771 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000772 {
Greg Clayton7b242382011-07-08 00:48:09 +0000773 m_kext_summaries.clear();
774 }
775 return true;
776 }
777 }
778 return false;
779}
780
781//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000782// Dump an image info structure to the file handle provided.
783//----------------------------------------------------------------------
784void
Greg Clayton944b8282011-08-22 22:30:57 +0000785DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000786{
787 if (log == NULL)
788 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000789 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000790
791 if (address == LLDB_INVALID_ADDRESS)
792 {
793 if (u)
794 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000795 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 +0000796 u[ 0], u[ 1], u[ 2], u[ 3],
797 u[ 4], u[ 5], u[ 6], u[ 7],
798 u[ 8], u[ 9], u[10], u[11],
799 u[12], u[13], u[14], u[15],
800 name);
801 }
802 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000803 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000804 }
805 else
806 {
807 if (u)
808 {
Daniel Malead01b2952012-11-29 21:49:15 +0000809 log->Printf("\taddr=0x%16.16" PRIx64 " size=0x%16.16" PRIx64 " version=0x%16.16" PRIx64 " load-tag=0x%8.8x flags=0x%8.8x ref-list=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\"",
Greg Claytona63d08c2011-07-19 03:57:15 +0000810 address, size, version, load_tag, flags, reference_list,
811 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
812 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000813 name);
814 }
815 else
816 {
Daniel Malead01b2952012-11-29 21:49:15 +0000817 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") version=0x%16.16" PRIx64 " load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16" PRIx64 " name=\"%s\"",
Greg Claytona63d08c2011-07-19 03:57:15 +0000818 address, address+size, version, load_tag, flags, reference_list,
819 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000820 }
Greg Clayton7b242382011-07-08 00:48:09 +0000821 }
822}
823
824//----------------------------------------------------------------------
825// Dump the _dyld_all_image_infos members and all current image infos
826// that we have parsed to the file handle provided.
827//----------------------------------------------------------------------
828void
Greg Clayton944b8282011-08-22 22:30:57 +0000829DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000830{
831 if (log == NULL)
832 return;
833
834 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000835 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000836 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000837 m_kext_summary_header.version,
838 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000839 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000840
841 size_t i;
842 const size_t count = m_kext_summaries.size();
843 if (count > 0)
844 {
845 log->PutCString("Loaded:");
846 for (i = 0; i<count; i++)
847 m_kext_summaries[i].PutToLog(log);
848 }
849}
850
851void
Greg Clayton944b8282011-08-22 22:30:57 +0000852DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000853{
Greg Clayton944b8282011-08-22 22:30:57 +0000854 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000855 Clear(true);
856 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +0000857}
858
Greg Clayton374972e2011-07-09 17:15:55 +0000859void
Greg Clayton944b8282011-08-22 22:30:57 +0000860DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000861{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000862 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000863 {
Greg Clayton944b8282011-08-22 22:30:57 +0000864 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000865
Greg Claytond16e1e52011-07-12 17:06:17 +0000866
Jim Inghama8558b62012-05-22 00:12:20 +0000867 const bool internal_bp = true;
Greg Claytond16e1e52011-07-12 17:06:17 +0000868 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000869 FileSpecList module_spec_list;
870 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
871 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000872 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000873 "OSKextLoadedKextSummariesUpdated",
874 eFunctionNameTypeFull,
Jim Inghama8558b62012-05-22 00:12:20 +0000875 skip_prologue,
876 internal_bp).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000877
Greg Clayton944b8282011-08-22 22:30:57 +0000878 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000879 m_break_id = bp->GetID();
880 }
Greg Clayton7b242382011-07-08 00:48:09 +0000881}
882
883//----------------------------------------------------------------------
884// Member function that gets called when the process state changes.
885//----------------------------------------------------------------------
886void
Greg Clayton944b8282011-08-22 22:30:57 +0000887DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000888{
Greg Clayton944b8282011-08-22 22:30:57 +0000889 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000890 switch (state)
891 {
892 case eStateConnected:
893 case eStateAttaching:
894 case eStateLaunching:
895 case eStateInvalid:
896 case eStateUnloaded:
897 case eStateExited:
898 case eStateDetached:
899 Clear(false);
900 break;
901
902 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000903 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000904 break;
905
906 case eStateRunning:
907 case eStateStepping:
908 case eStateCrashed:
909 case eStateSuspended:
910 break;
Greg Clayton7b242382011-07-08 00:48:09 +0000911 }
912}
913
914ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000915DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000916{
917 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000918 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
919 if (log)
920 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000921 return thread_plan_sp;
922}
923
924Error
Greg Clayton944b8282011-08-22 22:30:57 +0000925DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000926{
927 Error error;
928 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
929 return error;
930}
931
932void
Greg Clayton944b8282011-08-22 22:30:57 +0000933DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000934{
935 PluginManager::RegisterPlugin (GetPluginNameStatic(),
936 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +0000937 CreateInstance,
938 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +0000939}
940
941void
Greg Clayton944b8282011-08-22 22:30:57 +0000942DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000943{
944 PluginManager::UnregisterPlugin (CreateInstance);
945}
946
Greg Claytone8cd0c92012-10-19 18:02:49 +0000947void
948DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
949{
950 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
951 {
952 const bool is_global_setting = true;
953 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
954 GetGlobalProperties()->GetValueProperties(),
955 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
956 is_global_setting);
957 }
958}
Greg Clayton7b242382011-07-08 00:48:09 +0000959
960const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000961DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000962{
Greg Clayton468ea4e2012-10-19 18:14:47 +0000963 return "dynamic-loader.darwin-kernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000964}
965
966const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000967DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000968{
969 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
970}
971
972
973//------------------------------------------------------------------
974// PluginInterface protocol
975//------------------------------------------------------------------
976const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000977DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000978{
Greg Clayton944b8282011-08-22 22:30:57 +0000979 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000980}
981
982const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000983DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000984{
985 return GetPluginNameStatic();
986}
987
988uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000989DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000990{
991 return 1;
992}
993
Greg Clayton1f746072012-08-29 21:13:06 +0000994lldb::ByteOrder
995DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
996{
997 switch (magic)
998 {
999 case llvm::MachO::HeaderMagic32:
1000 case llvm::MachO::HeaderMagic64:
1001 return lldb::endian::InlHostByteOrder();
1002
1003 case llvm::MachO::HeaderMagic32Swapped:
1004 case llvm::MachO::HeaderMagic64Swapped:
1005 if (lldb::endian::InlHostByteOrder() == lldb::eByteOrderBig)
1006 return lldb::eByteOrderLittle;
1007 else
1008 return lldb::eByteOrderBig;
1009
1010 default:
1011 break;
1012 }
1013 return lldb::eByteOrderInvalid;
1014}
1015