blob: 847d18e4ddf6d07bdb239b9713718cdfed7a072f [file] [log] [blame]
Greg Clayton7b242382011-07-08 00:48:09 +00001//===-- DynamicLoaderMacOSXKernel.cpp -----------------------------*- C++ -*-===//
2//
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
10#include "lldb/Breakpoint/StoppointCallbackContext.h"
11#include "lldb/Core/DataBuffer.h"
12#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000013#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000014#include "lldb/Core/Log.h"
15#include "lldb/Core/Module.h"
16#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/State.h"
18#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Target/ObjCLanguageRuntime.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Target/ThreadPlanRunToAddress.h"
24#include "lldb/Target/StackFrame.h"
25
26#include "DynamicLoaderMacOSXKernel.h"
27
28//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
29#ifdef ENABLE_DEBUG_PRINTF
30#include <stdio.h>
31#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
32#else
33#define DEBUG_PRINTF(fmt, ...)
34#endif
35
36using namespace lldb;
37using namespace lldb_private;
38
39/// FIXME - The ObjC Runtime trampoline handler doesn't really belong here.
40/// I am putting it here so I can invoke it in the Trampoline code here, but
41/// it should be moved to the ObjC Runtime support when it is set up.
42
43
44//----------------------------------------------------------------------
45// Create an instance of this class. This function is filled into
46// the plugin info class that gets handed out by the plugin factory and
47// allows the lldb to instantiate an instance of this class.
48//----------------------------------------------------------------------
49DynamicLoader *
50DynamicLoaderMacOSXKernel::CreateInstance (Process* process, bool force)
51{
52 bool create = force;
53 if (!create)
54 {
Greg Claytondf0b7d52011-07-08 04:11:42 +000055 Module* exe_module = process->GetTarget().GetExecutableModule().get();
56 if (exe_module)
57 {
58 ObjectFile *object_file = exe_module->GetObjectFile();
59 if (object_file)
60 {
61 SectionList *section_list = object_file->GetSectionList();
62 if (section_list)
63 {
64 static ConstString g_kld_section_name ("__KLD");
65 if (section_list->FindSectionByName (g_kld_section_name))
66 {
67 create = true;
68 }
69 }
70 }
71 }
72
73 if (create)
74 {
75 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
76 create = triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple;
77 }
Greg Clayton7b242382011-07-08 00:48:09 +000078 }
79
80 if (create)
81 return new DynamicLoaderMacOSXKernel (process);
82 return NULL;
83}
84
85//----------------------------------------------------------------------
86// Constructor
87//----------------------------------------------------------------------
88DynamicLoaderMacOSXKernel::DynamicLoaderMacOSXKernel (Process* process) :
89 DynamicLoader(process),
90 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +000091 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +000092 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +000093 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +000094 m_break_id (LLDB_INVALID_BREAK_ID),
95 m_kext_summaries(),
Greg Clayton374972e2011-07-09 17:15:55 +000096 m_mutex(Mutex::eMutexTypeRecursive)
Greg Clayton7b242382011-07-08 00:48:09 +000097{
98}
99
100//----------------------------------------------------------------------
101// Destructor
102//----------------------------------------------------------------------
103DynamicLoaderMacOSXKernel::~DynamicLoaderMacOSXKernel()
104{
105 Clear(true);
106}
107
Greg Clayton374972e2011-07-09 17:15:55 +0000108void
109DynamicLoaderMacOSXKernel::UpdateIfNeeded()
110{
111 LoadKernelModuleIfNeeded();
112 SetNotificationBreakpointIfNeeded ();
113}
Greg Clayton7b242382011-07-08 00:48:09 +0000114//------------------------------------------------------------------
115/// Called after attaching a process.
116///
117/// Allow DynamicLoader plug-ins to execute some code after
118/// attaching to a process.
119//------------------------------------------------------------------
120void
121DynamicLoaderMacOSXKernel::DidAttach ()
122{
123 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000124 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000125}
126
127//------------------------------------------------------------------
128/// Called after attaching a process.
129///
130/// Allow DynamicLoader plug-ins to execute some code after
131/// attaching to a process.
132//------------------------------------------------------------------
133void
134DynamicLoaderMacOSXKernel::DidLaunch ()
135{
136 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000137 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000138}
139
140
141//----------------------------------------------------------------------
142// Clear out the state of this class.
143//----------------------------------------------------------------------
144void
145DynamicLoaderMacOSXKernel::Clear (bool clear_process)
146{
147 Mutex::Locker locker(m_mutex);
148
149 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
150 m_process->ClearBreakpointSiteByID(m_break_id);
151
152 if (clear_process)
153 m_process = NULL;
154 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000155 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000156 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000157 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000158 m_break_id = LLDB_INVALID_BREAK_ID;
159}
160
Greg Clayton7b242382011-07-08 00:48:09 +0000161
162//----------------------------------------------------------------------
163// Load the kernel module and initialize the "m_kernel" member. Return
164// true _only_ if the kernel is loaded the first time through (subsequent
165// calls to this function should return false after the kernel has been
166// already loaded).
167//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000168void
169DynamicLoaderMacOSXKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000170{
Greg Claytond16e1e52011-07-12 17:06:17 +0000171 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000172 {
173 m_kernel.Clear(false);
174 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
175 if (m_kernel.module_sp)
176 {
177 static ConstString mach_header_name ("_mh_execute_header");
Greg Claytondf0b7d52011-07-08 04:11:42 +0000178 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
179 const Symbol *symbol = NULL;
180 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
181 if (symbol)
Greg Claytond16e1e52011-07-12 17:06:17 +0000182 m_kext_summary_header_ptr_addr = symbol->GetValue();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000183
184 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (mach_header_name, eSymbolTypeAbsolute);
Greg Clayton7b242382011-07-08 00:48:09 +0000185 if (symbol)
186 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000187 // The "_mh_execute_header" symbol is absolute and not a section based
188 // symbol that will have a valid address, so we need to resolve it...
189 m_process->GetTarget().GetImages().ResolveFileAddress (symbol->GetValue().GetFileAddress(), m_kernel.so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000190 DataExtractor data; // Load command data
Greg Clayton0d9fc762011-07-08 03:21:57 +0000191 if (ReadMachHeader (m_kernel, &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000192 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000193 if (m_kernel.header.filetype == llvm::MachO::HeaderFileTypeExecutable)
Greg Clayton7b242382011-07-08 00:48:09 +0000194 {
195 if (ParseLoadCommands (data, m_kernel))
196 UpdateImageLoadAddress (m_kernel);
197
198 // Update all image infos
Greg Clayton374972e2011-07-09 17:15:55 +0000199 ReadAllKextSummaries ();
Greg Clayton7b242382011-07-08 00:48:09 +0000200 }
201 }
202 else
203 {
204 m_kernel.Clear(false);
205 }
Greg Clayton7b242382011-07-08 00:48:09 +0000206 }
207 }
208 }
Greg Clayton7b242382011-07-08 00:48:09 +0000209}
210
211bool
212DynamicLoaderMacOSXKernel::FindTargetModule (OSKextLoadedKextSummary &image_info, bool can_create, bool *did_create_ptr)
213{
214 if (did_create_ptr)
215 *did_create_ptr = false;
216
217 const bool image_info_uuid_is_valid = image_info.uuid.IsValid();
218
219 if (image_info.module_sp)
220 {
221 if (image_info_uuid_is_valid)
222 {
223 if (image_info.module_sp->GetUUID() == image_info.uuid)
224 return true;
225 else
226 image_info.module_sp.reset();
227 }
228 else
229 return true;
230 }
231
232 ModuleList &target_images = m_process->GetTarget().GetImages();
233 if (image_info_uuid_is_valid)
234 image_info.module_sp = target_images.FindModule(image_info.uuid);
235
236 if (image_info.module_sp)
237 return true;
238
239 ArchSpec arch (image_info.GetArchitecture ());
240 if (can_create)
241 {
242 if (image_info_uuid_is_valid)
243 {
244 image_info.module_sp = m_process->GetTarget().GetSharedModule (FileSpec(),
Greg Claytona63d08c2011-07-19 03:57:15 +0000245 arch,
246 &image_info.uuid);
Greg Clayton7b242382011-07-08 00:48:09 +0000247 if (did_create_ptr)
248 *did_create_ptr = image_info.module_sp;
249 }
250 }
251 return image_info.module_sp;
252}
253
254bool
255DynamicLoaderMacOSXKernel::UpdateCommPageLoadAddress(Module *module)
256{
257 bool changed = false;
258 if (module)
259 {
260 ObjectFile *image_object_file = module->GetObjectFile();
261 if (image_object_file)
262 {
263 SectionList *section_list = image_object_file->GetSectionList ();
264 if (section_list)
265 {
266 uint32_t num_sections = section_list->GetSize();
267 for (uint32_t i=0; i<num_sections; ++i)
268 {
269 Section* section = section_list->GetSectionAtIndex (i).get();
270 if (section)
271 {
272 const addr_t new_section_load_addr = section->GetFileAddress ();
273 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section);
274 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
275 old_section_load_addr != new_section_load_addr)
276 {
277 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress ()))
278 changed = true;
279 }
280 }
281 }
282 }
283 }
284 }
285 return changed;
286}
287
288//----------------------------------------------------------------------
289// Update the load addresses for all segments in MODULE using the
290// updated INFO that is passed in.
291//----------------------------------------------------------------------
292bool
293DynamicLoaderMacOSXKernel::UpdateImageLoadAddress (OSKextLoadedKextSummary& info)
294{
295 Module *module = info.module_sp.get();
296 bool changed = false;
297 if (module)
298 {
299 ObjectFile *image_object_file = module->GetObjectFile();
300 if (image_object_file)
301 {
302 SectionList *section_list = image_object_file->GetSectionList ();
303 if (section_list)
304 {
305 // We now know the slide amount, so go through all sections
306 // and update the load addresses with the correct values.
307 uint32_t num_segments = info.segments.size();
308 for (uint32_t i=0; i<num_segments; ++i)
309 {
Greg Clayton7b242382011-07-08 00:48:09 +0000310 const addr_t new_section_load_addr = info.segments[i].vmaddr;
Greg Claytona63d08c2011-07-19 03:57:15 +0000311 if (section_list->FindSectionByName(info.segments[i].name))
Greg Clayton7b242382011-07-08 00:48:09 +0000312 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000313 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
314 if (section_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000315 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000316 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section_sp.get());
317 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
318 old_section_load_addr != new_section_load_addr)
319 {
320 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), new_section_load_addr))
321 changed = true;
322 }
323 }
324 else
325 {
326 fprintf (stderr,
327 "warning: unable to find and load segment named '%s' at 0x%llx in '%s/%s' in macosx dynamic loader plug-in.\n",
328 info.segments[i].name.AsCString("<invalid>"),
329 (uint64_t)new_section_load_addr,
330 image_object_file->GetFileSpec().GetDirectory().AsCString(),
331 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000332 }
333 }
334 else
335 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000336 // The segment name is empty which means this is a .o file.
337 // Object files in LLDB end up getting reorganized so that
338 // the segment name that is in the section is promoted into
339 // an actual segment, so we just need to go through all sections
340 // and slide them by a single amount.
341
342 uint32_t num_sections = section_list->GetSize();
343 for (uint32_t i=0; i<num_sections; ++i)
344 {
345 Section* section = section_list->GetSectionAtIndex (i).get();
346 if (section)
347 {
348 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + new_section_load_addr))
349 changed = true;
350 }
351 }
Greg Clayton7b242382011-07-08 00:48:09 +0000352 }
353 }
354 }
355 }
356 }
357 return changed;
358}
359
360//----------------------------------------------------------------------
361// Update the load addresses for all segments in MODULE using the
362// updated INFO that is passed in.
363//----------------------------------------------------------------------
364bool
365DynamicLoaderMacOSXKernel::UnloadImageLoadAddress (OSKextLoadedKextSummary& info)
366{
367 Module *module = info.module_sp.get();
368 bool changed = false;
369 if (module)
370 {
371 ObjectFile *image_object_file = module->GetObjectFile();
372 if (image_object_file)
373 {
374 SectionList *section_list = image_object_file->GetSectionList ();
375 if (section_list)
376 {
377 uint32_t num_segments = info.segments.size();
378 for (uint32_t i=0; i<num_segments; ++i)
379 {
380 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
381 if (section_sp)
382 {
383 const addr_t old_section_load_addr = info.segments[i].vmaddr;
384 if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr))
385 changed = true;
386 }
387 else
388 {
389 fprintf (stderr,
390 "warning: unable to find and unload segment named '%s' in '%s/%s' in macosx dynamic loader plug-in.\n",
391 info.segments[i].name.AsCString("<invalid>"),
392 image_object_file->GetFileSpec().GetDirectory().AsCString(),
393 image_object_file->GetFileSpec().GetFilename().AsCString());
394 }
395 }
396 }
397 }
398 }
399 return changed;
400}
401
402
403//----------------------------------------------------------------------
404// Static callback function that gets called when our DYLD notification
405// breakpoint gets hit. We update all of our image infos and then
406// let our super class DynamicLoader class decide if we should stop
407// or not (based on global preference).
408//----------------------------------------------------------------------
409bool
Greg Clayton374972e2011-07-09 17:15:55 +0000410DynamicLoaderMacOSXKernel::BreakpointHitCallback (void *baton,
411 StoppointCallbackContext *context,
412 user_id_t break_id,
413 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000414{
Greg Clayton374972e2011-07-09 17:15:55 +0000415 return static_cast<DynamicLoaderMacOSXKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000416}
417
418bool
Greg Clayton374972e2011-07-09 17:15:55 +0000419DynamicLoaderMacOSXKernel::BreakpointHit (StoppointCallbackContext *context,
420 user_id_t break_id,
421 user_id_t break_loc_id)
422{
Greg Claytond16e1e52011-07-12 17:06:17 +0000423 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
424 if (log)
425 log->Printf ("DynamicLoaderMacOSXKernel::BreakpointHit (...)\n");
426
Greg Clayton374972e2011-07-09 17:15:55 +0000427 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000428
429 if (log)
430 PutToLog(log.get());
431
Greg Clayton374972e2011-07-09 17:15:55 +0000432 return GetStopWhenImagesChange();
433}
434
435
436bool
Greg Clayton7b242382011-07-08 00:48:09 +0000437DynamicLoaderMacOSXKernel::ReadKextSummaryHeader ()
438{
439 Mutex::Locker locker(m_mutex);
440
441 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000442
443 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000444 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000445 {
446 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
447 const ByteOrder byte_order = m_kernel.GetByteOrder();
448 Error error;
449 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
450 // which is currenty 4 uint32_t and a pointer.
451 uint8_t buf[24];
452 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
453 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000454 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000455 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
456 prefer_file_cache,
457 error,
458 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000459 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000460 // We got a valid address for our kext summary header and make sure it isn't NULL
461 if (m_kext_summary_header_addr.IsValid() &&
462 m_kext_summary_header_addr.GetFileAddress() != 0)
463 {
464 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
465 if (bytes_read == count)
466 {
467 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000468 m_kext_summary_header.version = data.GetU32(&offset);
469 if (m_kext_summary_header.version >= 2)
470 {
471 m_kext_summary_header.entry_size = data.GetU32(&offset);
472 }
473 else
474 {
475 // Versions less than 2 didn't have an entry size, it was hard coded
476 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
477 }
478 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000479 return true;
480 }
481 }
Greg Clayton7b242382011-07-08 00:48:09 +0000482 }
483 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000484 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000485 return false;
486}
487
488
489bool
Greg Clayton374972e2011-07-09 17:15:55 +0000490DynamicLoaderMacOSXKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000491 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000492{
493 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000494 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000495 if (log)
496 log->Printf ("Adding %d modules.\n");
497
498 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000499
500 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
501 return false;
502
Greg Clayton07e66e32011-07-20 03:41:06 +0000503 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000504 for (uint32_t i = 0; i < count; i++)
505 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000506 if (s)
507 {
508 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
509 if (u)
510 {
511 s->Printf("Loading kext: %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 0x%16.16llx \"%s\"...\n",
512 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
513 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
514 kext_summaries[i].address, kext_summaries[i].name);
515 }
516 else
517 {
518 s->Printf("0x%16.16llx \"%s\"...\n", kext_summaries[i].address, kext_summaries[i].name);
519 }
520 }
521
Greg Claytona63d08c2011-07-19 03:57:15 +0000522 DataExtractor data; // Load command data
523 if (ReadMachHeader (kext_summaries[i], &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000524 {
Greg Clayton7b242382011-07-08 00:48:09 +0000525 ParseLoadCommands (data, kext_summaries[i]);
526 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000527
Greg Clayton5b882162011-07-21 01:12:01 +0000528 if (s)
529 {
530 if (kext_summaries[i].module_sp)
531 s->Printf(" found kext: %s/%s\n",
532 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
533 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
534 }
535
Greg Claytona63d08c2011-07-19 03:57:15 +0000536 if (log)
537 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000538 }
539 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000540 return return_value;
541}
542
543// Adds the modules in image_infos to m_kext_summaries.
544// NB don't call this passing in m_kext_summaries.
545
546bool
547DynamicLoaderMacOSXKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
548{
549 // Now add these images to the main list.
550 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000551
552 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
553 {
Greg Clayton7b242382011-07-08 00:48:09 +0000554 m_kext_summaries.push_back(image_infos[idx]);
555
556 if (FindTargetModule (image_infos[idx], true, NULL))
557 {
558 // UpdateImageLoadAddress will return true if any segments
559 // change load address. We need to check this so we don't
560 // mention that all loaded shared libraries are newly loaded
561 // each time we hit out dyld breakpoint since dyld will list all
562 // shared libraries each time.
563 if (UpdateImageLoadAddress (image_infos[idx]))
564 {
565 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
566 }
567 }
568 }
569
570 if (loaded_module_list.GetSize() > 0)
571 {
572 // FIXME: This should really be in the Runtime handlers class, which should get
573 // called by the target's ModulesDidLoad, but we're doing it all locally for now
574 // to save time.
575 // Also, I'm assuming there can be only one libobjc dylib loaded...
576
577 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
578 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
579 {
580 size_t num_modules = loaded_module_list.GetSize();
581 for (int i = 0; i < num_modules; i++)
582 {
583 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
584 {
585 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
586 break;
587 }
588 }
589 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000590// if (log)
591// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderMacOSXKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000592 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
593 }
594 return true;
595}
596
Greg Clayton7b242382011-07-08 00:48:09 +0000597
598uint32_t
Greg Clayton374972e2011-07-09 17:15:55 +0000599DynamicLoaderMacOSXKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000600 uint32_t image_infos_count,
601 OSKextLoadedKextSummary::collection &image_infos)
602{
603 const ByteOrder endian = m_kernel.GetByteOrder();
604 const uint32_t addr_size = m_kernel.GetAddressByteSize();
605
606 image_infos.resize(image_infos_count);
607 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
608 DataBufferHeap data(count, 0);
609 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000610
611 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
612
613 if (s)
614 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000615 const bool prefer_file_cache = false;
616 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
617 prefer_file_cache,
618 data.GetBytes(),
619 data.GetByteSize(),
620 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000621 if (bytes_read == count)
622 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000623
Greg Clayton7b242382011-07-08 00:48:09 +0000624 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
625 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000626 for (uint32_t kext_summary_offset = 0;
627 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
628 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000629 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000630 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000631 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
632 if (name_data == NULL)
633 break;
634 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
635 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
636 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000637 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
638 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000639 image_infos[i].size = extractor.GetU64(&offset);
640 image_infos[i].version = extractor.GetU64(&offset);
641 image_infos[i].load_tag = extractor.GetU32(&offset);
642 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000643 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
644 {
645 image_infos[i].reference_list = extractor.GetU64(&offset);
646 }
647 else
648 {
649 image_infos[i].reference_list = 0;
650 }
Greg Clayton7b242382011-07-08 00:48:09 +0000651 }
652 if (i < image_infos.size())
653 image_infos.resize(i);
654 }
655 else
656 {
657 image_infos.clear();
658 }
659 return image_infos.size();
660}
661
662bool
Greg Clayton374972e2011-07-09 17:15:55 +0000663DynamicLoaderMacOSXKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000664{
Greg Clayton374972e2011-07-09 17:15:55 +0000665 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000666
667 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000668
Greg Clayton7b242382011-07-08 00:48:09 +0000669 if (ReadKextSummaryHeader ())
670 {
Greg Clayton374972e2011-07-09 17:15:55 +0000671 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000672 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000673 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000674 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000675 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000676 {
Greg Clayton7b242382011-07-08 00:48:09 +0000677 m_kext_summaries.clear();
678 }
679 return true;
680 }
681 }
682 return false;
683}
684
685//----------------------------------------------------------------------
686// Read a mach_header at ADDR into HEADER, and also fill in the load
687// command data into LOAD_COMMAND_DATA if it is non-NULL.
688//
689// Returns true if we succeed, false if we fail for any reason.
690//----------------------------------------------------------------------
691bool
Greg Clayton0d9fc762011-07-08 03:21:57 +0000692DynamicLoaderMacOSXKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data)
Greg Clayton7b242382011-07-08 00:48:09 +0000693{
694 DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
695 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000696 const bool prefer_file_cache = false;
697 size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address,
698 prefer_file_cache,
699 header_bytes.GetBytes(),
700 header_bytes.GetByteSize(),
701 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000702 if (bytes_read == sizeof(llvm::MachO::mach_header))
703 {
704 uint32_t offset = 0;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000705 ::memset (&kext_summary.header, 0, sizeof(kext_summary.header));
Greg Clayton7b242382011-07-08 00:48:09 +0000706
707 // Get the magic byte unswapped so we can figure out what we are dealing with
Greg Clayton374972e2011-07-09 17:15:55 +0000708 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000709 kext_summary.header.magic = data.GetU32(&offset);
710 Address load_cmd_addr = kext_summary.so_address;
711 data.SetByteOrder(DynamicLoaderMacOSXKernel::GetByteOrderFromMagic(kext_summary.header.magic));
712 switch (kext_summary.header.magic)
Greg Clayton7b242382011-07-08 00:48:09 +0000713 {
714 case llvm::MachO::HeaderMagic32:
715 case llvm::MachO::HeaderMagic32Swapped:
716 data.SetAddressByteSize(4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000717 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header));
Greg Clayton7b242382011-07-08 00:48:09 +0000718 break;
719
720 case llvm::MachO::HeaderMagic64:
721 case llvm::MachO::HeaderMagic64Swapped:
722 data.SetAddressByteSize(8);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000723 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64));
Greg Clayton7b242382011-07-08 00:48:09 +0000724 break;
725
726 default:
727 return false;
728 }
729
730 // Read the rest of dyld's mach header
Greg Clayton0d9fc762011-07-08 03:21:57 +0000731 if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1))
Greg Clayton7b242382011-07-08 00:48:09 +0000732 {
733 if (load_command_data == NULL)
734 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
735
Greg Clayton0d9fc762011-07-08 03:21:57 +0000736 DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0));
Greg Clayton7b242382011-07-08 00:48:09 +0000737
Greg Clayton0d9fc762011-07-08 03:21:57 +0000738 size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr,
739 prefer_file_cache,
740 load_cmd_data_sp->GetBytes(),
741 load_cmd_data_sp->GetByteSize(),
742 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000743
Greg Clayton0d9fc762011-07-08 03:21:57 +0000744 if (load_cmd_bytes_read == kext_summary.header.sizeofcmds)
Greg Clayton7b242382011-07-08 00:48:09 +0000745 {
746 // Set the load command data and also set the correct endian
747 // swap settings and the correct address size
Greg Clayton0d9fc762011-07-08 03:21:57 +0000748 load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds);
Greg Clayton7b242382011-07-08 00:48:09 +0000749 load_command_data->SetByteOrder(data.GetByteOrder());
750 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
751 return true; // We successfully read the mach_header and the load command data
752 }
753
754 return false; // We weren't able to read the load command data
755 }
756 }
757 return false; // We failed the read the mach_header
758}
759
760
761//----------------------------------------------------------------------
762// Parse the load commands for an image
763//----------------------------------------------------------------------
764uint32_t
Greg Claytondf0b7d52011-07-08 04:11:42 +0000765DynamicLoaderMacOSXKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info)
Greg Clayton7b242382011-07-08 00:48:09 +0000766{
767 uint32_t offset = 0;
768 uint32_t cmd_idx;
769 Segment segment;
Greg Claytondf0b7d52011-07-08 04:11:42 +0000770 image_info.Clear (true);
Greg Clayton7b242382011-07-08 00:48:09 +0000771
Greg Claytondf0b7d52011-07-08 04:11:42 +0000772 for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++)
Greg Clayton7b242382011-07-08 00:48:09 +0000773 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000774 // Clear out any load command specific data from image_info since
Greg Clayton7b242382011-07-08 00:48:09 +0000775 // we are about to read it.
776
777 if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command)))
778 {
779 llvm::MachO::load_command load_cmd;
780 uint32_t load_cmd_offset = offset;
781 load_cmd.cmd = data.GetU32 (&offset);
782 load_cmd.cmdsize = data.GetU32 (&offset);
783 switch (load_cmd.cmd)
784 {
785 case llvm::MachO::LoadCommandSegment32:
786 {
787 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
788 // We are putting 4 uint32_t values 4 uint64_t values so
789 // we have to use multiple 32 bit gets below.
790 segment.vmaddr = data.GetU32 (&offset);
791 segment.vmsize = data.GetU32 (&offset);
792 segment.fileoff = data.GetU32 (&offset);
793 segment.filesize = data.GetU32 (&offset);
794 // Extract maxprot, initprot, nsects and flags all at once
795 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000796 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000797 }
798 break;
799
800 case llvm::MachO::LoadCommandSegment64:
801 {
802 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
803 // Extract vmaddr, vmsize, fileoff, and filesize all at once
804 data.GetU64(&offset, &segment.vmaddr, 4);
805 // Extract maxprot, initprot, nsects and flags all at once
806 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000807 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000808 }
809 break;
810
811 case llvm::MachO::LoadCommandUUID:
Greg Claytondf0b7d52011-07-08 04:11:42 +0000812 image_info.uuid.SetBytes(data.GetData (&offset, 16));
Greg Clayton7b242382011-07-08 00:48:09 +0000813 break;
814
815 default:
816 break;
817 }
818 // Set offset to be the beginning of the next load command.
819 offset = load_cmd_offset + load_cmd.cmdsize;
820 }
821 }
822#if 0
823 // No slide in the kernel...
824
825 // All sections listed in the dyld image info structure will all
826 // either be fixed up already, or they will all be off by a single
827 // slide amount that is determined by finding the first segment
828 // that is at file offset zero which also has bytes (a file size
829 // that is greater than zero) in the object file.
830
831 // Determine the slide amount (if any)
Greg Claytondf0b7d52011-07-08 04:11:42 +0000832 const size_t num_sections = image_info.segments.size();
Greg Clayton7b242382011-07-08 00:48:09 +0000833 for (size_t i = 0; i < num_sections; ++i)
834 {
835 // Iterate through the object file sections to find the
836 // first section that starts of file offset zero and that
837 // has bytes in the file...
Greg Claytondf0b7d52011-07-08 04:11:42 +0000838 if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0)
Greg Clayton7b242382011-07-08 00:48:09 +0000839 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000840 image_info.slide = image_info.address - image_info.segments[i].vmaddr;
Greg Clayton7b242382011-07-08 00:48:09 +0000841 // We have found the slide amount, so we can exit
842 // this for loop.
843 break;
844 }
845 }
846#endif
Greg Claytondf0b7d52011-07-08 04:11:42 +0000847 if (image_info.uuid.IsValid())
848 {
849 bool did_create = false;
850 if (FindTargetModule(image_info, true, &did_create))
851 {
852 if (did_create)
853 image_info.module_create_stop_id = m_process->GetStopID();
854 }
855 }
Greg Clayton7b242382011-07-08 00:48:09 +0000856 return cmd_idx;
857}
858
859//----------------------------------------------------------------------
860// Dump a Segment to the file handle provided.
861//----------------------------------------------------------------------
862void
Greg Clayton374972e2011-07-09 17:15:55 +0000863DynamicLoaderMacOSXKernel::Segment::PutToLog (Log *log, addr_t slide) const
Greg Clayton7b242382011-07-08 00:48:09 +0000864{
865 if (log)
866 {
867 if (slide == 0)
868 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)",
869 name.AsCString(""),
870 vmaddr + slide,
871 vmaddr + slide + vmsize);
872 else
873 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx",
874 name.AsCString(""),
875 vmaddr + slide,
876 vmaddr + slide + vmsize,
877 slide);
878 }
879}
880
881const DynamicLoaderMacOSXKernel::Segment *
882DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const
883{
884 const size_t num_segments = segments.size();
885 for (size_t i=0; i<num_segments; ++i)
886 {
887 if (segments[i].name == name)
888 return &segments[i];
889 }
890 return NULL;
891}
892
893
894//----------------------------------------------------------------------
895// Dump an image info structure to the file handle provided.
896//----------------------------------------------------------------------
897void
898DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
899{
900 if (log == NULL)
901 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000902 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000903
904 if (address == LLDB_INVALID_ADDRESS)
905 {
906 if (u)
907 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000908 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 +0000909 u[ 0], u[ 1], u[ 2], u[ 3],
910 u[ 4], u[ 5], u[ 6], u[ 7],
911 u[ 8], u[ 9], u[10], u[11],
912 u[12], u[13], u[14], u[15],
913 name);
914 }
915 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000916 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000917 }
918 else
919 {
920 if (u)
921 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000922 log->Printf("\taddr=0x%16.16llx size=0x%16.16llx version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx 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\"",
923 address, size, version, load_tag, flags, reference_list,
924 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
925 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000926 name);
927 }
928 else
929 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000930 log->Printf("\t[0x%16.16llx - 0x%16.16llx) version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx name=\"%s\"",
931 address, address+size, version, load_tag, flags, reference_list,
932 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000933 }
934 for (uint32_t i=0; i<segments.size(); ++i)
935 segments[i].PutToLog(log, 0);
936 }
937}
938
939//----------------------------------------------------------------------
940// Dump the _dyld_all_image_infos members and all current image infos
941// that we have parsed to the file handle provided.
942//----------------------------------------------------------------------
943void
944DynamicLoaderMacOSXKernel::PutToLog(Log *log) const
945{
946 if (log == NULL)
947 return;
948
949 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000950 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000951 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000952 m_kext_summary_header.version,
953 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000954 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000955
956 size_t i;
957 const size_t count = m_kext_summaries.size();
958 if (count > 0)
959 {
960 log->PutCString("Loaded:");
961 for (i = 0; i<count; i++)
962 m_kext_summaries[i].PutToLog(log);
963 }
964}
965
966void
967DynamicLoaderMacOSXKernel::PrivateInitialize(Process *process)
968{
969 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
970 Clear(true);
971 m_process = process;
972 m_process->GetTarget().GetSectionLoadList().Clear();
973}
974
Greg Clayton374972e2011-07-09 17:15:55 +0000975void
976DynamicLoaderMacOSXKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000977{
Greg Clayton374972e2011-07-09 17:15:55 +0000978 if (m_break_id == LLDB_INVALID_BREAK_ID)
979 {
980 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
981
Greg Claytond16e1e52011-07-12 17:06:17 +0000982
Greg Clayton374972e2011-07-09 17:15:55 +0000983 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000984 const LazyBool skip_prologue = eLazyBoolNo;
Greg Clayton374972e2011-07-09 17:15:55 +0000985 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&m_kernel.module_sp->GetFileSpec(),
986 "OSKextLoadedKextSummariesUpdated",
987 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000988 internal_bp,
989 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000990
991 bp->SetCallback (DynamicLoaderMacOSXKernel::BreakpointHitCallback, this, true);
992 m_break_id = bp->GetID();
993 }
Greg Clayton7b242382011-07-08 00:48:09 +0000994}
995
996//----------------------------------------------------------------------
997// Member function that gets called when the process state changes.
998//----------------------------------------------------------------------
999void
1000DynamicLoaderMacOSXKernel::PrivateProcessStateChanged (Process *process, StateType state)
1001{
1002 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
1003 switch (state)
1004 {
1005 case eStateConnected:
1006 case eStateAttaching:
1007 case eStateLaunching:
1008 case eStateInvalid:
1009 case eStateUnloaded:
1010 case eStateExited:
1011 case eStateDetached:
1012 Clear(false);
1013 break;
1014
1015 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001016 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001017 break;
1018
1019 case eStateRunning:
1020 case eStateStepping:
1021 case eStateCrashed:
1022 case eStateSuspended:
1023 break;
1024
1025 default:
1026 break;
1027 }
1028}
1029
1030ThreadPlanSP
1031DynamicLoaderMacOSXKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
1032{
1033 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001034 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1035 if (log)
1036 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001037 return thread_plan_sp;
1038}
1039
1040Error
1041DynamicLoaderMacOSXKernel::CanLoadImage ()
1042{
1043 Error error;
1044 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1045 return error;
1046}
1047
1048void
1049DynamicLoaderMacOSXKernel::Initialize()
1050{
1051 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1052 GetPluginDescriptionStatic(),
1053 CreateInstance);
1054}
1055
1056void
1057DynamicLoaderMacOSXKernel::Terminate()
1058{
1059 PluginManager::UnregisterPlugin (CreateInstance);
1060}
1061
1062
1063const char *
1064DynamicLoaderMacOSXKernel::GetPluginNameStatic()
1065{
1066 return "dynamic-loader.macosx-kernel";
1067}
1068
1069const char *
1070DynamicLoaderMacOSXKernel::GetPluginDescriptionStatic()
1071{
1072 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1073}
1074
1075
1076//------------------------------------------------------------------
1077// PluginInterface protocol
1078//------------------------------------------------------------------
1079const char *
1080DynamicLoaderMacOSXKernel::GetPluginName()
1081{
1082 return "DynamicLoaderMacOSXKernel";
1083}
1084
1085const char *
1086DynamicLoaderMacOSXKernel::GetShortPluginName()
1087{
1088 return GetPluginNameStatic();
1089}
1090
1091uint32_t
1092DynamicLoaderMacOSXKernel::GetPluginVersion()
1093{
1094 return 1;
1095}
1096