blob: 8225d3decf09910dc9de0ba76ce51f42e5d177ed [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_kext_summaries(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000098 m_mutex(Mutex::eMutexTypeRecursive),
99 m_break_id (LLDB_INVALID_BREAK_ID)
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 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000329 Host::SystemLog (Host::eSystemLogWarning, "warning: unable to find and load segment named '%s' at 0x%llx in '%s/%s' in macosx dynamic loader plug-in.\n",
330 info.segments[i].name.AsCString("<invalid>"),
331 (uint64_t)new_section_load_addr,
332 image_object_file->GetFileSpec().GetDirectory().AsCString(),
333 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000334 }
335 }
336 else
337 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000338 // The segment name is empty which means this is a .o file.
339 // Object files in LLDB end up getting reorganized so that
340 // the segment name that is in the section is promoted into
341 // an actual segment, so we just need to go through all sections
342 // and slide them by a single amount.
343
344 uint32_t num_sections = section_list->GetSize();
345 for (uint32_t i=0; i<num_sections; ++i)
346 {
347 Section* section = section_list->GetSectionAtIndex (i).get();
348 if (section)
349 {
350 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + new_section_load_addr))
351 changed = true;
352 }
353 }
Greg Clayton7b242382011-07-08 00:48:09 +0000354 }
355 }
356 }
357 }
358 }
359 return changed;
360}
361
362//----------------------------------------------------------------------
363// Update the load addresses for all segments in MODULE using the
364// updated INFO that is passed in.
365//----------------------------------------------------------------------
366bool
Greg Clayton944b8282011-08-22 22:30:57 +0000367DynamicLoaderDarwinKernel::UnloadImageLoadAddress (OSKextLoadedKextSummary& info)
Greg Clayton7b242382011-07-08 00:48:09 +0000368{
369 Module *module = info.module_sp.get();
370 bool changed = false;
371 if (module)
372 {
373 ObjectFile *image_object_file = module->GetObjectFile();
374 if (image_object_file)
375 {
376 SectionList *section_list = image_object_file->GetSectionList ();
377 if (section_list)
378 {
379 uint32_t num_segments = info.segments.size();
380 for (uint32_t i=0; i<num_segments; ++i)
381 {
382 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
383 if (section_sp)
384 {
385 const addr_t old_section_load_addr = info.segments[i].vmaddr;
386 if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr))
387 changed = true;
388 }
389 else
390 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000391 Host::SystemLog (Host::eSystemLogWarning,
392 "warning: unable to find and unload segment named '%s' in '%s/%s' in macosx dynamic loader plug-in.\n",
393 info.segments[i].name.AsCString("<invalid>"),
394 image_object_file->GetFileSpec().GetDirectory().AsCString(),
395 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000396 }
397 }
398 }
399 }
400 }
401 return changed;
402}
403
404
405//----------------------------------------------------------------------
406// Static callback function that gets called when our DYLD notification
407// breakpoint gets hit. We update all of our image infos and then
408// let our super class DynamicLoader class decide if we should stop
409// or not (based on global preference).
410//----------------------------------------------------------------------
411bool
Greg Clayton944b8282011-08-22 22:30:57 +0000412DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000413 StoppointCallbackContext *context,
414 user_id_t break_id,
415 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000416{
Greg Clayton944b8282011-08-22 22:30:57 +0000417 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000418}
419
420bool
Greg Clayton944b8282011-08-22 22:30:57 +0000421DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000422 user_id_t break_id,
423 user_id_t break_loc_id)
424{
Greg Claytond16e1e52011-07-12 17:06:17 +0000425 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
426 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000427 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000428
Greg Clayton374972e2011-07-09 17:15:55 +0000429 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000430
431 if (log)
432 PutToLog(log.get());
433
Greg Clayton374972e2011-07-09 17:15:55 +0000434 return GetStopWhenImagesChange();
435}
436
437
438bool
Greg Clayton944b8282011-08-22 22:30:57 +0000439DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000440{
441 Mutex::Locker locker(m_mutex);
442
443 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000444
445 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000446 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000447 {
448 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
449 const ByteOrder byte_order = m_kernel.GetByteOrder();
450 Error error;
451 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
452 // which is currenty 4 uint32_t and a pointer.
453 uint8_t buf[24];
454 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
455 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000456 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000457 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
458 prefer_file_cache,
459 error,
460 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000461 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000462 // We got a valid address for our kext summary header and make sure it isn't NULL
463 if (m_kext_summary_header_addr.IsValid() &&
464 m_kext_summary_header_addr.GetFileAddress() != 0)
465 {
466 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
467 if (bytes_read == count)
468 {
469 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000470 m_kext_summary_header.version = data.GetU32(&offset);
471 if (m_kext_summary_header.version >= 2)
472 {
473 m_kext_summary_header.entry_size = data.GetU32(&offset);
474 }
475 else
476 {
477 // Versions less than 2 didn't have an entry size, it was hard coded
478 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
479 }
480 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000481 return true;
482 }
483 }
Greg Clayton7b242382011-07-08 00:48:09 +0000484 }
485 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000486 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000487 return false;
488}
489
490
491bool
Greg Clayton944b8282011-08-22 22:30:57 +0000492DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000493 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000494{
495 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000496 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000497 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000498 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000499
500 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000501
502 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
503 return false;
504
Greg Clayton07e66e32011-07-20 03:41:06 +0000505 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000506 for (uint32_t i = 0; i < count; i++)
507 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000508 if (s)
509 {
510 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
511 if (u)
512 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000513 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\"...",
Greg Clayton07e66e32011-07-20 03:41:06 +0000514 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
515 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
516 kext_summaries[i].address, kext_summaries[i].name);
517 }
518 else
519 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000520 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000521 }
522 }
523
Greg Claytona63d08c2011-07-19 03:57:15 +0000524 DataExtractor data; // Load command data
525 if (ReadMachHeader (kext_summaries[i], &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000526 {
Greg Clayton7b242382011-07-08 00:48:09 +0000527 ParseLoadCommands (data, kext_summaries[i]);
528 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000529
Greg Clayton5b882162011-07-21 01:12:01 +0000530 if (s)
531 {
532 if (kext_summaries[i].module_sp)
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000533 s->Printf("\n found kext: %s/%s\n",
Greg Clayton5b882162011-07-21 01:12:01 +0000534 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
535 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000536 else
537 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000538 }
539
Greg Claytona63d08c2011-07-19 03:57:15 +0000540 if (log)
541 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000542 }
543 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000544 return return_value;
545}
546
547// Adds the modules in image_infos to m_kext_summaries.
548// NB don't call this passing in m_kext_summaries.
549
550bool
Greg Clayton944b8282011-08-22 22:30:57 +0000551DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000552{
553 // Now add these images to the main list.
554 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000555
556 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
557 {
Greg Clayton7b242382011-07-08 00:48:09 +0000558 m_kext_summaries.push_back(image_infos[idx]);
559
560 if (FindTargetModule (image_infos[idx], true, NULL))
561 {
562 // UpdateImageLoadAddress will return true if any segments
563 // change load address. We need to check this so we don't
564 // mention that all loaded shared libraries are newly loaded
565 // each time we hit out dyld breakpoint since dyld will list all
566 // shared libraries each time.
567 if (UpdateImageLoadAddress (image_infos[idx]))
568 {
569 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
570 }
571 }
572 }
573
574 if (loaded_module_list.GetSize() > 0)
575 {
576 // FIXME: This should really be in the Runtime handlers class, which should get
577 // called by the target's ModulesDidLoad, but we're doing it all locally for now
578 // to save time.
579 // Also, I'm assuming there can be only one libobjc dylib loaded...
580
581 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
582 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
583 {
584 size_t num_modules = loaded_module_list.GetSize();
585 for (int i = 0; i < num_modules; i++)
586 {
587 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
588 {
589 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
590 break;
591 }
592 }
593 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000594// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000595// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000596 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
597 }
598 return true;
599}
600
Greg Clayton7b242382011-07-08 00:48:09 +0000601
602uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000603DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000604 uint32_t image_infos_count,
605 OSKextLoadedKextSummary::collection &image_infos)
606{
607 const ByteOrder endian = m_kernel.GetByteOrder();
608 const uint32_t addr_size = m_kernel.GetAddressByteSize();
609
610 image_infos.resize(image_infos_count);
611 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
612 DataBufferHeap data(count, 0);
613 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000614
615 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
616
617 if (s)
618 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000619 const bool prefer_file_cache = false;
620 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
621 prefer_file_cache,
622 data.GetBytes(),
623 data.GetByteSize(),
624 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000625 if (bytes_read == count)
626 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000627
Greg Clayton7b242382011-07-08 00:48:09 +0000628 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
629 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000630 for (uint32_t kext_summary_offset = 0;
631 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
632 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000633 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000634 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000635 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
636 if (name_data == NULL)
637 break;
638 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
639 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
640 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000641 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
642 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000643 image_infos[i].size = extractor.GetU64(&offset);
644 image_infos[i].version = extractor.GetU64(&offset);
645 image_infos[i].load_tag = extractor.GetU32(&offset);
646 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000647 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
648 {
649 image_infos[i].reference_list = extractor.GetU64(&offset);
650 }
651 else
652 {
653 image_infos[i].reference_list = 0;
654 }
Greg Clayton7b242382011-07-08 00:48:09 +0000655 }
656 if (i < image_infos.size())
657 image_infos.resize(i);
658 }
659 else
660 {
661 image_infos.clear();
662 }
663 return image_infos.size();
664}
665
666bool
Greg Clayton944b8282011-08-22 22:30:57 +0000667DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000668{
Greg Clayton374972e2011-07-09 17:15:55 +0000669 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000670
671 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000672
Greg Clayton7b242382011-07-08 00:48:09 +0000673 if (ReadKextSummaryHeader ())
674 {
Greg Clayton374972e2011-07-09 17:15:55 +0000675 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000676 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000677 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000678 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000679 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000680 {
Greg Clayton7b242382011-07-08 00:48:09 +0000681 m_kext_summaries.clear();
682 }
683 return true;
684 }
685 }
686 return false;
687}
688
689//----------------------------------------------------------------------
690// Read a mach_header at ADDR into HEADER, and also fill in the load
691// command data into LOAD_COMMAND_DATA if it is non-NULL.
692//
693// Returns true if we succeed, false if we fail for any reason.
694//----------------------------------------------------------------------
695bool
Greg Clayton944b8282011-08-22 22:30:57 +0000696DynamicLoaderDarwinKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data)
Greg Clayton7b242382011-07-08 00:48:09 +0000697{
698 DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
699 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000700 const bool prefer_file_cache = false;
701 size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address,
702 prefer_file_cache,
703 header_bytes.GetBytes(),
704 header_bytes.GetByteSize(),
705 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000706 if (bytes_read == sizeof(llvm::MachO::mach_header))
707 {
708 uint32_t offset = 0;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000709 ::memset (&kext_summary.header, 0, sizeof(kext_summary.header));
Greg Clayton7b242382011-07-08 00:48:09 +0000710
711 // Get the magic byte unswapped so we can figure out what we are dealing with
Greg Clayton374972e2011-07-09 17:15:55 +0000712 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000713 kext_summary.header.magic = data.GetU32(&offset);
714 Address load_cmd_addr = kext_summary.so_address;
Greg Clayton944b8282011-08-22 22:30:57 +0000715 data.SetByteOrder(DynamicLoaderDarwinKernel::GetByteOrderFromMagic(kext_summary.header.magic));
Greg Clayton0d9fc762011-07-08 03:21:57 +0000716 switch (kext_summary.header.magic)
Greg Clayton7b242382011-07-08 00:48:09 +0000717 {
718 case llvm::MachO::HeaderMagic32:
719 case llvm::MachO::HeaderMagic32Swapped:
720 data.SetAddressByteSize(4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000721 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header));
Greg Clayton7b242382011-07-08 00:48:09 +0000722 break;
723
724 case llvm::MachO::HeaderMagic64:
725 case llvm::MachO::HeaderMagic64Swapped:
726 data.SetAddressByteSize(8);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000727 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64));
Greg Clayton7b242382011-07-08 00:48:09 +0000728 break;
729
730 default:
731 return false;
732 }
733
734 // Read the rest of dyld's mach header
Greg Clayton0d9fc762011-07-08 03:21:57 +0000735 if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1))
Greg Clayton7b242382011-07-08 00:48:09 +0000736 {
737 if (load_command_data == NULL)
738 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
739
Greg Clayton0d9fc762011-07-08 03:21:57 +0000740 DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0));
Greg Clayton7b242382011-07-08 00:48:09 +0000741
Greg Clayton0d9fc762011-07-08 03:21:57 +0000742 size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr,
743 prefer_file_cache,
744 load_cmd_data_sp->GetBytes(),
745 load_cmd_data_sp->GetByteSize(),
746 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000747
Greg Clayton0d9fc762011-07-08 03:21:57 +0000748 if (load_cmd_bytes_read == kext_summary.header.sizeofcmds)
Greg Clayton7b242382011-07-08 00:48:09 +0000749 {
750 // Set the load command data and also set the correct endian
751 // swap settings and the correct address size
Greg Clayton0d9fc762011-07-08 03:21:57 +0000752 load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds);
Greg Clayton7b242382011-07-08 00:48:09 +0000753 load_command_data->SetByteOrder(data.GetByteOrder());
754 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
755 return true; // We successfully read the mach_header and the load command data
756 }
757
758 return false; // We weren't able to read the load command data
759 }
760 }
761 return false; // We failed the read the mach_header
762}
763
764
765//----------------------------------------------------------------------
766// Parse the load commands for an image
767//----------------------------------------------------------------------
768uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000769DynamicLoaderDarwinKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info)
Greg Clayton7b242382011-07-08 00:48:09 +0000770{
771 uint32_t offset = 0;
772 uint32_t cmd_idx;
773 Segment segment;
Greg Claytondf0b7d52011-07-08 04:11:42 +0000774 image_info.Clear (true);
Greg Clayton7b242382011-07-08 00:48:09 +0000775
Greg Claytondf0b7d52011-07-08 04:11:42 +0000776 for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++)
Greg Clayton7b242382011-07-08 00:48:09 +0000777 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000778 // Clear out any load command specific data from image_info since
Greg Clayton7b242382011-07-08 00:48:09 +0000779 // we are about to read it.
780
781 if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command)))
782 {
783 llvm::MachO::load_command load_cmd;
784 uint32_t load_cmd_offset = offset;
785 load_cmd.cmd = data.GetU32 (&offset);
786 load_cmd.cmdsize = data.GetU32 (&offset);
787 switch (load_cmd.cmd)
788 {
789 case llvm::MachO::LoadCommandSegment32:
790 {
791 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
792 // We are putting 4 uint32_t values 4 uint64_t values so
793 // we have to use multiple 32 bit gets below.
794 segment.vmaddr = data.GetU32 (&offset);
795 segment.vmsize = data.GetU32 (&offset);
796 segment.fileoff = data.GetU32 (&offset);
797 segment.filesize = data.GetU32 (&offset);
798 // Extract maxprot, initprot, nsects and flags all at once
799 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000800 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000801 }
802 break;
803
804 case llvm::MachO::LoadCommandSegment64:
805 {
806 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
807 // Extract vmaddr, vmsize, fileoff, and filesize all at once
808 data.GetU64(&offset, &segment.vmaddr, 4);
809 // Extract maxprot, initprot, nsects and flags all at once
810 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000811 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000812 }
813 break;
814
815 case llvm::MachO::LoadCommandUUID:
Greg Claytondf0b7d52011-07-08 04:11:42 +0000816 image_info.uuid.SetBytes(data.GetData (&offset, 16));
Greg Clayton7b242382011-07-08 00:48:09 +0000817 break;
818
819 default:
820 break;
821 }
822 // Set offset to be the beginning of the next load command.
823 offset = load_cmd_offset + load_cmd.cmdsize;
824 }
825 }
826#if 0
827 // No slide in the kernel...
828
829 // All sections listed in the dyld image info structure will all
830 // either be fixed up already, or they will all be off by a single
831 // slide amount that is determined by finding the first segment
832 // that is at file offset zero which also has bytes (a file size
833 // that is greater than zero) in the object file.
834
835 // Determine the slide amount (if any)
Greg Claytondf0b7d52011-07-08 04:11:42 +0000836 const size_t num_sections = image_info.segments.size();
Greg Clayton7b242382011-07-08 00:48:09 +0000837 for (size_t i = 0; i < num_sections; ++i)
838 {
839 // Iterate through the object file sections to find the
840 // first section that starts of file offset zero and that
841 // has bytes in the file...
Greg Claytondf0b7d52011-07-08 04:11:42 +0000842 if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0)
Greg Clayton7b242382011-07-08 00:48:09 +0000843 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000844 image_info.slide = image_info.address - image_info.segments[i].vmaddr;
Greg Clayton7b242382011-07-08 00:48:09 +0000845 // We have found the slide amount, so we can exit
846 // this for loop.
847 break;
848 }
849 }
850#endif
Greg Claytondf0b7d52011-07-08 04:11:42 +0000851 if (image_info.uuid.IsValid())
852 {
853 bool did_create = false;
854 if (FindTargetModule(image_info, true, &did_create))
855 {
856 if (did_create)
857 image_info.module_create_stop_id = m_process->GetStopID();
858 }
859 }
Greg Clayton7b242382011-07-08 00:48:09 +0000860 return cmd_idx;
861}
862
863//----------------------------------------------------------------------
864// Dump a Segment to the file handle provided.
865//----------------------------------------------------------------------
866void
Greg Clayton944b8282011-08-22 22:30:57 +0000867DynamicLoaderDarwinKernel::Segment::PutToLog (Log *log, addr_t slide) const
Greg Clayton7b242382011-07-08 00:48:09 +0000868{
869 if (log)
870 {
871 if (slide == 0)
872 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)",
873 name.AsCString(""),
874 vmaddr + slide,
875 vmaddr + slide + vmsize);
876 else
877 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx",
878 name.AsCString(""),
879 vmaddr + slide,
880 vmaddr + slide + vmsize,
881 slide);
882 }
883}
884
Greg Clayton944b8282011-08-22 22:30:57 +0000885const DynamicLoaderDarwinKernel::Segment *
886DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const
Greg Clayton7b242382011-07-08 00:48:09 +0000887{
888 const size_t num_segments = segments.size();
889 for (size_t i=0; i<num_segments; ++i)
890 {
891 if (segments[i].name == name)
892 return &segments[i];
893 }
894 return NULL;
895}
896
897
898//----------------------------------------------------------------------
899// Dump an image info structure to the file handle provided.
900//----------------------------------------------------------------------
901void
Greg Clayton944b8282011-08-22 22:30:57 +0000902DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000903{
904 if (log == NULL)
905 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000906 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000907
908 if (address == LLDB_INVALID_ADDRESS)
909 {
910 if (u)
911 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000912 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 +0000913 u[ 0], u[ 1], u[ 2], u[ 3],
914 u[ 4], u[ 5], u[ 6], u[ 7],
915 u[ 8], u[ 9], u[10], u[11],
916 u[12], u[13], u[14], u[15],
917 name);
918 }
919 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000920 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000921 }
922 else
923 {
924 if (u)
925 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000926 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\"",
927 address, size, version, load_tag, flags, reference_list,
928 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
929 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000930 name);
931 }
932 else
933 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000934 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\"",
935 address, address+size, version, load_tag, flags, reference_list,
936 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000937 }
938 for (uint32_t i=0; i<segments.size(); ++i)
939 segments[i].PutToLog(log, 0);
940 }
941}
942
943//----------------------------------------------------------------------
944// Dump the _dyld_all_image_infos members and all current image infos
945// that we have parsed to the file handle provided.
946//----------------------------------------------------------------------
947void
Greg Clayton944b8282011-08-22 22:30:57 +0000948DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000949{
950 if (log == NULL)
951 return;
952
953 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000954 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000955 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000956 m_kext_summary_header.version,
957 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000958 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000959
960 size_t i;
961 const size_t count = m_kext_summaries.size();
962 if (count > 0)
963 {
964 log->PutCString("Loaded:");
965 for (i = 0; i<count; i++)
966 m_kext_summaries[i].PutToLog(log);
967 }
968}
969
970void
Greg Clayton944b8282011-08-22 22:30:57 +0000971DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000972{
Greg Clayton944b8282011-08-22 22:30:57 +0000973 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000974 Clear(true);
975 m_process = process;
976 m_process->GetTarget().GetSectionLoadList().Clear();
977}
978
Greg Clayton374972e2011-07-09 17:15:55 +0000979void
Greg Clayton944b8282011-08-22 22:30:57 +0000980DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000981{
Greg Clayton374972e2011-07-09 17:15:55 +0000982 if (m_break_id == LLDB_INVALID_BREAK_ID)
983 {
Greg Clayton944b8282011-08-22 22:30:57 +0000984 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000985
Greg Claytond16e1e52011-07-12 17:06:17 +0000986
Greg Clayton374972e2011-07-09 17:15:55 +0000987 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000988 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000989 FileSpecList module_spec_list;
990 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
991 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000992 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000993 "OSKextLoadedKextSummariesUpdated",
994 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000995 internal_bp,
996 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000997
Greg Clayton944b8282011-08-22 22:30:57 +0000998 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000999 m_break_id = bp->GetID();
1000 }
Greg Clayton7b242382011-07-08 00:48:09 +00001001}
1002
1003//----------------------------------------------------------------------
1004// Member function that gets called when the process state changes.
1005//----------------------------------------------------------------------
1006void
Greg Clayton944b8282011-08-22 22:30:57 +00001007DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001008{
Greg Clayton944b8282011-08-22 22:30:57 +00001009 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001010 switch (state)
1011 {
1012 case eStateConnected:
1013 case eStateAttaching:
1014 case eStateLaunching:
1015 case eStateInvalid:
1016 case eStateUnloaded:
1017 case eStateExited:
1018 case eStateDetached:
1019 Clear(false);
1020 break;
1021
1022 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001023 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001024 break;
1025
1026 case eStateRunning:
1027 case eStateStepping:
1028 case eStateCrashed:
1029 case eStateSuspended:
1030 break;
1031
1032 default:
1033 break;
1034 }
1035}
1036
1037ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001038DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001039{
1040 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001041 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1042 if (log)
1043 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001044 return thread_plan_sp;
1045}
1046
1047Error
Greg Clayton944b8282011-08-22 22:30:57 +00001048DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001049{
1050 Error error;
1051 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1052 return error;
1053}
1054
1055void
Greg Clayton944b8282011-08-22 22:30:57 +00001056DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001057{
1058 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1059 GetPluginDescriptionStatic(),
1060 CreateInstance);
1061}
1062
1063void
Greg Clayton944b8282011-08-22 22:30:57 +00001064DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001065{
1066 PluginManager::UnregisterPlugin (CreateInstance);
1067}
1068
1069
1070const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001071DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001072{
1073 return "dynamic-loader.macosx-kernel";
1074}
1075
1076const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001077DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001078{
1079 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1080}
1081
1082
1083//------------------------------------------------------------------
1084// PluginInterface protocol
1085//------------------------------------------------------------------
1086const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001087DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001088{
Greg Clayton944b8282011-08-22 22:30:57 +00001089 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001090}
1091
1092const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001093DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001094{
1095 return GetPluginNameStatic();
1096}
1097
1098uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001099DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001100{
1101 return 1;
1102}
1103