blob: 2361b6394dc64c65b617b2863a00bc3157657284 [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
528 if (log)
529 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000530 }
531 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000532 return return_value;
533}
534
535// Adds the modules in image_infos to m_kext_summaries.
536// NB don't call this passing in m_kext_summaries.
537
538bool
539DynamicLoaderMacOSXKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
540{
541 // Now add these images to the main list.
542 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000543
544 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
545 {
Greg Clayton7b242382011-07-08 00:48:09 +0000546 m_kext_summaries.push_back(image_infos[idx]);
547
548 if (FindTargetModule (image_infos[idx], true, NULL))
549 {
550 // UpdateImageLoadAddress will return true if any segments
551 // change load address. We need to check this so we don't
552 // mention that all loaded shared libraries are newly loaded
553 // each time we hit out dyld breakpoint since dyld will list all
554 // shared libraries each time.
555 if (UpdateImageLoadAddress (image_infos[idx]))
556 {
557 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
558 }
559 }
560 }
561
562 if (loaded_module_list.GetSize() > 0)
563 {
564 // FIXME: This should really be in the Runtime handlers class, which should get
565 // called by the target's ModulesDidLoad, but we're doing it all locally for now
566 // to save time.
567 // Also, I'm assuming there can be only one libobjc dylib loaded...
568
569 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
570 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
571 {
572 size_t num_modules = loaded_module_list.GetSize();
573 for (int i = 0; i < num_modules; i++)
574 {
575 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
576 {
577 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
578 break;
579 }
580 }
581 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000582// if (log)
583// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderMacOSXKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000584 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
585 }
586 return true;
587}
588
Greg Clayton7b242382011-07-08 00:48:09 +0000589
590uint32_t
Greg Clayton374972e2011-07-09 17:15:55 +0000591DynamicLoaderMacOSXKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000592 uint32_t image_infos_count,
593 OSKextLoadedKextSummary::collection &image_infos)
594{
595 const ByteOrder endian = m_kernel.GetByteOrder();
596 const uint32_t addr_size = m_kernel.GetAddressByteSize();
597
598 image_infos.resize(image_infos_count);
599 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
600 DataBufferHeap data(count, 0);
601 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000602 const bool prefer_file_cache = false;
603 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
604 prefer_file_cache,
605 data.GetBytes(),
606 data.GetByteSize(),
607 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000608 if (bytes_read == count)
609 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000610
Greg Clayton7b242382011-07-08 00:48:09 +0000611 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
612 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000613 for (uint32_t kext_summary_offset = 0;
614 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
615 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000616 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000617 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000618 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
619 if (name_data == NULL)
620 break;
621 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
622 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
623 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000624 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
625 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000626 image_infos[i].size = extractor.GetU64(&offset);
627 image_infos[i].version = extractor.GetU64(&offset);
628 image_infos[i].load_tag = extractor.GetU32(&offset);
629 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000630 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
631 {
632 image_infos[i].reference_list = extractor.GetU64(&offset);
633 }
634 else
635 {
636 image_infos[i].reference_list = 0;
637 }
Greg Clayton7b242382011-07-08 00:48:09 +0000638 }
639 if (i < image_infos.size())
640 image_infos.resize(i);
641 }
642 else
643 {
644 image_infos.clear();
645 }
646 return image_infos.size();
647}
648
649bool
Greg Clayton374972e2011-07-09 17:15:55 +0000650DynamicLoaderMacOSXKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000651{
Greg Clayton374972e2011-07-09 17:15:55 +0000652 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000653
654 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000655
Greg Clayton7b242382011-07-08 00:48:09 +0000656 if (ReadKextSummaryHeader ())
657 {
Greg Clayton374972e2011-07-09 17:15:55 +0000658 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000659 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000660 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000661 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000662 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000663 {
Greg Clayton7b242382011-07-08 00:48:09 +0000664 m_kext_summaries.clear();
665 }
666 return true;
667 }
668 }
669 return false;
670}
671
672//----------------------------------------------------------------------
673// Read a mach_header at ADDR into HEADER, and also fill in the load
674// command data into LOAD_COMMAND_DATA if it is non-NULL.
675//
676// Returns true if we succeed, false if we fail for any reason.
677//----------------------------------------------------------------------
678bool
Greg Clayton0d9fc762011-07-08 03:21:57 +0000679DynamicLoaderMacOSXKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data)
Greg Clayton7b242382011-07-08 00:48:09 +0000680{
681 DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
682 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000683 const bool prefer_file_cache = false;
684 size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address,
685 prefer_file_cache,
686 header_bytes.GetBytes(),
687 header_bytes.GetByteSize(),
688 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000689 if (bytes_read == sizeof(llvm::MachO::mach_header))
690 {
691 uint32_t offset = 0;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000692 ::memset (&kext_summary.header, 0, sizeof(kext_summary.header));
Greg Clayton7b242382011-07-08 00:48:09 +0000693
694 // Get the magic byte unswapped so we can figure out what we are dealing with
Greg Clayton374972e2011-07-09 17:15:55 +0000695 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000696 kext_summary.header.magic = data.GetU32(&offset);
697 Address load_cmd_addr = kext_summary.so_address;
698 data.SetByteOrder(DynamicLoaderMacOSXKernel::GetByteOrderFromMagic(kext_summary.header.magic));
699 switch (kext_summary.header.magic)
Greg Clayton7b242382011-07-08 00:48:09 +0000700 {
701 case llvm::MachO::HeaderMagic32:
702 case llvm::MachO::HeaderMagic32Swapped:
703 data.SetAddressByteSize(4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000704 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header));
Greg Clayton7b242382011-07-08 00:48:09 +0000705 break;
706
707 case llvm::MachO::HeaderMagic64:
708 case llvm::MachO::HeaderMagic64Swapped:
709 data.SetAddressByteSize(8);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000710 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64));
Greg Clayton7b242382011-07-08 00:48:09 +0000711 break;
712
713 default:
714 return false;
715 }
716
717 // Read the rest of dyld's mach header
Greg Clayton0d9fc762011-07-08 03:21:57 +0000718 if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1))
Greg Clayton7b242382011-07-08 00:48:09 +0000719 {
720 if (load_command_data == NULL)
721 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
722
Greg Clayton0d9fc762011-07-08 03:21:57 +0000723 DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0));
Greg Clayton7b242382011-07-08 00:48:09 +0000724
Greg Clayton0d9fc762011-07-08 03:21:57 +0000725 size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr,
726 prefer_file_cache,
727 load_cmd_data_sp->GetBytes(),
728 load_cmd_data_sp->GetByteSize(),
729 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000730
Greg Clayton0d9fc762011-07-08 03:21:57 +0000731 if (load_cmd_bytes_read == kext_summary.header.sizeofcmds)
Greg Clayton7b242382011-07-08 00:48:09 +0000732 {
733 // Set the load command data and also set the correct endian
734 // swap settings and the correct address size
Greg Clayton0d9fc762011-07-08 03:21:57 +0000735 load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds);
Greg Clayton7b242382011-07-08 00:48:09 +0000736 load_command_data->SetByteOrder(data.GetByteOrder());
737 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
738 return true; // We successfully read the mach_header and the load command data
739 }
740
741 return false; // We weren't able to read the load command data
742 }
743 }
744 return false; // We failed the read the mach_header
745}
746
747
748//----------------------------------------------------------------------
749// Parse the load commands for an image
750//----------------------------------------------------------------------
751uint32_t
Greg Claytondf0b7d52011-07-08 04:11:42 +0000752DynamicLoaderMacOSXKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info)
Greg Clayton7b242382011-07-08 00:48:09 +0000753{
754 uint32_t offset = 0;
755 uint32_t cmd_idx;
756 Segment segment;
Greg Claytondf0b7d52011-07-08 04:11:42 +0000757 image_info.Clear (true);
Greg Clayton7b242382011-07-08 00:48:09 +0000758
Greg Claytondf0b7d52011-07-08 04:11:42 +0000759 for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++)
Greg Clayton7b242382011-07-08 00:48:09 +0000760 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000761 // Clear out any load command specific data from image_info since
Greg Clayton7b242382011-07-08 00:48:09 +0000762 // we are about to read it.
763
764 if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command)))
765 {
766 llvm::MachO::load_command load_cmd;
767 uint32_t load_cmd_offset = offset;
768 load_cmd.cmd = data.GetU32 (&offset);
769 load_cmd.cmdsize = data.GetU32 (&offset);
770 switch (load_cmd.cmd)
771 {
772 case llvm::MachO::LoadCommandSegment32:
773 {
774 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
775 // We are putting 4 uint32_t values 4 uint64_t values so
776 // we have to use multiple 32 bit gets below.
777 segment.vmaddr = data.GetU32 (&offset);
778 segment.vmsize = data.GetU32 (&offset);
779 segment.fileoff = data.GetU32 (&offset);
780 segment.filesize = data.GetU32 (&offset);
781 // Extract maxprot, initprot, nsects and flags all at once
782 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000783 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000784 }
785 break;
786
787 case llvm::MachO::LoadCommandSegment64:
788 {
789 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
790 // Extract vmaddr, vmsize, fileoff, and filesize all at once
791 data.GetU64(&offset, &segment.vmaddr, 4);
792 // Extract maxprot, initprot, nsects and flags all at once
793 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000794 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000795 }
796 break;
797
798 case llvm::MachO::LoadCommandUUID:
Greg Claytondf0b7d52011-07-08 04:11:42 +0000799 image_info.uuid.SetBytes(data.GetData (&offset, 16));
Greg Clayton7b242382011-07-08 00:48:09 +0000800 break;
801
802 default:
803 break;
804 }
805 // Set offset to be the beginning of the next load command.
806 offset = load_cmd_offset + load_cmd.cmdsize;
807 }
808 }
809#if 0
810 // No slide in the kernel...
811
812 // All sections listed in the dyld image info structure will all
813 // either be fixed up already, or they will all be off by a single
814 // slide amount that is determined by finding the first segment
815 // that is at file offset zero which also has bytes (a file size
816 // that is greater than zero) in the object file.
817
818 // Determine the slide amount (if any)
Greg Claytondf0b7d52011-07-08 04:11:42 +0000819 const size_t num_sections = image_info.segments.size();
Greg Clayton7b242382011-07-08 00:48:09 +0000820 for (size_t i = 0; i < num_sections; ++i)
821 {
822 // Iterate through the object file sections to find the
823 // first section that starts of file offset zero and that
824 // has bytes in the file...
Greg Claytondf0b7d52011-07-08 04:11:42 +0000825 if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0)
Greg Clayton7b242382011-07-08 00:48:09 +0000826 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000827 image_info.slide = image_info.address - image_info.segments[i].vmaddr;
Greg Clayton7b242382011-07-08 00:48:09 +0000828 // We have found the slide amount, so we can exit
829 // this for loop.
830 break;
831 }
832 }
833#endif
Greg Claytondf0b7d52011-07-08 04:11:42 +0000834 if (image_info.uuid.IsValid())
835 {
836 bool did_create = false;
837 if (FindTargetModule(image_info, true, &did_create))
838 {
839 if (did_create)
840 image_info.module_create_stop_id = m_process->GetStopID();
841 }
842 }
Greg Clayton7b242382011-07-08 00:48:09 +0000843 return cmd_idx;
844}
845
846//----------------------------------------------------------------------
847// Dump a Segment to the file handle provided.
848//----------------------------------------------------------------------
849void
Greg Clayton374972e2011-07-09 17:15:55 +0000850DynamicLoaderMacOSXKernel::Segment::PutToLog (Log *log, addr_t slide) const
Greg Clayton7b242382011-07-08 00:48:09 +0000851{
852 if (log)
853 {
854 if (slide == 0)
855 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)",
856 name.AsCString(""),
857 vmaddr + slide,
858 vmaddr + slide + vmsize);
859 else
860 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx",
861 name.AsCString(""),
862 vmaddr + slide,
863 vmaddr + slide + vmsize,
864 slide);
865 }
866}
867
868const DynamicLoaderMacOSXKernel::Segment *
869DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const
870{
871 const size_t num_segments = segments.size();
872 for (size_t i=0; i<num_segments; ++i)
873 {
874 if (segments[i].name == name)
875 return &segments[i];
876 }
877 return NULL;
878}
879
880
881//----------------------------------------------------------------------
882// Dump an image info structure to the file handle provided.
883//----------------------------------------------------------------------
884void
885DynamicLoaderMacOSXKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
886{
887 if (log == NULL)
888 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000889 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000890
891 if (address == LLDB_INVALID_ADDRESS)
892 {
893 if (u)
894 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000895 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 +0000896 u[ 0], u[ 1], u[ 2], u[ 3],
897 u[ 4], u[ 5], u[ 6], u[ 7],
898 u[ 8], u[ 9], u[10], u[11],
899 u[12], u[13], u[14], u[15],
900 name);
901 }
902 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000903 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000904 }
905 else
906 {
907 if (u)
908 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000909 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\"",
910 address, size, version, load_tag, flags, reference_list,
911 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
912 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000913 name);
914 }
915 else
916 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000917 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\"",
918 address, address+size, version, load_tag, flags, reference_list,
919 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000920 }
921 for (uint32_t i=0; i<segments.size(); ++i)
922 segments[i].PutToLog(log, 0);
923 }
924}
925
926//----------------------------------------------------------------------
927// Dump the _dyld_all_image_infos members and all current image infos
928// that we have parsed to the file handle provided.
929//----------------------------------------------------------------------
930void
931DynamicLoaderMacOSXKernel::PutToLog(Log *log) const
932{
933 if (log == NULL)
934 return;
935
936 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000937 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000938 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000939 m_kext_summary_header.version,
940 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000941 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000942
943 size_t i;
944 const size_t count = m_kext_summaries.size();
945 if (count > 0)
946 {
947 log->PutCString("Loaded:");
948 for (i = 0; i<count; i++)
949 m_kext_summaries[i].PutToLog(log);
950 }
951}
952
953void
954DynamicLoaderMacOSXKernel::PrivateInitialize(Process *process)
955{
956 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
957 Clear(true);
958 m_process = process;
959 m_process->GetTarget().GetSectionLoadList().Clear();
960}
961
Greg Clayton374972e2011-07-09 17:15:55 +0000962void
963DynamicLoaderMacOSXKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000964{
Greg Clayton374972e2011-07-09 17:15:55 +0000965 if (m_break_id == LLDB_INVALID_BREAK_ID)
966 {
967 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
968
Greg Claytond16e1e52011-07-12 17:06:17 +0000969
Greg Clayton374972e2011-07-09 17:15:55 +0000970 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000971 const LazyBool skip_prologue = eLazyBoolNo;
Greg Clayton374972e2011-07-09 17:15:55 +0000972 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&m_kernel.module_sp->GetFileSpec(),
973 "OSKextLoadedKextSummariesUpdated",
974 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000975 internal_bp,
976 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000977
978 bp->SetCallback (DynamicLoaderMacOSXKernel::BreakpointHitCallback, this, true);
979 m_break_id = bp->GetID();
980 }
Greg Clayton7b242382011-07-08 00:48:09 +0000981}
982
983//----------------------------------------------------------------------
984// Member function that gets called when the process state changes.
985//----------------------------------------------------------------------
986void
987DynamicLoaderMacOSXKernel::PrivateProcessStateChanged (Process *process, StateType state)
988{
989 DEBUG_PRINTF("DynamicLoaderMacOSXKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
990 switch (state)
991 {
992 case eStateConnected:
993 case eStateAttaching:
994 case eStateLaunching:
995 case eStateInvalid:
996 case eStateUnloaded:
997 case eStateExited:
998 case eStateDetached:
999 Clear(false);
1000 break;
1001
1002 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001003 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001004 break;
1005
1006 case eStateRunning:
1007 case eStateStepping:
1008 case eStateCrashed:
1009 case eStateSuspended:
1010 break;
1011
1012 default:
1013 break;
1014 }
1015}
1016
1017ThreadPlanSP
1018DynamicLoaderMacOSXKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
1019{
1020 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001021 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1022 if (log)
1023 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001024 return thread_plan_sp;
1025}
1026
1027Error
1028DynamicLoaderMacOSXKernel::CanLoadImage ()
1029{
1030 Error error;
1031 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1032 return error;
1033}
1034
1035void
1036DynamicLoaderMacOSXKernel::Initialize()
1037{
1038 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1039 GetPluginDescriptionStatic(),
1040 CreateInstance);
1041}
1042
1043void
1044DynamicLoaderMacOSXKernel::Terminate()
1045{
1046 PluginManager::UnregisterPlugin (CreateInstance);
1047}
1048
1049
1050const char *
1051DynamicLoaderMacOSXKernel::GetPluginNameStatic()
1052{
1053 return "dynamic-loader.macosx-kernel";
1054}
1055
1056const char *
1057DynamicLoaderMacOSXKernel::GetPluginDescriptionStatic()
1058{
1059 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1060}
1061
1062
1063//------------------------------------------------------------------
1064// PluginInterface protocol
1065//------------------------------------------------------------------
1066const char *
1067DynamicLoaderMacOSXKernel::GetPluginName()
1068{
1069 return "DynamicLoaderMacOSXKernel";
1070}
1071
1072const char *
1073DynamicLoaderMacOSXKernel::GetShortPluginName()
1074{
1075 return GetPluginNameStatic();
1076}
1077
1078uint32_t
1079DynamicLoaderMacOSXKernel::GetPluginVersion()
1080{
1081 return 1;
1082}
1083