blob: b82d6869edfeba981eed69eec8cb10564398dd36 [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 {
Sean Callanan49bce8e2012-02-10 20:22:35 +000061 create = (object_file->GetStrata() == ObjectFile::eStrataKernel);
Greg Claytondf0b7d52011-07-08 04:11:42 +000062 }
63 }
64
65 if (create)
66 {
67 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
68 create = triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple;
69 }
Greg Clayton7b242382011-07-08 00:48:09 +000070 }
71
72 if (create)
Sean Callanan90539452011-09-20 23:01:51 +000073 {
74 process->SetCanJIT(false);
Greg Clayton944b8282011-08-22 22:30:57 +000075 return new DynamicLoaderDarwinKernel (process);
Sean Callanan90539452011-09-20 23:01:51 +000076 }
Greg Clayton7b242382011-07-08 00:48:09 +000077 return NULL;
78}
79
80//----------------------------------------------------------------------
81// Constructor
82//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000083DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
Greg Clayton7b242382011-07-08 00:48:09 +000084 DynamicLoader(process),
85 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +000086 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +000087 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +000088 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +000089 m_kext_summaries(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000090 m_mutex(Mutex::eMutexTypeRecursive),
91 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +000092{
93}
94
95//----------------------------------------------------------------------
96// Destructor
97//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000098DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +000099{
100 Clear(true);
101}
102
Greg Clayton374972e2011-07-09 17:15:55 +0000103void
Greg Clayton944b8282011-08-22 22:30:57 +0000104DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000105{
106 LoadKernelModuleIfNeeded();
107 SetNotificationBreakpointIfNeeded ();
108}
Greg Clayton7b242382011-07-08 00:48:09 +0000109//------------------------------------------------------------------
110/// Called after attaching a process.
111///
112/// Allow DynamicLoader plug-ins to execute some code after
113/// attaching to a process.
114//------------------------------------------------------------------
115void
Greg Clayton944b8282011-08-22 22:30:57 +0000116DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000117{
118 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000119 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000120}
121
122//------------------------------------------------------------------
123/// Called after attaching a process.
124///
125/// Allow DynamicLoader plug-ins to execute some code after
126/// attaching to a process.
127//------------------------------------------------------------------
128void
Greg Clayton944b8282011-08-22 22:30:57 +0000129DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000130{
131 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000132 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000133}
134
135
136//----------------------------------------------------------------------
137// Clear out the state of this class.
138//----------------------------------------------------------------------
139void
Greg Clayton944b8282011-08-22 22:30:57 +0000140DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000141{
142 Mutex::Locker locker(m_mutex);
143
144 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
145 m_process->ClearBreakpointSiteByID(m_break_id);
146
147 if (clear_process)
148 m_process = NULL;
149 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000150 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000151 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000152 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000153 m_break_id = LLDB_INVALID_BREAK_ID;
154}
155
Greg Clayton7b242382011-07-08 00:48:09 +0000156
157//----------------------------------------------------------------------
158// Load the kernel module and initialize the "m_kernel" member. Return
159// true _only_ if the kernel is loaded the first time through (subsequent
160// calls to this function should return false after the kernel has been
161// already loaded).
162//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000163void
Greg Clayton944b8282011-08-22 22:30:57 +0000164DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000165{
Greg Claytond16e1e52011-07-12 17:06:17 +0000166 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000167 {
168 m_kernel.Clear(false);
169 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
170 if (m_kernel.module_sp)
171 {
172 static ConstString mach_header_name ("_mh_execute_header");
Greg Claytondf0b7d52011-07-08 04:11:42 +0000173 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
174 const Symbol *symbol = NULL;
175 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
176 if (symbol)
Greg Claytond16e1e52011-07-12 17:06:17 +0000177 m_kext_summary_header_ptr_addr = symbol->GetValue();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000178
179 symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (mach_header_name, eSymbolTypeAbsolute);
Greg Clayton7b242382011-07-08 00:48:09 +0000180 if (symbol)
181 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000182 // The "_mh_execute_header" symbol is absolute and not a section based
183 // symbol that will have a valid address, so we need to resolve it...
184 m_process->GetTarget().GetImages().ResolveFileAddress (symbol->GetValue().GetFileAddress(), m_kernel.so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000185 DataExtractor data; // Load command data
Greg Clayton0d9fc762011-07-08 03:21:57 +0000186 if (ReadMachHeader (m_kernel, &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000187 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000188 if (m_kernel.header.filetype == llvm::MachO::HeaderFileTypeExecutable)
Greg Clayton7b242382011-07-08 00:48:09 +0000189 {
190 if (ParseLoadCommands (data, m_kernel))
191 UpdateImageLoadAddress (m_kernel);
192
193 // Update all image infos
Greg Clayton374972e2011-07-09 17:15:55 +0000194 ReadAllKextSummaries ();
Greg Clayton7b242382011-07-08 00:48:09 +0000195 }
196 }
197 else
198 {
199 m_kernel.Clear(false);
200 }
Greg Clayton7b242382011-07-08 00:48:09 +0000201 }
202 }
203 }
Greg Clayton7b242382011-07-08 00:48:09 +0000204}
205
206bool
Greg Clayton944b8282011-08-22 22:30:57 +0000207DynamicLoaderDarwinKernel::FindTargetModule (OSKextLoadedKextSummary &image_info, bool can_create, bool *did_create_ptr)
Greg Clayton7b242382011-07-08 00:48:09 +0000208{
209 if (did_create_ptr)
210 *did_create_ptr = false;
211
212 const bool image_info_uuid_is_valid = image_info.uuid.IsValid();
213
214 if (image_info.module_sp)
215 {
216 if (image_info_uuid_is_valid)
217 {
218 if (image_info.module_sp->GetUUID() == image_info.uuid)
219 return true;
220 else
221 image_info.module_sp.reset();
222 }
223 else
224 return true;
225 }
226
227 ModuleList &target_images = m_process->GetTarget().GetImages();
228 if (image_info_uuid_is_valid)
229 image_info.module_sp = target_images.FindModule(image_info.uuid);
230
231 if (image_info.module_sp)
232 return true;
233
234 ArchSpec arch (image_info.GetArchitecture ());
235 if (can_create)
236 {
237 if (image_info_uuid_is_valid)
238 {
239 image_info.module_sp = m_process->GetTarget().GetSharedModule (FileSpec(),
Greg Claytona63d08c2011-07-19 03:57:15 +0000240 arch,
241 &image_info.uuid);
Greg Clayton7b242382011-07-08 00:48:09 +0000242 if (did_create_ptr)
243 *did_create_ptr = image_info.module_sp;
244 }
245 }
246 return image_info.module_sp;
247}
248
249bool
Greg Clayton944b8282011-08-22 22:30:57 +0000250DynamicLoaderDarwinKernel::UpdateCommPageLoadAddress(Module *module)
Greg Clayton7b242382011-07-08 00:48:09 +0000251{
252 bool changed = false;
253 if (module)
254 {
255 ObjectFile *image_object_file = module->GetObjectFile();
256 if (image_object_file)
257 {
258 SectionList *section_list = image_object_file->GetSectionList ();
259 if (section_list)
260 {
261 uint32_t num_sections = section_list->GetSize();
262 for (uint32_t i=0; i<num_sections; ++i)
263 {
264 Section* section = section_list->GetSectionAtIndex (i).get();
265 if (section)
266 {
267 const addr_t new_section_load_addr = section->GetFileAddress ();
268 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section);
269 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
270 old_section_load_addr != new_section_load_addr)
271 {
272 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress ()))
273 changed = true;
274 }
275 }
276 }
277 }
278 }
279 }
280 return changed;
281}
282
283//----------------------------------------------------------------------
284// Update the load addresses for all segments in MODULE using the
285// updated INFO that is passed in.
286//----------------------------------------------------------------------
287bool
Greg Clayton944b8282011-08-22 22:30:57 +0000288DynamicLoaderDarwinKernel::UpdateImageLoadAddress (OSKextLoadedKextSummary& info)
Greg Clayton7b242382011-07-08 00:48:09 +0000289{
290 Module *module = info.module_sp.get();
291 bool changed = false;
292 if (module)
293 {
294 ObjectFile *image_object_file = module->GetObjectFile();
295 if (image_object_file)
296 {
297 SectionList *section_list = image_object_file->GetSectionList ();
298 if (section_list)
299 {
300 // We now know the slide amount, so go through all sections
301 // and update the load addresses with the correct values.
302 uint32_t num_segments = info.segments.size();
303 for (uint32_t i=0; i<num_segments; ++i)
304 {
Greg Clayton7b242382011-07-08 00:48:09 +0000305 const addr_t new_section_load_addr = info.segments[i].vmaddr;
Greg Claytona63d08c2011-07-19 03:57:15 +0000306 if (section_list->FindSectionByName(info.segments[i].name))
Greg Clayton7b242382011-07-08 00:48:09 +0000307 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000308 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
309 if (section_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000310 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000311 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section_sp.get());
312 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
313 old_section_load_addr != new_section_load_addr)
314 {
315 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), new_section_load_addr))
316 changed = true;
317 }
318 }
319 else
320 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000321 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",
322 info.segments[i].name.AsCString("<invalid>"),
323 (uint64_t)new_section_load_addr,
324 image_object_file->GetFileSpec().GetDirectory().AsCString(),
325 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000326 }
327 }
328 else
329 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000330 // The segment name is empty which means this is a .o file.
331 // Object files in LLDB end up getting reorganized so that
332 // the segment name that is in the section is promoted into
333 // an actual segment, so we just need to go through all sections
334 // and slide them by a single amount.
335
336 uint32_t num_sections = section_list->GetSize();
337 for (uint32_t i=0; i<num_sections; ++i)
338 {
339 Section* section = section_list->GetSectionAtIndex (i).get();
340 if (section)
341 {
342 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress() + new_section_load_addr))
343 changed = true;
344 }
345 }
Greg Clayton7b242382011-07-08 00:48:09 +0000346 }
347 }
348 }
349 }
350 }
351 return changed;
352}
353
354//----------------------------------------------------------------------
355// Update the load addresses for all segments in MODULE using the
356// updated INFO that is passed in.
357//----------------------------------------------------------------------
358bool
Greg Clayton944b8282011-08-22 22:30:57 +0000359DynamicLoaderDarwinKernel::UnloadImageLoadAddress (OSKextLoadedKextSummary& info)
Greg Clayton7b242382011-07-08 00:48:09 +0000360{
361 Module *module = info.module_sp.get();
362 bool changed = false;
363 if (module)
364 {
365 ObjectFile *image_object_file = module->GetObjectFile();
366 if (image_object_file)
367 {
368 SectionList *section_list = image_object_file->GetSectionList ();
369 if (section_list)
370 {
371 uint32_t num_segments = info.segments.size();
372 for (uint32_t i=0; i<num_segments; ++i)
373 {
374 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
375 if (section_sp)
376 {
377 const addr_t old_section_load_addr = info.segments[i].vmaddr;
378 if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr))
379 changed = true;
380 }
381 else
382 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000383 Host::SystemLog (Host::eSystemLogWarning,
384 "warning: unable to find and unload segment named '%s' in '%s/%s' in macosx dynamic loader plug-in.\n",
385 info.segments[i].name.AsCString("<invalid>"),
386 image_object_file->GetFileSpec().GetDirectory().AsCString(),
387 image_object_file->GetFileSpec().GetFilename().AsCString());
Greg Clayton7b242382011-07-08 00:48:09 +0000388 }
389 }
390 }
391 }
392 }
393 return changed;
394}
395
396
397//----------------------------------------------------------------------
398// Static callback function that gets called when our DYLD notification
399// breakpoint gets hit. We update all of our image infos and then
400// let our super class DynamicLoader class decide if we should stop
401// or not (based on global preference).
402//----------------------------------------------------------------------
403bool
Greg Clayton944b8282011-08-22 22:30:57 +0000404DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000405 StoppointCallbackContext *context,
406 user_id_t break_id,
407 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000408{
Greg Clayton944b8282011-08-22 22:30:57 +0000409 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000410}
411
412bool
Greg Clayton944b8282011-08-22 22:30:57 +0000413DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000414 user_id_t break_id,
415 user_id_t break_loc_id)
416{
Greg Claytond16e1e52011-07-12 17:06:17 +0000417 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
418 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000419 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000420
Greg Clayton374972e2011-07-09 17:15:55 +0000421 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000422
423 if (log)
424 PutToLog(log.get());
425
Greg Clayton374972e2011-07-09 17:15:55 +0000426 return GetStopWhenImagesChange();
427}
428
429
430bool
Greg Clayton944b8282011-08-22 22:30:57 +0000431DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000432{
433 Mutex::Locker locker(m_mutex);
434
435 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000436
437 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000438 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000439 {
440 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
441 const ByteOrder byte_order = m_kernel.GetByteOrder();
442 Error error;
443 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
444 // which is currenty 4 uint32_t and a pointer.
445 uint8_t buf[24];
446 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
447 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000448 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000449 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
450 prefer_file_cache,
451 error,
452 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000453 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000454 // We got a valid address for our kext summary header and make sure it isn't NULL
455 if (m_kext_summary_header_addr.IsValid() &&
456 m_kext_summary_header_addr.GetFileAddress() != 0)
457 {
458 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
459 if (bytes_read == count)
460 {
461 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000462 m_kext_summary_header.version = data.GetU32(&offset);
463 if (m_kext_summary_header.version >= 2)
464 {
465 m_kext_summary_header.entry_size = data.GetU32(&offset);
466 }
467 else
468 {
469 // Versions less than 2 didn't have an entry size, it was hard coded
470 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
471 }
472 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000473 return true;
474 }
475 }
Greg Clayton7b242382011-07-08 00:48:09 +0000476 }
477 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000478 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000479 return false;
480}
481
482
483bool
Greg Clayton944b8282011-08-22 22:30:57 +0000484DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000485 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000486{
487 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000488 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000489 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000490 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000491
492 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000493
494 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
495 return false;
496
Greg Clayton07e66e32011-07-20 03:41:06 +0000497 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000498 for (uint32_t i = 0; i < count; i++)
499 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000500 if (s)
501 {
502 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
503 if (u)
504 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000505 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 +0000506 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
507 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
508 kext_summaries[i].address, kext_summaries[i].name);
509 }
510 else
511 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000512 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000513 }
514 }
515
Greg Claytona63d08c2011-07-19 03:57:15 +0000516 DataExtractor data; // Load command data
517 if (ReadMachHeader (kext_summaries[i], &data))
Greg Clayton7b242382011-07-08 00:48:09 +0000518 {
Greg Clayton7b242382011-07-08 00:48:09 +0000519 ParseLoadCommands (data, kext_summaries[i]);
520 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000521
Greg Clayton5b882162011-07-21 01:12:01 +0000522 if (s)
523 {
524 if (kext_summaries[i].module_sp)
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000525 s->Printf("\n found kext: %s/%s\n",
Greg Clayton5b882162011-07-21 01:12:01 +0000526 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
527 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000528 else
529 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000530 }
531
Greg Claytona63d08c2011-07-19 03:57:15 +0000532 if (log)
533 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000534 }
535 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000536 return return_value;
537}
538
539// Adds the modules in image_infos to m_kext_summaries.
540// NB don't call this passing in m_kext_summaries.
541
542bool
Greg Clayton944b8282011-08-22 22:30:57 +0000543DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000544{
545 // Now add these images to the main list.
546 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000547
548 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
549 {
Greg Clayton7b242382011-07-08 00:48:09 +0000550 m_kext_summaries.push_back(image_infos[idx]);
551
552 if (FindTargetModule (image_infos[idx], true, NULL))
553 {
554 // UpdateImageLoadAddress will return true if any segments
555 // change load address. We need to check this so we don't
556 // mention that all loaded shared libraries are newly loaded
557 // each time we hit out dyld breakpoint since dyld will list all
558 // shared libraries each time.
559 if (UpdateImageLoadAddress (image_infos[idx]))
560 {
561 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
562 }
563 }
564 }
565
566 if (loaded_module_list.GetSize() > 0)
567 {
568 // FIXME: This should really be in the Runtime handlers class, which should get
569 // called by the target's ModulesDidLoad, but we're doing it all locally for now
570 // to save time.
571 // Also, I'm assuming there can be only one libobjc dylib loaded...
572
573 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
574 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
575 {
576 size_t num_modules = loaded_module_list.GetSize();
577 for (int i = 0; i < num_modules; i++)
578 {
579 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
580 {
581 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
582 break;
583 }
584 }
585 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000586// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000587// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000588 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
589 }
590 return true;
591}
592
Greg Clayton7b242382011-07-08 00:48:09 +0000593
594uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000595DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000596 uint32_t image_infos_count,
597 OSKextLoadedKextSummary::collection &image_infos)
598{
599 const ByteOrder endian = m_kernel.GetByteOrder();
600 const uint32_t addr_size = m_kernel.GetAddressByteSize();
601
602 image_infos.resize(image_infos_count);
603 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
604 DataBufferHeap data(count, 0);
605 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000606
607 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
608
609 if (s)
610 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000611 const bool prefer_file_cache = false;
612 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
613 prefer_file_cache,
614 data.GetBytes(),
615 data.GetByteSize(),
616 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000617 if (bytes_read == count)
618 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000619
Greg Clayton7b242382011-07-08 00:48:09 +0000620 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
621 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000622 for (uint32_t kext_summary_offset = 0;
623 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
624 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000625 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000626 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000627 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
628 if (name_data == NULL)
629 break;
630 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
631 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
632 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000633 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
634 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000635 image_infos[i].size = extractor.GetU64(&offset);
636 image_infos[i].version = extractor.GetU64(&offset);
637 image_infos[i].load_tag = extractor.GetU32(&offset);
638 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000639 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
640 {
641 image_infos[i].reference_list = extractor.GetU64(&offset);
642 }
643 else
644 {
645 image_infos[i].reference_list = 0;
646 }
Greg Clayton7b242382011-07-08 00:48:09 +0000647 }
648 if (i < image_infos.size())
649 image_infos.resize(i);
650 }
651 else
652 {
653 image_infos.clear();
654 }
655 return image_infos.size();
656}
657
658bool
Greg Clayton944b8282011-08-22 22:30:57 +0000659DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000660{
Greg Clayton374972e2011-07-09 17:15:55 +0000661 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000662
663 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000664
Greg Clayton7b242382011-07-08 00:48:09 +0000665 if (ReadKextSummaryHeader ())
666 {
Greg Clayton374972e2011-07-09 17:15:55 +0000667 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000668 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000669 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000670 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000671 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000672 {
Greg Clayton7b242382011-07-08 00:48:09 +0000673 m_kext_summaries.clear();
674 }
675 return true;
676 }
677 }
678 return false;
679}
680
681//----------------------------------------------------------------------
682// Read a mach_header at ADDR into HEADER, and also fill in the load
683// command data into LOAD_COMMAND_DATA if it is non-NULL.
684//
685// Returns true if we succeed, false if we fail for any reason.
686//----------------------------------------------------------------------
687bool
Greg Clayton944b8282011-08-22 22:30:57 +0000688DynamicLoaderDarwinKernel::ReadMachHeader (OSKextLoadedKextSummary& kext_summary, DataExtractor *load_command_data)
Greg Clayton7b242382011-07-08 00:48:09 +0000689{
690 DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
691 Error error;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000692 const bool prefer_file_cache = false;
693 size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary.so_address,
694 prefer_file_cache,
695 header_bytes.GetBytes(),
696 header_bytes.GetByteSize(),
697 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000698 if (bytes_read == sizeof(llvm::MachO::mach_header))
699 {
700 uint32_t offset = 0;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000701 ::memset (&kext_summary.header, 0, sizeof(kext_summary.header));
Greg Clayton7b242382011-07-08 00:48:09 +0000702
703 // Get the magic byte unswapped so we can figure out what we are dealing with
Greg Clayton374972e2011-07-09 17:15:55 +0000704 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), endian::InlHostByteOrder(), 4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000705 kext_summary.header.magic = data.GetU32(&offset);
706 Address load_cmd_addr = kext_summary.so_address;
Greg Clayton944b8282011-08-22 22:30:57 +0000707 data.SetByteOrder(DynamicLoaderDarwinKernel::GetByteOrderFromMagic(kext_summary.header.magic));
Greg Clayton0d9fc762011-07-08 03:21:57 +0000708 switch (kext_summary.header.magic)
Greg Clayton7b242382011-07-08 00:48:09 +0000709 {
710 case llvm::MachO::HeaderMagic32:
711 case llvm::MachO::HeaderMagic32Swapped:
712 data.SetAddressByteSize(4);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000713 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header));
Greg Clayton7b242382011-07-08 00:48:09 +0000714 break;
715
716 case llvm::MachO::HeaderMagic64:
717 case llvm::MachO::HeaderMagic64Swapped:
718 data.SetAddressByteSize(8);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000719 load_cmd_addr.Slide (sizeof(llvm::MachO::mach_header_64));
Greg Clayton7b242382011-07-08 00:48:09 +0000720 break;
721
722 default:
723 return false;
724 }
725
726 // Read the rest of dyld's mach header
Greg Clayton0d9fc762011-07-08 03:21:57 +0000727 if (data.GetU32(&offset, &kext_summary.header.cputype, (sizeof(llvm::MachO::mach_header)/sizeof(uint32_t)) - 1))
Greg Clayton7b242382011-07-08 00:48:09 +0000728 {
729 if (load_command_data == NULL)
730 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
731
Greg Clayton0d9fc762011-07-08 03:21:57 +0000732 DataBufferSP load_cmd_data_sp(new DataBufferHeap(kext_summary.header.sizeofcmds, 0));
Greg Clayton7b242382011-07-08 00:48:09 +0000733
Greg Clayton0d9fc762011-07-08 03:21:57 +0000734 size_t load_cmd_bytes_read = m_process->GetTarget().ReadMemory (load_cmd_addr,
735 prefer_file_cache,
736 load_cmd_data_sp->GetBytes(),
737 load_cmd_data_sp->GetByteSize(),
738 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000739
Greg Clayton0d9fc762011-07-08 03:21:57 +0000740 if (load_cmd_bytes_read == kext_summary.header.sizeofcmds)
Greg Clayton7b242382011-07-08 00:48:09 +0000741 {
742 // Set the load command data and also set the correct endian
743 // swap settings and the correct address size
Greg Clayton0d9fc762011-07-08 03:21:57 +0000744 load_command_data->SetData(load_cmd_data_sp, 0, kext_summary.header.sizeofcmds);
Greg Clayton7b242382011-07-08 00:48:09 +0000745 load_command_data->SetByteOrder(data.GetByteOrder());
746 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
747 return true; // We successfully read the mach_header and the load command data
748 }
749
750 return false; // We weren't able to read the load command data
751 }
752 }
753 return false; // We failed the read the mach_header
754}
755
756
757//----------------------------------------------------------------------
758// Parse the load commands for an image
759//----------------------------------------------------------------------
760uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000761DynamicLoaderDarwinKernel::ParseLoadCommands (const DataExtractor& data, OSKextLoadedKextSummary& image_info)
Greg Clayton7b242382011-07-08 00:48:09 +0000762{
763 uint32_t offset = 0;
764 uint32_t cmd_idx;
765 Segment segment;
Greg Claytondf0b7d52011-07-08 04:11:42 +0000766 image_info.Clear (true);
Greg Clayton7b242382011-07-08 00:48:09 +0000767
Greg Claytondf0b7d52011-07-08 04:11:42 +0000768 for (cmd_idx = 0; cmd_idx < image_info.header.ncmds; cmd_idx++)
Greg Clayton7b242382011-07-08 00:48:09 +0000769 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000770 // Clear out any load command specific data from image_info since
Greg Clayton7b242382011-07-08 00:48:09 +0000771 // we are about to read it.
772
773 if (data.ValidOffsetForDataOfSize (offset, sizeof(llvm::MachO::load_command)))
774 {
775 llvm::MachO::load_command load_cmd;
776 uint32_t load_cmd_offset = offset;
777 load_cmd.cmd = data.GetU32 (&offset);
778 load_cmd.cmdsize = data.GetU32 (&offset);
779 switch (load_cmd.cmd)
780 {
781 case llvm::MachO::LoadCommandSegment32:
782 {
783 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
784 // We are putting 4 uint32_t values 4 uint64_t values so
785 // we have to use multiple 32 bit gets below.
786 segment.vmaddr = data.GetU32 (&offset);
787 segment.vmsize = data.GetU32 (&offset);
788 segment.fileoff = data.GetU32 (&offset);
789 segment.filesize = data.GetU32 (&offset);
790 // Extract maxprot, initprot, nsects and flags all at once
791 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000792 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000793 }
794 break;
795
796 case llvm::MachO::LoadCommandSegment64:
797 {
798 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
799 // Extract vmaddr, vmsize, fileoff, and filesize all at once
800 data.GetU64(&offset, &segment.vmaddr, 4);
801 // Extract maxprot, initprot, nsects and flags all at once
802 data.GetU32(&offset, &segment.maxprot, 4);
Greg Claytondf0b7d52011-07-08 04:11:42 +0000803 image_info.segments.push_back (segment);
Greg Clayton7b242382011-07-08 00:48:09 +0000804 }
805 break;
806
807 case llvm::MachO::LoadCommandUUID:
Greg Claytondf0b7d52011-07-08 04:11:42 +0000808 image_info.uuid.SetBytes(data.GetData (&offset, 16));
Greg Clayton7b242382011-07-08 00:48:09 +0000809 break;
810
811 default:
812 break;
813 }
814 // Set offset to be the beginning of the next load command.
815 offset = load_cmd_offset + load_cmd.cmdsize;
816 }
817 }
818#if 0
819 // No slide in the kernel...
820
821 // All sections listed in the dyld image info structure will all
822 // either be fixed up already, or they will all be off by a single
823 // slide amount that is determined by finding the first segment
824 // that is at file offset zero which also has bytes (a file size
825 // that is greater than zero) in the object file.
826
827 // Determine the slide amount (if any)
Greg Claytondf0b7d52011-07-08 04:11:42 +0000828 const size_t num_sections = image_info.segments.size();
Greg Clayton7b242382011-07-08 00:48:09 +0000829 for (size_t i = 0; i < num_sections; ++i)
830 {
831 // Iterate through the object file sections to find the
832 // first section that starts of file offset zero and that
833 // has bytes in the file...
Greg Claytondf0b7d52011-07-08 04:11:42 +0000834 if (image_info.segments[i].fileoff == 0 && image_info.segments[i].filesize > 0)
Greg Clayton7b242382011-07-08 00:48:09 +0000835 {
Greg Claytondf0b7d52011-07-08 04:11:42 +0000836 image_info.slide = image_info.address - image_info.segments[i].vmaddr;
Greg Clayton7b242382011-07-08 00:48:09 +0000837 // We have found the slide amount, so we can exit
838 // this for loop.
839 break;
840 }
841 }
842#endif
Greg Claytondf0b7d52011-07-08 04:11:42 +0000843 if (image_info.uuid.IsValid())
844 {
845 bool did_create = false;
846 if (FindTargetModule(image_info, true, &did_create))
847 {
848 if (did_create)
849 image_info.module_create_stop_id = m_process->GetStopID();
850 }
851 }
Greg Clayton7b242382011-07-08 00:48:09 +0000852 return cmd_idx;
853}
854
855//----------------------------------------------------------------------
856// Dump a Segment to the file handle provided.
857//----------------------------------------------------------------------
858void
Greg Clayton944b8282011-08-22 22:30:57 +0000859DynamicLoaderDarwinKernel::Segment::PutToLog (Log *log, addr_t slide) const
Greg Clayton7b242382011-07-08 00:48:09 +0000860{
861 if (log)
862 {
863 if (slide == 0)
864 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx)",
865 name.AsCString(""),
866 vmaddr + slide,
867 vmaddr + slide + vmsize);
868 else
869 log->Printf ("\t\t%16s [0x%16.16llx - 0x%16.16llx) slide = 0x%llx",
870 name.AsCString(""),
871 vmaddr + slide,
872 vmaddr + slide + vmsize,
873 slide);
874 }
875}
876
Greg Clayton944b8282011-08-22 22:30:57 +0000877const DynamicLoaderDarwinKernel::Segment *
878DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::FindSegment (const ConstString &name) const
Greg Clayton7b242382011-07-08 00:48:09 +0000879{
880 const size_t num_segments = segments.size();
881 for (size_t i=0; i<num_segments; ++i)
882 {
883 if (segments[i].name == name)
884 return &segments[i];
885 }
886 return NULL;
887}
888
889
890//----------------------------------------------------------------------
891// Dump an image info structure to the file handle provided.
892//----------------------------------------------------------------------
893void
Greg Clayton944b8282011-08-22 22:30:57 +0000894DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000895{
896 if (log == NULL)
897 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000898 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000899
900 if (address == LLDB_INVALID_ADDRESS)
901 {
902 if (u)
903 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000904 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 +0000905 u[ 0], u[ 1], u[ 2], u[ 3],
906 u[ 4], u[ 5], u[ 6], u[ 7],
907 u[ 8], u[ 9], u[10], u[11],
908 u[12], u[13], u[14], u[15],
909 name);
910 }
911 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000912 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000913 }
914 else
915 {
916 if (u)
917 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000918 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\"",
919 address, size, version, load_tag, flags, reference_list,
920 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
921 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000922 name);
923 }
924 else
925 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000926 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\"",
927 address, address+size, version, load_tag, flags, reference_list,
928 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000929 }
930 for (uint32_t i=0; i<segments.size(); ++i)
931 segments[i].PutToLog(log, 0);
932 }
933}
934
935//----------------------------------------------------------------------
936// Dump the _dyld_all_image_infos members and all current image infos
937// that we have parsed to the file handle provided.
938//----------------------------------------------------------------------
939void
Greg Clayton944b8282011-08-22 22:30:57 +0000940DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000941{
942 if (log == NULL)
943 return;
944
945 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000946 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000947 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000948 m_kext_summary_header.version,
949 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000950 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000951
952 size_t i;
953 const size_t count = m_kext_summaries.size();
954 if (count > 0)
955 {
956 log->PutCString("Loaded:");
957 for (i = 0; i<count; i++)
958 m_kext_summaries[i].PutToLog(log);
959 }
960}
961
962void
Greg Clayton944b8282011-08-22 22:30:57 +0000963DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000964{
Greg Clayton944b8282011-08-22 22:30:57 +0000965 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000966 Clear(true);
967 m_process = process;
968 m_process->GetTarget().GetSectionLoadList().Clear();
969}
970
Greg Clayton374972e2011-07-09 17:15:55 +0000971void
Greg Clayton944b8282011-08-22 22:30:57 +0000972DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000973{
Greg Clayton374972e2011-07-09 17:15:55 +0000974 if (m_break_id == LLDB_INVALID_BREAK_ID)
975 {
Greg Clayton944b8282011-08-22 22:30:57 +0000976 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000977
Greg Claytond16e1e52011-07-12 17:06:17 +0000978
Greg Clayton374972e2011-07-09 17:15:55 +0000979 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000980 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000981 FileSpecList module_spec_list;
982 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
983 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000984 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000985 "OSKextLoadedKextSummariesUpdated",
986 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000987 internal_bp,
988 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000989
Greg Clayton944b8282011-08-22 22:30:57 +0000990 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000991 m_break_id = bp->GetID();
992 }
Greg Clayton7b242382011-07-08 00:48:09 +0000993}
994
995//----------------------------------------------------------------------
996// Member function that gets called when the process state changes.
997//----------------------------------------------------------------------
998void
Greg Clayton944b8282011-08-22 22:30:57 +0000999DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001000{
Greg Clayton944b8282011-08-22 22:30:57 +00001001 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001002 switch (state)
1003 {
1004 case eStateConnected:
1005 case eStateAttaching:
1006 case eStateLaunching:
1007 case eStateInvalid:
1008 case eStateUnloaded:
1009 case eStateExited:
1010 case eStateDetached:
1011 Clear(false);
1012 break;
1013
1014 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001015 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001016 break;
1017
1018 case eStateRunning:
1019 case eStateStepping:
1020 case eStateCrashed:
1021 case eStateSuspended:
1022 break;
1023
1024 default:
1025 break;
1026 }
1027}
1028
1029ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001030DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001031{
1032 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +00001033 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1034 if (log)
1035 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001036 return thread_plan_sp;
1037}
1038
1039Error
Greg Clayton944b8282011-08-22 22:30:57 +00001040DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001041{
1042 Error error;
1043 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1044 return error;
1045}
1046
1047void
Greg Clayton944b8282011-08-22 22:30:57 +00001048DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001049{
1050 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1051 GetPluginDescriptionStatic(),
1052 CreateInstance);
1053}
1054
1055void
Greg Clayton944b8282011-08-22 22:30:57 +00001056DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001057{
1058 PluginManager::UnregisterPlugin (CreateInstance);
1059}
1060
1061
1062const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001063DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001064{
1065 return "dynamic-loader.macosx-kernel";
1066}
1067
1068const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001069DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001070{
1071 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1072}
1073
1074
1075//------------------------------------------------------------------
1076// PluginInterface protocol
1077//------------------------------------------------------------------
1078const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001079DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001080{
Greg Clayton944b8282011-08-22 22:30:57 +00001081 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +00001082}
1083
1084const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001085DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001086{
1087 return GetPluginNameStatic();
1088}
1089
1090uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001091DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001092{
1093 return 1;
1094}
1095