blob: 55e0af68cb8a963644cc3a8bbf8aa61527065986 [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
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
Greg Clayton944b8282011-08-22 22:30:57 +000026#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000027
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 *
Greg Clayton944b8282011-08-22 22:30:57 +000050DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +000051{
52 bool create = force;
53 if (!create)
54 {
Greg Claytonaa149cb2011-08-11 02:48:45 +000055 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +000056 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)
Sean Callanan90539452011-09-20 23:01:51 +000081 {
82 process->SetCanJIT(false);
Greg Clayton944b8282011-08-22 22:30:57 +000083 return new DynamicLoaderDarwinKernel (process);
Sean Callanan90539452011-09-20 23:01:51 +000084 }
Greg Clayton7b242382011-07-08 00:48:09 +000085 return NULL;
86}
87
88//----------------------------------------------------------------------
89// Constructor
90//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000091DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
Greg Clayton7b242382011-07-08 00:48:09 +000092 DynamicLoader(process),
93 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +000094 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +000095 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +000096 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +000097 m_break_id (LLDB_INVALID_BREAK_ID),
98 m_kext_summaries(),
Greg Clayton374972e2011-07-09 17:15:55 +000099 m_mutex(Mutex::eMutexTypeRecursive)
Greg Clayton7b242382011-07-08 00:48:09 +0000100{
101}
102
103//----------------------------------------------------------------------
104// Destructor
105//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000106DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000107{
108 Clear(true);
109}
110
Greg Clayton374972e2011-07-09 17:15:55 +0000111void
Greg Clayton944b8282011-08-22 22:30:57 +0000112DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000113{
114 LoadKernelModuleIfNeeded();
115 SetNotificationBreakpointIfNeeded ();
116}
Greg Clayton7b242382011-07-08 00:48:09 +0000117//------------------------------------------------------------------
118/// Called after attaching a process.
119///
120/// Allow DynamicLoader plug-ins to execute some code after
121/// attaching to a process.
122//------------------------------------------------------------------
123void
Greg Clayton944b8282011-08-22 22:30:57 +0000124DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000125{
126 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000127 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000128}
129
130//------------------------------------------------------------------
131/// Called after attaching a process.
132///
133/// Allow DynamicLoader plug-ins to execute some code after
134/// attaching to a process.
135//------------------------------------------------------------------
136void
Greg Clayton944b8282011-08-22 22:30:57 +0000137DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000138{
139 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000140 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000141}
142
143
144//----------------------------------------------------------------------
145// Clear out the state of this class.
146//----------------------------------------------------------------------
147void
Greg Clayton944b8282011-08-22 22:30:57 +0000148DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000149{
150 Mutex::Locker locker(m_mutex);
151
152 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
153 m_process->ClearBreakpointSiteByID(m_break_id);
154
155 if (clear_process)
156 m_process = NULL;
157 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000158 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000159 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000160 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000161 m_break_id = LLDB_INVALID_BREAK_ID;
162}
163
Greg Clayton7b242382011-07-08 00:48:09 +0000164
165//----------------------------------------------------------------------
166// Load the kernel module and initialize the "m_kernel" member. Return
167// true _only_ if the kernel is loaded the first time through (subsequent
168// calls to this function should return false after the kernel has been
169// already loaded).
170//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000171void
Greg Clayton944b8282011-08-22 22:30:57 +0000172DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000173{
Greg Claytond16e1e52011-07-12 17:06:17 +0000174 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000175 {
176 m_kernel.Clear(false);
177 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
178 if (m_kernel.module_sp)
179 {
180 static ConstString mach_header_name ("_mh_execute_header");
Greg Claytondf0b7d52011-07-08 04:11:42 +0000181 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
182 const Symbol *symbol = NULL;
183 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
184 if (symbol)
Greg Claytond16e1e52011-07-12 17:06:17 +0000185 m_kext_summary_header_ptr_addr = symbol->GetValue();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000186
187 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (mach_header_name, eSymbolTypeAbsolute);
Greg Clayton7b242382011-07-08 00:48:09 +0000188 if (symbol)
189 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000190 // The "_mh_execute_header" symbol is absolute and not a section based
191 // symbol that will have a valid address, so we need to resolve it...
192 m_process->GetTarget().GetImages().ResolveFileAddress (symbol->GetValue().GetFileAddress(), m_kernel.so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000193 DataExtractor data; // Load command data
Greg Clayton0d9fc762011-07-08 03:21:57 +0000194 if (ReadMachHeader (m_kernel, &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000195 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000196 if (m_kernel.header.filetype == llvm::MachO::HeaderFileTypeExecutable)
Greg Clayton7b242382011-07-08 00:48:09 +0000197 {
198 if (ParseLoadCommands (data, m_kernel))
199 UpdateImageLoadAddress (m_kernel);
200
201 // Update all image infos
Greg Clayton374972e2011-07-09 17:15:55 +0000202 ReadAllKextSummaries ();
Greg Clayton7b242382011-07-08 00:48:09 +0000203 }
204 }
205 else
206 {
207 m_kernel.Clear(false);
208 }
Greg Clayton7b242382011-07-08 00:48:09 +0000209 }
210 }
211 }
Greg Clayton7b242382011-07-08 00:48:09 +0000212}
213
214bool
Greg Clayton944b8282011-08-22 22:30:57 +0000215DynamicLoaderDarwinKernel::FindTargetModule (OSKextLoadedKextSummary &image_info, bool can_create, bool *did_create_ptr)
Greg Clayton7b242382011-07-08 00:48:09 +0000216{
217 if (did_create_ptr)
218 *did_create_ptr = false;
219
220 const bool image_info_uuid_is_valid = image_info.uuid.IsValid();
221
222 if (image_info.module_sp)
223 {
224 if (image_info_uuid_is_valid)
225 {
226 if (image_info.module_sp->GetUUID() == image_info.uuid)
227 return true;
228 else
229 image_info.module_sp.reset();
230 }
231 else
232 return true;
233 }
234
235 ModuleList &target_images = m_process->GetTarget().GetImages();
236 if (image_info_uuid_is_valid)
237 image_info.module_sp = target_images.FindModule(image_info.uuid);
238
239 if (image_info.module_sp)
240 return true;
241
242 ArchSpec arch (image_info.GetArchitecture ());
243 if (can_create)
244 {
245 if (image_info_uuid_is_valid)
246 {
247 image_info.module_sp = m_process->GetTarget().GetSharedModule (FileSpec(),
Greg Claytona63d08c2011-07-19 03:57:15 +0000248 arch,
249 &image_info.uuid);
Greg Clayton7b242382011-07-08 00:48:09 +0000250 if (did_create_ptr)
251 *did_create_ptr = image_info.module_sp;
252 }
253 }
254 return image_info.module_sp;
255}
256
257bool
Greg Clayton944b8282011-08-22 22:30:57 +0000258DynamicLoaderDarwinKernel::UpdateCommPageLoadAddress(Module *module)
Greg Clayton7b242382011-07-08 00:48:09 +0000259{
260 bool changed = false;
261 if (module)
262 {
263 ObjectFile *image_object_file = module->GetObjectFile();
264 if (image_object_file)
265 {
266 SectionList *section_list = image_object_file->GetSectionList ();
267 if (section_list)
268 {
269 uint32_t num_sections = section_list->GetSize();
270 for (uint32_t i=0; i<num_sections; ++i)
271 {
272 Section* section = section_list->GetSectionAtIndex (i).get();
273 if (section)
274 {
275 const addr_t new_section_load_addr = section->GetFileAddress ();
276 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section);
277 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
278 old_section_load_addr != new_section_load_addr)
279 {
280 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress ()))
281 changed = true;
282 }
283 }
284 }
285 }
286 }
287 }
288 return changed;
289}
290
291//----------------------------------------------------------------------
292// Update the load addresses for all segments in MODULE using the
293// updated INFO that is passed in.
294//----------------------------------------------------------------------
295bool
Greg Clayton944b8282011-08-22 22:30:57 +0000296DynamicLoaderDarwinKernel::UpdateImageLoadAddress (OSKextLoadedKextSummary& info)
Greg Clayton7b242382011-07-08 00:48:09 +0000297{
298 Module *module = info.module_sp.get();
299 bool changed = false;
300 if (module)
301 {
302 ObjectFile *image_object_file = module->GetObjectFile();
303 if (image_object_file)
304 {
305 SectionList *section_list = image_object_file->GetSectionList ();
306 if (section_list)
307 {
308 // We now know the slide amount, so go through all sections
309 // and update the load addresses with the correct values.
310 uint32_t num_segments = info.segments.size();
311 for (uint32_t i=0; i<num_segments; ++i)
312 {
Greg Clayton7b242382011-07-08 00:48:09 +0000313 const addr_t new_section_load_addr = info.segments[i].vmaddr;
Greg Claytona63d08c2011-07-19 03:57:15 +0000314 if (section_list->FindSectionByName(info.segments[i].name))
Greg Clayton7b242382011-07-08 00:48:09 +0000315 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000316 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
317 if (section_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000318 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000319 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section_sp.get());
320 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
321 old_section_load_addr != new_section_load_addr)
322 {
323 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), new_section_load_addr))
324 changed = true;
325 }
326 }
327 else
328 {
329 fprintf (stderr,
330 "warning: unable to find and load segment named '%s' at 0x%llx in '%s/%s' in macosx dynamic loader plug-in.\n",
331 info.segments[i].name.AsCString("<invalid>"),
332 (uint64_t)new_section_load_addr,
333 image_object_file->GetFileSpec().GetDirectory().AsCString(),
334 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000335 }
336 }
337 else
338 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000339 // The segment name is empty which means this is a .o file.
340 // Object files in LLDB end up getting reorganized so that
341 // the segment name that is in the section is promoted into
342 // an actual segment, so we just need to go through all sections
343 // and slide them by a single amount.
344
345 uint32_t num_sections = section_list->GetSize();
346 for (uint32_t i=0; i<num_sections; ++i)
347 {
348 Section* section = section_list->GetSectionAtIndex (i).get();
349 if (section)
350 {
351 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + new_section_load_addr))
352 changed = true;
353 }
354 }
Greg Clayton7b242382011-07-08 00:48:09 +0000355 }
356 }
357 }
358 }
359 }
360 return changed;
361}
362
363//----------------------------------------------------------------------
364// Update the load addresses for all segments in MODULE using the
365// updated INFO that is passed in.
366//----------------------------------------------------------------------
367bool
Greg Clayton944b8282011-08-22 22:30:57 +0000368DynamicLoaderDarwinKernel::UnloadImageLoadAddress (OSKextLoadedKextSummary& info)
Greg Clayton7b242382011-07-08 00:48:09 +0000369{
370 Module *module = info.module_sp.get();
371 bool changed = false;
372 if (module)
373 {
374 ObjectFile *image_object_file = module->GetObjectFile();
375 if (image_object_file)
376 {
377 SectionList *section_list = image_object_file->GetSectionList ();
378 if (section_list)
379 {
380 uint32_t num_segments = info.segments.size();
381 for (uint32_t i=0; i<num_segments; ++i)
382 {
383 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
384 if (section_sp)
385 {
386 const addr_t old_section_load_addr = info.segments[i].vmaddr;
387 if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr))
388 changed = true;
389 }
390 else
391 {
392 fprintf (stderr,
393 "warning: unable to find and unload segment named '%s' in '%s/%s' in macosx dynamic loader plug-in.\n",
394 info.segments[i].name.AsCString("<invalid>"),
395 image_object_file->GetFileSpec().GetDirectory().AsCString(),
396 image_object_file->GetFileSpec().GetFilename().AsCString());
397 }
398 }
399 }
400 }
401 }
402 return changed;
403}
404
405
406//----------------------------------------------------------------------
407// Static callback function that gets called when our DYLD notification
408// breakpoint gets hit. We update all of our image infos and then
409// let our super class DynamicLoader class decide if we should stop
410// or not (based on global preference).
411//----------------------------------------------------------------------
412bool
Greg Clayton944b8282011-08-22 22:30:57 +0000413DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000414 StoppointCallbackContext *context,
415 user_id_t break_id,
416 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000417{
Greg Clayton944b8282011-08-22 22:30:57 +0000418 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000419}
420
421bool
Greg Clayton944b8282011-08-22 22:30:57 +0000422DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000423 user_id_t break_id,
424 user_id_t break_loc_id)
425{
Greg Claytond16e1e52011-07-12 17:06:17 +0000426 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
427 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000428 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000429
Greg Clayton374972e2011-07-09 17:15:55 +0000430 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000431
432 if (log)
433 PutToLog(log.get());
434
Greg Clayton374972e2011-07-09 17:15:55 +0000435 return GetStopWhenImagesChange();
436}
437
438
439bool
Greg Clayton944b8282011-08-22 22:30:57 +0000440DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000441{
442 Mutex::Locker locker(m_mutex);
443
444 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000445
446 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000447 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000448 {
449 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
450 const ByteOrder byte_order = m_kernel.GetByteOrder();
451 Error error;
452 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
453 // which is currenty 4 uint32_t and a pointer.
454 uint8_t buf[24];
455 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
456 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000457 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000458 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
459 prefer_file_cache,
460 error,
461 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000462 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000463 // We got a valid address for our kext summary header and make sure it isn't NULL
464 if (m_kext_summary_header_addr.IsValid() &&
465 m_kext_summary_header_addr.GetFileAddress() != 0)
466 {
467 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
468 if (bytes_read == count)
469 {
470 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000471 m_kext_summary_header.version = data.GetU32(&offset);
472 if (m_kext_summary_header.version >= 2)
473 {
474 m_kext_summary_header.entry_size = data.GetU32(&offset);
475 }
476 else
477 {
478 // Versions less than 2 didn't have an entry size, it was hard coded
479 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
480 }
481 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000482 return true;
483 }
484 }
Greg Clayton7b242382011-07-08 00:48:09 +0000485 }
486 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000487 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000488 return false;
489}
490
491
492bool
Greg Clayton944b8282011-08-22 22:30:57 +0000493DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000494 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000495{
496 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000497 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000498 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000499 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000500
501 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000502
503 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
504 return false;
505
Greg Clayton07e66e32011-07-20 03:41:06 +0000506 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000507 for (uint32_t i = 0; i < count; i++)
508 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000509 if (s)
510 {
511 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
512 if (u)
513 {
514 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",
515 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
516 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
517 kext_summaries[i].address, kext_summaries[i].name);
518 }
519 else
520 {
521 s->Printf("0x%16.16llx \"%s\"...\n", kext_summaries[i].address, kext_summaries[i].name);
522 }
523 }
524
Greg Claytona63d08c2011-07-19 03:57:15 +0000525 DataExtractor data; // Load command data
526 if (ReadMachHeader (kext_summaries[i], &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000527 {
Greg Clayton7b242382011-07-08 00:48:09 +0000528 ParseLoadCommands (data, kext_summaries[i]);
529 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000530
Greg Clayton5b882162011-07-21 01:12:01 +0000531 if (s)
532 {
533 if (kext_summaries[i].module_sp)
534 s->Printf(" found kext: %s/%s\n",
535 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
536 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
537 }
538
Greg Claytona63d08c2011-07-19 03:57:15 +0000539 if (log)
540 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000541 }
542 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000543 return return_value;
544}
545
546// Adds the modules in image_infos to m_kext_summaries.
547// NB don't call this passing in m_kext_summaries.
548
549bool
Greg Clayton944b8282011-08-22 22:30:57 +0000550DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000551{
552 // Now add these images to the main list.
553 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000554
555 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
556 {
Greg Clayton7b242382011-07-08 00:48:09 +0000557 m_kext_summaries.push_back(image_infos[idx]);
558
559 if (FindTargetModule (image_infos[idx], true, NULL))
560 {
561 // UpdateImageLoadAddress will return true if any segments
562 // change load address. We need to check this so we don't
563 // mention that all loaded shared libraries are newly loaded
564 // each time we hit out dyld breakpoint since dyld will list all
565 // shared libraries each time.
566 if (UpdateImageLoadAddress (image_infos[idx]))
567 {
568 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
569 }
570 }
571 }
572
573 if (loaded_module_list.GetSize() > 0)
574 {
575 // FIXME: This should really be in the Runtime handlers class, which should get
576 // called by the target's ModulesDidLoad, but we're doing it all locally for now
577 // to save time.
578 // Also, I'm assuming there can be only one libobjc dylib loaded...
579
580 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
581 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
582 {
583 size_t num_modules = loaded_module_list.GetSize();
584 for (int i = 0; i < num_modules; i++)
585 {
586 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
587 {
588 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
589 break;
590 }
591 }
592 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000593// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000594// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000595 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
596 }
597 return true;
598}
599
Greg Clayton7b242382011-07-08 00:48:09 +0000600
601uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000602DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000603 uint32_t image_infos_count,
604 OSKextLoadedKextSummary::collection &image_infos)
605{
606 const ByteOrder endian = m_kernel.GetByteOrder();
607 const uint32_t addr_size = m_kernel.GetAddressByteSize();
608
609 image_infos.resize(image_infos_count);
610 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
611 DataBufferHeap data(count, 0);
612 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000613
614 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
615
616 if (s)
617 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000618 const bool prefer_file_cache = false;
619 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
620 prefer_file_cache,
621 data.GetBytes(),
622 data.GetByteSize(),
623 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000624 if (bytes_read == count)
625 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000626
Greg Clayton7b242382011-07-08 00:48:09 +0000627 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
628 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000629 for (uint32_t kext_summary_offset = 0;
630 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
631 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000632 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000633 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000634 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
635 if (name_data == NULL)
636 break;
637 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
638 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
639 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000640 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
641 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000642 image_infos[i].size = extractor.GetU64(&offset);
643 image_infos[i].version = extractor.GetU64(&offset);
644 image_infos[i].load_tag = extractor.GetU32(&offset);
645 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000646 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
647 {
648 image_infos[i].reference_list = extractor.GetU64(&offset);
649 }
650 else
651 {
652 image_infos[i].reference_list = 0;
653 }
Greg Clayton7b242382011-07-08 00:48:09 +0000654 }
655 if (i < image_infos.size())
656 image_infos.resize(i);
657 }
658 else
659 {
660 image_infos.clear();
661 }
662 return image_infos.size();
663}
664
665bool
Greg Clayton944b8282011-08-22 22:30:57 +0000666DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000667{
Greg Clayton374972e2011-07-09 17:15:55 +0000668 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000669
670 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000671
Greg Clayton7b242382011-07-08 00:48:09 +0000672 if (ReadKextSummaryHeader ())
673 {
Greg Clayton374972e2011-07-09 17:15:55 +0000674 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000675 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000676 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000677 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000678 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000679 {
Greg Clayton7b242382011-07-08 00:48:09 +0000680 m_kext_summaries.clear();
681 }
682 return true;
683 }
684 }
685 return false;
686}
687
688//----------------------------------------------------------------------
689// Read a mach_header at ADDR into HEADER, and also fill in the load
690// command data into LOAD_COMMAND_DATA if it is non-NULL.
691//
692// Returns true if we succeed, false if we fail for any reason.
693//----------------------------------------------------------------------
694bool
Greg Clayton944b8282011-08-22 22:30:57 +0000695DynamicLoaderDarwinKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data)
Greg Clayton7b242382011-07-08 00:48:09 +0000696{
697 DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
698 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000699 const bool prefer_file_cache = false;
700 size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address,
701 prefer_file_cache,
702 header_bytes.GetBytes(),
703 header_bytes.GetByteSize(),
704 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000705 if (bytes_read == sizeof(llvm::MachO::mach_header))
706 {
707 uint32_t offset = 0;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000708 ::memset (&kext_summary.header, 0, sizeof(kext_summary.header));
Greg Clayton7b242382011-07-08 00:48:09 +0000709
710 // Get the magic byte unswapped so we can figure out what we are dealing with
Greg Clayton374972e2011-07-09 17:15:55 +0000711 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000712 kext_summary.header.magic = data.GetU32(&offset);
713 Address load_cmd_addr = kext_summary.so_address;
Greg Clayton944b8282011-08-22 22:30:57 +0000714 data.SetByteOrder(DynamicLoaderDarwinKernel::GetByteOrderFromMagic(kext_summary.header.magic));
Greg Clayton0d9fc762011-07-08 03:21:57 +0000715 switch (kext_summary.header.magic)
Greg Clayton7b242382011-07-08 00:48:09 +0000716 {
717 case llvm::MachO::HeaderMagic32:
718 case llvm::MachO::HeaderMagic32Swapped:
719 data.SetAddressByteSize(4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000720 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header));
Greg Clayton7b242382011-07-08 00:48:09 +0000721 break;
722
723 case llvm::MachO::HeaderMagic64:
724 case llvm::MachO::HeaderMagic64Swapped:
725 data.SetAddressByteSize(8);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000726 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64));
Greg Clayton7b242382011-07-08 00:48:09 +0000727 break;
728
729 default:
730 return false;
731 }
732
733 // Read the rest of dyld's mach header
Greg Clayton0d9fc762011-07-08 03:21:57 +0000734 if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1))
Greg Clayton7b242382011-07-08 00:48:09 +0000735 {
736 if (load_command_data == NULL)
737 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
738
Greg Clayton0d9fc762011-07-08 03:21:57 +0000739 DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0));
Greg Clayton7b242382011-07-08 00:48:09 +0000740
Greg Clayton0d9fc762011-07-08 03:21:57 +0000741 size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr,
742 prefer_file_cache,
743 load_cmd_data_sp->GetBytes(),
744 load_cmd_data_sp->GetByteSize(),
745 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000746
Greg Clayton0d9fc762011-07-08 03:21:57 +0000747 if (load_cmd_bytes_read == kext_summary.header.sizeofcmds)
Greg Clayton7b242382011-07-08 00:48:09 +0000748 {
749 // Set the load command data and also set the correct endian
750 // swap settings and the correct address size
Greg Clayton0d9fc762011-07-08 03:21:57 +0000751 load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds);
Greg Clayton7b242382011-07-08 00:48:09 +0000752 load_command_data->SetByteOrder(data.GetByteOrder());
753 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
754 return true; // We successfully read the mach_header and the load command data
755 }
756
757 return false; // We weren't able to read the load command data
758 }
759 }
760 return false; // We failed the read the mach_header
761}
762
763
764//----------------------------------------------------------------------
765// Parse the load commands for an image
766//----------------------------------------------------------------------
767uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000768DynamicLoaderDarwinKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info)
Greg Clayton7b242382011-07-08 00:48:09 +0000769{
770 uint32_t offset = 0;
771 uint32_t cmd_idx;
772 Segment segment;
Greg Claytondf0b7d52011-07-08 04:11:42 +0000773 image_info.Clear (true);
Greg Clayton7b242382011-07-08 00:48:09 +0000774
Greg Claytondf0b7d52011-07-08 04:11:42 +0000775 for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++)
Greg Clayton7b242382011-07-08 00:48:09 +0000776 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000777 // Clear out any load command specific data from image_info since
Greg Clayton7b242382011-07-08 00:48:09 +0000778 // we are about to read it.
779
780 if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command)))
781 {
782 llvm::MachO::load_command load_cmd;
783 uint32_t load_cmd_offset = offset;
784 load_cmd.cmd = data.GetU32 (&offset);
785 load_cmd.cmdsize = data.GetU32 (&offset);
786 switch (load_cmd.cmd)
787 {
788 case llvm::MachO::LoadCommandSegment32:
789 {
790 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
791 // We are putting 4 uint32_t values 4 uint64_t values so
792 // we have to use multiple 32 bit gets below.
793 segment.vmaddr = data.GetU32 (&offset);
794 segment.vmsize = data.GetU32 (&offset);
795 segment.fileoff = data.GetU32 (&offset);
796 segment.filesize = data.GetU32 (&offset);
797 // Extract maxprot, initprot, nsects and flags all at once
798 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000799 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000800 }
801 break;
802
803 case llvm::MachO::LoadCommandSegment64:
804 {
805 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
806 // Extract vmaddr, vmsize, fileoff, and filesize all at once
807 data.GetU64(&offset, &segment.vmaddr, 4);
808 // Extract maxprot, initprot, nsects and flags all at once
809 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000810 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000811 }
812 break;
813
814 case llvm::MachO::LoadCommandUUID:
Greg Claytondf0b7d52011-07-08 04:11:42 +0000815 image_info.uuid.SetBytes(data.GetData (&offset, 16));
Greg Clayton7b242382011-07-08 00:48:09 +0000816 break;
817
818 default:
819 break;
820 }
821 // Set offset to be the beginning of the next load command.
822 offset = load_cmd_offset + load_cmd.cmdsize;
823 }
824 }
825#if 0
826 // No slide in the kernel...
827
828 // All sections listed in the dyld image info structure will all
829 // either be fixed up already, or they will all be off by a single
830 // slide amount that is determined by finding the first segment
831 // that is at file offset zero which also has bytes (a file size
832 // that is greater than zero) in the object file.
833
834 // Determine the slide amount (if any)
Greg Claytondf0b7d52011-07-08 04:11:42 +0000835 const size_t num_sections = image_info.segments.size();
Greg Clayton7b242382011-07-08 00:48:09 +0000836 for (size_t i = 0; i < num_sections; ++i)
837 {
838 // Iterate through the object file sections to find the
839 // first section that starts of file offset zero and that
840 // has bytes in the file...
Greg Claytondf0b7d52011-07-08 04:11:42 +0000841 if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0)
Greg Clayton7b242382011-07-08 00:48:09 +0000842 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000843 image_info.slide = image_info.address - image_info.segments[i].vmaddr;
Greg Clayton7b242382011-07-08 00:48:09 +0000844 // We have found the slide amount, so we can exit
845 // this for loop.
846 break;
847 }
848 }
849#endif
Greg Claytondf0b7d52011-07-08 04:11:42 +0000850 if (image_info.uuid.IsValid())
851 {
852 bool did_create = false;
853 if (FindTargetModule(image_info, true, &did_create))
854 {
855 if (did_create)
856 image_info.module_create_stop_id = m_process->GetStopID();
857 }
858 }
Greg Clayton7b242382011-07-08 00:48:09 +0000859 return cmd_idx;
860}
861
862//----------------------------------------------------------------------
863// Dump a Segment to the file handle provided.
864//----------------------------------------------------------------------
865void
Greg Clayton944b8282011-08-22 22:30:57 +0000866DynamicLoaderDarwinKernel::Segment::PutToLog (Log *log, addr_t slide) const
Greg Clayton7b242382011-07-08 00:48:09 +0000867{
868 if (log)
869 {
870 if (slide == 0)
871 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)",
872 name.AsCString(""),
873 vmaddr + slide,
874 vmaddr + slide + vmsize);
875 else
876 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx",
877 name.AsCString(""),
878 vmaddr + slide,
879 vmaddr + slide + vmsize,
880 slide);
881 }
882}
883
Greg Clayton944b8282011-08-22 22:30:57 +0000884const DynamicLoaderDarwinKernel::Segment *
885DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const
Greg Clayton7b242382011-07-08 00:48:09 +0000886{
887 const size_t num_segments = segments.size();
888 for (size_t i=0; i<num_segments; ++i)
889 {
890 if (segments[i].name == name)
891 return &segments[i];
892 }
893 return NULL;
894}
895
896
897//----------------------------------------------------------------------
898// Dump an image info structure to the file handle provided.
899//----------------------------------------------------------------------
900void
Greg Clayton944b8282011-08-22 22:30:57 +0000901DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000902{
903 if (log == NULL)
904 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000905 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000906
907 if (address == LLDB_INVALID_ADDRESS)
908 {
909 if (u)
910 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000911 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 +0000912 u[ 0], u[ 1], u[ 2], u[ 3],
913 u[ 4], u[ 5], u[ 6], u[ 7],
914 u[ 8], u[ 9], u[10], u[11],
915 u[12], u[13], u[14], u[15],
916 name);
917 }
918 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000919 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000920 }
921 else
922 {
923 if (u)
924 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000925 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\"",
926 address, size, version, load_tag, flags, reference_list,
927 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
928 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000929 name);
930 }
931 else
932 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000933 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\"",
934 address, address+size, version, load_tag, flags, reference_list,
935 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000936 }
937 for (uint32_t i=0; i<segments.size(); ++i)
938 segments[i].PutToLog(log, 0);
939 }
940}
941
942//----------------------------------------------------------------------
943// Dump the _dyld_all_image_infos members and all current image infos
944// that we have parsed to the file handle provided.
945//----------------------------------------------------------------------
946void
Greg Clayton944b8282011-08-22 22:30:57 +0000947DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000948{
949 if (log == NULL)
950 return;
951
952 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000953 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000954 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000955 m_kext_summary_header.version,
956 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000957 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000958
959 size_t i;
960 const size_t count = m_kext_summaries.size();
961 if (count > 0)
962 {
963 log->PutCString("Loaded:");
964 for (i = 0; i<count; i++)
965 m_kext_summaries[i].PutToLog(log);
966 }
967}
968
969void
Greg Clayton944b8282011-08-22 22:30:57 +0000970DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000971{
Greg Clayton944b8282011-08-22 22:30:57 +0000972 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000973 Clear(true);
974 m_process = process;
975 m_process->GetTarget().GetSectionLoadList().Clear();
976}
977
Greg Clayton374972e2011-07-09 17:15:55 +0000978void
Greg Clayton944b8282011-08-22 22:30:57 +0000979DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000980{
Greg Clayton374972e2011-07-09 17:15:55 +0000981 if (m_break_id == LLDB_INVALID_BREAK_ID)
982 {
Greg Clayton944b8282011-08-22 22:30:57 +0000983 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000984
Greg Claytond16e1e52011-07-12 17:06:17 +0000985
Greg Clayton374972e2011-07-09 17:15:55 +0000986 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000987 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000988 FileSpecList module_spec_list;
989 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
990 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Greg Clayton374972e2011-07-09 17:15:55 +0000991 "OSKextLoadedKextSummariesUpdated",
992 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000993 internal_bp,
994 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000995
Greg Clayton944b8282011-08-22 22:30:57 +0000996 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000997 m_break_id = bp->GetID();
998 }
Greg Clayton7b242382011-07-08 00:48:09 +0000999}
1000
1001//----------------------------------------------------------------------
1002// Member function that gets called when the process state changes.
1003//----------------------------------------------------------------------
1004void
Greg Clayton944b8282011-08-22 22:30:57 +00001005DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001006{
Greg Clayton944b8282011-08-22 22:30:57 +00001007 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001008 switch (state)
1009 {
1010 case eStateConnected:
1011 case eStateAttaching:
1012 case eStateLaunching:
1013 case eStateInvalid:
1014 case eStateUnloaded:
1015 case eStateExited:
1016 case eStateDetached:
1017 Clear(false);
1018 break;
1019
1020 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001021 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001022 break;
1023
1024 case eStateRunning:
1025 case eStateStepping:
1026 case eStateCrashed:
1027 case eStateSuspended:
1028 break;
1029
1030 default:
1031 break;
1032 }
1033}
1034
1035ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001036DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001037{
1038 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001039 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1040 if (log)
1041 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001042 return thread_plan_sp;
1043}
1044
1045Error
Greg Clayton944b8282011-08-22 22:30:57 +00001046DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001047{
1048 Error error;
1049 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1050 return error;
1051}
1052
1053void
Greg Clayton944b8282011-08-22 22:30:57 +00001054DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001055{
1056 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1057 GetPluginDescriptionStatic(),
1058 CreateInstance);
1059}
1060
1061void
Greg Clayton944b8282011-08-22 22:30:57 +00001062DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001063{
1064 PluginManager::UnregisterPlugin (CreateInstance);
1065}
1066
1067
1068const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001069DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001070{
1071 return "dynamic-loader.macosx-kernel";
1072}
1073
1074const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001075DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001076{
1077 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1078}
1079
1080
1081//------------------------------------------------------------------
1082// PluginInterface protocol
1083//------------------------------------------------------------------
1084const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001085DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001086{
Greg Clayton944b8282011-08-22 22:30:57 +00001087 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001088}
1089
1090const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001091DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001092{
1093 return GetPluginNameStatic();
1094}
1095
1096uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001097DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001098{
1099 return 1;
1100}
1101